2006-09-30 20:02:39 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2007-07-14 18:31:59 +04:00
|
|
|
* Copyright (C) 2006 Christophe Dumez
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* 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.
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
|
|
|
* 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
|
2007-07-14 18:31:59 +04:00
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
|
|
|
* Contact : chris@qbittorrent.org
|
2006-09-30 20:02:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef MISC_H
|
|
|
|
#define MISC_H
|
|
|
|
|
|
|
|
#include <sstream>
|
|
|
|
#include <stdexcept>
|
|
|
|
#include <QObject>
|
|
|
|
#include <QString>
|
|
|
|
#include <QDir>
|
|
|
|
#include <QList>
|
|
|
|
#include <QPair>
|
|
|
|
#include <QThread>
|
|
|
|
|
|
|
|
#define MAX_CHAR_TMP 128
|
|
|
|
|
|
|
|
/* Miscellaneaous functions that can be useful */
|
|
|
|
class misc : public QObject{
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
public:
|
|
|
|
// Convert any type of variable to C++ String
|
2006-12-27 06:19:38 +03:00
|
|
|
// convert=true will convert -1 to 0
|
|
|
|
template <class T> static std::string toString(const T& x, bool convert=false){
|
2006-09-30 20:02:39 +04:00
|
|
|
std::ostringstream o;
|
|
|
|
if(!(o<<x)) {
|
|
|
|
throw std::runtime_error("::toString()");
|
|
|
|
}
|
2006-12-27 06:19:38 +03:00
|
|
|
if(o.str() == "-1" && convert)
|
|
|
|
return "0";
|
2006-09-30 20:02:39 +04:00
|
|
|
return o.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;
|
|
|
|
}
|
|
|
|
|
|
|
|
// return best userfriendly storage unit (B, KiB, MiB, GiB, TiB)
|
|
|
|
// use Binary prefix standards from IEC 60027-2
|
|
|
|
// see http://en.wikipedia.org/wiki/Kilobyte
|
|
|
|
// value must be given in bytes
|
2007-03-30 16:47:57 +04:00
|
|
|
static QString friendlyUnit(float val){
|
2006-09-30 20:02:39 +04:00
|
|
|
char tmp[MAX_CHAR_TMP];
|
2007-03-30 16:47:57 +04:00
|
|
|
if(val < 0){
|
2006-09-30 20:02:39 +04:00
|
|
|
return QString(tr("Unknown", "Unknown (size)"));
|
|
|
|
}
|
2007-03-30 16:47:57 +04:00
|
|
|
const QString units[4] = {tr("B", "bytes"), tr("KiB", "kibibytes (1024 bytes)"), tr("MiB", "mebibytes (1024 kibibytes)"), tr("GiB", "gibibytes (1024 mibibytes)")};
|
|
|
|
for(unsigned short i=0; i<5; ++i){
|
2006-09-30 20:02:39 +04:00
|
|
|
if (val < 1024.){
|
2007-03-30 16:47:57 +04:00
|
|
|
snprintf(tmp, MAX_CHAR_TMP, "%.1f", val);
|
2006-09-30 20:02:39 +04:00
|
|
|
return QString(tmp) + " " + units[i];
|
|
|
|
}
|
|
|
|
val /= 1024.;
|
|
|
|
}
|
2007-03-30 16:47:57 +04:00
|
|
|
snprintf(tmp, MAX_CHAR_TMP, "%.1f", val);
|
2006-09-30 20:02:39 +04:00
|
|
|
return QString(tmp) + " " + tr("TiB", "tebibytes (1024 gibibytes)");
|
|
|
|
}
|
|
|
|
|
|
|
|
// return qBittorrent config path
|
|
|
|
static QString qBittorrentPath() {
|
|
|
|
QString qBtPath = QDir::homePath();
|
|
|
|
if(qBtPath.isNull()){
|
|
|
|
return QString();
|
|
|
|
}
|
|
|
|
if(qBtPath[qBtPath.length()-1] == QDir::separator()){
|
|
|
|
qBtPath = qBtPath + ".qbittorrent" + QDir::separator();
|
|
|
|
}else{
|
|
|
|
qBtPath = qBtPath + QDir::separator() + ".qbittorrent" + QDir::separator();
|
|
|
|
}
|
|
|
|
// Create dir if it does not exist
|
|
|
|
QDir dir(qBtPath);
|
|
|
|
if(!dir.exists()){
|
|
|
|
dir.mkpath(qBtPath);
|
|
|
|
}
|
|
|
|
return qBtPath;
|
|
|
|
}
|
|
|
|
|
2006-12-28 03:54:05 +03:00
|
|
|
static bool removePath(QString path){
|
2007-02-24 01:52:24 +03:00
|
|
|
qDebug((QString("file to delete:") + path).toUtf8());
|
2006-12-28 03:54:05 +03:00
|
|
|
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 != "." && item != ".."){
|
|
|
|
qDebug("-> Removing "+(path+QDir::separator()+item).toUtf8());
|
|
|
|
removePath(path+QDir::separator()+item);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Remove empty folder
|
2007-06-30 23:19:09 +04:00
|
|
|
if(current_dir.rmdir(path)){
|
2006-12-28 03:54:05 +03:00
|
|
|
return true;
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}else{
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2007-07-22 13:47:27 +04:00
|
|
|
static QString findFileInDir(QString dir_path, QString fileName){
|
2006-09-30 20:02:39 +04:00
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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);
|
|
|
|
}
|
|
|
|
|
2006-10-03 04:11:35 +04:00
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2006-09-30 20:02:39 +04:00
|
|
|
// 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(const long int seconds){
|
2007-07-22 15:53:43 +04:00
|
|
|
if(seconds < 0){
|
2006-09-30 20:02:39 +04:00
|
|
|
return QString::QString(tr("Unknown"));
|
|
|
|
}
|
|
|
|
if(seconds < 60){
|
2007-03-09 01:51:52 +03:00
|
|
|
return tr("< 1m", "< 1 minute");
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
int minutes = seconds / 60;
|
|
|
|
if(minutes < 60){
|
2007-03-09 01:51:52 +03:00
|
|
|
return tr("%1m","e.g: 10minutes").arg(QString::QString(misc::toString(minutes).c_str()));
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
int hours = minutes / 60;
|
|
|
|
minutes = minutes - hours*60;
|
|
|
|
if(hours < 24){
|
2007-03-09 01:51:52 +03:00
|
|
|
return tr("%1h%2m", "e.g: 3hours 5minutes").arg(QString(misc::toString(hours).c_str())).arg(QString(misc::toString(minutes).c_str()));
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
int days = hours / 24;
|
|
|
|
hours = hours - days * 24;
|
|
|
|
if(days < 100){
|
2007-03-09 01:51:52 +03:00
|
|
|
return tr("%1d%2h%3m", "e.g: 2days 10hours 2minutes").arg(QString(misc::toString(days).c_str())).arg(QString(misc::toString(hours).c_str())).arg(QString(misc::toString(minutes).c_str()));
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
return QString::QString(tr("Unknown"));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
// Trick to get a portable sleep() function
|
|
|
|
class SleeperThread : public QThread{
|
|
|
|
public:
|
|
|
|
static void msleep(unsigned long msecs)
|
|
|
|
{
|
|
|
|
QThread::msleep(msecs);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|