2
* This file Copyright (C) 2009 Charles Kerr <charles@transmissionbt.com>
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.
18
#include <QLibraryInfo>
20
#include <QTranslator>
22
#include <libtransmission/transmission.h>
23
#include <libtransmission/tr-getopt.h>
24
#include <libtransmission/version.h>
31
#include "session-dialog.h"
32
#include "torrent-model.h"
38
const char * MY_NAME( "transmission" );
40
const tr_option opts[] =
42
{ 'g', "config-dir", "Where to look for configuration files", "g", 1, "<path>" },
43
{ 'm', "minimized", "Start minimized in system tray", "m", 0, NULL },
44
{ 'v', "version", "Show version number and exit", "v", 0, NULL },
45
{ 0, NULL, NULL, NULL, 0, NULL }
51
return "Transmission " LONG_VERSION_STRING "\n"
52
"http://www.transmissionbt.com/\n"
53
"A fast and easy BitTorrent client";
59
tr_getopt_usage( MY_NAME, getUsage( ), opts );
65
STATS_REFRESH_INTERVAL_MSEC = 3000,
66
SESSION_REFRESH_INTERVAL_MSEC = 3000,
67
MODEL_REFRESH_INTERVAL_MSEC = 3000
71
MyApp :: MyApp( int& argc, char ** argv ):
72
QApplication( argc, argv ),
73
myLastFullUpdateTime( 0 )
75
setApplicationName( MY_NAME );
77
// install the qt translator
78
QTranslator * t = new QTranslator( );
79
t->load( "qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
80
installTranslator( t );
82
// install the transmission translator
83
t = new QTranslator( );
84
t->load( QString(MY_NAME) + "_" + QLocale::system().name() );
85
installTranslator( t );
87
// set the default 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 );
96
// parse the command-line arguments
98
bool minimized = false;
100
const char * configDir = 0;
101
while( ( c = tr_getopt( getUsage( ), argc, (const char**)argv, opts, &optarg ) ) ) {
103
case 'g': configDir = optarg; break;
104
case 'm': minimized = true; break;
105
case 'v': Utils::toStderr( QObject::tr( "transmission %1" ).arg( LONG_VERSION_STRING ) ); exit( 0 ); break;
106
case TR_OPT_ERR: Utils::toStderr( QObject::tr( "Invalid option" ) ); showUsage( ); break;
107
default: Utils::toStderr( QObject::tr( "Got opt %1" ).arg((int)c) ); showUsage( ); break;
111
// set the fallback config dir
113
configDir = tr_getDefaultConfigDir( MY_NAME );
115
// is this the first time we've run transmission?
116
const bool firstTime = !QFile(QDir(configDir).absoluteFilePath("settings.json")).exists();
118
myPrefs = new Prefs ( configDir );
119
mySession = new Session( configDir, *myPrefs );
120
myModel = new TorrentModel( *myPrefs );
121
myWindow = new TrMainWindow( *mySession, *myPrefs, *myModel, minimized );
122
myWatchDir = new WatchDir( *myModel );
124
// when the session gets torrent info, update the model
125
connect( mySession, SIGNAL(torrentsUpdated(tr_benc*,bool)), myModel, SLOT(updateTorrents(tr_benc*,bool)) );
126
connect( mySession, SIGNAL(torrentsUpdated(tr_benc*,bool)), myWindow, SLOT(refreshActionSensitivity()) );
127
connect( mySession, SIGNAL(torrentsRemoved(tr_benc*)), myModel, SLOT(removeTorrents(tr_benc*)) );
128
// when the model sees a torrent for the first time, ask the session for full info on it
129
connect( myModel, SIGNAL(torrentsAdded(QSet<int>)), mySession, SLOT(initTorrents(QSet<int>)) );
131
mySession->initTorrents( );
132
mySession->refreshSessionStats( );
134
// when torrents are added to the watch directory, tell the session
135
connect( myWatchDir, SIGNAL(torrentFileAdded(QString)), this, SLOT(addTorrent(QString)) );
137
// init from preferences
139
initKeys << Prefs::DIR_WATCH;
140
foreach( int key, initKeys )
142
connect( myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(const int)) );
144
QTimer * timer = &myModelTimer;
145
connect( timer, SIGNAL(timeout()), this, SLOT(refreshTorrents()) );
146
timer->setSingleShot( false );
147
timer->setInterval( MODEL_REFRESH_INTERVAL_MSEC );
150
timer = &myStatsTimer;
151
connect( timer, SIGNAL(timeout()), mySession, SLOT(refreshSessionStats()) );
152
timer->setSingleShot( false );
153
timer->setInterval( STATS_REFRESH_INTERVAL_MSEC );
156
timer = &mySessionTimer;
157
connect( timer, SIGNAL(timeout()), mySession, SLOT(refreshSessionInfo()) );
158
timer->setSingleShot( false );
159
timer->setInterval( SESSION_REFRESH_INTERVAL_MSEC );
162
maybeUpdateBlocklist( );
165
mySession->restart( );
167
QDialog * d = new SessionDialog( *mySession, *myPrefs, myWindow );
174
const QRect mainwinRect( myWindow->geometry( ) );
180
myPrefs->set( Prefs :: MAIN_WINDOW_HEIGHT, std::max( 100, mainwinRect.height( ) ) );
181
myPrefs->set( Prefs :: MAIN_WINDOW_WIDTH, std::max( 100, mainwinRect.width( ) ) );
182
myPrefs->set( Prefs :: MAIN_WINDOW_X, mainwinRect.x( ) );
183
myPrefs->set( Prefs :: MAIN_WINDOW_Y, mainwinRect.y( ) );
192
MyApp :: refreshPref( int key )
196
case Prefs :: BLOCKLIST_UPDATES_ENABLED:
197
maybeUpdateBlocklist( );
200
case Prefs :: DIR_WATCH:
201
case Prefs :: DIR_WATCH_ENABLED: {
202
const QString path( myPrefs->getString( Prefs::DIR_WATCH ) );
203
const bool isEnabled( myPrefs->getBool( Prefs::DIR_WATCH_ENABLED ) );
204
myWatchDir->setPath( path, isEnabled );
214
MyApp :: maybeUpdateBlocklist( )
216
if( !myPrefs->getBool( Prefs :: BLOCKLIST_UPDATES_ENABLED ) )
219
const QDateTime lastUpdatedAt = myPrefs->getDateTime( Prefs :: BLOCKLIST_DATE );
220
const QDateTime nextUpdateAt = lastUpdatedAt.addDays( 7 );
221
const QDateTime now = QDateTime::currentDateTime( );
222
if( now < nextUpdateAt )
224
mySession->updateBlocklist( );
225
myPrefs->set( Prefs :: BLOCKLIST_DATE, now );
230
MyApp :: refreshTorrents( )
232
// usually we just poll the torrents that have shown recent activity,
233
// but we also periodically ask for updates on the others to ensure
234
// nothing's falling through the cracks.
235
const time_t now = time( NULL );
236
if( myLastFullUpdateTime + 60 >= now )
237
mySession->refreshActiveTorrents( );
239
myLastFullUpdateTime = now;
240
mySession->refreshAllTorrents( );
245
MyApp :: addTorrent( const QString& filename )
247
if( myPrefs->getBool( Prefs :: OPTIONS_PROMPT ) ) {
248
Options * o = new Options( *mySession, *myPrefs, filename, myWindow );
250
QApplication :: alert( o );
252
mySession->addTorrent( filename );
253
QApplication :: alert ( myWindow );
263
main( int argc, char * argv[] )
265
MyApp app( argc, argv );