2011-09-30 21:44:18 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2013-01-29 13:16:24 +01:00
|
|
|
* Copyright (C) 2010-2013 David Rosca <nowrep@gmail.com>
|
2011-09-30 21:44:18 +02:00
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
* ============================================================ */
|
2011-09-30 19:22:50 +02:00
|
|
|
#include "downloadfilehelper.h"
|
|
|
|
#include "webpage.h"
|
2012-01-21 20:27:45 +01:00
|
|
|
#include "tabbedwebview.h"
|
2011-09-30 19:22:50 +02:00
|
|
|
#include "downloadoptionsdialog.h"
|
|
|
|
#include "mainapplication.h"
|
|
|
|
#include "qupzilla.h"
|
|
|
|
#include "downloaditem.h"
|
|
|
|
#include "downloadmanager.h"
|
2013-01-22 19:04:22 +01:00
|
|
|
#include "qztools.h"
|
2012-01-11 21:58:25 +01:00
|
|
|
#include "settings.h"
|
2011-09-30 19:22:50 +02:00
|
|
|
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QFileIconProvider>
|
|
|
|
#include <QListWidgetItem>
|
|
|
|
#include <QTemporaryFile>
|
|
|
|
#include <QWebHistory>
|
|
|
|
#include <QDebug>
|
|
|
|
#include <QFileDialog>
|
2012-12-20 14:45:35 +01:00
|
|
|
|
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
#include <QStandardPaths>
|
|
|
|
#else
|
2012-02-29 18:33:50 +01:00
|
|
|
#include <QDesktopServices>
|
2012-12-20 14:45:35 +01:00
|
|
|
#endif
|
2012-02-29 18:33:50 +01:00
|
|
|
|
2012-08-14 18:53:55 +02:00
|
|
|
DownloadFileHelper::DownloadFileHelper(const QString &lastDownloadPath, const QString &downloadPath, bool useNativeDialog)
|
2011-09-30 19:22:50 +02:00
|
|
|
: QObject()
|
2011-12-06 19:18:06 +01:00
|
|
|
, m_lastDownloadOption(DownloadManager::SaveFile)
|
2011-09-30 19:22:50 +02:00
|
|
|
, m_lastDownloadPath(lastDownloadPath)
|
|
|
|
, m_downloadPath(downloadPath)
|
|
|
|
, m_useNativeDialog(useNativeDialog)
|
2011-11-06 17:01:23 +01:00
|
|
|
, m_timer(0)
|
2011-09-30 19:22:50 +02:00
|
|
|
, m_reply(0)
|
|
|
|
, m_openFileChoosed(false)
|
2011-11-06 17:01:23 +01:00
|
|
|
, m_listWidget(0)
|
2011-09-30 19:22:50 +02:00
|
|
|
, m_iconProvider(new QFileIconProvider)
|
2011-10-07 15:37:49 +02:00
|
|
|
, m_manager(0)
|
2011-09-30 19:22:50 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//// Getting where to download requested file
|
|
|
|
//// in 3 functions, as we are using non blocking
|
|
|
|
//// dialogs ( this is important to make secure downloading
|
2011-10-08 12:10:25 +02:00
|
|
|
//// on Windows working properly )
|
2011-09-30 19:22:50 +02:00
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
2012-08-14 18:53:55 +02:00
|
|
|
void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, const DownloadManager::DownloadInfo &info)
|
2011-09-30 19:22:50 +02:00
|
|
|
{
|
|
|
|
m_timer = new QTime();
|
|
|
|
m_timer->start();
|
2012-08-14 18:53:55 +02:00
|
|
|
m_h_fileName = info.suggestedFileName.isEmpty() ? getFileName(reply) : info.suggestedFileName;
|
2011-09-30 19:22:50 +02:00
|
|
|
m_reply = reply;
|
|
|
|
|
2012-08-14 18:53:55 +02:00
|
|
|
QFileInfo fileInfo(m_h_fileName);
|
2013-01-29 13:16:24 +01:00
|
|
|
QTemporaryFile tempFile(mApp->tempPath() + "/XXXXXX." + fileInfo.suffix());
|
2011-09-30 19:22:50 +02:00
|
|
|
tempFile.open();
|
2012-02-11 11:38:02 +01:00
|
|
|
tempFile.write(m_reply->peek(1024 * 1024));
|
2011-09-30 19:22:50 +02:00
|
|
|
QFileInfo tempInfo(tempFile.fileName());
|
2011-11-06 17:01:23 +01:00
|
|
|
m_fileIcon = m_iconProvider->icon(tempInfo).pixmap(30, 30);
|
2011-09-30 19:22:50 +02:00
|
|
|
QString mimeType = m_iconProvider->type(tempInfo);
|
|
|
|
|
2012-03-08 12:48:23 +01:00
|
|
|
m_fileSize = m_reply->header(QNetworkRequest::ContentLengthHeader).toLongLong();
|
|
|
|
if (m_fileSize > 0) {
|
2013-01-22 19:04:22 +01:00
|
|
|
mimeType.append(QString(" (%1)").arg(QzTools::fileSizeToString(m_fileSize)));
|
2012-02-11 14:37:19 +01:00
|
|
|
}
|
|
|
|
|
2011-11-08 15:20:53 +01:00
|
|
|
// Close Empty Tab
|
2012-08-14 18:53:55 +02:00
|
|
|
if (info.page) {
|
|
|
|
WebView* view = qobject_cast<WebView*>(info.page->view());
|
|
|
|
if (!info.page->url().isEmpty()) {
|
|
|
|
m_downloadPage = info.page->url();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-08-14 18:53:55 +02:00
|
|
|
else if (info.page->history()->canGoBack()) {
|
|
|
|
m_downloadPage = info.page->history()->backItem().url();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-08-14 18:53:55 +02:00
|
|
|
else if (view && info.page->history()->count() == 0) {
|
2012-01-21 20:27:45 +01:00
|
|
|
view->closeView();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
2012-08-14 18:53:55 +02:00
|
|
|
if (info.askWhatToDo && m_downloadPath.isEmpty()) {
|
2011-09-30 19:22:50 +02:00
|
|
|
DownloadOptionsDialog* dialog = new DownloadOptionsDialog(m_h_fileName, m_fileIcon, mimeType, reply->url(), mApp->activeWindow());
|
2012-08-14 11:52:06 +02:00
|
|
|
dialog->showExternalManagerOption(m_manager->useExternalManager());
|
2011-12-06 19:18:06 +01:00
|
|
|
dialog->setLastDownloadOption(m_lastDownloadOption);
|
2011-09-30 19:22:50 +02:00
|
|
|
dialog->show();
|
2012-08-14 11:52:06 +02:00
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
connect(dialog, SIGNAL(dialogFinished(int)), this, SLOT(optionsDialogAccepted(int)));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-08-14 18:53:55 +02:00
|
|
|
else if (info.forceChoosingPath) {
|
|
|
|
optionsDialogAccepted(4);
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
else {
|
2011-09-30 19:22:50 +02:00
|
|
|
optionsDialogAccepted(2);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
2013-02-04 22:48:34 +01:00
|
|
|
QString DownloadFileHelper::parseContentDisposition(const QByteArray &header)
|
|
|
|
{
|
|
|
|
QString path;
|
|
|
|
|
|
|
|
if (header.isEmpty()) {
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
|
|
|
QString value;
|
|
|
|
|
2013-02-13 17:19:29 +01:00
|
|
|
if (QzTools::isUtf8(header.constData())) {
|
2013-02-04 22:48:34 +01:00
|
|
|
value = QString::fromUtf8(header);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
value = QString::fromLatin1(header);
|
|
|
|
}
|
|
|
|
|
|
|
|
// We try to use UTF-8 encoded filename first if present
|
|
|
|
if (value.contains(QRegExp("[ ;]{1,}filename*\\*\\s*=\\s*UTF-8''", Qt::CaseInsensitive))) {
|
|
|
|
QRegExp reg("filename\\s*\\*\\s*=\\s*UTF-8''([^;]*)", Qt::CaseInsensitive);
|
|
|
|
reg.indexIn(value);
|
|
|
|
path = QUrl::fromPercentEncoding(reg.cap(1).toUtf8()).trimmed();
|
|
|
|
}
|
|
|
|
else if (value.contains(QRegExp("[ ;]{1,}filename\\s*=", Qt::CaseInsensitive))) {
|
|
|
|
QRegExp reg("[ ;]{1,}filename\\s*=(.*)", Qt::CaseInsensitive);
|
|
|
|
reg.indexIn(value);
|
|
|
|
path = reg.cap(1).trimmed();
|
|
|
|
|
|
|
|
// Parse filename in quotes (to support semicolon inside filename)
|
|
|
|
if (path.startsWith(QLatin1Char('"')) && path.count(QLatin1Char('"')) > 1) {
|
|
|
|
int pos = path.indexOf(QLatin1Char('"'), 1);
|
|
|
|
while (pos != -1) {
|
|
|
|
if (path[pos - 1] != QLatin1Char('\\')) {
|
|
|
|
// We also need to strip starting quote
|
|
|
|
path = path.left(pos).mid(1);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
pos = path.indexOf(QLatin1Char('"'), pos + 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
QRegExp reg("([^;]*)", Qt::CaseInsensitive);
|
|
|
|
reg.indexIn(path);
|
|
|
|
path = reg.cap(1).trimmed();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (path.startsWith(QLatin1Char('"')) && path.endsWith(QLatin1Char('"'))) {
|
|
|
|
path = path.mid(1, path.length() - 2);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
void DownloadFileHelper::optionsDialogAccepted(int finish)
|
|
|
|
{
|
2012-08-14 18:53:55 +02:00
|
|
|
bool forceChoosingPath = false;
|
2011-09-30 19:22:50 +02:00
|
|
|
m_openFileChoosed = false;
|
2012-08-14 18:53:55 +02:00
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
switch (finish) {
|
2012-08-14 11:52:06 +02:00
|
|
|
case 0: // Cancelled
|
2012-09-11 11:43:11 +02:00
|
|
|
delete m_timer;
|
2012-01-25 17:46:37 +01:00
|
|
|
|
|
|
|
m_reply->abort();
|
|
|
|
m_reply->deleteLater();
|
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
return;
|
2012-08-14 11:52:06 +02:00
|
|
|
|
|
|
|
case 1: // Open
|
2011-09-30 19:22:50 +02:00
|
|
|
m_openFileChoosed = true;
|
2011-12-06 19:18:06 +01:00
|
|
|
m_lastDownloadOption = DownloadManager::OpenFile;
|
2011-09-30 19:22:50 +02:00
|
|
|
break;
|
2012-08-14 11:52:06 +02:00
|
|
|
|
|
|
|
case 2: // Save
|
2011-12-06 19:18:06 +01:00
|
|
|
m_lastDownloadOption = DownloadManager::SaveFile;
|
2011-09-30 19:22:50 +02:00
|
|
|
break;
|
2012-02-04 18:45:59 +01:00
|
|
|
|
2012-08-14 11:52:06 +02:00
|
|
|
case 3: // External manager
|
|
|
|
m_manager->startExternalManager(m_reply->url());
|
|
|
|
m_reply->abort();
|
|
|
|
m_reply->deleteLater();
|
|
|
|
return;
|
|
|
|
|
2012-08-14 18:53:55 +02:00
|
|
|
case 4: // Force opening save file dialog
|
|
|
|
m_lastDownloadOption = DownloadManager::SaveFile;
|
|
|
|
forceChoosingPath = true;
|
|
|
|
break;
|
|
|
|
|
2012-02-04 18:45:59 +01:00
|
|
|
default:
|
|
|
|
qWarning() << "DownloadFileHelper::optionsDialogAccepted invalid return value!";
|
2012-09-11 11:43:11 +02:00
|
|
|
delete m_timer;
|
2012-02-04 18:45:59 +01:00
|
|
|
|
|
|
|
m_reply->abort();
|
|
|
|
m_reply->deleteLater();
|
|
|
|
return;
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
2011-12-06 19:18:06 +01:00
|
|
|
m_manager->setLastDownloadOption(m_lastDownloadOption);
|
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
if (!m_openFileChoosed) {
|
2012-08-14 18:53:55 +02:00
|
|
|
if (m_downloadPath.isEmpty() || forceChoosingPath) {
|
2011-09-30 19:22:50 +02:00
|
|
|
if (m_useNativeDialog) {
|
|
|
|
fileNameChoosed(QFileDialog::getSaveFileName(mApp->getWindow(), tr("Save file as..."), m_lastDownloadPath + m_h_fileName));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
else {
|
2011-09-30 19:22:50 +02:00
|
|
|
QFileDialog* dialog = new QFileDialog(mApp->getWindow());
|
|
|
|
dialog->setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
dialog->setWindowTitle(tr("Save file as..."));
|
2012-01-29 11:23:16 +01:00
|
|
|
dialog->setAcceptMode(QFileDialog::AcceptSave);
|
2011-09-30 19:22:50 +02:00
|
|
|
dialog->setDirectory(m_lastDownloadPath);
|
|
|
|
dialog->selectFile(m_h_fileName);
|
2012-01-29 11:23:16 +01:00
|
|
|
|
|
|
|
QList<QUrl> urls;
|
2012-12-20 14:45:35 +01:00
|
|
|
urls <<
|
|
|
|
#if QT_VERSION >= 0x050000
|
|
|
|
QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::HomeLocation))
|
|
|
|
<< QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DesktopLocation))
|
|
|
|
<< QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation))
|
|
|
|
<< QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::PicturesLocation))
|
|
|
|
<< QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MusicLocation))
|
|
|
|
<< QUrl::fromLocalFile(QStandardPaths::writableLocation(QStandardPaths::MoviesLocation));
|
|
|
|
#else
|
|
|
|
QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::HomeLocation))
|
2012-01-29 11:23:16 +01:00
|
|
|
<< QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DesktopLocation))
|
|
|
|
<< QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation))
|
|
|
|
<< QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::PicturesLocation))
|
|
|
|
<< QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MusicLocation))
|
|
|
|
<< QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::MoviesLocation));
|
2012-12-20 14:45:35 +01:00
|
|
|
#endif
|
2012-01-29 11:23:16 +01:00
|
|
|
dialog->setSidebarUrls(urls);
|
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
dialog->show();
|
|
|
|
connect(dialog, SIGNAL(fileSelected(QString)), this, SLOT(fileNameChoosed(QString)));
|
|
|
|
}
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
else {
|
2011-09-30 19:22:50 +02:00
|
|
|
fileNameChoosed(m_downloadPath + m_h_fileName, true);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
2012-11-27 19:14:03 +01:00
|
|
|
fileNameChoosed(mApp->tempPath() + "/" + m_h_fileName, true);
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DownloadFileHelper::fileNameChoosed(const QString &name, bool fileNameAutoGenerated)
|
|
|
|
{
|
2013-01-22 22:56:39 +01:00
|
|
|
m_userFileName = name.trimmed();
|
2011-10-17 09:57:07 +02:00
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
if (m_userFileName.isEmpty()) {
|
|
|
|
m_reply->abort();
|
2012-02-01 17:01:08 +01:00
|
|
|
m_reply->deleteLater();
|
|
|
|
|
2012-09-11 11:43:11 +02:00
|
|
|
delete m_timer;
|
2011-09-30 19:22:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-08 12:10:25 +02:00
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
int pos = m_userFileName.lastIndexOf(QLatin1Char('/'));
|
2011-09-30 19:22:50 +02:00
|
|
|
if (pos != -1) {
|
|
|
|
int size = m_userFileName.size();
|
2011-11-06 17:01:23 +01:00
|
|
|
m_path = m_userFileName.left(pos + 1);
|
|
|
|
m_fileName = m_userFileName.right(size - pos - 1);
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
2012-03-23 13:58:31 +01:00
|
|
|
if (fileNameAutoGenerated) {
|
2013-01-22 19:04:22 +01:00
|
|
|
m_fileName = QzTools::ensureUniqueFilename(m_fileName);
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
2012-11-27 19:14:03 +01:00
|
|
|
if (!m_path.contains(mApp->tempPath())) {
|
2011-09-30 19:22:50 +02:00
|
|
|
m_lastDownloadPath = m_path;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
|
2012-01-11 21:58:25 +01:00
|
|
|
Settings settings;
|
2011-09-30 19:22:50 +02:00
|
|
|
settings.beginGroup("DownloadManager");
|
|
|
|
settings.setValue("lastDownloadPath", m_lastDownloadPath);
|
|
|
|
settings.endGroup();
|
2011-10-18 14:30:17 +02:00
|
|
|
m_manager->setLastDownloadPath(m_lastDownloadPath);
|
2011-09-30 19:22:50 +02:00
|
|
|
|
|
|
|
QListWidgetItem* item = new QListWidgetItem(m_listWidget);
|
2011-10-07 15:37:49 +02:00
|
|
|
DownloadItem* downItem = new DownloadItem(item, m_reply, m_path, m_fileName, m_fileIcon, m_timer, m_openFileChoosed, m_downloadPage, m_manager);
|
2012-03-08 12:48:23 +01:00
|
|
|
downItem->setTotalSize(m_fileSize);
|
2011-09-30 19:22:50 +02:00
|
|
|
|
|
|
|
emit itemCreated(item, downItem);
|
|
|
|
}
|
|
|
|
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
//// End here
|
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
QString DownloadFileHelper::getFileName(QNetworkReply* reply)
|
|
|
|
{
|
2013-02-04 22:48:34 +01:00
|
|
|
QString path = parseContentDisposition(reply->rawHeader("Content-Disposition"));
|
2012-04-21 22:19:35 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (path.isEmpty()) {
|
2011-09-30 19:22:50 +02:00
|
|
|
path = reply->url().path();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
|
|
|
|
QFileInfo info(path);
|
|
|
|
QString baseName = info.completeBaseName();
|
|
|
|
QString endName = info.suffix();
|
|
|
|
|
|
|
|
if (baseName.isEmpty()) {
|
|
|
|
baseName = tr("NoNameDownload");
|
|
|
|
}
|
|
|
|
|
2012-03-22 19:41:12 +01:00
|
|
|
if (!endName.isEmpty()) {
|
2012-09-04 12:42:45 +02:00
|
|
|
endName.prepend(QLatin1Char('.'));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
QString name = baseName + endName;
|
2012-03-18 11:06:09 +01:00
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
if (name.contains(QLatin1Char('"'))) {
|
|
|
|
name.remove(QLatin1String("\";"));
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
|
2013-01-22 19:04:22 +01:00
|
|
|
return QzTools::filterCharsFromFilename(name);
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DownloadFileHelper::~DownloadFileHelper()
|
|
|
|
{
|
|
|
|
delete m_iconProvider;
|
|
|
|
}
|