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 DOWNLOADTHREAD_H
|
|
|
|
#define DOWNLOADTHREAD_H
|
|
|
|
|
|
|
|
#include <QThread>
|
|
|
|
#include <QFile>
|
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QMutex>
|
|
|
|
#include <QMutexLocker>
|
|
|
|
#include <QWaitCondition>
|
2007-08-08 02:29:46 +04:00
|
|
|
#include <QStringList>
|
2006-09-30 20:02:39 +04:00
|
|
|
#include <iostream>
|
2007-07-22 07:04:47 +04:00
|
|
|
#include <cc++/common.h>
|
2007-07-20 19:55:50 +04:00
|
|
|
|
2007-07-22 07:04:47 +04:00
|
|
|
#ifdef CCXX_NAMESPACES
|
|
|
|
using namespace std;
|
|
|
|
using namespace ost;
|
|
|
|
#endif
|
|
|
|
|
2007-07-22 19:48:34 +04:00
|
|
|
class subDownloadThread : public QThread {
|
2006-09-30 20:02:39 +04:00
|
|
|
Q_OBJECT
|
|
|
|
private:
|
2007-07-22 19:48:34 +04:00
|
|
|
QString url;
|
|
|
|
URLStream url_stream;
|
2007-03-09 21:11:43 +03:00
|
|
|
bool abort;
|
2006-09-30 20:02:39 +04:00
|
|
|
|
|
|
|
public:
|
2007-08-28 20:17:32 +04:00
|
|
|
subDownloadThread(QObject *parent, QString url) : QThread(parent), url(url), abort(false){}
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2007-07-22 19:48:34 +04:00
|
|
|
~subDownloadThread(){
|
2007-03-09 21:11:43 +03:00
|
|
|
abort = true;
|
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
2007-07-22 19:48:34 +04:00
|
|
|
static QString errorCodeToString(URLStream::Error status){
|
2007-07-22 07:04:47 +04:00
|
|
|
switch(status){
|
|
|
|
case URLStream::errUnreachable:
|
|
|
|
return tr("Host is unreachable");
|
|
|
|
case URLStream::errMissing:
|
|
|
|
return tr("File was not found (404)");
|
|
|
|
case URLStream::errDenied:
|
|
|
|
return tr("Connection was denied");
|
|
|
|
case URLStream::errInvalid:
|
|
|
|
return tr("Url is invalid");
|
|
|
|
case URLStream::errForbidden:
|
|
|
|
return tr("Connection forbidden (403)");
|
|
|
|
case URLStream::errUnauthorized:
|
|
|
|
return tr("Connection was not authorized (401)");
|
|
|
|
case URLStream::errRelocated:
|
|
|
|
return tr("Content has moved (301)");
|
|
|
|
case URLStream::errFailure:
|
|
|
|
return tr("Connection failure");
|
|
|
|
case URLStream::errTimeout:
|
|
|
|
return tr("Connection was timed out");
|
|
|
|
case URLStream::errInterface:
|
|
|
|
return tr("Incorrect network interface");
|
|
|
|
default:
|
|
|
|
return tr("Unknown error");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-22 19:48:34 +04:00
|
|
|
signals:
|
|
|
|
// For subthreads
|
|
|
|
void downloadFinishedST(subDownloadThread* st, QString url, QString file_path);
|
|
|
|
void downloadFailureST(subDownloadThread* st, QString url, QString reason);
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void run(){
|
|
|
|
// XXX: Trick to get a unique filename
|
|
|
|
QString filePath;
|
|
|
|
QTemporaryFile *tmpfile = new QTemporaryFile();
|
|
|
|
if (tmpfile->open()) {
|
|
|
|
filePath = tmpfile->fileName();
|
|
|
|
}
|
|
|
|
delete tmpfile;
|
|
|
|
QFile dest_file(filePath);
|
|
|
|
if(!dest_file.open(QIODevice::WriteOnly | QIODevice::Text)){
|
|
|
|
std::cerr << "Error: could't create temporary file: " << (const char*)filePath.toUtf8() << '\n';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
URLStream::Error status = url_stream.get((const char*)url.toUtf8());
|
|
|
|
if(status){
|
|
|
|
// Failure
|
|
|
|
QString error_msg = errorCodeToString(status);
|
|
|
|
qDebug("Download failed for %s, reason: %s", (const char*)url.toUtf8(), (const char*)error_msg.toUtf8());
|
|
|
|
url_stream.close();
|
|
|
|
emit downloadFailureST(this, url, error_msg);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
qDebug("Downloading %s...", (const char*)url.toUtf8());
|
|
|
|
char cbuf[1024];
|
|
|
|
int len;
|
|
|
|
while(!url_stream.eof()) {
|
|
|
|
url_stream.read(cbuf, sizeof(cbuf));
|
|
|
|
len = url_stream.gcount();
|
|
|
|
if(len > 0)
|
|
|
|
dest_file.write(cbuf, len);
|
|
|
|
if(abort){
|
|
|
|
dest_file.close();
|
|
|
|
url_stream.close();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
dest_file.close();
|
|
|
|
url_stream.close();
|
|
|
|
emit downloadFinishedST(this, url, filePath);
|
|
|
|
qDebug("download completed here: %s", (const char*)filePath.toUtf8());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
class downloadThread : public QThread {
|
|
|
|
Q_OBJECT
|
|
|
|
|
|
|
|
private:
|
|
|
|
QStringList url_list;
|
2007-08-27 14:06:13 +04:00
|
|
|
QStringList downloading_list;
|
2007-07-22 19:48:34 +04:00
|
|
|
QMutex mutex;
|
|
|
|
QWaitCondition condition;
|
|
|
|
bool abort;
|
|
|
|
QList<subDownloadThread*> subThreads;
|
|
|
|
|
|
|
|
signals:
|
|
|
|
void downloadFinished(QString url, QString file_path);
|
|
|
|
void downloadFailure(QString url, QString reason);
|
|
|
|
|
|
|
|
public:
|
2007-08-28 20:17:32 +04:00
|
|
|
downloadThread(QObject* parent) : QThread(parent), abort(false){}
|
2007-07-22 19:48:34 +04:00
|
|
|
|
|
|
|
~downloadThread(){
|
|
|
|
mutex.lock();
|
|
|
|
abort = true;
|
|
|
|
condition.wakeOne();
|
|
|
|
mutex.unlock();
|
2007-08-19 17:00:51 +04:00
|
|
|
qDeleteAll(subThreads);
|
2007-07-22 19:48:34 +04:00
|
|
|
wait();
|
|
|
|
}
|
|
|
|
|
|
|
|
void downloadUrl(QString url){
|
|
|
|
QMutexLocker locker(&mutex);
|
2007-08-27 14:06:13 +04:00
|
|
|
if(downloading_list.contains(url)) return;
|
2007-07-22 19:48:34 +04:00
|
|
|
url_list << url;
|
2007-08-27 14:06:13 +04:00
|
|
|
downloading_list << url;
|
2007-07-22 19:48:34 +04:00
|
|
|
if(!isRunning()){
|
|
|
|
start();
|
|
|
|
}else{
|
|
|
|
condition.wakeOne();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-03-09 21:11:43 +03:00
|
|
|
protected:
|
2006-09-30 20:02:39 +04:00
|
|
|
void run(){
|
|
|
|
forever{
|
2007-03-09 21:11:43 +03:00
|
|
|
if(abort)
|
|
|
|
return;
|
2006-09-30 20:02:39 +04:00
|
|
|
mutex.lock();
|
|
|
|
if(url_list.size() != 0){
|
|
|
|
QString url = url_list.takeFirst();
|
|
|
|
mutex.unlock();
|
2007-07-22 19:48:34 +04:00
|
|
|
subDownloadThread *st = new subDownloadThread(0, url);
|
|
|
|
subThreads << st;
|
|
|
|
connect(st, SIGNAL(downloadFinishedST(subDownloadThread*, QString, QString)), this, SLOT(propagateDownloadedFile(subDownloadThread*, QString, QString)));
|
|
|
|
connect(st, SIGNAL(downloadFailureST(subDownloadThread*, QString, QString)), this, SLOT(propagateDownloadFailure(subDownloadThread*, QString, QString)));
|
|
|
|
st->start();
|
2006-09-30 20:02:39 +04:00
|
|
|
}else{
|
2007-03-09 21:11:43 +03:00
|
|
|
condition.wait(&mutex);
|
2006-09-30 20:02:39 +04:00
|
|
|
mutex.unlock();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2007-07-22 17:21:04 +04:00
|
|
|
protected slots:
|
2007-07-22 19:48:34 +04:00
|
|
|
void propagateDownloadedFile(subDownloadThread* st, QString url, QString path){
|
2007-07-22 17:21:04 +04:00
|
|
|
int index = subThreads.indexOf(st);
|
2007-07-31 12:52:04 +04:00
|
|
|
Q_ASSERT(index != -1);
|
|
|
|
subThreads.removeAt(index);
|
2007-07-22 17:21:04 +04:00
|
|
|
delete st;
|
|
|
|
emit downloadFinished(url, path);
|
2007-08-27 14:06:13 +04:00
|
|
|
mutex.lock();
|
|
|
|
index = downloading_list.indexOf(url);
|
|
|
|
Q_ASSERT(index != -1);
|
|
|
|
downloading_list.removeAt(index);
|
|
|
|
mutex.unlock();
|
2007-07-22 17:21:04 +04:00
|
|
|
}
|
|
|
|
|
2007-07-22 19:48:34 +04:00
|
|
|
void propagateDownloadFailure(subDownloadThread* st, QString url, QString reason){
|
2007-07-22 17:21:04 +04:00
|
|
|
int index = subThreads.indexOf(st);
|
2007-07-31 12:52:04 +04:00
|
|
|
Q_ASSERT(index != -1);
|
|
|
|
subThreads.removeAt(index);
|
2007-07-22 17:21:04 +04:00
|
|
|
delete st;
|
|
|
|
emit downloadFailure(url, reason);
|
2007-08-27 14:06:13 +04:00
|
|
|
mutex.lock();
|
|
|
|
index = downloading_list.indexOf(url);
|
|
|
|
Q_ASSERT(index != -1);
|
|
|
|
downloading_list.removeAt(index);
|
|
|
|
mutex.unlock();
|
2007-07-22 17:21:04 +04:00
|
|
|
}
|
2006-09-30 20:02:39 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
#endif
|