X-Git-Url: https://git.sven.stormbind.net/?a=blobdiff_plain;f=downloadagent.cpp;fp=downloadagent.cpp;h=0000000000000000000000000000000000000000;hb=d483bd8e6523c23c6f1d8908a2e0611c2bc9ff4f;hp=b5d5e9dbecc023a1a53d212ea32cbfd47f9f742b;hpb=7dfa3fe589d1722d49681f42cdb0bf1e6efb5223;p=sven%2Fvym.git diff --git a/downloadagent.cpp b/downloadagent.cpp deleted file mode 100644 index b5d5e9d..0000000 --- a/downloadagent.cpp +++ /dev/null @@ -1,205 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include - -#include - -QT_BEGIN_NAMESPACE -class QSslError; -QT_END_NAMESPACE - -QT_USE_NAMESPACE - -#include "downloadagent.h" -#include "mainwindow.h" -#include "settings.h" -#include "vymmodel.h" - -extern Main *mainWindow; -extern QString vymVersion; -extern QString vymPlatform; -extern QString tmpVymDir; -extern Settings settings; -extern bool debug; - -DownloadAgent::DownloadAgent(const QUrl &u) -{ - finishedScriptModelID = 0; - url = u; - connect(&agent, SIGNAL(finished(QNetworkReply*)), - SLOT(requestFinished(QNetworkReply*))); - - userAgent = QString("vym %1 ( %2)") - .arg(vymVersion) - .arg(vymPlatform).toUtf8(); -} - -QString DownloadAgent::getDestination() -{ - return tmpFile.fileName(); -} - -bool DownloadAgent::isSuccess() -{ - return success; -} - -QString DownloadAgent::getResultMessage() -{ - return resultMessage; -} - -void DownloadAgent::setFinishedAction (VymModel *m, const QString &script) -{ - finishedScriptModelID = m->getModelID(); - finishedScript = script; -} - -uint DownloadAgent::getFinishedScriptModelID() -{ - return finishedScriptModelID; -} - -QString DownloadAgent::getFinishedScript () -{ - return finishedScript; -} - -void DownloadAgent::setUserAgent(const QString &s) -{ - userAgent = s.toLocal8Bit(); -} - -void DownloadAgent::doDownload(const QUrl &url) -{ - QNetworkRequest request(url); - if (!userAgent.isEmpty()) request.setRawHeader("User-Agent", userAgent); - - // Only send cookies if talking to my own domain - bool useCookies = false; - if (url.host().contains("insilmaril.de") ) useCookies = true; - - if (useCookies) - { - if (debug) qDebug() << "DownloadAgent::doDownload Using cookies to download " << url.toString(); - QByteArray idCookieValue = settings.value("/downloads/cookies/vymID/value",QByteArray() ).toByteArray(); - //idCookieValue = QVariant("2000000002601").toByteArray(); //TESTING!!! - //qDebug()<<"idCookie="<insertCookie(idCookie); - - QNetworkCookie platformCookie; - platformCookie.setPath("/"); - // platformCookie.setDomain("localhost"); - platformCookie.setDomain("www.insilmaril.de"); - platformCookie.setName("vymPlatform"); - platformCookie.setValue( QVariant(vymPlatform).toByteArray() ); - platformCookie.setExpirationDate( QDateTime( QDate(2099,1,1) ) ); - agent.cookieJar()->insertCookie(platformCookie); - } - } - - QNetworkReply *reply = agent.get(request); - connect(reply, SIGNAL(sslErrors(QList)), SLOT(sslErrors(QList))); - - currentDownloads.append(reply); -} - -bool DownloadAgent::saveToDisk(const QString &filename, const QString &data) -{ - QFile file(filename); - if (!file.open(QIODevice::WriteOnly)) { - fprintf(stderr, "Could not open %s for writing: %s\n", - qPrintable(filename), - qPrintable(file.errorString())); - return false; - } - - file.write(data.toLatin1() ); - file.close(); - - return true; -} - -void DownloadAgent::execute() -{ - doDownload(url); -} - -void DownloadAgent::sslErrors(const QList &sslErrors) -{ -#ifndef QT_NO_OPENSSL - foreach (const QSslError &error, sslErrors) - fprintf(stderr, "SSL error: %s\n", qPrintable(error.errorString())); -#endif -} - -void DownloadAgent::requestFinished(QNetworkReply *reply) -{ - QUrl url = reply->url(); - if (reply->error()) - { - success = false; - resultMessage = reply->errorString(); - emit ( downloadFinished()); - } - else { - success = true; - - if (debug) qDebug()<<"\n* DownloadAgent::reqFinished: "; - QList cookies = reply->manager()->cookieJar()->cookiesForUrl(url); - foreach (QNetworkCookie c, cookies) - { - if (debug) - { - qDebug() << " url: " << url.toString(); - qDebug() << " cookie name: " << c.name(); - qDebug() << " cookie path: " << c.path(); - qDebug() << " cookie value: " << c.value(); - qDebug() << " cookie domain: " << c.domain(); - qDebug() << " cookie exdate: " << c.expirationDate().toLocalTime().toString(); - } - - if (c.name() == "vymID" ) - { - settings.setValue( "/downloads/cookies/vymID/value", c.value()); - // settings.setValue( "/downloads/cookies/vymID/expires", c.expirationDate()); - } - } - - QString data = reply->readAll(); - if (!tmpFile.open() ) - QMessageBox::warning( 0, tr("Warning"), "Couldn't open tmpFile " + tmpFile.fileName()); - else - { - if (!saveToDisk(tmpFile.fileName(), data)) - QMessageBox::warning( 0, tr("Warning"), "Couldn't write to " + tmpFile.fileName()); - else - resultMessage = QString ("saved to %1").arg(tmpFile.fileName()); - } - emit ( downloadFinished()); - } - - currentDownloads.removeAll(reply); - reply->deleteLater(); -} -