Subversion Repositories Scribus

Rev

Rev 19821 | Rev 19825 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
17530 craig 1
 
2
#include "scdlmgr.h"
3
#include "scdlthread.h"
4
#include "scpaths.h"
5
 
6
#include <QDebug>
7
#include <QDir>
8
#include <QFileInfo>
9
#include <QNetworkReply>
10
#include <QNetworkRequest>
11
#include <QUrl>
12
 
13
 
14
ScDLThread::ScDLThread(QObject *parent) : QThread(parent),
15
        downloadedCount(0), totalCount(0)
16
{
17
        connect(this, SIGNAL(runSignal()), this, SLOT(runSlot()));
18
}
19
 
20
ScDLThread::~ScDLThread()
21
{
22
}
23
 
24
void ScDLThread::run()
25
{
26
        emit(runSignal());
27
}
28
 
19785 craig 29
void ScDLThread::addURL(const QUrl &url, bool overwrite, const QString& location, const QString& destinationLocation)
17530 craig 30
{
19789 craig 31
        //qDebug()<<"ScDLThread::addURL:"<<url;
17530 craig 32
        if (!urlOK(url))
33
                return;
34
        QString l(QDir::cleanPath(location));
35
        if (!l.endsWith("/"))
36
                l += "/";
37
        downloadQueue.enqueue(qMakePair(url, l));
38
        ++totalCount;
39
}
40
 
19785 craig 41
void ScDLThread::addURLs(const QStringList &urlList, bool overwrite, const QString& location, const QString& destinationLocation)
17530 craig 42
{
19789 craig 43
        //qDebug()<<"ScDLThread::addURLs:"<<urlList;
17530 craig 44
        m_urlList=urlList;
45
        QString l(QDir::cleanPath(location));
46
        if (!l.endsWith("/"))
47
                l += "/";
48
        foreach (QString u, m_urlList)
49
        {
50
                QUrl url(u);
51
                if (!urlOK(u))
52
                        return;
53
                downloadQueue.enqueue(qMakePair(url, l));
54
                ++totalCount;
55
        }
56
}
57
 
58
void ScDLThread::startDownloads()
59
{
60
        if (downloadQueue.isEmpty())
61
        {
19789 craig 62
                //qDebug()<<"No more downloads left";
17530 craig 63
                emit finished();
64
                return;
65
        }
66
        start();
67
}
68
 
69
QString ScDLThread::saveFileName(const QUrl &url, const QString& location, bool overwrite)
70
{
71
        QDir d(location);
72
        QString path = url.path();
73
        QString basename = QFileInfo(path).fileName();
74
        if (location.isEmpty() || !d.exists())
75
        {
76
                if (basename.isEmpty())
77
                        basename = "download";
78
                basename = ScPaths::downloadDir() + basename;
79
        }
80
        else
81
        {
82
                basename = location + basename;
83
        }
84
        if (!overwrite && QFile::exists(basename))
85
        {
86
                // already exists, don't overwrite
87
                int i = 0;
88
                basename += '.';
89
                while (QFile::exists(basename + QString::number(i)))
90
                        ++i;
91
                basename += QString::number(i);
92
        }
93
        return basename;
94
}
95
 
96
void ScDLThread::startNextDownload()
97
{
98
        if (downloadQueue.isEmpty())
99
        {
19821 craig 100
                qDebug()<<downloadedCount<<"/"<<totalCount<<"files downloaded successfully";
17551 craig 101
                downloadedCount=totalCount=0;
17530 craig 102
                emit finished();
103
                return;
104
        }
105
        QPair<QUrl, QString> urlPair=downloadQueue.dequeue();
106
 
107
        QString filename = saveFileName(urlPair.first, urlPair.second, true);
108
        if (filename.isEmpty())
109
        {
19821 craig 110
                qDebug()<<"File name empty for url:"<<urlPair.first.toEncoded().constData();
17530 craig 111
                return;
112
        }
113
        output.setFileName(filename);
114
        if (!output.open(QIODevice::WriteOnly))
115
        {
19789 craig 116
                //qDebug()<<"Problem opening save file '"<<qPrintable(filename)<<"' for download '"
117
                //         <<urlPair.first.toEncoded().constData()<<"': "<<qPrintable(output.errorString());
17530 craig 118
 
119
                startNextDownload();
120
                return;
121
        }
19789 craig 122
        emit fileStarted(output.fileName());
17530 craig 123
        QNetworkRequest request(urlPair.first);
19821 craig 124
        //QNetworkReply *nwr=manager.head(request);
125
        //qDebug()<<nwr->url()<<nwr->rawHeaderList();
126
        //connect(&manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(replyFinished(QNetworkReply*)));
17530 craig 127
        currentDownload = manager.get(request);
19821 craig 128
        connect(currentDownload, SIGNAL(finished()), this, SLOT(downloadFinished()));
129
        connect(currentDownload, SIGNAL(readyRead()), this, SLOT(downloadReadyRead()));
17530 craig 130
 
19822 craig 131
        //qDebug()<<"Downloading:"<<urlPair.first.toEncoded().constData();
17530 craig 132
}
133
 
134
void ScDLThread::downloadFinished()
135
{
136
        output.close();
137
        if (currentDownload->error())
19787 craig 138
        {
19821 craig 139
/*
140
                if(currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 301 || currentDownload->rawHeaderList().contains("Location"))
141
                        qDebug()<<currentDownload->header(QNetworkRequest::LocationHeader).toString();
142
                if(currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 302 || currentDownload->rawHeaderList().contains("Location"))
143
                        qDebug()<<currentDownload->header(QNetworkRequest::LocationHeader).toString();
144
                if(currentDownload->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt() == 307 || currentDownload->rawHeaderList().contains("Location"))
145
                        qDebug()<<currentDownload->header(QNetworkRequest::LocationHeader).toString();
146
 
147
                QUrl redirectUrl = currentDownload->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl();
148
                qDebug()<<redirectUrl;
149
*/
150
                qDebug()<<"Failed: "<<qPrintable(currentDownload->errorString());
19789 craig 151
                emit fileFailed(output.fileName());
152
                if (output.exists())
153
                        output.remove();
19787 craig 154
        }
17530 craig 155
        else
156
        {
19822 craig 157
                //qDebug()<<"Saving file:"<<qPrintable(output.fileName());
17530 craig 158
                ++downloadedCount;
19789 craig 159
                emit fileReceived(output.fileName());
17530 craig 160
        }
161
        currentDownload->deleteLater();
162
        startNextDownload();
163
}
164
 
19821 craig 165
void ScDLThread::replyFinished(QNetworkReply* reply)
166
{
167
        qDebug()<<"replyFinished";
168
        QVariant possibleRedirectUrl =reply->attribute(QNetworkRequest::RedirectionTargetAttribute);
169
        QUrl urlRedirectedTo = possibleRedirectUrl.toUrl();
170
        QString text;
171
        /* If the URL is not empty, we're being redirected. */
172
        if(!urlRedirectedTo.isEmpty())
173
        {
174
                qDebug()<<reply->header(QNetworkRequest::LocationHeader);
175
                text = QString("QNAMRedirect::replyFinished: Redirected to ").append(urlRedirectedTo.toString());
176
                addURL(text, true, ScPaths::downloadDir(), ScPaths::downloadDir());
177
                /* We'll do another request to the redirection url. */
178
        //      this->_qnam->get(QNetworkRequest(_urlRedirectedTo));
179
        }
180
        else
181
        {
182
                /*
183
                 * We weren't redirected anymore
184
                 * so we arrived to the final destination...
185
                 */
186
                text = QString("QNAMRedirect::replyFinished: Arrived to ").append(reply->url().toString());
187
                urlRedirectedTo.clear();
188
        }
189
        qDebug()<<text;
190
        /* Clean up. */
191
        reply->deleteLater();
192
}
17530 craig 193
 
19821 craig 194
 
17530 craig 195
void ScDLThread::downloadReadyRead()
196
{
197
        output.write(currentDownload->readAll());
198
}
199
 
200
void ScDLThread::runSlot()
201
{
202
        startNextDownload();
203
}
204
 
205
bool ScDLThread::urlOK(QUrl url)
206
{
207
        //TODO: Add some more URL checks
208
        if (!url.isValid() || url.isEmpty() || url.host().isEmpty())
209
        {
19789 craig 210
                //qDebug()<<"URL invalid:"<<url;
17530 craig 211
                return false;
212
        }
213
        return true;
214
}
215
 
216
 
217