1 #include <QCoreApplication>
7 #include <QNetworkAccessManager>
8 #include <QNetworkRequest>
9 #include <QNetworkReply>
11 #include <QStringList>
23 #include "downloadagent.h"
24 #include "mainwindow.h"
28 extern Main *mainWindow;
29 extern QString vymVersion;
30 extern QString vymPlatform;
31 extern QString tmpVymDir;
32 extern Settings settings;
35 DownloadAgent::DownloadAgent(const QUrl &u)
37 finishedScriptModelID = 0;
39 connect(&agent, SIGNAL(finished(QNetworkReply*)),
40 SLOT(requestFinished(QNetworkReply*)));
42 userAgent = QString("vym %1 ( %2)")
44 .arg(vymPlatform).toUtf8();
47 QString DownloadAgent::getDestination()
49 return tmpFile.fileName();
52 bool DownloadAgent::isSuccess()
57 QString DownloadAgent::getResultMessage()
62 void DownloadAgent::setFinishedAction (VymModel *m, const QString &script)
64 finishedScriptModelID = m->getModelID();
65 finishedScript = script;
68 uint DownloadAgent::getFinishedScriptModelID()
70 return finishedScriptModelID;
73 QString DownloadAgent::getFinishedScript ()
75 return finishedScript;
78 void DownloadAgent::setUserAgent(const QString &s)
80 userAgent = s.toLocal8Bit();
83 void DownloadAgent::doDownload(const QUrl &url)
85 QNetworkRequest request(url);
86 if (!userAgent.isEmpty()) request.setRawHeader("User-Agent", userAgent);
88 // Only send cookies if talking to my own domain
89 bool useCookies = false;
90 if (url.host().contains("insilmaril.de") ) useCookies = true;
94 if (debug) qDebug() << "DownloadAgent::doDownload Using cookies to download " << url.toString();
95 QByteArray idCookieValue = settings.value("/downloads/cookies/vymID/value",QByteArray() ).toByteArray();
96 //idCookieValue = QVariant("2000000002601").toByteArray(); //TESTING!!!
97 //qDebug()<<"idCookie="<<idCookieValue;
98 if (!idCookieValue.size() == 0 )
100 QNetworkCookie idCookie;
101 idCookie.setPath("/");
102 // idCookie.setDomain("localhost");
103 idCookie.setDomain("www.insilmaril.de");
104 idCookie.setName("vymID");
105 idCookie.setValue(idCookieValue);
106 //idCookie.setExpirationDate( settings.value("/downloads/cookies/id/expires", QVariant(QDateTime::currentDateTime().addSecs(60) )).toDateTime() ); // testing
107 idCookie.setExpirationDate( QDateTime( QDate(2099,1,1) ) );
108 agent.cookieJar()->insertCookie(idCookie);
110 QNetworkCookie platformCookie;
111 platformCookie.setPath("/");
112 // platformCookie.setDomain("localhost");
113 platformCookie.setDomain("www.insilmaril.de");
114 platformCookie.setName("vymPlatform");
115 platformCookie.setValue( QVariant(vymPlatform).toByteArray() );
116 platformCookie.setExpirationDate( QDateTime( QDate(2099,1,1) ) );
117 agent.cookieJar()->insertCookie(platformCookie);
121 QNetworkReply *reply = agent.get(request);
122 connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(sslErrors(QList<QSslError>)));
124 currentDownloads.append(reply);
127 bool DownloadAgent::saveToDisk(const QString &filename, const QString &data)
129 QFile file(filename);
130 if (!file.open(QIODevice::WriteOnly)) {
131 fprintf(stderr, "Could not open %s for writing: %s\n",
132 qPrintable(filename),
133 qPrintable(file.errorString()));
137 file.write(data.toLatin1() );
143 void DownloadAgent::execute()
148 void DownloadAgent::sslErrors(const QList<QSslError> &sslErrors)
150 #ifndef QT_NO_OPENSSL
151 foreach (const QSslError &error, sslErrors)
152 fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
156 void DownloadAgent::requestFinished(QNetworkReply *reply)
158 QUrl url = reply->url();
162 resultMessage = reply->errorString();
163 emit ( downloadFinished());
168 if (debug) qDebug()<<"\n* DownloadAgent::reqFinished: ";
169 QList <QNetworkCookie> cookies = reply->manager()->cookieJar()->cookiesForUrl(url);
170 foreach (QNetworkCookie c, cookies)
174 qDebug() << " url: " << url.toString();
175 qDebug() << " cookie name: " << c.name();
176 qDebug() << " cookie path: " << c.path();
177 qDebug() << " cookie value: " << c.value();
178 qDebug() << " cookie domain: " << c.domain();
179 qDebug() << " cookie exdate: " << c.expirationDate().toLocalTime().toString();
182 if (c.name() == "vymID" )
184 settings.setValue( "/downloads/cookies/vymID/value", c.value());
185 // settings.setValue( "/downloads/cookies/vymID/expires", c.expirationDate());
189 QString data = reply->readAll();
190 if (!tmpFile.open() )
191 QMessageBox::warning( 0, tr("Warning"), "Couldn't open tmpFile " + tmpFile.fileName());
194 if (!saveToDisk(tmpFile.fileName(), data))
195 QMessageBox::warning( 0, tr("Warning"), "Couldn't write to " + tmpFile.fileName());
197 resultMessage = QString ("saved to %1").arg(tmpFile.fileName());
199 emit ( downloadFinished());
202 currentDownloads.removeAll(reply);
203 reply->deleteLater();