Switch to unified view

a/HelperStructs/Helper.cpp b/HelperStructs/Helper.cpp
1
/* Copyright (C) 2011  Lucio Carreras
1
/* Copyright (C) 2011  Lucio Carreras
2
 * Copyright (C) 2017  J.F. Dockes
2
 *
3
 *
3
 * This file is part of sayonara player
4
 * This file is part of Upplay
4
 *
5
 *
5
 * This program is free software: you can redistribute it and/or modify
6
 * This program is free software: you can redistribute it and/or modify
6
 * it under the terms of the GNU General Public License as published by
7
 * it under the terms of the GNU General Public License as published by
7
 * the Free Software Foundation, either version 3 of the License, or
8
 * the Free Software Foundation, either version 3 of the License, or
8
 * (at your option) any later version.
9
 * (at your option) any later version.
9
10
 *
10
 * This program is distributed in the hope that it will be useful,
11
 * This program is distributed in the hope that it will be useful,
11
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 * GNU General Public License for more details.
14
 * GNU General Public License for more details.
14
15
 *
15
 * You should have received a copy of the GNU General Public License
16
 * You should have received a copy of the GNU General Public License
16
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
 */
18
19
19
#include <sys/types.h>
20
#include <sys/stat.h>
21
#include <stdio.h>
22
#include <string.h>
23
#include <errno.h>
24
#include <fcntl.h>
25
26
#include <ctime>
27
#include <string>
20
#include <string>
28
#include <iostream>
29
#include <sstream>
30
#include <set>
21
#include <set>
31
#include <vector>
22
#include <vector>
32
#include <list>
23
#include <list>
33
24
34
#include <Qt>
35
#include <QDir>
25
#include <QDir>
36
#include <QUrl>
37
#include <QString>
26
#include <QString>
38
#include <QTextDocument>
39
#include <QCryptographicHash>
40
#include <QDebug>
27
#include <QDebug>
41
#include <QList>
42
#include <QFile>
43
#include <QFontMetrics>
44
#include <QFileInfo>
45
#include <QMap>
46
#include <QString>
47
#include <QtGlobal>
48
28
49
#include "HelperStructs/Helper.h"
29
#include "HelperStructs/Helper.h"
50
#include "HelperStructs/MetaData.h"
30
#include "HelperStructs/MetaData.h"
51
#include "HelperStructs/globals.h"
31
#include "HelperStructs/globals.h"
52
#include "HelperStructs/CSettingsStorage.h"
32
#include "HelperStructs/CSettingsStorage.h"
...
...
61
        ret = qf.read(1000 * 1000);
41
        ret = qf.read(1000 * 1000);
62
    }
42
    }
63
    return ret;
43
    return ret;
64
}
44
}
65
45
66
QString Helper::cvtQString2FirstUpper(QString str)
67
{
68
69
    QString ret_str = "";
70
    QStringList lst = str.split(" ");
71
72
    foreach(QString word, lst) {
73
        QString first = word.left(1);
74
        word.remove(0, 1);
75
        word = first.toUpper() + word + " ";
76
77
        ret_str += word;
78
    }
79
80
    return ret_str.left(ret_str.size() - 1);
81
}
82
83
QString cvtNum2String(int num, int digits)
46
static QString cvtNum2String(int num, int digits)
84
{
47
{
85
    QString str = QString::number(num);
48
    QString str = QString::number(num);
86
    while (str.size() < digits) {
49
    while (str.size() < digits) {
87
        str.prepend("0");
50
        str.prepend("0");
88
    }
51
    }
...
...
130
    } else {
93
    } else {
131
        final_str +=  cvtNum2String(min, 2) + "m " + cvtNum2String(secs, 2);
94
        final_str +=  cvtNum2String(min, 2) + "m " + cvtNum2String(secs, 2);
132
    }
95
    }
133
96
134
    return sign + final_str;
97
    return sign + final_str;
135
136
}
98
}
137
99
138
#ifdef Q_OS_WIN
100
#ifdef Q_OS_WIN
139
#include <Shlwapi.h>
101
#include <Shlwapi.h>
140
#pragma comment(lib, "shlwapi.lib")
102
#pragma comment(lib, "shlwapi.lib")
...
...
215
177
216
    homedir.mkpath(rpath);
178
    homedir.mkpath(rpath);
217
    return homedir.absoluteFilePath(rpath);
179
    return homedir.absoluteFilePath(rpath);
218
}
180
}
219
181
182
#define DARK_BLUE(x) QString("<font color=#0000FF>") + x + QString("</font>")
183
#define LIGHT_BLUE(x) QString("<font color=#8888FF>") + x + QString("</font>")
184
220
QString Helper::createLink(QString name, QString target, bool underline)
185
QString Helper::createLink(QString name, QString target, bool underline)
221
{
186
{
222
187
223
    int dark = CSettingsStorage::getInstance()->getPlayerStyle();
188
    int dark = CSettingsStorage::getInstance()->getPlayerStyle();
224
    if (target.size() == 0) {
189
    if (target.size() == 0) {
...
...
238
        content = DARK_BLUE(name);
203
        content = DARK_BLUE(name);
239
    }
204
    }
240
205
241
    return QString("<a href=\"") + target + "\"" + style + ">" +
206
    return QString("<a href=\"") + target + "\"" + style + ">" +
242
           content + "</a>";
207
           content + "</a>";
243
}
244
245
QString Helper::calc_filesize_str(qint64 filesize)
246
{
247
    qint64 kb = 1024;
248
    qint64 mb = kb * 1024;
249
    qint64 gb = mb * 1024;
250
251
    QString size;
252
    if (filesize > gb) {
253
        size = QString::number(filesize / gb) + "." +
254
               QString::number((filesize / mb) % gb).left(2)  + " GB";
255
    } else if (filesize > mb) {
256
        size = QString::number(filesize / mb) + "." +
257
               QString::number((filesize / kb) % mb).left(2)  + " MB";
258
    }  else {
259
        size = QString::number(filesize / kb) + " KB";
260
    }
261
262
    return size;
263
}
264
265
QString Helper::calc_cover_token(QString artist, QString album)
266
{
267
    QString ret =
268
        QCryptographicHash::hash(artist.trimmed().toLower().toUtf8() +
269
                                 album.trimmed().toLower().toUtf8(),
270
                                 QCryptographicHash::Md5).toHex();
271
    return ret;
272
}
273
274
QString Helper::get_album_w_disc(const MetaData& md)
275
{
276
    return md.album.trimmed();
277
}
278
279
QStringList Helper::get_soundfile_extensions()
280
{
281
    QStringList filters;
282
    filters << "*.mp3"
283
            << "*.ogg"
284
            << "*.oga"
285
            << "*.m4a"
286
            << "*.wav"
287
            << "*.flac"
288
            << "*.aac"
289
            << "*.wma";
290
291
    QString bla;
292
    foreach(QString filter, filters) {
293
        filters.push_back(filter.toUpper());
294
    }
295
296
    return filters;
297
}
208
}
298
209
299
bool Helper::read_file_into_str(QString filename, QString* content)
210
bool Helper::read_file_into_str(QString filename, QString* content)
300
{
211
{
301
    QFile file(filename);
212
    QFile file(filename);