Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Save as operation remembers the previous save path
Browse files Browse the repository at this point in the history
After saving a page, the location is remembered and is used as the default folder to save a page next time. Documents folder is the original default folder.
ShaopengLin committed Jun 22, 2024

Verified

This commit was signed with the committer’s verified signature.
rouault Even Rouault
1 parent 48b2c87 commit c23025d
Showing 4 changed files with 42 additions and 6 deletions.
20 changes: 20 additions & 0 deletions src/kiwixapp.cpp
Original file line number Diff line number Diff line change
@@ -19,6 +19,8 @@
#include <QtPlatformHeaders\QWindowsWindowFunctions>
#endif

const QString DEFAULT_SAVE_DIR = QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation);

////////////////////////////////////////////////////////////////////////////////
// KiwixApp
////////////////////////////////////////////////////////////////////////////////
@@ -125,6 +127,7 @@ void KiwixApp::init()

restoreTabs();
restoreWindowState();
restorePrevSaveDir();
}

KiwixApp::~KiwixApp()
@@ -220,6 +223,11 @@ void KiwixApp::restoreTabs()
getTabWidget()->setCurrentIndex(mp_session->value("currentTabIndex", 0).toInt());
}

void KiwixApp::restorePrevSaveDir()
{
m_prevSaveDir = mp_session->value("prevSaveDir", DEFAULT_SAVE_DIR).toString();
}

KiwixApp *KiwixApp::instance()
{
return static_cast<KiwixApp*>(QApplication::instance());
@@ -573,3 +581,15 @@ void KiwixApp::saveCurrentTabIndex()
{
return mp_session->setValue("currentTabIndex", getTabWidget()->currentIndex());
}

void KiwixApp::savePrevSaveDir(const QString &prevSaveDir)
{
m_prevSaveDir = prevSaveDir;
mp_session->setValue("prevSaveDir", m_prevSaveDir);
}

QString KiwixApp::getPrevSaveDir() const
{
QDir dir(m_prevSaveDir);
return dir.exists() ? m_prevSaveDir : DEFAULT_SAVE_DIR;
}
4 changes: 4 additions & 0 deletions src/kiwixapp.h
Original file line number Diff line number Diff line change
@@ -92,6 +92,8 @@ class KiwixApp : public QtSingleApplication
void saveWindowState();
void restoreWindowState();
void saveCurrentTabIndex();
void savePrevSaveDir(const QString& prevSaveDir);
QString getPrevSaveDir() const;

public slots:
void newTab();
@@ -112,6 +114,7 @@ public slots:
SettingsManager m_settingsManager;
KProfile m_profile;
QString m_libraryDirectory;
QString m_prevSaveDir;
Library m_library;
kiwix::Downloader* mp_downloader;
ContentManager* mp_manager;
@@ -127,6 +130,7 @@ public slots:

QString findLibraryDirectory();
void restoreTabs();
void restorePrevSaveDir();
void loadAndInstallTranslations(QTranslator& translator, const QString& filename, const QString& directory);
};

15 changes: 12 additions & 3 deletions src/kprofile.cpp
Original file line number Diff line number Diff line change
@@ -24,12 +24,21 @@ void KProfile::startDownload(QWebEngineDownloadItem* download)
void KProfile::startDownload(QWebEngineDownloadRequest* download)
#endif
{
QString defaultFileName = download->url().fileName();
QString fileName = QFileDialog::getSaveFileName(KiwixApp::instance()->getMainWindow(),
gt("save-file-as-window-title"), defaultFileName);
auto app = KiwixApp::instance();
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
auto suggestedPath = download->path();
#else
auto suggestedPath = download->downloadFileName();
#endif
QString fileName = QFileDialog::getSaveFileName(
KiwixApp::instance()->getMainWindow(), gt("save-file-as-window-title"),
suggestedPath);

if (fileName.isEmpty()) {
return;
}
app->savePrevSaveDir(QFileInfo(fileName).absolutePath());

QString extension = "." + download->url().url().section('.', -1);
if (!fileName.endsWith(extension)) {
fileName.append(extension);
9 changes: 6 additions & 3 deletions src/webview.cpp
Original file line number Diff line number Diff line change
@@ -146,18 +146,21 @@ void WebView::downloadViewContent()
auto mimeType = QByteArray::fromStdString(item.getMimetype());
mimeType = mimeType.split(';')[0];

auto suggestedDir = app->getPrevSaveDir();
auto suggestedPath = suggestedDir + "/" + item.getTitle().c_str() + ".pdf";
if (mimeType == "text/html")
{
QString fileName = QFileDialog::getSaveFileName(
app->getMainWindow(), gt("save-file-as-window-title"), "",
gt("pdf-files-filter") + " (*.pdf)");
app->getMainWindow(), gt("save-file-as-window-title"),
suggestedPath, gt("pdf-files-filter") + " (*.pdf)");
if (fileName.isEmpty())
return;

app->savePrevSaveDir(QFileInfo(fileName).absolutePath());
page()->printToPdf(fileName);
}
else
page()->download(this->url());
page()->download(this->url(), suggestedPath);
}
catch (...) { /* Blank */}
}

0 comments on commit c23025d

Please sign in to comment.