Merge pull request #355 from kiwix/download-path

[WIP] Download path can be changed in the settings
This commit is contained in:
Matthieu Gautier 2020-03-03 15:31:33 +01:00 committed by GitHub
commit ffdf7f2a20
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 143 additions and 25 deletions

View File

@ -12,6 +12,13 @@ html, body {
position: relative;
}
#header {
display: flex;
flex-direction: row;
flex-wrap: nowrap;
justify-content: space-between;
}
.row {
display: flex;
flex-direction: row;
@ -22,8 +29,39 @@ html, body {
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 {
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,

View File

@ -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() {
new QWebChannel(qt.webChannelTransport, function(channel) {
settingsManager = channel.objects.settingsManager;
@ -7,24 +32,26 @@ function init() {
settingsManager: settingsManager,
kiwixServerPort: settingsManager.kiwixServerPort,
zoomFactor: Math.floor(settingsManager.zoomFactor * 100),
downloadDir: settingsManager.downloadDir,
},
methods: {
setPort : function() {
// regex for valid port
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))
{
settingsManager.setKiwixServerPort(this.kiwixServerPort);
} else {
alert("invalid port");
saveSettings : function() {
if (!validPort(this.kiwixServerPort)) {
alert("Invalid port");
this.kiwixServerPort = settingsManager.kiwixServerPort;
return;
}
validDownloadDir(this.downloadDir);
},
setZoomFactor : function() {
this.zoomFactor = (this.zoomFactor < 30) ? 30 : this.zoomFactor;
this.zoomFactor = (this.zoomFactor > 500) ? 500 : this.zoomFactor;
settingsManager.setZoomFactor(this.zoomFactor / 100);
resetDownloadDir : function() {
settingsManager.resetDownloadDir();
},
browseDownloadDir : function() {
settingsManager.browseDownloadDir();
}
}
});
settingsManager.downloadDirChanged.connect(onDownloadDirChanged)
settingsManager.settingsChecked.connect(onSettingsChecked)
});
}

View File

@ -8,14 +8,23 @@
</head>
<body onload="init()">
<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">
<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 class="row">
<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>
</body></html>

View File

@ -203,7 +203,10 @@ QString ContentManager::downloadBook(const QString &id)
return "";
kiwix::Download *download;
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) {
return "";
}
@ -214,12 +217,11 @@ QString ContentManager::downloadBook(const QString &id)
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(dirName, fileToRemove);
for(const QString& filename: dir.entryList()) {
dir.remove(filename);
QDir dir(dirPath, filename);
for(const QString& file: dir.entryList()) {
dir.remove(file);
}
}
@ -236,8 +238,9 @@ void ContentManager::eraseBook(const QString& id)
}
}
kiwix::Book book = mp_library->getBookById(id);
QString fileToRemove = QString::fromUtf8(getLastPathElement(book.getPath()).c_str()) + "*";
eraseBookFilesFromComputer(fileToRemove);
QString dirPath = QString::fromStdString(removeLastPathElement(book.getPath()));
QString filename = QString::fromStdString(getLastPathElement(book.getPath())) + "*";
eraseBookFilesFromComputer(dirPath, filename);
mp_library->removeBookFromLibraryById(id);
mp_library->save();
emit mp_library->bookmarksChanged();
@ -281,8 +284,9 @@ void ContentManager::cancelBook(const QString& id)
if (download->getStatus() != kiwix::Download::K_COMPLETE) {
download->cancelDownload();
}
QString fileToRemove = QString::fromUtf8(getLastPathElement(download->getPath()).c_str()) + "*";
eraseBookFilesFromComputer(fileToRemove);
QString dirPath = QString::fromStdString(removeLastPathElement(download->getPath()));
QString filename = QString::fromStdString(getLastPathElement(download->getPath())) + "*";
eraseBookFilesFromComputer(dirPath, filename);
mp_library->removeBookFromLibraryById(id);
mp_library->save();
emit(oneBookChanged(id));

View File

@ -40,7 +40,7 @@ private:
bool m_sortOrderAsc = true;
QStringList getBookIds();
void eraseBookFilesFromComputer(const QString fileSelection);
void eraseBookFilesFromComputer(const QString dirPath, const QString filename);
signals:
void filterParamsChanged();

View File

@ -1,4 +1,8 @@
#include "settingsmanager.h"
#include "kiwix/tools/pathTools.h"
#include "kiwixapp.h"
#include <QDir>
#include <QFileDialog>
SettingsManager::SettingsManager(QObject *parent)
: QObject(parent),
@ -61,8 +65,35 @@ void SettingsManager::setZoomFactor(qreal 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()
{
m_kiwixServerPort = m_settings.value("localKiwixServer/port", 8181).toInt();
m_zoomFactor = m_settings.value("view/zoomFactor", 1).toDouble();
m_downloadDir = m_settings.value("download/dir", QString::fromStdString(getDataDirectory())).toString();
}

View File

@ -10,6 +10,7 @@ class SettingsManager : public QObject
Q_OBJECT
Q_PROPERTY(int kiwixServerPort READ getKiwixServerPort NOTIFY portChanged)
Q_PROPERTY(qreal zoomFactor READ getZoomFactor NOTIFY zoomChanged)
Q_PROPERTY(QString downloadDir READ getDownloadDir NOTIFY downloadDirChanged)
public:
explicit SettingsManager(QObject *parent = nullptr);
@ -28,6 +29,11 @@ public slots:
int getKiwixServerPort() { return m_kiwixServerPort; };
void setZoomFactor(qreal zoomFactor);
qreal getZoomFactor() { return m_zoomFactor; };
void validDownloadDir(QString dir);
bool setDownloadDir(QString downloadDir);
QString getDownloadDir() { return m_downloadDir; }
void resetDownloadDir();
void browseDownloadDir();
private:
void initSettings();
@ -35,12 +41,15 @@ private:
signals:
void portChanged(int port);
void zoomChanged(qreal zoomFactor);
void downloadDirChanged(QString downloadDir);
void settingsChecked(bool valid);
private:
QSettings m_settings;
bool m_settingsViewDisplayed;
int m_kiwixServerPort;
qreal m_zoomFactor;
QString m_downloadDir;
};
#endif // SETTINGSMANAGER_H