diff options
author | craig <craig@11d20701-8431-0410-a711-e3c959e3b870> | 2012-05-31 20:25:57 +0000 |
---|---|---|
committer | craig <craig@11d20701-8431-0410-a711-e3c959e3b870> | 2012-05-31 20:25:57 +0000 |
commit | d2ea48aace0d70a369d7f288d08615c475c7e5a6 (patch) | |
tree | ccab0059dcfaab01f8ab41ecc1aaa15e2cc12e07 | |
parent | 3e45d96fc4cc935f1a318da181f9adafcbd4e7e4 (diff) | |
download | scribus-d2ea48aace0d70a369d7f288d08615c475c7e5a6.tar.gz scribus-d2ea48aace0d70a369d7f288d08615c475c7e5a6.tar.xz scribus-d2ea48aace0d70a369d7f288d08615c475c7e5a6.zip |
Add beginnings of threaded download manager
git-svn-id: svn://scribus.net/branches/Version14x/Scribus@17531 11d20701-8431-0410-a711-e3c959e3b870
-rw-r--r-- | scribus/downloadmanager/scdlmgr.cpp | 45 | ||||
-rw-r--r-- | scribus/downloadmanager/scdlmgr.h | 39 | ||||
-rw-r--r-- | scribus/downloadmanager/scdlthread.cpp | 170 | ||||
-rw-r--r-- | scribus/downloadmanager/scdlthread.h | 47 |
4 files changed, 301 insertions, 0 deletions
diff --git a/scribus/downloadmanager/scdlmgr.cpp b/scribus/downloadmanager/scdlmgr.cpp new file mode 100644 index 0000000..455011f --- /dev/null +++ b/scribus/downloadmanager/scdlmgr.cpp @@ -0,0 +1,45 @@ +#include "scdlmgr.h" +#include "scdlthread.h" + +#include <QDebug> + +#include <QString> +#include <QStringList> +#include <QTimer> + +#include <stdio.h> + +ScDLManager::ScDLManager(QObject *parent) + : QObject(parent) +{ + thread=new ScDLThread(); + connect(thread, SIGNAL(received(const QString &)), this, SLOT(updateText(const QString&))); + connect(thread, SIGNAL(finished()), this, SIGNAL(finished())); +} + +void ScDLManager::addURL(const QUrl &url, bool overwrite, const QString& location) +{ + thread->addURL(url, overwrite, location); +} + +void ScDLManager::addURL(const QString &url, bool overwrite, const QString &location) +{ + thread->addURL(QUrl(url), overwrite, location); +} + +void ScDLManager::addURLs(const QStringList &urlList, bool overwrite, const QString &location) +{ + thread->addURLs(urlList, overwrite, location); +} + +void ScDLManager::startDownloads() +{ + qDebug()<<"Manager starting downloads..."; + thread->startDownloads(); +} + +void ScDLManager::updateText(const QString& t) +{ + emit fileReceived(t); + qDebug()<<t; +} diff --git a/scribus/downloadmanager/scdlmgr.h b/scribus/downloadmanager/scdlmgr.h new file mode 100644 index 0000000..b9c88fc --- /dev/null +++ b/scribus/downloadmanager/scdlmgr.h @@ -0,0 +1,39 @@ +#ifndef SCDLMANAGER_H +#define SCDLMANAGER_H + +#include <QFile> +#include <QList> +#include <QNetworkAccessManager> +#include <QObject> +#include <QQueue> +#include <QStringList> +#include <QThread> +#include <QTime> +#include <QUrl> +#include "scdlthread.h" +#include <QDialog> + +class ScDLManager: public QObject +{ + Q_OBJECT + public: + ScDLManager(QObject *parent = 0); +//TODO: Add download groups so different parts of Scribus can be downloading at the same time + + void addURL(const QUrl &url, bool overwrite, const QString &location=""); + void addURL(const QString &url, bool overwrite, const QString &location=""); + void addURLs(const QStringList &urlList, bool overwrite, const QString &location=""); + void startDownloads(); + + public slots: + void updateText(const QString& t); + + signals: + void finished(); + void fileReceived(const QString& t); + + private: + ScDLThread *thread; +}; + +#endif diff --git a/scribus/downloadmanager/scdlthread.cpp b/scribus/downloadmanager/scdlthread.cpp new file mode 100644 index 0000000..5be2c15 --- /dev/null +++ b/scribus/downloadmanager/scdlthread.cpp @@ -0,0 +1,170 @@ + +#include "scdlmgr.h" +#include "scdlthread.h" +#include "scpaths.h" + +#include <QDebug> +#include <QDir> +#include <QFileInfo> +#include <QNetworkReply> +#include <QNetworkRequest> +#include <QUrl> + + +ScDLThread::ScDLThread(QObject *parent) : QThread(parent), + downloadedCount(0), totalCount(0) +{ + connect(this, SIGNAL(runSignal()), this, SLOT(runSlot())); +} + +ScDLThread::~ScDLThread() +{ +} + +void ScDLThread::run() +{ + emit(runSignal()); +} + +void ScDLThread::addURL(const QUrl &url, bool overwrite, const QString& location) +{ + qDebug()<<"ScDLThread::addURL:"<<url; + if (!urlOK(url)) + return; + QString l(QDir::cleanPath(location)); + if (!l.endsWith("/")) + l += "/"; + downloadQueue.enqueue(qMakePair(url, l)); + ++totalCount; +} + +void ScDLThread::addURLs(const QStringList &urlList, bool overwrite, const QString& location) +{ + qDebug()<<"ScDLThread::addURLs:"<<urlList; + m_urlList=urlList; + QString l(QDir::cleanPath(location)); + if (!l.endsWith("/")) + l += "/"; + foreach (QString u, m_urlList) + { + QUrl url(u); + if (!urlOK(u)) + return; + downloadQueue.enqueue(qMakePair(url, l)); + ++totalCount; + } +} + +void ScDLThread::startDownloads() +{ + if (downloadQueue.isEmpty()) + { + qDebug()<<"No more downloads left"; + emit finished(); + return; + } + start(); +} + +QString ScDLThread::saveFileName(const QUrl &url, const QString& location, bool overwrite) +{ + QDir d(location); + QString path = url.path(); + QString basename = QFileInfo(path).fileName(); + if (location.isEmpty() || !d.exists()) + { + if (basename.isEmpty()) + basename = "download"; + basename = ScPaths::downloadDir() + basename; + } + else + { + basename = location + basename; + } + if (!overwrite && QFile::exists(basename)) + { + // already exists, don't overwrite + int i = 0; + basename += '.'; + while (QFile::exists(basename + QString::number(i))) + ++i; + basename += QString::number(i); + } + return basename; +} + +void ScDLThread::startNextDownload() +{ + if (downloadQueue.isEmpty()) + { + qDebug()<<downloadedCount<<"/"<<totalCount<<"files downloaded successfully"; + emit finished(); + return; + } + QPair<QUrl, QString> urlPair=downloadQueue.dequeue(); + + QString filename = saveFileName(urlPair.first, urlPair.second, true); + if (filename.isEmpty()) + { + qDebug()<<"File name empty for url:"<<urlPair.first.toEncoded().constData(); + return; + } + output.setFileName(filename); + if (!output.open(QIODevice::WriteOnly)) + { + qDebug()<<"Problem opening save file '"<<qPrintable(filename)<<"' for download '" + <<urlPair.first.toEncoded().constData()<<"': "<<qPrintable(output.errorString()); + + startNextDownload(); + return; + } + + QNetworkRequest request(urlPair.first); + currentDownload = manager.get(request); + connect(currentDownload, SIGNAL(finished()), SLOT(downloadFinished())); + connect(currentDownload, SIGNAL(readyRead()), SLOT(downloadReadyRead())); + + qDebug()<<"Downloading:"<<urlPair.first.toEncoded().constData(); +} + +void ScDLThread::downloadFinished() +{ + output.close(); + + if (currentDownload->error()) + qDebug()<<"Failed: "<<qPrintable(currentDownload->errorString()); + else + { + printf("Succeeded.\n"); + qDebug()<<"Saving file:"<<qPrintable(output.fileName()); + ++downloadedCount; + } + emit (received(output.fileName())); + currentDownload->deleteLater(); + startNextDownload(); +} + + +void ScDLThread::downloadReadyRead() +{ + output.write(currentDownload->readAll()); +} + +void ScDLThread::runSlot() +{ + startNextDownload(); +} + +bool ScDLThread::urlOK(QUrl url) +{ + //TODO: Add some more URL checks + if (!url.isValid() || url.isEmpty() || url.host().isEmpty()) + { + qDebug()<<"URL invalid:"<<url; + return false; + } + return true; +} + + + diff --git a/scribus/downloadmanager/scdlthread.h b/scribus/downloadmanager/scdlthread.h new file mode 100644 index 0000000..7d5b4eb --- /dev/null +++ b/scribus/downloadmanager/scdlthread.h @@ -0,0 +1,47 @@ +#ifndef SCDLTHREAD_H +#define SCDLTHREAD_H + +#include <QThread> +#include <QUrl> +#include <QStringList> +#include <QPair> +#include <QString> +#include <QQueue> +#include <QFile> +#include <QNetworkAccessManager> + +class ScDLThread : public QThread +{ + Q_OBJECT + public: + ScDLThread(QObject * parent = 0); + ~ScDLThread(); + void run(); + + void addURL(const QUrl &url, bool overwrite, const QString& location=""); + void addURLs(const QStringList &urlList, bool overwrite, const QString& location=""); + void startDownloads(); + QString saveFileName(const QUrl &url, const QString &location, bool overwrite); + + signals: + void finished(); + void runSignal(); + void received(const QString &); + + private slots: + void startNextDownload(); + void downloadFinished(); + void downloadReadyRead(); + void runSlot(); + + private: + bool urlOK(QUrl url); + QStringList m_urlList; + QFile output; + QQueue<QPair<QUrl, QString> > downloadQueue; + int downloadedCount; + int totalCount; + QNetworkReply *currentDownload; + QNetworkAccessManager manager; +}; +#endif |