mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-16 02:03:07 -07:00
- Big code cleanup
- Added a Qwrapper for torrent_handle to make code more readable
This commit is contained in:
parent
5946f20783
commit
c9925eddb6
24 changed files with 1560 additions and 1201 deletions
141
src/misc.h
141
src/misc.h
|
@ -26,6 +26,7 @@
|
|||
#include <stdexcept>
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QByteArray>
|
||||
#include <QDir>
|
||||
#include <QList>
|
||||
#include <QPair>
|
||||
|
@ -43,7 +44,7 @@ class misc : public QObject{
|
|||
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){
|
||||
template <class T> static std::string toString(const T& x, bool convert=false) {
|
||||
std::ostringstream o;
|
||||
if(!(o<<x)) {
|
||||
throw std::runtime_error("::toString()");
|
||||
|
@ -53,73 +54,111 @@ class misc : public QObject{
|
|||
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){
|
||||
template <class T> static T fromString(const std::string& s) {
|
||||
T x;
|
||||
std::istringstream i(s);
|
||||
if(!(i>>x)){
|
||||
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://en.wikipedia.org/wiki/Kilobyte
|
||||
// value must be given in bytes
|
||||
static QString friendlyUnit(float val){
|
||||
static QString friendlyUnit(float val) {
|
||||
char tmp[MAX_CHAR_TMP];
|
||||
if(val < 0){
|
||||
return QString(tr("Unknown", "Unknown (size)"));
|
||||
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 short i=0; i<5; ++i){
|
||||
if (val < 1024.){
|
||||
for(unsigned short i=0; i<5; ++i) {
|
||||
if (val < 1024.) {
|
||||
snprintf(tmp, MAX_CHAR_TMP, "%.1f", val);
|
||||
return QString(tmp) + " " + units[i];
|
||||
return QString::fromUtf8(tmp) + QString::fromUtf8(" ") + units[i];
|
||||
}
|
||||
val /= 1024.;
|
||||
}
|
||||
snprintf(tmp, MAX_CHAR_TMP, "%.1f", val);
|
||||
return QString(tmp) + " " + tr("TiB", "tebibytes (1024 gibibytes)");
|
||||
return QString::fromUtf8(tmp) + QString::fromUtf8(" ") + tr("TiB", "tebibytes (1024 gibibytes)");
|
||||
}
|
||||
|
||||
// return qBittorrent config path
|
||||
static QString qBittorrentPath() {
|
||||
QString qBtPath = QDir::homePath();
|
||||
if(qBtPath.isNull()){
|
||||
if(qBtPath.isNull()) {
|
||||
return QString();
|
||||
}
|
||||
if(qBtPath[qBtPath.length()-1] == QDir::separator()){
|
||||
qBtPath = qBtPath + ".qbittorrent" + QDir::separator();
|
||||
if(qBtPath[qBtPath.length()-1] == QDir::separator()) {
|
||||
qBtPath = qBtPath + QString::fromUtf8(".qbittorrent") + QDir::separator();
|
||||
}else{
|
||||
qBtPath = qBtPath + QDir::separator() + ".qbittorrent" + QDir::separator();
|
||||
qBtPath = qBtPath + QDir::separator() + QString::fromUtf8(".qbittorrent") + QDir::separator();
|
||||
}
|
||||
// Create dir if it does not exist
|
||||
QDir dir(qBtPath);
|
||||
if(!dir.exists()){
|
||||
if(!dir.exists()) {
|
||||
dir.mkpath(qBtPath);
|
||||
}
|
||||
return qBtPath;
|
||||
}
|
||||
|
||||
static bool removePath(QString path){
|
||||
qDebug((QString("file to delete:") + path).toUtf8());
|
||||
if(!QFile::remove(path)){
|
||||
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()){
|
||||
if(current_dir.exists()) {
|
||||
//Remove sub items
|
||||
QStringList subItems = current_dir.entryList();
|
||||
QString item;
|
||||
foreach(item, subItems){
|
||||
if(item != "." && 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)){
|
||||
if(current_dir.rmdir(path)) {
|
||||
return true;
|
||||
}else{
|
||||
return false;
|
||||
|
@ -131,53 +170,53 @@ class misc : public QObject{
|
|||
return true;
|
||||
}
|
||||
|
||||
static QString findFileInDir(QString dir_path, QString fileName){
|
||||
static QString findFileInDir(QString dir_path, QString fileName) {
|
||||
QDir dir(dir_path);
|
||||
if(dir.exists(fileName)){
|
||||
if(dir.exists(fileName)) {
|
||||
return dir.filePath(fileName);
|
||||
}
|
||||
QStringList subDirs = dir.entryList(QDir::Dirs);
|
||||
QString subdir_name;
|
||||
foreach(subdir_name, subDirs){
|
||||
foreach(subdir_name, subDirs) {
|
||||
QString result = findFileInDir(dir.path()+QDir::separator()+subdir_name, fileName);
|
||||
if(!result.isNull()){
|
||||
if(!result.isNull()) {
|
||||
return result;
|
||||
}
|
||||
}
|
||||
return QString();
|
||||
}
|
||||
|
||||
static void fixTrackersTiers(std::vector<announce_entry> trackers){
|
||||
static void fixTrackersTiers(std::vector<announce_entry> trackers) {
|
||||
unsigned int nbTrackers = trackers.size();
|
||||
for(unsigned int i=0; i<nbTrackers; ++i){
|
||||
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){
|
||||
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){
|
||||
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){
|
||||
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){
|
||||
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){
|
||||
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){
|
||||
while(i < list.size() and value.first < list.at(i).first) {
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
@ -186,14 +225,14 @@ class misc : public QObject{
|
|||
|
||||
// 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){
|
||||
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){
|
||||
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){
|
||||
while(i < list.size() and QString::localeAwareCompare(value.second, list.at(i).second) < 0) {
|
||||
++i;
|
||||
}
|
||||
}
|
||||
|
@ -202,28 +241,28 @@ class misc : public QObject{
|
|||
|
||||
// Take a number of seconds and return an user-friendly
|
||||
// time duration like "1d 2h 10m".
|
||||
static QString userFriendlyDuration(const long int seconds){
|
||||
if(seconds < 0){
|
||||
return QString::QString(tr("Unknown"));
|
||||
static QString userFriendlyDuration(const long int seconds) {
|
||||
if(seconds < 0) {
|
||||
return tr("Unknown");
|
||||
}
|
||||
if(seconds < 60){
|
||||
if(seconds < 60) {
|
||||
return tr("< 1m", "< 1 minute");
|
||||
}
|
||||
int minutes = seconds / 60;
|
||||
if(minutes < 60){
|
||||
return tr("%1m","e.g: 10minutes").arg(QString::QString(misc::toString(minutes).c_str()));
|
||||
if(minutes < 60) {
|
||||
return tr("%1m","e.g: 10minutes").arg(QString::QString::fromUtf8(misc::toString(minutes).c_str()));
|
||||
}
|
||||
int hours = minutes / 60;
|
||||
minutes = minutes - hours*60;
|
||||
if(hours < 24){
|
||||
return tr("%1h%2m", "e.g: 3hours 5minutes").arg(QString(misc::toString(hours).c_str())).arg(QString(misc::toString(minutes).c_str()));
|
||||
if(hours < 24) {
|
||||
return tr("%1h%2m", "e.g: 3hours 5minutes").arg(QString::fromUtf8(misc::toString(hours).c_str())).arg(QString::fromUtf8(misc::toString(minutes).c_str()));
|
||||
}
|
||||
int days = hours / 24;
|
||||
hours = hours - days * 24;
|
||||
if(days < 100){
|
||||
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()));
|
||||
if(days < 100) {
|
||||
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()));
|
||||
}
|
||||
return QString::QString(tr("Unknown"));
|
||||
return tr("Unknown");
|
||||
}
|
||||
};
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue