Changeset 14349


Ignore:
Timestamp:
Dec 1, 2014, 7:24:07 PM (8 years ago)
Author:
mikedld
Message:

#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}.

Location:
trunk/qt
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/options.cc

    r14225 r14349  
    1010#include <algorithm> // std::min()
    1111
    12 #include <QApplication>
    1312#include <QCheckBox>
    1413#include <QComboBox>
     
    2019#include <QGridLayout>
    2120#include <QLabel>
    22 #include <QMessageBox>
    2321#include <QPushButton>
    2422#include <QResizeEvent>
     
    4139#include "torrent.h"
    4240#include "utils.h"
    43 
    44 /***
    45 ****
    46 ***/
    47 
    48 void
    49 FileAdded :: executed (int64_t tag, const QString& result, struct tr_variant * arguments)
    50 {
    51   Q_UNUSED (arguments);
    52 
    53   if (tag != myTag)
    54     return;
    55 
    56   if ((result == "success") && !myDelFile.isEmpty ())
    57     {
    58       QFile file (myDelFile);
    59       file.setPermissions (QFile::ReadOwner | QFile::WriteOwner);
    60       file.remove ();
    61     }
    62 
    63   if (result != "success")
    64     {
    65       QString text = result;
    66 
    67       for (int i=0, n=text.size (); i<n; ++i)
    68         if (!i || text[i-1].isSpace ())
    69           text[i] = text[i].toUpper ();
    70 
    71       QMessageBox::warning (QApplication::activeWindow (),
    72                             tr ("Error Adding Torrent"),
    73                             QString ("<p><b>%1</b></p><p>%2</p>").arg (text).arg (myName));
    74     }
    75 
    76   deleteLater ();
    77 }
    7841
    7942/***
     
    375338  // rpc spec section 3.4 "adding a torrent"
    376339
    377   const int64_t tag = mySession.getUniqueTag ();
    378340  tr_variant top;
    379341  tr_variantInitDict (&top, 3);
    380   tr_variantDictAddStr (&top, TR_KEY_method, "torrent-add");
    381   tr_variantDictAddInt (&top, TR_KEY_tag, tag);
    382342  tr_variant * args (tr_variantDictAddDict (&top, TR_KEY_arguments, 10));
    383343  QString downloadDir;
     
    391351  tr_variantDictAddStr (args, TR_KEY_download_dir, downloadDir.toUtf8 ().constData ());
    392352
    393   // "metainfo"
    394   switch (myAdd.type)
    395     {
    396       case AddData::MAGNET:
    397         tr_variantDictAddStr (args, TR_KEY_filename, myAdd.magnet.toUtf8 ().constData ());
    398         break;
    399 
    400       case AddData::URL:
    401         tr_variantDictAddStr (args, TR_KEY_filename, myAdd.url.toString ().toUtf8 ().constData ());
    402         break;
    403 
    404       case AddData::FILENAME:
    405       case AddData::METAINFO: {
    406         const QByteArray b64 = myAdd.toBase64 ();
    407         tr_variantDictAddRaw (args, TR_KEY_metainfo, b64.constData (), b64.size ());
    408         break;
    409       }
    410 
    411       default:
    412         qWarning ("unhandled AddData.type: %d", myAdd.type);
    413     }
    414 
    415353  // paused
    416354  tr_variantDictAddBool (args, TR_KEY_paused, !myStartCheck->isChecked ());
     
    451389    }
    452390
    453   // maybe delete the source .torrent
    454   FileAdded * fileAdded = new FileAdded (tag, myAdd.readableName ());
    455   if (myTrashCheck->isChecked () && (myAdd.type==AddData::FILENAME))
    456     fileAdded->setFileToDelete (myAdd.filename);
    457   connect (&mySession, SIGNAL (executed (int64_t,const QString&, struct tr_variant*)),
    458            fileAdded, SLOT (executed (int64_t,const QString&, struct tr_variant*)));
    459 
    460   mySession.exec (&top);
     391  mySession.addTorrent (myAdd, top, myTrashCheck->isChecked ());
    461392
    462393  tr_variantFree (&top);
  • trunk/qt/options.h

    r14241 r14349  
    1010#ifndef OPTIONS_DIALOG_H
    1111#define OPTIONS_DIALOG_H
    12 
    13 #include <iostream>
    1412
    1513#include <QDialog>
     
    4240  struct tr_variant;
    4341}
    44 
    45 class FileAdded: public QObject
    46 {
    47     Q_OBJECT
    48 
    49   public:
    50     FileAdded (int tag, const QString& name): myTag (tag), myName (name) {}
    51     ~FileAdded () {}
    52     void setFileToDelete (const QString& file) { myDelFile = file; }
    53 
    54   public slots:
    55     void executed (int64_t tag, const QString& result, struct tr_variant * arguments);
    56 
    57   private:
    58     const int64_t myTag;
    59     QString myName;
    60     QString myDelFile;
    61 };
    6242
    6343class Options: public QDialog
  • trunk/qt/session.cc

    r14225 r14349  
    1616#include <QCoreApplication>
    1717#include <QDesktopServices>
     18#include <QFile>
    1819#include <QMessageBox>
    1920#include <QNetworkProxy>
     
    8283      tr_variantListAddQuark (list, key);
    8384  }
     85}
     86
     87/***
     88****
     89***/
     90
     91void
     92FileAdded :: executed (int64_t tag, const QString& result, struct tr_variant * arguments)
     93{
     94  Q_UNUSED (arguments);
     95
     96  if (tag != myTag)
     97    return;
     98
     99  if (result == "success")
     100    {
     101      if (!myDelFile.isEmpty ())
     102        {
     103          QFile file (myDelFile);
     104          file.setPermissions (QFile::ReadOwner | QFile::WriteOwner);
     105          file.remove ();
     106        }
     107    }
     108  else
     109    {
     110      QString text = result;
     111
     112      for (int i=0, n=text.size (); i<n; ++i)
     113        if (!i || text[i-1].isSpace ())
     114          text[i] = text[i].toUpper ();
     115
     116      QMessageBox::warning (QApplication::activeWindow (),
     117                            tr ("Error Adding Torrent"),
     118                            QString ("<p><b>%1</b></p><p>%2</p>").arg (text).arg (myName));
     119    }
     120
     121  deleteLater ();
    84122}
    85123
     
    10221060
    10231061void
    1024 Session :: addTorrent (const AddData& addMe)
    1025 {
    1026   const QByteArray b64 = addMe.toBase64 ();
    1027 
    1028   tr_variant top, *args;
    1029   tr_variantInitDict (&top, 2);
     1062Session :: addTorrent (const AddData& addMe, tr_variant& top, bool trashOriginal)
     1063{
     1064  assert (tr_variantDictFind (&top, TR_KEY_method) == nullptr);
     1065  assert (tr_variantDictFind (&top, TR_KEY_tag) == nullptr);
     1066
    10301067  tr_variantDictAddStr (&top, TR_KEY_method, "torrent-add");
    1031   args = tr_variantDictAddDict (&top, TR_KEY_arguments, 2);
    1032   tr_variantDictAddBool (args, TR_KEY_paused, !myPrefs.getBool (Prefs::START));
     1068
     1069  const int64_t tag = getUniqueTag ();
     1070  tr_variantDictAddInt (&top, TR_KEY_tag, tag);
     1071
     1072  tr_variant * args;
     1073  if (!tr_variantDictFindDict (&top, TR_KEY_arguments, &args))
     1074    args = tr_variantDictAddDict (&top, TR_KEY_arguments, 2);
     1075
     1076  assert (tr_variantDictFind (args, TR_KEY_filename) == nullptr);
     1077  assert (tr_variantDictFind (args, TR_KEY_metainfo) == nullptr);
     1078
     1079  if (tr_variantDictFind (args, TR_KEY_paused) == nullptr)
     1080    tr_variantDictAddBool (args, TR_KEY_paused, !myPrefs.getBool (Prefs::START));
    10331081
    10341082  switch (addMe.type)
     
    10441092      case AddData::FILENAME: /* fall-through */
    10451093      case AddData::METAINFO:
    1046         tr_variantDictAddRaw (args, TR_KEY_metainfo, b64.constData (), b64.size ());
    1047         break;
     1094        {
     1095          const QByteArray b64 = addMe.toBase64 ();
     1096          tr_variantDictAddRaw (args, TR_KEY_metainfo, b64.constData (), b64.size ());
     1097          break;
     1098        }
    10481099
    10491100      default:
    1050         std::cerr << "Unhandled AddData type: " << addMe.type << std::endl;
    1051         break;
    1052     }
    1053 
    1054   exec (&top);
     1101        qWarning() << "Unhandled AddData type: " << addMe.type;
     1102        break;
     1103    }
     1104
     1105  // maybe delete the source .torrent
     1106  FileAdded * fileAdded = new FileAdded (tag, addMe.readableName ());
     1107  if (trashOriginal && addMe.type == AddData::FILENAME)
     1108    fileAdded->setFileToDelete (addMe.filename);
     1109  connect (this, SIGNAL (executed (int64_t, QString, struct tr_variant *)),
     1110           fileAdded, SLOT (executed (int64_t, QString, struct tr_variant *)));
     1111
     1112  exec (&top);
     1113}
     1114
     1115void
     1116Session :: addTorrent (const AddData& addMe)
     1117{
     1118  tr_variant top;
     1119  tr_variantInitDict (&top, 3);
     1120
     1121  addTorrent (addMe, top, myPrefs.getBool (Prefs::TRASH_ORIGINAL));
     1122
    10551123  tr_variantFree (&top);
    10561124}
  • trunk/qt/session.h

    r14241 r14349  
    3535
    3636class Prefs;
     37
     38class FileAdded: public QObject
     39{
     40    Q_OBJECT
     41
     42  public:
     43    FileAdded (int tag, const QString& name): myTag (tag), myName (name) {}
     44    ~FileAdded () {}
     45    void setFileToDelete (const QString& file) { myDelFile = file; }
     46
     47  public slots:
     48    void executed (int64_t tag, const QString& result, struct tr_variant * arguments);
     49
     50  private:
     51    const int64_t myTag;
     52    const QString myName;
     53    QString myDelFile;
     54};
    3755
    3856class Session: public QObject
     
    102120    void torrentSetLocation (const QSet<int>& ids, const QString& path, bool doMove);
    103121    void torrentRenamePath (const QSet<int>& ids, const QString& oldpath, const QString& newname);
     122    void addTorrent (const AddData& addme, tr_variant& top, bool trashOriginal);
    104123
    105124  public slots:
Note: See TracChangeset for help on using the changeset viewer.