1
mirror of https://invent.kde.org/network/falkon.git synced 2024-12-20 18:56:34 +01:00

"Save x as..." will always show file dialog.

closes #471
This commit is contained in:
nowrep 2012-08-14 18:53:55 +02:00
parent 826d85a5e1
commit d771ba728b
7 changed files with 68 additions and 40 deletions

View File

@ -1473,14 +1473,7 @@ void QupZilla::fullScreen(bool make)
void QupZilla::savePage() void QupZilla::savePage()
{ {
QNetworkRequest request(weView()->url()); weView()->savePageAs();
QString suggestedFileName = qz_getFileNameFromUrl(weView()->url());
if (!suggestedFileName.contains('.')) {
suggestedFileName.append(".html");
}
DownloadManager* dManager = mApp->downManager();
dManager->download(request, weView()->page(), false, suggestedFileName);
} }
void QupZilla::sendLink() void QupZilla::sendLink()

View File

@ -34,7 +34,7 @@
#include <QFileDialog> #include <QFileDialog>
#include <QDesktopServices> #include <QDesktopServices>
DownloadFileHelper::DownloadFileHelper(const QString &lastDownloadPath, const QString &downloadPath, bool useNativeDialog, WebPage* page) DownloadFileHelper::DownloadFileHelper(const QString &lastDownloadPath, const QString &downloadPath, bool useNativeDialog)
: QObject() : QObject()
, m_lastDownloadOption(DownloadManager::SaveFile) , m_lastDownloadOption(DownloadManager::SaveFile)
, m_lastDownloadPath(lastDownloadPath) , m_lastDownloadPath(lastDownloadPath)
@ -46,7 +46,6 @@ DownloadFileHelper::DownloadFileHelper(const QString &lastDownloadPath, const QS
, m_listWidget(0) , m_listWidget(0)
, m_iconProvider(new QFileIconProvider) , m_iconProvider(new QFileIconProvider)
, m_manager(0) , m_manager(0)
, m_webPage(page)
{ {
} }
@ -57,15 +56,15 @@ DownloadFileHelper::DownloadFileHelper(const QString &lastDownloadPath, const QS
//// on Windows working properly ) //// on Windows working properly )
////////////////////////////////////////////////////// //////////////////////////////////////////////////////
void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, bool askWhatToDo, const QString &suggestedFileName) void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, const DownloadManager::DownloadInfo &info)
{ {
m_timer = new QTime(); m_timer = new QTime();
m_timer->start(); m_timer->start();
m_h_fileName = suggestedFileName.isEmpty() ? getFileName(reply) : suggestedFileName; m_h_fileName = info.suggestedFileName.isEmpty() ? getFileName(reply) : info.suggestedFileName;
m_reply = reply; m_reply = reply;
QFileInfo info(m_h_fileName); QFileInfo fileInfo(m_h_fileName);
QTemporaryFile tempFile("XXXXXX." + info.suffix()); QTemporaryFile tempFile("XXXXXX." + fileInfo.suffix());
tempFile.open(); tempFile.open();
tempFile.write(m_reply->peek(1024 * 1024)); tempFile.write(m_reply->peek(1024 * 1024));
QFileInfo tempInfo(tempFile.fileName()); QFileInfo tempInfo(tempFile.fileName());
@ -78,20 +77,20 @@ void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, bool ask
} }
// Close Empty Tab // Close Empty Tab
if (m_webPage) { if (info.page) {
WebView* view = qobject_cast<WebView*>(m_webPage->view()); WebView* view = qobject_cast<WebView*>(info.page->view());
if (!m_webPage->url().isEmpty()) { if (!info.page->url().isEmpty()) {
m_downloadPage = m_webPage->url(); m_downloadPage = info.page->url();
} }
else if (m_webPage->history()->canGoBack()) { else if (info.page->history()->canGoBack()) {
m_downloadPage = m_webPage->history()->backItem().url(); m_downloadPage = info.page->history()->backItem().url();
} }
else if (view && m_webPage->history()->count() == 0) { else if (view && info.page->history()->count() == 0) {
view->closeView(); view->closeView();
} }
} }
if (askWhatToDo) { if (info.askWhatToDo && m_downloadPath.isEmpty()) {
DownloadOptionsDialog* dialog = new DownloadOptionsDialog(m_h_fileName, m_fileIcon, mimeType, reply->url(), mApp->activeWindow()); DownloadOptionsDialog* dialog = new DownloadOptionsDialog(m_h_fileName, m_fileIcon, mimeType, reply->url(), mApp->activeWindow());
dialog->showExternalManagerOption(m_manager->useExternalManager()); dialog->showExternalManagerOption(m_manager->useExternalManager());
dialog->setLastDownloadOption(m_lastDownloadOption); dialog->setLastDownloadOption(m_lastDownloadOption);
@ -99,6 +98,9 @@ void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, bool ask
connect(dialog, SIGNAL(dialogFinished(int)), this, SLOT(optionsDialogAccepted(int))); connect(dialog, SIGNAL(dialogFinished(int)), this, SLOT(optionsDialogAccepted(int)));
} }
else if (info.forceChoosingPath) {
optionsDialogAccepted(4);
}
else { else {
optionsDialogAccepted(2); optionsDialogAccepted(2);
} }
@ -106,7 +108,9 @@ void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, bool ask
void DownloadFileHelper::optionsDialogAccepted(int finish) void DownloadFileHelper::optionsDialogAccepted(int finish)
{ {
bool forceChoosingPath = false;
m_openFileChoosed = false; m_openFileChoosed = false;
switch (finish) { switch (finish) {
case 0: // Cancelled case 0: // Cancelled
if (m_timer) { if (m_timer) {
@ -133,6 +137,11 @@ void DownloadFileHelper::optionsDialogAccepted(int finish)
m_reply->deleteLater(); m_reply->deleteLater();
return; return;
case 4: // Force opening save file dialog
m_lastDownloadOption = DownloadManager::SaveFile;
forceChoosingPath = true;
break;
default: default:
qWarning() << "DownloadFileHelper::optionsDialogAccepted invalid return value!"; qWarning() << "DownloadFileHelper::optionsDialogAccepted invalid return value!";
if (m_timer) { if (m_timer) {
@ -147,7 +156,7 @@ void DownloadFileHelper::optionsDialogAccepted(int finish)
m_manager->setLastDownloadOption(m_lastDownloadOption); m_manager->setLastDownloadOption(m_lastDownloadOption);
if (!m_openFileChoosed) { if (!m_openFileChoosed) {
if (m_downloadPath.isEmpty()) { if (m_downloadPath.isEmpty() || forceChoosingPath) {
if (m_useNativeDialog) { if (m_useNativeDialog) {
fileNameChoosed(QFileDialog::getSaveFileName(mApp->getWindow(), tr("Save file as..."), m_lastDownloadPath + m_h_fileName)); fileNameChoosed(QFileDialog::getSaveFileName(mApp->getWindow(), tr("Save file as..."), m_lastDownloadPath + m_h_fileName));
} }

View File

@ -36,14 +36,14 @@ class QT_QUPZILLA_EXPORT DownloadFileHelper : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
explicit DownloadFileHelper(const QString &lastDownloadPath, const QString &downloadPath, bool useNativeDialog, WebPage* page); explicit DownloadFileHelper(const QString &lastDownloadPath, const QString &downloadPath, bool useNativeDialog);
~DownloadFileHelper(); ~DownloadFileHelper();
void setListWidget(QListWidget* tw) { m_listWidget = tw; } void setListWidget(QListWidget* tw) { m_listWidget = tw; }
void setDownloadManager(DownloadManager* m) { m_manager = m; } void setDownloadManager(DownloadManager* m) { m_manager = m; }
void setLastDownloadOption(const DownloadManager::DownloadOption &option) { m_lastDownloadOption = option; } void setLastDownloadOption(const DownloadManager::DownloadOption &option) { m_lastDownloadOption = option; }
void handleUnsupportedContent(QNetworkReply* reply, bool askWhatToDo, const QString &suggestedFileName); void handleUnsupportedContent(QNetworkReply* reply, const DownloadManager::DownloadInfo &info);
signals: signals:
void itemCreated(QListWidgetItem* item, DownloadItem* downItem); void itemCreated(QListWidgetItem* item, DownloadItem* downItem);
@ -74,7 +74,6 @@ private:
QListWidget* m_listWidget; QListWidget* m_listWidget;
QFileIconProvider* m_iconProvider; QFileIconProvider* m_iconProvider;
DownloadManager* m_manager; DownloadManager* m_manager;
WebPage* m_webPage;
}; };
#endif // DOWNLOADFILEHELPER_H #endif // DOWNLOADFILEHELPER_H

View File

@ -195,9 +195,9 @@ void DownloadManager::clearList()
qDeleteAll(items); qDeleteAll(items);
} }
void DownloadManager::download(const QNetworkRequest &request, WebPage* page, bool fromPageDownload, const QString &suggestedFileName) void DownloadManager::download(const QNetworkRequest &request, const DownloadInfo &info)
{ {
if (!page) { if (!info.page) {
return; return;
} }
@ -205,12 +205,12 @@ void DownloadManager::download(const QNetworkRequest &request, WebPage* page, bo
QNetworkRequest req = request; QNetworkRequest req = request;
req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100), 0); req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100), 0);
handleUnsupportedContent(m_networkManager->get(req), page, fromPageDownload, suggestedFileName); handleUnsupportedContent(m_networkManager->get(req), info);
} }
void DownloadManager::handleUnsupportedContent(QNetworkReply* reply, WebPage* page, bool fromPageDownload, const QString &suggestedFileName) void DownloadManager::handleUnsupportedContent(QNetworkReply* reply, const DownloadInfo &info)
{ {
if (!page || reply->url().scheme() == "qupzilla") { if (!info.page || reply->url().scheme() == "qupzilla") {
return; return;
} }
@ -223,13 +223,13 @@ void DownloadManager::handleUnsupportedContent(QNetworkReply* reply, WebPage* pa
reply->setProperty("downReply", QVariant(true)); reply->setProperty("downReply", QVariant(true));
DownloadFileHelper* h = new DownloadFileHelper(m_lastDownloadPath, m_downloadPath, m_useNativeDialog, page); DownloadFileHelper* h = new DownloadFileHelper(m_lastDownloadPath, m_downloadPath, m_useNativeDialog);
connect(h, SIGNAL(itemCreated(QListWidgetItem*, DownloadItem*)), this, SLOT(itemCreated(QListWidgetItem*, DownloadItem*))); connect(h, SIGNAL(itemCreated(QListWidgetItem*, DownloadItem*)), this, SLOT(itemCreated(QListWidgetItem*, DownloadItem*)));
h->setLastDownloadOption(m_lastDownloadOption); h->setLastDownloadOption(m_lastDownloadOption);
h->setDownloadManager(this); h->setDownloadManager(this);
h->setListWidget(ui->list); h->setListWidget(ui->list);
h->handleUnsupportedContent(reply, fromPageDownload, suggestedFileName); h->handleUnsupportedContent(reply, info);
} }
void DownloadManager::itemCreated(QListWidgetItem* item, DownloadItem* downItem) void DownloadManager::itemCreated(QListWidgetItem* item, DownloadItem* downItem)

View File

@ -44,13 +44,28 @@ class QT_QUPZILLA_EXPORT DownloadManager : public QWidget
public: public:
enum DownloadOption { OpenFile, SaveFile, ExternalManager }; enum DownloadOption { OpenFile, SaveFile, ExternalManager };
struct DownloadInfo {
WebPage* page;
QString suggestedFileName;
bool askWhatToDo;
bool forceChoosingPath;
DownloadInfo(WebPage* p = 0) {
page = p;
suggestedFileName = QString();
askWhatToDo = true;
forceChoosingPath = false;
}
};
explicit DownloadManager(QWidget* parent = 0); explicit DownloadManager(QWidget* parent = 0);
~DownloadManager(); ~DownloadManager();
void loadSettings(); void loadSettings();
void download(const QNetworkRequest &request, WebPage* page, bool fromPageDownload = true, const QString &suggestedFileName = QString()); void download(const QNetworkRequest &request, const DownloadInfo &info);
void handleUnsupportedContent(QNetworkReply* reply, WebPage* page, bool fromPageDownload = true, const QString &suggestedFileName = QString()); void handleUnsupportedContent(QNetworkReply* reply, const DownloadInfo &info);
bool canClose(); bool canClose();

View File

@ -397,7 +397,7 @@ void WebView::copyLinkToClipboard()
} }
} }
void WebView::downloadPage() void WebView::savePageAs()
{ {
QNetworkRequest request(url()); QNetworkRequest request(url());
QString suggestedFileName = qz_getFileNameFromUrl(url()); QString suggestedFileName = qz_getFileNameFromUrl(url());
@ -405,8 +405,14 @@ void WebView::downloadPage()
suggestedFileName.append(".html"); suggestedFileName.append(".html");
} }
DownloadManager::DownloadInfo info;
info.page = page();
info.suggestedFileName = suggestedFileName;
info.askWhatToDo = false;
info.forceChoosingPath = true;
DownloadManager* dManager = mApp->downManager(); DownloadManager* dManager = mApp->downManager();
dManager->download(request, page(), false, suggestedFileName); dManager->download(request, info);
} }
void WebView::downloadUrlToDisk() void WebView::downloadUrlToDisk()
@ -414,8 +420,14 @@ void WebView::downloadUrlToDisk()
if (QAction* action = qobject_cast<QAction*>(sender())) { if (QAction* action = qobject_cast<QAction*>(sender())) {
QNetworkRequest request(action->data().toUrl()); QNetworkRequest request(action->data().toUrl());
DownloadManager::DownloadInfo info;
info.page = page();
info.suggestedFileName = QString();
info.askWhatToDo = false;
info.forceChoosingPath = true;
DownloadManager* dManager = mApp->downManager(); DownloadManager* dManager = mApp->downManager();
dManager->download(request, page(), false); dManager->download(request, info);
} }
} }
@ -770,7 +782,7 @@ void WebView::createPageContextMenu(QMenu* menu, const QPoint &pos)
menu->addSeparator(); menu->addSeparator();
menu->addAction(qIconProvider->fromTheme("user-bookmarks"), tr("Book&mark page"), this, SLOT(bookmarkLink())); menu->addAction(qIconProvider->fromTheme("user-bookmarks"), tr("Book&mark page"), this, SLOT(bookmarkLink()));
menu->addAction(QIcon::fromTheme("document-save"), tr("&Save page as..."), this, SLOT(downloadPage())); menu->addAction(QIcon::fromTheme("document-save"), tr("&Save page as..."), this, SLOT(savePageAs()));
menu->addAction(QIcon::fromTheme("edit-copy"), tr("&Copy page link"), this, SLOT(copyLinkToClipboard()))->setData(url()); menu->addAction(QIcon::fromTheme("edit-copy"), tr("&Copy page link"), this, SLOT(copyLinkToClipboard()))->setData(url());
menu->addAction(QIcon::fromTheme("mail-message-new"), tr("Send page link..."), this, SLOT(sendPageByMail())); menu->addAction(QIcon::fromTheme("mail-message-new"), tr("Send page link..."), this, SLOT(sendPageByMail()));
menu->addAction(QIcon::fromTheme("document-print"), tr("&Print page"), this, SLOT(printPage())); menu->addAction(QIcon::fromTheme("document-print"), tr("&Print page"), this, SLOT(printPage()));

View File

@ -75,6 +75,7 @@ public slots:
void selectAll(); void selectAll();
void printPage(QWebFrame* frame = 0); void printPage(QWebFrame* frame = 0);
void sendPageByMail(); void sendPageByMail();
void savePageAs();
virtual void closeView() = 0; virtual void closeView() = 0;
virtual void openUrlInNewTab(const QUrl &url, Qz::NewTabPositionFlag position) = 0; virtual void openUrlInNewTab(const QUrl &url, Qz::NewTabPositionFlag position) = 0;
@ -90,7 +91,6 @@ protected slots:
void openUrlInNewWindow(); void openUrlInNewWindow();
void sendLinkByMail(); void sendLinkByMail();
void copyLinkToClipboard(); void copyLinkToClipboard();
void downloadPage();
void downloadUrlToDisk(); void downloadUrlToDisk();
void copyImageToClipboard(); void copyImageToClipboard();
void openActionUrl(); void openActionUrl();