#include <algorithm> // std::min()
-#include <QApplication>
#include <QCheckBox>
#include <QComboBox>
#include <QDialogButtonBox>
#include <QFileInfo>
#include <QGridLayout>
#include <QLabel>
-#include <QMessageBox>
#include <QPushButton>
#include <QResizeEvent>
#include <QSet>
****
***/
-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),
{
// 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;
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 ());
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 ();
#ifndef OPTIONS_DIALOG_H
#define OPTIONS_DIALOG_H
-#include <iostream>
-
#include <QDialog>
#include <QEvent>
#include <QString>
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
#include <QClipboard>
#include <QCoreApplication>
#include <QDesktopServices>
+#include <QFile>
#include <QMessageBox>
#include <QNetworkProxy>
#include <QNetworkProxyFactory>
****
***/
+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)
{
}
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)
{
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);
}
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
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> ());