LeechCraft  0.6.70-18450-gabe19ee3b0
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  * Distributed under the Boost Software License, Version 1.0.
6  * (See accompanying file LICENSE or copy at https://www.boost.org/LICENSE_1_0.txt)
7  **********************************************************************/
8 
9 #include "util.h"
10 #include <stdexcept>
11 #include <QString>
12 #include <QApplication>
13 #include <QTranslator>
14 #include <QLocale>
15 #include <QTime>
16 #include <QSettings>
17 #include <QUrl>
18 #include <QAction>
19 #include <QBuffer>
20 #include <QAction>
21 #include <QModelIndexList>
22 #include <QtDebug>
23 #include <util/sll/qtutil.h>
24 
25 namespace LC::Util
26 {
27  const QString LCLowercase = QStringLiteral ("leechcraft");
28 
29  QString GetAsBase64Src (const QImage& pix)
30  {
31  QBuffer buf;
32  buf.open (QIODevice::ReadWrite);
33  const auto compression = 100;
34  pix.save (&buf, "PNG", compression);
35  return QStringLiteral ("data:image/png;base64,") + buf.buffer ().toBase64 ();
36  }
37 
38  namespace
39  {
40  QString MakePrettySizeWith (qint64 sourceSize, const QStringList& units, const QString& space)
41  {
42  int strNum = 0;
43  long double size = sourceSize;
44 
45  for (; strNum < 3 && size >= 1024; ++strNum, size /= 1024)
46  ;
47 
48  return GetLocale ().toString (static_cast<double> (size), 'f', 1) + space + units.value (strNum);
49  }
50  }
51 
52  QString MakePrettySize (qint64 sourcesize)
53  {
54  static QStringList units
55  {
56  QObject::tr ("B", "The unit for bytes."),
57  QObject::tr ("KiB", "The unit for kilobytes."),
58  QObject::tr ("MiB", "The unit for megabytes."),
59  QObject::tr ("GiB", "The unit for gigabytes.")
60  };
61 
62  return MakePrettySizeWith (sourcesize, units, " "_qs);
63  }
64 
65  QString MakePrettySizeShort (qint64 sourcesize)
66  {
67  static const QStringList units
68  {
69  QObject::tr ("B", "Short one-character unit for bytes."),
70  QObject::tr ("K", "Short one-character unit for kilobytes."),
71  QObject::tr ("M", "Short one-character unit for megabytes."),
72  QObject::tr ("G", "Short one-character unit for gigabytes.")
73  };
74 
75  return MakePrettySizeWith (sourcesize, units, {});
76  }
77 
78  QString MakeTimeFromLong (ulong time)
79  {
80  const auto secsPerDay = 86400;
81  int d = time / secsPerDay;
82  time -= d * secsPerDay;
83  QString result;
84  if (d)
85  result += QObject::tr ("%n day(s), ", "", d);
86  result += QTime (0, 0, 0).addSecs (time).toString ();
87  return result;
88  }
89 
90  QTranslator* LoadTranslator (const QString& baseName,
91  const QString& localeName,
92  const QString& prefix,
93  const QString& appName)
94  {
95  auto filename = prefix;
96  filename.append ("_");
97  if (!baseName.isEmpty ())
98  filename.append (baseName).append ("_");
99  filename.append (localeName);
100 
101  auto transl = new QTranslator;
102  #ifdef Q_OS_WIN32
103  Q_UNUSED (appName)
104  if (transl->load (filename, ":/") ||
105  transl->load (filename,
106  QCoreApplication::applicationDirPath () + "/translations"))
107  #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
108  Q_UNUSED (appName)
109  if (transl->load (filename, ":/") ||
110  transl->load (filename,
111  QCoreApplication::applicationDirPath () + "/../Resources/translations"))
112  #elif defined (INSTALL_PREFIX)
113  if (transl->load (filename, ":/") ||
114  transl->load (filename,
115  QString (INSTALL_PREFIX "/share/%1/translations").arg (appName)))
116  #else
117  if (transl->load (filename, ":/") ||
118  transl->load (filename,
119  QString ("/usr/local/share/%1/translations").arg (appName)) ||
120  transl->load (filename,
121  QString ("/usr/share/%1/translations").arg (appName)))
122  #endif
123  return transl;
124 
125  delete transl;
126 
127  return nullptr;
128  }
129 
130  QTranslator* InstallTranslator (const QString& baseName,
131  const QString& prefix,
132  const QString& appName)
133  {
134  const auto& localeName = GetLocaleName ();
135  if (auto transl = LoadTranslator (baseName, localeName, prefix, appName))
136  {
137  QCoreApplication::installTranslator (transl);
138  return transl;
139  }
140 
141  qWarning () << Q_FUNC_INFO
142  << "could not load translation file for locale"
143  << localeName
144  << baseName
145  << prefix
146  << appName;
147  return nullptr;
148  }
149 
150  QLocale GetLocale ()
151  {
152  const QSettings settings { QCoreApplication::organizationName (), QCoreApplication::applicationName () };
153  const auto& localeName = settings.value ("Language"_qs, "system"_qs).toString ();
154  if (localeName == "system"_ql)
155  return QLocale {};
156 
157  QLocale locale { localeName };
158  if (locale.language () == QLocale::AnyLanguage)
159  {
160  qWarning () << "unable to parse" << localeName;
161  return QLocale {};
162  }
163  return locale;
164  }
165 
166  QString GetLocaleName ()
167  {
168  return GetLocale ().name ();
169  }
170 
171  QString GetInternetLocaleName (const QLocale& locale)
172  {
173  if (locale.language () == QLocale::AnyLanguage)
174  return QStringLiteral ("*");
175 
176  auto locStr = locale.name ();
177  locStr.replace ('_', '-');
178  return locStr;
179  }
180 
181  QString GetLanguage ()
182  {
183  return GetLocaleName ().left (2);
184  }
185 
186  QAction* CreateSeparator (QObject *parent)
187  {
188  const auto result = new QAction (parent);
189  result->setSeparator (true);
190  return result;
191  }
192 }
QString GetAsBase64Src(const QImage &pix)
Returns the given image in a Base64-encoded form.
Definition: util.cpp:29
QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
Definition: util.cpp:186
QString GetLanguage()
Returns the current language name.
Definition: util.cpp:181
QLocale GetLocale()
Definition: util.cpp:150
QString MakePrettySize(qint64 sourcesize)
Makes a formatted size from number.
Definition: util.cpp:52
QString GetLocaleName()
Returns the current locale name, like en_US.
Definition: util.cpp:166
QString MakeTimeFromLong(ulong time)
Makes a formatted time from number.
Definition: util.cpp:78
QString MakePrettySizeShort(qint64 sourcesize)
Converts a bytes count to a string representation with appropriately chosen units.
Definition: util.cpp:65
QTranslator * InstallTranslator(const QString &baseName, const QString &prefix, const QString &appName)
Loads and installs a translator.
Definition: util.cpp:130
const QString LCLowercase
The "leechcraft" literal, with no run-time overhead.
Definition: util.cpp:27
QTranslator * LoadTranslator(const QString &baseName, const QString &localeName, const QString &prefix, const QString &appName)
Definition: util.cpp:90
QString GetInternetLocaleName(const QLocale &locale)
Definition: util.cpp:171