#5077: Remove torrent file from watch directory even if "show options dialog" is not set (patch from rb07 + some improvements)

Refactor Session::addTorrent (add new method) to eliminate duplicate
code in options.cc and ensure that FileAdded object is being created
on torrent addition even with non-interactive workflow.
Move FileAdded class from options.{h,cc} to session.{h,cc}.
This commit is contained in:
Mike Gelfand 2014-12-01 19:24:07 +00:00
parent 13cad2fb55
commit 17769e2e2a
4 changed files with 97 additions and 99 deletions

View File

@ -9,7 +9,6 @@
#include <algorithm> // std::min()
#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QDialogButtonBox>
@ -19,7 +18,6 @@
#include <QFileInfo>
#include <QGridLayout>
#include <QLabel>
#include <QMessageBox>
#include <QPushButton>
#include <QResizeEvent>
#include <QSet>
@ -45,41 +43,6 @@
****
***/
void
FileAdded :: executed (int64_t tag, const QString& result, struct tr_variant * arguments)
{
Q_UNUSED (arguments);
if (tag != myTag)
return;
if ((result == "success") && !myDelFile.isEmpty ())
{
QFile file (myDelFile);
file.setPermissions (QFile::ReadOwner | QFile::WriteOwner);
file.remove ();
}
if (result != "success")
{
QString text = result;
for (int i=0, n=text.size (); i<n; ++i)
if (!i || text[i-1].isSpace ())
text[i] = text[i].toUpper ();
QMessageBox::warning (QApplication::activeWindow (),
tr ("Error Adding Torrent"),
QString ("<p><b>%1</b></p><p>%2</p>").arg (text).arg (myName));
}
deleteLater ();
}
/***
****
***/
Options :: Options (Session& session, const Prefs& prefs, const AddData& addme, QWidget * parent):
QDialog (parent, Qt::Dialog),
mySession (session),
@ -374,11 +337,8 @@ Options :: onAccepted ()
{
// rpc spec section 3.4 "adding a torrent"
const int64_t tag = mySession.getUniqueTag ();
tr_variant top;
tr_variantInitDict (&top, 3);
tr_variantDictAddStr (&top, TR_KEY_method, "torrent-add");
tr_variantDictAddInt (&top, TR_KEY_tag, tag);
tr_variant * args (tr_variantDictAddDict (&top, TR_KEY_arguments, 10));
QString downloadDir;
@ -390,28 +350,6 @@ Options :: onAccepted ()
tr_variantDictAddStr (args, TR_KEY_download_dir, downloadDir.toUtf8 ().constData ());
// "metainfo"
switch (myAdd.type)
{
case AddData::MAGNET:
tr_variantDictAddStr (args, TR_KEY_filename, myAdd.magnet.toUtf8 ().constData ());
break;
case AddData::URL:
tr_variantDictAddStr (args, TR_KEY_filename, myAdd.url.toString ().toUtf8 ().constData ());
break;
case AddData::FILENAME:
case AddData::METAINFO: {
const QByteArray b64 = myAdd.toBase64 ();
tr_variantDictAddRaw (args, TR_KEY_metainfo, b64.constData (), b64.size ());
break;
}
default:
qWarning ("unhandled AddData.type: %d", myAdd.type);
}
// paused
tr_variantDictAddBool (args, TR_KEY_paused, !myStartCheck->isChecked ());
@ -450,14 +388,7 @@ Options :: onAccepted ()
tr_variantListAddInt (l, i);
}
// maybe delete the source .torrent
FileAdded * fileAdded = new FileAdded (tag, myAdd.readableName ());
if (myTrashCheck->isChecked () && (myAdd.type==AddData::FILENAME))
fileAdded->setFileToDelete (myAdd.filename);
connect (&mySession, SIGNAL (executed (int64_t,const QString&, struct tr_variant*)),
fileAdded, SLOT (executed (int64_t,const QString&, struct tr_variant*)));
mySession.exec (&top);
mySession.addTorrent (myAdd, top, myTrashCheck->isChecked ());
tr_variantFree (&top);
deleteLater ();

View File

@ -10,8 +10,6 @@
#ifndef OPTIONS_DIALOG_H
#define OPTIONS_DIALOG_H
#include <iostream>
#include <QDialog>
#include <QEvent>
#include <QString>
@ -42,24 +40,6 @@ extern "C"
struct tr_variant;
}
class FileAdded: public QObject
{
Q_OBJECT
public:
FileAdded (int tag, const QString& name): myTag (tag), myName (name) {}
~FileAdded () {}
void setFileToDelete (const QString& file) { myDelFile = file; }
public slots:
void executed (int64_t tag, const QString& result, struct tr_variant * arguments);
private:
const int64_t myTag;
QString myName;
QString myDelFile;
};
class Options: public QDialog
{
Q_OBJECT

View File

@ -15,6 +15,7 @@
#include <QClipboard>
#include <QCoreApplication>
#include <QDesktopServices>
#include <QFile>
#include <QMessageBox>
#include <QNetworkProxy>
#include <QNetworkProxyFactory>
@ -87,6 +88,43 @@ namespace
****
***/
void
FileAdded :: executed (int64_t tag, const QString& result, struct tr_variant * arguments)
{
Q_UNUSED (arguments);
if (tag != myTag)
return;
if (result == "success")
{
if (!myDelFile.isEmpty ())
{
QFile file (myDelFile);
file.setPermissions (QFile::ReadOwner | QFile::WriteOwner);
file.remove ();
}
}
else
{
QString text = result;
for (int i=0, n=text.size (); i<n; ++i)
if (!i || text[i-1].isSpace ())
text[i] = text[i].toUpper ();
QMessageBox::warning (QApplication::activeWindow (),
tr ("Error Adding Torrent"),
QString ("<p><b>%1</b></p><p>%2</p>").arg (text).arg (myName));
}
deleteLater ();
}
/***
****
***/
void
Session :: sessionSet (const tr_quark key, const QVariant& value)
{
@ -1021,15 +1059,25 @@ Session :: setBlocklistSize (int64_t i)
}
void
Session :: addTorrent (const AddData& addMe)
Session :: addTorrent (const AddData& addMe, tr_variant& top, bool trashOriginal)
{
const QByteArray b64 = addMe.toBase64 ();
assert (tr_variantDictFind (&top, TR_KEY_method) == nullptr);
assert (tr_variantDictFind (&top, TR_KEY_tag) == nullptr);
tr_variant top, *args;
tr_variantInitDict (&top, 2);
tr_variantDictAddStr (&top, TR_KEY_method, "torrent-add");
args = tr_variantDictAddDict (&top, TR_KEY_arguments, 2);
tr_variantDictAddBool (args, TR_KEY_paused, !myPrefs.getBool (Prefs::START));
const int64_t tag = getUniqueTag ();
tr_variantDictAddInt (&top, TR_KEY_tag, tag);
tr_variant * args;
if (!tr_variantDictFindDict (&top, TR_KEY_arguments, &args))
args = tr_variantDictAddDict (&top, TR_KEY_arguments, 2);
assert (tr_variantDictFind (args, TR_KEY_filename) == nullptr);
assert (tr_variantDictFind (args, TR_KEY_metainfo) == nullptr);
if (tr_variantDictFind (args, TR_KEY_paused) == nullptr)
tr_variantDictAddBool (args, TR_KEY_paused, !myPrefs.getBool (Prefs::START));
switch (addMe.type)
{
@ -1043,15 +1091,35 @@ Session :: addTorrent (const AddData& addMe)
case AddData::FILENAME: /* fall-through */
case AddData::METAINFO:
tr_variantDictAddRaw (args, TR_KEY_metainfo, b64.constData (), b64.size ());
break;
{
const QByteArray b64 = addMe.toBase64 ();
tr_variantDictAddRaw (args, TR_KEY_metainfo, b64.constData (), b64.size ());
break;
}
default:
std::cerr << "Unhandled AddData type: " << addMe.type << std::endl;
qWarning() << "Unhandled AddData type: " << addMe.type;
break;
}
// maybe delete the source .torrent
FileAdded * fileAdded = new FileAdded (tag, addMe.readableName ());
if (trashOriginal && addMe.type == AddData::FILENAME)
fileAdded->setFileToDelete (addMe.filename);
connect (this, SIGNAL (executed (int64_t, QString, struct tr_variant *)),
fileAdded, SLOT (executed (int64_t, QString, struct tr_variant *)));
exec (&top);
}
void
Session :: addTorrent (const AddData& addMe)
{
tr_variant top;
tr_variantInitDict (&top, 3);
addTorrent (addMe, top, myPrefs.getBool (Prefs::TRASH_ORIGINAL));
tr_variantFree (&top);
}

View File

@ -35,6 +35,24 @@ extern "C"
class Prefs;
class FileAdded: public QObject
{
Q_OBJECT
public:
FileAdded (int tag, const QString& name): myTag (tag), myName (name) {}
~FileAdded () {}
void setFileToDelete (const QString& file) { myDelFile = file; }
public slots:
void executed (int64_t tag, const QString& result, struct tr_variant * arguments);
private:
const int64_t myTag;
const QString myName;
QString myDelFile;
};
class Session: public QObject
{
Q_OBJECT
@ -101,6 +119,7 @@ class Session: public QObject
void torrentSet (const QSet<int>& ids, const tr_quark key, const QPair<int,QString>& val);
void torrentSetLocation (const QSet<int>& ids, const QString& path, bool doMove);
void torrentRenamePath (const QSet<int>& ids, const QString& oldpath, const QString& newname);
void addTorrent (const AddData& addme, tr_variant& top, bool trashOriginal);
public slots:
void pauseTorrents (const QSet<int>& torrentIds = QSet<int> ());