diff --git a/src/downloadThread.cpp b/src/downloadThread.cpp index 37f095f31..089bd4b64 100644 --- a/src/downloadThread.cpp +++ b/src/downloadThread.cpp @@ -24,6 +24,8 @@ #include #include +#define MAX_THREADS 3 + // http://curl.rtin.bz/libcurl/c/libcurl-errors.html QString subDownloadThread::errorCodeToString(CURLcode status) { switch(status){ @@ -150,9 +152,7 @@ downloadThread::~downloadThread(){ void downloadThread::downloadUrl(QString url){ QMutexLocker locker(&mutex); - if(downloading_list.contains(url)) return; - url_list << url; - downloading_list << url; + urls_queue.enqueue(url); if(!isRunning()){ start(); }else{ @@ -165,8 +165,8 @@ void downloadThread::run(){ if(abort) return; mutex.lock(); - if(url_list.size() != 0){ - QString url = url_list.takeFirst(); + if(!urls_queue.empty() && subThreads.size() < MAX_THREADS){ + QString url = urls_queue.dequeue(); mutex.unlock(); subDownloadThread *st = new subDownloadThread(0, url); subThreads << st; @@ -187,9 +187,9 @@ void downloadThread::propagateDownloadedFile(subDownloadThread* st, QString url, delete st; emit downloadFinished(url, path); mutex.lock(); - index = downloading_list.indexOf(url); - Q_ASSERT(index != -1); - downloading_list.removeAt(index); + if(!urls_queue.empty()) { + condition.wakeOne(); + } mutex.unlock(); } @@ -200,8 +200,8 @@ void downloadThread::propagateDownloadFailure(subDownloadThread* st, QString url delete st; emit downloadFailure(url, reason); mutex.lock(); - index = downloading_list.indexOf(url); - Q_ASSERT(index != -1); - downloading_list.removeAt(index); + if(!urls_queue.empty()) { + condition.wakeOne(); + } mutex.unlock(); } diff --git a/src/downloadThread.h b/src/downloadThread.h index 752b8f534..15d7a39c0 100644 --- a/src/downloadThread.h +++ b/src/downloadThread.h @@ -30,6 +30,7 @@ #include #include #include +#include class subDownloadThread : public QThread { Q_OBJECT @@ -55,8 +56,7 @@ class downloadThread : public QThread { Q_OBJECT private: - QStringList url_list; - QStringList downloading_list; + QQueue urls_queue; QMutex mutex; QWaitCondition condition; bool abort; diff --git a/src/torrentAddition.h b/src/torrentAddition.h index 766db1921..bd65520c0 100644 --- a/src/torrentAddition.h +++ b/src/torrentAddition.h @@ -80,7 +80,7 @@ class torrentAdditionDialog : public QDialog, private Ui_addTorrentDialog{ connect(actionHigh, SIGNAL(triggered()), this, SLOT(highSelection())); connect(actionMaximum, SIGNAL(triggered()), this, SLOT(maximumSelection())); torrentContentList->header()->resizeSection(0, 200); - torrentContentList->header()->setResizeMode(0, QHeaderView::Stretch); + //torrentContentList->header()->setResizeMode(0, QHeaderView::Stretch); QString home = QDir::homePath(); if(home[home.length()-1] != QDir::separator()){ home += QDir::separator();