2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2015-06-10 21:27:11 +00:00
|
|
|
* This file Copyright (C) 2009-2015 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
2014-12-21 23:49:39 +00:00
|
|
|
* It may be used under the GNU GPL versions 2 or 3
|
2014-01-19 01:09:44 +00:00
|
|
|
* or any future license endorsed by Mnemosyne LLC.
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QDir>
|
|
|
|
#include <QFileSystemWatcher>
|
2009-10-06 00:27:26 +00:00
|
|
|
#include <QTimer>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
|
2015-06-10 21:27:11 +00:00
|
|
|
#include "Prefs.h"
|
|
|
|
#include "TorrentModel.h"
|
|
|
|
#include "WatchDir.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
WatchDir::WatchDir(TorrentModel const& model) :
|
2020-05-27 21:53:12 +00:00
|
|
|
model_(model)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
int WatchDir::metainfoTest(QString const& filename) const
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
int ret;
|
|
|
|
tr_info inf;
|
2017-04-30 09:29:58 +00:00
|
|
|
tr_ctor* ctor = tr_ctorNew(nullptr);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// parse
|
|
|
|
tr_ctorSetMetainfoFromFile(ctor, filename.toUtf8().constData());
|
2017-04-20 16:02:19 +00:00
|
|
|
int const err = tr_torrentParse(ctor, &inf);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
2017-04-30 16:25:26 +00:00
|
|
|
if (err != 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ret = ERROR;
|
|
|
|
}
|
2020-09-07 21:19:10 +00:00
|
|
|
else if (model_.hasTorrent(TorrentHash(inf.hashString)))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
ret = DUPLICATE;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
ret = OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup
|
2017-04-30 16:25:26 +00:00
|
|
|
if (err == 0)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
tr_metainfoFree(&inf);
|
|
|
|
}
|
|
|
|
|
|
|
|
tr_ctorFree(ctor);
|
|
|
|
return ret;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void WatchDir::onTimeout()
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* t = qobject_cast<QTimer*>(sender());
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const filename = t->objectName();
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
if (metainfoTest(filename) == OK)
|
|
|
|
{
|
|
|
|
emit torrentFileAdded(filename);
|
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
t->deleteLater();
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
void WatchDir::setPath(QString const& path, bool is_enabled)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// clear out any remnants of the previous watcher, if any
|
2020-05-27 21:53:12 +00:00
|
|
|
watch_dir_files_.clear();
|
2020-07-29 23:21:11 +00:00
|
|
|
watcher_.reset();
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// maybe create a new watcher
|
2020-05-27 21:53:12 +00:00
|
|
|
if (is_enabled)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2020-07-29 23:21:11 +00:00
|
|
|
watcher_ = std::make_unique<QFileSystemWatcher>(QStringList{ path });
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(watcher_.get(), &QFileSystemWatcher::directoryChanged, this, &WatchDir::watcherActivated);
|
2017-04-19 12:04:45 +00:00
|
|
|
QTimer::singleShot(0, this, SLOT(rescanAllWatchedDirectories())); // trigger the watchdir for .torrent files in there already
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
void WatchDir::watcherActivated(QString const& path)
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QDir const dir(path);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
// get the list of files currently in the watch directory
|
|
|
|
QSet<QString> files;
|
|
|
|
|
2017-04-20 16:02:19 +00:00
|
|
|
for (QString const& str : dir.entryList(QDir::Readable | QDir::Files))
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
files.insert(str);
|
|
|
|
}
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// try to add any new files which end in .torrent
|
2020-10-31 18:56:12 +00:00
|
|
|
auto const new_files = files - watch_dir_files_;
|
2020-05-29 17:40:07 +00:00
|
|
|
auto const torrent_suffix = QStringLiteral(".torrent");
|
2013-09-14 22:45:04 +00:00
|
|
|
|
2020-05-27 21:53:12 +00:00
|
|
|
for (QString const& name : new_files)
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2020-05-27 21:53:12 +00:00
|
|
|
if (name.endsWith(torrent_suffix, Qt::CaseInsensitive))
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-20 16:02:19 +00:00
|
|
|
QString const filename = dir.absoluteFilePath(name);
|
2017-04-19 12:04:45 +00:00
|
|
|
|
|
|
|
switch (metainfoTest(filename))
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
case OK:
|
|
|
|
emit torrentFileAdded(filename);
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case DUPLICATE:
|
2013-09-14 22:45:04 +00:00
|
|
|
break;
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
case ERROR:
|
2013-09-14 22:45:04 +00:00
|
|
|
{
|
2017-04-19 12:04:45 +00:00
|
|
|
// give the .torrent a few seconds to finish downloading
|
2020-05-20 01:32:51 +00:00
|
|
|
auto* t = new QTimer(this);
|
2017-04-19 12:04:45 +00:00
|
|
|
t->setObjectName(dir.absoluteFilePath(name));
|
|
|
|
t->setSingleShot(true);
|
2020-10-31 18:56:12 +00:00
|
|
|
connect(t, &QTimer::timeout, this, &WatchDir::onTimeout);
|
2017-04-19 12:04:45 +00:00
|
|
|
t->start(5000);
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
// update our file list so that we can use it
|
|
|
|
// for comparison the next time around
|
2020-05-27 21:53:12 +00:00
|
|
|
watch_dir_files_ = files;
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
2014-12-11 22:21:24 +00:00
|
|
|
|
2017-04-19 12:04:45 +00:00
|
|
|
void WatchDir::rescanAllWatchedDirectories()
|
2014-12-11 22:21:24 +00:00
|
|
|
{
|
2020-07-29 23:21:11 +00:00
|
|
|
if (!watcher_)
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
2014-12-11 22:21:24 +00:00
|
|
|
|
2020-07-29 23:21:11 +00:00
|
|
|
for (auto const& path : watcher_->directories())
|
2017-04-19 12:04:45 +00:00
|
|
|
{
|
|
|
|
watcherActivated(path);
|
|
|
|
}
|
2014-12-11 22:21:24 +00:00
|
|
|
}
|