1 #include <QCoreApplication>
7 #include <QNetworkAccessManager>
8 #include <QNetworkReply>
9 #include <QNetworkRequest>
11 #include <QStringList>
23 #include "download-agent.h"
24 #include "mainwindow.h"
28 extern Main *mainWindow;
29 extern QString vymVersion;
30 extern QString vymPlatform;
31 extern QDir tmpVymDir;
32 extern Settings settings;
35 DownloadAgent::DownloadAgent(const QUrl &u)
37 // qDebug() << "Constr. DownloadAgent: " << u.toString();
40 finishedScriptModelID = 0;
42 connect(&agent, SIGNAL(finished(QNetworkReply *)),
43 SLOT(requestFinished(QNetworkReply *)));
46 QString("vym %1 ( %2)").arg(vymVersion).arg(vymPlatform).toUtf8();
49 DownloadAgent::~DownloadAgent()
51 // qDebug() << "Destr. DownloadAgent";
52 // qDebug() << getFinishedScript();
55 QString DownloadAgent::getDestination() { return tmpFilePath; }
57 bool DownloadAgent::isSuccess() { return success; }
59 QString DownloadAgent::getResultMessage() { return resultMessage; }
61 void DownloadAgent::setFinishedAction(VymModel *m, const QString &script)
63 finishedScriptModelID = m->getModelID();
64 finishedScript = script;
67 uint DownloadAgent::getFinishedScriptModelID() { return finishedScriptModelID; }
69 QString DownloadAgent::getFinishedScript() { return finishedScript; }
71 void DownloadAgent::setUserAgent(const QString &s)
73 userAgent = s.toLocal8Bit();
76 void DownloadAgent::doDownload(const QUrl &url)
78 QNetworkRequest request(url);
79 if (!userAgent.isEmpty())
80 request.setRawHeader("User-Agent", userAgent);
82 // Only send cookies if talking to my own domain
83 bool useCookies = false;
84 if (url.host().contains("insilmaril.de"))
89 qDebug() << "DownloadAgent::doDownload Using cookies to download "
91 QByteArray idCookieValue =
92 settings.value("/downloads/cookies/vymID/value", QByteArray())
94 // idCookieValue = QVariant("2000000002601").toByteArray(); //TESTING!!!
95 // qDebug()<<"idCookie="<<idCookieValue;
96 if (idCookieValue.size() != 0) {
97 QNetworkCookie idCookie;
98 QDate expDate(2099, 1, 1);
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);
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);
117 QNetworkReply *reply = agent.get(request);
118 connect(reply, SIGNAL(sslErrors(QList<QSslError>)),
119 SLOT(sslErrors(QList<QSslError>)));
121 currentDownloads.append(reply);
124 bool DownloadAgent::saveToDisk(const QString &filename, const QByteArray &data)
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()));
139 void DownloadAgent::execute() { doDownload(url); }
141 void DownloadAgent::sslErrors(const QList<QSslError> &sslErrors)
143 #ifndef QT_NO_OPENSSL
144 foreach (const QSslError &error, sslErrors)
145 fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
149 void DownloadAgent::requestFinished(QNetworkReply *reply)
151 QUrl url = reply->url();
152 if (reply->error()) {
154 resultMessage = reply->errorString();
155 emit(downloadFinished());
161 qDebug() << "\n* DownloadAgent::reqFinished: ";
162 QList<QNetworkCookie> cookies =
163 reply->manager()->cookieJar()->cookiesForUrl(url);
164 foreach (QNetworkCookie c, cookies) {
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();
175 if (c.name() == "vymID") {
176 settings.setValue("/downloads/cookies/vymID/value", c.value());
177 // settings.setValue( "/downloads/cookies/vymID/expires",
178 // c.expirationDate());
182 QByteArray data = reply->readAll();
183 QTemporaryFile tmpFile(tmpVymDir.path() + "/download-XXXXXX");
184 tmpFile.setAutoRemove(
185 false); // tmpFile is within tmpDir, removed automatically later
188 QMessageBox::warning(0, tr("Warning"),
189 "Couldn't open tmpFile " + tmpFile.fileName());
191 if (!saveToDisk(tmpFile.fileName(), data))
192 QMessageBox::warning(0, tr("Warning"),
193 "Couldn't write to " + tmpFile.fileName());
195 tmpFilePath = tmpFile.fileName();
196 resultMessage = QString("saved to %1").arg(tmpFile.fileName());
200 qDebug() << "DownloadAgent: resultMessage = " << resultMessage;
201 emit(downloadFinished());
204 currentDownloads.removeAll(reply);
205 reply->deleteLater();