~ubuntu-branches/ubuntu/vivid/psi-plus/vivid

« back to all changes in this revision

Viewing changes to src/plugins/generic/juickplugin/juickdownloader.cpp

  • Committer: Package Import Robot
  • Author(s): Boris Pek
  • Date: 2012-06-22 00:00:09 UTC
  • mfrom: (1.4.4)
  • Revision ID: package-import@ubuntu.com-20120622000009-9ur1mi189ulab5af
Tags: 0.15.5338-1
* New upstream release.
* debian/copyright was updated: updated years.
* Used hardening flags in compiler options:
  - added file debian/patches/add-hardening-flags-in-compiler-options
  - updated debian/rules

Show diffs side-by-side

added added

removed removed

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