LeechCraft 0.6.70-17335-ge406ffdcaf
Modular cross-platform feature rich live environment.
Loading...
Searching...
No Matches
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
25namespace 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)
41 {
42 int strNum = 0;
43 long double size = sourceSize;
44
45 for (; strNum < 3 && size >= 1024; ++strNum, size /= 1024)
46 ;
47
48 return QString::number (size, 'f', 1) + units.value (strNum);
49 }
50 }
51
52 QString MakePrettySize (qint64 sourcesize)
53 {
54 static QStringList units
55 {
56 QObject::tr (" b"),
57 QObject::tr (" KiB"),
58 QObject::tr (" MiB"),
59 QObject::tr (" GiB")
60 };
61
62 return MakePrettySizeWith (sourcesize, units);
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 QString GetLocaleName ()
151 {
152 QSettings settings (QCoreApplication::organizationName (),
153 QCoreApplication::applicationName ());
154 QString localeName = settings.value (QStringLiteral ("Language"), QStringLiteral ("system")).toString ();
155
156 if (localeName == "system"_ql)
157 {
158 const auto localeLen = 5;
159 localeName = qEnvironmentVariable ("LANG").left (localeLen);
160
161 if (localeName == "C"_ql || localeName.isEmpty ())
162 localeName = QStringLiteral ("en_US");
163
164 if (localeName.isEmpty () || localeName.size () != localeLen)
165 localeName = QLocale::system ().name ().left (localeLen);
166 }
167
168 if (localeName.size () == 2)
169 {
170 auto lang = QLocale (localeName).language ();
171 const auto& cs = QLocale::countriesForLanguage (lang);
172 if (cs.isEmpty ())
173 localeName += "_00"_ql;
174 else
175 localeName = QLocale (lang, cs.at (0)).name ();
176 }
177
178 return localeName;
179 }
180
181 QString GetInternetLocaleName (const QLocale& locale)
182 {
183 if (locale.language () == QLocale::AnyLanguage)
184 return QStringLiteral ("*");
185
186 auto locStr = locale.name ();
187 locStr.replace ('_', '-');
188 return locStr;
189 }
190
191 QString GetLanguage ()
192 {
193 return GetLocaleName ().left (2);
194 }
195
196 QModelIndexList GetSummarySelectedRows (QObject *sender)
197 {
198 const auto senderAct = qobject_cast<QAction*> (sender);
199 if (!senderAct)
200 {
201 QString debugString;
202 {
203 QDebug d (&debugString);
204 d << "sender is not a QAction*"
205 << sender;
206 }
207 throw std::runtime_error (qPrintable (debugString));
208 }
209
210 return senderAct->property ("SelectedRows").value<QList<QModelIndex>> ();
211 }
212
213 QAction* CreateSeparator (QObject *parent)
214 {
215 const auto result = new QAction (parent);
216 result->setSeparator (true);
217 return result;
218 }
219}
QString MakePrettySizeShort(qint64 sourcesize)
Converts a bytes count to a string representation with appropriately chosen units.
Definition util.cpp:65
QString GetLocaleName()
Returns the current locale name, like en_US.
Definition util.cpp:150
QTranslator * LoadTranslator(const QString &baseName, const QString &localeName, const QString &prefix, const QString &appName)
Definition util.cpp:90
QString MakeTimeFromLong(ulong time)
Makes a formatted time from number.
Definition util.cpp:78
QModelIndexList GetSummarySelectedRows(QObject *sender)
Definition util.cpp:196
QString GetAsBase64Src(const QImage &pix)
Returns the given image in a Base64-encoded form.
Definition util.cpp:29
QString GetLanguage()
Returns the current language name.
Definition util.cpp:191
QAction * CreateSeparator(QObject *parent)
Returns the action that is set to act as a separator.
Definition util.cpp:213
const QString LCLowercase
The "leechcraft" literal, with no run-time overhead.
Definition util.cpp:27
QString MakePrettySize(qint64 sourcesize)
Makes a formatted size from number.
Definition util.cpp:52
QTranslator * InstallTranslator(const QString &baseName, const QString &prefix, const QString &appName)
Loads and installs a translator.
Definition util.cpp:130
QString GetInternetLocaleName(const QLocale &locale)
Definition util.cpp:181