2
* juickdownloader.cpp - plugin
3
* Copyright (C) 2012 Khryukin Evgeny
5
* This program is free software; you can redistribute it and/or
6
* modify it under the terms of the GNU General Public License
7
* as published by the Free Software Foundation; either version 2
8
* of the License, or (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this library; if not, write to the Free Software
17
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21
#include "juickdownloader.h"
22
#include "applicationinfoaccessinghost.h"
25
#include <QNetworkProxy>
26
#include <QNetworkReply>
27
#include <QMessageBox>
33
const int DOWNLOAD_TIMEOUT = 60000;
35
static void save(const QString &path, const QByteArray &img)
39
if(file.open(QIODevice::WriteOnly | QIODevice::Truncate)) {
43
QMessageBox::warning(0, QObject::tr("Warning"), QObject::tr("Cannot write to file %1:\n%2.")
45
.arg(file.errorString()));
50
JuickDownloader::JuickDownloader(ApplicationInfoAccessingHost *host, QObject *p)
53
, manager_(new QNetworkAccessManager(this))
55
, waitTimer_(new QTimer(this))
57
connect(manager_, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
59
waitTimer_->setSingleShot(true);
60
waitTimer_->setInterval(1000);
61
connect(waitTimer_, SIGNAL(timeout()), SLOT(timeOut()));
63
// qRegisterMetaType<JuickDownloadItem>("JuickDownloadItem");
66
void JuickDownloader::get(const JuickDownloadItem &item)
68
if(waitTimer_->isActive())
72
Proxy prx = appInfo_->getProxyFor(constPluginName);
73
setProxyHostPort(prx.host, prx.port, prx.user, prx.pass, prx.type);
80
void JuickDownloader::setProxyHostPort(const QString& host, int port, const QString& username, const QString& pass, const QString& type)
85
prx.setType(QNetworkProxy::HttpCachingProxy);
87
prx.setType(QNetworkProxy::Socks5Proxy);
89
prx.setHostName(host);
90
if(!username.isEmpty()) {
91
prx.setUser(username);
92
prx.setPassword(pass);
96
manager_->setProxy(prx);
99
void JuickDownloader::peekNext()
101
if(items_.isEmpty()) {
107
JuickDownloadItem it = items_.dequeue();
108
QNetworkRequest request;
109
request.setUrl(QUrl(it.url));
110
request.setRawHeader("User-Agent", "Juick Plugin (Psi+)");
111
QNetworkReply *reply = manager_->get(request);
114
reply->setProperty("jdi", v);
118
void JuickDownloader::requestFinished(QNetworkReply *reply)
120
if (reply->error() == QNetworkReply::NoError ) {
121
QByteArray ba = reply->readAll();
122
JuickDownloadItem it = reply->property("jdi").value<JuickDownloadItem>();
126
qDebug() << reply->errorString();
129
reply->deleteLater();
133
void JuickDownloader::timeOut()
135
emit finished(urls_);
140
void JuickDownloader::dataReady(const QByteArray &ba, const JuickDownloadItem& it)
142
urls_.append(QUrl::fromLocalFile(it.path).toEncoded());