Merge pull request #1169 from kiwix/feature/data-directory

Migrate getDataDirectory from libkiwix to kiwix-desktop
This commit is contained in:
Kelson 2024-08-21 23:51:10 +02:00 committed by GitHub
commit 57dc50f25a
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 17 additions and 8 deletions

View File

@ -105,7 +105,7 @@ namespace
kiwix::Downloader* createDownloader() kiwix::Downloader* createDownloader()
{ {
try { try {
return new kiwix::Downloader(); return new kiwix::Downloader(getDataDirectory().toStdString());
} catch (std::exception& e) { } catch (std::exception& e) {
QMessageBox::critical(nullptr, gt("error-downloader-window-title"), QMessageBox::critical(nullptr, gt("error-downloader-window-title"),
gt("error-downloader-launch-message") + "<br><br>" + e.what()); gt("error-downloader-launch-message") + "<br><br>" + e.what());
@ -283,19 +283,17 @@ void DownloadManager::checkThatBookCanBeDownloaded(const kiwix::Book& book, cons
std::string DownloadManager::startDownload(const kiwix::Book& book, const QString& downloadDirPath) std::string DownloadManager::startDownload(const kiwix::Book& book, const QString& downloadDirPath)
{ {
typedef std::vector<std::pair<std::string, std::string>> DownloadOptions;
const std::string& url = book.getUrl(); const std::string& url = book.getUrl();
const QString bookId = QString::fromStdString(book.getId()); const QString bookId = QString::fromStdString(book.getId());
const DownloadOptions downloadOptions{{"dir", downloadDirPath.toStdString()}};
std::string downloadId; std::string downloadId;
try { try {
const auto d = mp_downloader->startDownload(url, downloadOptions); const auto d = mp_downloader->startDownload(url, downloadDirPath.toStdString());
downloadId = d->getDid(); downloadId = d->getDid();
} catch (std::exception& e) { } catch (std::exception& e) {
throwDownloadUnavailableError(); throwDownloadUnavailableError();
} }
return downloadId; return downloadId;
} }

View File

@ -157,7 +157,7 @@ QString KiwixApp::findLibraryDirectory()
return currentDataDir; return currentDataDir;
// Check for default dataDirectory. // Check for default dataDirectory.
currentDataDir = QString::fromStdString(kiwix::getDataDirectory()); currentDataDir = getDataDirectory();
libraryFile = QFileInfo(currentDataDir, "library.xml"); libraryFile = QFileInfo(currentDataDir, "library.xml");
if (libraryFile.exists()) if (libraryFile.exists())
return currentDataDir; return currentDataDir;

View File

@ -7,6 +7,16 @@
#include <QLocale> #include <QLocale>
#include <QList> #include <QList>
QString getDataDirectory()
{
QString dataDir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
if (!dataDir.isEmpty() && QDir().mkpath(dataDir))
return dataDir;
return QString::fromStdString(kiwix::getCurrentDirectory());
}
SettingsManager::SettingsManager(QObject *parent) SettingsManager::SettingsManager(QObject *parent)
: QObject(parent), : QObject(parent),
m_settings("Kiwix", "Kiwix-desktop"), m_settings("Kiwix", "Kiwix-desktop"),
@ -152,7 +162,7 @@ void SettingsManager::initSettings()
{ {
m_kiwixServerPort = m_settings.value("localKiwixServer/port", 8080).toInt(); m_kiwixServerPort = m_settings.value("localKiwixServer/port", 8080).toInt();
m_zoomFactor = m_settings.value("view/zoomFactor", 1).toDouble(); m_zoomFactor = m_settings.value("view/zoomFactor", 1).toDouble();
m_downloadDir = m_settings.value("download/dir", QString::fromStdString(kiwix::getDataDirectory())).toString(); m_downloadDir = m_settings.value("download/dir", getDataDirectory()).toString();
m_kiwixServerIpAddress = m_settings.value("localKiwixServer/ipAddress", QString("0.0.0.0")).toString(); m_kiwixServerIpAddress = m_settings.value("localKiwixServer/ipAddress", QString("0.0.0.0")).toString();
m_monitorDir = m_settings.value("monitor/dir", QString("")).toString(); m_monitorDir = m_settings.value("monitor/dir", QString("")).toString();
m_moveToTrash = m_settings.value("moveToTrash", true).toBool(); m_moveToTrash = m_settings.value("moveToTrash", true).toBool();

View File

@ -77,4 +77,5 @@ private:
QList<QVariant> m_contentTypeList; QList<QVariant> m_contentTypeList;
}; };
QString getDataDirectory();
#endif // SETTINGSMANAGER_H #endif // SETTINGSMANAGER_H

View File

@ -109,7 +109,7 @@ bool SettingsView::confirmDialogMonitorDir(const QString &dir) {
void SettingsView::resetDownloadDir() void SettingsView::resetDownloadDir()
{ {
auto dir = QString::fromStdString(kiwix::getDataDirectory()); auto dir = getDataDirectory();
const auto &downloadDir = KiwixApp::instance()->getSettingsManager()->getDownloadDir(); const auto &downloadDir = KiwixApp::instance()->getSettingsManager()->getDownloadDir();
if (dir == downloadDir) { if (dir == downloadDir) {
return; return;