1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
/*
* juickdownloader.cpp - plugin
* Copyright (C) 2012 Evgeny Khryukin
*
* 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.
*
* 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
* along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
*/
#include "juickdownloader.h"
#include "applicationinfoaccessinghost.h"
#include "defines.h"
#include <QNetworkProxy>
#include <QNetworkReply>
#include <QMessageBox>
#include <QFile>
#include <QTimer>
#include <QDebug>
const int DOWNLOAD_TIMEOUT = 60000;
static void save(const QString &path, const QByteArray &img)
{
QFile file(path);
if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
file.write(img);
}
else
QMessageBox::warning(0, QObject::tr("Warning"), QObject::tr("Cannot write to file %1:\n%2.")
.arg(file.fileName())
.arg(file.errorString()));
}
JuickDownloader::JuickDownloader(ApplicationInfoAccessingHost *host, QObject *p)
: QObject(p)
, inProgress_(false)
, manager_(new QNetworkAccessManager(this))
, appInfo_(host)
, waitTimer_(new QTimer(this))
{
connect(manager_, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
waitTimer_->setSingleShot(true);
waitTimer_->setInterval(1000);
connect(waitTimer_, SIGNAL(timeout()), SLOT(timeOut()));
// qRegisterMetaType<JuickDownloadItem>("JuickDownloadItem");
}
void JuickDownloader::get(const JuickDownloadItem &item)
{
if(waitTimer_->isActive())
waitTimer_->stop();
items_.enqueue(item);
Proxy prx = appInfo_->getProxyFor(constPluginName);
setProxyHostPort(prx.host, prx.port, prx.user, prx.pass, prx.type);
if(!inProgress_) {
peekNext();
}
}
void JuickDownloader::setProxyHostPort(const QString& host, int port, const QString& username, const QString& pass, const QString& type)
{
QNetworkProxy prx;
if(!host.isEmpty()) {
prx.setType(QNetworkProxy::HttpCachingProxy);
if(type == "socks")
prx.setType(QNetworkProxy::Socks5Proxy);
prx.setPort(port);
prx.setHostName(host);
if(!username.isEmpty()) {
prx.setUser(username);
prx.setPassword(pass);
}
}
manager_->setProxy(prx);
}
void JuickDownloader::peekNext()
{
if(items_.isEmpty()) {
inProgress_ = false;
waitTimer_->start();
}
else {
inProgress_ = true;
JuickDownloadItem it = items_.dequeue();
QNetworkRequest request;
request.setUrl(QUrl(it.url));
request.setRawHeader("User-Agent", "Juick Plugin (Psi+)");
QNetworkReply *reply = manager_->get(request);
QVariant v;
v.setValue(it);
reply->setProperty("jdi", v);
}
}
void JuickDownloader::requestFinished(QNetworkReply *reply)
{
if (reply->error() == QNetworkReply::NoError ) {
QByteArray ba = reply->readAll();
JuickDownloadItem it = reply->property("jdi").value<JuickDownloadItem>();
dataReady(ba, it);
}
else {
qDebug() << reply->errorString();
}
reply->deleteLater();
peekNext();
}
void JuickDownloader::timeOut()
{
emit finished(urls_);
urls_.clear();
}
void JuickDownloader::dataReady(const QByteArray &ba, const JuickDownloadItem& it)
{
urls_.append(QUrl::fromLocalFile(it.path).toEncoded());
save(it.path, ba);
}
|