Changeset 8323 for trunk/qt/session.cc


Ignore:
Timestamp:
May 3, 2009, 5:37:39 PM (13 years ago)
Author:
charles
Message:

(trunk qt) Remember between runs whether we were running a local or remote session. Add a gui dialog for switching sessions during runtime.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/session.cc

    r8255 r8323  
    3333#include "qticonloader.h"
    3434#include "session.h"
     35#include "session-dialog.h"
    3536#include "torrent.h"
    3637
     
    175176***/
    176177
    177 Session :: Session( const char * configDir, Prefs& prefs, const char * url, bool paused ):
     178Session :: Session( const char * configDir, Prefs& prefs ):
    178179    myBlocklistSize( -1 ),
    179180    myPrefs( prefs ),
    180181    mySession( 0 ),
    181     myUrl( url )
     182    myConfigDir( configDir )
    182183{
    183184    myStats.ratio = TR_RATIO_NA;
     
    189190    myCumulativeStats = myStats;
    190191
    191     if( url != 0 )
    192     {
    193         connect( &myHttp, SIGNAL(requestStarted(int)), this, SLOT(onRequestStarted(int)));
    194         connect( &myHttp, SIGNAL(requestFinished(int,bool)), this, SLOT(onRequestFinished(int,bool)));
    195         connect( &myHttp, SIGNAL(dataReadProgress(int,int)), this, SIGNAL(dataReadProgress()));
    196         connect( &myHttp, SIGNAL(dataSendProgress(int,int)), this, SIGNAL(dataSendProgress()));
    197         myHttp.setHost( myUrl.host( ), myUrl.port( ) );
    198         myHttp.setUser( myUrl.userName( ), myUrl.password( ) );
    199         myBuffer.open( QIODevice::ReadWrite );
    200 
    201         if( paused )
    202             exec( "{ \"method\": \"torrent-stop\" }" );
     192    connect( &myHttp, SIGNAL(requestStarted(int)), this, SLOT(onRequestStarted(int)));
     193    connect( &myHttp, SIGNAL(requestFinished(int,bool)), this, SLOT(onRequestFinished(int,bool)));
     194    connect( &myHttp, SIGNAL(dataReadProgress(int,int)), this, SIGNAL(dataReadProgress()));
     195    connect( &myHttp, SIGNAL(dataSendProgress(int,int)), this, SIGNAL(dataSendProgress()));
     196    connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(updatePref(int)) );
     197
     198    myBuffer.open( QIODevice::ReadWrite );
     199}
     200
     201Session :: ~Session( )
     202{
     203    stop( );
     204}
     205
     206/***
     207****
     208***/
     209
     210void
     211Session :: stop( )
     212{
     213    myHttp.abort( );
     214    myUrl.clear( );
     215
     216    if( mySession )
     217    {
     218        tr_sessionClose( mySession );
     219        mySession = 0;
     220    }
     221}
     222
     223void
     224Session :: restart( )
     225{
     226    stop( );
     227    start( );
     228}
     229
     230void
     231Session :: start( )
     232{
     233    if( myPrefs.get<bool>(Prefs::SESSION_IS_REMOTE) )
     234    {
     235        const int port( myPrefs.get<int>(Prefs::SESSION_REMOTE_PORT) );
     236        const bool auth( myPrefs.get<bool>(Prefs::SESSION_REMOTE_AUTH) );
     237        const QString host( myPrefs.get<QString>(Prefs::SESSION_REMOTE_HOST) );
     238        const QString user( myPrefs.get<QString>(Prefs::SESSION_REMOTE_USERNAME) );
     239        const QString pass( myPrefs.get<QString>(Prefs::SESSION_REMOTE_PASSWORD) );
     240
     241        QUrl url;
     242        url.setScheme( "http" );
     243        url.setHost( host );
     244        url.setPort( port );
     245        if( auth ) {
     246            url.setUserName( user );
     247            url.setPassword( pass );
     248        }
     249        myUrl = url;
     250
     251        myHttp.setHost( host, port );
     252        myHttp.setUser( user, pass );
    203253    }
    204254    else
     
    207257        tr_bencInitDict( &settings, 0 );
    208258        tr_sessionGetDefaultSettings( &settings );
    209         tr_sessionLoadSettings( &settings, configDir, "qt" );
    210         mySession = tr_sessionInit( "qt", configDir, true, &settings );
     259        tr_sessionLoadSettings( &settings, myConfigDir.toUtf8().constData(), "qt" );
     260        mySession = tr_sessionInit( "qt", myConfigDir.toUtf8().constData(), true, &settings );
    211261        tr_bencFree( &settings );
    212262
    213263        tr_ctor * ctor = tr_ctorNew( mySession );
    214         if( paused )
    215             tr_ctorSetPaused( ctor, TR_FORCE, TRUE );
    216264        int torrentCount;
    217265        tr_torrent ** torrents = tr_sessionLoadTorrents( mySession, ctor, &torrentCount );
     
    220268    }
    221269
    222     connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(updatePref(int)) );
    223 }
    224 
    225 Session :: ~Session( )
    226 {
    227     if( mySession )
    228         tr_sessionClose( mySession );
     270    emit sourceChanged( );
    229271}
    230272
     
    387429
    388430void
    389 Session :: pause( const QSet<int>& ids )
     431Session :: pauseTorrents( const QSet<int>& ids )
    390432{
    391433    sendTorrentRequest( "torrent-stop", ids );
     
    393435
    394436void
    395 Session :: start( const QSet<int>& ids )
     437Session :: startTorrents( const QSet<int>& ids )
    396438{
    397439    sendTorrentRequest( "torrent-start", ids );
     
    497539        tr_rpc_request_exec_json( mySession, request, strlen( request ), localSessionCallback, this );
    498540    }
    499     else
     541    else if( !myUrl.isEmpty( ) )
    500542    {
    501543        const QByteArray data( request, strlen( request ) );
     
    538580        const char * json( response.constData( ) );
    539581        int jsonLength( response.size( ) );
    540         if( json[jsonLength-1] == '\n' ) --jsonLength;
     582        if( jsonLength>0 && json[jsonLength-1] == '\n' ) --jsonLength;
    541583
    542584        parseResponse( json, jsonLength );
     
    743785    QFile file( filename );
    744786    file.open( QIODevice::ReadOnly );
    745     QByteArray raw( file.readAll( ) );
     787    const QByteArray raw( file.readAll( ) );
    746788    file.close( );
    747789
Note: See TracChangeset for help on using the changeset viewer.