Improve "Watched folders" feature

Make "file system watcher" an application core component
and separate it from its presentation model.
This commit is contained in:
Vladimir Golovnev (Glassez)
2021-04-23 12:02:25 +03:00
committed by Vladimir Golovnev
parent baa32a20e0
commit d957eef331
30 changed files with 1650 additions and 1020 deletions

View File

@@ -46,7 +46,6 @@ add_library(qbt_gui STATIC
rss/feedlistwidget.h
rss/htmlbrowser.h
rss/rsswidget.h
scanfoldersdelegate.h
search/pluginselectdialog.h
search/pluginsourcedialog.h
search/searchjobwidget.h
@@ -78,6 +77,8 @@ add_library(qbt_gui STATIC
tristatewidget.h
uithememanager.h
utils.h
watchedfolderoptionsdialog.h
watchedfoldersmodel.h
# sources
aboutdialog.cpp
@@ -126,7 +127,6 @@ add_library(qbt_gui STATIC
rss/feedlistwidget.cpp
rss/htmlbrowser.cpp
rss/rsswidget.cpp
scanfoldersdelegate.cpp
search/pluginselectdialog.cpp
search/pluginsourcedialog.cpp
search/searchjobwidget.cpp
@@ -158,6 +158,8 @@ add_library(qbt_gui STATIC
tristatewidget.cpp
uithememanager.cpp
utils.cpp
watchedfolderoptionsdialog.cpp
watchedfoldersmodel.cpp
# forms
aboutdialog.ui
@@ -188,6 +190,7 @@ add_library(qbt_gui STATIC
torrentcreatordialog.ui
torrentoptionsdialog.ui
trackerentriesdialog.ui
watchedfolderoptionsdialog.ui
)
target_sources(qbt_gui INTERFACE about.qrc)

View File

@@ -47,7 +47,6 @@ HEADERS += \
$$PWD/rss/feedlistwidget.h \
$$PWD/rss/htmlbrowser.h \
$$PWD/rss/rsswidget.h \
$$PWD/scanfoldersdelegate.h \
$$PWD/search/pluginselectdialog.h \
$$PWD/search/pluginsourcedialog.h \
$$PWD/search/searchjobwidget.h \
@@ -78,7 +77,9 @@ HEADERS += \
$$PWD/tristateaction.h \
$$PWD/tristatewidget.h \
$$PWD/uithememanager.h \
$$PWD/utils.h
$$PWD/utils.h \
$$PWD/watchedfolderoptionsdialog.h \
$$PWD/watchedfoldersmodel.h
SOURCES += \
$$PWD/aboutdialog.cpp \
@@ -127,7 +128,6 @@ SOURCES += \
$$PWD/rss/feedlistwidget.cpp \
$$PWD/rss/htmlbrowser.cpp \
$$PWD/rss/rsswidget.cpp \
$$PWD/scanfoldersdelegate.cpp \
$$PWD/search/pluginselectdialog.cpp \
$$PWD/search/pluginsourcedialog.cpp \
$$PWD/search/searchjobwidget.cpp \
@@ -158,7 +158,9 @@ SOURCES += \
$$PWD/tristateaction.cpp \
$$PWD/tristatewidget.cpp \
$$PWD/uithememanager.cpp \
$$PWD/utils.cpp
$$PWD/utils.cpp \
$$PWD/watchedfolderoptionsdialog.cpp \
$$PWD/watchedfoldersmodel.cpp
win32|macx {
HEADERS += $$PWD/programupdater.h
@@ -208,6 +210,7 @@ FORMS += \
$$PWD/torrentcategorydialog.ui \
$$PWD/torrentcreatordialog.ui \
$$PWD/torrentoptionsdialog.ui \
$$PWD/trackerentriesdialog.ui
$$PWD/trackerentriesdialog.ui \
$$PWD/watchedfolderoptionsdialog.ui
RESOURCES += $$PWD/about.qrc

View File

@@ -43,6 +43,7 @@
#include <QTranslator>
#include "base/bittorrent/session.h"
#include "base/exceptions.h"
#include "base/global.h"
#include "base/net/dnsupdater.h"
#include "base/net/portforwarder.h"
@@ -50,8 +51,8 @@
#include "base/preferences.h"
#include "base/rss/rss_autodownloader.h"
#include "base/rss/rss_session.h"
#include "base/scanfoldersmodel.h"
#include "base/torrentfileguard.h"
#include "base/torrentfileswatcher.h"
#include "base/unicodestrings.h"
#include "base/utils/fs.h"
#include "base/utils/net.h"
@@ -63,10 +64,11 @@
#include "banlistoptionsdialog.h"
#include "ipsubnetwhitelistoptionsdialog.h"
#include "rss/automatedrssdownloader.h"
#include "scanfoldersdelegate.h"
#include "ui_optionsdialog.h"
#include "uithememanager.h"
#include "utils.h"
#include "watchedfolderoptionsdialog.h"
#include "watchedfoldersmodel.h"
#define SETTINGS_KEY(name) "OptionsDialog/" name
@@ -236,11 +238,12 @@ OptionsDialog::OptionsDialog(QWidget *parent)
m_applyButton = m_ui->buttonBox->button(QDialogButtonBox::Apply);
connect(m_applyButton, &QPushButton::clicked, this, &OptionsDialog::applySettings);
auto watchedFoldersModel = new WatchedFoldersModel(TorrentFilesWatcher::instance(), this);
connect(watchedFoldersModel, &QAbstractListModel::dataChanged, this, &ThisType::enableApplyButton);
m_ui->scanFoldersView->header()->setSectionResizeMode(QHeaderView::ResizeToContents);
m_ui->scanFoldersView->setModel(ScanFoldersModel::instance());
m_ui->scanFoldersView->setItemDelegate(new ScanFoldersDelegate(this, m_ui->scanFoldersView));
connect(ScanFoldersModel::instance(), &QAbstractListModel::dataChanged, this, &ThisType::enableApplyButton);
connect(m_ui->scanFoldersView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ThisType::handleScanFolderViewSelectionChanged);
m_ui->scanFoldersView->setModel(watchedFoldersModel);
connect(m_ui->scanFoldersView->selectionModel(), &QItemSelectionModel::selectionChanged, this, &ThisType::handleWatchedFolderViewSelectionChanged);
connect(m_ui->scanFoldersView, &QTreeView::doubleClicked, this, &ThisType::editWatchedFolderOptions);
// Languages supported
initializeLanguageCombo();
@@ -368,8 +371,8 @@ OptionsDialog::OptionsDialog(QWidget *parent)
connect(m_ui->actionTorrentFnOnDblClBox, qComboBoxCurrentIndexChanged, this, &ThisType::enableApplyButton);
connect(m_ui->checkTempFolder, &QAbstractButton::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->checkTempFolder, &QAbstractButton::toggled, m_ui->textTempPath, &QWidget::setEnabled);
connect(m_ui->addScanFolderButton, &QAbstractButton::clicked, this, &ThisType::enableApplyButton);
connect(m_ui->removeScanFolderButton, &QAbstractButton::clicked, this, &ThisType::enableApplyButton);
connect(m_ui->addWatchedFolderButton, &QAbstractButton::clicked, this, &ThisType::enableApplyButton);
connect(m_ui->removeWatchedFolderButton, &QAbstractButton::clicked, this, &ThisType::enableApplyButton);
connect(m_ui->groupMailNotification, &QGroupBox::toggled, this, &ThisType::enableApplyButton);
connect(m_ui->senderEmailTxt, &QLineEdit::textChanged, this, &ThisType::enableApplyButton);
connect(m_ui->lineEditDestEmail, &QLineEdit::textChanged, this, &ThisType::enableApplyButton);
@@ -609,9 +612,6 @@ OptionsDialog::~OptionsDialog()
hSplitterSizes.append(QString::number(size));
m_storeHSplitterSize = hSplitterSizes;
for (const QString &path : asConst(m_addedScanDirs))
ScanFoldersModel::instance()->removePath(path);
ScanFoldersModel::instance()->configure(); // reloads "removed" paths
delete m_ui;
}
@@ -736,11 +736,8 @@ void OptionsDialog::saveOptions()
AddNewTorrentDialog::setTopLevel(m_ui->checkAdditionDialogFront->isChecked());
session->setAddTorrentPaused(addTorrentsInPause());
session->setTorrentContentLayout(static_cast<BitTorrent::TorrentContentLayout>(m_ui->contentLayoutComboBox->currentIndex()));
ScanFoldersModel::instance()->removeFromFSWatcher(m_removedScanDirs);
ScanFoldersModel::instance()->addToFSWatcher(m_addedScanDirs);
ScanFoldersModel::instance()->makePersistent();
m_removedScanDirs.clear();
m_addedScanDirs.clear();
auto watchedFoldersModel = static_cast<WatchedFoldersModel *>(m_ui->scanFoldersView->model());
watchedFoldersModel->apply();
session->setTorrentExportDirectory(getTorrentExportDir());
session->setFinishedTorrentExportDirectory(getFinishedTorrentExportDir());
pref->setMailNotificationEnabled(m_ui->groupMailNotification->isChecked());
@@ -1648,57 +1645,85 @@ int OptionsDialog::getActionOnDblClOnTorrentFn() const
return m_ui->actionTorrentFnOnDblClBox->currentIndex();
}
void OptionsDialog::on_addScanFolderButton_clicked()
void OptionsDialog::on_addWatchedFolderButton_clicked()
{
Preferences *const pref = Preferences::instance();
const QString dir = QFileDialog::getExistingDirectory(this, tr("Select folder to monitor"),
Utils::Fs::toNativePath(Utils::Fs::folderName(pref->getScanDirsLastPath())));
if (!dir.isEmpty())
Utils::Fs::toNativePath(Utils::Fs::folderName(pref->getScanDirsLastPath())));
if (dir.isEmpty())
return;
auto dialog = new WatchedFolderOptionsDialog({}, this);
dialog->setModal(true);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &QDialog::accepted, this, [this, dialog, dir, pref]()
{
const ScanFoldersModel::PathStatus status = ScanFoldersModel::instance()->addPath(dir, ScanFoldersModel::DEFAULT_LOCATION, QString(), false);
QString error;
switch (status)
try
{
case ScanFoldersModel::AlreadyInList:
error = tr("Folder is already being monitored:");
break;
case ScanFoldersModel::DoesNotExist:
error = tr("Folder does not exist:");
break;
case ScanFoldersModel::CannotRead:
error = tr("Folder is not readable:");
break;
default:
auto watchedFoldersModel = static_cast<WatchedFoldersModel *>(m_ui->scanFoldersView->model());
watchedFoldersModel->addFolder(dir, dialog->watchedFolderOptions());
pref->setScanDirsLastPath(dir);
m_addedScanDirs << dir;
for (int i = 0; i < ScanFoldersModel::instance()->columnCount(); ++i)
for (int i = 0; i < watchedFoldersModel->columnCount(); ++i)
m_ui->scanFoldersView->resizeColumnToContents(i);
enableApplyButton();
}
catch (const RuntimeError &err)
{
QMessageBox::critical(this, tr("Adding entry failed"), err.message());
}
});
if (!error.isEmpty())
QMessageBox::critical(this, tr("Adding entry failed"), QString::fromLatin1("%1\n%2").arg(error, dir));
}
dialog->open();
}
void OptionsDialog::on_removeScanFolderButton_clicked()
void OptionsDialog::on_editWatchedFolderButton_clicked()
{
const QModelIndex selected
= m_ui->scanFoldersView->selectionModel()->selectedIndexes().at(0);
editWatchedFolderOptions(selected);
}
void OptionsDialog::on_removeWatchedFolderButton_clicked()
{
const QModelIndexList selected
= m_ui->scanFoldersView->selectionModel()->selectedIndexes();
if (selected.isEmpty())
return;
Q_ASSERT(selected.count() == ScanFoldersModel::instance()->columnCount());
for (const QModelIndex &index : selected)
{
if (index.column() == ScanFoldersModel::WATCH)
m_removedScanDirs << index.data().toString();
}
ScanFoldersModel::instance()->removePath(selected.first().row(), false);
m_ui->scanFoldersView->model()->removeRow(index.row());
}
void OptionsDialog::handleScanFolderViewSelectionChanged()
void OptionsDialog::handleWatchedFolderViewSelectionChanged()
{
m_ui->removeScanFolderButton->setEnabled(!m_ui->scanFoldersView->selectionModel()->selectedIndexes().isEmpty());
const QModelIndexList selectedIndexes = m_ui->scanFoldersView->selectionModel()->selectedIndexes();
m_ui->removeWatchedFolderButton->setEnabled(!selectedIndexes.isEmpty());
m_ui->editWatchedFolderButton->setEnabled(selectedIndexes.count() == 1);
}
void OptionsDialog::editWatchedFolderOptions(const QModelIndex &index)
{
if (!index.isValid())
return;
auto watchedFoldersModel = static_cast<WatchedFoldersModel *>(m_ui->scanFoldersView->model());
auto dialog = new WatchedFolderOptionsDialog(watchedFoldersModel->folderOptions(index.row()), this);
dialog->setModal(true);
dialog->setAttribute(Qt::WA_DeleteOnClose);
connect(dialog, &QDialog::accepted, this, [this, dialog, index, watchedFoldersModel]()
{
if (index.isValid())
{
// The index could be invalidated while the dialog was displayed,
// for example, if you deleted the folder using the Web API.
watchedFoldersModel->setFolderOptions(index.row(), dialog->watchedFolderOptions());
enableApplyButton();
}
});
dialog->open();
}
QString OptionsDialog::askForExportDir(const QString &currentExportPath)

View File

@@ -99,14 +99,16 @@ private slots:
void toggleComboRatioLimitAct();
void changePage(QListWidgetItem *, QListWidgetItem *);
void loadSplitterState();
void handleScanFolderViewSelectionChanged();
void handleWatchedFolderViewSelectionChanged();
void editWatchedFolderOptions(const QModelIndex &index);
void on_IpFilterRefreshBtn_clicked();
void handleIPFilterParsed(bool error, int ruleCount);
void on_banListButton_clicked();
void on_IPSubnetWhitelistButton_clicked();
void on_randomButton_clicked();
void on_addScanFolderButton_clicked();
void on_removeScanFolderButton_clicked();
void on_addWatchedFolderButton_clicked();
void on_editWatchedFolderButton_clicked();
void on_removeWatchedFolderButton_clicked();
void on_registerDNSBtn_clicked();
void setLocale(const QString &localeStr);
void webUIHttpsCertChanged(const QString &path, ShowError showError);
@@ -184,8 +186,5 @@ private:
AdvancedSettings *m_advancedSettings;
QList<QString> m_addedScanDirs;
QList<QString> m_removedScanDirs;
bool m_refreshingIpFilter = false;
};

View File

@@ -1183,19 +1183,29 @@ Manual: Various torrent properties (e.g. save path) must be assigned manually</s
<item>
<layout class="QVBoxLayout" name="verticalLayout_37">
<item>
<widget class="QPushButton" name="addScanFolderButton">
<widget class="QPushButton" name="addWatchedFolderButton">
<property name="text">
<string>Add entry</string>
<string>Add...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeScanFolderButton">
<widget class="QPushButton" name="editWatchedFolderButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Remove entry</string>
<string>Options..</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="removeWatchedFolderButton">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string>Remove</string>
</property>
</widget>
</item>
@@ -3469,8 +3479,9 @@ Use ';' to split multiple entries. Can use wildcard '*'.</string>
<tabstop>textTempPath</tabstop>
<tabstop>checkAppendqB</tabstop>
<tabstop>scanFoldersView</tabstop>
<tabstop>addScanFolderButton</tabstop>
<tabstop>removeScanFolderButton</tabstop>
<tabstop>addWatchedFolderButton</tabstop>
<tabstop>editWatchedFolderButton</tabstop>
<tabstop>removeWatchedFolderButton</tabstop>
<tabstop>checkExportDir</tabstop>
<tabstop>textExportDir</tabstop>
<tabstop>checkExportDirFin</tabstop>

View File

@@ -1,118 +0,0 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 sledgehammer999 <hammered999@gmail.com>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "scanfoldersdelegate.h"
#include <QComboBox>
#include <QDebug>
#include <QFileDialog>
#include <QTreeView>
#include "base/bittorrent/session.h"
#include "base/scanfoldersmodel.h"
ScanFoldersDelegate::ScanFoldersDelegate(QObject *parent, QTreeView *foldersView)
: QStyledItemDelegate(parent)
, m_folderView(foldersView)
{
}
void ScanFoldersDelegate::setEditorData(QWidget *editor, const QModelIndex &index) const
{
auto *combobox = static_cast<QComboBox*>(editor);
// Set combobox index
if (index.data(Qt::UserRole).toInt() == ScanFoldersModel::CUSTOM_LOCATION)
combobox->setCurrentIndex(4); // '4' is the index of the item after the separator in the QComboBox menu
else
combobox->setCurrentIndex(index.data(Qt::UserRole).toInt());
}
QWidget *ScanFoldersDelegate::createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const
{
if (index.column() != ScanFoldersModel::DOWNLOAD) return nullptr;
auto *editor = new QComboBox(parent);
editor->setFocusPolicy(Qt::StrongFocus);
editor->addItem(ScanFoldersModel::pathTypeDisplayName(ScanFoldersModel::DOWNLOAD_IN_WATCH_FOLDER));
editor->addItem(ScanFoldersModel::pathTypeDisplayName(ScanFoldersModel::DEFAULT_LOCATION));
editor->addItem(ScanFoldersModel::pathTypeDisplayName(ScanFoldersModel::CUSTOM_LOCATION));
if (index.data(Qt::UserRole).toInt() == ScanFoldersModel::CUSTOM_LOCATION)
{
editor->insertSeparator(3);
editor->addItem(index.data().toString());
}
connect(editor, qOverload<int>(&QComboBox::currentIndexChanged)
, this, &ScanFoldersDelegate::comboboxIndexChanged);
return editor;
}
void ScanFoldersDelegate::comboboxIndexChanged(int index)
{
if (index == ScanFoldersModel::CUSTOM_LOCATION)
{
auto *w = static_cast<QWidget *>(sender());
if (w && w->parentWidget())
w->parentWidget()->setFocus();
}
}
void ScanFoldersDelegate::setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const
{
auto *combobox = static_cast<QComboBox*>(editor);
int value = combobox->currentIndex();
switch (value)
{
case ScanFoldersModel::DOWNLOAD_IN_WATCH_FOLDER:
case ScanFoldersModel::DEFAULT_LOCATION:
model->setData(index, value, Qt::UserRole);
break;
case ScanFoldersModel::CUSTOM_LOCATION:
model->setData(
index,
QFileDialog::getExistingDirectory(
nullptr, tr("Select save location"),
index.data(Qt::UserRole).toInt() == ScanFoldersModel::CUSTOM_LOCATION ?
index.data().toString() :
BitTorrent::Session::instance()->defaultSavePath()),
Qt::DisplayRole);
break;
default:
break;
}
}
void ScanFoldersDelegate::updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const
{
qDebug("UpdateEditor Geometry called");
editor->setGeometry(option.rect);
}

View File

@@ -0,0 +1,154 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "watchedfolderoptionsdialog.h"
#include <QDir>
#include <QPushButton>
#include "base/bittorrent/session.h"
#include "base/global.h"
#include "base/settingsstorage.h"
#include "base/utils/fs.h"
#include "base/utils/string.h"
#include "ui_watchedfolderoptionsdialog.h"
#include "utils.h"
#define SETTINGS_KEY(name) "WatchedFolderOptionsDialog/" name
WatchedFolderOptionsDialog::WatchedFolderOptionsDialog(
const TorrentFilesWatcher::WatchedFolderOptions &watchedFolderOptions, QWidget *parent)
: QDialog {parent}
, m_ui {new Ui::WatchedFolderOptionsDialog}
, m_savePath {watchedFolderOptions.addTorrentParams.savePath}
, m_storeDialogSize {SETTINGS_KEY("DialogSize")}
{
m_ui->setupUi(this);
m_ui->savePath->setMode(FileSystemPathEdit::Mode::DirectorySave);
m_ui->savePath->setDialogCaption(tr("Choose save path"));
connect(m_ui->comboTTM, qOverload<int>(&QComboBox::currentIndexChanged), this, &WatchedFolderOptionsDialog::onTMMChanged);
connect(m_ui->categoryComboBox, qOverload<int>(&QComboBox::currentIndexChanged), this, &WatchedFolderOptionsDialog::onCategoryChanged);
m_ui->checkBoxRecursive->setChecked(watchedFolderOptions.recursive);
populateSavePathComboBox();
const auto *session = BitTorrent::Session::instance();
const BitTorrent::AddTorrentParams &torrentParams = watchedFolderOptions.addTorrentParams;
m_ui->startTorrentCheckBox->setChecked(!torrentParams.addPaused.value_or(session->isAddTorrentPaused()));
m_ui->comboTTM->setCurrentIndex(torrentParams.useAutoTMM.value_or(!session->isAutoTMMDisabledByDefault()));
m_ui->contentLayoutComboBox->setCurrentIndex(
static_cast<int>(torrentParams.contentLayout.value_or(session->torrentContentLayout())));
// Load categories
QStringList categories = session->categories().keys();
std::sort(categories.begin(), categories.end(), Utils::String::naturalLessThan<Qt::CaseInsensitive>);
if (!torrentParams.category.isEmpty())
m_ui->categoryComboBox->addItem(torrentParams.category);
m_ui->categoryComboBox->addItem("");
for (const QString &category : asConst(categories))
{
if (category != torrentParams.category)
m_ui->categoryComboBox->addItem(category);
}
loadState();
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setFocus();
}
WatchedFolderOptionsDialog::~WatchedFolderOptionsDialog()
{
saveState();
delete m_ui;
}
TorrentFilesWatcher::WatchedFolderOptions WatchedFolderOptionsDialog::watchedFolderOptions() const
{
TorrentFilesWatcher::WatchedFolderOptions watchedFolderOptions;
watchedFolderOptions.recursive = m_ui->checkBoxRecursive->isChecked();
BitTorrent::AddTorrentParams &params = watchedFolderOptions.addTorrentParams;
params.useAutoTMM = (m_ui->comboTTM->currentIndex() == 1);
if (!*params.useAutoTMM)
params.savePath = m_ui->savePath->selectedPath();
params.category = m_ui->categoryComboBox->currentText();;
params.addPaused = !m_ui->startTorrentCheckBox->isChecked();
params.contentLayout = static_cast<BitTorrent::TorrentContentLayout>(m_ui->contentLayoutComboBox->currentIndex());
return watchedFolderOptions;
}
void WatchedFolderOptionsDialog::loadState()
{
Utils::Gui::resize(this, m_storeDialogSize);
}
void WatchedFolderOptionsDialog::saveState()
{
m_storeDialogSize = size();
}
void WatchedFolderOptionsDialog::onCategoryChanged(const int index)
{
Q_UNUSED(index);
const QString category = m_ui->categoryComboBox->currentText();
if (m_ui->comboTTM->currentIndex() == 1)
{
const QString savePath = BitTorrent::Session::instance()->categorySavePath(category);
m_ui->savePath->setSelectedPath(Utils::Fs::toNativePath(savePath));
}
}
void WatchedFolderOptionsDialog::populateSavePathComboBox()
{
const QString defSavePath {BitTorrent::Session::instance()->defaultSavePath()};
m_ui->savePath->setSelectedPath(!m_savePath.isEmpty() ? m_savePath : defSavePath);
}
void WatchedFolderOptionsDialog::onTMMChanged(const int index)
{
if (index != 1)
{ // 0 is Manual mode and 1 is Automatic mode. Handle all non 1 values as manual mode.
populateSavePathComboBox();
m_ui->groupBoxSavePath->setEnabled(true);
m_ui->savePath->blockSignals(false);
}
else
{
m_ui->groupBoxSavePath->setEnabled(false);
m_ui->savePath->blockSignals(true);
m_savePath = m_ui->savePath->selectedPath();
const QString savePath = BitTorrent::Session::instance()->categorySavePath(m_ui->categoryComboBox->currentText());
m_ui->savePath->setSelectedPath(savePath);
}
}

View File

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2015 sledgehammer999 <hammered999@gmail.com>
* Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -28,31 +28,35 @@
#pragma once
#include <QStyledItemDelegate>
#include <QDialog>
class QAbstractItemModel;
class QModelIndex;
class QPainter;
class QStyleOptionViewItem;
class QTreeView;
#include "base/settingvalue.h"
#include "base/torrentfileswatcher.h"
class PropertiesWidget;
namespace Ui
{
class WatchedFolderOptionsDialog;
}
class ScanFoldersDelegate final : public QStyledItemDelegate
class WatchedFolderOptionsDialog final : public QDialog
{
Q_OBJECT
Q_DISABLE_COPY(WatchedFolderOptionsDialog)
public:
ScanFoldersDelegate(QObject *parent, QTreeView *foldersView);
explicit WatchedFolderOptionsDialog(const TorrentFilesWatcher::WatchedFolderOptions &watchedFolderOptions, QWidget *parent);
~WatchedFolderOptionsDialog() override;
private slots:
void comboboxIndexChanged(int index);
TorrentFilesWatcher::WatchedFolderOptions watchedFolderOptions() const;
private:
void setModelData(QWidget *editor, QAbstractItemModel *model, const QModelIndex &index) const override;
void updateEditorGeometry(QWidget *editor, const QStyleOptionViewItem &option, const QModelIndex &) const override;
void setEditorData(QWidget *editor, const QModelIndex &index) const override;
QWidget *createEditor(QWidget *parent, const QStyleOptionViewItem &, const QModelIndex &index) const override;
void populateSavePathComboBox();
void loadState();
void saveState();
void onTMMChanged(int index);
void onCategoryChanged(int index);
QTreeView *m_folderView;
Ui::WatchedFolderOptionsDialog *m_ui;
QString m_savePath;
SettingValue<QSize> m_storeDialogSize;
};

View File

@@ -0,0 +1,315 @@
<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
<class>WatchedFolderOptionsDialog</class>
<widget class="QDialog" name="WatchedFolderOptionsDialog">
<property name="geometry">
<rect>
<x>0</x>
<y>0</y>
<width>462</width>
<height>306</height>
</rect>
</property>
<property name="windowTitle">
<string>Watched Folder Options</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_3">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QCheckBox" name="checkBoxRecursive">
<property name="toolTip">
<string>&lt;html&gt;&lt;head/&gt;&lt;body&gt;&lt;p&gt;Will watch the folder and all its subfolders. In Manual torrent management mode it will also add subfolder name to the selected Save path.&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;</string>
</property>
<property name="text">
<string>Recursive mode</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QSplitter" name="splitter">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="childrenCollapsible">
<bool>false</bool>
</property>
<widget class="QFrame" name="torrentoptionsFrame">
<layout class="QVBoxLayout" name="mainlayout_addui">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QGroupBox" name="groupBoxParameters">
<property name="title">
<string>Torrent parameters</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<layout class="QHBoxLayout" name="managementLayout">
<item>
<widget class="QLabel" name="labelTorrentManagementMode">
<property name="text">
<string>Torrent Management Mode:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboTTM">
<property name="toolTip">
<string>Automatic mode means that various torrent properties(eg save path) will be decided by the associated category</string>
</property>
<item>
<property name="text">
<string>Manual</string>
</property>
</item>
<item>
<property name="text">
<string>Automatic</string>
</property>
</item>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<widget class="QGroupBox" name="groupBoxSavePath">
<property name="title">
<string>Save at</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout">
<item>
<widget class="FileSystemPathLineEdit" name="savePath" native="true"/>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="categoryLayout">
<item>
<widget class="QLabel" name="labelCategory">
<property name="text">
<string>Category:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="categoryComboBox">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="editable">
<bool>true</bool>
</property>
<property name="insertPolicy">
<enum>QComboBox::InsertAtTop</enum>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_7">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
<layout class="QVBoxLayout" name="verticalLayout_4">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QCheckBox" name="startTorrentCheckBox">
<property name="text">
<string>Start torrent</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_2">
<item>
<widget class="QLabel" name="contentLayoutLabel">
<property name="text">
<string>Content layout:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="contentLayoutComboBox">
<property name="currentIndex">
<number>0</number>
</property>
<item>
<property name="text">
<string>Original</string>
</property>
</item>
<item>
<property name="text">
<string>Create subfolder</string>
</property>
</item>
<item>
<property name="text">
<string>Don't create subfolder</string>
</property>
</item>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
</layout>
</widget>
</item>
</layout>
</widget>
</widget>
</item>
<item>
<spacer name="verticalSpacer">
<property name="orientation">
<enum>Qt::Vertical</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>20</width>
<height>40</height>
</size>
</property>
</spacer>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="standardButtons">
<set>QDialogButtonBox::Cancel|QDialogButtonBox::Ok</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>FileSystemPathLineEdit</class>
<extends>QWidget</extends>
<header>gui/fspathedit.h</header>
<container>1</container>
</customwidget>
</customwidgets>
<resources/>
<connections>
<connection>
<sender>buttonBox</sender>
<signal>accepted()</signal>
<receiver>WatchedFolderOptionsDialog</receiver>
<slot>accept()</slot>
<hints>
<hint type="sourcelabel">
<x>928</x>
<y>855</y>
</hint>
<hint type="destinationlabel">
<x>157</x>
<y>274</y>
</hint>
</hints>
</connection>
<connection>
<sender>buttonBox</sender>
<signal>rejected()</signal>
<receiver>WatchedFolderOptionsDialog</receiver>
<slot>reject()</slot>
<hints>
<hint type="sourcelabel">
<x>928</x>
<y>855</y>
</hint>
<hint type="destinationlabel">
<x>286</x>
<y>274</y>
</hint>
</hints>
</connection>
</connections>
</ui>

View File

@@ -0,0 +1,178 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#include "watchedfoldersmodel.h"
#include <QDir>
#include "base/exceptions.h"
#include "base/global.h"
#include "base/utils/fs.h"
WatchedFoldersModel::WatchedFoldersModel(TorrentFilesWatcher *fsWatcher, QObject *parent)
: QAbstractListModel {parent}
, m_fsWatcher {fsWatcher}
, m_watchedFolders {m_fsWatcher->folders().keys()}
, m_watchedFoldersOptions {m_fsWatcher->folders()}
{
connect(m_fsWatcher, &TorrentFilesWatcher::watchedFolderSet, this, &WatchedFoldersModel::onFolderSet);
connect(m_fsWatcher, &TorrentFilesWatcher::watchedFolderRemoved, this, &WatchedFoldersModel::onFolderRemoved);
}
int WatchedFoldersModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : m_watchedFolders.count();
}
int WatchedFoldersModel::columnCount(const QModelIndex &parent) const
{
Q_UNUSED(parent);
return 1;
}
QVariant WatchedFoldersModel::data(const QModelIndex &index, const int role) const
{
if (!index.isValid() || (index.row() >= rowCount()) || (index.column() >= columnCount()))
return {};
if (role == Qt::DisplayRole)
return Utils::Fs::toNativePath(m_watchedFolders.at(index.row()));
return {};
}
QVariant WatchedFoldersModel::headerData(const int section, const Qt::Orientation orientation, const int role) const
{
if ((orientation != Qt::Horizontal) || (role != Qt::DisplayRole)
|| (section < 0) || (section >= columnCount()))
{
return {};
}
return tr("Watched Folder");
}
bool WatchedFoldersModel::removeRows(const int row, const int count, const QModelIndex &parent)
{
if (parent.isValid() || (row < 0) || (row >= rowCount())
|| (count <= 0) || ((row + count) > rowCount()))
{
return false;
}
const int firstRow = row;
const int lastRow = row + (count - 1);
beginRemoveRows(parent, firstRow, lastRow);
for (int i = firstRow; i <= lastRow; ++i)
{
const QString folderPath = m_watchedFolders.takeAt(i);
m_watchedFoldersOptions.remove(folderPath);
m_deletedFolders.insert(folderPath);
}
endRemoveRows();
return true;
}
void WatchedFoldersModel::addFolder(const QString &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
{
if (path.isEmpty())
throw InvalidArgument(tr("Watched folder path cannot be empty."));
const QDir watchDir {path};
const QString canonicalWatchPath = watchDir.canonicalPath();
if (m_watchedFoldersOptions.contains(canonicalWatchPath))
throw RuntimeError(tr("Folder '%1' is already in watch list.").arg(path));
if (!watchDir.exists())
throw RuntimeError(tr("Folder '%1' doesn't exist.").arg(path));
if (!watchDir.isReadable())
throw RuntimeError(tr("Folder '%1' isn't readable.").arg(path));
m_deletedFolders.remove(canonicalWatchPath);
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_watchedFolders.append(canonicalWatchPath);
m_watchedFoldersOptions[canonicalWatchPath] = options;
endInsertRows();
}
TorrentFilesWatcher::WatchedFolderOptions WatchedFoldersModel::folderOptions(const int row) const
{
Q_ASSERT((row >= 0) && (row < rowCount()));
const QString folderPath = m_watchedFolders.at(row);
return m_watchedFoldersOptions[folderPath];
}
void WatchedFoldersModel::setFolderOptions(const int row, const TorrentFilesWatcher::WatchedFolderOptions &options)
{
Q_ASSERT((row >= 0) && (row < rowCount()));
const QString folderPath = m_watchedFolders.at(row);
m_watchedFoldersOptions[folderPath] = options;
}
void WatchedFoldersModel::apply()
{
const QSet<QString> deletedFolders {m_deletedFolders};
// We have to clear `m_deletedFolders` for optimization reason, otherwise
// it will be cleared one element at a time in `onFolderRemoved()` handler
m_deletedFolders.clear();
for (const QString &path : deletedFolders)
m_fsWatcher->removeWatchedFolder(path);
for (const QString &path : asConst(m_watchedFolders))
m_fsWatcher->setWatchedFolder(path, m_watchedFoldersOptions.value(path));
}
void WatchedFoldersModel::onFolderSet(const QString &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
{
if (!m_watchedFoldersOptions.contains(path))
{
m_deletedFolders.remove(path);
beginInsertRows(QModelIndex(), rowCount(), rowCount());
m_watchedFolders.append(path);
m_watchedFoldersOptions[path] = options;
endInsertRows();
}
else
{
m_watchedFoldersOptions[path] = options;
}
}
void WatchedFoldersModel::onFolderRemoved(const QString &path)
{
const int row = m_watchedFolders.indexOf(path);
if (row >= 0)
removeRows(row, 1);
m_deletedFolders.remove(path);
}

View File

@@ -0,0 +1,68 @@
/*
* Bittorrent Client using Qt and libtorrent.
* Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*/
#pragma once
#include <QtContainerFwd>
#include <QAbstractListModel>
#include <QHash>
#include <QSet>
#include <QStringList>
#include "base/torrentfileswatcher.h"
class WatchedFoldersModel final : public QAbstractListModel
{
Q_OBJECT
Q_DISABLE_COPY(WatchedFoldersModel)
public:
explicit WatchedFoldersModel(TorrentFilesWatcher *fsWatcher, QObject *parent = nullptr);
int rowCount(const QModelIndex &parent = {}) const override;
int columnCount(const QModelIndex &parent = {}) const override;
QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
QVariant headerData(int section, Qt::Orientation orientation, int role = Qt::DisplayRole) const override;
bool removeRows(int row, int count, const QModelIndex &parent = QModelIndex()) override;
void addFolder(const QString &path, const TorrentFilesWatcher::WatchedFolderOptions &options);
TorrentFilesWatcher::WatchedFolderOptions folderOptions(int row) const;
void setFolderOptions(int row, const TorrentFilesWatcher::WatchedFolderOptions &options);
void apply();
private:
void onFolderSet(const QString &path, const TorrentFilesWatcher::WatchedFolderOptions &options);
void onFolderRemoved(const QString &path);
TorrentFilesWatcher *m_fsWatcher;
QStringList m_watchedFolders;
QHash<QString, TorrentFilesWatcher::WatchedFolderOptions> m_watchedFoldersOptions;
QSet<QString> m_deletedFolders;
};