diff --git a/src/Gui/DownloadItem.cpp b/src/Gui/DownloadItem.cpp index bfb79a515..b1d15ec7d 100644 --- a/src/Gui/DownloadItem.cpp +++ b/src/Gui/DownloadItem.cpp @@ -464,16 +464,16 @@ void DownloadItem::metaDataChanged() header.chop(1); m_fileName = QUrl::fromPercentEncoding(header); } - else { - index = header.indexOf("filename*=UTF-8''"); - if (index >= 0) { - header = header.mid(index+17); - if (header.startsWith("\"") || header.startsWith("'")) - header = header.mid(1); - if (header.endsWith("\"") || header.endsWith("'")) - header.chop(1); - m_fileName = QUrl::fromPercentEncoding(header); - } + // Sometimes "filename=" and "filename*=UTF-8''" is set. + // So, search for this too. + index = header.indexOf("filename*=UTF-8''"); + if (index >= 0) { + header = header.mid(index+17); + if (header.startsWith("\"") || header.startsWith("'")) + header = header.mid(1); + if (header.endsWith("\"") || header.endsWith("'")) + header.chop(1); + m_fileName = QUrl::fromPercentEncoding(header); } } diff --git a/src/Gui/DownloadManager.cpp b/src/Gui/DownloadManager.cpp index 4158f8593..cbc0bf38c 100644 --- a/src/Gui/DownloadManager.cpp +++ b/src/Gui/DownloadManager.cpp @@ -71,6 +71,8 @@ DownloadManager::DownloadManager(QWidget *parent) m_model = new DownloadModel(this); ui->downloadsView->setModel(m_model); connect(ui->cleanupButton, SIGNAL(clicked()), this, SLOT(cleanup())); + connect(m_manager, SIGNAL(finished(QNetworkReply*)), + this, SLOT(replyFinished(QNetworkReply*))); load(); Gui::DockWindowManager* pDockMgr = Gui::DockWindowManager::instance(); @@ -108,11 +110,57 @@ int DownloadManager::activeDownloads() const return count; } +QUrl DownloadManager::redirectUrl(const QUrl& url) const +{ + QUrl redirectUrl = url; + if (url.host() == QLatin1String("www.dropbox.com")) { + QList< QPair > query = url.queryItems(); + for (QList< QPair >::iterator it = query.begin(); it != query.end(); ++it) { + if (it->first == QLatin1String("dl") && it->second == QLatin1String("0\r\n")) { + redirectUrl.removeQueryItem(QLatin1String("dl")); + redirectUrl.addQueryItem(QLatin1String("dl"), QLatin1String("1\r\n")); + break; + } + } + } + + return redirectUrl; +} + +void DownloadManager::replyFinished(QNetworkReply* reply) +{ + // The 'requestFileName' is used to store the argument passed by 'download()' + // and to also distinguish between replies created by 'download()' and + // this method. + // Replies from this method shouldn't be further examined because it's not + // assumed to get re-directed urls over several steps. + QVariant var = reply->property("requestFileName"); + if (var.isValid()) { + bool requestFileName = reply->property("requestFileName").toBool(); + + QUrl url = reply->url(); + + // If this is a redirected url use this instead + QUrl redirectUrl = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); + if (!redirectUrl.isEmpty()) { + url = redirectUrl; + } + + // start the actual download now + handleUnsupportedContent(m_manager->get(QNetworkRequest(url)), requestFileName); + } + + reply->deleteLater(); +} + void DownloadManager::download(const QNetworkRequest &request, bool requestFileName) { if (request.url().isEmpty()) return; - handleUnsupportedContent(m_manager->get(request), requestFileName); + + // postpone this reply until we can examine it in replyFinished + QNetworkReply* reply = m_manager->get(request); + reply->setProperty("requestFileName", QVariant(requestFileName)); } void DownloadManager::handleUnsupportedContent(QNetworkReply *reply, bool requestFileName) diff --git a/src/Gui/DownloadManager.h b/src/Gui/DownloadManager.h index 818e95f52..6ef1cebdb 100644 --- a/src/Gui/DownloadManager.h +++ b/src/Gui/DownloadManager.h @@ -65,6 +65,7 @@ public: RemovePolicy removePolicy() const; void setRemovePolicy(RemovePolicy policy); void closeEvent(QCloseEvent* e); + QUrl redirectUrl(const QUrl&) const; public Q_SLOTS: void download(const QNetworkRequest &request, bool requestFileName = false); @@ -76,6 +77,7 @@ public Q_SLOTS: private Q_SLOTS: void save() const; void updateRow(); + void replyFinished(QNetworkReply* reply); private: void addItem(DownloadItem *item); diff --git a/src/Gui/MainWindow.cpp b/src/Gui/MainWindow.cpp index 97358d18d..fec5bc4ac 100644 --- a/src/Gui/MainWindow.cpp +++ b/src/Gui/MainWindow.cpp @@ -1585,7 +1585,8 @@ void MainWindow::loadUrls(App::Document* doc, const QList& url) } } else if (it->scheme().toLower() == QLatin1String("http")) { - Gui::Dialog::DownloadManager::getInstance()->download(*it); + Gui::Dialog::DownloadManager* dm = Gui::Dialog::DownloadManager::getInstance(); + dm->download(dm->redirectUrl(*it)); } //#ifndef QT_NO_OPENSSL else if (it->scheme().toLower() == QLatin1String("https")) { @@ -1594,7 +1595,8 @@ void MainWindow::loadUrls(App::Document* doc, const QList& url) url.removeEncodedQueryItem(QByteArray("sid")); url.setScheme(QLatin1String("http")); } - Gui::Dialog::DownloadManager::getInstance()->download(url); + Gui::Dialog::DownloadManager* dm = Gui::Dialog::DownloadManager::getInstance(); + dm->download(dm->redirectUrl(url)); } //#endif else if (it->scheme().toLower() == QLatin1String("ftp")) {