From 2c9768bc12250dc34706372676f6e6b09777a913 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sat, 4 Nov 2023 13:14:08 -0500 Subject: [PATCH] fix: only append '.added' suffix to watchdir files (#5705) --- qt/AddData.cc | 25 +++++++++++++++++++++++++ qt/AddData.h | 23 +++++++++++++++++++++++ qt/Application.cc | 22 +++++++++++++++++----- qt/Application.h | 5 +++-- qt/MainWindow.cc | 10 ++++------ qt/OptionsDialog.cc | 5 ++++- qt/Session.cc | 32 ++++++-------------------------- qt/Session.h | 2 +- 8 files changed, 83 insertions(+), 41 deletions(-) diff --git a/qt/AddData.cc b/qt/AddData.cc index e60f82beb..35b1323fc 100644 --- a/qt/AddData.cc +++ b/qt/AddData.cc @@ -124,3 +124,28 @@ QString AddData::readableShortName() const return readableName(); } } + +void AddData::disposeSourceFile() const +{ + auto file = QFile{ filename }; + if (!disposal_ || !file.exists()) + { + return; + } + + switch (*disposal_) + { + case FilenameDisposal::Delete: + file.setPermissions(QFile::ReadOwner | QFile::WriteOwner); + file.remove(); + break; + + case FilenameDisposal::Rename: + file.rename(QStringLiteral("%1.added").arg(filename)); + break; + + default: + // no action + break; + } +} diff --git a/qt/AddData.h b/qt/AddData.h index 37659b2ad..25fff6738 100644 --- a/qt/AddData.h +++ b/qt/AddData.h @@ -14,6 +14,14 @@ class AddData { public: + // what to do with the source file after adding the torrent + enum class FilenameDisposal + { + NoAction, + Delete, + Rename + }; + enum { NONE, @@ -36,6 +44,18 @@ public: QString readableName() const; QString readableShortName() const; + void disposeSourceFile() const; + + constexpr void setFileDisposal(FilenameDisposal disposal) + { + disposal_ = disposal; + } + + constexpr auto& fileDisposal() const noexcept + { + return disposal_; + } + static std::optional create(QString const& str) { if (auto ret = AddData{ str }; ret.type != NONE) @@ -51,4 +71,7 @@ public: QString filename; QString magnet; QUrl url; + +private: + std::optional disposal_; }; diff --git a/qt/Application.cc b/qt/Application.cc index 1b783e225..7fb866564 100644 --- a/qt/Application.cc +++ b/qt/Application.cc @@ -286,7 +286,7 @@ Application::Application(int& argc, char** argv) connect(session_.get(), &Session::sourceChanged, this, &Application::onSessionSourceChanged); connect(session_.get(), &Session::torrentsRemoved, model_.get(), &TorrentModel::removeTorrents); connect(session_.get(), &Session::torrentsUpdated, model_.get(), &TorrentModel::updateTorrents); - connect(watch_dir_.get(), &WatchDir::torrentFileAdded, this, qOverload(&Application::addTorrent)); + connect(watch_dir_.get(), &WatchDir::torrentFileAdded, this, qOverload(&Application::addWatchdirTorrent)); // init from preferences for (auto const key : { Prefs::DIR_WATCH }) @@ -343,9 +343,10 @@ Application::Application(int& argc, char** argv) dialog->show(); } + // torrent files passed in on the command line for (QString const& filename : filenames) { - addTorrent(filename); + addTorrent(AddData{ filename }); } InteropHelper::registerObject(this); @@ -571,18 +572,29 @@ void Application::refreshTorrents() **** ***/ -void Application::addTorrent(QString const& addme) const +void Application::addWatchdirTorrent(QString const& filename) const { - addTorrent(AddData(addme)); + auto add_data = AddData{ filename }; + auto const disposal = prefs_->getBool(Prefs::TRASH_ORIGINAL) ? AddData::FilenameDisposal::Delete : + AddData::FilenameDisposal::Rename; + add_data.setFileDisposal(disposal); + addTorrent(std::move(add_data)); } -void Application::addTorrent(AddData const& addme) const +void Application::addTorrent(AddData addme) const { if (addme.type == addme.NONE) { return; } + // if there's not already a disposal action set, + // then honor the `trash original` preference setting + if (!addme.fileDisposal() && prefs_->getBool(Prefs::TRASH_ORIGINAL)) + { + addme.setFileDisposal(AddData::FilenameDisposal::Delete); + } + if (!prefs_->getBool(Prefs::OPTIONS_PROMPT)) { session_->addTorrent(addme); diff --git a/qt/Application.h b/qt/Application.h index 7e59c5872..144de6ee1 100644 --- a/qt/Application.h +++ b/qt/Application.h @@ -18,6 +18,7 @@ #include #include +#include "AddData.h" #include "Typedefs.h" #include "Utils.h" // std::hash @@ -71,8 +72,8 @@ signals: void faviconsChanged(); public slots: - void addTorrent(AddData const&) const; - void addTorrent(QString const&) const; + void addTorrent(AddData) const; + void addWatchdirTorrent(QString const& filename) const; private slots: void consentGiven(int result) const; diff --git a/qt/MainWindow.cc b/qt/MainWindow.cc index cc15efa7e..40b9f5d56 100644 --- a/qt/MainWindow.cc +++ b/qt/MainWindow.cc @@ -1315,9 +1315,7 @@ void MainWindow::addTorrents(QStringList const& filenames) if (auto const* const file_dialog = qobject_cast(sender()); file_dialog != nullptr) { - auto const* const b = file_dialog->findChild(show_options_checkbox_name_); - - if (b != nullptr) + if (auto const* const b = file_dialog->findChild(show_options_checkbox_name_); b != nullptr) { show_options = b->isChecked(); } @@ -1325,7 +1323,7 @@ void MainWindow::addTorrents(QStringList const& filenames) for (QString const& filename : filenames) { - addTorrent(AddData(filename), show_options); + addTorrent(AddData{ filename }, show_options); } } @@ -1594,7 +1592,7 @@ void MainWindow::dropEvent(QDropEvent* event) key = url.toLocalFile(); } - trApp->addTorrent(AddData(key)); + trApp->addTorrent(AddData{ key }); } } } @@ -1625,7 +1623,7 @@ bool MainWindow::event(QEvent* e) if (!clipboard_processed_keys_.contains(key)) { clipboard_processed_keys_.append(key); - trApp->addTorrent(AddData(key)); + trApp->addTorrent(AddData{ key }); } } } diff --git a/qt/OptionsDialog.cc b/qt/OptionsDialog.cc index ac20aab0d..d0d744e67 100644 --- a/qt/OptionsDialog.cc +++ b/qt/OptionsDialog.cc @@ -297,7 +297,10 @@ void OptionsDialog::onAccepted() } } - session_.addTorrent(add_, &args, ui_.trashCheck->isChecked()); + auto const disposal = ui_.trashCheck->isChecked() ? AddData::FilenameDisposal::Delete : AddData::FilenameDisposal::NoAction; + add_.setFileDisposal(disposal); + + session_.addTorrent(add_, &args); deleteLater(); } diff --git a/qt/Session.cc b/qt/Session.cc index affe94ba9..39515941a 100644 --- a/qt/Session.cc +++ b/qt/Session.cc @@ -990,7 +990,7 @@ void Session::setBlocklistSize(int64_t i) emit blocklistUpdated(i); } -void Session::addTorrent(AddData add_me, tr_variant* args_dict, bool trash_original) +void Session::addTorrent(AddData add_me, tr_variant* args_dict) { assert(tr_variantDictFind(args_dict, TR_KEY_filename) == nullptr); assert(tr_variantDictFind(args_dict, TR_KEY_metainfo) == nullptr); @@ -1037,41 +1037,22 @@ void Session::addTorrent(AddData add_me, tr_variant* args_dict, bool trash_origi }); q->add( - [this, add_me, trash_original](RpcResponse const& r) + [this, add_me](RpcResponse const& r) { - bool session_has_torrent = false; - if (tr_variant* dup = nullptr; tr_variantDictFindDict(r.args.get(), TR_KEY_torrent_added, &dup)) { - session_has_torrent = true; + add_me.disposeSourceFile(); } else if (tr_variantDictFindDict(r.args.get(), TR_KEY_torrent_duplicate, &dup)) { - session_has_torrent = true; + add_me.disposeSourceFile(); - auto const hash = dictFind(dup, TR_KEY_hashString); - if (hash) + if (auto const hash = dictFind(dup, TR_KEY_hashString); hash) { duplicates_.try_emplace(add_me.readableShortName(), *hash); duplicates_timer_.start(1000); } } - - if (auto const& filename = add_me.filename; - session_has_torrent && !filename.isEmpty() && add_me.type == AddData::FILENAME) - { - auto file = QFile{ filename }; - - if (trash_original) - { - file.setPermissions(QFile::ReadOwner | QFile::WriteOwner); - file.remove(); - } - else - { - file.rename(QStringLiteral("%1.added").arg(filename)); - } - } }); q->run(); @@ -1112,8 +1093,7 @@ void Session::addTorrent(AddData add_me) { tr_variant args; tr_variantInitDict(&args, 3); - - addTorrent(std::move(add_me), &args, prefs_.getBool(Prefs::TRASH_ORIGINAL)); + addTorrent(std::move(add_me), &args); } void Session::addNewlyCreatedTorrent(QString const& filename, QString const& local_path) diff --git a/qt/Session.h b/qt/Session.h index 80fde708c..d622d4999 100644 --- a/qt/Session.h +++ b/qt/Session.h @@ -93,7 +93,7 @@ public: void torrentSetLocation(torrent_ids_t const& torrent_ids, QString const& path, bool do_move); void torrentRenamePath(torrent_ids_t const& torrent_ids, QString const& oldpath, QString const& newname); - void addTorrent(AddData add_me, tr_variant* args_dict, bool trash_original); + void addTorrent(AddData add_me, tr_variant* args_dict); void initTorrents(torrent_ids_t const& ids = {}); void pauseTorrents(torrent_ids_t const& torrent_ids = {}); void startTorrents(torrent_ids_t const& torrent_ids = {});