Changeset 14349 for trunk/qt/session.cc


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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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}
Note: See TracChangeset for help on using the changeset viewer.