Parent: [c92eb3] (diff)

Download this file

Helper.cpp    288 lines (241 with data), 6.5 kB

  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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* Copyright (C) 2011 Lucio Carreras
* Copyright (C) 2017 J.F. Dockes
*
* This file is part of Upplay
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <string>
#include <set>
#include <vector>
#include <list>
#include <time.h>
#include <QDir>
#include <QString>
#include <QDebug>
#include "HelperStructs/Helper.h"
#include "HelperStructs/MetaData.h"
#include "HelperStructs/globals.h"
#include "HelperStructs/CSettingsStorage.h"
using namespace std;
void Helper::msleep(int millis)
{
struct timespec spec;
spec.tv_sec = millis / 1000;
spec.tv_nsec = (millis % 1000) * 1000000;
nanosleep(&spec, 0);
}
QByteArray Helper::readFileToByteArray(const QString& fn)
{
QFile qf(fn);
QByteArray ret;
if (qf.open(QIODevice::ReadOnly)) {
ret = qf.read(1000 * 1000);
}
return ret;
}
static QString cvtNum2String(int num, int digits)
{
QString str = QString::number(num);
while (str.size() < digits) {
str.prepend("0");
}
return str;
}
QString Helper::cvtMsecs2TitleLengthString(long int msec, bool colon,
bool show_days)
{
QString sign;
if (msec < 0) {
msec = -msec;
sign = "-";
}
bool show_hrs = false;
int sec = msec / 1000;
int min = sec / 60;
int secs = sec % 60;
int hrs = min / 60;
int days = hrs / 24;
QString final_str;
if (days > 0 && show_days) {
final_str += QString::number(days) + "d ";
hrs = hrs % 24;
show_hrs = true;
}
if (!show_days) {
hrs += (days * 24);
}
if (hrs > 0 || show_hrs) {
final_str += QString::number(hrs) + "h ";
min = min % 60;
}
if (colon) {
final_str += cvtNum2String(min, 2) + ":" + cvtNum2String(secs, 2);
} else {
final_str += cvtNum2String(min, 2) + "m " + cvtNum2String(secs, 2);
}
return sign + final_str;
}
#ifdef Q_OS_WIN
#include <Shlwapi.h>
#pragma comment(lib, "shlwapi.lib")
QString GetExecPath()
{
TCHAR dest[MAX_PATH];
DWORD length = GetModuleFileName(NULL, dest, MAX_PATH);
#ifdef NTDDI_WIN8_future
PathCchRemoveFileSpec(dest, MAX_PATH);
#else
PathRemoveFileSpec(dest);
#endif
if (sizeof(TCHAR) == 2)
return QString::fromUtf16((const ushort*)dest);
else
return QString::fromUtf8((const char*)dest);
}
#endif
QString Helper::getSharePath()
{
QString path;
#if defined(Q_OS_WIN)
QDir execdir(GetExecPath());
QString rpath("Share");
execdir.mkpath(rpath);
path = execdir.absoluteFilePath(rpath);
#elif defined(Q_OS_MACOS)
path = QCoreApplication::applicationDirPath() + QString("/..");
path = QDir::cleanPath(path);
#else
if (QFile::exists(PREFIX "/share/upplay")) {
path = PREFIX "/share/upplay/";
} else {
path = "";
}
#endif
return path;
}
QString Helper::getCSSPath()
{
#if defined(Q_OS_MACOS) || defined(Q_OS_MAC)
return Helper::getSharePath() + "/Resources/";
#else
return Helper::getSharePath() + "/cdbrowser/";
#endif
}
static QString styleSubDir;
void Helper::setStyleSubDir(const QString& subd)
{
styleSubDir = subd;
}
QString Helper::getIconDir()
{
#if defined(Q_OS_MACOS)
return QDir(getSharePath()).filePath("Resources");
#else
return QDir(getSharePath()).filePath("icons");
#endif
}
QString Helper::getIconPath(const QString& icnm)
{
if (!styleSubDir.isEmpty()) {
QDir styledir(QDir(getIconDir()).filePath(styleSubDir));
if (QFile::exists(styledir.filePath(icnm))) {
return styledir.filePath(icnm);
}
}
return QDir(getIconDir()).filePath(icnm);
}
QString Helper::getHomeDataPath()
{
QDir homedir(QDir::home());
#ifndef Q_OS_WIN
QString rpath = QString::fromUtf8(".local/share/upplay");
#else
QString rpath = QString::fromUtf8("AppData/Local/upplay/");
#endif
homedir.mkpath(rpath);
return homedir.absoluteFilePath(rpath);
}
#define DARK_BLUE(x) QString("<font color=#0000FF>") + x + QString("</font>")
#define LIGHT_BLUE(x) QString("<font color=#8888FF>") + x + QString("</font>")
QString Helper::createLink(QString name, QString target, bool underline)
{
int dark = CSettingsStorage::getInstance()->getPlayerStyle();
if (target.size() == 0) {
target = name;
}
QString content;
QString style = "";
if (!underline) {
style = " style: \"text-decoration=none;\" ";
}
if (dark) {
content = LIGHT_BLUE(name);
} else {
content = DARK_BLUE(name);
}
return QString("<a href=\"") + target + "\"" + style + ">" +
content + "</a>";
}
bool Helper::read_file_into_str(QString filename, QString* content)
{
QFile file(filename);
content->clear();
if (!file.open(QIODevice::ReadOnly)) {
return false;
}
while (!file.atEnd()) {
QByteArray arr = file.readLine();
QString str = QString::fromLocal8Bit(arr);
content->append(str);
}
file.close();
if (content->size() > 0) {
return true;
}
return false;
}
// Escape things that would look like HTML markup
string Helper::escapeHtml(const string &in)
{
string out;
for (string::size_type pos = 0; pos < in.length(); pos++) {
switch(in.at(pos)) {
case '<': out += "&lt;"; break;
case '>': out += "&gt;"; break;
case '&': out += "&amp;"; break;
case '"': out += "&quot;"; break;
default: out += in.at(pos); break;
}
}
return out;
}
QString Helper::escapeHtml(const QString& in)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 0, 0))
return in.toHtmlEscaped();
#else
return Qt::escape(in);
#endif
}
string ivtos(const vector<int>& nids)
{
string sids;
for (unsigned int i = 0; i < nids.size(); i++) {
char cbuf[30];
sprintf(cbuf, "%d", nids[i]);
sids += string(cbuf) + " ";
}
return sids;
}