mirror of
https://github.com/kiwix/kiwix-desktop.git
synced 2025-09-24 04:32:15 -04:00
Merge pull request #355 from kiwix/download-path
[WIP] Download path can be changed in the settings
This commit is contained in:
commit
ffdf7f2a20
@ -12,6 +12,13 @@ html, body {
|
|||||||
position: relative;
|
position: relative;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#header {
|
||||||
|
display: flex;
|
||||||
|
flex-direction: row;
|
||||||
|
flex-wrap: nowrap;
|
||||||
|
justify-content: space-between;
|
||||||
|
}
|
||||||
|
|
||||||
.row {
|
.row {
|
||||||
display: flex;
|
display: flex;
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
@ -22,8 +29,39 @@ html, body {
|
|||||||
height: 40px;
|
height: 40px;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#save-button {
|
||||||
|
color: black;
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 14px;
|
||||||
|
border: 1px solid black;
|
||||||
|
background: transparent;
|
||||||
|
border-radius: 2px;
|
||||||
|
height: 20px;
|
||||||
|
}
|
||||||
|
|
||||||
|
#save-button:hover {
|
||||||
|
color: white;
|
||||||
|
background: black;
|
||||||
|
}
|
||||||
|
|
||||||
input {
|
input {
|
||||||
width: 80px;
|
width: 80px;
|
||||||
|
margin: 5px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input[type=button] {
|
||||||
|
width: 65px;
|
||||||
|
color: blue;
|
||||||
|
font-weight: bold;
|
||||||
|
font-size: 14px;
|
||||||
|
border: 0;
|
||||||
|
background: transparent;
|
||||||
|
border-radius: 2px;
|
||||||
|
}
|
||||||
|
|
||||||
|
input[type=button]:hover {
|
||||||
|
color: white;
|
||||||
|
background: blue;
|
||||||
}
|
}
|
||||||
|
|
||||||
input[type=number]::-webkit-inner-spin-button,
|
input[type=number]::-webkit-inner-spin-button,
|
||||||
|
@ -1,3 +1,28 @@
|
|||||||
|
function onDownloadDirChanged (downloadDir) {
|
||||||
|
app.downloadDir = downloadDir;
|
||||||
|
}
|
||||||
|
|
||||||
|
function onSettingsChecked (valid) {
|
||||||
|
if (!valid) {
|
||||||
|
alert("Invalid download path");
|
||||||
|
app.downloadDir = settingsManager.downloadDir;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
settingsManager.setKiwixServerPort(app.kiwixServerPort);
|
||||||
|
app.zoomFactor = (app.zoomFactor < 30) ? 30 : app.zoomFactor;
|
||||||
|
app.zoomFactor = (app.zoomFactor > 500) ? 500 : app.zoomFactor;
|
||||||
|
settingsManager.setZoomFactor(app.zoomFactor / 100);
|
||||||
|
settingsManager.setDownloadDir(app.downloadDir);
|
||||||
|
}
|
||||||
|
|
||||||
|
function validPort (port) {
|
||||||
|
return /^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/.test(port);
|
||||||
|
}
|
||||||
|
|
||||||
|
function validDownloadDir (dir) {
|
||||||
|
settingsManager.validDownloadDir(dir);
|
||||||
|
}
|
||||||
|
|
||||||
function init() {
|
function init() {
|
||||||
new QWebChannel(qt.webChannelTransport, function(channel) {
|
new QWebChannel(qt.webChannelTransport, function(channel) {
|
||||||
settingsManager = channel.objects.settingsManager;
|
settingsManager = channel.objects.settingsManager;
|
||||||
@ -7,24 +32,26 @@ function init() {
|
|||||||
settingsManager: settingsManager,
|
settingsManager: settingsManager,
|
||||||
kiwixServerPort: settingsManager.kiwixServerPort,
|
kiwixServerPort: settingsManager.kiwixServerPort,
|
||||||
zoomFactor: Math.floor(settingsManager.zoomFactor * 100),
|
zoomFactor: Math.floor(settingsManager.zoomFactor * 100),
|
||||||
|
downloadDir: settingsManager.downloadDir,
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
setPort : function() {
|
saveSettings : function() {
|
||||||
// regex for valid port
|
if (!validPort(this.kiwixServerPort)) {
|
||||||
if (/^([0-9]{1,4}|[1-5][0-9]{4}|6[0-4][0-9]{3}|65[0-4][0-9]{2}|655[0-2][0-9]|6553[0-5])$/.test(this.kiwixServerPort))
|
alert("Invalid port");
|
||||||
{
|
|
||||||
settingsManager.setKiwixServerPort(this.kiwixServerPort);
|
|
||||||
} else {
|
|
||||||
alert("invalid port");
|
|
||||||
this.kiwixServerPort = settingsManager.kiwixServerPort;
|
this.kiwixServerPort = settingsManager.kiwixServerPort;
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
validDownloadDir(this.downloadDir);
|
||||||
},
|
},
|
||||||
setZoomFactor : function() {
|
resetDownloadDir : function() {
|
||||||
this.zoomFactor = (this.zoomFactor < 30) ? 30 : this.zoomFactor;
|
settingsManager.resetDownloadDir();
|
||||||
this.zoomFactor = (this.zoomFactor > 500) ? 500 : this.zoomFactor;
|
},
|
||||||
settingsManager.setZoomFactor(this.zoomFactor / 100);
|
browseDownloadDir : function() {
|
||||||
|
settingsManager.browseDownloadDir();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
settingsManager.downloadDirChanged.connect(onDownloadDirChanged)
|
||||||
|
settingsManager.settingsChecked.connect(onSettingsChecked)
|
||||||
});
|
});
|
||||||
}
|
}
|
@ -8,14 +8,23 @@
|
|||||||
</head>
|
</head>
|
||||||
<body onload="init()">
|
<body onload="init()">
|
||||||
<div id="settings">
|
<div id="settings">
|
||||||
<h1>Settings</h1>
|
<div id="header">
|
||||||
|
<h1>Settings</h1>
|
||||||
|
<input id="save-button" type="button" value="Apply" v-on:click="saveSettings()">
|
||||||
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<label>Port for local Kiwix server : </label>
|
<label>Port for local Kiwix server : </label>
|
||||||
<input type="number" min="1" max="65535" v-model="kiwixServerPort" v-on:change="setPort()">
|
<input type="number" min="1" max="65535" v-model="kiwixServerPort" >
|
||||||
</div>
|
</div>
|
||||||
<div class="row">
|
<div class="row">
|
||||||
<label>Zoom level : </label>
|
<label>Zoom level : </label>
|
||||||
<div class="percentage-symbol"><input type="number" step="10" min="30" max="500" v-model="zoomFactor" v-on:change="setZoomFactor()"></div>
|
<div class="percentage-symbol"><input type="number" step="10" min="30" max="500" v-model="zoomFactor" ></div>
|
||||||
|
</div>
|
||||||
|
<div class="row">
|
||||||
|
<label>Download directory : </label>
|
||||||
|
<input style="flex: 1;" type="text" v-model="downloadDir" >
|
||||||
|
<input type="button" value="Reset" v-on:click="resetDownloadDir()">
|
||||||
|
<input type="button" value="Browse" v-on:click="browseDownloadDir()">
|
||||||
</div>
|
</div>
|
||||||
</div>
|
</div>
|
||||||
</body></html>
|
</body></html>
|
||||||
|
@ -203,7 +203,10 @@ QString ContentManager::downloadBook(const QString &id)
|
|||||||
return "";
|
return "";
|
||||||
kiwix::Download *download;
|
kiwix::Download *download;
|
||||||
try {
|
try {
|
||||||
download = mp_downloader->startDownload(book.getUrl());
|
auto downloadPath = KiwixApp::instance()->getSettingsManager()->getDownloadDir();
|
||||||
|
std::pair<std::string, std::string> downloadDir("dir", downloadPath.toStdString());
|
||||||
|
const std::vector<std::pair<std::string, std::string>> options = { downloadDir };
|
||||||
|
download = mp_downloader->startDownload(book.getUrl(), options);
|
||||||
} catch (std::exception& e) {
|
} catch (std::exception& e) {
|
||||||
return "";
|
return "";
|
||||||
}
|
}
|
||||||
@ -214,12 +217,11 @@ QString ContentManager::downloadBook(const QString &id)
|
|||||||
return QString::fromStdString(download->getDid());
|
return QString::fromStdString(download->getDid());
|
||||||
}
|
}
|
||||||
|
|
||||||
void ContentManager::eraseBookFilesFromComputer(const QString fileToRemove)
|
void ContentManager::eraseBookFilesFromComputer(const QString dirPath, const QString filename)
|
||||||
{
|
{
|
||||||
QString dirName = KiwixApp::instance()->getLibraryDirectory();
|
QDir dir(dirPath, filename);
|
||||||
QDir dir(dirName, fileToRemove);
|
for(const QString& file: dir.entryList()) {
|
||||||
for(const QString& filename: dir.entryList()) {
|
dir.remove(file);
|
||||||
dir.remove(filename);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -236,8 +238,9 @@ void ContentManager::eraseBook(const QString& id)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
kiwix::Book book = mp_library->getBookById(id);
|
kiwix::Book book = mp_library->getBookById(id);
|
||||||
QString fileToRemove = QString::fromUtf8(getLastPathElement(book.getPath()).c_str()) + "*";
|
QString dirPath = QString::fromStdString(removeLastPathElement(book.getPath()));
|
||||||
eraseBookFilesFromComputer(fileToRemove);
|
QString filename = QString::fromStdString(getLastPathElement(book.getPath())) + "*";
|
||||||
|
eraseBookFilesFromComputer(dirPath, filename);
|
||||||
mp_library->removeBookFromLibraryById(id);
|
mp_library->removeBookFromLibraryById(id);
|
||||||
mp_library->save();
|
mp_library->save();
|
||||||
emit mp_library->bookmarksChanged();
|
emit mp_library->bookmarksChanged();
|
||||||
@ -281,8 +284,9 @@ void ContentManager::cancelBook(const QString& id)
|
|||||||
if (download->getStatus() != kiwix::Download::K_COMPLETE) {
|
if (download->getStatus() != kiwix::Download::K_COMPLETE) {
|
||||||
download->cancelDownload();
|
download->cancelDownload();
|
||||||
}
|
}
|
||||||
QString fileToRemove = QString::fromUtf8(getLastPathElement(download->getPath()).c_str()) + "*";
|
QString dirPath = QString::fromStdString(removeLastPathElement(download->getPath()));
|
||||||
eraseBookFilesFromComputer(fileToRemove);
|
QString filename = QString::fromStdString(getLastPathElement(download->getPath())) + "*";
|
||||||
|
eraseBookFilesFromComputer(dirPath, filename);
|
||||||
mp_library->removeBookFromLibraryById(id);
|
mp_library->removeBookFromLibraryById(id);
|
||||||
mp_library->save();
|
mp_library->save();
|
||||||
emit(oneBookChanged(id));
|
emit(oneBookChanged(id));
|
||||||
|
@ -40,7 +40,7 @@ private:
|
|||||||
bool m_sortOrderAsc = true;
|
bool m_sortOrderAsc = true;
|
||||||
|
|
||||||
QStringList getBookIds();
|
QStringList getBookIds();
|
||||||
void eraseBookFilesFromComputer(const QString fileSelection);
|
void eraseBookFilesFromComputer(const QString dirPath, const QString filename);
|
||||||
|
|
||||||
signals:
|
signals:
|
||||||
void filterParamsChanged();
|
void filterParamsChanged();
|
||||||
|
@ -1,4 +1,8 @@
|
|||||||
#include "settingsmanager.h"
|
#include "settingsmanager.h"
|
||||||
|
#include "kiwix/tools/pathTools.h"
|
||||||
|
#include "kiwixapp.h"
|
||||||
|
#include <QDir>
|
||||||
|
#include <QFileDialog>
|
||||||
|
|
||||||
SettingsManager::SettingsManager(QObject *parent)
|
SettingsManager::SettingsManager(QObject *parent)
|
||||||
: QObject(parent),
|
: QObject(parent),
|
||||||
@ -61,8 +65,35 @@ void SettingsManager::setZoomFactor(qreal zoomFactor)
|
|||||||
m_settings.setValue("view/zoomFactor", zoomFactor);
|
m_settings.setValue("view/zoomFactor", zoomFactor);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void SettingsManager::validDownloadDir(QString dir)
|
||||||
|
{
|
||||||
|
emit(settingsChecked(fileExists(dir.toStdString())));
|
||||||
|
}
|
||||||
|
|
||||||
|
bool SettingsManager::setDownloadDir(QString downloadDir)
|
||||||
|
{
|
||||||
|
m_downloadDir = downloadDir;
|
||||||
|
m_settings.setValue("download/dir", downloadDir);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void SettingsManager::resetDownloadDir()
|
||||||
|
{
|
||||||
|
emit(downloadDirChanged(QString::fromStdString(getDataDirectory())));
|
||||||
|
}
|
||||||
|
|
||||||
|
void SettingsManager::browseDownloadDir()
|
||||||
|
{
|
||||||
|
QString dir = QFileDialog::getExistingDirectory(KiwixApp::instance()->getMainWindow(),
|
||||||
|
tr("Browse Directory"),
|
||||||
|
QString(),
|
||||||
|
QFileDialog::ShowDirsOnly);
|
||||||
|
emit(downloadDirChanged(dir));
|
||||||
|
}
|
||||||
|
|
||||||
void SettingsManager::initSettings()
|
void SettingsManager::initSettings()
|
||||||
{
|
{
|
||||||
m_kiwixServerPort = m_settings.value("localKiwixServer/port", 8181).toInt();
|
m_kiwixServerPort = m_settings.value("localKiwixServer/port", 8181).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(getDataDirectory())).toString();
|
||||||
}
|
}
|
@ -10,6 +10,7 @@ class SettingsManager : public QObject
|
|||||||
Q_OBJECT
|
Q_OBJECT
|
||||||
Q_PROPERTY(int kiwixServerPort READ getKiwixServerPort NOTIFY portChanged)
|
Q_PROPERTY(int kiwixServerPort READ getKiwixServerPort NOTIFY portChanged)
|
||||||
Q_PROPERTY(qreal zoomFactor READ getZoomFactor NOTIFY zoomChanged)
|
Q_PROPERTY(qreal zoomFactor READ getZoomFactor NOTIFY zoomChanged)
|
||||||
|
Q_PROPERTY(QString downloadDir READ getDownloadDir NOTIFY downloadDirChanged)
|
||||||
|
|
||||||
public:
|
public:
|
||||||
explicit SettingsManager(QObject *parent = nullptr);
|
explicit SettingsManager(QObject *parent = nullptr);
|
||||||
@ -28,6 +29,11 @@ public slots:
|
|||||||
int getKiwixServerPort() { return m_kiwixServerPort; };
|
int getKiwixServerPort() { return m_kiwixServerPort; };
|
||||||
void setZoomFactor(qreal zoomFactor);
|
void setZoomFactor(qreal zoomFactor);
|
||||||
qreal getZoomFactor() { return m_zoomFactor; };
|
qreal getZoomFactor() { return m_zoomFactor; };
|
||||||
|
void validDownloadDir(QString dir);
|
||||||
|
bool setDownloadDir(QString downloadDir);
|
||||||
|
QString getDownloadDir() { return m_downloadDir; }
|
||||||
|
void resetDownloadDir();
|
||||||
|
void browseDownloadDir();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
void initSettings();
|
void initSettings();
|
||||||
@ -35,12 +41,15 @@ private:
|
|||||||
signals:
|
signals:
|
||||||
void portChanged(int port);
|
void portChanged(int port);
|
||||||
void zoomChanged(qreal zoomFactor);
|
void zoomChanged(qreal zoomFactor);
|
||||||
|
void downloadDirChanged(QString downloadDir);
|
||||||
|
void settingsChecked(bool valid);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QSettings m_settings;
|
QSettings m_settings;
|
||||||
bool m_settingsViewDisplayed;
|
bool m_settingsViewDisplayed;
|
||||||
int m_kiwixServerPort;
|
int m_kiwixServerPort;
|
||||||
qreal m_zoomFactor;
|
qreal m_zoomFactor;
|
||||||
|
QString m_downloadDir;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SETTINGSMANAGER_H
|
#endif // SETTINGSMANAGER_H
|
||||||
|
Loading…
x
Reference in New Issue
Block a user