1
mirror of https://invent.kde.org/network/falkon.git synced 2024-09-21 17:52:10 +02:00

Improved suggesting file name when downloading page / links.

- it now appends .html ONLY when you are attempting to download
  the whole page and when there is no suffix set in url
This commit is contained in:
nowrep 2012-03-23 13:58:31 +01:00
parent 9dc38ca000
commit 373d6162d7
8 changed files with 49 additions and 40 deletions

View File

@ -1504,9 +1504,13 @@ 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()->webPage(), false);
dManager->download(request, weView()->webPage(), false, suggestedFileName);
}
void QupZilla::sendLink()

View File

@ -57,11 +57,11 @@ DownloadFileHelper::DownloadFileHelper(const QString &lastDownloadPath, const QS
//// on Windows working properly )
//////////////////////////////////////////////////////
void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, bool askWhatToDo)
void DownloadFileHelper::handleUnsupportedContent(QNetworkReply* reply, bool askWhatToDo, const QString &suggestedFileName)
{
m_timer = new QTime();
m_timer->start();
m_h_fileName = getFileName(reply);
m_h_fileName = suggestedFileName.isEmpty() ? getFileName(reply) : suggestedFileName;
m_reply = reply;
QFileInfo info(m_h_fileName);
@ -196,22 +196,8 @@ void DownloadFileHelper::fileNameChoosed(const QString &name, bool fileNameAutoG
m_fileName = m_userFileName.right(size - pos - 1);
}
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) {
_tmpFileName.append("(" + QString::number(i) + ")");
}
else {
_tmpFileName = _tmpFileName.mid(0, index) + "(" + QString::number(i) + ")" + _tmpFileName.mid(index);
}
i++;
}
m_fileName = _tmpFileName;
if (fileNameAutoGenerated) {
m_fileName = qz_ensureUniqueFilename(m_fileName);
}
if (!m_path.contains(QDir::tempPath())) {
@ -279,9 +265,7 @@ QString DownloadFileHelper::getFileName(QNetworkReply* reply)
name.remove("\";");
}
name = qz_filterCharsFromFilename(name);
return name;
return qz_filterCharsFromFilename(name);
}
DownloadFileHelper::~DownloadFileHelper()

View File

@ -43,7 +43,7 @@ public:
void setDownloadManager(DownloadManager* m) { m_manager = m; }
void setLastDownloadOption(const DownloadManager::DownloadOption &option) { m_lastDownloadOption = option; }
void handleUnsupportedContent(QNetworkReply* reply, bool askWhatToDo);
void handleUnsupportedContent(QNetworkReply* reply, bool askWhatToDo, const QString &suggestedFileName);
signals:
void itemCreated(QListWidgetItem* item, DownloadItem* downItem);

View File

@ -190,7 +190,7 @@ void DownloadManager::clearList()
qDeleteAll(items);
}
void DownloadManager::download(const QNetworkRequest &request, WebPage* page, bool askWhatToDo)
void DownloadManager::download(const QNetworkRequest &request, WebPage* page, bool fromPageDownload, const QString &suggestedFileName)
{
if (!page) {
return;
@ -201,16 +201,16 @@ void DownloadManager::download(const QNetworkRequest &request, WebPage* page, bo
req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 100), 0);
req.setAttribute((QNetworkRequest::Attribute)(QNetworkRequest::User + 101), 0);
handleUnsupportedContent(m_networkManager->get(req), page, askWhatToDo);
handleUnsupportedContent(m_networkManager->get(req), page, fromPageDownload, suggestedFileName);
}
void DownloadManager::handleUnsupportedContent(QNetworkReply* reply, WebPage* page, bool askWhatToDo)
void DownloadManager::handleUnsupportedContent(QNetworkReply *reply, WebPage *page, bool fromPageDownload, const QString &suggestedFileName)
{
if (reply->url().scheme() == "qupzilla") {
if (!page || reply->url().scheme() == "qupzilla") {
return;
}
if (m_useExternalManager) {
if (!fromPageDownload && m_useExternalManager) {
startExternalManager(reply->url());
reply->abort();
reply->deleteLater();
@ -225,7 +225,7 @@ void DownloadManager::handleUnsupportedContent(QNetworkReply* reply, WebPage* pa
h->setLastDownloadOption(m_lastDownloadOption);
h->setDownloadManager(this);
h->setListWidget(ui->list);
h->handleUnsupportedContent(reply, askWhatToDo);
h->handleUnsupportedContent(reply, fromPageDownload, suggestedFileName);
}
void DownloadManager::itemCreated(QListWidgetItem* item, DownloadItem* downItem)

View File

@ -49,8 +49,9 @@ public:
void loadSettings();
void download(const QNetworkRequest &request, WebPage* page, bool askWhatToDo = true);
void handleUnsupportedContent(QNetworkReply* reply, WebPage* page, bool askWhatToDo = true);
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());
bool canClose();
void setLastDownloadPath(const QString &lastPath) { m_lastDownloadPath = lastPath; }
void setLastDownloadOption(const DownloadOption &option) { m_lastDownloadOption = option; }

View File

@ -210,14 +210,20 @@ QString qz_getFileNameFromUrl(const QUrl &url)
fileName.remove("/");
}
return qz_filterCharsFromFilename(fileName);
fileName = qz_filterCharsFromFilename(fileName);
if (fileName.isEmpty()) {
fileName = qz_filterCharsFromFilename(url.host().replace(".", "-"));
}
return fileName;
}
QString qz_filterCharsFromFilename(const QString &name)
{
QString value = name;
value.replace("/", "-");
value.remove("\\");
value.remove("/");
value.remove(":");
value.remove("*");
value.remove("?");

View File

@ -338,11 +338,24 @@ void WebView::copyLinkToClipboard()
}
}
void WebView::downloadLinkToDisk()
void WebView::downloadPage()
{
QNetworkRequest request(url());
QString suggestedFileName = qz_getFileNameFromUrl(url());
if (!suggestedFileName.contains(".")) {
suggestedFileName.append(".html");
}
DownloadManager* dManager = mApp->downManager();
dManager->download(request, qobject_cast<WebPage*>(page()), false, suggestedFileName);
}
void WebView::downloadUrlToDisk()
{
if (QAction* action = qobject_cast<QAction*>(sender())) {
DownloadManager* dManager = mApp->downManager();
QNetworkRequest request(action->data().toUrl());
DownloadManager* dManager = mApp->downManager();
dManager->download(request, qobject_cast<WebPage*>(page()), false);
}
}
@ -662,7 +675,7 @@ void WebView::createPageContextMenu(QMenu* menu, const QPoint &pos)
menu->addSeparator();
menu->addAction(IconProvider::fromTheme("user-bookmarks"), tr("Book&mark page"), this, SLOT(bookmarkLink()));
menu->addAction(QIcon::fromTheme("document-save"), tr("&Save page as..."), this, SLOT(downloadLinkToDisk()))->setData(url());
menu->addAction(QIcon::fromTheme("document-save"), tr("&Save page as..."), this, SLOT(downloadPage()));
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(sendLinkByMail()))->setData(url());
menu->addAction(QIcon::fromTheme("document-print"), tr("&Print page"), this, SLOT(printPage()));
@ -691,7 +704,7 @@ void WebView::createLinkContextMenu(QMenu* menu, const QWebHitTestResult &hitTes
menu->addAction(QIcon::fromTheme("window-new"), tr("Open link in new &window"), this, SLOT(openUrlInNewWindow()))->setData(hitTest.linkUrl());
menu->addSeparator();
menu->addAction(IconProvider::fromTheme("user-bookmarks"), tr("B&ookmark link"), this, SLOT(bookmarkLink()))->setData(hitTest.linkUrl());
menu->addAction(QIcon::fromTheme("document-save"), tr("&Save link as..."), this, SLOT(downloadLinkToDisk()))->setData(hitTest.linkUrl());
menu->addAction(QIcon::fromTheme("document-save"), tr("&Save link as..."), this, SLOT(downloadUrlToDisk()))->setData(hitTest.linkUrl());
menu->addAction(QIcon::fromTheme("mail-message-new"), tr("Send link..."), this, SLOT(sendLinkByMail()))->setData(hitTest.linkUrl());
menu->addAction(QIcon::fromTheme("edit-copy"), tr("&Copy link address"), this, SLOT(copyLinkToClipboard()))->setData(hitTest.linkUrl());
menu->addSeparator();
@ -713,7 +726,7 @@ void WebView::createImageContextMenu(QMenu* menu, const QWebHitTestResult &hitTe
menu->addAction(tr("Copy im&age"), this, SLOT(copyImageToClipboard()))->setData(hitTest.imageUrl());
menu->addAction(QIcon::fromTheme("edit-copy"), tr("Copy image ad&dress"), this, SLOT(copyLinkToClipboard()))->setData(hitTest.imageUrl());
menu->addSeparator();
menu->addAction(QIcon::fromTheme("document-save"), tr("&Save image as..."), this, SLOT(downloadLinkToDisk()))->setData(hitTest.imageUrl());
menu->addAction(QIcon::fromTheme("document-save"), tr("&Save image as..."), this, SLOT(downloadUrlToDisk()))->setData(hitTest.imageUrl());
menu->addAction(QIcon::fromTheme("mail-message-new"), tr("Send image..."), this, SLOT(sendLinkByMail()))->setData(hitTest.imageUrl());
menu->addSeparator();
@ -797,7 +810,7 @@ void WebView::createMediaContextMenu(QMenu* menu, const QWebHitTestResult &hitTe
menu->addSeparator();
menu->addAction(QIcon::fromTheme("edit-copy"), tr("&Copy Media Address"), this, SLOT(copyLinkToClipboard()))->setData(videoUrl);
menu->addAction(QIcon::fromTheme("mail-message-new"), tr("&Send Media Address"), this, SLOT(sendLinkByMail()))->setData(videoUrl);
menu->addAction(QIcon::fromTheme("document-save"), tr("Save Media To &Disk"), this, SLOT(downloadLinkToDisk()))->setData(videoUrl);
menu->addAction(QIcon::fromTheme("document-save"), tr("Save Media To &Disk"), this, SLOT(downloadUrlToDisk()))->setData(videoUrl);
}
void WebView::pauseMedia()

View File

@ -83,7 +83,8 @@ protected slots:
void openUrlInNewWindow();
void sendLinkByMail();
void copyLinkToClipboard();
void downloadLinkToDisk();
void downloadPage();
void downloadUrlToDisk();
void copyImageToClipboard();
void openActionUrl();
void showSource(QWebFrame* frame = 0, const QString &selectedHtml = QString());