Changeset 8323


Ignore:
Timestamp:
May 3, 2009, 5:37:39 PM (12 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.

Location:
trunk/qt
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • trunk/qt/app.cc

    r8233 r8323  
    2828#include "options.h"
    2929#include "prefs.h"
     30#include "session.h"
     31#include "session-dialog.h"
    3032#include "torrent-model.h"
    31 #include "session.h"
    3233#include "utils.h"
    3334#include "watchdir.h"
     
    4142        { 'g', "config-dir", "Where to look for configuration files", "g", 1, "<path>" },
    4243        { 'm', "minimized",  "Start minimized in system tray", "m", 0, NULL },
    43         { 'p', "paused",  "Pause all torrents on sartup", "p", 0, NULL },
    44         { 'r', "remote",  "Remotely control a pre-existing session", "r", 1, "<URL>" },
    4544        { 'v', "version", "Show version number and exit", "v", 0, NULL },
    4645        { 0, NULL, NULL, NULL, 0, NULL }
     
    9796    // parse the command-line arguments
    9897    int c;
    99     bool paused = false;
    10098    bool minimized = false;
    10199    const char * optarg;
    102100    const char * configDir = 0;
    103     const char * url = 0;
    104101    while( ( c = tr_getopt( getUsage( ), argc, (const char**)argv, opts, &optarg ) ) ) {
    105102        switch( c ) {
    106103            case 'g': configDir = optarg; break;
    107104            case 'm': minimized = true; break;
    108             case 'p': paused = true; break;
    109             case 'r': url = optarg; break;
    110105            case 'v':        Utils::toStderr( QObject::tr( "transmission %1" ).arg( LONG_VERSION_STRING ) ); exit( 0 ); break;
    111106            case TR_OPT_ERR: Utils::toStderr( QObject::tr( "Invalid option" ) ); showUsage( ); break;
     
    118113        configDir = tr_getDefaultConfigDir( MY_NAME );
    119114
     115    // is this the first time we've run transmission?
     116    const bool firstTime = !QFile(QDir(configDir).absoluteFilePath("settings.json")).exists();
     117
    120118    myPrefs = new Prefs ( configDir );
    121     mySession = new Session( configDir, *myPrefs, url, paused );
     119    mySession = new Session( configDir, *myPrefs );
    122120    myModel = new TorrentModel( *myPrefs );
    123121    myWindow = new TrMainWindow( *mySession, *myPrefs, *myModel, minimized );
    124122    myWatchDir = new WatchDir( *myModel );
    125123
    126     /* when the session gets torrent info, update the model */
     124    // when the session gets torrent info, update the model
    127125    connect( mySession, SIGNAL(torrentsUpdated(tr_benc*,bool)), myModel, SLOT(updateTorrents(tr_benc*,bool)) );
    128126    connect( mySession, SIGNAL(torrentsUpdated(tr_benc*,bool)), myWindow, SLOT(refreshActionSensitivity()) );
    129127    connect( mySession, SIGNAL(torrentsRemoved(tr_benc*)), myModel, SLOT(removeTorrents(tr_benc*)) );
    130     /* when the model sees a torrent for the first time, ask the session for full info on it */
     128    // when the model sees a torrent for the first time, ask the session for full info on it
    131129    connect( myModel, SIGNAL(torrentsAdded(QSet<int>)), mySession, SLOT(initTorrents(QSet<int>)) );
    132130
     
    134132    mySession->refreshSessionStats( );
    135133
    136     /* when torrents are added to the watch directory, tell the session */
     134    // when torrents are added to the watch directory, tell the session
    137135    connect( myWatchDir, SIGNAL(torrentFileAdded(QString)), this, SLOT(addTorrent(QString)) );
    138136
    139     /* init from preferences */
     137    // init from preferences
    140138    QList<int> initKeys;
    141139    initKeys << Prefs::DIR_WATCH;
     
    163161
    164162    maybeUpdateBlocklist( );
     163
     164    if( !firstTime )
     165        mySession->restart( );
     166    else {
     167        QDialog * d = new SessionDialog( *mySession, *myPrefs, myWindow );
     168        d->show( );
     169    }
    165170}
    166171
  • trunk/qt/mainwin.cc

    r8322 r8323  
    3737#include "prefs-dialog.h"
    3838#include "session.h"
     39#include "session-dialog.h"
    3940#include "speed.h"
    4041#include "stats-dialog.h"
     
    100101
    101102    ui.setupUi( this );
    102 
    103     QString title( "Transmission" );
    104     const QUrl remoteUrl( session.getRemoteUrl( ) );
    105     if( !remoteUrl.isEmpty( ) )
    106         title += tr( " - %1" ).arg( remoteUrl.toString(QUrl::RemoveUserInfo) );
    107     setWindowTitle( title );
    108103
    109104    QStyle * style = this->style();
     
    163158    connect( ui.action_OpenFolder, SIGNAL(triggered()), this, SLOT(openFolder()));
    164159    connect( ui.action_Properties, SIGNAL(triggered()), this, SLOT(openProperties()));
     160    connect( ui.action_SessionDialog, SIGNAL(triggered()), this, SLOT(openSessionDialog()));
    165161    connect( ui.listView, SIGNAL(activated(const QModelIndex&)), ui.action_Properties, SLOT(trigger()));
    166162
     
    253249        refreshPref( key );
    254250
     251    connect( &mySession, SIGNAL(sourceChanged()), this, SLOT(onSessionSourceChanged()) );
    255252    connect( &mySession, SIGNAL(statsUpdated()), this, SLOT(refreshStatusBar()) );
    256253    connect( &mySession, SIGNAL(dataReadProgress()), this, SLOT(dataReadProgress()) );
     
    266263    refreshActionSensitivity( );
    267264    refreshStatusBar( );
     265    refreshTitle( );
    268266    refreshVisibleCount( );
    269267}
     
    271269TrMainWindow :: ~TrMainWindow( )
    272270{
     271}
     272
     273/****
     274*****
     275****/
     276
     277void
     278TrMainWindow :: onSessionSourceChanged( )
     279{
     280    myModel.clear( );
     281    refreshTitle( );
     282    refreshVisibleCount( );
     283    refreshActionSensitivity( );
     284    refreshStatusBar( );
    273285}
    274286
     
    578590
    579591void
     592TrMainWindow :: openSessionDialog( )
     593{
     594    SessionDialog * d = new SessionDialog( mySession, myPrefs, this );
     595    d->show( );
     596}
     597
     598void
    580599TrMainWindow :: openFolder( )
    581600{
     
    595614    snprintf( url, sizeof( url ), fmt, major, minor/10 );
    596615    QDesktopServices :: openUrl( QUrl( QString( url ) ) );
     616}
     617
     618void
     619TrMainWindow :: refreshTitle( )
     620{
     621    QString title( "Transmission" );
     622    const QUrl url( mySession.getRemoteUrl( ) );
     623    if( !url.isEmpty() )
     624        title += tr( " - %1" ).arg( url.toString(QUrl::RemoveUserInfo) );
     625    setWindowTitle( title );
    597626}
    598627
     
    724753TrMainWindow :: startSelected( )
    725754{
    726     mySession.start( getSelectedTorrents( ) );
     755    mySession.startTorrents( getSelectedTorrents( ) );
    727756}
    728757void
    729758TrMainWindow :: pauseSelected( )
    730759{
    731     mySession.pause( getSelectedTorrents( ) );
     760    mySession.pauseTorrents( getSelectedTorrents( ) );
    732761}
    733762void
    734763TrMainWindow :: startAll( )
    735764{
    736     mySession.start( );
     765    mySession.startTorrents( );
    737766}
    738767void
    739768TrMainWindow :: pauseAll( )
    740769{
    741     mySession.pause( );
     770    mySession.pauseTorrents( );
    742771}
    743772void
  • trunk/qt/mainwin.h

    r8317 r8323  
    104104        void showSessionTransfer( );
    105105        void refreshVisibleCount( );
     106        void refreshTitle( );
    106107        void refreshStatusBar( );
    107108        void openTorrent( );
     
    113114        void openFolder( );
    114115        void openProperties( );
     116        void openSessionDialog( );
    115117        void toggleSpeedMode( );
    116118        void dataReadProgress( );
     
    119121        void onSetPrefs( );
    120122        void onSetPrefs( bool );
     123        void onSessionSourceChanged( );
    121124
    122125    private slots:
  • trunk/qt/mainwin.ui

    r8237 r8323  
    6565     <string>&amp;Torrent</string>
    6666    </property>
     67    <addaction name="separator"/>
    6768    <addaction name="action_Add"/>
    6869    <addaction name="action_New"/>
     
    8990    <addaction name="action_SelectAll"/>
    9091    <addaction name="action_DeselectAll"/>
     92    <addaction name="separator"/>
     93    <addaction name="action_SessionDialog"/>
    9194    <addaction name="separator"/>
    9295    <addaction name="action_Preferences"/>
     
    521524   </property>
    522525  </action>
     526  <action name="action_SessionDialog">
     527   <property name="text">
     528    <string>&amp;Change Session...</string>
     529   </property>
     530   <property name="toolTip">
     531    <string extracomment="Start a local session or connect to a running session">Choose Session</string>
     532   </property>
     533  </action>
    523534 </widget>
    524535 <resources>
  • trunk/qt/prefs.cc

    r8216 r8323  
    5757    { MAIN_WINDOW_Y, "main-window-y", QVariant::Int },
    5858    { FILTER_MODE, "filter-mode", TrTypes::FilterModeType },
     59    { SESSION_IS_REMOTE, "remote-session-enabled", QVariant::Bool },
     60    { SESSION_REMOTE_HOST, "remote-session-host", QVariant::String },
     61    { SESSION_REMOTE_PORT, "remote-session-port", QVariant::Int },
     62    { SESSION_REMOTE_AUTH, "remote-session-requres-authentication", QVariant::Bool },
     63    { SESSION_REMOTE_USERNAME, "remote-session-username", QVariant::String },
     64    { SESSION_REMOTE_PASSWORD, "remote-session-password", QVariant::String },
    5965
    6066    /* libtransmission settings */
     
    257263    tr_bencDictAddInt( d, keyStr(START), true );
    258264    tr_bencDictAddInt( d, keyStr(TRASH_ORIGINAL), false );
     265    tr_bencDictAddStr( d, keyStr(SESSION_REMOTE_HOST), "localhost" );
     266    tr_bencDictAddInt( d, keyStr(SESSION_REMOTE_PORT), 9091 );
     267    tr_bencDictAddBool( d, keyStr(SESSION_IS_REMOTE), false );
     268    tr_bencDictAddBool( d, keyStr(SESSION_REMOTE_AUTH), false );
    259269}
    260270
  • trunk/qt/prefs.h

    r8232 r8323  
    6060            MAIN_WINDOW_Y,
    6161            FILTER_MODE,
     62            SESSION_IS_REMOTE,
     63            SESSION_REMOTE_HOST,
     64            SESSION_REMOTE_PORT,
     65            SESSION_REMOTE_AUTH,
     66            SESSION_REMOTE_USERNAME,
     67            SESSION_REMOTE_PASSWORD,
    6268
    6369            /* core prefs */
  • trunk/qt/qtr.pro

    r8262 r8323  
    2525SOURCES += about.cc app.cc details.cc file-tree.cc filters.cc hig.cc \
    2626           mainwin.cc make-dialog.cc options.cc prefs.cc prefs-dialog.cc \
    27            qticonloader.cc session.cc squeezelabel.cc stats-dialog.cc \
    28            torrent.cc torrent-delegate.cc torrent-delegate-min.cc \
    29            torrent-filter.cc torrent-model.cc triconpushbutton.cc \
    30            utils.cc watchdir.cc
     27           qticonloader.cc session.cc session-dialog.cc squeezelabel.cc \
     28           stats-dialog.cc torrent.cc torrent-delegate.cc \
     29           torrent-delegate-min.cc torrent-filter.cc torrent-model.cc \
     30           triconpushbutton.cc utils.cc watchdir.cc
    3131HEADERS += $$replace(SOURCES, .cc, .h)
    3232HEADERS += speed.h types.h
  • 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
  • trunk/qt/session.h

    r8255 r8323  
    3838
    3939    public:
    40         Session( const char * configDir, Prefs& prefs, const char * remoteUrl, bool paused );
     40        Session( const char * configDir, Prefs& prefs );
    4141        ~Session( );
    4242
    4343        static const int ADD_TORRENT_TAG;
     44
     45    public:
     46        void stop( );
     47        void restart( );
     48
     49    private:
     50        void start( );
    4451
    4552    public:
     
    8592
    8693    public slots:
    87         void pause( const QSet<int>& torrentIds = QSet<int>() );
    88         void start( const QSet<int>& torrentIds = QSet<int>() );
     94        void pauseTorrents( const QSet<int>& torrentIds = QSet<int>() );
     95        void startTorrents( const QSet<int>& torrentIds = QSet<int>() );
    8996        void refreshSessionInfo( );
    9097        void refreshSessionStats( );
     
    107114
    108115    signals:
     116        void sourceChanged( );
    109117        void portTested( bool isOpen );
    110118        void statsUpdated( );
     
    120128        Prefs& myPrefs;
    121129        tr_session * mySession;
     130        QString myConfigDir;
    122131        QUrl myUrl;
    123132        QBuffer myBuffer;
  • trunk/qt/torrent-model.cc

    r8188 r8323  
    2020#include "torrent-model.h"
    2121
     22void
     23TorrentModel :: clear( )
     24{
     25    myIdToRow.clear( );
     26    myIdToTorrent.clear( );
     27    foreach( Torrent * tor, myTorrents ) delete tor;
     28    myTorrents.clear( );
     29    reset( );
     30}
     31
    2232int
    2333TorrentModel :: rowCount( const QModelIndex& parent ) const
     
    7989TorrentModel :: ~TorrentModel( )
    8090{
    81     foreach( Torrent * tor, myTorrents )
    82         delete tor;
    83     myTorrents.clear( );
     91    clear( );
    8492}
    8593
  • trunk/qt/torrent-model.h

    r8188 r8323  
    4343
    4444    public:
     45        void clear( );
    4546        bool hasTorrent( const QString& hashString ) const;
    4647        virtual int rowCount( const QModelIndex& parent = QModelIndex() ) const;
Note: See TracChangeset for help on using the changeset viewer.