From 77398618a48027f56c9bbd95e8a02245d79f6884 Mon Sep 17 00:00:00 2001 From: Soren Stoutner Date: Thu, 28 Dec 2023 14:19:10 -0700 Subject: [PATCH] Add option to auto update the download directory. https://redmine.stoutner.com/issues/1103 --- src/settings/Settings.kcfg | 6 +- src/settings/Settings.kcfgc | 2 +- src/uis/SettingsGeneral.ui | 106 +++++++++++++++++++--------------- src/widgets/TabWidget.cpp | 35 +++++++++-- src/widgets/TabWidget.h | 3 + src/windows/BrowserWindow.cpp | 31 +++++----- src/windows/BrowserWindow.h | 4 +- 7 files changed, 117 insertions(+), 70 deletions(-) diff --git a/src/settings/Settings.kcfg b/src/settings/Settings.kcfg index 4216013..3607793 100644 --- a/src/settings/Settings.kcfg +++ b/src/settings/Settings.kcfg @@ -60,10 +60,14 @@ 1.00 - + System Download Directory + + true + + true diff --git a/src/settings/Settings.kcfgc b/src/settings/Settings.kcfgc index dce7cb8..3cb234c 100644 --- a/src/settings/Settings.kcfgc +++ b/src/settings/Settings.kcfgc @@ -29,4 +29,4 @@ ClassName=Settings Singleton=true # List of variables that can be manually changed. -Mutators=spellCheckLanguages +Mutators=spellCheckLanguages,downloadDirectory diff --git a/src/uis/SettingsGeneral.ui b/src/uis/SettingsGeneral.ui index 4c08bda..15f776f 100644 --- a/src/uis/SettingsGeneral.ui +++ b/src/uis/SettingsGeneral.ui @@ -146,52 +146,6 @@ - - - - - - - Download Location - - - - The default is System Download Directory. - - - - - - - - - 0 - 0 - - - - - true - - - - - System Download Directory - - - - - - - - - Browse - - - - - - @@ -213,8 +167,66 @@ - Spatial navigation allows the moving between links and input fields using the keyboard arrow keys. The default is enabled. + Allow moving between links and input fields using the keyboard arrow keys. The default is enabled. + + + + + + + + + Download Location + + + + + + + + + + 0 + 0 + + + + + true + + + + + System Download Directory + + + + + + + + + Browse + + + + + + + + + + + Auto update the download directory + + + + Automatically update the download directory to be whatever was used for the last download. The default is enabled. + + + + diff --git a/src/widgets/TabWidget.cpp b/src/widgets/TabWidget.cpp index efd677d..4f4a635 100644 --- a/src/widgets/TabWidget.cpp +++ b/src/widgets/TabWidget.cpp @@ -916,7 +916,7 @@ void TabWidget::saveArchive() QString suggestedFileName = currentPrivacyWebEngineViewPointer->title() + ".mht"; // Get the download directory. - QString downloadDirectory = Settings::downloadLocation(); + QString downloadDirectory = Settings::downloadDirectory(); // Resolve the system download directory if specified. if (downloadDirectory == QLatin1String("System Download Directory")) @@ -928,6 +928,10 @@ void TabWidget::saveArchive() // Save the webpage as an archive if the file save path is populated. if (!saveFilePath.isEmpty()) { + // Update the download directory if specified. + if (Settings::autoUpateDownloadDirectory()) + updateDownloadDirectory(saveFilePath); + // Set the saving archive flag. Otherwise, a second download tries to run. savingArchive = true; @@ -966,7 +970,7 @@ void TabWidget::showSaveDialog(QWebEngineDownloadItem *webEngineDownloadItemPoin if (saveDialogResult == QDialog::Accepted) // Save was selected. { // Get the download directory. - QString downloadDirectory = Settings::downloadLocation(); + QString downloadDirectory = Settings::downloadDirectory(); // Resolve the system download directory if specified. if (downloadDirectory == QLatin1String("System Download Directory")) @@ -978,6 +982,10 @@ void TabWidget::showSaveDialog(QWebEngineDownloadItem *webEngineDownloadItemPoin // Process the save file path. if (!saveFilePath.isEmpty()) // The file save path is populated. { + // Update the download directory if specified. + if (Settings::autoUpateDownloadDirectory()) + updateDownloadDirectory(saveFilePath); + // Create a save file path file info. QFileInfo saveFilePathFileInfo = QFileInfo(saveFilePath); @@ -1199,6 +1207,21 @@ void TabWidget::toggleLocalStorage() currentPrivacyWebEngineViewPointer->reload(); } +void TabWidget::updateDownloadDirectory(QString newDownloadDirectory) const +{ + // Remove the file name from the save file path. + newDownloadDirectory.truncate(newDownloadDirectory.lastIndexOf(QLatin1Char('/'))); + + // Update the download location. + Settings::setDownloadDirectory(newDownloadDirectory); + + // Get a handle for the KConfig skeleton. + KConfigSkeleton *kConfigSkeletonPointer = Settings::self(); + + // Write the settings to disk. + kConfigSkeletonPointer->save(); +} + void TabWidget::updateUiFromWebEngineView(const PrivacyWebEngineView *privacyWebEngineViewPointer) const { // Only update the UI if the signal was emitted from the current privacy WebEngine. @@ -1262,7 +1285,7 @@ void TabWidget::updateUiWithTabSettings() void TabWidget::useNativeKdeDownloader(QUrl &downloadUrl, QString &suggestedFileName) { // Get the download directory. - QString downloadDirectory = Settings::downloadLocation(); + QString downloadDirectory = Settings::downloadDirectory(); // Resolve the system download directory if specified. if (downloadDirectory == QLatin1String("System Download Directory")) @@ -1281,11 +1304,15 @@ void TabWidget::useNativeKdeDownloader(QUrl &downloadUrl, QString &suggestedFile saveFileDialogPointer->setWindowModality(Qt::WindowModal); // Process the saving of the file. The save file dialog pointer must be captured directly instead of by reference or nasty crashes occur. - auto saveFile = [saveFileDialogPointer, downloadUrl] () + auto saveFile = [saveFileDialogPointer, downloadUrl, this] () { // Get the save location. The dialog box should only allow the selecting of one file location. QUrl saveLocation = saveFileDialogPointer->selectedUrls().value(0); + // Update the download directory if specified. + if (Settings::autoUpateDownloadDirectory()) + updateDownloadDirectory(saveLocation.toLocalFile()); + // Create a file copy job. `-1` creates the file with default permissions. KIO::FileCopyJob *fileCopyJobPointer = KIO::file_copy(downloadUrl, saveLocation, -1, KIO::Overwrite); diff --git a/src/widgets/TabWidget.h b/src/widgets/TabWidget.h index e47370c..06676d6 100644 --- a/src/widgets/TabWidget.h +++ b/src/widgets/TabWidget.h @@ -136,6 +136,9 @@ private Q_SLOTS: void useNativeKdeDownloader(QUrl &downloadUrl, QString &suggestedFileName); private: + // The private functions. + void updateDownloadDirectory(QString newDownloadDirectory) const; + // The private variables. PrivacyWebEngineView *currentPrivacyWebEngineViewPointer; QWebEngineCookieStore *currentWebEngineCookieStorePointer; diff --git a/src/windows/BrowserWindow.cpp b/src/windows/BrowserWindow.cpp index 394340b..1513d1a 100644 --- a/src/windows/BrowserWindow.cpp +++ b/src/windows/BrowserWindow.cpp @@ -1446,28 +1446,29 @@ void BrowserWindow::showCookiesDialog() connect(cookiesDialogPointer, SIGNAL(deleteCookie(QNetworkCookie)), tabWidgetPointer, SLOT(deleteCookieFromStore(QNetworkCookie))); } -void BrowserWindow::showDownloadLocationBrowseDialog() const +void BrowserWindow::showDownloadDirectoryBrowseDialog() const { - // Get the current download location. - QString currentDownloadLocation = downloadLocationComboBoxPointer->currentText(); + // Get the current download directory. + QString currentDownloadDirectory = downloadDirectoryComboBoxPointer->currentText(); // Resolve the system download directory if specified. - if (currentDownloadLocation == QStringLiteral("System Download Directory")) - currentDownloadLocation = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); + if (currentDownloadDirectory == QStringLiteral("System Download Directory")) + currentDownloadDirectory = QStandardPaths::writableLocation(QStandardPaths::DownloadLocation); - // Get the new download location. - QString newDownloadLocation = QFileDialog::getExistingDirectory(configDialogPointer, i18nc("Select download location dialog caption", "Select Download Location"), currentDownloadLocation); + // Get the new download directory. + QString newDownloadDirectory = QFileDialog::getExistingDirectory(configDialogPointer, i18nc("Select download directory dialog caption", "Select Download Directory"), + currentDownloadDirectory); - // Populate the download location combo box according to the new download location. - if (newDownloadLocation == QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)) // The default download location was selected. + // Populate the download directory combo box according to the new download location. + if (newDownloadDirectory == QStandardPaths::writableLocation(QStandardPaths::DownloadLocation)) // The default download location was selected. { // Populate the download location with the default text. - downloadLocationComboBoxPointer->setCurrentText("System Download Directory"); + downloadDirectoryComboBoxPointer->setCurrentText("System Download Directory"); } - else if (newDownloadLocation != QStringLiteral("")) // A different directory was selected. + else if (newDownloadDirectory != QStringLiteral("")) // A different directory was selected. { // Populate the download location. - downloadLocationComboBoxPointer->setCurrentText(newDownloadLocation); + downloadDirectoryComboBoxPointer->setCurrentText(newDownloadDirectory); } } @@ -1542,7 +1543,7 @@ void BrowserWindow::showSettingsDialog() userAgentLabelPointer = privacySettingsUi.userAgentLabel; QComboBox *searchEngineComboBoxPointer = generalSettingsUi.kcfg_searchEngine; searchEngineLabelPointer = generalSettingsUi.searchEngineLabel; - downloadLocationComboBoxPointer = generalSettingsUi.kcfg_downloadLocation; + downloadDirectoryComboBoxPointer = generalSettingsUi.kcfg_downloadDirectory; QPushButton *browseButtonPointer = generalSettingsUi.browseButton; QListWidget *spellCheckListWidgetPointer = spellCheckSettingsUi.spellCheckListWidget; @@ -1554,8 +1555,8 @@ void BrowserWindow::showSettingsDialog() connect(userAgentComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateUserAgentLabel(const QString))); connect(searchEngineComboBoxPointer, SIGNAL(currentTextChanged(const QString)), this, SLOT(updateSearchEngineLabel(const QString))); - // Connect the download location directory browse button. - connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(showDownloadLocationBrowseDialog())); + // Connect the download directory directory browse button. + connect(browseButtonPointer, SIGNAL(clicked()), this, SLOT(showDownloadDirectoryBrowseDialog())); // Create a dictionaries QDir from the `QTWEBENGINE_DICTIONARIES_PATH` environment variable. QDir dictionariesDir = QDir(qEnvironmentVariable("QTWEBENGINE_DICTIONARIES_PATH")); diff --git a/src/windows/BrowserWindow.h b/src/windows/BrowserWindow.h index 87297dc..143e35f 100644 --- a/src/windows/BrowserWindow.h +++ b/src/windows/BrowserWindow.h @@ -77,7 +77,7 @@ private Q_SLOTS: void reloadAndBypassCache() const; void showBookmarkContextMenu(const QPoint &point); void showCookiesDialog(); - void showDownloadLocationBrowseDialog() const; + void showDownloadDirectoryBrowseDialog() const; void showDomainSettingsDialog() const; void showFindTextActions() const; void showProgressBar(const int &progress) const; @@ -133,7 +133,7 @@ private: double defaultZoomFactor; QAction *developerToolsActionPointer; QAction *domStorageActionPointer; - QComboBox *downloadLocationComboBoxPointer; + QComboBox *downloadDirectoryComboBoxPointer; QList *> finalBookmarkFolderMenuActionList; QAction *findCaseSensitiveActionPointer; QAction *findNextActionPointer; -- 2.45.2