]> git.sven.stormbind.net Git - sven/vym.git/blob - downloadagent.cpp
Import Upstream version 2.6.11
[sven/vym.git] / downloadagent.cpp
1 #include <QCoreApplication>
2 #include <QDebug>
3 #include <QFile>
4 #include <QFileInfo>
5 #include <QList>
6 #include <QMessageBox>
7 #include <QNetworkAccessManager>
8 #include <QNetworkRequest>
9 #include <QNetworkReply>
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 "downloadagent.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 QString tmpVymDir;
32 extern Settings settings;
33 extern bool debug;
34
35 DownloadAgent::DownloadAgent(const QUrl &u)
36 {
37     finishedScriptModelID = 0;
38     url = u;
39     connect(&agent, SIGNAL(finished(QNetworkReply*)),
40             SLOT(requestFinished(QNetworkReply*)));
41
42     userAgent = QString("vym %1 ( %2)")
43         .arg(vymVersion)
44         .arg(vymPlatform).toUtf8();
45 }
46
47 QString  DownloadAgent::getDestination()
48 {
49     return tmpFile.fileName();
50 }
51
52 bool DownloadAgent::isSuccess()
53 {
54     return success;
55 }
56
57 QString DownloadAgent::getResultMessage()
58 {
59     return resultMessage;
60 }
61
62 void DownloadAgent::setFinishedAction (VymModel *m, const QString &script)
63 {
64     finishedScriptModelID = m->getModelID();
65     finishedScript = script;
66 }
67
68 uint DownloadAgent::getFinishedScriptModelID()
69 {
70     return finishedScriptModelID;
71 }
72
73 QString DownloadAgent::getFinishedScript ()
74 {
75     return finishedScript;
76 }
77
78 void DownloadAgent::setUserAgent(const QString &s)
79 {
80     userAgent = s.toLocal8Bit();
81 }
82
83 void DownloadAgent::doDownload(const QUrl &url) 
84 {
85     QNetworkRequest request(url);
86     if (!userAgent.isEmpty()) request.setRawHeader("User-Agent", userAgent);
87
88     // Only send cookies if talking to my own domain
89     bool useCookies = false;
90     if (url.host().contains("insilmaril.de") ) useCookies = true;
91
92     if (useCookies)
93     {
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 )
99         {
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);
109
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);
118         }
119     }
120
121     QNetworkReply *reply = agent.get(request);
122     connect(reply, SIGNAL(sslErrors(QList<QSslError>)), SLOT(sslErrors(QList<QSslError>)));
123
124     currentDownloads.append(reply);
125 }
126
127 bool DownloadAgent::saveToDisk(const QString &filename, const QString &data)
128 {
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()));
134         return false;
135     }
136
137     file.write(data.toLatin1() );
138     file.close();
139
140     return true;
141 }
142
143 void DownloadAgent::execute()
144 {
145     doDownload(url);
146 }
147
148 void DownloadAgent::sslErrors(const QList<QSslError> &sslErrors)
149 {
150 #ifndef QT_NO_OPENSSL
151     foreach (const QSslError &error, sslErrors)
152         fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString()));
153 #endif
154 }
155
156 void DownloadAgent::requestFinished(QNetworkReply *reply)
157 {
158     QUrl url = reply->url();
159     if (reply->error()) 
160     {
161         success = false;
162         resultMessage = reply->errorString();
163         emit ( downloadFinished());
164     }
165     else {
166         success = true;
167         
168         if (debug) qDebug()<<"\n* DownloadAgent::reqFinished: ";
169         QList <QNetworkCookie> cookies =  reply->manager()->cookieJar()->cookiesForUrl(url);
170         foreach (QNetworkCookie c, cookies)
171         {
172             if (debug)
173             {
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();
180             }
181
182             if (c.name() == "vymID" ) 
183             {
184                 settings.setValue( "/downloads/cookies/vymID/value", c.value());
185                 // settings.setValue( "/downloads/cookies/vymID/expires", c.expirationDate());
186             }
187         }
188
189         QString data = reply->readAll();
190         if (!tmpFile.open() )
191             QMessageBox::warning( 0, tr("Warning"), "Couldn't open tmpFile " + tmpFile.fileName());
192         else
193         {
194             if (!saveToDisk(tmpFile.fileName(), data))
195                 QMessageBox::warning( 0, tr("Warning"), "Couldn't write to " + tmpFile.fileName());
196             else
197                 resultMessage = QString ("saved to %1").arg(tmpFile.fileName());
198         }
199         emit ( downloadFinished());
200     }
201
202     currentDownloads.removeAll(reply);
203     reply->deleteLater();
204 }
205