1 | /* |
---|
2 | * This file Copyright (C) 2009-2010 Mnemosyne LLC |
---|
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: app.cc 11065 2010-07-28 20:17:16Z charles $ |
---|
11 | */ |
---|
12 | |
---|
13 | #include <cassert> |
---|
14 | #include <ctime> |
---|
15 | #include <iostream> |
---|
16 | |
---|
17 | #include <QDBusConnection> |
---|
18 | #include <QDBusError> |
---|
19 | #include <QDBusMessage> |
---|
20 | #include <QDialogButtonBox> |
---|
21 | #include <QIcon> |
---|
22 | #include <QLabel> |
---|
23 | #include <QLibraryInfo> |
---|
24 | #include <QRect> |
---|
25 | #include <QTranslator> |
---|
26 | |
---|
27 | #include <libtransmission/transmission.h> |
---|
28 | #include <libtransmission/tr-getopt.h> |
---|
29 | #include <libtransmission/utils.h> |
---|
30 | #include <libtransmission/version.h> |
---|
31 | |
---|
32 | #include "app.h" |
---|
33 | #include "dbus-adaptor.h" |
---|
34 | #include "formatter.h" |
---|
35 | #include "mainwin.h" |
---|
36 | #include "options.h" |
---|
37 | #include "prefs.h" |
---|
38 | #include "session.h" |
---|
39 | #include "session-dialog.h" |
---|
40 | #include "torrent-model.h" |
---|
41 | #include "utils.h" |
---|
42 | #include "watchdir.h" |
---|
43 | |
---|
44 | namespace |
---|
45 | { |
---|
46 | const char * DBUS_SERVICE ( "com.transmissionbt.Transmission" ); |
---|
47 | const char * DBUS_OBJECT_PATH ( "/com/transmissionbt/Transmission" ); |
---|
48 | const char * DBUS_INTERFACE ( "com.transmissionbt.Transmission" ); |
---|
49 | |
---|
50 | const char * MY_NAME( "transmission" ); |
---|
51 | |
---|
52 | const tr_option opts[] = |
---|
53 | { |
---|
54 | { 'g', "config-dir", "Where to look for configuration files", "g", 1, "<path>" }, |
---|
55 | { 'm', "minimized", "Start minimized in system tray", "m", 0, NULL }, |
---|
56 | { 'p', "port", "Port to use when connecting to an existing session", "p", 1, "<port>" }, |
---|
57 | { 'r', "remote", "Connect to an existing session at the specified hostname", "r", 1, "<host>" }, |
---|
58 | { 'u', "username", "Username to use when connecting to an existing session", "u", 1, "<username>" }, |
---|
59 | { 'v', "version", "Show version number and exit", "v", 0, NULL }, |
---|
60 | { 'w', "password", "Password to use when connecting to an existing session", "w", 1, "<password>" }, |
---|
61 | { 0, NULL, NULL, NULL, 0, NULL } |
---|
62 | }; |
---|
63 | |
---|
64 | const char* |
---|
65 | getUsage( void ) |
---|
66 | { |
---|
67 | return "Usage:\n" |
---|
68 | " transmission [OPTIONS...] [torrent files]"; |
---|
69 | } |
---|
70 | |
---|
71 | void |
---|
72 | showUsage( void ) |
---|
73 | { |
---|
74 | tr_getopt_usage( MY_NAME, getUsage( ), opts ); |
---|
75 | exit( 0 ); |
---|
76 | } |
---|
77 | |
---|
78 | enum |
---|
79 | { |
---|
80 | STATS_REFRESH_INTERVAL_MSEC = 3000, |
---|
81 | SESSION_REFRESH_INTERVAL_MSEC = 3000, |
---|
82 | MODEL_REFRESH_INTERVAL_MSEC = 3000 |
---|
83 | }; |
---|
84 | } |
---|
85 | |
---|
86 | MyApp :: MyApp( int& argc, char ** argv ): |
---|
87 | QApplication( argc, argv ), |
---|
88 | myLastFullUpdateTime( 0 ) |
---|
89 | { |
---|
90 | setApplicationName( MY_NAME ); |
---|
91 | |
---|
92 | // install the qt translator |
---|
93 | QTranslator qtTranslator; |
---|
94 | qtTranslator.load( "qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath)); |
---|
95 | installTranslator( &qtTranslator ); |
---|
96 | |
---|
97 | // install the transmission translator |
---|
98 | QTranslator appTranslator; |
---|
99 | appTranslator.load( QString(MY_NAME) + "_" + QLocale::system().name() ); |
---|
100 | installTranslator( &appTranslator ); |
---|
101 | |
---|
102 | Formatter::initUnits( ); |
---|
103 | |
---|
104 | // set the default icon |
---|
105 | QIcon icon; |
---|
106 | icon.addPixmap( QPixmap( ":/icons/transmission-16.png" ) ); |
---|
107 | icon.addPixmap( QPixmap( ":/icons/transmission-22.png" ) ); |
---|
108 | icon.addPixmap( QPixmap( ":/icons/transmission-24.png" ) ); |
---|
109 | icon.addPixmap( QPixmap( ":/icons/transmission-32.png" ) ); |
---|
110 | icon.addPixmap( QPixmap( ":/icons/transmission-48.png" ) ); |
---|
111 | setWindowIcon( icon ); |
---|
112 | |
---|
113 | // parse the command-line arguments |
---|
114 | int c; |
---|
115 | bool minimized = false; |
---|
116 | const char * optarg; |
---|
117 | const char * host = 0; |
---|
118 | const char * port = 0; |
---|
119 | const char * username = 0; |
---|
120 | const char * password = 0; |
---|
121 | const char * configDir = 0; |
---|
122 | QStringList filenames; |
---|
123 | while( ( c = tr_getopt( getUsage( ), argc, (const char**)argv, opts, &optarg ) ) ) { |
---|
124 | switch( c ) { |
---|
125 | case 'g': configDir = optarg; break; |
---|
126 | case 'p': port = optarg; break; |
---|
127 | case 'r': host = optarg; break; |
---|
128 | case 'u': username = optarg; break; |
---|
129 | case 'w': password = optarg; break; |
---|
130 | case 'm': minimized = true; break; |
---|
131 | case 'v': Utils::toStderr( QObject::tr( "transmission %1" ).arg( LONG_VERSION_STRING ) ); ::exit( 0 ); break; |
---|
132 | case TR_OPT_ERR: Utils::toStderr( QObject::tr( "Invalid option" ) ); showUsage( ); break; |
---|
133 | default: filenames.append( optarg ); break; |
---|
134 | } |
---|
135 | } |
---|
136 | |
---|
137 | // set the fallback config dir |
---|
138 | if( configDir == 0 ) |
---|
139 | configDir = tr_getDefaultConfigDir( MY_NAME ); |
---|
140 | |
---|
141 | // is this the first time we've run transmission? |
---|
142 | const bool firstTime = !QFile(QDir(configDir).absoluteFilePath("settings.json")).exists(); |
---|
143 | |
---|
144 | // initialize the prefs |
---|
145 | myPrefs = new Prefs ( configDir ); |
---|
146 | if( host != 0 ) |
---|
147 | myPrefs->set( Prefs::SESSION_REMOTE_HOST, host ); |
---|
148 | if( port != 0 ) |
---|
149 | myPrefs->set( Prefs::SESSION_REMOTE_PORT, port ); |
---|
150 | if( username != 0 ) |
---|
151 | myPrefs->set( Prefs::SESSION_REMOTE_USERNAME, username ); |
---|
152 | if( password != 0 ) |
---|
153 | myPrefs->set( Prefs::SESSION_REMOTE_PASSWORD, password ); |
---|
154 | if( ( host != 0 ) || ( port != 0 ) || ( username != 0 ) || ( password != 0 ) ) |
---|
155 | myPrefs->set( Prefs::SESSION_IS_REMOTE, true ); |
---|
156 | |
---|
157 | mySession = new Session( configDir, *myPrefs ); |
---|
158 | myModel = new TorrentModel( *myPrefs ); |
---|
159 | myWindow = new TrMainWindow( *mySession, *myPrefs, *myModel, minimized ); |
---|
160 | myWatchDir = new WatchDir( *myModel ); |
---|
161 | |
---|
162 | // when the session gets torrent info, update the model |
---|
163 | connect( mySession, SIGNAL(torrentsUpdated(tr_benc*,bool)), myModel, SLOT(updateTorrents(tr_benc*,bool)) ); |
---|
164 | connect( mySession, SIGNAL(torrentsUpdated(tr_benc*,bool)), myWindow, SLOT(refreshActionSensitivity()) ); |
---|
165 | connect( mySession, SIGNAL(torrentsRemoved(tr_benc*)), myModel, SLOT(removeTorrents(tr_benc*)) ); |
---|
166 | // when the session source gets changed, request a full refresh |
---|
167 | connect( mySession, SIGNAL(sourceChanged()), this, SLOT(onSessionSourceChanged()) ); |
---|
168 | // when the model sees a torrent for the first time, ask the session for full info on it |
---|
169 | connect( myModel, SIGNAL(torrentsAdded(QSet<int>)), mySession, SLOT(initTorrents(QSet<int>)) ); |
---|
170 | |
---|
171 | mySession->initTorrents( ); |
---|
172 | mySession->refreshSessionStats( ); |
---|
173 | |
---|
174 | // when torrents are added to the watch directory, tell the session |
---|
175 | connect( myWatchDir, SIGNAL(torrentFileAdded(QString)), this, SLOT(addTorrent(QString)) ); |
---|
176 | |
---|
177 | // init from preferences |
---|
178 | QList<int> initKeys; |
---|
179 | initKeys << Prefs::DIR_WATCH; |
---|
180 | foreach( int key, initKeys ) |
---|
181 | refreshPref( key ); |
---|
182 | connect( myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(const int)) ); |
---|
183 | |
---|
184 | QTimer * timer = &myModelTimer; |
---|
185 | connect( timer, SIGNAL(timeout()), this, SLOT(refreshTorrents()) ); |
---|
186 | timer->setSingleShot( false ); |
---|
187 | timer->setInterval( MODEL_REFRESH_INTERVAL_MSEC ); |
---|
188 | timer->start( ); |
---|
189 | |
---|
190 | timer = &myStatsTimer; |
---|
191 | connect( timer, SIGNAL(timeout()), mySession, SLOT(refreshSessionStats()) ); |
---|
192 | timer->setSingleShot( false ); |
---|
193 | timer->setInterval( STATS_REFRESH_INTERVAL_MSEC ); |
---|
194 | timer->start( ); |
---|
195 | |
---|
196 | timer = &mySessionTimer; |
---|
197 | connect( timer, SIGNAL(timeout()), mySession, SLOT(refreshSessionInfo()) ); |
---|
198 | timer->setSingleShot( false ); |
---|
199 | timer->setInterval( SESSION_REFRESH_INTERVAL_MSEC ); |
---|
200 | timer->start( ); |
---|
201 | |
---|
202 | maybeUpdateBlocklist( ); |
---|
203 | |
---|
204 | if( !firstTime ) |
---|
205 | mySession->restart( ); |
---|
206 | else { |
---|
207 | QDialog * d = new SessionDialog( *mySession, *myPrefs, myWindow ); |
---|
208 | d->show( ); |
---|
209 | } |
---|
210 | |
---|
211 | if( !myPrefs->getBool( Prefs::USER_HAS_GIVEN_INFORMED_CONSENT )) |
---|
212 | { |
---|
213 | QDialog * dialog = new QDialog( myWindow ); |
---|
214 | dialog->setModal( true ); |
---|
215 | QVBoxLayout * v = new QVBoxLayout( dialog ); |
---|
216 | QLabel * l = new QLabel( tr( "Transmission is a file-sharing program. When you run a torrent, its data will be made available to others by means of upload. You and you alone are fully responsible for exercising proper judgement and abiding by your local laws." ) ); |
---|
217 | l->setWordWrap( true ); |
---|
218 | v->addWidget( l ); |
---|
219 | QDialogButtonBox * box = new QDialogButtonBox; |
---|
220 | box->addButton( new QPushButton( tr( "&Cancel" ) ), QDialogButtonBox::RejectRole ); |
---|
221 | QPushButton * agree = new QPushButton( tr( "I &Agree" ) ); |
---|
222 | agree->setDefault( true ); |
---|
223 | box->addButton( agree, QDialogButtonBox::AcceptRole ); |
---|
224 | box->setSizePolicy( QSizePolicy::Expanding, QSizePolicy::Fixed ); |
---|
225 | box->setOrientation( Qt::Horizontal ); |
---|
226 | v->addWidget( box ); |
---|
227 | connect( box, SIGNAL(rejected()), this, SLOT(quit()) ); |
---|
228 | connect( box, SIGNAL(accepted()), dialog, SLOT(deleteLater()) ); |
---|
229 | connect( box, SIGNAL(accepted()), this, SLOT(consentGiven()) ); |
---|
230 | dialog->show(); |
---|
231 | } |
---|
232 | |
---|
233 | for( QStringList::const_iterator it=filenames.begin(), end=filenames.end(); it!=end; ++it ) |
---|
234 | addTorrent( *it ); |
---|
235 | |
---|
236 | // register as the dbus handler for Transmission |
---|
237 | new TrDBusAdaptor( this ); |
---|
238 | QDBusConnection bus = QDBusConnection::sessionBus(); |
---|
239 | if (!bus.registerService("com.transmissionbt.Transmission")) |
---|
240 | fprintf(stderr, "%s\n", qPrintable(bus.lastError().message())); |
---|
241 | if( !bus.registerObject( "/com/transmissionbt/Transmission", this )) |
---|
242 | fprintf(stderr, "%s\n", qPrintable(bus.lastError().message())); |
---|
243 | } |
---|
244 | |
---|
245 | void |
---|
246 | MyApp :: consentGiven( ) |
---|
247 | { |
---|
248 | myPrefs->set<bool>( Prefs::USER_HAS_GIVEN_INFORMED_CONSENT, true ); |
---|
249 | } |
---|
250 | |
---|
251 | MyApp :: ~MyApp( ) |
---|
252 | { |
---|
253 | const QRect mainwinRect( myWindow->geometry( ) ); |
---|
254 | delete myWatchDir; |
---|
255 | delete myWindow; |
---|
256 | delete myModel; |
---|
257 | delete mySession; |
---|
258 | |
---|
259 | myPrefs->set( Prefs :: MAIN_WINDOW_HEIGHT, std::max( 100, mainwinRect.height( ) ) ); |
---|
260 | myPrefs->set( Prefs :: MAIN_WINDOW_WIDTH, std::max( 100, mainwinRect.width( ) ) ); |
---|
261 | myPrefs->set( Prefs :: MAIN_WINDOW_X, mainwinRect.x( ) ); |
---|
262 | myPrefs->set( Prefs :: MAIN_WINDOW_Y, mainwinRect.y( ) ); |
---|
263 | delete myPrefs; |
---|
264 | } |
---|
265 | |
---|
266 | /*** |
---|
267 | **** |
---|
268 | ***/ |
---|
269 | |
---|
270 | void |
---|
271 | MyApp :: refreshPref( int key ) |
---|
272 | { |
---|
273 | switch( key ) |
---|
274 | { |
---|
275 | case Prefs :: BLOCKLIST_UPDATES_ENABLED: |
---|
276 | maybeUpdateBlocklist( ); |
---|
277 | break; |
---|
278 | |
---|
279 | case Prefs :: DIR_WATCH: |
---|
280 | case Prefs :: DIR_WATCH_ENABLED: { |
---|
281 | const QString path( myPrefs->getString( Prefs::DIR_WATCH ) ); |
---|
282 | const bool isEnabled( myPrefs->getBool( Prefs::DIR_WATCH_ENABLED ) ); |
---|
283 | myWatchDir->setPath( path, isEnabled ); |
---|
284 | break; |
---|
285 | } |
---|
286 | |
---|
287 | default: |
---|
288 | break; |
---|
289 | } |
---|
290 | } |
---|
291 | |
---|
292 | void |
---|
293 | MyApp :: maybeUpdateBlocklist( ) |
---|
294 | { |
---|
295 | if( !myPrefs->getBool( Prefs :: BLOCKLIST_UPDATES_ENABLED ) ) |
---|
296 | return; |
---|
297 | |
---|
298 | const QDateTime lastUpdatedAt = myPrefs->getDateTime( Prefs :: BLOCKLIST_DATE ); |
---|
299 | const QDateTime nextUpdateAt = lastUpdatedAt.addDays( 7 ); |
---|
300 | const QDateTime now = QDateTime::currentDateTime( ); |
---|
301 | if( now < nextUpdateAt ) |
---|
302 | { |
---|
303 | mySession->updateBlocklist( ); |
---|
304 | myPrefs->set( Prefs :: BLOCKLIST_DATE, now ); |
---|
305 | } |
---|
306 | } |
---|
307 | |
---|
308 | void |
---|
309 | MyApp :: onSessionSourceChanged( ) |
---|
310 | { |
---|
311 | mySession->initTorrents( ); |
---|
312 | mySession->refreshSessionStats( ); |
---|
313 | mySession->refreshSessionInfo( ); |
---|
314 | } |
---|
315 | |
---|
316 | void |
---|
317 | MyApp :: refreshTorrents( ) |
---|
318 | { |
---|
319 | // usually we just poll the torrents that have shown recent activity, |
---|
320 | // but we also periodically ask for updates on the others to ensure |
---|
321 | // nothing's falling through the cracks. |
---|
322 | const time_t now = time( NULL ); |
---|
323 | if( myLastFullUpdateTime + 60 >= now ) |
---|
324 | mySession->refreshActiveTorrents( ); |
---|
325 | else { |
---|
326 | myLastFullUpdateTime = now; |
---|
327 | mySession->refreshAllTorrents( ); |
---|
328 | } |
---|
329 | } |
---|
330 | |
---|
331 | /*** |
---|
332 | **** |
---|
333 | ***/ |
---|
334 | |
---|
335 | void |
---|
336 | MyApp :: addTorrent( const QString& key ) |
---|
337 | { |
---|
338 | if( !myPrefs->getBool( Prefs :: OPTIONS_PROMPT ) ) |
---|
339 | { |
---|
340 | mySession->addTorrent( key ); |
---|
341 | } |
---|
342 | else if( Utils::isMagnetLink( key ) || QFile( key ).exists( ) ) |
---|
343 | { |
---|
344 | Options * o = new Options( *mySession, *myPrefs, key, myWindow ); |
---|
345 | o->show( ); |
---|
346 | } |
---|
347 | else if( Utils::isURL( key ) ) |
---|
348 | { |
---|
349 | myWindow->openURL( key ); |
---|
350 | } |
---|
351 | |
---|
352 | raise( ); |
---|
353 | } |
---|
354 | |
---|
355 | void |
---|
356 | MyApp :: raise( ) |
---|
357 | { |
---|
358 | QApplication :: alert ( myWindow ); |
---|
359 | } |
---|
360 | |
---|
361 | /*** |
---|
362 | **** |
---|
363 | ***/ |
---|
364 | |
---|
365 | int |
---|
366 | main( int argc, char * argv[] ) |
---|
367 | { |
---|
368 | // find .torrents, URLs, magnet links, etc in the command-line args |
---|
369 | int c; |
---|
370 | QStringList addme; |
---|
371 | const char * optarg; |
---|
372 | char ** argvv = argv; |
---|
373 | while( ( c = tr_getopt( getUsage( ), argc, (const char **)argvv, opts, &optarg ) ) ) |
---|
374 | if( c == TR_OPT_UNK ) |
---|
375 | addme.append( optarg ); |
---|
376 | |
---|
377 | // try to delegate the work to an existing copy of Transmission |
---|
378 | // before starting ourselves... |
---|
379 | bool delegated = false; |
---|
380 | QDBusConnection bus = QDBusConnection::sessionBus(); |
---|
381 | for( int i=0, n=addme.size(); i<n; ++i ) |
---|
382 | { |
---|
383 | const QString key = addme[i]; |
---|
384 | |
---|
385 | QDBusMessage request = QDBusMessage::createMethodCall( DBUS_SERVICE, |
---|
386 | DBUS_OBJECT_PATH, |
---|
387 | DBUS_INTERFACE, |
---|
388 | "AddMetainfo" ); |
---|
389 | QList<QVariant> arguments; |
---|
390 | arguments.push_back( QVariant( key ) ); |
---|
391 | request.setArguments( arguments ); |
---|
392 | |
---|
393 | QDBusMessage response = bus.call( request ); |
---|
394 | arguments = response.arguments( ); |
---|
395 | delegated |= (arguments.size()==1) && arguments[0].toBool(); |
---|
396 | } |
---|
397 | |
---|
398 | if( delegated ) |
---|
399 | return 0; |
---|
400 | |
---|
401 | tr_optind = 1; |
---|
402 | MyApp app( argc, argv ); |
---|
403 | return app.exec( ); |
---|
404 | } |
---|