2011-09-30 21:44:18 +02:00
|
|
|
/* ============================================================
|
|
|
|
* QupZilla - WebKit based browser
|
2012-01-01 15:29:55 +01:00
|
|
|
* Copyright (C) 2010-2012 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"
|
2011-10-17 09:57:07 +02:00
|
|
|
#include "globalfunctions.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>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
|
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);
|
|
|
|
QTemporaryFile tempFile("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) {
|
2012-08-23 15:40:10 +02:00
|
|
|
mimeType.append(QString(" (%1)").arg(qz_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
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
urls << QUrl::fromLocalFile(QDesktopServices::storageLocation(QDesktopServices::HomeLocation))
|
|
|
|
<< 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));
|
|
|
|
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 {
|
2011-09-30 19:22:50 +02:00
|
|
|
fileNameChoosed(QDir::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)
|
|
|
|
{
|
|
|
|
m_userFileName = name;
|
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) {
|
|
|
|
m_fileName = qz_ensureUniqueFilename(m_fileName);
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!m_path.contains(QDir::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)
|
|
|
|
{
|
|
|
|
QString path;
|
|
|
|
if (reply->hasRawHeader("Content-Disposition")) {
|
2011-12-13 16:36:45 +01:00
|
|
|
QString value = QString::fromLatin1(reply->rawHeader("Content-Disposition"));
|
2012-02-15 13:20:35 +01:00
|
|
|
|
|
|
|
// We try to use UTF-8 encoded filename first if present
|
2012-04-20 16:03:00 +02:00
|
|
|
if (value.contains(QRegExp("filename\\s*\\*\\s*=\\s*UTF-8", Qt::CaseInsensitive))) {
|
|
|
|
QRegExp reg("filename\\s*\\*\\s*=\\s*UTF-8''([^;]*)", Qt::CaseInsensitive);
|
2012-02-15 13:20:35 +01:00
|
|
|
reg.indexIn(value);
|
|
|
|
path = QUrl::fromPercentEncoding(reg.cap(1).toUtf8()).trimmed();
|
|
|
|
}
|
2012-04-20 16:03:00 +02:00
|
|
|
else if (value.contains(QRegExp("filename\\s*=", Qt::CaseInsensitive))) {
|
|
|
|
QRegExp reg("filename\\s*=([^;]*)", Qt::CaseInsensitive);
|
2012-02-15 13:20:35 +01:00
|
|
|
reg.indexIn(value);
|
|
|
|
path = reg.cap(1).trimmed();
|
2012-02-15 14:39:46 +01:00
|
|
|
|
2012-09-04 12:42:45 +02:00
|
|
|
if (path.startsWith(QLatin1Char('"')) && path.endsWith(QLatin1Char('"'))) {
|
2012-02-15 14:39:46 +01:00
|
|
|
path = path.mid(1, path.length() - 2);
|
|
|
|
}
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
2012-03-23 13:58:31 +01:00
|
|
|
return qz_filterCharsFromFilename(name);
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
DownloadFileHelper::~DownloadFileHelper()
|
|
|
|
{
|
|
|
|
delete m_iconProvider;
|
|
|
|
}
|