LeechCraft  0.6.70-10870-g558588d6ec
Modular cross-platform feature rich live environment.
util.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 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 #include "util.h"
31 #include <functional>
32 #include <stdexcept>
33 #include <type_traits>
34 #include <QString>
35 #include <QApplication>
36 #include <QTranslator>
37 #include <QLocale>
38 #include <QFile>
39 #include <QDir>
40 #include <QTime>
41 #include <QSettings>
42 #include <QTextCodec>
43 #include <QUrl>
44 #include <QAction>
45 #include <QBuffer>
46 #include <QPainter>
47 #include <QAction>
48 #include <QtDebug>
49 
50 QString LeechCraft::Util::GetAsBase64Src (const QImage& pix)
51 {
52  QBuffer buf;
53  buf.open (QIODevice::ReadWrite);
54  pix.save (&buf, "PNG", 100);
55  return QString ("data:image/png;base64,%1")
56  .arg (QString (buf.buffer ().toBase64 ()));
57 }
58 
60 {
61  QString string = QObject::tr ("Too long to show");
62  if (p.Additional_.contains ("UserVisibleName") &&
63  p.Additional_ ["UserVisibleName"].canConvert<QString> ())
64  string = p.Additional_ ["UserVisibleName"].toString ();
65  else if (p.Entity_.canConvert<QByteArray> ())
66  {
67  QByteArray entity = p.Entity_.toByteArray ();
68  if (entity.size () < 100)
69  string = QTextCodec::codecForName ("UTF-8")->toUnicode (entity);
70  }
71  else if (p.Entity_.canConvert<QUrl> ())
72  {
73  string = p.Entity_.toUrl ().toString ();
74  if (string.size () > 100)
75  string = string.left (97) + "...";
76  }
77  else
78  string = QObject::tr ("Binary entity");
79 
80  if (!p.Mime_.isEmpty ())
81  string += QObject::tr ("<br /><br />of type <code>%1</code>").arg (p.Mime_);
82 
83  if (!p.Additional_ ["SourceURL"].toUrl ().isEmpty ())
84  {
85  QString urlStr = p.Additional_ ["SourceURL"].toUrl ().toString ();
86  if (urlStr.size () > 63)
87  urlStr = urlStr.left (60) + "...";
88  string += QObject::tr ("<br />from %1")
89  .arg (urlStr);
90  }
91 
92  return string;
93 }
94 
95 namespace
96 {
97  QString MakePrettySizeWith (qint64 sourceSize, const QStringList& units)
98  {
99  int strNum = 0;
100  long double size = sourceSize;
101 
102  for (; strNum < 3 && size >= 1024; ++strNum, size /= 1024)
103  ;
104 
105  return QString::number (size, 'f', 1) + units.value (strNum);
106  }
107 }
108 
109 QString LeechCraft::Util::MakePrettySize (qint64 sourcesize)
110 {
111  static QStringList units
112  {
113  QObject::tr (" b"),
114  QObject::tr (" KiB"),
115  QObject::tr (" MiB"),
116  QObject::tr (" GiB")
117  };
118 
119  return MakePrettySizeWith (sourcesize, units);
120 }
121 
122 QString LeechCraft::Util::MakePrettySizeShort (qint64 sourcesize)
123 {
124  static const QStringList units
125  {
126  QObject::tr ("b", "Short one-character unit for bytes."),
127  QObject::tr ("K", "Short one-character unit for kilobytes."),
128  QObject::tr ("M", "Short one-character unit for megabytes."),
129  QObject::tr ("G", "Short one-character unit for gigabytes.")
130  };
131 
132  return MakePrettySizeWith (sourcesize, units);
133 }
134 
136 {
137  int d = time / 86400;
138  time -= d * 86400;
139  QString result;
140  if (d)
141  result += QObject::tr ("%n day(s), ", "", d);
142  result += QTime (0, 0, 0).addSecs (time).toString ();
143  return result;
144 }
145 
146 QTranslator* LeechCraft::Util::LoadTranslator (const QString& baseName,
147  const QString& localeName,
148  const QString& prefix,
149  const QString& appName)
150 {
151  auto filename = prefix;
152  filename.append ("_");
153  if (!baseName.isEmpty ())
154  filename.append (baseName).append ("_");
155  filename.append (localeName);
156 
157  auto transl = new QTranslator;
158 #ifdef Q_OS_WIN32
159  if (transl->load (filename, ":/") ||
160  transl->load (filename,
161  QCoreApplication::applicationDirPath () + "/translations"))
162 #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
163  if (transl->load (filename, ":/") ||
164  transl->load (filename,
165  QCoreApplication::applicationDirPath () + "/../Resources/translations"))
166 #elif defined (INSTALL_PREFIX)
167  if (transl->load (filename, ":/") ||
168  transl->load (filename,
169  QString (INSTALL_PREFIX "/share/%1/translations").arg (appName)))
170 #else
171  if (transl->load (filename, ":/") ||
172  transl->load (filename,
173  QString ("/usr/local/share/%1/translations").arg (appName)) ||
174  transl->load (filename,
175  QString ("/usr/share/%1/translations").arg (appName)))
176 #endif
177  return transl;
178 
179  delete transl;
180 
181  return nullptr;
182 }
183 
184 QTranslator* LeechCraft::Util::InstallTranslator (const QString& baseName,
185  const QString& prefix,
186  const QString& appName)
187 {
188  const auto& localeName = GetLocaleName ();
189  if (auto transl = LoadTranslator (baseName, localeName, prefix, appName))
190  {
191  qApp->installTranslator (transl);
192  return transl;
193  }
194 
195  qWarning () << Q_FUNC_INFO
196  << "could not load translation file for locale"
197  << localeName
198  << baseName
199  << prefix
200  << appName;
201  return nullptr;
202 }
203 
205 {
206  QSettings settings (QCoreApplication::organizationName (),
207  QCoreApplication::applicationName ());
208  QString localeName = settings.value ("Language", "system").toString ();
209 
210  if (localeName == "system")
211  {
212  localeName = QString (::getenv ("LANG")).left (5);
213 
214  if (localeName == "C" || localeName.isEmpty ())
215  localeName = "en_US";
216 
217  if (localeName.isEmpty () || localeName.size () != 5)
218  localeName = QLocale::system ().name ();
219  localeName = localeName.left (5);
220  }
221 
222  if (localeName.size () == 2)
223  {
224  auto lang = QLocale (localeName).language ();
225  const auto& cs = QLocale::countriesForLanguage (lang);
226  if (cs.isEmpty ())
227  localeName += "_00";
228  else
229  localeName = QLocale (lang, cs.at (0)).name ();
230  }
231 
232  return localeName;
233 }
234 
235 QString LeechCraft::Util::GetInternetLocaleName (const QLocale& locale)
236 {
237  if (locale.language () == QLocale::AnyLanguage)
238  return "*";
239 
240  auto locStr = locale.name ();
241  locStr.replace ('_', '-');
242  return locStr;
243 }
244 
246 {
247  return GetLocaleName ().left (2);
248 }
249 
250 QModelIndexList LeechCraft::Util::GetSummarySelectedRows (QObject *sender)
251 {
252  QAction *senderAct = qobject_cast<QAction*> (sender);
253  if (!senderAct)
254  {
255  QString debugString;
256  {
257  QDebug d (&debugString);
258  d << "sender is not a QAction*"
259  << sender;
260  }
261  throw std::runtime_error (qPrintable (debugString));
262  }
263 
264  return senderAct->
265  property ("SelectedRows").value<QList<QModelIndex>> ();
266 }
267 
268 QAction* LeechCraft::Util::CreateSeparator (QObject *parent)
269 {
270  QAction *result = new QAction (parent);
271  result->setSeparator (true);
272  return result;
273 }
274 
276  const QString& text, QFont font, const QPen& pen, const QBrush& brush)
277 {
278  const auto& iconSize = px.size ();
279 
280  const auto fontHeight = px.height () * 0.45;
281  font.setPixelSize (std::max (6., fontHeight));
282 
283  const QFontMetrics fm (font);
284  const auto width = fm.width (text) + 2. * px.width () / 10.;
285  const auto height = fm.height () + 2. * px.height () / 10.;
286  const bool tooSmall = width > iconSize.width ();
287 
288  const QRect textRect (iconSize.width () - width, iconSize.height () - height, width, height);
289 
290  QPainter p (&px);
291  p.setBrush (brush);
292  p.setFont (font);
293  p.setPen (pen);
294  p.setRenderHint (QPainter::Antialiasing);
295  p.setRenderHint (QPainter::TextAntialiasing);
296  p.setRenderHint (QPainter::HighQualityAntialiasing);
297  p.drawRoundedRect (textRect, 4, 4);
298  p.drawText (textRect,
299  Qt::AlignCenter,
300  tooSmall ? "#" : text);
301  p.end ();
302 
303  return px;
304 }
UTIL_API QString MakePrettySize(qint64 sourceSize)
Makes a formatted size from number.
Definition: util.cpp:109
UTIL_API QTranslator * LoadTranslator(const QString &base, const QString &locale, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Definition: util.cpp:146
UTIL_API QString MakePrettySizeShort(qint64 size)
Converts a bytes count to a string representation with appropriately chosen units.
Definition: util.cpp:122
UTIL_API QString GetLocaleName()
Returns the current locale name, like en_US.
Definition: util.cpp:204
UTIL_API QString MakeTimeFromLong(ulong time)
Makes a formatted time from number.
Definition: util.cpp:135
UTIL_API QString GetLanguage()
Returns the current language name.
Definition: util.cpp:245
UTIL_API QString GetInternetLocaleName(const QLocale &)
Definition: util.cpp:235
UTIL_API QTranslator * InstallTranslator(const QString &base, const QString &prefix="leechcraft", const QString &appname="leechcraft")
Loads and installs a translator.
Definition: util.cpp:184
UTIL_API QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
Definition: util.cpp:268
UTIL_API QString GetUserText(const Entity &entity)
Return the user-readable representation of the entity.
Definition: util.cpp:59
UTIL_API QPixmap DrawOverlayText(QPixmap px, const QString &text, QFont font, const QPen &pen, const QBrush &brush)
Definition: util.cpp:275
UTIL_API QString GetAsBase64Src(const QImage &image)
Returns the given image in a Base64-encoded form.
Definition: util.cpp:50
UTIL_API QModelIndexList GetSummarySelectedRows(QObject *sender)
Definition: util.cpp:250
QMap< QString, QVariant > Additional_
Additional parameters.
Definition: structures.h:188
QString Mime_
MIME type of the entity.
Definition: structures.h:172
QVariant Entity_
The entity that this object represents.
Definition: structures.h:136
A message used for inter-plugin communication.
Definition: structures.h:119