]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blobdiff - src/widgets/TabWidget.cpp
Add option to auto update the download directory. https://redmine.stoutner.com/issue...
[PrivacyBrowserPC.git] / src / widgets / TabWidget.cpp
index efd677d73e636d37f9018839a019edaf54c1feb8..4f4a635651a16fca4ebd60a335408081cd056b13 100644 (file)
@@ -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);