LeechCraft  %{LEECHCRAFT_VERSION}
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Pages
util.h
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2013 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #ifndef UTIL_UTIL_H
31 #define UTIL_UTIL_H
32 #include "utilconfig.h"
33 #include <QString>
34 #include <QDir>
35 #include <QModelIndex>
36 #include <QtXml/QDomElement>
37 #include <QtDebug>
38 #include <interfaces/structures.h>
39 
40 class QTranslator;
41 class QLocale;
42 
43 namespace LeechCraft
44 {
45  namespace Util
46  {
53  inline QString FromStdString (const std::string& str)
54  {
55  return QString::fromUtf8 (str.c_str ());
56  }
57 
58  template<typename T>
59  struct ValueFinder
60  {
61  typedef typename T::data_type data_type;
63 
65  : Object_ (data)
66  {
67  }
68 
69  bool operator() (typename T::value_type i)
70  {
71  return i.second == Object_;
72  }
73  };
74 
84  UTIL_API QString GetAsBase64Src (const QImage& image);
85 
92  UTIL_API QString GetUserText (const Entity& entity);
93 
104  UTIL_API QString MakePrettySize (qint64);
105 
115  UTIL_API QString MakeTimeFromLong (ulong);
116 
135  UTIL_API QTranslator* InstallTranslator (const QString& base,
136  const QString& prefix = "leechcraft",
137  const QString& appname = "leechcraft");
138 
157  UTIL_API QString GetLocaleName ();
158 
159  UTIL_API QString GetInternetLocaleName (const QLocale&);
160 
170  UTIL_API QString GetLanguage ();
171 
183  UTIL_API QDir CreateIfNotExists (const QString& path);
184 
195  UTIL_API QDir GetUserDir (const QString& path);
196 
205  UTIL_API QString GetTemporaryName (const QString& pattern = QString ("lc_temp.XXXXXX"));
206 
224  UTIL_API Entity MakeEntity (const QVariant& entity,
225  const QString& location,
226  LeechCraft::TaskParameters tp,
227  const QString& mime = QString ());
228 
248  UTIL_API Entity MakeNotification (const QString& header,
249  const QString& text,
250  Priority priority);
251 
263  UTIL_API Entity MakeANCancel (const Entity& event);
264 
283  UTIL_API Entity MakeANCancel (const QString& senderId, const QString& eventId);
284 
285  UTIL_API QModelIndexList GetSummarySelectedRows (QObject *sender);
286 
294  UTIL_API QAction* CreateSeparator (QObject *parent);
295 
296  UTIL_API QVariantList GetPersistentData (const QList<QVariant>& keys,
297  QObject *object);
298 
299  UTIL_API QPixmap DrawOverlayText (QPixmap px, const QString& text, QFont font, const QPen& pen, const QBrush& brush);
300 
333  template<typename TagGetter, typename TagSetter>
334  QDomElement GetElementForTags (const QStringList& tags,
335  QDomNode& node,
336  QDomDocument& document,
337  const QString& elementName,
338  TagGetter tagGetter,
339  TagSetter tagSetter)
340  {
341  if (!tags.size ())
342  {
343  qWarning () << Q_FUNC_INFO
344  << "no tags"
345  << elementName;
346  return node.toElement ();
347  }
348 
349  QDomNodeList elements = node.childNodes ();
350  for (int i = 0; i < elements.size (); ++i)
351  {
352  QDomElement elem = elements.at (i).toElement ();
353  if (tagGetter (elem) == tags.at (0))
354  {
355  if (tags.size () > 1)
356  {
357  QStringList childTags = tags;
358  childTags.removeAt (0);
359  return GetElementForTags (childTags, elem,
360  document, elementName,
361  tagGetter, tagSetter);
362  }
363  else
364  return elem;
365  }
366  }
367 
368  QDomElement result = document.createElement (elementName);
369  tagSetter (result, tags.at (0));
370  node.appendChild (result);
371  if (tags.size () > 1)
372  {
373  QStringList childTags = tags;
374  childTags.removeAt (0);
375  return GetElementForTags (childTags, result,
376  document, elementName,
377  tagGetter, tagSetter);
378  }
379  else
380  return result;
381  }
382 
383  template<typename K, typename V>
384  QMap<K, V> MakeMap (std::initializer_list<QPair<K, V>> l)
385  {
386  QMap<K, V> result;
387  for (const auto& pair : l)
388  result [pair.first] = pair.second;
389  return result;
390  }
391  };
392 };
393 
394 #endif
395 
UTIL_API QVariantList GetPersistentData(const QList< QVariant > &keys, QObject *object)
Definition: util.cpp:347
bool operator()(typename T::value_type i)
Definition: util.h:69
#define UTIL_API
Definition: utilconfig.h:37
UTIL_API QDir GetUserDir(const QString &path)
Returns the path relative to user directory.
Definition: util.cpp:252
UTIL_API QString GetLocaleName()
Returns the current locale name, like en_US.
Definition: util.cpp:189
T::data_type data_type
Definition: util.h:61
UTIL_API QString GetTemporaryName(const QString &pattern=QString("lc_temp.XXXXXX"))
Returns a temporary filename.
Definition: util.cpp:269
UTIL_API QString MakePrettySize(qint64)
Makes a formatted size from number.
Definition: util.cpp:99
UTIL_API QDir CreateIfNotExists(const QString &path)
Creates a path if it doesn&#39;t exist.
Definition: util.cpp:233
UTIL_API QString GetLanguage()
Returns the current language name.
Definition: util.cpp:228
UTIL_API QString GetInternetLocaleName(const QLocale &)
Definition: util.cpp:216
UTIL_API QTranslator * InstallTranslator(const QString &base, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Loads and installs a translator.
Definition: util.cpp:145
UTIL_API QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
Definition: util.cpp:340
UTIL_API QString GetUserText(const Entity &entity)
Return the user-readable representation of the entity.
Definition: util.cpp:63
UTIL_API QString MakeTimeFromLong(ulong)
Makes a formatted time from number.
Definition: util.cpp:134
UTIL_API QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
Definition: util.cpp:366
ValueFinder(data_type data)
Definition: util.h:64
UTIL_API QString GetAsBase64Src(const QImage &image)
Returns the given image in a Base64-encoded form.
Definition: util.cpp:54
UTIL_API QModelIndexList GetSummarySelectedRows(QObject *sender)
Definition: util.cpp:322
UTIL_API Entity MakeANCancel(const Entity &event)
Makes an event for canceling another Advanced Notifications event.
Definition: util.cpp:304
UTIL_API Entity MakeEntity(const QVariant &entity, const QString &location, LeechCraft::TaskParameters tp, const QString &mime=QString())
An utility function to make a Entity.
Definition: util.cpp:279
UTIL_API Entity MakeNotification(const QString &header, const QString &text, Priority priority)
An utility function to make a Entity with notification.
Definition: util.cpp:292
Describes parameters of an entity.
Definition: structures.h:157
QMap< K, V > MakeMap(std::initializer_list< QPair< K, V >> l)
Definition: util.h:384
QDomElement GetElementForTags(const QStringList &tags, QDomNode &node, QDomDocument &document, const QString &elementName, TagGetter tagGetter, TagSetter tagSetter)
Returns an element for a given tags list.
Definition: util.h:334
QString FromStdString(const std::string &str)
An utility function that creates a QString from UTF8-encoded std::string.
Definition: util.h:53