forked from qbittorrent/qBittorrent
-
Notifications
You must be signed in to change notification settings - Fork 0
/
misc.h
330 lines (309 loc) · 10.9 KB
/
misc.h
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
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Christophe Dumez
*
* 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 2
* 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, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Contact : [email protected]
*/
#ifndef MISC_H
#define MISC_H
#include <sstream>
#include <stdexcept>
#include <QObject>
#include <QString>
#include <QByteArray>
#include <QFileInfo>
#include <QDir>
#include <QList>
#include <QPair>
#include <QThread>
#include <libtorrent/torrent_info.hpp>
#include "qtorrenthandle.h"
using namespace libtorrent;
/* Miscellaneaous functions that can be useful */
class misc : public QObject{
Q_OBJECT
public:
// Convert any type of variable to C++ String
// convert=true will convert -1 to 0
template <class T> static std::string toString(const T& x, bool convert=false) {
std::ostringstream o;
if(!(o<<x)) {
throw std::runtime_error("::toString()");
}
if(o.str() == "-1" && convert)
return "0";
return o.str();
}
template <class T> static QString toQString(const T& x, bool convert=false) {
std::ostringstream o;
if(!(o<<x)) {
throw std::runtime_error("::toString()");
}
if(o.str() == "-1" && convert)
return QString::fromUtf8("0");
return QString::fromUtf8(o.str().c_str());
}
template <class T> static QByteArray toQByteArray(const T& x, bool convert=false) {
std::ostringstream o;
if(!(o<<x)) {
throw std::runtime_error("::toString()");
}
if(o.str() == "-1" && convert)
return "0";
return QByteArray(o.str().c_str());
}
// Convert C++ string to any type of variable
template <class T> static T fromString(const std::string& s) {
T x;
std::istringstream i(s);
if(!(i>>x)) {
throw std::runtime_error("::fromString()");
}
return x;
}
// template <class T> static T fromQString::fromUtf8(const QString& s) {
// T x;
// std::istringstream i((const char*)s.toUtf8());
// if(!(i>>x)) {
// throw std::runtime_error("::fromString()");
// }
// return x;
// }
//
// template <class T> static T fromQByteArray(const QByteArray& s) {
// T x;
// std::istringstream i((const char*)s);
// if(!(i>>x)) {
// throw std::runtime_error("::fromString()");
// }
// return x;
// }
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
// use Binary prefix standards from IEC 60027-2
// see http:https://en.wikipedia.org/wiki/Kilobyte
// value must be given in bytes
static QString friendlyUnit(float val) {
if(val < 0) {
return tr("Unknown", "Unknown (size)");
}
const QString units[4] = {tr("B", "bytes"), tr("KiB", "kibibytes (1024 bytes)"), tr("MiB", "mebibytes (1024 kibibytes)"), tr("GiB", "gibibytes (1024 mibibytes)")};
for(unsigned int i=0; i<5; ++i) {
if (val < 1024.) {
return QString(QByteArray::number(val, 'f', 1)) + units[i];
}
val /= 1024.;
}
return QString(QByteArray::number(val, 'f', 1)) + tr("TiB", "tebibytes (1024 gibibytes)");
}
// return qBittorrent config path
static QString qBittorrentPath() {
QString qBtPath = QDir::homePath()+QDir::separator()+QString::fromUtf8(".qbittorrent") + QDir::separator();
// Create dir if it does not exist
if(!QFile::exists(qBtPath)){
QDir dir(qBtPath);
dir.mkpath(qBtPath);
}
return qBtPath;
}
// Not used anymore because it is not safe
// static bool removePath(QString path) {
// qDebug((QString::fromUtf8("file to delete:") + path).toUtf8());
// if(!QFile::remove(path)) {
// // Probably a folder
// QDir current_dir(path);
// if(current_dir.exists()) {
// //Remove sub items
// QStringList subItems = current_dir.entryList();
// QString item;
// foreach(item, subItems) {
// if(item != QString::fromUtf8(".") && item != QString::fromUtf8("..")) {
// qDebug("-> Removing "+(path+QDir::separator()+item).toUtf8());
// removePath(path+QDir::separator()+item);
// }
// }
// // Remove empty folder
// if(current_dir.rmdir(path)) {
// return true;
// }else{
// return false;
// }
// }else{
// return false;
// }
// }
// return true;
// }
// safe function to remove a torrent from hard-drive
static bool removeTorrentSavePath(QString savePath, QStringList filesPath) {
bool success = true;
QDir saveDir(savePath);
QString path;
// Check how many file there are
if(filesPath.size() == 1){
// Only one file, not in a folder
path = filesPath.first();
if(QFile::exists(path)) {
if(QFile::remove(path)){
qDebug("Deleted only file in torrent at %s", path.toUtf8().data());
} else {
std::cerr << "Could not delete only file in torrent at " << path.toUtf8().data() << '\n';
success = false;
}
}else{
// File didn't exist, nothing to do
qDebug("Only file %s did not exist, nothing to delete", path.toUtf8().data());
}
// Try to remove parent folder if empty (and not save_dir)
QFileInfo fi(path);
QDir parentFolder = fi.absoluteDir();
while(parentFolder != saveDir) {
qDebug("trying to remove parent folder: %s", parentFolder.absolutePath().toUtf8().data());
if(!saveDir.rmdir(parentFolder.absolutePath())) break;
parentFolder.cdUp();
}
return success;
}
// Torrent has several files in a subFolder
foreach(path, filesPath) {
if(QFile::exists(path)) {
if(QFile::remove(path)){
qDebug("Deleted file in torrent at %s", path.toUtf8().data());
} else {
std::cerr << "Could not delete file in torrent at " << path.toUtf8().data() << '\n';
success = false;
}
} else {
qDebug("File %s did not exist, nothing to delete", path.toUtf8().data());
}
// Try to remove parent folder if empty (and not save_dir)
QFileInfo fi(path);
QDir parentFolder = fi.absoluteDir();
while(parentFolder != saveDir) {
qDebug("trying to remove parent folder: %s", parentFolder.absolutePath().toUtf8().data());
if(!saveDir.rmdir(parentFolder.absolutePath())) break;
parentFolder.cdUp();
}
}
return success;
}
static QString findFileInDir(QString dir_path, QString fileName) {
QDir dir(dir_path);
if(dir.exists(fileName)) {
return dir.filePath(fileName);
}
QStringList subDirs = dir.entryList(QDir::Dirs);
QString subdir_name;
foreach(subdir_name, subDirs) {
QString result = findFileInDir(dir.path()+QDir::separator()+subdir_name, fileName);
if(!result.isNull()) {
return result;
}
}
return QString();
}
static void fixTrackersTiers(std::vector<announce_entry> trackers) {
unsigned int nbTrackers = trackers.size();
for(unsigned int i=0; i<nbTrackers; ++i) {
trackers[i].tier = i;
}
}
// Insertion sort, used instead of bubble sort because it is
// approx. 5 times faster.
template <class T> static void insertSort(QList<QPair<int, T> > &list, const QPair<int, T>& value, Qt::SortOrder sortOrder) {
int i = 0;
if(sortOrder == Qt::AscendingOrder) {
while(i < list.size() and value.second > list.at(i).second) {
++i;
}
}else{
while(i < list.size() and value.second < list.at(i).second) {
++i;
}
}
list.insert(i, value);
}
template <class T> static void insertSort2(QList<QPair<int, T> > &list, const QPair<int, T>& value, Qt::SortOrder sortOrder) {
int i = 0;
if(sortOrder == Qt::AscendingOrder) {
while(i < list.size() and value.first > list.at(i).first) {
++i;
}
}else{
while(i < list.size() and value.first < list.at(i).first) {
++i;
}
}
list.insert(i, value);
}
// Can't use template class for QString because >,< use unicode code for sorting
// which is not what a human would expect when sorting strings.
static void insertSortString(QList<QPair<int, QString> > &list, QPair<int, QString> value, Qt::SortOrder sortOrder) {
int i = 0;
if(sortOrder == Qt::AscendingOrder) {
while(i < list.size() and QString::localeAwareCompare(value.second, list.at(i).second) > 0) {
++i;
}
}else{
while(i < list.size() and QString::localeAwareCompare(value.second, list.at(i).second) < 0) {
++i;
}
}
list.insert(i, value);
}
// Take a number of seconds and return an user-friendly
// time duration like "1d 2h 10m".
static QString userFriendlyDuration(qlonglong seconds) {
if(seconds < 0 or seconds > 8640000) {
return tr("Unknown");
}
int level = 0;
int days = int(seconds / 86400.);
if(!days)
level = 1;
else
seconds -= days * 86400;
int hours = int(seconds / 3600.);
if(!hours)
level = 2;
else
seconds -= hours * 3600;
int minutes = int(seconds / 60.0);
if(!minutes)
level = 3;
else
seconds -= minutes * 60;
switch(level){
case 3:
return tr("< 1m", "< 1 minute");
case 2:
return tr("%1m","e.g: 10minutes").arg(QString::QString::fromUtf8(misc::toString(minutes).c_str()));
case 1:
return tr("%1h%2m", "e.g: 3hours 5minutes").arg(QString::fromUtf8(misc::toString(hours).c_str())).arg(QString::fromUtf8(misc::toString(minutes).c_str()));
default:
return tr("%1d%2h%3m", "e.g: 2days 10hours 2minutes").arg(QString::fromUtf8(misc::toString(days).c_str())).arg(QString::fromUtf8(misc::toString(hours).c_str())).arg(QString::fromUtf8(misc::toString(minutes).c_str()));
}
}
};
// Trick to get a portable sleep() function
class SleeperThread : public QThread{
public:
static void msleep(unsigned long msecs)
{
QThread::msleep(msecs);
}
};
#endif