Skip to content

Commit

Permalink
"Save x as..." will always show file dialog.
Browse files Browse the repository at this point in the history
  • Loading branch information
nowrep committed Aug 14, 2012
1 parent 826d85a commit d771ba7
Show file tree
Hide file tree
Showing 7 changed files with 68 additions and 40 deletions.
9 changes: 1 addition & 8 deletions src/lib/app/qupzilla.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1473,14 +1473,7 @@ void QupZilla::fullScreen(bool make)

void QupZilla::savePage()
{
QNetworkRequest request(weView()->url());
QString suggestedFileName = qz_getFileNameFromUrl(weView()->url());
if (!suggestedFileName.contains('.')) {
suggestedFileName.append(".html");
}

DownloadManager* dManager = mApp->downManager();
dManager->download(request, weView()->page(), false, suggestedFileName);
weView()->savePageAs();
}

void QupZilla::sendLink()
Expand Down
39 changes: 24 additions & 15 deletions src/lib/downloads/downloadfilehelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <QFileDialog>
#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()
, m_lastDownloadOption(DownloadManager::SaveFile)
, m_lastDownloadPath(lastDownloadPath)
Expand All @@ -46,7 +46,6 @@ DownloadFileHelper::DownloadFileHelper(const QString &lastDownloadPath, const QS
, m_listWidget(0)
, m_iconProvider(new QFileIconProvider)
, m_manager(0)
, m_webPage(page)
{
}

Expand All @@ -57,15 +56,15 @@ DownloadFileHelper::DownloadFileHelper(const QString &lastDownloadPath, const QS
//// 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->start();
m_h_fileName = suggestedFileName.isEmpty() ? getFileName(reply) : suggestedFileName;
m_h_fileName = info.suggestedFileName.isEmpty() ? getFileName(reply) : info.suggestedFileName;
m_reply = reply;

QFileInfo info(m_h_fileName);
QTemporaryFile tempFile("XXXXXX." + info.suffix());
QFileInfo fileInfo(m_h_fileName);
QTemporaryFile tempFile("XXXXXX." + fileInfo.suffix());
tempFile.open();
tempFile.write(m_reply->peek(1024 * 1024));
QFileInfo tempInfo(tempFile.fileName());
Expand All @@ -78,35 +77,40 @@ void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, bool ask
}

// Close Empty Tab
if (m_webPage) {
WebView* view = qobject_cast<WebView*>(m_webPage->view());
if (!m_webPage->url().isEmpty()) {
m_downloadPage = m_webPage->url();
if (info.page) {
WebView* view = qobject_cast<WebView*>(info.page->view());
if (!info.page->url().isEmpty()) {
m_downloadPage = info.page->url();
}
else if (m_webPage->history()->canGoBack()) {
m_downloadPage = m_webPage->history()->backItem().url();
else if (info.page->history()->canGoBack()) {
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();
}
}

if (askWhatToDo) {
if (info.askWhatToDo && m_downloadPath.isEmpty()) {
DownloadOptionsDialog* dialog = new DownloadOptionsDialog(m_h_fileName, m_fileIcon, mimeType, reply->url(), mApp->activeWindow());
dialog->showExternalManagerOption(m_manager->useExternalManager());
dialog->setLastDownloadOption(m_lastDownloadOption);
dialog->show();

connect(dialog, SIGNAL(dialogFinished(int)), this, SLOT(optionsDialogAccepted(int)));
}
else if (info.forceChoosingPath) {
optionsDialogAccepted(4);
}
else {
optionsDialogAccepted(2);
}
}

void DownloadFileHelper::optionsDialogAccepted(int finish)
{
bool forceChoosingPath = false;
m_openFileChoosed = false;

switch (finish) {
case 0: // Cancelled
if (m_timer) {
Expand All @@ -133,6 +137,11 @@ void DownloadFileHelper::optionsDialogAccepted(int finish)
m_reply->deleteLater();
return;

case 4: // Force opening save file dialog
m_lastDownloadOption = DownloadManager::SaveFile;
forceChoosingPath = true;
break;

default:
qWarning() << "DownloadFileHelper::optionsDialogAccepted invalid return value!";
if (m_timer) {
Expand All @@ -147,7 +156,7 @@ void DownloadFileHelper::optionsDialogAccepted(int finish)
m_manager->setLastDownloadOption(m_lastDownloadOption);

if (!m_openFileChoosed) {
if (m_downloadPath.isEmpty()) {
if (m_downloadPath.isEmpty() || forceChoosingPath) {
if (m_useNativeDialog) {
fileNameChoosed(QFileDialog::getSaveFileName(mApp->getWindow(), tr("Save file as..."), m_lastDownloadPath + m_h_fileName));
}
Expand Down
5 changes: 2 additions & 3 deletions src/lib/downloads/downloadfilehelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ class QT_QUPZILLA_EXPORT DownloadFileHelper : public QObject
{
Q_OBJECT
public:
explicit DownloadFileHelper(const QString &lastDownloadPath, const QString &downloadPath, bool useNativeDialog, WebPage* page);
explicit DownloadFileHelper(const QString &lastDownloadPath, const QString &downloadPath, bool useNativeDialog);
~DownloadFileHelper();

void setListWidget(QListWidget* tw) { m_listWidget = tw; }
void setDownloadManager(DownloadManager* m) { m_manager = m; }
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:
void itemCreated(QListWidgetItem* item, DownloadItem* downItem);
Expand Down Expand Up @@ -74,7 +74,6 @@ private slots:
QListWidget* m_listWidget;
QFileIconProvider* m_iconProvider;
DownloadManager* m_manager;
WebPage* m_webPage;
};

#endif // DOWNLOADFILEHELPER_H
14 changes: 7 additions & 7 deletions src/lib/downloads/downloadmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,22 +195,22 @@ void DownloadManager::clearList()
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;
}

// Clearing web page info from request
QNetworkRequest req = request;
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;
}

Expand All @@ -223,13 +223,13 @@ void DownloadManager::handleUnsupportedContent(QNetworkReply* reply, WebPage* pa

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*)));

h->setLastDownloadOption(m_lastDownloadOption);
h->setDownloadManager(this);
h->setListWidget(ui->list);
h->handleUnsupportedContent(reply, fromPageDownload, suggestedFileName);
h->handleUnsupportedContent(reply, info);
}

void DownloadManager::itemCreated(QListWidgetItem* item, DownloadItem* downItem)
Expand Down
19 changes: 17 additions & 2 deletions src/lib/downloads/downloadmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,28 @@ class QT_QUPZILLA_EXPORT DownloadManager : public QWidget
public:
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);
~DownloadManager();

void loadSettings();

void download(const QNetworkRequest &request, WebPage* page, bool fromPageDownload = true, const QString &suggestedFileName = QString());
void handleUnsupportedContent(QNetworkReply* reply, WebPage* page, bool fromPageDownload = true, const QString &suggestedFileName = QString());
void download(const QNetworkRequest &request, const DownloadInfo &info);
void handleUnsupportedContent(QNetworkReply* reply, const DownloadInfo &info);

bool canClose();

Expand Down
20 changes: 16 additions & 4 deletions src/lib/webview/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -397,25 +397,37 @@ void WebView::copyLinkToClipboard()
}
}

void WebView::downloadPage()
void WebView::savePageAs()
{
QNetworkRequest request(url());
QString suggestedFileName = qz_getFileNameFromUrl(url());
if (!suggestedFileName.contains('.')) {
suggestedFileName.append(".html");
}

DownloadManager::DownloadInfo info;
info.page = page();
info.suggestedFileName = suggestedFileName;
info.askWhatToDo = false;
info.forceChoosingPath = true;

DownloadManager* dManager = mApp->downManager();
dManager->download(request, page(), false, suggestedFileName);
dManager->download(request, info);
}

void WebView::downloadUrlToDisk()
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
QNetworkRequest request(action->data().toUrl());

DownloadManager::DownloadInfo info;
info.page = page();
info.suggestedFileName = QString();
info.askWhatToDo = false;
info.forceChoosingPath = true;

DownloadManager* dManager = mApp->downManager();
dManager->download(request, page(), false);
dManager->download(request, info);
}
}

Expand Down Expand Up @@ -770,7 +782,7 @@ void WebView::createPageContextMenu(QMenu* menu, const QPoint &pos)

menu->addSeparator();
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("mail-message-new"), tr("Send page link..."), this, SLOT(sendPageByMail()));
menu->addAction(QIcon::fromTheme("document-print"), tr("&Print page"), this, SLOT(printPage()));
Expand Down
2 changes: 1 addition & 1 deletion src/lib/webview/webview.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ public slots:
void selectAll();
void printPage(QWebFrame* frame = 0);
void sendPageByMail();
void savePageAs();

virtual void closeView() = 0;
virtual void openUrlInNewTab(const QUrl &url, Qz::NewTabPositionFlag position) = 0;
Expand All @@ -90,7 +91,6 @@ protected slots:
void openUrlInNewWindow();
void sendLinkByMail();
void copyLinkToClipboard();
void downloadPage();
void downloadUrlToDisk();
void copyImageToClipboard();
void openActionUrl();
Expand Down

0 comments on commit d771ba7

Please sign in to comment.