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>
|
|
|
|
|
2011-11-08 15:20:53 +01:00
|
|
|
DownloadFileHelper::DownloadFileHelper(const QString &lastDownloadPath, const QString &downloadPath, bool useNativeDialog, WebPage* page)
|
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-11-08 15:20:53 +01:00
|
|
|
, m_webPage(page)
|
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
|
|
|
//////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, bool askWhatToDo)
|
|
|
|
{
|
|
|
|
m_timer = new QTime();
|
|
|
|
m_timer->start();
|
|
|
|
m_h_fileName = getFileName(reply);
|
|
|
|
m_reply = reply;
|
|
|
|
|
2012-02-11 11:38:02 +01:00
|
|
|
QFileInfo info(m_h_fileName);
|
2011-10-08 12:10:25 +02:00
|
|
|
QTemporaryFile tempFile("XXXXXX." + info.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-02-11 14:37:19 +01:00
|
|
|
qint64 size = m_reply->header(QNetworkRequest::ContentLengthHeader).toLongLong();
|
|
|
|
if (size > 0) {
|
|
|
|
mimeType.append(QString(" (%1)").arg(DownloadItem::fileSizeToString(size)));
|
|
|
|
}
|
|
|
|
|
2011-11-08 15:20:53 +01:00
|
|
|
// Close Empty Tab
|
|
|
|
if (m_webPage) {
|
2012-01-21 20:27:45 +01:00
|
|
|
WebView* view = qobject_cast<WebView*>(m_webPage->view());
|
|
|
|
if (!m_webPage->url().isEmpty() && m_webPage->url().toString() != "about:blank") {
|
|
|
|
m_downloadPage = m_webPage->url();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-11-08 15:20:53 +01:00
|
|
|
else if (m_webPage->history()->canGoBack()) {
|
|
|
|
m_downloadPage = m_webPage->history()->backItem().url();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-01-21 20:27:45 +01:00
|
|
|
else if (view && m_webPage->history()->count() == 0) {
|
|
|
|
view->closeView();
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (askWhatToDo) {
|
|
|
|
DownloadOptionsDialog* dialog = new DownloadOptionsDialog(m_h_fileName, m_fileIcon, mimeType, reply->url(), mApp->activeWindow());
|
2011-12-06 19:18:06 +01:00
|
|
|
dialog->setLastDownloadOption(m_lastDownloadOption);
|
2011-09-30 19:22:50 +02:00
|
|
|
dialog->show();
|
|
|
|
connect(dialog, SIGNAL(dialogFinished(int)), this, SLOT(optionsDialogAccepted(int)));
|
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)
|
|
|
|
{
|
|
|
|
m_openFileChoosed = false;
|
|
|
|
switch (finish) {
|
|
|
|
case 0: //Cancelled
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_timer) {
|
2011-09-30 19:22:50 +02:00
|
|
|
delete m_timer;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2012-01-25 17:46:37 +01:00
|
|
|
|
|
|
|
m_reply->abort();
|
|
|
|
m_reply->deleteLater();
|
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
return;
|
|
|
|
break;
|
|
|
|
case 1: //Open
|
|
|
|
m_openFileChoosed = true;
|
2011-12-06 19:18:06 +01:00
|
|
|
m_lastDownloadOption = DownloadManager::OpenFile;
|
2011-09-30 19:22:50 +02:00
|
|
|
break;
|
|
|
|
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
|
|
|
|
|
|
|
default:
|
|
|
|
qWarning() << "DownloadFileHelper::optionsDialogAccepted invalid return value!";
|
|
|
|
if (m_timer) {
|
|
|
|
delete m_timer;
|
|
|
|
}
|
|
|
|
|
|
|
|
m_reply->abort();
|
|
|
|
m_reply->deleteLater();
|
|
|
|
return;
|
|
|
|
|
|
|
|
break;
|
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) {
|
|
|
|
if (m_downloadPath.isEmpty()) {
|
|
|
|
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();
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (m_timer) {
|
2011-09-30 19:22:50 +02:00
|
|
|
delete m_timer;
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2011-10-08 12:10:25 +02:00
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
int pos = m_userFileName.lastIndexOf("/");
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
if (fileNameAutoGenerated && QFile::exists(m_userFileName)) {
|
|
|
|
QString _tmpFileName = m_fileName;
|
|
|
|
int i = 1;
|
|
|
|
while (QFile::exists(m_path + "/" + _tmpFileName)) {
|
|
|
|
_tmpFileName = m_fileName;
|
|
|
|
int index = _tmpFileName.lastIndexOf(".");
|
|
|
|
|
|
|
|
if (index == -1) {
|
2011-11-06 17:01:23 +01:00
|
|
|
_tmpFileName.append("(" + QString::number(i) + ")");
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
_tmpFileName = _tmpFileName.mid(0, index) + "(" + QString::number(i) + ")" + _tmpFileName.mid(index);
|
2011-09-30 19:22:50 +02:00
|
|
|
}
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
m_fileName = _tmpFileName;
|
|
|
|
}
|
|
|
|
|
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);
|
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
|
|
|
|
if (value.contains("filename*=UTF-8")) {
|
|
|
|
QRegExp reg("filename\\*=UTF-8''([^;]*)");
|
|
|
|
reg.indexIn(value);
|
|
|
|
path = QUrl::fromPercentEncoding(reg.cap(1).toUtf8()).trimmed();
|
|
|
|
}
|
2012-02-15 14:39:46 +01:00
|
|
|
else if (value.contains("filename=")) {
|
|
|
|
QRegExp reg("filename=([^;]*)");
|
2012-02-15 13:20:35 +01:00
|
|
|
reg.indexIn(value);
|
|
|
|
path = reg.cap(1).trimmed();
|
2012-02-15 14:39:46 +01:00
|
|
|
|
|
|
|
if (path.startsWith("\"") && path.endsWith("\"")) {
|
|
|
|
path = path.mid(1, path.length() - 2);
|
|
|
|
}
|
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");
|
|
|
|
}
|
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
if (!endName.isEmpty()) {
|
|
|
|
endName = "." + endName;
|
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
|
2011-11-06 17:01:23 +01:00
|
|
|
QString name = baseName + endName;
|
2012-02-15 13:20:35 +01:00
|
|
|
if (name.contains("\"")) {
|
2011-09-30 19:22:50 +02:00
|
|
|
name.remove("\";");
|
2011-11-06 17:01:23 +01:00
|
|
|
}
|
2011-09-30 19:22:50 +02:00
|
|
|
|
2011-12-18 15:42:28 +01:00
|
|
|
name = qz_filterCharsFromFilename(name);
|
|
|
|
|
2011-09-30 19:22:50 +02:00
|
|
|
return name;
|
|
|
|
}
|
|
|
|
|
|
|
|
DownloadFileHelper::~DownloadFileHelper()
|
|
|
|
{
|
|
|
|
delete m_iconProvider;
|
|
|
|
}
|