1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
#include "utils.h"
#include <QApplication>
#include <QDateTime>
#include <QMessageBox>
#include <QProcessEnvironment>
#include <QRandomGenerator>
#include <QRegularExpression>
#include <time.h>
utils::utils(QObject *parent) : QObject(parent) { setParent(parent); }
utils::~utils() { this->deleteLater(); }
// calculate dir size
quint64 utils::dir_size(const QString &directory) {
quint64 sizex = 0;
QFileInfo str_info(directory);
if (str_info.isDir()) {
QDir dir(directory);
QFileInfoList list =
dir.entryInfoList(QDir::Files | QDir::Dirs | QDir::Hidden |
QDir::NoSymLinks | QDir::NoDotAndDotDot);
for (int i = 0; i < list.size(); ++i) {
QFileInfo fileInfo = list.at(i);
if (fileInfo.isDir()) {
sizex += dir_size(fileInfo.absoluteFilePath());
} else {
sizex += fileInfo.size();
}
}
}
return sizex;
}
// get the size of cache folder in human readble format
QString utils::refreshCacheSize(const QString cache_dir) {
qint64 cache_size = dir_size(cache_dir);
QString cache_unit;
if (cache_size > 1024 * 1024 * 1024) {
cache_size = cache_size / (1024 * 1024 * 1024);
cache_unit = " GB";
}
if (cache_size > 1024 * 1024) {
cache_size = cache_size / (1024 * 1024);
cache_unit = " MB";
} else if (cache_size > 1024) {
cache_size = cache_size / (1024);
cache_unit = " kB";
} else {
cache_unit = " B";
}
return QString::number(cache_size) + cache_unit;
}
bool utils::delete_cache(const QString cache_dir) {
bool deleted = QDir(cache_dir).removeRecursively();
QDir(cache_dir).mkpath(cache_dir);
return deleted;
}
// returns string with first letter capitalized
QString utils::toCamelCase(const QString &s) {
QStringList parts = s.split(' ', Qt::SkipEmptyParts);
for (int i = 0; i < parts.size(); ++i)
parts[i].replace(0, 1, parts[i][0].toUpper());
return parts.join(" ");
}
QString utils::generateRandomId(int length) {
QString str = QUuid::createUuid().toString();
str.remove(QRegularExpression("{|}|-"));
if (str.length() < length) {
while (str.length() != length) {
int required_char = length - str.length();
str = str + str.append(genRand(required_char));
}
}
if (str.length() > length) {
while (str.length() != length) {
str = str.remove(str.length() - 1, 1);
}
}
return str;
}
QString utils::genRand(int length) {
QDateTime cd = QDateTime::currentDateTime();
const QString possibleCharacters(
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789" +
QString::number(cd.currentMSecsSinceEpoch())
.remove(QRegExp("[^a-zA-Z\\d\\s]")));
const int randomStringLength = length;
QString randomString;
int rand = QRandomGenerator::global()->generate();
for (int i = 0; i < randomStringLength; ++i) {
int index = rand % possibleCharacters.length();
QChar nextChar = possibleCharacters.at(index);
randomString.append(nextChar);
}
randomString = randomString.trimmed().simplified().remove(" ");
return randomString;
}
QString utils::convertSectoDay(qint64 secs) {
int day = secs / (24 * 3600);
secs = secs % (24 * 3600);
int hour = secs / 3600;
secs %= 3600;
int minutes = secs / 60;
secs %= 60;
int seconds = secs;
QString days = QString::number(day) + " " + "days " + QString::number(hour) +
" " + "hours " + QString::number(minutes) + " " + "minutes " +
QString::number(seconds) + " " + "seconds ";
return days;
}
// static on demand path maker
QString utils::returnPath(QString pathname,QString standardLocation = QStandardPaths::writableLocation(
QStandardPaths::DataLocation)) {
QChar sepe = QDir::separator();
QDir d(standardLocation + sepe + pathname);
d.mkpath(standardLocation + sepe + pathname);
return standardLocation + sepe + pathname + sepe;
}
QString utils::EncodeXML(const QString &encodeMe) {
QString temp;
int length = encodeMe.size();
for (int index = 0; index < length; index++) {
QChar character(encodeMe.at(index));
switch (character.unicode()) {
case '&':
temp += "&";
break;
case '\'':
temp += "'";
break;
case '"':
temp += """;
break;
case '<':
temp += "<";
break;
case '>':
temp += ">";
break;
default:
temp += character;
break;
}
}
return temp;
}
QString utils::DecodeXML(const QString &decodeMe) {
QString temp(decodeMe);
temp.replace("&", "&");
temp.replace("'", "'");
temp.replace(""", "\"");
temp.replace("<", "<");
temp.replace(">", ">");
return temp;
}
QString utils::htmlToPlainText(QString str) {
QString out;
QTextDocument text;
text.setHtml(str);
out = text.toPlainText();
text.deleteLater();
return out.replace("\\\"", "'")
.replace("&", "&")
.replace(">", ">")
.replace("<", "<")
.replace("'", "'");
}
QString utils::appDebugInfo() {
QStringList debugInfo;
debugInfo << "<h3>" + QApplication::applicationName() + "</h3>"
<< "<ul>"
<< "<li><b>" + QObject::tr("Version") + ":</b> " +
QString(VERSIONSTR) + "</li>"
<< "<li><b>" + QObject::tr("Source Branch") + ":</b> " +
QString(GIT_BRANCH) + "</li>"
<< "<li><b>" + QObject::tr("Commit Hash") + ":</b> " +
QString(GIT_HASH) + "</li>"
<< "<li><b>" + QObject::tr("Build Datetime") + ":</b> " +
QString::fromLatin1(BUILD_TIMESTAMP) + "</li>"
<< "<li><b>" + QObject::tr("Qt Runtime Version") + ":</b> " +
QString(qVersion()) + "</li>"
<< "<li><b>" + QObject::tr("Qt Compiled Version") + ":</b> " +
QString(QT_VERSION_STR) + "</li>"
<< "<li><b>" + QObject::tr("System") + ":</b> " +
QSysInfo::prettyProductName() + "</li>"
<< "<li><b>" + QObject::tr("Architecture") + ":</b> " +
QSysInfo::currentCpuArchitecture() + "</li>";
debugInfo << "</ul>";
return debugInfo.join("\n");
}
void utils::DisplayExceptionErrorDialog(const QString &error_info) {
QMessageBox message_box(QApplication::activeWindow());
message_box.setAttribute(Qt::WA_DeleteOnClose, true);
message_box.setWindowFlags(Qt::Window | Qt::WindowStaysOnTopHint);
message_box.setModal(true);
message_box.setIcon(QMessageBox::Critical);
message_box.setWindowTitle(QApplication::applicationName() +
QObject::tr("Exception"));
// spaces are added to the end because otherwise the dialog is too small
message_box.setText(QApplication::applicationName() +
QObject::tr(" has encountered a problem."));
message_box.setInformativeText(
QApplication::applicationName() +
QObject::tr(
" may need to Restart. Please report the error to developer."));
message_box.setStandardButtons(QMessageBox::Close);
QStringList detailed_text;
detailed_text << "Error info: " + error_info
<< "\nApp version: " + QString(VERSIONSTR)
<< "\nSource Branch: " + QString(GIT_BRANCH)
<< "\nCommit Hash: " + QString(GIT_HASH)
<< "\nBuild Datetime: " + QString(BUILD_TIMESTAMP)
<< "\nQt Runtime Version: " + QString(qVersion())
<< "\nQt Compiled Version: " + QString(QT_VERSION_STR)
<< "\nSystem: " + QSysInfo::prettyProductName()
<< "\nArchitecture: " + QSysInfo::currentCpuArchitecture();
message_box.setDetailedText(detailed_text.join("\n"));
message_box.exec();
}
// Returns the same number, but rounded to one decimal place
float utils::RoundToOneDecimal(float number) {
return QString::number(number, 'f', 1).toFloat();
}
// Returns a value for the environment variable name passed;
// if the env var isn't set, it returns an empty string
QString utils::GetEnvironmentVar(const QString &variable_name) {
#if QT_VERSION >= QT_VERSION_CHECK(5, 10, 0)
// The only time this might fall down is on Linux when an
// environment variable holds bytedata. Don't use this
// utility function for retrieval if that's the case.
return qEnvironmentVariable(variable_name.toUtf8().constData(), "").trimmed();
#else
// This will typically only be used on older Qts on Linux
return QProcessEnvironment::systemEnvironment()
.value(variable_name, "")
.trimmed();
#endif
}
|