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