Sync translations from Transifex and run lupdate
[qBittorrent.git] / src / base / torrentfileswatcher.cpp
blob9c455aa9b8cc2c5d0c61b46f089ea9c2e99e4472
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2021 Vladimir Golovnev <glassez@yandex.ru>
4 * Copyright (C) 2010 Christian Kandeler, Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "torrentfileswatcher.h"
32 #include <chrono>
34 #include <QtGlobal>
35 #include <QDir>
36 #include <QDirIterator>
37 #include <QFile>
38 #include <QFileSystemWatcher>
39 #include <QJsonArray>
40 #include <QJsonDocument>
41 #include <QJsonObject>
42 #include <QJsonValue>
43 #include <QSaveFile>
44 #include <QSet>
45 #include <QTextStream>
46 #include <QThread>
47 #include <QTimer>
48 #include <QVariant>
50 #include "base/algorithm.h"
51 #include "base/bittorrent/magneturi.h"
52 #include "base/bittorrent/torrentcontentlayout.h"
53 #include "base/bittorrent/session.h"
54 #include "base/bittorrent/torrent.h"
55 #include "base/bittorrent/torrentinfo.h"
56 #include "base/exceptions.h"
57 #include "base/global.h"
58 #include "base/logger.h"
59 #include "base/profile.h"
60 #include "base/settingsstorage.h"
61 #include "base/tagset.h"
62 #include "base/utils/fs.h"
63 #include "base/utils/string.h"
65 using namespace std::chrono_literals;
67 const std::chrono::duration WATCH_INTERVAL = 10s;
68 const int MAX_FAILED_RETRIES = 5;
69 const QString CONF_FILE_NAME {QStringLiteral("watched_folders.json")};
71 const QString OPTION_ADDTORRENTPARAMS {QStringLiteral("add_torrent_params")};
72 const QString OPTION_RECURSIVE {QStringLiteral("recursive")};
74 const QString PARAM_CATEGORY {QStringLiteral("category")};
75 const QString PARAM_TAGS {QStringLiteral("tags")};
76 const QString PARAM_SAVEPATH {QStringLiteral("save_path")};
77 const QString PARAM_OPERATINGMODE {QStringLiteral("operating_mode")};
78 const QString PARAM_STOPPED {QStringLiteral("stopped")};
79 const QString PARAM_CONTENTLAYOUT {QStringLiteral("content_layout")};
80 const QString PARAM_AUTOTMM {QStringLiteral("use_auto_tmm")};
81 const QString PARAM_UPLOADLIMIT {QStringLiteral("upload_limit")};
82 const QString PARAM_DOWNLOADLIMIT {QStringLiteral("download_limit")};
83 const QString PARAM_SEEDINGTIMELIMIT {QStringLiteral("seeding_time_limit")};
84 const QString PARAM_RATIOLIMIT {QStringLiteral("ratio_limit")};
86 namespace
88 TagSet parseTagSet(const QJsonArray &jsonArr)
90 TagSet tags;
91 for (const QJsonValue &jsonVal : jsonArr)
92 tags.insert(jsonVal.toString());
94 return tags;
97 QJsonArray serializeTagSet(const TagSet &tags)
99 QJsonArray arr;
100 for (const QString &tag : tags)
101 arr.append(tag);
103 return arr;
106 std::optional<bool> getOptionalBool(const QJsonObject &jsonObj, const QString &key)
108 const QJsonValue jsonVal = jsonObj.value(key);
109 if (jsonVal.isUndefined() || jsonVal.isNull())
110 return std::nullopt;
112 return jsonVal.toBool();
115 template <typename Enum>
116 std::optional<Enum> getOptionalEnum(const QJsonObject &jsonObj, const QString &key)
118 const QJsonValue jsonVal = jsonObj.value(key);
119 if (jsonVal.isUndefined() || jsonVal.isNull())
120 return std::nullopt;
122 return Utils::String::toEnum<Enum>(jsonVal.toString(), {});
125 template <typename Enum>
126 Enum getEnum(const QJsonObject &jsonObj, const QString &key)
128 const QJsonValue jsonVal = jsonObj.value(key);
129 return Utils::String::toEnum<Enum>(jsonVal.toString(), {});
132 BitTorrent::AddTorrentParams parseAddTorrentParams(const QJsonObject &jsonObj)
134 BitTorrent::AddTorrentParams params;
135 params.category = jsonObj.value(PARAM_CATEGORY).toString();
136 params.tags = parseTagSet(jsonObj.value(PARAM_TAGS).toArray());
137 params.savePath = jsonObj.value(PARAM_SAVEPATH).toString();
138 params.addForced = (getEnum<BitTorrent::TorrentOperatingMode>(jsonObj, PARAM_OPERATINGMODE) == BitTorrent::TorrentOperatingMode::Forced);
139 params.addPaused = getOptionalBool(jsonObj, PARAM_STOPPED);
140 params.contentLayout = getOptionalEnum<BitTorrent::TorrentContentLayout>(jsonObj, PARAM_CONTENTLAYOUT);
141 params.useAutoTMM = getOptionalBool(jsonObj, PARAM_AUTOTMM);
142 params.uploadLimit = jsonObj.value(PARAM_UPLOADLIMIT).toInt(-1);
143 params.downloadLimit = jsonObj.value(PARAM_DOWNLOADLIMIT).toInt(-1);
144 params.seedingTimeLimit = jsonObj.value(PARAM_SEEDINGTIMELIMIT).toInt(BitTorrent::Torrent::USE_GLOBAL_SEEDING_TIME);
145 params.ratioLimit = jsonObj.value(PARAM_RATIOLIMIT).toDouble(BitTorrent::Torrent::USE_GLOBAL_RATIO);
147 return params;
150 QJsonObject serializeAddTorrentParams(const BitTorrent::AddTorrentParams &params)
152 QJsonObject jsonObj {
153 {PARAM_CATEGORY, params.category},
154 {PARAM_TAGS, serializeTagSet(params.tags)},
155 {PARAM_SAVEPATH, params.savePath},
156 {PARAM_OPERATINGMODE, Utils::String::fromEnum(params.addForced
157 ? BitTorrent::TorrentOperatingMode::Forced : BitTorrent::TorrentOperatingMode::AutoManaged)},
158 {PARAM_UPLOADLIMIT, params.uploadLimit},
159 {PARAM_DOWNLOADLIMIT, params.downloadLimit},
160 {PARAM_SEEDINGTIMELIMIT, params.seedingTimeLimit},
161 {PARAM_RATIOLIMIT, params.ratioLimit}
164 if (params.addPaused)
165 jsonObj[PARAM_STOPPED] = *params.addPaused;
166 if (params.contentLayout)
167 jsonObj[PARAM_CONTENTLAYOUT] = Utils::String::fromEnum(*params.contentLayout);
168 if (params.useAutoTMM)
169 jsonObj[PARAM_AUTOTMM] = *params.useAutoTMM;
171 return jsonObj;
174 TorrentFilesWatcher::WatchedFolderOptions parseWatchedFolderOptions(const QJsonObject &jsonObj)
176 TorrentFilesWatcher::WatchedFolderOptions options;
177 options.addTorrentParams = parseAddTorrentParams(jsonObj.value(OPTION_ADDTORRENTPARAMS).toObject());
178 options.recursive = jsonObj.value(OPTION_RECURSIVE).toBool();
180 return options;
183 QJsonObject serializeWatchedFolderOptions(const TorrentFilesWatcher::WatchedFolderOptions &options)
185 return {
186 {OPTION_ADDTORRENTPARAMS, serializeAddTorrentParams(options.addTorrentParams)},
187 {OPTION_RECURSIVE, options.recursive}
192 class TorrentFilesWatcher::Worker final : public QObject
194 Q_OBJECT
195 Q_DISABLE_COPY_MOVE(Worker)
197 public:
198 Worker();
200 public slots:
201 void setWatchedFolder(const QString &path, const TorrentFilesWatcher::WatchedFolderOptions &options);
202 void removeWatchedFolder(const QString &path);
204 signals:
205 void magnetFound(const BitTorrent::MagnetUri &magnetURI, const BitTorrent::AddTorrentParams &addTorrentParams);
206 void torrentFound(const BitTorrent::TorrentInfo &torrentInfo, const BitTorrent::AddTorrentParams &addTorrentParams);
208 private:
209 void onTimeout();
210 void scheduleWatchedFolderProcessing(const QString &path);
211 void processWatchedFolder(const QString &path);
212 void processFolder(const QString &path, const QString &watchedFolderPath, const TorrentFilesWatcher::WatchedFolderOptions &options);
213 void processFailedTorrents();
214 void addWatchedFolder(const QString &watchedFolderID, const TorrentFilesWatcher::WatchedFolderOptions &options);
215 void updateWatchedFolder(const QString &watchedFolderID, const TorrentFilesWatcher::WatchedFolderOptions &options);
217 QFileSystemWatcher *m_watcher = nullptr;
218 QTimer *m_watchTimer = nullptr;
219 QHash<QString, TorrentFilesWatcher::WatchedFolderOptions> m_watchedFolders;
220 QSet<QString> m_watchedByTimeoutFolders;
222 // Failed torrents
223 QTimer *m_retryTorrentTimer = nullptr;
224 QHash<QString, QHash<QString, int>> m_failedTorrents;
227 TorrentFilesWatcher *TorrentFilesWatcher::m_instance = nullptr;
229 void TorrentFilesWatcher::initInstance()
231 if (!m_instance)
232 m_instance = new TorrentFilesWatcher;
235 void TorrentFilesWatcher::freeInstance()
237 delete m_instance;
238 m_instance = nullptr;
241 TorrentFilesWatcher *TorrentFilesWatcher::instance()
243 return m_instance;
246 TorrentFilesWatcher::TorrentFilesWatcher(QObject *parent)
247 : QObject {parent}
248 , m_ioThread {new QThread(this)}
249 , m_asyncWorker {new TorrentFilesWatcher::Worker}
251 connect(m_asyncWorker, &TorrentFilesWatcher::Worker::magnetFound, this, &TorrentFilesWatcher::onMagnetFound);
252 connect(m_asyncWorker, &TorrentFilesWatcher::Worker::torrentFound, this, &TorrentFilesWatcher::onTorrentFound);
254 m_asyncWorker->moveToThread(m_ioThread);
255 m_ioThread->start();
257 load();
260 TorrentFilesWatcher::~TorrentFilesWatcher()
262 m_ioThread->quit();
263 m_ioThread->wait();
264 delete m_asyncWorker;
267 QString TorrentFilesWatcher::makeCleanPath(const QString &path)
269 if (path.isEmpty())
270 throw InvalidArgument(tr("Watched folder path cannot be empty."));
272 if (QDir::isRelativePath(path))
273 throw InvalidArgument(tr("Watched folder path cannot be relative."));
275 return QDir::cleanPath(path);
278 void TorrentFilesWatcher::load()
280 QFile confFile {QDir(specialFolderLocation(SpecialFolder::Config)).absoluteFilePath(CONF_FILE_NAME)};
281 if (!confFile.exists())
283 loadLegacy();
284 return;
287 if (!confFile.open(QFile::ReadOnly))
289 LogMsg(tr("Couldn't load Watched Folders configuration from %1. Error: %2")
290 .arg(confFile.fileName(), confFile.errorString()), Log::WARNING);
291 return;
294 QJsonParseError jsonError;
295 const QJsonDocument jsonDoc = QJsonDocument::fromJson(confFile.readAll(), &jsonError);
296 if (jsonError.error != QJsonParseError::NoError)
298 LogMsg(tr("Couldn't parse Watched Folders configuration from %1. Error: %2")
299 .arg(confFile.fileName(), jsonError.errorString()), Log::WARNING);
300 return;
303 if (!jsonDoc.isObject())
305 LogMsg(tr("Couldn't load Watched Folders configuration from %1. Invalid data format.")
306 .arg(confFile.fileName()), Log::WARNING);
307 return;
310 const QJsonObject jsonObj = jsonDoc.object();
311 for (auto it = jsonObj.constBegin(); it != jsonObj.constEnd(); ++it)
313 const QString &watchedFolder = it.key();
314 const WatchedFolderOptions options = parseWatchedFolderOptions(it.value().toObject());
317 doSetWatchedFolder(watchedFolder, options);
319 catch (const InvalidArgument &err)
321 LogMsg(err.message(), Log::WARNING);
326 void TorrentFilesWatcher::loadLegacy()
328 const auto dirs = SettingsStorage::instance()->loadValue<QVariantHash>("Preferences/Downloads/ScanDirsV2");
330 for (auto i = dirs.cbegin(); i != dirs.cend(); ++i)
332 const QString watchedFolder = i.key();
333 BitTorrent::AddTorrentParams params;
334 if (i.value().type() == QVariant::Int)
336 if (i.value().toInt() == 0)
338 params.savePath = watchedFolder;
339 params.useAutoTMM = false;
342 else
344 const QString customSavePath = i.value().toString();
345 params.savePath = customSavePath;
346 params.useAutoTMM = false;
351 doSetWatchedFolder(watchedFolder, {params, false});
353 catch (const InvalidArgument &err)
355 LogMsg(err.message(), Log::WARNING);
359 store();
360 SettingsStorage::instance()->removeValue("Preferences/Downloads/ScanDirsV2");
363 void TorrentFilesWatcher::store() const
365 QJsonObject jsonObj;
366 for (auto it = m_watchedFolders.cbegin(); it != m_watchedFolders.cend(); ++it)
368 const QString &watchedFolder = it.key();
369 const WatchedFolderOptions &options = it.value();
370 jsonObj[watchedFolder] = serializeWatchedFolderOptions(options);
373 const QByteArray data = QJsonDocument(jsonObj).toJson();
375 QSaveFile confFile {QDir(specialFolderLocation(SpecialFolder::Config)).absoluteFilePath(CONF_FILE_NAME)};
376 if (!confFile.open(QIODevice::WriteOnly) || (confFile.write(data) != data.size()) || !confFile.commit())
378 LogMsg(tr("Couldn't store Watched Folders configuration to %1. Error: %2")
379 .arg(confFile.fileName(), confFile.errorString()), Log::WARNING);
383 QHash<QString, TorrentFilesWatcher::WatchedFolderOptions> TorrentFilesWatcher::folders() const
385 return m_watchedFolders;
388 void TorrentFilesWatcher::setWatchedFolder(const QString &path, const WatchedFolderOptions &options)
390 doSetWatchedFolder(path, options);
391 store();
394 void TorrentFilesWatcher::doSetWatchedFolder(const QString &path, const WatchedFolderOptions &options)
396 const QString cleanPath = makeCleanPath(path);
397 m_watchedFolders[cleanPath] = options;
399 QMetaObject::invokeMethod(m_asyncWorker, [this, path, options]()
401 m_asyncWorker->setWatchedFolder(path, options);
404 emit watchedFolderSet(cleanPath, options);
407 void TorrentFilesWatcher::removeWatchedFolder(const QString &path)
409 const QString cleanPath = makeCleanPath(path);
410 if (m_watchedFolders.remove(cleanPath))
412 QMetaObject::invokeMethod(m_asyncWorker, [this, cleanPath]()
414 m_asyncWorker->removeWatchedFolder(cleanPath);
417 emit watchedFolderRemoved(cleanPath);
419 store();
423 void TorrentFilesWatcher::onMagnetFound(const BitTorrent::MagnetUri &magnetURI
424 , const BitTorrent::AddTorrentParams &addTorrentParams)
426 BitTorrent::Session::instance()->addTorrent(magnetURI, addTorrentParams);
429 void TorrentFilesWatcher::onTorrentFound(const BitTorrent::TorrentInfo &torrentInfo
430 , const BitTorrent::AddTorrentParams &addTorrentParams)
432 BitTorrent::Session::instance()->addTorrent(torrentInfo, addTorrentParams);
435 TorrentFilesWatcher::Worker::Worker()
436 : m_watcher {new QFileSystemWatcher(this)}
437 , m_watchTimer {new QTimer(this)}
438 , m_retryTorrentTimer {new QTimer(this)}
440 connect(m_watcher, &QFileSystemWatcher::directoryChanged, this, &Worker::scheduleWatchedFolderProcessing);
441 connect(m_watchTimer, &QTimer::timeout, this, &Worker::onTimeout);
443 connect(m_retryTorrentTimer, &QTimer::timeout, this, &Worker::processFailedTorrents);
446 void TorrentFilesWatcher::Worker::onTimeout()
448 for (const QString &path : asConst(m_watchedByTimeoutFolders))
449 processWatchedFolder(path);
452 void TorrentFilesWatcher::Worker::setWatchedFolder(const QString &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
454 if (m_watchedFolders.contains(path))
455 updateWatchedFolder(path, options);
456 else
457 addWatchedFolder(path, options);
460 void TorrentFilesWatcher::Worker::removeWatchedFolder(const QString &path)
462 m_watchedFolders.remove(path);
464 m_watcher->removePath(path);
465 m_watchedByTimeoutFolders.remove(path);
466 if (m_watchedByTimeoutFolders.isEmpty())
467 m_watchTimer->stop();
469 m_failedTorrents.remove(path);
470 if (m_failedTorrents.isEmpty())
471 m_retryTorrentTimer->stop();
474 void TorrentFilesWatcher::Worker::scheduleWatchedFolderProcessing(const QString &path)
476 QTimer::singleShot(2000, this, [this, path]()
478 processWatchedFolder(path);
482 void TorrentFilesWatcher::Worker::processWatchedFolder(const QString &path)
484 const TorrentFilesWatcher::WatchedFolderOptions options = m_watchedFolders.value(path);
485 processFolder(path, path, options);
487 if (!m_failedTorrents.empty() && !m_retryTorrentTimer->isActive())
488 m_retryTorrentTimer->start(WATCH_INTERVAL);
491 void TorrentFilesWatcher::Worker::processFolder(const QString &path, const QString &watchedFolderPath
492 , const TorrentFilesWatcher::WatchedFolderOptions &options)
494 const QDir watchedDir {watchedFolderPath};
496 QDirIterator dirIter {path, {"*.torrent", "*.magnet"}, QDir::Files};
497 while (dirIter.hasNext())
499 const QString filePath = dirIter.next();
500 BitTorrent::AddTorrentParams addTorrentParams = options.addTorrentParams;
501 if (path != watchedFolderPath)
503 const QString subdirPath = watchedDir.relativeFilePath(path);
504 addTorrentParams.savePath = QDir::cleanPath(QDir(addTorrentParams.savePath).filePath(subdirPath));
507 if (filePath.endsWith(QLatin1String(".magnet"), Qt::CaseInsensitive))
509 QFile file {filePath};
510 if (file.open(QIODevice::ReadOnly | QIODevice::Text))
512 QTextStream str {&file};
513 while (!str.atEnd())
514 emit magnetFound(BitTorrent::MagnetUri(str.readLine()), addTorrentParams);
516 file.close();
517 Utils::Fs::forceRemove(filePath);
519 else
521 LogMsg(tr("Failed to open magnet file: %1").arg(file.errorString()));
524 else
526 const auto torrentInfo = BitTorrent::TorrentInfo::loadFromFile(filePath);
527 if (torrentInfo.isValid())
529 emit torrentFound(torrentInfo, addTorrentParams);
530 Utils::Fs::forceRemove(filePath);
532 else
534 if (!m_failedTorrents.value(path).contains(filePath))
536 m_failedTorrents[path][filePath] = 0;
542 if (options.recursive)
544 QDirIterator dirIter {path, (QDir::Dirs | QDir::NoDot | QDir::NoDotDot)};
545 while (dirIter.hasNext())
547 const QString folderPath = dirIter.next();
548 // Skip processing of subdirectory that is explicitly set as watched folder
549 if (!m_watchedFolders.contains(folderPath))
550 processFolder(folderPath, watchedFolderPath, options);
555 void TorrentFilesWatcher::Worker::processFailedTorrents()
557 // Check which torrents are still partial
558 Algorithm::removeIf(m_failedTorrents, [this](const QString &watchedFolderPath, QHash<QString, int> &partialTorrents)
560 const QDir dir {watchedFolderPath};
561 const TorrentFilesWatcher::WatchedFolderOptions options = m_watchedFolders.value(watchedFolderPath);
562 Algorithm::removeIf(partialTorrents, [this, &dir, &options](const QString &torrentPath, int &value)
564 if (!QFile::exists(torrentPath))
565 return true;
567 const auto torrentInfo = BitTorrent::TorrentInfo::loadFromFile(torrentPath);
568 if (torrentInfo.isValid())
570 BitTorrent::AddTorrentParams addTorrentParams = options.addTorrentParams;
571 const QString exactDirPath = QFileInfo(torrentPath).canonicalPath();
572 if (exactDirPath != dir.path())
574 const QString subdirPath = dir.relativeFilePath(exactDirPath);
575 addTorrentParams.savePath = QDir(addTorrentParams.savePath).filePath(subdirPath);
578 emit torrentFound(torrentInfo, addTorrentParams);
579 Utils::Fs::forceRemove(torrentPath);
581 return true;
584 if (value >= MAX_FAILED_RETRIES)
586 LogMsg(tr("Rejecting failed torrent file: %1").arg(torrentPath));
587 QFile::rename(torrentPath, torrentPath + ".qbt_rejected");
588 return true;
591 ++value;
592 return false;
595 if (partialTorrents.isEmpty())
596 return true;
598 return false;
601 // Stop the partial timer if necessary
602 if (m_failedTorrents.empty())
603 m_retryTorrentTimer->stop();
604 else
605 m_retryTorrentTimer->start(WATCH_INTERVAL);
608 void TorrentFilesWatcher::Worker::addWatchedFolder(const QString &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
610 #if !defined Q_OS_HAIKU
611 // Check if the path points to a network file system or not
612 if (Utils::Fs::isNetworkFileSystem(path) || options.recursive)
613 #else
614 if (options.recursive)
615 #endif
617 m_watchedByTimeoutFolders.insert(path);
618 if (!m_watchTimer->isActive())
619 m_watchTimer->start(WATCH_INTERVAL);
621 else
623 m_watcher->addPath(path);
624 scheduleWatchedFolderProcessing(path);
627 m_watchedFolders[path] = options;
629 LogMsg(tr("Watching folder: \"%1\"").arg(Utils::Fs::toNativePath(path)));
632 void TorrentFilesWatcher::Worker::updateWatchedFolder(const QString &path, const TorrentFilesWatcher::WatchedFolderOptions &options)
634 const bool recursiveModeChanged = (m_watchedFolders[path].recursive != options.recursive);
635 #if !defined Q_OS_HAIKU
636 if (recursiveModeChanged && !Utils::Fs::isNetworkFileSystem(path))
637 #else
638 if (recursiveModeChanged)
639 #endif
641 if (options.recursive)
643 m_watcher->removePath(path);
645 m_watchedByTimeoutFolders.insert(path);
646 if (!m_watchTimer->isActive())
647 m_watchTimer->start(WATCH_INTERVAL);
649 else
651 m_watchedByTimeoutFolders.remove(path);
652 if (m_watchedByTimeoutFolders.isEmpty())
653 m_watchTimer->stop();
655 m_watcher->addPath(path);
656 scheduleWatchedFolderProcessing(path);
660 m_watchedFolders[path] = options;
663 #include "torrentfileswatcher.moc"