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