~hendrik-grewe/transmission/private-patch

« back to all changes in this revision

Viewing changes to qt/app.cc

  • Committer: charles
  • Date: 2009-04-09 17:55:47 UTC
  • Revision ID: svn-v4:f4695dd4-2c0a-0410-b89c-da849a56a58e:trunk:8188
(trunk) add the Qt beta into svn 

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
 
3
 *
 
4
 * This file is licensed by the GPL version 2.  Works owned by the
 
5
 * Transmission project are granted a special exemption to clause 2(b)
 
6
 * so that the bulk of its code can remain under the MIT license.
 
7
 * This exemption does not extend to derived works not owned by
 
8
 * the Transmission project.
 
9
 *
 
10
 * $Id:$
 
11
 */
 
12
 
 
13
#include <cassert>
 
14
#include <ctime>
 
15
#include <iostream>
 
16
 
 
17
#include <QIcon>
 
18
#include <QLibraryInfo>
 
19
#include <QRect>
 
20
#include <QTranslator>
 
21
 
 
22
#include <libtransmission/transmission.h>
 
23
#include <libtransmission/tr-getopt.h>
 
24
 
 
25
#include "app.h"
 
26
#include "mainwin.h"
 
27
#include "options.h"
 
28
#include "prefs.h"
 
29
#include "torrent-model.h"
 
30
#include "session.h"
 
31
#include "utils.h"
 
32
#include "watchdir.h"
 
33
 
 
34
namespace
 
35
{
 
36
    const char * MY_NAME( "transmission" );
 
37
 
 
38
    const tr_option opts[] =
 
39
    {
 
40
        { 'g', "config-dir", "Where to look for configuration files", "g", 1, "<path>" },
 
41
        { 'm', "minimized",  "Start minimized in system tray", "m", 0, NULL },
 
42
        { 'p', "paused",  "Pause all torrents on sartup", "p", 0, NULL },
 
43
        { 'r', "remote",  "Remotely control a pre-existing session", "r", 1, "<URL>" },
 
44
        { 'v', "version", "Show version number and exit", "v", 0, NULL },
 
45
        { 0, NULL, NULL, NULL, 0, NULL }
 
46
    };
 
47
 
 
48
    const char*
 
49
    getUsage( void )
 
50
    {
 
51
        return "Transmission " LONG_VERSION_STRING "\n"
 
52
               "http://www.transmissionbt.com/\n"
 
53
               "A fast and easy BitTorrent client";
 
54
    }
 
55
 
 
56
    void
 
57
    showUsage( void )
 
58
    {
 
59
        tr_getopt_usage( MY_NAME, getUsage( ), opts );
 
60
        exit( 0 );
 
61
    }
 
62
 
 
63
    enum
 
64
    {
 
65
        STATS_REFRESH_INTERVAL_MSEC = 3000,
 
66
        SESSION_REFRESH_INTERVAL_MSEC = 3000,
 
67
        MODEL_REFRESH_INTERVAL_MSEC = 3000
 
68
    };
 
69
}
 
70
 
 
71
MyApp :: MyApp( int& argc, char ** argv ):
 
72
    QApplication( argc, argv ),
 
73
    myLastFullUpdateTime( 0 )
 
74
{
 
75
    setApplicationName( MY_NAME );
 
76
 
 
77
    // install the qt translator
 
78
    QTranslator * t = new QTranslator( );
 
79
    t->load( "qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
 
80
    installTranslator( t );
 
81
 
 
82
    // install the transmission translator
 
83
    t = new QTranslator( );
 
84
    t->load( QString(MY_NAME) + "_" + QLocale::system().name() );
 
85
    installTranslator( t );
 
86
 
 
87
    // set the default icon
 
88
    QIcon icon;
 
89
    icon.addPixmap( QPixmap( ":/icons/transmission-16.png" ) );
 
90
    icon.addPixmap( QPixmap( ":/icons/transmission-22.png" ) );
 
91
    icon.addPixmap( QPixmap( ":/icons/transmission-24.png" ) );
 
92
    icon.addPixmap( QPixmap( ":/icons/transmission-32.png" ) );
 
93
    icon.addPixmap( QPixmap( ":/icons/transmission-48.png" ) );
 
94
    setWindowIcon( icon );
 
95
 
 
96
    // parse the command-line arguments
 
97
    int c;
 
98
    bool paused = false;
 
99
    bool minimized = false;
 
100
    const char * optarg;
 
101
    const char * configDir = 0;
 
102
    const char * url = 0;
 
103
    while( ( c = tr_getopt( getUsage( ), argc, (const char**)argv, opts, &optarg ) ) ) {
 
104
        switch( c ) {
 
105
            case 'g': configDir = optarg; break;
 
106
            case 'm': minimized = true; break;
 
107
            case 'p': paused = true; break;
 
108
            case 'r': url = optarg; break;
 
109
            case 'v':        Utils::toStderr( QObject::tr( "transmission %1" ).arg( LONG_VERSION_STRING ) ); exit( 0 ); break;
 
110
            case TR_OPT_ERR: Utils::toStderr( QObject::tr( "Invalid option" ) ); showUsage( ); break;
 
111
            default:         Utils::toStderr( QObject::tr( "Got opt %1" ).arg((int)c) ); showUsage( ); break;
 
112
        }
 
113
    }
 
114
 
 
115
    // set the fallback config dir
 
116
    if( configDir == 0 )
 
117
        configDir = tr_getDefaultConfigDir( MY_NAME );
 
118
 
 
119
    myPrefs = new Prefs ( configDir );
 
120
    mySession = new Session( configDir, *myPrefs, url, paused );
 
121
    myModel = new TorrentModel( *myPrefs );
 
122
    myWindow = new TrMainWindow( *mySession, *myPrefs, *myModel, minimized );
 
123
    myWatchDir = new WatchDir( *myModel );
 
124
 
 
125
    /* when the session gets torrent info, update the model */
 
126
    connect( mySession, SIGNAL(torrentsUpdated(tr_benc*,bool)), myModel, SLOT(updateTorrents(tr_benc*,bool)) );
 
127
    connect( mySession, SIGNAL(torrentsUpdated(tr_benc*,bool)), myWindow, SLOT(refreshActionSensitivity()) );
 
128
    connect( mySession, SIGNAL(torrentsRemoved(tr_benc*)), myModel, SLOT(removeTorrents(tr_benc*)) );
 
129
    /* when the model sees a torrent for the first time, ask the session for full info on it */
 
130
    connect( myModel, SIGNAL(torrentsAdded(QSet<int>)), mySession, SLOT(initTorrents(QSet<int>)) );
 
131
 
 
132
    mySession->initTorrents( );
 
133
    mySession->refreshSessionStats( );
 
134
 
 
135
    /* when torrents are added to the watch directory, tell the session */
 
136
    connect( myWatchDir, SIGNAL(torrentFileAdded(QString)), this, SLOT(addTorrent(QString)) );
 
137
 
 
138
    /* init from preferences */
 
139
    QList<int> initKeys;
 
140
    initKeys << Prefs::DIR_WATCH;
 
141
    foreach( int key, initKeys )
 
142
        refreshPref( key );
 
143
    connect( myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(const int)) );
 
144
 
 
145
    QTimer * timer = &myModelTimer;
 
146
    connect( timer, SIGNAL(timeout()), this, SLOT(refreshTorrents()) );
 
147
    timer->setSingleShot( false );
 
148
    timer->setInterval( MODEL_REFRESH_INTERVAL_MSEC );
 
149
    timer->start( );
 
150
 
 
151
    timer = &myStatsTimer;
 
152
    connect( timer, SIGNAL(timeout()), mySession, SLOT(refreshSessionStats()) );
 
153
    timer->setSingleShot( false );
 
154
    timer->setInterval( STATS_REFRESH_INTERVAL_MSEC );
 
155
    timer->start( );
 
156
 
 
157
    timer = &mySessionTimer;
 
158
    connect( timer, SIGNAL(timeout()), mySession, SLOT(refreshSessionInfo()) );
 
159
    timer->setSingleShot( false );
 
160
    timer->setInterval( SESSION_REFRESH_INTERVAL_MSEC );
 
161
    timer->start( );
 
162
 
 
163
    maybeUpdateBlocklist( );
 
164
}
 
165
 
 
166
MyApp :: ~MyApp( )
 
167
{
 
168
    const QRect mainwinRect( myWindow->geometry( ) );
 
169
    delete myWatchDir;
 
170
    delete myWindow;
 
171
    delete myModel;
 
172
    delete mySession;
 
173
 
 
174
    myPrefs->set( Prefs :: MAIN_WINDOW_HEIGHT, std::max( 100, mainwinRect.height( ) ) );
 
175
    myPrefs->set( Prefs :: MAIN_WINDOW_WIDTH, std::max( 100, mainwinRect.width( ) ) );
 
176
    myPrefs->set( Prefs :: MAIN_WINDOW_X, mainwinRect.x( ) );
 
177
    myPrefs->set( Prefs :: MAIN_WINDOW_Y, mainwinRect.y( ) );
 
178
    delete myPrefs;
 
179
}
 
180
 
 
181
/***
 
182
****
 
183
***/
 
184
 
 
185
void
 
186
MyApp :: refreshPref( int key )
 
187
{
 
188
    switch( key )
 
189
    {
 
190
        case Prefs :: BLOCKLIST_UPDATES_ENABLED:
 
191
            maybeUpdateBlocklist( );
 
192
            break;
 
193
 
 
194
        case Prefs :: DIR_WATCH:
 
195
        case Prefs :: DIR_WATCH_ENABLED: {
 
196
            const QString path( myPrefs->getString( Prefs::DIR_WATCH ) );
 
197
            const bool isEnabled( myPrefs->getBool( Prefs::DIR_WATCH_ENABLED ) );
 
198
            myWatchDir->setPath( path, isEnabled );
 
199
            break;
 
200
        }
 
201
 
 
202
        default:
 
203
            break;
 
204
    }
 
205
}
 
206
 
 
207
void
 
208
MyApp :: maybeUpdateBlocklist( )
 
209
{
 
210
    if( !myPrefs->getBool( Prefs :: BLOCKLIST_UPDATES_ENABLED ) )
 
211
        return;
 
212
 
 
213
     const QDateTime lastUpdatedAt = myPrefs->getDateTime( Prefs :: BLOCKLIST_DATE );
 
214
     const QDateTime nextUpdateAt = lastUpdatedAt.addDays( 7 );
 
215
     const QDateTime now = QDateTime::currentDateTime( );
 
216
     if( now < nextUpdateAt )
 
217
     {
 
218
         mySession->updateBlocklist( );
 
219
         myPrefs->set( Prefs :: BLOCKLIST_DATE, now );
 
220
     }
 
221
}
 
222
 
 
223
void
 
224
MyApp :: refreshTorrents( )
 
225
{
 
226
    // usually we just poll the torrents that have shown recent activity,
 
227
    // but we also periodically ask for updates on the others to ensure
 
228
    // nothing's falling through the cracks.
 
229
    const time_t now = time( NULL );
 
230
    if( myLastFullUpdateTime + 60 >= now )
 
231
        mySession->refreshActiveTorrents( );
 
232
    else {
 
233
        myLastFullUpdateTime = now;
 
234
        mySession->refreshAllTorrents( );
 
235
    }
 
236
}
 
237
 
 
238
void
 
239
MyApp :: addTorrent( const QString& filename )
 
240
{
 
241
    if( myPrefs->getBool( Prefs :: OPTIONS_PROMPT ) ) {
 
242
        Options * o = new Options( *mySession, *myPrefs, filename, myWindow );
 
243
        o->show( );
 
244
        QApplication :: alert( o );
 
245
    } else {
 
246
        mySession->addTorrent( filename );
 
247
        QApplication :: alert ( myWindow );
 
248
    }
 
249
}
 
250
 
 
251
 
 
252
/***
 
253
****
 
254
***/
 
255
 
 
256
int
 
257
main( int argc, char * argv[] )
 
258
{
 
259
    MyApp app( argc, argv );
 
260
    return app.exec( );
 
261
}