From 8a087a876e0f35ba40fa4af2f1bb29b60b5e6930 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 31 Mar 2021 00:10:52 +0800 Subject: [PATCH] Clean up code --- src/base/filesystemwatcher.cpp | 6 +++- src/base/filesystemwatcher.h | 6 ++-- src/webui/api/appcontroller.cpp | 33 +++++++------------- src/webui/www/private/views/preferences.html | 16 +++++----- 4 files changed, 28 insertions(+), 33 deletions(-) diff --git a/src/base/filesystemwatcher.cpp b/src/base/filesystemwatcher.cpp index fd1b19fff..ccc6bd1d9 100644 --- a/src/base/filesystemwatcher.cpp +++ b/src/base/filesystemwatcher.cpp @@ -30,6 +30,8 @@ #include +#include + #if defined(Q_OS_MACOS) || defined(Q_OS_FREEBSD) || defined(Q_OS_OPENBSD) #include #include @@ -42,9 +44,11 @@ #include "base/logger.h" #include "base/utils/fs.h" +using namespace std::chrono_literals; + namespace { - const int WATCH_INTERVAL = 10000; // 10 sec + const std::chrono::duration WATCH_INTERVAL = 10s; const int MAX_PARTIAL_RETRIES = 5; } diff --git a/src/base/filesystemwatcher.h b/src/base/filesystemwatcher.h index 56d5f6dd6..2f946e0a8 100644 --- a/src/base/filesystemwatcher.h +++ b/src/base/filesystemwatcher.h @@ -31,7 +31,6 @@ #include #include #include -#include #include #include @@ -39,9 +38,10 @@ * Subclassing QFileSystemWatcher in order to support Network File * System watching (NFS, CIFS) on Linux and Mac OS. */ -class FileSystemWatcher : public QFileSystemWatcher +class FileSystemWatcher final : public QFileSystemWatcher { Q_OBJECT + Q_DISABLE_COPY(FileSystemWatcher) public: explicit FileSystemWatcher(QObject *parent = nullptr); @@ -53,7 +53,7 @@ public: signals: void torrentsAdded(const QStringList &pathList); -protected slots: +private slots: void scanLocalFolder(const QString &path); void processPartialTorrents(); void scanNetworkFolders(); diff --git a/src/webui/api/appcontroller.cpp b/src/webui/api/appcontroller.cpp index 0f82313c9..3a3c01b04 100644 --- a/src/webui/api/appcontroller.cpp +++ b/src/webui/api/appcontroller.cpp @@ -394,15 +394,14 @@ void AppController::setPreferencesAction() if (hasKey("scan_dirs")) { const QVariantHash nativeDirs = it.value().toHash(); - QVariantHash oldScanDirs = pref->getScanDirs(); + const QVariantHash oldScanDirs = pref->getScanDirs(); QVariantHash scanDirs; ScanFoldersModel *model = ScanFoldersModel::instance(); + for (auto i = nativeDirs.cbegin(); i != nativeDirs.cend(); ++i) { - QString folder = Utils::Fs::toUniformPath(i.key()); - int downloadType; + int downloadType = 0; QString downloadPath; - ScanFoldersModel::PathStatus ec; if (i.value().type() == QVariant::String) { downloadType = ScanFoldersModel::CUSTOM_LOCATION; @@ -411,23 +410,17 @@ void AppController::setPreferencesAction() else { downloadType = i.value().toInt(); - downloadPath = (downloadType == ScanFoldersModel::DEFAULT_LOCATION) ? "Default folder" : "Watch folder"; + downloadPath = (downloadType == ScanFoldersModel::DEFAULT_LOCATION) + ? QLatin1String("Default folder") + : QLatin1String("Watch folder"); } - if (!oldScanDirs.contains(folder)) - ec = model->addPath(folder, static_cast(downloadType), downloadPath); - else - ec = model->updatePath(folder, static_cast(downloadType), downloadPath); - + const QString folder = Utils::Fs::toUniformPath(i.key()); + const ScanFoldersModel::PathStatus ec = !oldScanDirs.contains(folder) + ? model->addPath(folder, static_cast(downloadType), downloadPath) + : model->updatePath(folder, static_cast(downloadType), downloadPath); if (ec == ScanFoldersModel::Ok) - { - scanDirs.insert(folder, (downloadType == ScanFoldersModel::CUSTOM_LOCATION) ? QVariant(downloadPath) : QVariant(downloadType)); - qDebug("New watched folder: %s to %s", qUtf8Printable(folder), qUtf8Printable(downloadPath)); - } - else - { - qDebug("Watched folder %s failed with error %d", qUtf8Printable(folder), ec); - } + scanDirs.insert(folder, ((downloadType == ScanFoldersModel::CUSTOM_LOCATION) ? QVariant(downloadPath) : QVariant(downloadType))); } // Update deleted folders @@ -435,11 +428,9 @@ void AppController::setPreferencesAction() { const QString &folder = i.key(); if (!scanDirs.contains(folder)) - { model->removePath(folder); - qDebug("Removed watched folder %s", qUtf8Printable(folder)); - } } + pref->setScanDirs(scanDirs); } // Email notification upon download completion diff --git a/src/webui/www/private/views/preferences.html b/src/webui/www/private/views/preferences.html index a400472bf..f32ca3f2f 100644 --- a/src/webui/www/private/views/preferences.html +++ b/src/webui/www/private/views/preferences.html @@ -1283,7 +1283,7 @@ }; // Downloads tab - const WatchedFoldersTable = new HtmlTable($("watched_folders_tab")); + const watchedFoldersTable = new HtmlTable($("watched_folders_tab")); const updateTempDirEnabled = function() { const isTempDirEnabled = $('temppath_checkbox').getProperty('checked'); @@ -1334,7 +1334,7 @@ + "" + ""; - WatchedFoldersTable.push([myinput, mycb]); + watchedFoldersTable.push([myinput, mycb]); $('cb_watch_' + pos).setProperty('value', sel); if (disableInput) { const elt = $('cb_watch_' + pos); @@ -1349,14 +1349,14 @@ for (let i = 0; i < nb_folders; ++i) { const fpath = $('text_watch_' + i).getProperty('value').trim(); if (fpath.length > 0) { - let other; const sel = $('cb_watch_' + i).getProperty('value').trim(); - if (sel == "other") { + + let other; + if (sel == "other") other = $('cb_watch_txt_' + i).getProperty('value').trim(); - } - else { - other = (sel == "watch_folder") ? 0 : 1; - } + else + other = (sel === "watch_folder") ? 0 : 1; + folders.set(fpath, other); } }