Parent: [2c9b17] (diff)

Child: [0f99fd] (diff)

Download this file

Helper.cpp    464 lines (372 with data), 10.7 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
/* Copyright (C) 2011 Lucio Carreras
*
* This file is part of sayonara player
*
* 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 <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <ctime>
#include <string>
#include <iostream>
#include <sstream>
#include <QDir>
#include <QUrl>
#include <QString>
#include <QCryptographicHash>
#include <QDebug>
#include <QList>
#include <QFile>
#include <QFontMetrics>
#include <QFileInfo>
#include <QMap>
#include <QString>
#include "HelperStructs/Helper.h"
#include "HelperStructs/MetaData.h"
#include "HelperStructs/globals.h"
#include "HelperStructs/CSettingsStorage.h"
using namespace std;
QByteArray Helper::readFileToByteArray(const char *fn)
{
int fd = -1;
char *cp = 0;
QByteArray ret;
struct stat st;
fd = open(fn, 0);
if (fd < 0 || fstat(fd, &st) < 0) {
qDebug() << "readfile: open/fstat [" << fn << "] errno " << errno;
goto out;
}
cp = (char *)malloc(st.st_size);
if (cp == 0) {
qDebug() << "readfile: no mem: can't allocate " << st.st_size;
goto out;
}
if (read(fd, cp, st.st_size) != st.st_size) {
qDebug() << "readfile: read [" << fn << "] errno " << errno;
goto out;
}
ret.append(cp, st.st_size);
out:
if (fd >= 0)
close(fd);
if (cp)
free(cp);
return ret;
}
QString Helper::cvtQString2FirstUpper(QString str)
{
QString ret_str = "";
QStringList lst = str.split(" ");
foreach(QString word, lst){
QString first = word.left(1);
word.remove(0,1);
word = first.toUpper() + word + " ";
ret_str += word;
}
return ret_str.left(ret_str.size() - 1);
}
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)
{
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 final_str;
}
QString Helper::getSharePath()
{
QString path;
#ifndef Q_OS_WIN
if(QFile::exists(PREFIX "/share/upplay"))
path = PREFIX "/share/upplay/";
else
path = "";
#else
path = QDir::homePath() + QString("\\.upplay\\images\\");
if(QFile::exists(path)){
return path;
} else
path = "";
#endif
return path;
}
QString Helper::getIconPath()
{
return ":/icons/";
}
QString Helper::getHomeDataPath()
{
QString mydatadir = QDir::homePath() + "/.local/share/upplay";
if(!QFile::exists(mydatadir)) {
QDir().mkdir(mydatadir);
}
return mydatadir + "/";
}
QString Helper::get_cover_path(QString artist, QString album, QString extension)
{
QString cover_dir = getHomeDataPath() + QDir::separator() + "covers";
if(!QFile::exists(cover_dir)) {
QDir().mkdir(cover_dir);
}
QString cover_token = calc_cover_token(artist, album);
QString cover_path = cover_dir + QDir::separator() + cover_token +
"." + extension;
return cover_path;
}
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>";
}
QString Helper::calc_filesize_str(qint64 filesize)
{
qint64 kb = 1024;
qint64 mb = kb * 1024;
qint64 gb = mb * 1024;
QString size;
if(filesize > gb){
size = QString::number(filesize / gb) + "." +
QString::number((filesize / mb) % gb).left(2) + " GB";
} else if (filesize > mb){
size = QString::number(filesize / mb) + "." +
QString::number((filesize / kb) % mb).left(2) + " MB";
} else {
size = QString::number( filesize / kb) + " KB";
}
return size;
}
QString Helper::calc_cover_token(QString artist, QString album)
{
QString ret =
QCryptographicHash::hash(artist.trimmed().toLower().toUtf8() +
album.trimmed().toLower().toUtf8(),
QCryptographicHash::Md5).toHex();
return ret;
}
QString Helper::get_album_w_disc(const MetaData& md)
{
return md.album.trimmed();
}
QStringList Helper::get_soundfile_extensions()
{
QStringList filters;
filters << "*.mp3"
<< "*.ogg"
<< "*.oga"
<< "*.m4a"
<< "*.wav"
<< "*.flac"
<< "*.aac"
<< "*.wma";
QString bla;
foreach(QString filter, filters) {
filters.push_back(filter.toUpper());
}
return filters;
}
bool Helper::is_soundfile(QString filename)
{
QStringList extensions = get_soundfile_extensions();
foreach (QString extension, extensions) {
if (filename.toLower().endsWith(extension.right(4).toLower())) {
return true;
}
}
return false;
}
QStringList Helper::get_playlistfile_extensions()
{
QStringList filters;
filters << "*.pls"
<< "*.m3u"
<< "*.ram"
<< "*.asx";
foreach (QString filter, filters) {
filters.push_back(filter.toUpper());
}
return filters;
}
bool Helper::is_playlistfile(QString filename)
{
QStringList extensions = get_playlistfile_extensions();
foreach(QString extension, extensions){
if(filename.toLower().endsWith(extension.right(4).toLower())){
return true;
}
}
return false;
}
QString Helper::calc_file_extension(QString filename)
{
int last_point = filename.lastIndexOf(".") + 1;
return filename.right(filename.size() - last_point);
}
void Helper::remove_files_in_directory(QString dir_name, QStringList filters)
{
if(filters.size() == 0)
filters << "*";
QDir dir(dir_name);
dir.setFilter(QDir::Files);
dir.setNameFilters(filters);
QStringList file_list = dir.entryList();
foreach(QString filename, file_list) {
QFile file(dir.absoluteFilePath(filename));
file.remove();
}
}
QString Helper::get_parent_folder(QString filename)
{
QString ret= filename.left(filename.lastIndexOf(QDir::separator()) + 1);
int last_idx = ret.lastIndexOf(QDir::separator());
while(last_idx == ret.size() - 1){
ret = ret.left(ret.size() - 1);
last_idx = ret.lastIndexOf(QDir::separator());
}
return ret;
}
QString Helper::get_filename_of_path(QString path)
{
while (path.endsWith(QDir::separator()))
path.remove(path.size() - 1, 1);
path.remove(Helper::get_parent_folder(path));
path.remove(QDir::separator());
return path;
}
void Helper::split_filename(QString src, QString& path, QString& filename)
{
path = Helper::get_parent_folder(src);
filename = Helper::get_filename_of_path(src);
}
QStringList Helper::extract_folders_of_files(QStringList files)
{
QStringList folders;
foreach(QString file, files) {
QString folder = get_parent_folder(file);
if(!folders.contains(folder))
folders << folder;
}
return folders;
}
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;
}
QString Helper::split_string_to_widget(QString str, QWidget* w, QChar sep)
{
QFontMetrics fm(w->font());
int width = w->width();
QString subtext = str;
QStringList lst;
while(fm.width(subtext) > width){
int textsize = fm.width(subtext);
double scale = (width * 1.0) / textsize;
int idx = subtext.size() * scale - 2;
if(idx < 0) idx = 0;
while(subtext.at(idx) != sep && idx >= 0){
idx --;
}
if(idx >= 0){
lst << subtext.left(idx+1);
subtext = subtext.right(subtext.size() - idx);
}
else
break;
}
lst << subtext;
return lst.join("<br />");
}
bool Helper::is_url(QString str){
if(is_www(str)) return true;
if(str.startsWith("file"), Qt::CaseInsensitive) return true;
return false;
}
bool Helper::is_www(QString str){
if(str.startsWith("http")) return true;
else if(str.startsWith("ftp")) return true;
return false;
}
bool Helper::is_dir(QString filename){
if(!QFile::exists(filename)) return false;
QFileInfo fileinfo(filename);
return fileinfo.isDir();
}
bool Helper::is_file(QString filename){
if(!QFile::exists(filename)) return false;
QFileInfo fileinfo(filename);
return fileinfo.isFile();
}
void Helper::set_deja_vu_font(QWidget* w){
QFont f = w->font();
f.setFamily("DejaVu Sans");
f.setStyleStrategy(QFont::PreferAntialias);
f.setHintingPreference(QFont::PreferNoHinting);
w->setFont(f);
}