From: Mike Gelfand Date: Mon, 1 Dec 2014 19:24:07 +0000 (+0000) Subject: #5077: Remove torrent file from watch directory even if "show options dialog" is... X-Git-Tag: 2.90~343 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=17769e2e2aba4a58cf297f12f49c5501cf64629f;p=transmission #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}. --- diff --git a/qt/options.cc b/qt/options.cc index b0669995d..aa425f563 100644 --- a/qt/options.cc +++ b/qt/options.cc @@ -9,7 +9,6 @@ #include // std::min() -#include #include #include #include @@ -19,7 +18,6 @@ #include #include #include -#include #include #include #include @@ -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%1

%2

").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 (); diff --git a/qt/options.h b/qt/options.h index 0f4a2bf0d..169ec98c9 100644 --- a/qt/options.h +++ b/qt/options.h @@ -10,8 +10,6 @@ #ifndef OPTIONS_DIALOG_H #define OPTIONS_DIALOG_H -#include - #include #include #include @@ -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 diff --git a/qt/session.cc b/qt/session.cc index 5492e08b5..ff09020a3 100644 --- a/qt/session.cc +++ b/qt/session.cc @@ -15,6 +15,7 @@ #include #include #include +#include #include #include #include @@ -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%1

%2

").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); } diff --git a/qt/session.h b/qt/session.h index 842026343..3452af60d 100644 --- a/qt/session.h +++ b/qt/session.h @@ -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& ids, const tr_quark key, const QPair& val); void torrentSetLocation (const QSet& ids, const QString& path, bool doMove); void torrentRenamePath (const QSet& ids, const QString& oldpath, const QString& newname); + void addTorrent (const AddData& addme, tr_variant& top, bool trashOriginal); public slots: void pauseTorrents (const QSet& torrentIds = QSet ());