]> git.sven.stormbind.net Git - sven/vym.git/blob - src/download-agent.cpp
New upstream version 2.9.22
[sven/vym.git] / src / download-agent.cpp
1 #include <QCoreApplication>
2 #include <QDebug>
3 #include <QFile>
4 #include <QFileInfo>
5 #include <QList>
6 #include <QMessageBox>
7 #include <QNetworkAccessManager>
8 #include <QNetworkReply>
9 #include <QNetworkRequest>
10 #include <QSslError>
11 #include <QStringList>
12 #include <QTimer>
13 #include <QUrl>
14
15 #include <stdio.h>
16
17 QT_BEGIN_NAMESPACE
18 class QSslError;
19 QT_END_NAMESPACE
20
21 QT_USE_NAMESPACE
22
23 #include "download-agent.h"
24 #include "mainwindow.h"
25 #include "settings.h"
26 #include "vymmodel.h"
27
28 extern Main *mainWindow;
29 extern QString vymVersion;
30 extern QString vymPlatform;
31 extern QDir tmpVymDir;
32 extern Settings settings;
33 extern bool debug;
34
35 DownloadAgent::DownloadAgent(const QUrl &u)
36 {
37     // qDebug() << "Constr. DownloadAgent: " << u.toString();
38     tmpFilePath = "";
39
40     finishedScriptModelID = 0;
41     url = u;
42     connect(&agent, SIGNAL(finished(QNetworkReply *)),
43             SLOT(requestFinished(QNetworkReply *)));
44
45     userAgent =
46         QString("vym %1 ( %2)").arg(vymVersion).arg(vymPlatform).toUtf8();
47 }
48
49 DownloadAgent::~DownloadAgent()
50 {
51     // qDebug() << "Destr. DownloadAgent";
52     // qDebug() << getFinishedScript();
53 }
54
55 QString DownloadAgent::getDestination() { return tmpFilePath; }
56
57 bool DownloadAgent::isSuccess() { return success; }
58
59 QString DownloadAgent::getResultMessage() { return resultMessage; }
60
61 void DownloadAgent::setFinishedAction(VymModel *m, const QString &script)
62 {
63     finishedScriptModelID = m->getModelID();
64     finishedScript = script;
65 }
66
67 uint DownloadAgent::getFinishedScriptModelID() { return finishedScriptModelID; }
68
69 QString DownloadAgent::getFinishedScript() { return finishedScript; }
70
71 void DownloadAgent::setUserAgent(const QString &s)
72 {
73     userAgent = s.toLocal8Bit();
74 }
75
76 void DownloadAgent::doDownload(const QUrl &url)
77 {
78     QNetworkRequest request(url);
79     if (!userAgent.isEmpty())
80         request.setRawHeader("User-Agent", userAgent);
81
82     // Only send cookies if talking to my own domain
83     bool useCookies = false;
84     if (url.host().contains("insilmaril.de"))
85         useCookies = true;
86
87     if (useCookies) {
88         if (debug)
89             qDebug() << "DownloadAgent::doDownload  Using cookies to download "
90                      << url.toString();
91         QByteArray idCookieValue =
92             settings.value("/downloads/cookies/vymID/value", QByteArray())
93                 .toByteArray();
94         // idCookieValue = QVariant("2000000002601").toByteArray(); //TESTING!!!
95         // qDebug()<<"idCookie="<<idCookieValue;
96         if (idCookieValue.size() != 0) {
97             QNetworkCookie idCookie;
98             QDate expDate(2099, 1, 1);
99
100             idCookie.setPath("/");
101             idCookie.setDomain("www.insilmaril.de");
102             idCookie.setName("vymID");
103             idCookie.setValue(idCookieValue);
104             idCookie.setExpirationDate( expDate.startOfDay() );
105             agent.cookieJar()->insertCookie(idCookie);
106
107             QNetworkCookie platformCookie;
108             platformCookie.setPath("/");
109             platformCookie.setDomain("www.insilmaril.de");
110             platformCookie.setName("vymPlatform");
111             platformCookie.setValue(QVariant(vymPlatform).toByteArray());
112             platformCookie.setExpirationDate( expDate.startOfDay());
113             agent.cookieJar()->insertCookie(platformCookie);
114         }
115     }
116
117     QNetworkReply *reply = agent.get(request);
118     connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
119             SLOT(sslErrors(QList<QSslError>)));
120
121     currentDownloads.append(reply);
122 }
123
124 bool DownloadAgent::saveToDisk(const QString &filename, const QByteArray &data)
125 {
126     QFile file(filename);
127     if (!file.open(QIODevice::WriteOnly)) {
128         fprintf(stderr, "Could not open %s for writing: %s\n",
129                 qPrintable(filename), qPrintable(file.errorString()));
130         return false;
131     }
132
133     file.write(data);
134     file.close();
135
136     return true;
137 }
138
139 void DownloadAgent::execute() { doDownload(url); }
140
141 void DownloadAgent::sslErrors(const QList<QSslError> &sslErrors)
142 {
143 #ifndef QT_NO_OPENSSL
144     foreach (const QSslError &error, sslErrors)
145         fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
146 #endif
147 }
148
149 void DownloadAgent::requestFinished(QNetworkReply *reply)
150 {
151     QUrl url = reply->url();
152     if (reply->error()) {
153         success = false;
154         resultMessage = reply->errorString();
155         emit(downloadFinished());
156     }
157     else {
158         success = true;
159
160         if (debug)
161             qDebug() << "\n* DownloadAgent::reqFinished: ";
162         QList<QNetworkCookie> cookies =
163             reply->manager()->cookieJar()->cookiesForUrl(url);
164         foreach (QNetworkCookie c, cookies) {
165             if (debug) {
166                 qDebug() << "           url: " << url.toString();
167                 qDebug() << "   cookie name: " << c.name();
168                 qDebug() << "   cookie path: " << c.path();
169                 qDebug() << "  cookie value: " << c.value();
170                 qDebug() << " cookie domain: " << c.domain();
171                 qDebug() << " cookie exdate: "
172                          << c.expirationDate().toLocalTime().toString();
173             }
174
175             if (c.name() == "vymID") {
176                 settings.setValue("/downloads/cookies/vymID/value", c.value());
177                 // settings.setValue( "/downloads/cookies/vymID/expires",
178                 // c.expirationDate());
179             }
180         }
181
182         QByteArray data = reply->readAll();
183         QTemporaryFile tmpFile(tmpVymDir.path() + "/download-XXXXXX");
184         tmpFile.setAutoRemove(
185             false); // tmpFile is within tmpDir, removed automatically later
186
187         if (!tmpFile.open())
188             QMessageBox::warning(0, tr("Warning"),
189                                  "Couldn't open tmpFile " + tmpFile.fileName());
190         else {
191             if (!saveToDisk(tmpFile.fileName(), data))
192                 QMessageBox::warning(0, tr("Warning"),
193                                      "Couldn't write to " + tmpFile.fileName());
194             else {
195                 tmpFilePath = tmpFile.fileName();
196                 resultMessage = QString("saved to %1").arg(tmpFile.fileName());
197             }
198         }
199         if (debug)
200             qDebug() << "DownloadAgent:  resultMessage  = " << resultMessage;
201         emit(downloadFinished());
202     }
203
204     currentDownloads.removeAll(reply);
205     reply->deleteLater();
206 }