From: Mike Gelfand
%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%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