2009-04-09 18:55:47 +00:00
|
|
|
/*
|
2010-01-04 21:00:47 +00:00
|
|
|
* This file Copyright (C) 2009-2010 Mnemosyne LLC
|
2009-04-09 18:55:47 +00:00
|
|
|
*
|
|
|
|
* This file is licensed by the GPL version 2. Works owned by the
|
|
|
|
* Transmission project are granted a special exemption to clause 2(b)
|
|
|
|
* so that the bulk of its code can remain under the MIT license.
|
|
|
|
* This exemption does not extend to derived works not owned by
|
|
|
|
* the Transmission project.
|
|
|
|
*
|
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>
|
|
|
|
#include <QDesktopServices>
|
|
|
|
#include <QMessageBox>
|
|
|
|
#include <QSet>
|
|
|
|
#include <QStyle>
|
2009-04-12 21:15:35 +00:00
|
|
|
#include <QTextStream>
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/bencode.h>
|
2009-04-13 18:21:22 +00:00
|
|
|
#include <libtransmission/json.h>
|
2009-04-09 18:55:47 +00:00
|
|
|
#include <libtransmission/rpcimpl.h>
|
|
|
|
#include <libtransmission/utils.h> /* tr_free */
|
|
|
|
#include <libtransmission/version.h> /* LONG_VERSION */
|
|
|
|
|
|
|
|
#include "prefs.h"
|
|
|
|
#include "qticonloader.h"
|
|
|
|
#include "session.h"
|
2009-05-03 17:37:39 +00:00
|
|
|
#include "session-dialog.h"
|
2009-04-09 18:55:47 +00:00
|
|
|
#include "torrent.h"
|
|
|
|
|
|
|
|
// #define DEBUG_HTTP
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
enum
|
|
|
|
{
|
|
|
|
TAG_SOME_TORRENTS,
|
|
|
|
TAG_ALL_TORRENTS,
|
|
|
|
TAG_SESSION_STATS,
|
|
|
|
TAG_SESSION_INFO,
|
|
|
|
TAG_BLOCKLIST_UPDATE,
|
|
|
|
TAG_ADD_TORRENT,
|
2009-05-15 02:05:07 +00:00
|
|
|
TAG_PORT_TEST,
|
2009-12-03 15:23:43 +00:00
|
|
|
TAG_MAGNET_LINK,
|
2009-05-15 02:05:07 +00:00
|
|
|
|
|
|
|
FIRST_UNIQUE_TAG
|
2009-04-09 18:55:47 +00:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
typedef Torrent::KeyList KeyList;
|
|
|
|
const KeyList& getInfoKeys( ) { return Torrent::getInfoKeys( ); }
|
|
|
|
const KeyList& getStatKeys( ) { return Torrent::getStatKeys( ); }
|
|
|
|
const KeyList& getExtraStatKeys( ) { return Torrent::getExtraStatKeys( ); }
|
|
|
|
|
|
|
|
void
|
|
|
|
addList( tr_benc * list, const KeyList& strings )
|
|
|
|
{
|
|
|
|
tr_bencListReserve( list, strings.size( ) );
|
|
|
|
foreach( const char * str, strings )
|
|
|
|
tr_bencListAddStr( list, str );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: sessionSet( const char * key, const QVariant& value )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-set" );
|
|
|
|
tr_benc * args( tr_bencDictAddDict( &top, "arguments", 1 ) );
|
|
|
|
switch( value.type( ) ) {
|
|
|
|
case QVariant::Bool: tr_bencDictAddBool ( args, key, value.toBool() ); break;
|
|
|
|
case QVariant::Int: tr_bencDictAddInt ( args, key, value.toInt() ); break;
|
|
|
|
case QVariant::Double: tr_bencDictAddReal ( args, key, value.toDouble() ); break;
|
|
|
|
case QVariant::String: tr_bencDictAddStr ( args, key, value.toString().toUtf8() ); break;
|
|
|
|
default: assert( "unknown type" );
|
|
|
|
}
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: portTest( )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "port-test" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_PORT_TEST );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
2009-12-03 15:23:43 +00:00
|
|
|
void
|
|
|
|
Session :: copyMagnetLinkToClipboard( int torrentId )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 3 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_MAGNET_LINK );
|
|
|
|
tr_benc * args = tr_bencDictAddDict( &top, "arguments", 2 );
|
|
|
|
tr_bencListAddInt( tr_bencDictAddList( args, "ids", 1 ), torrentId );
|
|
|
|
tr_bencListAddStr( tr_bencDictAddList( args, "fields", 1 ), "magnetLink" );
|
|
|
|
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
void
|
|
|
|
Session :: updatePref( int key )
|
|
|
|
{
|
|
|
|
if( myPrefs.isCore( key ) ) switch( key )
|
|
|
|
{
|
|
|
|
case Prefs :: ALT_SPEED_LIMIT_UP:
|
|
|
|
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_END:
|
|
|
|
case Prefs :: ALT_SPEED_LIMIT_TIME_ENABLED:
|
|
|
|
case Prefs :: ALT_SPEED_LIMIT_TIME_DAY:
|
|
|
|
case Prefs :: BLOCKLIST_ENABLED:
|
|
|
|
case Prefs :: BLOCKLIST_DATE:
|
2009-05-21 00:15:54 +00:00
|
|
|
case Prefs :: DHT_ENABLED:
|
2009-04-09 18:55:47 +00:00
|
|
|
case Prefs :: DOWNLOAD_DIR:
|
2009-10-19 05:25:50 +00:00
|
|
|
case Prefs :: INCOMPLETE_DIR:
|
|
|
|
case Prefs :: INCOMPLETE_DIR_ENABLED:
|
2009-04-09 18:55:47 +00:00
|
|
|
case Prefs :: PEER_LIMIT_GLOBAL:
|
|
|
|
case Prefs :: PEER_LIMIT_TORRENT:
|
|
|
|
case Prefs :: USPEED_ENABLED:
|
|
|
|
case Prefs :: USPEED:
|
|
|
|
case Prefs :: DSPEED_ENABLED:
|
|
|
|
case Prefs :: DSPEED:
|
|
|
|
case Prefs :: PEX_ENABLED:
|
|
|
|
case Prefs :: PORT_FORWARDING:
|
|
|
|
case Prefs :: PEER_PORT:
|
|
|
|
case Prefs :: PEER_PORT_RANDOM_ON_START:
|
2009-05-14 18:28:11 +00:00
|
|
|
sessionSet( myPrefs.keyStr(key), myPrefs.variant(key) );
|
|
|
|
break;
|
|
|
|
|
2009-04-14 22:05:11 +00:00
|
|
|
case Prefs :: RATIO:
|
2010-01-05 23:47:50 +00:00
|
|
|
sessionSet( "seedRatioLimit", myPrefs.variant(key) );
|
|
|
|
break;
|
2009-04-14 22:05:11 +00:00
|
|
|
case Prefs :: RATIO_ENABLED:
|
2009-05-14 18:28:11 +00:00
|
|
|
sessionSet( "seedRatioLimited", myPrefs.variant(key) );
|
2009-04-09 18:55:47 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case Prefs :: RPC_AUTH_REQUIRED:
|
|
|
|
if( mySession )
|
|
|
|
tr_sessionSetRPCEnabled( mySession, myPrefs.getBool(key) );
|
|
|
|
break;
|
|
|
|
case Prefs :: RPC_ENABLED:
|
|
|
|
if( mySession )
|
|
|
|
tr_sessionSetRPCEnabled( mySession, myPrefs.getBool(key) );
|
|
|
|
break;
|
|
|
|
case Prefs :: RPC_PASSWORD:
|
|
|
|
if( mySession )
|
|
|
|
tr_sessionSetRPCPassword( mySession, myPrefs.getString(key).toUtf8().constData() );
|
|
|
|
break;
|
|
|
|
case Prefs :: RPC_PORT:
|
|
|
|
if( mySession )
|
|
|
|
tr_sessionSetRPCPort( mySession, myPrefs.getInt(key) );
|
|
|
|
break;
|
|
|
|
case Prefs :: RPC_USERNAME:
|
|
|
|
if( mySession )
|
|
|
|
tr_sessionSetRPCUsername( mySession, myPrefs.getString(key).toUtf8().constData() );
|
|
|
|
break;
|
|
|
|
case Prefs :: RPC_WHITELIST_ENABLED:
|
|
|
|
if( mySession )
|
|
|
|
tr_sessionSetRPCWhitelistEnabled( mySession, myPrefs.getBool(key) );
|
|
|
|
break;
|
|
|
|
case Prefs :: RPC_WHITELIST:
|
|
|
|
if( mySession )
|
|
|
|
tr_sessionSetRPCWhitelist( mySession, myPrefs.getString(key).toUtf8().constData() );
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
std::cerr << "unhandled pref: " << key << std::endl;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
2009-05-03 17:37:39 +00:00
|
|
|
Session :: Session( const char * configDir, Prefs& prefs ):
|
2009-05-15 02:05:07 +00:00
|
|
|
nextUniqueTag( FIRST_UNIQUE_TAG ),
|
2009-04-09 22:42:55 +00:00
|
|
|
myBlocklistSize( -1 ),
|
2009-04-09 18:55:47 +00:00
|
|
|
myPrefs( prefs ),
|
|
|
|
mySession( 0 ),
|
2009-05-03 17:37:39 +00:00
|
|
|
myConfigDir( configDir )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
myStats.ratio = TR_RATIO_NA;
|
|
|
|
myStats.uploadedBytes = 0;
|
|
|
|
myStats.downloadedBytes = 0;
|
|
|
|
myStats.filesAdded = 0;
|
|
|
|
myStats.sessionCount = 0;
|
|
|
|
myStats.secondsActive = 0;
|
|
|
|
myCumulativeStats = myStats;
|
|
|
|
|
2009-05-03 17:37:39 +00:00
|
|
|
connect( &myHttp, SIGNAL(requestStarted(int)), this, SLOT(onRequestStarted(int)));
|
|
|
|
connect( &myHttp, SIGNAL(requestFinished(int,bool)), this, SLOT(onRequestFinished(int,bool)));
|
|
|
|
connect( &myHttp, SIGNAL(dataReadProgress(int,int)), this, SIGNAL(dataReadProgress()));
|
|
|
|
connect( &myHttp, SIGNAL(dataSendProgress(int,int)), this, SIGNAL(dataSendProgress()));
|
2009-05-03 22:00:56 +00:00
|
|
|
connect( &myHttp, SIGNAL(authenticationRequired(QString, quint16, QAuthenticator*)), this, SIGNAL(httpAuthenticationRequired()) );
|
2009-05-03 17:37:39 +00:00
|
|
|
connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(updatePref(int)) );
|
|
|
|
|
|
|
|
myBuffer.open( QIODevice::ReadWrite );
|
|
|
|
}
|
|
|
|
|
|
|
|
Session :: ~Session( )
|
|
|
|
{
|
|
|
|
stop( );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: stop( )
|
|
|
|
{
|
|
|
|
myHttp.abort( );
|
|
|
|
myUrl.clear( );
|
|
|
|
|
|
|
|
if( mySession )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2009-05-03 17:37:39 +00:00
|
|
|
tr_sessionClose( mySession );
|
|
|
|
mySession = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: restart( )
|
|
|
|
{
|
|
|
|
stop( );
|
|
|
|
start( );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: start( )
|
|
|
|
{
|
|
|
|
if( myPrefs.get<bool>(Prefs::SESSION_IS_REMOTE) )
|
|
|
|
{
|
|
|
|
const int port( myPrefs.get<int>(Prefs::SESSION_REMOTE_PORT) );
|
|
|
|
const bool auth( myPrefs.get<bool>(Prefs::SESSION_REMOTE_AUTH) );
|
|
|
|
const QString host( myPrefs.get<QString>(Prefs::SESSION_REMOTE_HOST) );
|
|
|
|
const QString user( myPrefs.get<QString>(Prefs::SESSION_REMOTE_USERNAME) );
|
|
|
|
const QString pass( myPrefs.get<QString>(Prefs::SESSION_REMOTE_PASSWORD) );
|
|
|
|
|
|
|
|
QUrl url;
|
|
|
|
url.setScheme( "http" );
|
|
|
|
url.setHost( host );
|
|
|
|
url.setPort( port );
|
|
|
|
if( auth ) {
|
|
|
|
url.setUserName( user );
|
|
|
|
url.setPassword( pass );
|
|
|
|
}
|
|
|
|
myUrl = url;
|
|
|
|
|
|
|
|
myHttp.setHost( host, port );
|
|
|
|
myHttp.setUser( user, pass );
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_benc settings;
|
|
|
|
tr_bencInitDict( &settings, 0 );
|
2009-05-03 17:37:39 +00:00
|
|
|
tr_sessionLoadSettings( &settings, myConfigDir.toUtf8().constData(), "qt" );
|
|
|
|
mySession = tr_sessionInit( "qt", myConfigDir.toUtf8().constData(), true, &settings );
|
2009-04-09 18:55:47 +00:00
|
|
|
tr_bencFree( &settings );
|
|
|
|
|
|
|
|
tr_ctor * ctor = tr_ctorNew( mySession );
|
|
|
|
int torrentCount;
|
|
|
|
tr_torrent ** torrents = tr_sessionLoadTorrents( mySession, ctor, &torrentCount );
|
|
|
|
tr_free( torrents );
|
|
|
|
tr_ctorFree( ctor );
|
|
|
|
}
|
|
|
|
|
2009-05-03 17:37:39 +00:00
|
|
|
emit sourceChanged( );
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Session :: isServer( ) const
|
|
|
|
{
|
|
|
|
return mySession != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
Session :: isLocal( ) const
|
|
|
|
{
|
|
|
|
if( mySession != 0 )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( myUrl.host() == "127.0.0.1" )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
if( !myUrl.host().compare( "localhost", Qt::CaseInsensitive ) )
|
|
|
|
return true;
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
namespace
|
|
|
|
{
|
|
|
|
tr_benc *
|
|
|
|
buildRequest( const char * method, tr_benc& top, int tag=-1 )
|
|
|
|
{
|
|
|
|
tr_bencInitDict( &top, 3 );
|
|
|
|
tr_bencDictAddStr( &top, "method", method );
|
|
|
|
if( tag >= 0 )
|
|
|
|
tr_bencDictAddInt( &top, "tag", tag );
|
|
|
|
return tr_bencDictAddDict( &top, "arguments", 0 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
addOptionalIds( tr_benc * args, const QSet<int>& ids )
|
|
|
|
{
|
|
|
|
if( !ids.isEmpty( ) )
|
|
|
|
{
|
|
|
|
tr_benc * idList( tr_bencDictAddList( args, "ids", ids.size( ) ) );
|
|
|
|
foreach( int i, ids )
|
|
|
|
tr_bencListAddInt( idList, i );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2009-04-12 21:15:35 +00:00
|
|
|
void
|
2009-04-18 23:18:28 +00:00
|
|
|
Session :: torrentSet( const QSet<int>& ids, const QString& key, double value )
|
2009-04-12 21:15:35 +00:00
|
|
|
{
|
2009-04-18 23:18:28 +00:00
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
tr_benc * args = tr_bencDictAddDict( &top, "arguments", 2 );
|
|
|
|
tr_bencDictAddReal( args, key.toUtf8().constData(), value );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
2009-04-12 21:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-18 23:18:28 +00:00
|
|
|
Session :: torrentSet( const QSet<int>& ids, const QString& key, int value )
|
2009-04-12 21:15:35 +00:00
|
|
|
{
|
2009-04-18 23:18:28 +00:00
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
tr_benc * args = tr_bencDictAddDict( &top, "arguments", 2 );
|
|
|
|
tr_bencDictAddInt( args, key.toUtf8().constData(), value );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
2009-04-12 21:15:35 +00:00
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
void
|
2009-04-18 23:18:28 +00:00
|
|
|
Session :: torrentSet( const QSet<int>& ids, const QString& key, bool value )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2009-04-18 23:18:28 +00:00
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
tr_benc * args = tr_bencDictAddDict( &top, "arguments", 2 );
|
|
|
|
tr_bencDictAddBool( args, key.toUtf8().constData(), value );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-18 23:18:28 +00:00
|
|
|
Session :: torrentSet( const QSet<int>& ids, const QString& key, const QList<int>& value )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set" );
|
|
|
|
tr_benc * args( tr_bencDictAddDict( &top, "arguments", 2 ) );
|
2009-04-18 23:18:28 +00:00
|
|
|
addOptionalIds( args, ids );
|
2009-04-12 21:15:35 +00:00
|
|
|
tr_benc * list( tr_bencDictAddList( args, key.toUtf8().constData(), value.size( ) ) );
|
2009-04-09 18:55:47 +00:00
|
|
|
foreach( int i, value )
|
|
|
|
tr_bencListAddInt( list, i );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
2009-05-13 15:54:04 +00:00
|
|
|
void
|
|
|
|
Session :: torrentSetLocation( const QSet<int>& ids, const QString& location, bool doMove )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-set-location" );
|
|
|
|
tr_benc * args( tr_bencDictAddDict( &top, "arguments", 3 ) );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
tr_bencDictAddStr( args, "location", location.toUtf8().constData() );
|
|
|
|
tr_bencDictAddBool( args, "move", doMove );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
2009-04-09 18:55:47 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
Session :: refreshTorrents( const QSet<int>& ids )
|
|
|
|
{
|
|
|
|
if( ids.empty( ) )
|
|
|
|
{
|
|
|
|
refreshAllTorrents( );
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 3 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_SOME_TORRENTS );
|
|
|
|
tr_benc * args( tr_bencDictAddDict( &top, "arguments", 2 ) );
|
|
|
|
addList( tr_bencDictAddList( args, "fields", 0 ), getStatKeys( ) );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-04-18 23:18:28 +00:00
|
|
|
Session :: refreshExtraStats( const QSet<int>& ids )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 3 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_SOME_TORRENTS );
|
|
|
|
tr_benc * args( tr_bencDictAddDict( &top, "arguments", 2 ) );
|
2009-04-18 23:18:28 +00:00
|
|
|
addOptionalIds( args, ids );
|
2009-04-09 18:55:47 +00:00
|
|
|
addList( tr_bencDictAddList( args, "fields", 0 ), getStatKeys( ) + getExtraStatKeys( ));
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: sendTorrentRequest( const char * request, const QSet<int>& ids )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_benc * args( buildRequest( request, top ) );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
|
|
|
|
refreshTorrents( ids );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-03 17:37:39 +00:00
|
|
|
Session :: pauseTorrents( const QSet<int>& ids )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
sendTorrentRequest( "torrent-stop", ids );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-05-03 17:37:39 +00:00
|
|
|
Session :: startTorrents( const QSet<int>& ids )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
sendTorrentRequest( "torrent-start", ids );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: refreshActiveTorrents( )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 3 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_SOME_TORRENTS );
|
|
|
|
tr_benc * args( tr_bencDictAddDict( &top, "arguments", 2 ) );
|
|
|
|
tr_bencDictAddStr( args, "ids", "recently-active" );
|
|
|
|
addList( tr_bencDictAddList( args, "fields", 0 ), getStatKeys( ) );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: refreshAllTorrents( )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 3 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_ALL_TORRENTS );
|
|
|
|
tr_benc * args( tr_bencDictAddDict( &top, "arguments", 1 ) );
|
|
|
|
addList( tr_bencDictAddList( args, "fields", 0 ), getStatKeys( ) );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: initTorrents( const QSet<int>& ids )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
const int tag( ids.isEmpty() ? TAG_ALL_TORRENTS : TAG_SOME_TORRENTS );
|
|
|
|
tr_benc * args( buildRequest( "torrent-get", top, tag ) );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
addList( tr_bencDictAddList( args, "fields", 0 ), getStatKeys()+getInfoKeys() );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: refreshSessionStats( )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-stats" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_SESSION_STATS );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: refreshSessionInfo( )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "session-get" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_SESSION_INFO );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: updateBlocklist( )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "blocklist-update" );
|
|
|
|
tr_bencDictAddInt( &top, "tag", TAG_BLOCKLIST_UPDATE );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: exec( const tr_benc * request )
|
|
|
|
{
|
2009-06-02 01:48:48 +00:00
|
|
|
char * str = tr_bencToStr( request, TR_FMT_JSON_LEAN, NULL );
|
2009-04-09 18:55:47 +00:00
|
|
|
exec( str );
|
|
|
|
tr_free( str );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: localSessionCallback( tr_session * session, const char * json, size_t len, void * self )
|
|
|
|
{
|
|
|
|
Q_UNUSED( session );
|
|
|
|
|
|
|
|
((Session*)self)->parseResponse( json, len );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: exec( const char * request )
|
|
|
|
{
|
|
|
|
if( mySession )
|
|
|
|
{
|
|
|
|
tr_rpc_request_exec_json( mySession, request, strlen( request ), localSessionCallback, this );
|
|
|
|
}
|
2009-05-03 17:37:39 +00:00
|
|
|
else if( !myUrl.isEmpty( ) )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
|
|
|
static const QString path( "/transmission/rpc" );
|
|
|
|
QHttpRequestHeader header( "POST", path );
|
|
|
|
header.setValue( "User-Agent", QCoreApplication::instance()->applicationName() + "/" + LONG_VERSION_STRING );
|
|
|
|
header.setValue( "Content-Type", "application/json; charset=UTF-8" );
|
2009-05-08 14:56:11 +00:00
|
|
|
if( !mySessionId.isEmpty( ) )
|
|
|
|
header.setValue( TR_RPC_SESSION_ID_HEADER, mySessionId );
|
2009-05-13 15:54:04 +00:00
|
|
|
QBuffer * reqbuf = new QBuffer;
|
|
|
|
reqbuf->setData( QByteArray( request ) );
|
|
|
|
myHttp.request( header, reqbuf, &myBuffer );
|
2009-04-09 18:55:47 +00:00
|
|
|
#ifdef DEBUG_HTTP
|
|
|
|
std::cerr << "sending " << qPrintable(header.toString()) << "\nBody:\n" << request << std::endl;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: onRequestStarted( int id )
|
|
|
|
{
|
|
|
|
Q_UNUSED( id );
|
|
|
|
|
|
|
|
assert( myBuffer.atEnd( ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: onRequestFinished( int id, bool error )
|
|
|
|
{
|
|
|
|
Q_UNUSED( id );
|
2009-05-13 15:54:04 +00:00
|
|
|
QIODevice * sourceDevice = myHttp.currentSourceDevice( );
|
2009-04-09 18:55:47 +00:00
|
|
|
|
2009-05-08 14:56:11 +00:00
|
|
|
QHttpResponseHeader response = myHttp.lastResponse();
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
#ifdef DEBUG_HTTP
|
|
|
|
std::cerr << "http request " << id << " ended.. response header: "
|
|
|
|
<< qPrintable( myHttp.lastResponse().toString() )
|
|
|
|
<< std::endl
|
|
|
|
<< "json: " << myBuffer.buffer( ).constData( )
|
|
|
|
<< std::endl;
|
|
|
|
#endif
|
|
|
|
|
2009-05-09 00:23:42 +00:00
|
|
|
if( ( response.statusCode() == 409 ) && ( myBuffer.buffer().indexOf("invalid session-id") != -1 ) )
|
2009-05-08 14:56:11 +00:00
|
|
|
{
|
|
|
|
// we got a 409 telling us our session id has expired.
|
|
|
|
// update it and resubmit the request.
|
|
|
|
mySessionId = response.value( TR_RPC_SESSION_ID_HEADER );
|
2009-05-13 15:54:04 +00:00
|
|
|
exec( qobject_cast<QBuffer*>(sourceDevice)->buffer().constData() );
|
2009-05-08 14:56:11 +00:00
|
|
|
}
|
|
|
|
else if( error )
|
|
|
|
{
|
2009-04-09 18:55:47 +00:00
|
|
|
std::cerr << "http error: " << qPrintable(myHttp.errorString()) << std::endl;
|
2009-05-08 14:56:11 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-04-09 18:55:47 +00:00
|
|
|
const QByteArray& response( myBuffer.buffer( ) );
|
|
|
|
const char * json( response.constData( ) );
|
|
|
|
int jsonLength( response.size( ) );
|
2009-05-03 17:37:39 +00:00
|
|
|
if( jsonLength>0 && json[jsonLength-1] == '\n' ) --jsonLength;
|
2009-04-09 18:55:47 +00:00
|
|
|
parseResponse( json, jsonLength );
|
|
|
|
}
|
|
|
|
|
2009-06-01 14:11:52 +00:00
|
|
|
delete sourceDevice;
|
2009-04-09 18:55:47 +00:00
|
|
|
myBuffer.buffer( ).clear( );
|
|
|
|
myBuffer.reset( );
|
|
|
|
assert( myBuffer.bytesAvailable( ) < 1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: parseResponse( const char * json, size_t jsonLength )
|
|
|
|
{
|
|
|
|
tr_benc top;
|
|
|
|
const uint8_t * end( 0 );
|
2009-06-01 18:57:37 +00:00
|
|
|
const int err( tr_jsonParse( "rpc", json, jsonLength, &top, &end ) );
|
2009-04-09 18:55:47 +00:00
|
|
|
if( !err )
|
|
|
|
{
|
2009-05-15 02:05:07 +00:00
|
|
|
int64_t tag = -1;
|
|
|
|
const char * result = NULL;
|
|
|
|
tr_benc * args = NULL;
|
|
|
|
|
|
|
|
tr_bencDictFindInt ( &top, "tag", &tag );
|
|
|
|
tr_bencDictFindStr ( &top, "result", &result );
|
|
|
|
tr_bencDictFindDict( &top, "arguments", &args );
|
|
|
|
|
|
|
|
emit executed( tag, result, args );
|
|
|
|
|
|
|
|
tr_benc * torrents;
|
2009-04-09 18:55:47 +00:00
|
|
|
const char * str;
|
2009-05-15 02:05:07 +00:00
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
if( tr_bencDictFindInt( &top, "tag", &tag ) )
|
|
|
|
{
|
|
|
|
switch( tag )
|
|
|
|
{
|
|
|
|
case TAG_SOME_TORRENTS:
|
|
|
|
case TAG_ALL_TORRENTS:
|
|
|
|
if( tr_bencDictFindDict( &top, "arguments", &args ) ) {
|
|
|
|
if( tr_bencDictFindList( args, "torrents", &torrents ) )
|
|
|
|
emit torrentsUpdated( torrents, tag==TAG_ALL_TORRENTS );
|
|
|
|
if( tr_bencDictFindList( args, "removed", &torrents ) )
|
|
|
|
emit torrentsRemoved( torrents );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAG_SESSION_STATS:
|
|
|
|
if( tr_bencDictFindDict( &top, "arguments", &args ) )
|
|
|
|
updateStats( args );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAG_SESSION_INFO:
|
|
|
|
if( tr_bencDictFindDict( &top, "arguments", &args ) )
|
|
|
|
updateInfo( args );
|
|
|
|
break;
|
|
|
|
|
|
|
|
case TAG_BLOCKLIST_UPDATE: {
|
|
|
|
int64_t intVal = 0;
|
|
|
|
if( tr_bencDictFindDict( &top, "arguments", &args ) )
|
|
|
|
if( tr_bencDictFindInt( args, "blocklist-size", &intVal ) )
|
|
|
|
setBlocklistSize( intVal );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
case TAG_PORT_TEST: {
|
|
|
|
tr_bool isOpen = 0;
|
|
|
|
if( tr_bencDictFindDict( &top, "arguments", &args ) )
|
|
|
|
tr_bencDictFindBool( args, "port-is-open", &isOpen );
|
|
|
|
emit portTested( (bool)isOpen );
|
|
|
|
}
|
|
|
|
|
2009-12-03 15:23:43 +00:00
|
|
|
case TAG_MAGNET_LINK: {
|
|
|
|
tr_benc * args;
|
|
|
|
tr_benc * torrents;
|
|
|
|
tr_benc * child;
|
|
|
|
const char * str;
|
|
|
|
if( tr_bencDictFindDict( &top, "arguments", &args )
|
|
|
|
&& tr_bencDictFindList( args, "torrents", &torrents )
|
|
|
|
&& (( child = tr_bencListChild( torrents, 0 )))
|
|
|
|
&& tr_bencDictFindStr( child, "magnetLink", &str ) )
|
|
|
|
QApplication::clipboard()->setText( str );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2009-04-09 18:55:47 +00:00
|
|
|
case TAG_ADD_TORRENT:
|
|
|
|
str = "";
|
|
|
|
if( tr_bencDictFindStr( &top, "result", &str ) && strcmp( str, "success" ) ) {
|
|
|
|
QMessageBox * d = new QMessageBox( QMessageBox::Information,
|
|
|
|
tr( "Add Torrent" ),
|
|
|
|
QString::fromUtf8(str),
|
|
|
|
QMessageBox::Close,
|
|
|
|
QApplication::activeWindow());
|
2010-01-05 23:47:50 +00:00
|
|
|
QPixmap pixmap;
|
2009-04-09 18:55:47 +00:00
|
|
|
QIcon icon = QtIconLoader :: icon( "dialog-information" );
|
|
|
|
if( !icon.isNull( ) ) {
|
|
|
|
const int size = QApplication::style()->pixelMetric( QStyle::PM_LargeIconSize );
|
|
|
|
d->setIconPixmap( icon.pixmap( size, size ) );
|
|
|
|
}
|
|
|
|
connect( d, SIGNAL(rejected()), d, SLOT(deleteLater()) );
|
|
|
|
d->show( );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: updateStats( tr_benc * d, struct tr_session_stats * stats )
|
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
|
|
|
|
if( tr_bencDictFindInt( d, "uploadedBytes", &i ) )
|
|
|
|
stats->uploadedBytes = i;
|
|
|
|
if( tr_bencDictFindInt( d, "downloadedBytes", &i ) )
|
|
|
|
stats->downloadedBytes = i;
|
|
|
|
if( tr_bencDictFindInt( d, "filesAdded", &i ) )
|
|
|
|
stats->filesAdded = i;
|
|
|
|
if( tr_bencDictFindInt( d, "sessionCount", &i ) )
|
|
|
|
stats->sessionCount = i;
|
|
|
|
if( tr_bencDictFindInt( d, "secondsActive", &i ) )
|
|
|
|
stats->secondsActive = i;
|
|
|
|
|
|
|
|
stats->ratio = tr_getRatio( stats->uploadedBytes, stats->downloadedBytes );
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: updateStats( tr_benc * d )
|
|
|
|
{
|
|
|
|
tr_benc * c;
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( d, "current-stats", &c ) )
|
|
|
|
updateStats( c, &myStats );
|
|
|
|
|
|
|
|
if( tr_bencDictFindDict( d, "cumulative-stats", &c ) )
|
|
|
|
updateStats( c, &myCumulativeStats );
|
|
|
|
|
|
|
|
emit statsUpdated( );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: updateInfo( tr_benc * d )
|
|
|
|
{
|
|
|
|
int64_t i;
|
|
|
|
const char * str;
|
|
|
|
disconnect( &myPrefs, SIGNAL(changed(int)), this, SLOT(updatePref(int)) );
|
|
|
|
|
|
|
|
for( int i=Prefs::FIRST_CORE_PREF; i<=Prefs::LAST_CORE_PREF; ++i )
|
|
|
|
{
|
|
|
|
const tr_benc * b( tr_bencDictFind( d, myPrefs.keyStr( i ) ) );
|
|
|
|
|
|
|
|
if( !b )
|
|
|
|
continue;
|
|
|
|
|
|
|
|
switch( myPrefs.type( i ) )
|
|
|
|
{
|
|
|
|
case QVariant :: Int: {
|
|
|
|
int64_t val;
|
|
|
|
if( tr_bencGetInt( b, &val ) )
|
|
|
|
myPrefs.set( i, (int)val );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QVariant :: Double: {
|
|
|
|
double val;
|
|
|
|
if( tr_bencGetReal( b, &val ) )
|
|
|
|
myPrefs.set( i, val );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case QVariant :: Bool: {
|
|
|
|
tr_bool val;
|
|
|
|
if( tr_bencGetBool( b, &val ) )
|
|
|
|
myPrefs.set( i, (bool)val );
|
|
|
|
break;
|
|
|
|
}
|
2009-04-11 18:25:12 +00:00
|
|
|
case TrTypes :: FilterModeType:
|
|
|
|
case TrTypes :: SortModeType:
|
2009-04-09 18:55:47 +00:00
|
|
|
case QVariant :: String: {
|
|
|
|
const char * val;
|
|
|
|
if( tr_bencGetStr( b, &val ) )
|
|
|
|
myPrefs.set( i, QString(val) );
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Use the C API to get settings that, for security reasons, aren't supported by RPC */
|
|
|
|
if( mySession != 0 )
|
|
|
|
{
|
|
|
|
myPrefs.set( Prefs::RPC_ENABLED, tr_sessionIsRPCEnabled ( mySession ) );
|
|
|
|
myPrefs.set( Prefs::RPC_AUTH_REQUIRED, tr_sessionIsRPCPasswordEnabled ( mySession ) );
|
|
|
|
myPrefs.set( Prefs::RPC_PASSWORD, tr_sessionGetRPCPassword ( mySession ) );
|
|
|
|
myPrefs.set( Prefs::RPC_PORT, tr_sessionGetRPCPort ( mySession ) );
|
|
|
|
myPrefs.set( Prefs::RPC_USERNAME, tr_sessionGetRPCUsername ( mySession ) );
|
|
|
|
myPrefs.set( Prefs::RPC_WHITELIST_ENABLED, tr_sessionGetRPCWhitelistEnabled ( mySession ) );
|
|
|
|
myPrefs.set( Prefs::RPC_WHITELIST, tr_sessionGetRPCWhitelist ( mySession ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
if( tr_bencDictFindInt( d, "blocklist-size", &i ) && i!=blocklistSize( ) )
|
|
|
|
setBlocklistSize( i );
|
|
|
|
|
|
|
|
if( tr_bencDictFindStr( d, "version", &str ) && ( mySessionVersion != str ) )
|
|
|
|
mySessionVersion = str;
|
|
|
|
|
|
|
|
//std::cerr << "Session :: updateInfo end" << std::endl;
|
|
|
|
connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(updatePref(int)) );
|
|
|
|
|
|
|
|
emit sessionUpdated( );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: setBlocklistSize( int64_t i )
|
|
|
|
{
|
|
|
|
myBlocklistSize = i;
|
|
|
|
|
|
|
|
emit blocklistUpdated( i );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: addTorrent( QString filename )
|
2009-10-05 19:53:38 +00:00
|
|
|
{
|
|
|
|
addTorrent( filename, myPrefs.getString( Prefs::DOWNLOAD_DIR ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-12-03 17:23:53 +00:00
|
|
|
Session :: addTorrent( QString key, QString localPath )
|
2009-04-09 18:55:47 +00:00
|
|
|
{
|
2009-12-03 17:23:53 +00:00
|
|
|
tr_benc top, *args;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-add" );
|
|
|
|
args = tr_bencDictAddDict( &top, "arguments", 3 );
|
|
|
|
tr_bencDictAddStr( args, "download-dir", qPrintable(localPath) );
|
|
|
|
tr_bencDictAddInt( args, "paused", !myPrefs.getBool( Prefs::START ) );
|
|
|
|
|
|
|
|
// if "key" is a readable local file, add it as metadata...
|
|
|
|
// otherwise it's probably a URL or magnet link, so pass it along
|
|
|
|
// for the daemon to handle
|
|
|
|
QFile file( key );
|
2009-04-09 18:55:47 +00:00
|
|
|
file.open( QIODevice::ReadOnly );
|
2009-05-03 17:37:39 +00:00
|
|
|
const QByteArray raw( file.readAll( ) );
|
2009-04-09 18:55:47 +00:00
|
|
|
file.close( );
|
|
|
|
if( !raw.isEmpty( ) )
|
|
|
|
{
|
|
|
|
int b64len = 0;
|
|
|
|
char * b64 = tr_base64_encode( raw.constData(), raw.size(), &b64len );
|
|
|
|
tr_bencDictAddRaw( args, "metainfo", b64, b64len );
|
|
|
|
tr_free( b64 );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
2009-12-03 17:23:53 +00:00
|
|
|
else
|
|
|
|
{
|
|
|
|
tr_bencDictAddStr( args, "filename", key.toUtf8().constData() );
|
|
|
|
}
|
|
|
|
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
2009-04-09 18:55:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: removeTorrents( const QSet<int>& ids, bool deleteFiles )
|
|
|
|
{
|
|
|
|
if( !ids.isEmpty( ) )
|
|
|
|
{
|
|
|
|
tr_benc top, *args;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-remove" );
|
|
|
|
args = tr_bencDictAddDict( &top, "arguments", 2 );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
tr_bencDictAddInt( args, "delete-local-data", deleteFiles );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: verifyTorrents( const QSet<int>& ids )
|
|
|
|
{
|
|
|
|
if( !ids.isEmpty( ) )
|
|
|
|
{
|
|
|
|
tr_benc top, *args;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-verify" );
|
|
|
|
args = tr_bencDictAddDict( &top, "arguments", 1 );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: reannounceTorrents( const QSet<int>& ids )
|
|
|
|
{
|
|
|
|
if( !ids.isEmpty( ) )
|
|
|
|
{
|
|
|
|
tr_benc top, *args;
|
|
|
|
tr_bencInitDict( &top, 2 );
|
|
|
|
tr_bencDictAddStr( &top, "method", "torrent-reannounce" );
|
|
|
|
args = tr_bencDictAddDict( &top, "arguments", 1 );
|
|
|
|
addOptionalIds( args, ids );
|
|
|
|
exec( &top );
|
|
|
|
tr_bencFree( &top );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/***
|
|
|
|
****
|
|
|
|
***/
|
|
|
|
|
|
|
|
void
|
|
|
|
Session :: launchWebInterface( )
|
|
|
|
{
|
|
|
|
QUrl url;
|
2009-05-08 03:38:46 +00:00
|
|
|
if( !mySession ) // remote session
|
2009-04-09 18:55:47 +00:00
|
|
|
url = myUrl;
|
|
|
|
else { // local session
|
2009-05-08 03:38:46 +00:00
|
|
|
url.setScheme( "http" );
|
2009-04-09 18:55:47 +00:00
|
|
|
url.setHost( "localhost" );
|
|
|
|
url.setPort( myPrefs.getInt( Prefs::RPC_PORT ) );
|
|
|
|
}
|
|
|
|
QDesktopServices :: openUrl( url );
|
|
|
|
}
|