2009-04-09 18:55:47 +00:00
/*
2015-06-10 21:27:11 +00:00
* This file Copyright ( C ) 2009 - 2015 Mnemosyne LLC
2009-04-09 18:55:47 +00:00
*
2014-12-21 23:49:39 +00:00
* It may be used under the GNU GPL versions 2 or 3
2014-01-19 01:09:44 +00:00
* or any future license endorsed by Mnemosyne LLC .
2009-04-09 18:55:47 +00:00
*
2009-05-31 19:33:48 +00:00
* $ Id $
2009-04-09 18:55:47 +00:00
*/
# include <cassert>
# include <iostream>
# include <QApplication>
# include <QByteArray>
2009-12-03 15:23:43 +00:00
# include <QClipboard>
2009-04-09 18:55:47 +00:00
# include <QCoreApplication>
2014-12-27 20:08:10 +00:00
# include <QDebug>
2009-04-09 18:55:47 +00:00
# include <QDesktopServices>
2014-12-01 19:24:07 +00:00
# include <QFile>
2014-12-22 00:02:27 +00:00
# include <QFileInfo>
2009-04-09 18:55:47 +00:00
# include <QMessageBox>
# include <QStyle>
2009-04-12 21:15:35 +00:00
# include <QTextStream>
2009-04-09 18:55:47 +00:00
# include <libtransmission/transmission.h>
2010-12-11 21:27:15 +00:00
# include <libtransmission/utils.h> // tr_free
2012-12-14 04:34:42 +00:00
# include <libtransmission/variant.h>
2009-04-09 18:55:47 +00:00
2015-06-10 21:27:11 +00:00
# include "AddData.h"
# include "Prefs.h"
# include "Session.h"
# include "SessionDialog.h"
# include "Torrent.h"
# include "Utils.h"
2009-04-09 18:55:47 +00:00
namespace
{
2013-02-03 19:40:20 +00:00
enum
{
TAG_SOME_TORRENTS ,
TAG_ALL_TORRENTS ,
TAG_SESSION_STATS ,
TAG_SESSION_INFO ,
TAG_BLOCKLIST_UPDATE ,
TAG_ADD_TORRENT ,
TAG_PORT_TEST ,
TAG_MAGNET_LINK ,
TAG_RENAME_PATH ,
FIRST_UNIQUE_TAG
} ;
2009-04-09 18:55:47 +00:00
}
/***
* * * *
* * */
namespace
{
2012-12-22 20:35:19 +00:00
typedef Torrent : : KeyList KeyList ;
2013-02-03 19:40:20 +00:00
const KeyList & getInfoKeys ( ) { return Torrent : : getInfoKeys ( ) ; }
const KeyList & getStatKeys ( ) { return Torrent : : getStatKeys ( ) ; }
const KeyList & getExtraStatKeys ( ) { return Torrent : : getExtraStatKeys ( ) ; }
2012-12-22 20:35:19 +00:00
void
addList ( tr_variant * list , const KeyList & keys )
{
2013-02-03 19:40:20 +00:00
tr_variantListReserve ( list , keys . size ( ) ) ;
2015-04-18 14:41:06 +00:00
for ( const tr_quark key : keys )
2012-12-22 20:35:19 +00:00
tr_variantListAddQuark ( list , key ) ;
}
2009-04-09 18:55:47 +00:00
}
/***
* * * *
* * */
2014-12-01 19:24:07 +00:00
void
2014-12-12 23:21:04 +00:00
FileAdded : : executed ( int64_t tag , const QString & result , tr_variant * arguments )
2014-12-01 19:24:07 +00:00
{
if ( tag ! = myTag )
return ;
2015-01-29 21:53:05 +00:00
if ( result = = QLatin1String ( " success " ) )
2014-12-01 19:24:07 +00:00
{
2014-12-22 00:02:27 +00:00
tr_variant * dup ;
const char * str ;
if ( tr_variantDictFindDict ( arguments , TR_KEY_torrent_duplicate , & dup ) & &
tr_variantDictFindStr ( dup , TR_KEY_name , & str , NULL ) )
{
const QString myFilename = QFileInfo ( myName ) . fileName ( ) ;
const QString name = QString : : fromUtf8 ( str ) ;
2014-12-27 14:07:14 +00:00
QMessageBox : : warning ( qApp - > activeWindow ( ) ,
2014-12-22 00:02:27 +00:00
tr ( " Add Torrent " ) ,
tr ( " <p><b>Unable to add \" %1 \" .</b></p><p>It is a duplicate of \" %2 \" which is already added.</p> " ) . arg ( myFilename ) . arg ( name ) ) ;
}
2014-12-01 19:24:07 +00:00
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 ( ) ;
2014-12-27 14:07:14 +00:00
QMessageBox : : warning ( qApp - > activeWindow ( ) ,
2014-12-01 19:24:07 +00:00
tr ( " Error Adding Torrent " ) ,
2014-12-27 14:07:14 +00:00
QString : : fromLatin1 ( " <p><b>%1</b></p><p>%2</p> " ) . arg ( text ) . arg ( myName ) ) ;
2014-12-01 19:24:07 +00:00
}
deleteLater ( ) ;
}
/***
* * * *
* * */
2009-04-09 18:55:47 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : sessionSet ( const tr_quark key , const QVariant & value )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 1 ) ;
2013-02-03 19:40:20 +00:00
switch ( value . type ( ) )
{
2014-12-27 20:03:10 +00:00
case QVariant : : Bool : tr_variantDictAddBool ( & args , key , value . toBool ( ) ) ; break ;
case QVariant : : Int : tr_variantDictAddInt ( & args , key , value . toInt ( ) ) ; break ;
case QVariant : : Double : tr_variantDictAddReal ( & args , key , value . toDouble ( ) ) ; break ;
case QVariant : : String : tr_variantDictAddStr ( & args , key , value . toString ( ) . toUtf8 ( ) . constData ( ) ) ; break ;
2013-02-03 19:40:20 +00:00
default : assert ( " unknown type " ) ;
2009-04-09 18:55:47 +00:00
}
2014-12-27 20:03:10 +00:00
exec ( " session-set " , & args ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : portTest ( )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
exec ( " port-test " , nullptr , TAG_PORT_TEST ) ;
2009-04-09 18:55:47 +00:00
}
2009-12-03 15:23:43 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : copyMagnetLinkToClipboard ( int torrentId )
2009-12-03 15:23:43 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
tr_variantListAddInt ( tr_variantDictAddList ( & args , TR_KEY_ids , 1 ) , torrentId ) ;
tr_variantListAddStr ( tr_variantDictAddList ( & args , TR_KEY_fields , 1 ) , " magnetLink " ) ;
exec ( TR_KEY_torrent_get , & args , TAG_MAGNET_LINK ) ;
2009-12-03 15:23:43 +00:00
}
2009-04-09 18:55:47 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : updatePref ( int key )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
if ( myPrefs . isCore ( key ) ) switch ( key )
2009-04-09 18:55:47 +00:00
{
2014-12-12 23:05:10 +00:00
case Prefs : : ALT_SPEED_LIMIT_DOWN :
case Prefs : : ALT_SPEED_LIMIT_ENABLED :
case Prefs : : ALT_SPEED_LIMIT_TIME_BEGIN :
case Prefs : : ALT_SPEED_LIMIT_TIME_DAY :
case Prefs : : ALT_SPEED_LIMIT_TIME_ENABLED :
case Prefs : : ALT_SPEED_LIMIT_TIME_END :
case Prefs : : ALT_SPEED_LIMIT_UP :
case Prefs : : BLOCKLIST_DATE :
case Prefs : : BLOCKLIST_ENABLED :
case Prefs : : BLOCKLIST_URL :
case Prefs : : DHT_ENABLED :
case Prefs : : DOWNLOAD_QUEUE_ENABLED :
case Prefs : : DOWNLOAD_QUEUE_SIZE :
case Prefs : : DSPEED :
case Prefs : : DSPEED_ENABLED :
case Prefs : : IDLE_LIMIT :
case Prefs : : IDLE_LIMIT_ENABLED :
case Prefs : : INCOMPLETE_DIR :
case Prefs : : INCOMPLETE_DIR_ENABLED :
case Prefs : : LPD_ENABLED :
case Prefs : : PEER_LIMIT_GLOBAL :
case Prefs : : PEER_LIMIT_TORRENT :
case Prefs : : PEER_PORT :
case Prefs : : PEER_PORT_RANDOM_ON_START :
case Prefs : : QUEUE_STALLED_MINUTES :
case Prefs : : PEX_ENABLED :
case Prefs : : PORT_FORWARDING :
case Prefs : : RENAME_PARTIAL_FILES :
case Prefs : : SCRIPT_TORRENT_DONE_ENABLED :
case Prefs : : SCRIPT_TORRENT_DONE_FILENAME :
case Prefs : : START :
case Prefs : : TRASH_ORIGINAL :
case Prefs : : USPEED :
case Prefs : : USPEED_ENABLED :
case Prefs : : UTP_ENABLED :
2013-02-03 19:40:20 +00:00
sessionSet ( myPrefs . getKey ( key ) , myPrefs . variant ( key ) ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : DOWNLOAD_DIR :
2013-02-03 19:40:20 +00:00
sessionSet ( myPrefs . getKey ( key ) , myPrefs . variant ( key ) ) ;
/* this will change the 'freespace' argument, so refresh */
refreshSessionInfo ( ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : RATIO :
2013-02-03 19:40:20 +00:00
sessionSet ( TR_KEY_seedRatioLimit , myPrefs . variant ( key ) ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : RATIO_ENABLED :
2013-02-03 19:40:20 +00:00
sessionSet ( TR_KEY_seedRatioLimited , myPrefs . variant ( key ) ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : ENCRYPTION :
2013-02-03 19:40:20 +00:00
{
const int i = myPrefs . variant ( key ) . toInt ( ) ;
switch ( i )
2010-06-17 02:50:50 +00:00
{
2013-02-03 19:40:20 +00:00
case 0 :
2015-01-29 21:53:05 +00:00
sessionSet ( myPrefs . getKey ( key ) , QLatin1String ( " tolerated " ) ) ;
2013-02-03 19:40:20 +00:00
break ;
case 1 :
2015-01-29 21:53:05 +00:00
sessionSet ( myPrefs . getKey ( key ) , QLatin1String ( " preferred " ) ) ;
2013-02-03 19:40:20 +00:00
break ;
case 2 :
2015-01-29 21:53:05 +00:00
sessionSet ( myPrefs . getKey ( key ) , QLatin1String ( " required " ) ) ;
2010-06-17 02:50:50 +00:00
break ;
}
2013-02-03 19:40:20 +00:00
break ;
}
2010-06-17 02:50:50 +00:00
2014-12-12 23:05:10 +00:00
case Prefs : : RPC_AUTH_REQUIRED :
2013-02-03 19:40:20 +00:00
if ( mySession )
tr_sessionSetRPCPasswordEnabled ( mySession , myPrefs . getBool ( key ) ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : RPC_ENABLED :
2013-02-03 19:40:20 +00:00
if ( mySession )
tr_sessionSetRPCEnabled ( mySession , myPrefs . getBool ( key ) ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : RPC_PASSWORD :
2013-02-03 19:40:20 +00:00
if ( mySession )
tr_sessionSetRPCPassword ( mySession , myPrefs . getString ( key ) . toUtf8 ( ) . constData ( ) ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : RPC_PORT :
2013-02-03 19:40:20 +00:00
if ( mySession )
tr_sessionSetRPCPort ( mySession , myPrefs . getInt ( key ) ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : RPC_USERNAME :
2013-02-03 19:40:20 +00:00
if ( mySession )
tr_sessionSetRPCUsername ( mySession , myPrefs . getString ( key ) . toUtf8 ( ) . constData ( ) ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : RPC_WHITELIST_ENABLED :
2013-02-03 19:40:20 +00:00
if ( mySession )
tr_sessionSetRPCWhitelistEnabled ( mySession , myPrefs . getBool ( key ) ) ;
break ;
2014-12-12 23:05:10 +00:00
case Prefs : : RPC_WHITELIST :
2013-02-03 19:40:20 +00:00
if ( mySession )
tr_sessionSetRPCWhitelist ( mySession , myPrefs . getString ( key ) . toUtf8 ( ) . constData ( ) ) ;
break ;
default :
std : : cerr < < " unhandled pref: " < < key < < std : : endl ;
2009-04-09 18:55:47 +00:00
}
}
/***
* * * *
* * */
2015-01-28 22:57:46 +00:00
Session : : Session ( const QString & configDir , Prefs & prefs ) :
2015-06-12 22:12:12 +00:00
myConfigDir ( configDir ) ,
myPrefs ( prefs ) ,
2013-02-03 19:40:20 +00:00
nextUniqueTag ( FIRST_UNIQUE_TAG ) ,
myBlocklistSize ( - 1 ) ,
2015-06-12 22:12:12 +00:00
mySession ( 0 )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
myStats . ratio = TR_RATIO_NA ;
myStats . uploadedBytes = 0 ;
myStats . downloadedBytes = 0 ;
myStats . filesAdded = 0 ;
myStats . sessionCount = 0 ;
myStats . secondsActive = 0 ;
myCumulativeStats = myStats ;
connect ( & myPrefs , SIGNAL ( changed ( int ) ) , this , SLOT ( updatePref ( int ) ) ) ;
2014-12-27 20:03:10 +00:00
connect ( & myRpc , SIGNAL ( executed ( int64_t , QString , tr_variant * ) ) , this , SLOT ( responseReceived ( int64_t , QString , tr_variant * ) ) ) ;
connect ( & myRpc , SIGNAL ( httpAuthenticationRequired ( ) ) , this , SIGNAL ( httpAuthenticationRequired ( ) ) ) ;
connect ( & myRpc , SIGNAL ( dataReadProgress ( ) ) , this , SIGNAL ( dataReadProgress ( ) ) ) ;
connect ( & myRpc , SIGNAL ( dataSendProgress ( ) ) , this , SIGNAL ( dataSendProgress ( ) ) ) ;
connect ( & myRpc , SIGNAL ( error ( QNetworkReply : : NetworkError ) ) , this , SIGNAL ( error ( QNetworkReply : : NetworkError ) ) ) ;
connect ( & myRpc , SIGNAL ( errorMessage ( QString ) ) , this , SIGNAL ( errorMessage ( QString ) ) ) ;
2009-05-03 17:37:39 +00:00
}
2014-12-12 23:05:10 +00:00
Session : : ~ Session ( )
2009-05-03 17:37:39 +00:00
{
2013-02-03 19:40:20 +00:00
stop ( ) ;
2009-05-03 17:37:39 +00:00
}
/***
* * * *
* * */
void
2014-12-12 23:05:10 +00:00
Session : : stop ( )
2009-05-03 17:37:39 +00:00
{
2014-12-27 20:03:10 +00:00
myRpc . stop ( ) ;
2009-05-03 17:37:39 +00:00
2013-02-03 19:40:20 +00:00
if ( mySession )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
tr_sessionClose ( mySession ) ;
mySession = 0 ;
2009-05-03 17:37:39 +00:00
}
}
void
2014-12-12 23:05:10 +00:00
Session : : restart ( )
2009-05-03 17:37:39 +00:00
{
2013-02-03 19:40:20 +00:00
stop ( ) ;
start ( ) ;
2009-05-03 17:37:39 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : start ( )
2009-05-03 17:37:39 +00:00
{
2013-02-03 19:40:20 +00:00
if ( myPrefs . get < bool > ( Prefs : : SESSION_IS_REMOTE ) )
2009-05-03 17:37:39 +00:00
{
2013-02-03 19:40:20 +00:00
QUrl url ;
2015-01-29 21:53:05 +00:00
url . setScheme ( QLatin1String ( " http " ) ) ;
2013-02-03 19:40:20 +00:00
url . setHost ( myPrefs . get < QString > ( Prefs : : SESSION_REMOTE_HOST ) ) ;
url . setPort ( myPrefs . get < int > ( Prefs : : SESSION_REMOTE_PORT ) ) ;
2015-01-29 21:53:05 +00:00
url . setPath ( QLatin1String ( " /transmission/rpc " ) ) ;
2013-02-03 19:40:20 +00:00
if ( myPrefs . get < bool > ( Prefs : : SESSION_REMOTE_AUTH ) )
2010-08-04 13:56:52 +00:00
{
2013-02-03 19:40:20 +00:00
url . setUserName ( myPrefs . get < QString > ( Prefs : : SESSION_REMOTE_USERNAME ) ) ;
url . setPassword ( myPrefs . get < QString > ( Prefs : : SESSION_REMOTE_PASSWORD ) ) ;
2009-05-03 17:37:39 +00:00
}
2014-12-27 20:03:10 +00:00
myRpc . start ( url ) ;
2009-04-09 18:55:47 +00:00
}
2013-02-03 19:40:20 +00:00
else
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
tr_variant settings ;
tr_variantInitDict ( & settings , 0 ) ;
tr_sessionLoadSettings ( & settings , myConfigDir . toUtf8 ( ) . constData ( ) , " qt " ) ;
2015-10-18 18:39:14 +00:00
mySession = tr_sessionInit ( myConfigDir . toUtf8 ( ) . constData ( ) , true , & settings ) ;
2013-02-03 19:40:20 +00:00
tr_variantFree ( & settings ) ;
2014-12-27 20:03:10 +00:00
myRpc . start ( mySession ) ;
2013-02-03 19:40:20 +00:00
tr_ctor * ctor = tr_ctorNew ( mySession ) ;
int torrentCount ;
tr_torrent * * torrents = tr_sessionLoadTorrents ( mySession , ctor , & torrentCount ) ;
tr_free ( torrents ) ;
tr_ctorFree ( ctor ) ;
2009-04-09 18:55:47 +00:00
}
2013-02-03 19:40:20 +00:00
emit sourceChanged ( ) ;
2009-04-09 18:55:47 +00:00
}
bool
2014-12-12 23:05:10 +00:00
Session : : isServer ( ) const
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
return mySession ! = 0 ;
2009-04-09 18:55:47 +00:00
}
bool
2014-12-12 23:05:10 +00:00
Session : : isLocal ( ) const
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
return myRpc . isLocal ( ) ;
2009-04-09 18:55:47 +00:00
}
/***
* * * *
* * */
namespace
{
2012-12-22 20:35:19 +00:00
void
2013-02-03 19:40:20 +00:00
addOptionalIds ( tr_variant * args , const QSet < int > & ids )
2012-12-22 20:35:19 +00:00
{
2013-02-03 19:40:20 +00:00
if ( ! ids . isEmpty ( ) )
2012-12-22 20:35:19 +00:00
{
2013-02-03 19:40:20 +00:00
tr_variant * idList ( tr_variantDictAddList ( args , TR_KEY_ids , ids . size ( ) ) ) ;
2015-04-18 14:41:06 +00:00
for ( const int i : ids )
2012-12-22 20:35:19 +00:00
tr_variantListAddInt ( idList , i ) ;
}
}
2009-04-09 18:55:47 +00:00
}
2009-04-12 21:15:35 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : torrentSet ( const QSet < int > & ids , const tr_quark key , double value )
2009-04-12 21:15:35 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
tr_variantDictAddReal ( & args , key , value ) ;
addOptionalIds ( & args , ids ) ;
exec ( TR_KEY_torrent_set , & args ) ;
2009-04-12 21:15:35 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : torrentSet ( const QSet < int > & ids , const tr_quark key , int value )
2009-04-12 21:15:35 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
tr_variantDictAddInt ( & args , key , value ) ;
addOptionalIds ( & args , ids ) ;
exec ( TR_KEY_torrent_set , & args ) ;
2009-04-12 21:15:35 +00:00
}
2009-04-09 18:55:47 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : torrentSet ( const QSet < int > & ids , const tr_quark key , bool value )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
tr_variantDictAddBool ( & args , key , value ) ;
addOptionalIds ( & args , ids ) ;
exec ( TR_KEY_torrent_set , & args ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : torrentSet ( const QSet < int > & ids , const tr_quark key , const QStringList & value )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
addOptionalIds ( & args , ids ) ;
tr_variant * list ( tr_variantDictAddList ( & args , key , value . size ( ) ) ) ;
2015-04-18 14:41:06 +00:00
for ( const QString & str : value )
2013-02-03 19:40:20 +00:00
tr_variantListAddStr ( list , str . toUtf8 ( ) . constData ( ) ) ;
2014-12-27 20:03:10 +00:00
exec ( TR_KEY_torrent_set , & args ) ;
2009-04-09 18:55:47 +00:00
}
2010-06-30 05:55:46 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : torrentSet ( const QSet < int > & ids , const tr_quark key , const QList < int > & value )
2010-06-30 05:55:46 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
addOptionalIds ( & args , ids ) ;
tr_variant * list ( tr_variantDictAddList ( & args , key , value . size ( ) ) ) ;
2015-04-18 14:41:06 +00:00
for ( const int i : value )
2013-02-03 19:40:20 +00:00
tr_variantListAddInt ( list , i ) ;
2014-12-27 20:03:10 +00:00
exec ( TR_KEY_torrent_set , & args ) ;
2010-06-30 05:55:46 +00:00
}
2010-09-14 06:23:48 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : torrentSet ( const QSet < int > & ids , const tr_quark key , const QPair < int , QString > & value )
2010-09-14 06:23:48 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
addOptionalIds ( & args , ids ) ;
tr_variant * list ( tr_variantDictAddList ( & args , key , 2 ) ) ;
2012-12-22 20:35:19 +00:00
tr_variantListAddInt ( list , value . first ) ;
2013-02-03 19:40:20 +00:00
tr_variantListAddStr ( list , value . second . toUtf8 ( ) . constData ( ) ) ;
2014-12-27 20:03:10 +00:00
exec ( TR_KEY_torrent_set , & args ) ;
2010-09-14 06:23:48 +00:00
}
2009-05-13 15:54:04 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : torrentSetLocation ( const QSet < int > & ids , const QString & location , bool doMove )
2009-05-13 15:54:04 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 3 ) ;
addOptionalIds ( & args , ids ) ;
tr_variantDictAddStr ( & args , TR_KEY_location , location . toUtf8 ( ) . constData ( ) ) ;
tr_variantDictAddBool ( & args , TR_KEY_move , doMove ) ;
exec ( TR_KEY_torrent_set_location , & args ) ;
2009-05-13 15:54:04 +00:00
}
2009-04-09 18:55:47 +00:00
2013-01-20 01:31:58 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : torrentRenamePath ( const QSet < int > & ids , const QString & oldpath , const QString & newname )
2013-01-20 01:31:58 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
addOptionalIds ( & args , ids ) ;
tr_variantDictAddStr ( & args , TR_KEY_path , oldpath . toUtf8 ( ) . constData ( ) ) ;
tr_variantDictAddStr ( & args , TR_KEY_name , newname . toUtf8 ( ) . constData ( ) ) ;
exec ( " torrent-rename-path " , & args , TAG_RENAME_PATH ) ;
2013-01-20 01:31:58 +00:00
}
2009-04-09 18:55:47 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : refreshTorrents ( const QSet < int > & ids )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
if ( ids . empty ( ) )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
refreshAllTorrents ( ) ;
2009-04-09 18:55:47 +00:00
}
2013-02-03 19:40:20 +00:00
else
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
addList ( tr_variantDictAddList ( & args , TR_KEY_fields , 0 ) , getStatKeys ( ) ) ;
addOptionalIds ( & args , ids ) ;
exec ( TR_KEY_torrent_get , & args , TAG_SOME_TORRENTS ) ;
2009-04-09 18:55:47 +00:00
}
}
void
2014-12-12 23:05:10 +00:00
Session : : refreshExtraStats ( const QSet < int > & ids )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 3 ) ;
addOptionalIds ( & args , ids ) ;
addList ( tr_variantDictAddList ( & args , TR_KEY_fields , 0 ) , getStatKeys ( ) + getExtraStatKeys ( ) ) ;
exec ( TR_KEY_torrent_get , & args , TAG_SOME_TORRENTS ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : sendTorrentRequest ( const char * request , const QSet < int > & ids )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 1 ) ;
addOptionalIds ( & args , ids ) ;
2009-04-09 18:55:47 +00:00
2014-12-27 20:03:10 +00:00
exec ( request , & args ) ;
2012-12-22 20:35:19 +00:00
refreshTorrents ( ids ) ;
2009-04-09 18:55:47 +00:00
}
2014-12-12 23:05:10 +00:00
void Session : : pauseTorrents ( const QSet < int > & ids ) { sendTorrentRequest ( " torrent-stop " , ids ) ; }
2015-01-02 11:15:31 +00:00
void Session : : startTorrents ( const QSet < int > & ids ) { sendTorrentRequest ( " torrent-start " , ids ) ; }
2014-12-12 23:05:10 +00:00
void Session : : startTorrentsNow ( const QSet < int > & ids ) { sendTorrentRequest ( " torrent-start-now " , ids ) ; }
2015-01-02 11:15:31 +00:00
void Session : : queueMoveTop ( const QSet < int > & ids ) { sendTorrentRequest ( " queue-move-top " , ids ) ; }
void Session : : queueMoveUp ( const QSet < int > & ids ) { sendTorrentRequest ( " queue-move-up " , ids ) ; }
void Session : : queueMoveDown ( const QSet < int > & ids ) { sendTorrentRequest ( " queue-move-down " , ids ) ; }
void Session : : queueMoveBottom ( const QSet < int > & ids ) { sendTorrentRequest ( " queue-move-bottom " , ids ) ; }
2009-04-09 18:55:47 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : refreshActiveTorrents ( )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
tr_variantDictAddStr ( & args , TR_KEY_ids , " recently-active " ) ;
addList ( tr_variantDictAddList ( & args , TR_KEY_fields , 0 ) , getStatKeys ( ) ) ;
exec ( TR_KEY_torrent_get , & args , TAG_SOME_TORRENTS ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : refreshAllTorrents ( )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 1 ) ;
addList ( tr_variantDictAddList ( & args , TR_KEY_fields , 0 ) , getStatKeys ( ) ) ;
exec ( TR_KEY_torrent_get , & args , TAG_ALL_TORRENTS ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : initTorrents ( const QSet < int > & ids )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
addOptionalIds ( & args , ids ) ;
addList ( tr_variantDictAddList ( & args , TR_KEY_fields , 0 ) , getStatKeys ( ) + getInfoKeys ( ) ) ;
exec ( " torrent-get " , & args , ids . isEmpty ( ) ? TAG_ALL_TORRENTS : TAG_SOME_TORRENTS ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : refreshSessionStats ( )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
exec ( " session-stats " , nullptr , TAG_SESSION_STATS ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : refreshSessionInfo ( )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
exec ( " session-get " , nullptr , TAG_SESSION_INFO ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : updateBlocklist ( )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
exec ( " blocklist-update " , nullptr , TAG_BLOCKLIST_UPDATE ) ;
2009-04-09 18:55:47 +00:00
}
/***
* * * *
* * */
void
2014-12-27 20:03:10 +00:00
Session : : exec ( tr_quark method , tr_variant * args , int64_t tag )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
myRpc . exec ( method , args , tag ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-27 20:03:10 +00:00
Session : : exec ( const char * method , tr_variant * args , int64_t tag )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
myRpc . exec ( method , args , tag ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-27 20:03:10 +00:00
Session : : responseReceived ( int64_t tag , const QString & result , tr_variant * args )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
emit executed ( tag , result , args ) ;
2009-04-09 18:55:47 +00:00
2014-12-27 20:03:10 +00:00
if ( tag < 0 )
return ;
switch ( tag )
2009-05-08 14:56:11 +00:00
{
2014-12-27 20:03:10 +00:00
case TAG_SOME_TORRENTS :
case TAG_ALL_TORRENTS :
if ( args ! = nullptr )
{
tr_variant * torrents ;
if ( tr_variantDictFindList ( args , TR_KEY_torrents , & torrents ) )
emit torrentsUpdated ( torrents , tag = = TAG_ALL_TORRENTS ) ;
if ( tr_variantDictFindList ( args , TR_KEY_removed , & torrents ) )
emit torrentsRemoved ( torrents ) ;
}
break ;
2009-04-09 18:55:47 +00:00
2014-12-27 20:03:10 +00:00
case TAG_SESSION_STATS :
if ( args ! = nullptr )
updateStats ( args ) ;
break ;
2009-04-09 18:55:47 +00:00
2014-12-27 20:03:10 +00:00
case TAG_SESSION_INFO :
if ( args ! = nullptr )
updateInfo ( args ) ;
break ;
2013-01-20 01:31:58 +00:00
2014-12-27 20:03:10 +00:00
case TAG_BLOCKLIST_UPDATE :
{
int64_t intVal = 0 ;
if ( args ! = nullptr )
{
if ( tr_variantDictFindInt ( args , TR_KEY_blocklist_size , & intVal ) )
setBlocklistSize ( intVal ) ;
}
break ;
}
case TAG_RENAME_PATH :
{
int64_t id = 0 ;
if ( result ! = QLatin1String ( " success " ) )
{
const char * path = " " ;
const char * name = " " ;
tr_variantDictFindStr ( args , TR_KEY_path , & path , 0 ) ;
tr_variantDictFindStr ( args , TR_KEY_name , & name , 0 ) ;
const QString title = tr ( " Error Renaming Path " ) ;
2015-01-29 21:53:05 +00:00
const QString text = tr ( " <p><b>Unable to rename \" %1 \" as \" %2 \" : %3.</b></p> <p>Please correct the errors and try again.</p> " ) . arg ( QString : : fromUtf8 ( path ) ) . arg ( QString : : fromUtf8 ( name ) ) . arg ( result ) ;
2014-12-27 20:03:10 +00:00
QMessageBox * d = new QMessageBox ( QMessageBox : : Information , title , text ,
QMessageBox : : Close ,
qApp - > activeWindow ( ) ) ;
connect ( d , SIGNAL ( rejected ( ) ) , d , SLOT ( deleteLater ( ) ) ) ;
d - > show ( ) ;
}
else if ( tr_variantDictFindInt ( args , TR_KEY_id , & id ) & & id )
{
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
tr_variantDictAddInt ( & args , TR_KEY_ids , id ) ;
addList ( tr_variantDictAddList ( & args , TR_KEY_fields , 0 ) ,
KeyList ( ) < < TR_KEY_fileStats < < TR_KEY_files < < TR_KEY_id < < TR_KEY_name ) ;
exec ( " torrent-get " , & args , TAG_SOME_TORRENTS ) ;
}
2009-05-15 02:05:07 +00:00
2014-12-27 20:03:10 +00:00
break ;
}
2009-05-15 02:05:07 +00:00
2014-12-27 20:03:10 +00:00
case TAG_PORT_TEST :
{
2015-05-09 11:56:35 +00:00
bool isOpen ;
if ( args = = nullptr | |
! tr_variantDictFindBool ( args , TR_KEY_port_is_open , & isOpen ) )
isOpen = false ;
2014-12-27 20:03:10 +00:00
emit portTested ( isOpen ) ;
break ;
}
2009-05-15 02:05:07 +00:00
2014-12-27 20:03:10 +00:00
case TAG_MAGNET_LINK :
{
tr_variant * torrents ;
tr_variant * child ;
const char * str ;
if ( args ! = nullptr
& & tr_variantDictFindList ( args , TR_KEY_torrents , & torrents )
& & ( ( child = tr_variantListChild ( torrents , 0 ) ) )
& & tr_variantDictFindStr ( child , TR_KEY_magnetLink , & str , NULL ) )
2015-01-29 21:53:05 +00:00
qApp - > clipboard ( ) - > setText ( QString : : fromUtf8 ( str ) ) ;
2014-12-27 20:03:10 +00:00
break ;
}
2009-05-15 02:05:07 +00:00
2014-12-27 20:03:10 +00:00
case TAG_ADD_TORRENT :
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
const char * str = " " ;
if ( result ! = QLatin1String ( " success " ) )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
QMessageBox * d = new QMessageBox ( QMessageBox : : Information ,
tr ( " Add Torrent " ) ,
QString : : fromUtf8 ( str ) ,
QMessageBox : : Close ,
qApp - > activeWindow ( ) ) ;
connect ( d , SIGNAL ( rejected ( ) ) , d , SLOT ( deleteLater ( ) ) ) ;
d - > show ( ) ;
2009-04-09 18:55:47 +00:00
}
2014-12-27 20:03:10 +00:00
break ;
2009-04-09 18:55:47 +00:00
}
}
}
void
2014-12-12 23:21:04 +00:00
Session : : updateStats ( tr_variant * d , tr_session_stats * stats )
2009-04-09 18:55:47 +00:00
{
2013-08-29 00:37:37 +00:00
int64_t i ;
2009-04-09 18:55:47 +00:00
2013-02-03 19:40:20 +00:00
if ( tr_variantDictFindInt ( d , TR_KEY_uploadedBytes , & i ) )
stats - > uploadedBytes = i ;
if ( tr_variantDictFindInt ( d , TR_KEY_downloadedBytes , & i ) )
stats - > downloadedBytes = i ;
if ( tr_variantDictFindInt ( d , TR_KEY_filesAdded , & i ) )
stats - > filesAdded = i ;
if ( tr_variantDictFindInt ( d , TR_KEY_sessionCount , & i ) )
stats - > sessionCount = i ;
if ( tr_variantDictFindInt ( d , TR_KEY_secondsActive , & i ) )
stats - > secondsActive = i ;
stats - > ratio = tr_getRatio ( stats - > uploadedBytes , stats - > downloadedBytes ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : updateStats ( tr_variant * d )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
tr_variant * c ;
2009-04-09 18:55:47 +00:00
2013-02-03 19:40:20 +00:00
if ( tr_variantDictFindDict ( d , TR_KEY_current_stats , & c ) )
updateStats ( c , & myStats ) ;
2009-04-09 18:55:47 +00:00
2013-02-03 19:40:20 +00:00
if ( tr_variantDictFindDict ( d , TR_KEY_cumulative_stats , & c ) )
updateStats ( c , & myCumulativeStats ) ;
2009-04-09 18:55:47 +00:00
2013-02-03 19:40:20 +00:00
emit statsUpdated ( ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : updateInfo ( tr_variant * d )
2009-04-09 18:55:47 +00:00
{
2013-08-29 00:37:37 +00:00
int64_t i ;
2012-12-22 20:35:19 +00:00
const char * str ;
2013-02-03 19:40:20 +00:00
disconnect ( & myPrefs , SIGNAL ( changed ( int ) ) , this , SLOT ( updatePref ( int ) ) ) ;
2009-04-09 18:55:47 +00:00
2012-12-22 20:35:19 +00:00
for ( int i = Prefs : : FIRST_CORE_PREF ; i < = Prefs : : LAST_CORE_PREF ; + + i )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
const tr_variant * b ( tr_variantDictFind ( d , myPrefs . getKey ( i ) ) ) ;
2009-04-09 18:55:47 +00:00
2012-12-22 20:35:19 +00:00
if ( ! b )
continue ;
2009-04-09 18:55:47 +00:00
2014-12-12 23:05:10 +00:00
if ( i = = Prefs : : ENCRYPTION )
2010-06-17 02:50:50 +00:00
{
2012-12-22 20:35:19 +00:00
const char * val ;
2013-02-03 19:40:20 +00:00
if ( tr_variantGetStr ( b , & val , NULL ) )
2010-06-17 02:50:50 +00:00
{
2013-02-03 19:40:20 +00:00
if ( ! qstrcmp ( val , " required " ) )
myPrefs . set ( i , 2 ) ;
else if ( ! qstrcmp ( val , " preferred " ) )
myPrefs . set ( i , 1 ) ;
else if ( ! qstrcmp ( val , " tolerated " ) )
myPrefs . set ( i , 0 ) ;
2010-06-17 02:50:50 +00:00
}
2012-12-22 20:35:19 +00:00
continue ;
2010-06-17 02:50:50 +00:00
}
2013-02-03 19:40:20 +00:00
switch ( myPrefs . type ( i ) )
2009-04-09 18:55:47 +00:00
{
2014-12-12 23:05:10 +00:00
case QVariant : : Int :
2013-02-03 19:40:20 +00:00
{
2013-08-29 00:37:37 +00:00
int64_t val ;
2013-02-03 19:40:20 +00:00
if ( tr_variantGetInt ( b , & val ) )
2014-12-27 14:07:14 +00:00
myPrefs . set ( i , static_cast < int > ( val ) ) ;
2013-02-03 19:40:20 +00:00
break ;
2009-04-09 18:55:47 +00:00
}
2014-12-12 23:05:10 +00:00
case QVariant : : Double :
2013-02-03 19:40:20 +00:00
{
double val ;
if ( tr_variantGetReal ( b , & val ) )
myPrefs . set ( i , val ) ;
break ;
2009-04-09 18:55:47 +00:00
}
2014-12-12 23:05:10 +00:00
case QVariant : : Bool :
2013-02-03 19:40:20 +00:00
{
bool val ;
if ( tr_variantGetBool ( b , & val ) )
2014-12-27 14:07:14 +00:00
myPrefs . set ( i , val ) ;
2013-02-03 19:40:20 +00:00
break ;
2009-04-09 18:55:47 +00:00
}
2015-06-10 21:27:11 +00:00
case CustomVariantType : : FilterModeType :
case CustomVariantType : : SortModeType :
2014-12-12 23:05:10 +00:00
case QVariant : : String :
2013-02-03 19:40:20 +00:00
{
const char * val ;
if ( tr_variantGetStr ( b , & val , NULL ) )
2014-12-27 14:07:14 +00:00
myPrefs . set ( i , QString : : fromUtf8 ( val ) ) ;
2013-02-03 19:40:20 +00:00
break ;
2009-04-09 18:55:47 +00:00
}
2013-02-03 19:40:20 +00:00
default :
break ;
2009-04-09 18:55:47 +00:00
}
}
2013-02-03 19:40:20 +00:00
bool b ;
double x ;
if ( tr_variantDictFindBool ( d , TR_KEY_seedRatioLimited , & b ) )
2014-12-27 14:07:14 +00:00
myPrefs . set ( Prefs : : RATIO_ENABLED , b ) ;
2013-02-03 19:40:20 +00:00
if ( tr_variantDictFindReal ( d , TR_KEY_seedRatioLimit , & x ) )
myPrefs . set ( Prefs : : RATIO , x ) ;
2010-06-08 17:37:29 +00:00
2013-02-03 19:40:20 +00:00
/* Use the C API to get settings that, for security reasons, aren't supported by RPC */
if ( mySession ! = 0 )
2009-04-09 18:55:47 +00:00
{
2015-01-28 22:57:46 +00:00
myPrefs . set ( Prefs : : RPC_ENABLED , tr_sessionIsRPCEnabled ( mySession ) ) ;
myPrefs . set ( Prefs : : RPC_AUTH_REQUIRED , tr_sessionIsRPCPasswordEnabled ( mySession ) ) ;
myPrefs . set ( Prefs : : RPC_PASSWORD , QString : : fromUtf8 ( tr_sessionGetRPCPassword ( mySession ) ) ) ;
myPrefs . set ( Prefs : : RPC_PORT , tr_sessionGetRPCPort ( mySession ) ) ;
myPrefs . set ( Prefs : : RPC_USERNAME , QString : : fromUtf8 ( tr_sessionGetRPCUsername ( mySession ) ) ) ;
2013-02-03 19:40:20 +00:00
myPrefs . set ( Prefs : : RPC_WHITELIST_ENABLED , tr_sessionGetRPCWhitelistEnabled ( mySession ) ) ;
2015-01-28 22:57:46 +00:00
myPrefs . set ( Prefs : : RPC_WHITELIST , QString : : fromUtf8 ( tr_sessionGetRPCWhitelist ( mySession ) ) ) ;
2009-04-09 18:55:47 +00:00
}
2013-02-03 19:40:20 +00:00
if ( tr_variantDictFindInt ( d , TR_KEY_blocklist_size , & i ) & & i ! = blocklistSize ( ) )
setBlocklistSize ( i ) ;
2009-04-09 18:55:47 +00:00
2015-01-29 21:53:05 +00:00
if ( tr_variantDictFindStr ( d , TR_KEY_version , & str , NULL ) & & ( mySessionVersion ! = QString : : fromUtf8 ( str ) ) )
mySessionVersion = QString : : fromUtf8 ( str ) ;
2009-04-09 18:55:47 +00:00
2014-12-12 23:05:10 +00:00
//std::cerr << "Session::updateInfo end" << std::endl;
2013-02-03 19:40:20 +00:00
connect ( & myPrefs , SIGNAL ( changed ( int ) ) , this , SLOT ( updatePref ( int ) ) ) ;
2009-04-09 18:55:47 +00:00
2013-02-03 19:40:20 +00:00
emit sessionUpdated ( ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : setBlocklistSize ( int64_t i )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
myBlocklistSize = i ;
2009-04-09 18:55:47 +00:00
2013-02-03 19:40:20 +00:00
emit blocklistUpdated ( i ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-27 20:03:10 +00:00
Session : : addTorrent ( const AddData & addMe , tr_variant * args , bool trashOriginal )
2009-10-05 19:53:38 +00:00
{
2014-12-01 19:24:07 +00:00
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 ) ) ;
2013-02-03 19:40:20 +00:00
switch ( addMe . type )
{
case AddData : : MAGNET :
tr_variantDictAddStr ( args , TR_KEY_filename , addMe . magnet . toUtf8 ( ) . constData ( ) ) ;
break ;
case AddData : : URL :
tr_variantDictAddStr ( args , TR_KEY_filename , addMe . url . toString ( ) . toUtf8 ( ) . constData ( ) ) ;
break ;
case AddData : : FILENAME : /* fall-through */
case AddData : : METAINFO :
2014-12-01 19:24:07 +00:00
{
const QByteArray b64 = addMe . toBase64 ( ) ;
tr_variantDictAddRaw ( args , TR_KEY_metainfo , b64 . constData ( ) , b64 . size ( ) ) ;
break ;
}
2013-02-03 19:40:20 +00:00
default :
2014-12-01 19:24:07 +00:00
qWarning ( ) < < " Unhandled AddData type: " < < addMe . type ;
2013-02-03 19:40:20 +00:00
break ;
2010-04-27 03:10:32 +00:00
}
2013-02-03 19:40:20 +00:00
2014-12-27 20:03:10 +00:00
const int64_t tag = getUniqueTag ( ) ;
2014-12-01 19:24:07 +00:00
// maybe delete the source .torrent
FileAdded * fileAdded = new FileAdded ( tag , addMe . readableName ( ) ) ;
if ( trashOriginal & & addMe . type = = AddData : : FILENAME )
fileAdded - > setFileToDelete ( addMe . filename ) ;
2014-12-12 23:21:04 +00:00
connect ( this , SIGNAL ( executed ( int64_t , QString , tr_variant * ) ) ,
fileAdded , SLOT ( executed ( int64_t , QString , tr_variant * ) ) ) ;
2014-12-01 19:24:07 +00:00
2014-12-27 20:03:10 +00:00
exec ( " torrent-add " , args , tag ) ;
2014-12-01 19:24:07 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : addTorrent ( const AddData & addMe )
2014-12-01 19:24:07 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 3 ) ;
2014-12-01 19:24:07 +00:00
2014-12-27 20:03:10 +00:00
addTorrent ( addMe , & args , myPrefs . getBool ( Prefs : : TRASH_ORIGINAL ) ) ;
2010-04-27 03:10:32 +00:00
}
2009-10-05 19:53:38 +00:00
void
2014-12-12 23:05:10 +00:00
Session : : addNewlyCreatedTorrent ( const QString & filename , const QString & localPath )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
const QByteArray b64 = AddData ( filename ) . toBase64 ( ) ;
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 3 ) ;
tr_variantDictAddStr ( & args , TR_KEY_download_dir , localPath . toUtf8 ( ) . constData ( ) ) ;
tr_variantDictAddBool ( & args , TR_KEY_paused , ! myPrefs . getBool ( Prefs : : START ) ) ;
tr_variantDictAddRaw ( & args , TR_KEY_metainfo , b64 . constData ( ) , b64 . size ( ) ) ;
exec ( " torrent-add " , & args ) ;
2009-04-09 18:55:47 +00:00
}
void
2014-12-12 23:05:10 +00:00
Session : : removeTorrents ( const QSet < int > & ids , bool deleteFiles )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
if ( ! ids . isEmpty ( ) )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 2 ) ;
addOptionalIds ( & args , ids ) ;
tr_variantDictAddInt ( & args , TR_KEY_delete_local_data , deleteFiles ) ;
exec ( " torrent-remove " , & args ) ;
2009-04-09 18:55:47 +00:00
}
}
void
2014-12-12 23:05:10 +00:00
Session : : verifyTorrents ( const QSet < int > & ids )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
if ( ! ids . isEmpty ( ) )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 1 ) ;
addOptionalIds ( & args , ids ) ;
exec ( " torrent-verify " , & args ) ;
2009-04-09 18:55:47 +00:00
}
}
void
2014-12-12 23:05:10 +00:00
Session : : reannounceTorrents ( const QSet < int > & ids )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
if ( ! ids . isEmpty ( ) )
2009-04-09 18:55:47 +00:00
{
2014-12-27 20:03:10 +00:00
tr_variant args ;
tr_variantInitDict ( & args , 1 ) ;
addOptionalIds ( & args , ids ) ;
exec ( " torrent-reannounce " , & args ) ;
2009-04-09 18:55:47 +00:00
}
}
/***
* * * *
* * */
void
2014-12-12 23:05:10 +00:00
Session : : launchWebInterface ( )
2009-04-09 18:55:47 +00:00
{
2013-02-03 19:40:20 +00:00
QUrl url ;
if ( ! mySession ) // remote session
2012-07-22 15:18:52 +00:00
{
2014-12-27 20:03:10 +00:00
url = myRpc . url ( ) ;
2015-01-29 21:53:05 +00:00
url . setPath ( QLatin1String ( " /transmission/web/ " ) ) ;
2012-07-22 15:18:52 +00:00
}
2013-02-03 19:40:20 +00:00
else // local session
2012-07-22 15:18:52 +00:00
{
2015-01-29 21:53:05 +00:00
url . setScheme ( QLatin1String ( " http " ) ) ;
url . setHost ( QLatin1String ( " localhost " ) ) ;
2013-02-03 19:40:20 +00:00
url . setPort ( myPrefs . getInt ( Prefs : : RPC_PORT ) ) ;
2009-04-09 18:55:47 +00:00
}
2013-02-03 19:40:20 +00:00
2014-12-12 23:05:10 +00:00
QDesktopServices : : openUrl ( url ) ;
2009-04-09 18:55:47 +00:00
}