Skip to content

Commit 3d76a2e

Browse files
committed
Make sure that torrent file is deleted after sending torrent-add request (#563)
Also do it on the background thread.
1 parent 28f6d05 commit 3d76a2e

File tree

9 files changed

+90
-55
lines changed

9 files changed

+90
-55
lines changed

src/fileutils.cpp

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -216,29 +216,34 @@ namespace tremotesf {
216216
return data;
217217
}
218218

219-
void deleteFile(const QString& path) {
220-
info().log("Deleting file {}", path);
221-
QFile file(path);
222-
if (file.remove()) {
223-
info().log("Succesfully deleted file");
224-
} else {
225-
throw QFileError(fmt::format("Failed to delete {}: {}", fileDescription(file), errorDescription(file)));
219+
namespace {
220+
void deleteFileImpl(QFile& file) {
221+
info().log("Deleting {}", fileDescription(file));
222+
if (file.remove()) {
223+
info().log("Succesfully deleted file");
224+
} else {
225+
throw QFileError(fmt::format("Failed to delete {}: {}", fileDescription(file), errorDescription(file)));
226+
}
226227
}
227228
}
228229

229-
void moveFileToTrash(const QString& path) {
230-
info().log("Moving file {} to trash", path);
231-
QFile file(path);
230+
void deleteFile(const QString& filePath) {
231+
QFile file(filePath);
232+
deleteFileImpl(file);
233+
}
234+
235+
void moveFileToTrashOrDelete(const QString& filePath) {
236+
QFile file(filePath);
237+
info().log("Moving {} to trash", fileDescription(file));
232238
if (file.moveToTrash()) {
233239
if (const auto newPath = file.fileName(); !newPath.isEmpty()) {
234240
info().log("Successfully moved file to trash, new path is {}", newPath);
235241
} else {
236242
info().log("Successfully moved file to trash");
237243
}
238244
} else {
239-
throw QFileError(
240-
fmt::format("Failed to move {} to trash: {}", fileDescription(file), errorDescription(file))
241-
);
245+
warning().log("Failed to move {} to trash: {}", fileDescription(file), errorDescription(file));
246+
deleteFileImpl(file);
242247
}
243248
}
244249

src/fileutils.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,8 @@ namespace tremotesf {
2929

3030
[[nodiscard]] QByteArray readFile(const QString& path);
3131

32-
void deleteFile(const QString& path);
33-
void moveFileToTrash(const QString& path);
32+
void deleteFile(const QString &filePath);
33+
void moveFileToTrashOrDelete(const QString &filePath);
3434

3535
[[maybe_unused]]
3636
QString resolveExternalBundledResourcesPath(QLatin1String path);

src/rpc/rpc.cpp

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,8 @@ namespace tremotesf {
239239
std::vector<int> lowPriorityFiles,
240240
std::map<QString, QString> renamedFiles,
241241
TorrentData::Priority bandwidthPriority,
242-
bool start
242+
bool start,
243+
DeleteFileMode deleteFileMode
243244
) {
244245
if (isConnected()) {
245246
mBackgroundRequestsCoroutineScope.launch(addTorrentFileImpl(
@@ -250,7 +251,8 @@ namespace tremotesf {
250251
std::move(lowPriorityFiles),
251252
std::move(renamedFiles),
252253
bandwidthPriority,
253-
start
254+
start,
255+
deleteFileMode
254256
));
255257
}
256258
}
@@ -285,6 +287,20 @@ namespace tremotesf {
285287
{"paused"_l1, !start}}
286288
);
287289
}
290+
291+
Coroutine<> deleteTorrentFile(QString filePath, bool moveToTrash) {
292+
co_await runOnThreadPool([moveToTrash, filePath = std::move(filePath)] {
293+
try {
294+
if (moveToTrash) {
295+
moveFileToTrashOrDelete(filePath);
296+
} else {
297+
deleteFile(filePath);
298+
}
299+
} catch (const QFileError& e) {
300+
warning().logWithException(e, "Failed to delete torrent file");
301+
}
302+
});
303+
}
288304
}
289305

290306
Coroutine<> Rpc::addTorrentFileImpl(
@@ -295,7 +311,8 @@ namespace tremotesf {
295311
std::vector<int> lowPriorityFiles,
296312
std::map<QString, QString> renamedFiles,
297313
TorrentData::Priority bandwidthPriority,
298-
bool start
314+
bool start,
315+
DeleteFileMode deleteFileMode
299316
) {
300317
std::optional<QByteArray> requestData = co_await runOnThreadPool(
301318
makeAddTorrentFileRequestData,
@@ -311,6 +328,9 @@ namespace tremotesf {
311328
emit torrentAddError(filePath);
312329
co_return;
313330
}
331+
if (deleteFileMode != DeleteFileMode::No) {
332+
mDeletingFilesCoroutineScope.launch(deleteTorrentFile(filePath, deleteFileMode == DeleteFileMode::MoveToTrash));
333+
}
314334
if (!isConnected()) co_return;
315335
const auto response = co_await mRequestRouter->postRequest("torrent-add"_l1, std::move(requestData).value());
316336
if (response.arguments.contains(torrentDuplicateKey)) {

src/rpc/rpc.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,8 @@ namespace tremotesf {
126126
void connect();
127127
void disconnect();
128128

129+
enum class DeleteFileMode { No, Delete, MoveToTrash };
130+
129131
void addTorrentFile(
130132
QString filePath,
131133
QString downloadDirectory,
@@ -134,7 +136,8 @@ namespace tremotesf {
134136
std::vector<int> lowPriorityFiles,
135137
std::map<QString, QString> renamedFiles,
136138
TorrentData::Priority bandwidthPriority,
137-
bool start
139+
bool start,
140+
DeleteFileMode deleteFileMode
138141
);
139142

140143
void addTorrentLinks(
@@ -182,7 +185,8 @@ namespace tremotesf {
182185
std::vector<int> lowPriorityFiles,
183186
std::map<QString, QString> renamedFiles,
184187
TorrentData::Priority bandwidthPriority,
185-
bool start
188+
bool start,
189+
DeleteFileMode deleteFileMode
186190
);
187191
Coroutine<> addTorrentLinksImpl(
188192
QStringList links, QString downloadDirectory, TorrentData::Priority bandwidthPriority, bool start
@@ -211,6 +215,7 @@ namespace tremotesf {
211215
impl::RequestRouter* mRequestRouter{};
212216
CoroutineScope mBackgroundRequestsCoroutineScope{};
213217
CoroutineScope mAutoReconnectCoroutineScope{};
218+
CoroutineScope mDeletingFilesCoroutineScope{};
214219

215220
bool mAutoReconnectEnabled{};
216221

src/ui/screens/addtorrent/addtorrentdialog.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include <fmt/format.h>
2929

3030
#include "addtorrenthelpers.h"
31+
#include "fileutils.h"
3132
#include "formatutils.h"
3233
#include "magnetlinkparser.h"
3334
#include "settings.h"
@@ -64,6 +65,19 @@ namespace tremotesf {
6465
}
6566
return row;
6667
}
68+
69+
Rpc::DeleteFileMode determineDeleteFileMode(const AddTorrentDialog::AddTorrentParametersWidgets& widgets) {
70+
if (!widgets.deleteTorrentFileGroupBox || !widgets.moveTorrentFileToTrashCheckBox) {
71+
return Rpc::DeleteFileMode::No;
72+
}
73+
if (!widgets.deleteTorrentFileGroupBox->isChecked()) {
74+
return Rpc::DeleteFileMode::No;
75+
}
76+
if (widgets.moveTorrentFileToTrashCheckBox->isChecked()) {
77+
return Rpc::DeleteFileMode::MoveToTrash;
78+
}
79+
return Rpc::DeleteFileMode::Delete;
80+
}
6781
}
6882

6983
AddTorrentDialog::AddTorrentDialog(Rpc* rpc, std::variant<FileParams, UrlParams> params, QWidget* parent)
@@ -95,7 +109,8 @@ namespace tremotesf {
95109
mFilesModel->lowPriorityFiles(),
96110
mFilesModel->renamedFiles(),
97111
priorityFromComboBoxIndex(mAddTorrentParametersWidgets.priorityComboBox->currentIndex()),
98-
mAddTorrentParametersWidgets.startTorrentCheckBox->isChecked()
112+
mAddTorrentParametersWidgets.startTorrentCheckBox->isChecked(),
113+
determineDeleteFileMode(mAddTorrentParametersWidgets)
99114
);
100115
}
101116
} else {
@@ -117,7 +132,6 @@ namespace tremotesf {
117132
if (settings->rememberAddTorrentParameters()) {
118133
mAddTorrentParametersWidgets.saveToSettings();
119134
}
120-
deleteTorrentFileIfEnabled();
121135
QDialog::accept();
122136
}
123137

@@ -349,7 +363,9 @@ namespace tremotesf {
349363
mTorrentFileInfoHashAndTrackers =
350364
std::pair{std::move(torrentFile.infoHashV1), std::move(torrentFile.trackers)};
351365
if (checkIfTorrentFileExists()) {
352-
deleteTorrentFileIfEnabled();
366+
if (isAddingFile()) {
367+
co_await deleteTorrentFileIfEnabled();
368+
}
353369
close();
354370
co_return;
355371
}
@@ -407,13 +423,22 @@ namespace tremotesf {
407423
return false;
408424
}
409425

410-
void AddTorrentDialog::deleteTorrentFileIfEnabled() {
411-
if (isAddingFile() && mAddTorrentParametersWidgets.deleteTorrentFileGroupBox->isChecked()) {
412-
deleteTorrentFile(
413-
std::get<FileParams>(mParams).filePath,
414-
mAddTorrentParametersWidgets.moveTorrentFileToTrashCheckBox->isChecked()
415-
);
426+
Coroutine<> AddTorrentDialog::deleteTorrentFileIfEnabled() {
427+
const auto deleteFileMode = determineDeleteFileMode(mAddTorrentParametersWidgets);
428+
if (deleteFileMode == Rpc::DeleteFileMode::No) {
429+
co_return;
416430
}
431+
co_await runOnThreadPool([deleteFileMode, filePath = std::get<FileParams>(mParams).filePath] {
432+
try {
433+
if (deleteFileMode == Rpc::DeleteFileMode::MoveToTrash) {
434+
moveFileToTrashOrDelete(filePath);
435+
} else {
436+
deleteFile(filePath);
437+
}
438+
} catch (const QFileError& e) {
439+
warning().logWithException(e, "Failed to delete torrent file");
440+
}
441+
});
417442
}
418443

419444
void AddTorrentDialog::AddTorrentParametersWidgets::reset(Rpc* rpc) const {

src/ui/screens/addtorrent/addtorrentdialog.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ namespace tremotesf {
8181
void parseMagnetLinksAndCheckIfTorrentsExist(QStringList& urls);
8282
bool checkIfTorrentFileExists();
8383

84-
void deleteTorrentFileIfEnabled();
84+
Coroutine<> deleteTorrentFileIfEnabled();
8585

8686
Rpc* mRpc;
8787
std::variant<FileParams, UrlParams> mParams;

src/ui/screens/addtorrent/addtorrenthelpers.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,7 @@
99

1010
#include "addtorrenthelpers.h"
1111

12-
#include "fileutils.h"
1312
#include "settings.h"
14-
#include "log/log.h"
1513
#include "rpc/rpc.h"
1614
#include "rpc/servers.h"
1715
#include "rpc/serversettings.h"
@@ -44,23 +42,6 @@ namespace tremotesf {
4442
};
4543
}
4644

47-
void deleteTorrentFile(const QString& filePath, bool moveToTrash) {
48-
try {
49-
if (moveToTrash) {
50-
try {
51-
moveFileToTrash(filePath);
52-
} catch (const QFileError& e) {
53-
warning().logWithException(e, "Failed to move torrent file to trash");
54-
deleteFile(filePath);
55-
}
56-
} else {
57-
deleteFile(filePath);
58-
}
59-
} catch (const QFileError& e) {
60-
warning().logWithException(e, "Failed to delete torrent file");
61-
}
62-
}
63-
6445
QDialog* askForMergingTrackers(Torrent* torrent, std::vector<std::set<QString>> trackers, QWidget* parent) {
6546
auto* const settings = Settings::instance();
6647
QMessageBox* messageBox{};

src/ui/screens/addtorrent/addtorrenthelpers.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ namespace tremotesf {
2727
AddTorrentParameters getAddTorrentParameters(Rpc* rpc);
2828
AddTorrentParameters getInitialAddTorrentParameters(Rpc* rpc);
2929

30-
void deleteTorrentFile(const QString& filePath, bool moveToTrash);
31-
3230
QDialog* askForMergingTrackers(Torrent* torrent, std::vector<std::set<QString>> trackers, QWidget* parent);
3331

3432
QString bencodeErrorString(bencode::Error::Type type);

src/ui/screens/mainwindow/mainwindowviewmodel.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,12 @@ namespace tremotesf {
255255
{},
256256
{},
257257
parameters.priority,
258-
parameters.startAfterAdding
258+
parameters.startAfterAdding,
259+
parameters.deleteTorrentFile
260+
? (parameters.moveTorrentFileToTrash ? Rpc::DeleteFileMode::MoveToTrash : Rpc::DeleteFileMode::Delete)
261+
: Rpc::DeleteFileMode::No
262+
259263
);
260-
if (parameters.deleteTorrentFile) {
261-
deleteTorrentFile(filePath, parameters.moveTorrentFileToTrash);
262-
}
263264
}
264265
}
265266

0 commit comments

Comments
 (0)