source: branches/2.0x/qt/mainwin.cc @ 10871

Last change on this file since 10871 was 10871, checked in by Longinus00, 13 years ago

(2.0x qt) #3268:Within each session, each torrent added presents options dialog for that and all previous torrents added in that sesssion

  • Property svn:keywords set to Date Rev Author Id
File size: 46.1 KB
Line 
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: mainwin.cc 10871 2010-06-26 17:20:22Z Longinus00 $
11 */
12
13#include <cassert>
14#include <iostream>
15
16#include <QCheckBox>
17#include <QCloseEvent>
18#include <QDesktopServices>
19#include <QFileDialog>
20#include <QHBoxLayout>
21#include <QInputDialog>
22#include <QLabel>
23#include <QMessageBox>
24#include <QSignalMapper>
25#include <QSize>
26#include <QStyle>
27#include <QSystemTrayIcon>
28#include <QUrl>
29
30#include <libtransmission/transmission.h>
31#include <libtransmission/utils.h>
32#include <libtransmission/version.h>
33
34#include "about.h"
35#include "details.h"
36#include "filters.h"
37#include "hig.h"
38#include "mainwin.h"
39#include "make-dialog.h"
40#include "options.h"
41#include "prefs.h"
42#include "prefs-dialog.h"
43#include "relocate.h"
44#include "session.h"
45#include "session-dialog.h"
46#include "speed.h"
47#include "stats-dialog.h"
48#include "torrent-delegate.h"
49#include "torrent-delegate-min.h"
50#include "torrent-filter.h"
51#include "torrent-model.h"
52#include "triconpushbutton.h"
53#include "ui_mainwin.h"
54#include "utils.h"
55#include "qticonloader.h"
56
57#define PREFS_KEY "prefs-key";
58
59QIcon
60TrMainWindow :: getStockIcon( const QString& freedesktop_name, int fallback )
61{
62    QIcon fallbackIcon;
63
64    if( fallback > 0 )
65        fallbackIcon = style()->standardIcon( QStyle::StandardPixmap( fallback ), 0, this );
66
67    return QtIconLoader::icon( freedesktop_name, fallbackIcon );
68}
69
70namespace
71{
72    QSize calculateTextButtonSizeHint( QPushButton * button )
73    {
74        QStyleOptionButton opt;
75        opt.initFrom( button );
76        QString s( button->text( ) );
77        if( s.isEmpty( ) )
78            s = QString::fromLatin1( "XXXX" );
79        QFontMetrics fm = button->fontMetrics( );
80        QSize sz = fm.size( Qt::TextShowMnemonic, s );
81        return button->style()->sizeFromContents( QStyle::CT_PushButton, &opt, sz, button ).expandedTo( QApplication::globalStrut( ) );
82    }
83}
84
85
86TrMainWindow :: TrMainWindow( Session& session, Prefs& prefs, TorrentModel& model, bool minimized ):
87    myLastFullUpdateTime( 0 ),
88    mySessionDialog( new SessionDialog( session, prefs, this ) ),
89    myPrefsDialog( 0 ),
90    myAboutDialog( new AboutDialog( this ) ),
91    myStatsDialog( new StatsDialog( session, this ) ),
92    myDetailsDialog( 0 ),
93    myFilterModel( prefs ),
94    myTorrentDelegate( new TorrentDelegate( this ) ),
95    myTorrentDelegateMin( new TorrentDelegateMin( this ) ),
96    mySession( session ),
97    myPrefs( prefs ),
98    myModel( model ),
99    mySpeedModeOffIcon( ":/icons/alt-limit-off.png" ),
100    mySpeedModeOnIcon( ":/icons/alt-limit-on.png" ),
101    myLastSendTime( 0 ),
102    myLastReadTime( 0 ),
103    myNetworkTimer( this )
104{
105    QAction * sep = new QAction( this );
106    sep->setSeparator( true );
107
108    ui.setupUi( this );
109
110    QStyle * style = this->style();
111
112    int i = style->pixelMetric( QStyle::PM_SmallIconSize, 0, this );
113    const QSize smallIconSize( i, i );
114
115    // icons
116    ui.action_AddFile->setIcon( getStockIcon( "list-add", QStyle::SP_DialogOpenButton ) );
117    ui.action_New->setIcon( getStockIcon( "document-new", QStyle::SP_DesktopIcon ) );
118    ui.action_Properties->setIcon( getStockIcon( "document-properties", QStyle::SP_DesktopIcon ) );
119    ui.action_OpenFolder->setIcon( getStockIcon( "folder-open", QStyle::SP_DirOpenIcon ) );
120    ui.action_Start->setIcon( getStockIcon( "media-playback-start", QStyle::SP_MediaPlay ) );
121    ui.action_Announce->setIcon( getStockIcon( "network-transmit-receive" ) );
122    ui.action_Pause->setIcon( getStockIcon( "media-playback-pause", QStyle::SP_MediaPause ) );
123    ui.action_Remove->setIcon( getStockIcon( "list-remove", QStyle::SP_TrashIcon ) );
124    ui.action_Delete->setIcon( getStockIcon( "edit-delete", QStyle::SP_TrashIcon ) );
125    ui.action_StartAll->setIcon( getStockIcon( "media-playback-start", QStyle::SP_MediaPlay ) );
126    ui.action_PauseAll->setIcon( getStockIcon( "media-playback-pause", QStyle::SP_MediaPause ) );
127    ui.action_Quit->setIcon( getStockIcon( "application-exit" ) );
128    ui.action_SelectAll->setIcon( getStockIcon( "edit-select-all" ) );
129    ui.action_ReverseSortOrder->setIcon( getStockIcon( "view-sort-ascending", QStyle::SP_ArrowDown ) );
130    ui.action_Preferences->setIcon( getStockIcon( "preferences-system" ) );
131    ui.action_Contents->setIcon( getStockIcon( "help-contents", QStyle::SP_DialogHelpButton ) );
132    ui.action_About->setIcon( getStockIcon( "help-about" ) );
133
134    // ui signals
135    connect( ui.action_Toolbar, SIGNAL(toggled(bool)), this, SLOT(setToolbarVisible(bool)));
136    connect( ui.action_TrayIcon, SIGNAL(toggled(bool)), this, SLOT(setTrayIconVisible(bool)));
137    connect( ui.action_Filterbar, SIGNAL(toggled(bool)), this, SLOT(setFilterbarVisible(bool)));
138    connect( ui.action_Statusbar, SIGNAL(toggled(bool)), this, SLOT(setStatusbarVisible(bool)));
139    connect( ui.action_CompactView, SIGNAL(toggled(bool)), this, SLOT(setCompactView(bool)));
140    connect( ui.action_SortByActivity, SIGNAL(toggled(bool)), this, SLOT(onSortByActivityToggled(bool)));
141    connect( ui.action_SortByAge,      SIGNAL(toggled(bool)), this, SLOT(onSortByAgeToggled(bool)));
142    connect( ui.action_SortByETA,      SIGNAL(toggled(bool)), this, SLOT(onSortByETAToggled(bool)));
143    connect( ui.action_SortByName,     SIGNAL(toggled(bool)), this, SLOT(onSortByNameToggled(bool)));
144    connect( ui.action_SortByProgress, SIGNAL(toggled(bool)), this, SLOT(onSortByProgressToggled(bool)));
145    connect( ui.action_SortByRatio,    SIGNAL(toggled(bool)), this, SLOT(onSortByRatioToggled(bool)));
146    connect( ui.action_SortBySize,     SIGNAL(toggled(bool)), this, SLOT(onSortBySizeToggled(bool)));
147    connect( ui.action_SortByState,    SIGNAL(toggled(bool)), this, SLOT(onSortByStateToggled(bool)));
148    connect( ui.action_SortByTracker,  SIGNAL(toggled(bool)), this, SLOT(onSortByTrackerToggled(bool)));
149    connect( ui.action_ReverseSortOrder, SIGNAL(toggled(bool)), this, SLOT(setSortAscendingPref(bool)));
150    connect( ui.action_Start, SIGNAL(triggered()), this, SLOT(startSelected()));
151    connect( ui.action_Pause, SIGNAL(triggered()), this, SLOT(pauseSelected()));
152    connect( ui.action_Remove, SIGNAL(triggered()), this, SLOT(removeSelected()));
153    connect( ui.action_Delete, SIGNAL(triggered()), this, SLOT(deleteSelected()));
154    connect( ui.action_Verify, SIGNAL(triggered()), this, SLOT(verifySelected()) );
155    connect( ui.action_Announce, SIGNAL(triggered()), this, SLOT(reannounceSelected()) );
156    connect( ui.action_StartAll, SIGNAL(triggered()), this, SLOT(startAll()));
157    connect( ui.action_PauseAll, SIGNAL(triggered()), this, SLOT(pauseAll()));
158    connect( ui.action_AddFile, SIGNAL(triggered()), this, SLOT(openTorrent()));
159    connect( ui.action_AddURL, SIGNAL(triggered()), this, SLOT(openURL()));
160    connect( ui.action_New, SIGNAL(triggered()), this, SLOT(newTorrent()));
161    connect( ui.action_Preferences, SIGNAL(triggered()), this, SLOT(openPreferences()));
162    connect( ui.action_Statistics, SIGNAL(triggered()), myStatsDialog, SLOT(show()));
163    connect( ui.action_About, SIGNAL(triggered()), myAboutDialog, SLOT(show()));
164    connect( ui.action_Contents, SIGNAL(triggered()), this, SLOT(openHelp()));
165    connect( ui.action_OpenFolder, SIGNAL(triggered()), this, SLOT(openFolder()));
166    connect( ui.action_CopyMagnetToClipboard, SIGNAL(triggered()), this, SLOT(copyMagnetLinkToClipboard()));
167    connect( ui.action_SetLocation, SIGNAL(triggered()), this, SLOT(setLocation()));
168    connect( ui.action_Properties, SIGNAL(triggered()), this, SLOT(openProperties()));
169    connect( ui.action_SessionDialog, SIGNAL(triggered()), mySessionDialog, SLOT(show()));
170    connect( ui.listView, SIGNAL(activated(const QModelIndex&)), ui.action_Properties, SLOT(trigger()));
171
172    QAction * sep2 = new QAction( this );
173    sep2->setSeparator( true );
174    QAction * sep3 = new QAction( this );
175    sep3->setSeparator( true );
176
177    // context menu
178    QList<QAction*> actions;
179    actions << ui.action_Properties
180            << ui.action_OpenFolder
181            << sep2
182            << ui.action_Start
183            << ui.action_Announce
184            << ui.action_Pause
185            << ui.action_CopyMagnetToClipboard
186            << sep3
187            << ui.action_Verify
188            << ui.action_SetLocation
189            << sep
190            << ui.action_Remove
191            << ui.action_Delete;
192    addActions( actions );
193    setContextMenuPolicy( Qt::ActionsContextMenu );
194
195    // signals
196    connect( ui.action_SelectAll, SIGNAL(triggered()), ui.listView, SLOT(selectAll()));
197    connect( ui.action_DeselectAll, SIGNAL(triggered()), ui.listView, SLOT(clearSelection()));
198
199    connect( &myFilterModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)), this, SLOT(refreshVisibleCount()));
200    connect( &myFilterModel, SIGNAL(rowsRemoved(const QModelIndex&,int,int)), this, SLOT(refreshVisibleCount()));
201    connect( &myFilterModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)), this, SLOT(refreshActionSensitivity()));
202    connect( &myFilterModel, SIGNAL(rowsRemoved(const QModelIndex&,int,int)), this, SLOT(refreshActionSensitivity()));
203
204    connect( ui.action_Quit, SIGNAL(triggered()), QCoreApplication::instance(), SLOT(quit()) );
205
206    // torrent view
207    myFilterModel.setSourceModel( &myModel );
208    connect( &myModel, SIGNAL(modelReset()), this, SLOT(onModelReset()));
209    connect( &myModel, SIGNAL(rowsRemoved(const QModelIndex&,int,int)), this, SLOT(onModelReset()));
210    connect( &myModel, SIGNAL(rowsInserted(const QModelIndex&,int,int)), this, SLOT(onModelReset()));
211    ui.listView->setModel( &myFilterModel );
212    connect( ui.listView->selectionModel(), SIGNAL(selectionChanged(const QItemSelection&,const QItemSelection&)), this, SLOT(refreshActionSensitivity()));
213
214    QActionGroup * actionGroup = new QActionGroup( this );
215    actionGroup->addAction( ui.action_SortByActivity );
216    actionGroup->addAction( ui.action_SortByAge );
217    actionGroup->addAction( ui.action_SortByETA );
218    actionGroup->addAction( ui.action_SortByName );
219    actionGroup->addAction( ui.action_SortByProgress );
220    actionGroup->addAction( ui.action_SortByRatio );
221    actionGroup->addAction( ui.action_SortBySize );
222    actionGroup->addAction( ui.action_SortByState );
223    actionGroup->addAction( ui.action_SortByTracker );
224
225    QMenu * menu = new QMenu( );
226    menu->addAction( ui.action_AddFile );
227    menu->addAction( ui.action_AddURL );
228    menu->addSeparator( );
229    menu->addAction( ui.action_ShowMainWindow );
230    menu->addAction( ui.action_ShowMessageLog );
231    menu->addAction( ui.action_About );
232    menu->addSeparator( );
233    menu->addAction( ui.action_StartAll );
234    menu->addAction( ui.action_PauseAll );
235    menu->addSeparator( );
236    menu->addAction( ui.action_Quit );
237    myTrayIcon.setContextMenu( menu );
238    myTrayIcon.setIcon( QApplication::windowIcon( ) );
239
240    connect( &myPrefs, SIGNAL(changed(int)), this, SLOT(refreshPref(int)) );
241    connect( ui.action_ShowMainWindow, SIGNAL(toggled(bool)), this, SLOT(toggleWindows(bool)));
242    connect( &myTrayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
243             this, SLOT(trayActivated(QSystemTrayIcon::ActivationReason)));
244
245    ui.action_ShowMainWindow->setChecked( !minimized );
246    ui.action_TrayIcon->setChecked( minimized || prefs.getBool( Prefs::SHOW_TRAY_ICON ) );
247
248    ui.verticalLayout->addWidget( createStatusBar( ) );
249    ui.verticalLayout->insertWidget( 0, createFilterBar( ) );
250
251    QList<int> initKeys;
252    initKeys << Prefs :: MAIN_WINDOW_X
253             << Prefs :: SHOW_TRAY_ICON
254             << Prefs :: SORT_REVERSED
255             << Prefs :: SORT_MODE
256             << Prefs :: FILTER_MODE
257             << Prefs :: FILTERBAR
258             << Prefs :: STATUSBAR
259             << Prefs :: STATUSBAR_STATS
260             << Prefs :: TOOLBAR
261             << Prefs :: ALT_SPEED_LIMIT_ENABLED
262             << Prefs :: COMPACT_VIEW
263             << Prefs :: DSPEED
264             << Prefs :: DSPEED_ENABLED
265             << Prefs :: USPEED
266             << Prefs :: USPEED_ENABLED
267             << Prefs :: RATIO
268             << Prefs :: RATIO_ENABLED;
269    foreach( int key, initKeys )
270        refreshPref( key );
271
272    connect( &mySession, SIGNAL(sourceChanged()), this, SLOT(onSessionSourceChanged()) );
273    connect( &mySession, SIGNAL(statsUpdated()), this, SLOT(refreshStatusBar()) );
274    connect( &mySession, SIGNAL(dataReadProgress()), this, SLOT(dataReadProgress()) );
275    connect( &mySession, SIGNAL(dataSendProgress()), this, SLOT(dataSendProgress()) );
276    connect( &mySession, SIGNAL(httpAuthenticationRequired()), this, SLOT(wrongAuthentication()) );
277
278    if( mySession.isServer( ) )
279        myNetworkLabel->hide( );
280    else {
281        connect( &myNetworkTimer, SIGNAL(timeout()), this, SLOT(onNetworkTimer()));
282        myNetworkTimer.start( 1000 );
283    }
284
285    refreshActionSensitivity( );
286    refreshStatusBar( );
287    refreshTitle( );
288    refreshVisibleCount( );
289}
290
291TrMainWindow :: ~TrMainWindow( )
292{
293}
294
295/****
296*****
297****/
298
299void
300TrMainWindow :: closeEvent( QCloseEvent * event )
301{
302    // if they're using a tray icon, close to the tray
303    // instead of exiting
304    if( !myPrefs.getBool( Prefs :: SHOW_TRAY_ICON ) )
305        event->accept( );
306    else {
307        toggleWindows( false );
308        event->ignore( );
309    }
310}
311
312/****
313*****
314****/
315
316void
317TrMainWindow :: onSessionSourceChanged( )
318{
319    myModel.clear( );
320}
321
322void
323TrMainWindow :: onModelReset( )
324{
325    refreshTitle( );
326    refreshVisibleCount( );
327    refreshActionSensitivity( );
328    refreshStatusBar( );
329}
330
331/****
332*****
333****/
334
335#define PREF_VARIANTS_KEY "pref-variants-list"
336
337void
338TrMainWindow :: onSetPrefs( )
339{
340    const QVariantList p = sender()->property( PREF_VARIANTS_KEY ).toList( );
341    assert( ( p.size( ) % 2 ) == 0 );
342    for( int i=0, n=p.size(); i<n; i+=2 )
343        myPrefs.set( p[i].toInt(), p[i+1] );
344}
345
346void
347TrMainWindow :: onSetPrefs( bool isChecked )
348{
349    if( isChecked )
350        onSetPrefs( );
351}
352
353#define SHOW_KEY "show-mode"
354
355void
356TrMainWindow :: onShowModeClicked( )
357{
358    setShowMode( sender()->property(SHOW_KEY).toInt() );
359}
360
361QWidget *
362TrMainWindow :: createFilterBar( )
363{
364    int i;
365    QMenu * m;
366    QLineEdit * e;
367    QPushButton * p;
368    QHBoxLayout * h;
369    QActionGroup * a;
370    const int smallSize = style( )->pixelMetric( QStyle::PM_SmallIconSize, 0, this );
371    const QSize smallIconSize( smallSize, smallSize );
372
373    QWidget * top = myFilterBar = new QWidget;
374    h = new QHBoxLayout( top );
375    h->setContentsMargins( HIG::PAD_SMALL, HIG::PAD_SMALL, HIG::PAD_SMALL, HIG::PAD_SMALL );
376    h->setSpacing( HIG::PAD_SMALL );
377#ifdef Q_OS_MAC
378    top->setStyleSheet( "QPushButton{ "
379                        "  border-radius: 10px; "
380                        "  padding: 0 5px; "
381                        "  border: 1px none; "
382                        "} "
383                        "QPushButton:pressed, QPushButton:checked{ "
384                        "  border-width: 1px; "
385                        "  border-style: solid; "
386                        "  border-color: #5f5f5f #979797 #979797; "
387                        "  background-color: #979797; "
388                        "  color: white; "
389                        "} ");
390#endif
391
392        QList<QString> titles;
393        titles << tr( "A&ll" ) << tr( "&Active" ) << tr( "&Downloading" ) << tr( "&Seeding" ) << tr( "&Paused" );
394        for( i=0; i<titles.size(); ++i ) {
395            p = myFilterButtons[i] = new QPushButton( titles[i] );
396            p->setProperty( SHOW_KEY, i );
397            p->setFlat( true );
398            p->setCheckable( true );
399            p->setMaximumSize( calculateTextButtonSizeHint( p ) );
400            connect( p, SIGNAL(clicked()), this, SLOT(onShowModeClicked()));
401            h->addWidget( p );
402        }
403
404    h->addStretch( 1 );
405
406        a = new QActionGroup( this );
407        a->addAction( ui.action_FilterByName );
408        a->addAction( ui.action_FilterByFiles );
409        a->addAction( ui.action_FilterByTracker );
410        m = new QMenu( );
411        m->addAction( ui.action_FilterByName );
412        m->addAction( ui.action_FilterByFiles );
413        m->addAction( ui.action_FilterByTracker );
414        connect( ui.action_FilterByName, SIGNAL(triggered()), this, SLOT(filterByName()));
415        connect( ui.action_FilterByFiles, SIGNAL(triggered()), this, SLOT(filterByFiles()));
416        connect( ui.action_FilterByTracker, SIGNAL(triggered()), this, SLOT(filterByTracker()));
417        ui.action_FilterByName->setChecked( true );
418        p = myFilterTextButton = new TrIconPushButton;
419        p->setIcon( getStockIcon( "edit-find", QStyle::SP_ArrowForward ) );
420        p->setFlat( true );
421        p->setMenu( m );
422        h->addWidget( p );
423
424        e = myFilterTextLineEdit = new QLineEdit;
425        connect( e, SIGNAL(textChanged(QString)), &myFilterModel, SLOT(setText(QString)));
426        h->addWidget( e );
427
428        p = myFilterTextButton = new TrIconPushButton;
429        p->setIcon( getStockIcon( "edit-clear", QStyle::SP_DialogCloseButton ) );
430        p->setFlat( true );
431        connect( p, SIGNAL(clicked()), myFilterTextLineEdit, SLOT(clear()));
432        h->addWidget( p );
433
434    return top;
435}
436
437QWidget *
438TrMainWindow :: createStatusBar( )
439{
440    QMenu * m;
441    QLabel * l;
442    QWidget * w;
443    QHBoxLayout * h;
444    QPushButton * p;
445    QActionGroup * a;
446    const int i = style( )->pixelMetric( QStyle::PM_SmallIconSize, 0, this );
447    const QSize smallIconSize( i, i );
448
449    QWidget * top = myStatusBar = new QWidget;
450    h = new QHBoxLayout( top );
451    h->setContentsMargins( HIG::PAD_SMALL, HIG::PAD_SMALL, HIG::PAD_SMALL, HIG::PAD_SMALL );
452    h->setSpacing( HIG::PAD_SMALL );
453
454        p = myOptionsButton = new TrIconPushButton( this );
455        p->setIcon( QIcon( ":/icons/options.png" ) );
456        p->setIconSize( QPixmap( ":/icons/options.png" ).size() );
457        p->setFlat( true );
458        p->setMenu( createOptionsMenu( ) );
459        h->addWidget( p );
460
461        p = myAltSpeedButton = new QPushButton( this );
462        p->setIcon( myPrefs.get<bool>(Prefs::ALT_SPEED_LIMIT_ENABLED) ? mySpeedModeOnIcon : mySpeedModeOffIcon );
463        p->setIconSize( QPixmap( ":/icons/alt-limit-on.png" ).size() );
464        p->setCheckable( true );
465        p->setFixedWidth( p->height() );
466        p->setFlat( true );
467        h->addWidget( p );
468        connect( p, SIGNAL(clicked()), this, SLOT(toggleSpeedMode()));
469
470        l = myNetworkLabel = new QLabel;
471        h->addWidget( l );
472
473    h->addStretch( 1 );
474
475        l = myVisibleCountLabel = new QLabel( this );
476        h->addWidget( l );
477
478    h->addStretch( 1 );
479
480        a = new QActionGroup( this );
481        a->addAction( ui.action_TotalRatio );
482        a->addAction( ui.action_TotalTransfer );
483        a->addAction( ui.action_SessionRatio );
484        a->addAction( ui.action_SessionTransfer );
485        m = new QMenu( );
486        m->addAction( ui.action_TotalRatio );
487        m->addAction( ui.action_TotalTransfer );
488        m->addAction( ui.action_SessionRatio );
489        m->addAction( ui.action_SessionTransfer );
490        connect( ui.action_TotalRatio, SIGNAL(triggered()), this, SLOT(showTotalRatio()));
491        connect( ui.action_TotalTransfer, SIGNAL(triggered()), this, SLOT(showTotalTransfer()));
492        connect( ui.action_SessionRatio, SIGNAL(triggered()), this, SLOT(showSessionRatio()));
493        connect( ui.action_SessionTransfer, SIGNAL(triggered()), this, SLOT(showSessionTransfer()));
494        p = myStatsModeButton = new TrIconPushButton( this );
495        p->setIcon( QIcon( ":/icons/ratio.png" ) );
496        p->setIconSize( QPixmap( ":/icons/ratio.png" ).size() );
497        p->setFlat( true );
498        p->setMenu( m );
499        h->addWidget( p );
500        l = myStatsLabel = new QLabel( this );
501        h->addWidget( l );
502
503    h->addSpacing( HIG::PAD_BIG );
504
505        w = new QWidget( this );
506        w->setMinimumSize( HIG::PAD_BIG, 1 );
507        w->setMaximumSize( HIG::PAD_BIG, 1 );
508        h->addWidget( w );
509        l = myDownloadSpeedLabel = new QLabel( this );
510        h->addWidget( l );
511        l = new QLabel( this );
512        l->setPixmap( getStockIcon( "go-down", QStyle::SP_ArrowDown ).pixmap( smallIconSize ) );
513        h->addWidget( l );
514
515    h->addSpacing( HIG::PAD_BIG );
516
517        w = new QWidget( this );
518        w->setMinimumSize( HIG::PAD_BIG, 1 );
519        w->setMaximumSize( HIG::PAD_BIG, 1 );
520        h->addWidget( w );
521        l = myUploadSpeedLabel = new QLabel;
522        h->addWidget( l );
523        l = new QLabel;
524        l->setPixmap( getStockIcon( "go-up", QStyle::SP_ArrowUp ).pixmap( smallIconSize ) );
525        h->addWidget( l );
526
527    return top;
528}
529
530QMenu *
531TrMainWindow :: createOptionsMenu( )
532{
533    QMenu * menu;
534    QMenu * sub;
535    QAction * a;
536    QActionGroup * g;
537
538    QList<int> stockSpeeds;
539    stockSpeeds << 5 << 10 << 20 << 30 << 40 << 50 << 75 << 100 << 150 << 200 << 250 << 500 << 750;
540    QList<double> stockRatios;
541    stockRatios << 0.25 << 0.50 << 0.75 << 1 << 1.5 << 2 << 3;
542
543    menu = new QMenu;
544    sub = menu->addMenu( tr( "Limit Download Speed" ) );
545        int currentVal = myPrefs.get<int>( Prefs::DSPEED );
546        g = new QActionGroup( this );
547        a = myDlimitOffAction = sub->addAction( tr( "Unlimited" ) );
548        a->setCheckable( true );
549        a->setProperty( PREF_VARIANTS_KEY, QVariantList() << Prefs::DSPEED_ENABLED << false );
550        g->addAction( a );
551        connect( a, SIGNAL(triggered(bool)), this, SLOT(onSetPrefs(bool)) );
552        a = myDlimitOnAction = sub->addAction( tr( "Limited at %1" ).arg( Utils::speedToString( Speed::fromKbps( currentVal ) ) ) );
553        a->setCheckable( true );
554        a->setProperty( PREF_VARIANTS_KEY, QVariantList() << Prefs::DSPEED << currentVal << Prefs::DSPEED_ENABLED << true );
555        g->addAction( a );
556        connect( a, SIGNAL(triggered(bool)), this, SLOT(onSetPrefs(bool)) );
557        sub->addSeparator( );
558        foreach( int i, stockSpeeds ) {
559            a = sub->addAction( Utils::speedToString( Speed::fromKbps(i) ) );
560            a->setProperty( PREF_VARIANTS_KEY, QVariantList() << Prefs::DSPEED << i << Prefs::DSPEED_ENABLED << true );
561            connect( a, SIGNAL(triggered(bool)), this, SLOT(onSetPrefs()));
562        }
563
564    sub = menu->addMenu( tr( "Limit Upload Speed" ) );
565        currentVal = myPrefs.get<int>( Prefs::USPEED );
566        g = new QActionGroup( this );
567        a = myUlimitOffAction = sub->addAction( tr( "Unlimited" ) );
568        a->setCheckable( true );
569        a->setProperty( PREF_VARIANTS_KEY, QVariantList() << Prefs::USPEED_ENABLED << false );
570        g->addAction( a );
571        connect( a, SIGNAL(triggered(bool)), this, SLOT(onSetPrefs(bool)) );
572        a = myUlimitOnAction = sub->addAction( tr( "Limited at %1" ).arg( Utils::speedToString( Speed::fromKbps( currentVal ) ) ) );
573        a->setCheckable( true );
574        a->setProperty( PREF_VARIANTS_KEY, QVariantList() << Prefs::USPEED << currentVal << Prefs::USPEED_ENABLED << true );
575        g->addAction( a );
576        connect( a, SIGNAL(triggered(bool)), this, SLOT(onSetPrefs(bool)) );
577        sub->addSeparator( );
578        foreach( int i, stockSpeeds ) {
579            a = sub->addAction( Utils::speedToString( Speed::fromKbps(i) ) );
580            a->setProperty( PREF_VARIANTS_KEY, QVariantList() << Prefs::USPEED << i << Prefs::USPEED_ENABLED << true );
581            connect( a, SIGNAL(triggered(bool)), this, SLOT(onSetPrefs()));
582        }
583
584    menu->addSeparator( );
585    sub = menu->addMenu( tr( "Stop Seeding at Ratio" ) );
586
587        double d = myPrefs.get<double>( Prefs::RATIO );
588        g = new QActionGroup( this );
589        a = myRatioOffAction = sub->addAction( tr( "Seed Forever" ) );
590        a->setCheckable( true );
591        a->setProperty( PREF_VARIANTS_KEY, QVariantList() << Prefs::RATIO_ENABLED << false );
592        g->addAction( a );
593        connect( a, SIGNAL(triggered(bool)), this, SLOT(onSetPrefs(bool)) );
594        a = myRatioOnAction = sub->addAction( tr( "Stop at Ratio (%1)" ).arg( Utils::ratioToString( d ) ) );
595        a->setCheckable( true );
596        a->setProperty( PREF_VARIANTS_KEY, QVariantList() << Prefs::RATIO << d << Prefs::RATIO_ENABLED << true );
597        g->addAction( a );
598        connect( a, SIGNAL(triggered(bool)), this, SLOT(onSetPrefs(bool)) );
599        sub->addSeparator( );
600        foreach( double i, stockRatios ) {
601            a = sub->addAction( Utils::ratioToString( i ) );
602            a->setProperty( PREF_VARIANTS_KEY, QVariantList() << Prefs::RATIO << i << Prefs::RATIO_ENABLED << true );
603            connect( a, SIGNAL(triggered(bool)), this, SLOT(onSetPrefs()));
604        }
605
606    return menu;
607}
608
609/****
610*****
611****/
612
613void
614TrMainWindow :: setSortPref( int i )
615{
616    myPrefs.set( Prefs::SORT_MODE, SortMode( i ) );
617}
618void TrMainWindow :: onSortByActivityToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_ACTIVITY ); }
619void TrMainWindow :: onSortByAgeToggled      ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_AGE );      }
620void TrMainWindow :: onSortByETAToggled      ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_ETA );      }
621void TrMainWindow :: onSortByNameToggled     ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_NAME );     }
622void TrMainWindow :: onSortByProgressToggled ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_PROGRESS ); }
623void TrMainWindow :: onSortByRatioToggled    ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_RATIO );    }
624void TrMainWindow :: onSortBySizeToggled     ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_SIZE );     }
625void TrMainWindow :: onSortByStateToggled    ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_STATE );    }
626void TrMainWindow :: onSortByTrackerToggled  ( bool b ) { if( b ) setSortPref( SortMode::SORT_BY_TRACKER );  }
627
628void
629TrMainWindow :: setSortAscendingPref( bool b )
630{
631    myPrefs.set( Prefs::SORT_REVERSED, b );
632}
633
634/****
635*****
636****/
637
638void
639TrMainWindow :: onPrefsDestroyed( )
640{
641    myPrefsDialog = 0;
642}
643
644void
645TrMainWindow :: openPreferences( )
646{
647    if( myPrefsDialog == 0 ) {
648        myPrefsDialog = new PrefsDialog( mySession, myPrefs, this );
649        connect( myPrefsDialog, SIGNAL(destroyed(QObject*)), this, SLOT(onPrefsDestroyed()));
650    }
651
652    myPrefsDialog->show( );
653}
654
655void
656TrMainWindow :: onDetailsDestroyed( )
657{
658    myDetailsDialog = 0;
659}
660
661void
662TrMainWindow :: openProperties( )
663{
664    if( myDetailsDialog == 0 ) {
665        myDetailsDialog = new Details( mySession, myPrefs, myModel, this );
666        connect( myDetailsDialog, SIGNAL(destroyed(QObject*)), this, SLOT(onDetailsDestroyed()));
667    }
668
669    myDetailsDialog->setIds( getSelectedTorrents( ) );
670    myDetailsDialog->show( );
671}
672
673void
674TrMainWindow :: setLocation( )
675{
676    QDialog * d = new RelocateDialog( mySession, myModel, getSelectedTorrents(), this );
677    d->show( );
678}
679
680void
681TrMainWindow :: openFolder( )
682{
683    const int torrentId( *getSelectedTorrents().begin() );
684    const Torrent * tor( myModel.getTorrentFromId( torrentId ) );
685    const QString path( tor->getPath( ) );
686    QDesktopServices :: openUrl( QUrl::fromLocalFile( path ) );
687}
688
689void
690TrMainWindow :: copyMagnetLinkToClipboard( )
691{
692    const int id( *getSelectedTorrents().begin() );
693    mySession.copyMagnetLinkToClipboard( id );
694}
695
696void
697TrMainWindow :: openHelp( )
698{
699    const char * fmt = "http://www.transmissionbt.com/help/gtk/%d.%dx";
700    int major, minor;
701    sscanf( SHORT_VERSION_STRING, "%d.%d", &major, &minor );
702    char url[128];
703    tr_snprintf( url, sizeof( url ), fmt, major, minor/10 );
704    QDesktopServices :: openUrl( QUrl( QString( url ) ) );
705}
706
707void
708TrMainWindow :: refreshTitle( )
709{
710    QString title( "Transmission" );
711    const QUrl url( mySession.getRemoteUrl( ) );
712    if( !url.isEmpty() )
713        title += tr( " - %1:%2" ).arg( url.host() ).arg( url.port() );
714    setWindowTitle( title );
715}
716
717void
718TrMainWindow :: refreshVisibleCount( )
719{
720    const int visibleCount( myFilterModel.rowCount( ) );
721    const int totalCount( visibleCount + myFilterModel.hiddenRowCount( ) );
722    QString str;
723    if( visibleCount == totalCount )
724        str = tr( "%Ln Torrent(s)", 0, totalCount );
725    else
726        str = tr( "%L1 of %Ln Torrent(s)", 0, totalCount ).arg( visibleCount );
727    myVisibleCountLabel->setText( str );
728    myVisibleCountLabel->setVisible( totalCount > 0 );
729}
730
731void
732TrMainWindow :: refreshStatusBar( )
733{
734    const Speed up( myModel.getUploadSpeed( ) );
735    const Speed down( myModel.getDownloadSpeed( ) );
736    myUploadSpeedLabel->setText( Utils :: speedToString( up ) );
737    myDownloadSpeedLabel->setText( Utils :: speedToString( down ) );
738
739    myNetworkLabel->setVisible( !mySession.isServer( ) );
740
741    const QString mode( myPrefs.getString( Prefs::STATUSBAR_STATS ) );
742    QString str;
743
744    if( mode == "session-ratio" )
745    {
746        str = tr( "Ratio: %1" ).arg( Utils :: ratioToString( mySession.getStats().ratio ) );
747    }
748    else if( mode == "session-transfer" )
749    {
750        const tr_session_stats& stats( mySession.getStats( ) );
751        str = tr( "Down: %1, Up: %2" ).arg( Utils :: sizeToString( stats.downloadedBytes ) )
752                                      .arg( Utils :: sizeToString( stats.uploadedBytes ) );
753    }
754    else if( mode == "total-transfer" )
755    {
756        const tr_session_stats& stats( mySession.getCumulativeStats( ) );
757        str = tr( "Down: %1, Up: %2" ).arg( Utils :: sizeToString( stats.downloadedBytes ) )
758                                      .arg( Utils :: sizeToString( stats.uploadedBytes ) );
759    }
760    else // default is "total-ratio"
761    {
762        str = tr( "Ratio: %1" ).arg( Utils :: ratioToString( mySession.getCumulativeStats().ratio ) );
763    }
764
765    myStatsLabel->setText( str );
766}
767
768void
769TrMainWindow :: refreshActionSensitivity( )
770{
771    int selected( 0 );
772    int paused( 0 );
773    int selectedAndPaused( 0 );
774    int canAnnounce( 0 );
775    const QAbstractItemModel * model( ui.listView->model( ) );
776    const QItemSelectionModel * selectionModel( ui.listView->selectionModel( ) );
777    const int rowCount( model->rowCount( ) );
778
779    // count how many torrents are selected, paused, etc
780    for( int row=0; row<rowCount; ++row ) {
781        const QModelIndex modelIndex( model->index( row, 0 ) );
782        assert( model == modelIndex.model( ) );
783        const Torrent * tor( model->data( modelIndex, TorrentModel::TorrentRole ).value<const Torrent*>( ) );
784        if( tor ) {
785            const bool isSelected( selectionModel->isSelected( modelIndex ) );
786            const bool isPaused( tor->isPaused( ) );
787            if( isSelected )
788                ++selected;
789            if( isPaused )
790                ++ paused;
791            if( isSelected && isPaused )
792                ++selectedAndPaused;
793            if( tor->canManualAnnounce( ) )
794                ++canAnnounce;
795        }
796    }
797
798    const bool haveSelection( selected > 0 );
799    ui.action_Verify->setEnabled( haveSelection );
800    ui.action_Remove->setEnabled( haveSelection );
801    ui.action_Delete->setEnabled( haveSelection );
802    ui.action_Properties->setEnabled( haveSelection );
803    ui.action_DeselectAll->setEnabled( haveSelection );
804    ui.action_SetLocation->setEnabled( haveSelection );
805
806    const bool oneSelection( selected == 1 );
807    ui.action_OpenFolder->setEnabled( oneSelection && mySession.isLocal( ) );
808    ui.action_CopyMagnetToClipboard->setEnabled( oneSelection );
809
810    ui.action_SelectAll->setEnabled( selected < rowCount );
811    ui.action_StartAll->setEnabled( paused > 0 );
812    ui.action_PauseAll->setEnabled( paused < rowCount );
813    ui.action_Start->setEnabled( selectedAndPaused > 0 );
814    ui.action_Pause->setEnabled( selectedAndPaused < selected );
815    ui.action_Announce->setEnabled( selected > 0 && ( canAnnounce == selected ) );
816
817    if( myDetailsDialog )
818        myDetailsDialog->setIds( getSelectedTorrents( ) );
819}
820
821/**
822***
823**/
824
825void
826TrMainWindow :: clearSelection( )
827{
828    ui.action_DeselectAll->trigger( );
829}
830
831QSet<int>
832TrMainWindow :: getSelectedTorrents( ) const
833{
834    QSet<int> ids;
835
836    foreach( QModelIndex index, ui.listView->selectionModel( )->selectedRows( ) )
837    {
838        const Torrent * tor( index.model()->data( index, TorrentModel::TorrentRole ).value<const Torrent*>( ) );
839        ids.insert( tor->id( ) );
840    }
841
842    return ids;
843}
844
845void
846TrMainWindow :: startSelected( )
847{
848    mySession.startTorrents( getSelectedTorrents( ) );
849}
850void
851TrMainWindow :: pauseSelected( )
852{
853    mySession.pauseTorrents( getSelectedTorrents( ) );
854}
855void
856TrMainWindow :: startAll( )
857{
858    mySession.startTorrents( );
859}
860void
861TrMainWindow :: pauseAll( )
862{
863    mySession.pauseTorrents( );
864}
865void
866TrMainWindow :: removeSelected( )
867{
868    removeTorrents( false );
869}
870void
871TrMainWindow :: deleteSelected( )
872{
873    removeTorrents( true );
874}
875void
876TrMainWindow :: verifySelected( )
877{
878    mySession.verifyTorrents( getSelectedTorrents( ) );
879}
880void
881TrMainWindow :: reannounceSelected( )
882{
883    mySession.reannounceTorrents( getSelectedTorrents( ) );
884}
885
886/**
887***
888**/
889
890void TrMainWindow :: setShowMode     ( int i ) { myPrefs.set( Prefs::FILTER_MODE, FilterMode( i ) ); }
891void TrMainWindow :: showAll         ( ) { setShowMode( FilterMode :: SHOW_ALL ); }
892void TrMainWindow :: showActive      ( ) { setShowMode( FilterMode :: SHOW_ACTIVE ); }
893void TrMainWindow :: showDownloading ( ) { setShowMode( FilterMode :: SHOW_DOWNLOADING ); }
894void TrMainWindow :: showSeeding     ( ) { setShowMode( FilterMode :: SHOW_SEEDING ); }
895void TrMainWindow :: showPaused      ( ) { setShowMode( FilterMode :: SHOW_PAUSED ); }
896
897void TrMainWindow :: filterByName    ( ) { myFilterModel.setTextMode( TorrentFilter :: FILTER_BY_NAME ); }
898void TrMainWindow :: filterByTracker ( ) { myFilterModel.setTextMode( TorrentFilter :: FILTER_BY_TRACKER ); }
899void TrMainWindow :: filterByFiles   ( ) { myFilterModel.setTextMode( TorrentFilter :: FILTER_BY_FILES ); }
900
901void TrMainWindow :: showTotalRatio      ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "total-ratio"); }
902void TrMainWindow :: showTotalTransfer   ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "total-transfer"); }
903void TrMainWindow :: showSessionRatio    ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "session-ratio"); }
904void TrMainWindow :: showSessionTransfer ( ) { myPrefs.set( Prefs::STATUSBAR_STATS, "session-transfer"); }
905
906/**
907***
908**/
909
910void
911TrMainWindow :: setCompactView( bool visible )
912{
913    myPrefs.set( Prefs :: COMPACT_VIEW, visible );
914}
915void
916TrMainWindow :: setTrayIconVisible( bool visible )
917{
918    myPrefs.set( Prefs :: SHOW_TRAY_ICON, visible );
919}
920void
921TrMainWindow :: toggleSpeedMode( )
922{
923    myPrefs.toggleBool( Prefs :: ALT_SPEED_LIMIT_ENABLED );
924}
925void
926TrMainWindow :: setToolbarVisible( bool visible )
927{
928    myPrefs.set( Prefs::TOOLBAR, visible );
929}
930void
931TrMainWindow :: setFilterbarVisible( bool visible )
932{
933    myPrefs.set( Prefs::FILTERBAR, visible );
934}
935void
936TrMainWindow :: setStatusbarVisible( bool visible )
937{
938    myPrefs.set( Prefs::STATUSBAR, visible );
939}
940
941/**
942***
943**/
944
945void
946TrMainWindow :: toggleWindows( bool doShow )
947{
948    if( !doShow )
949    {
950        hide( );
951    }
952    else
953    {
954        if ( !isVisible( ) ) show( );
955        if ( isMinimized( ) ) showNormal( );
956        activateWindow( );
957        raise( );
958    }
959}
960
961void
962TrMainWindow :: trayActivated( QSystemTrayIcon::ActivationReason reason )
963{
964    if( reason == QSystemTrayIcon::Trigger )
965    {
966        if( isMinimized ( ) )
967            toggleWindows( true );
968        else
969            ui.action_ShowMainWindow->toggle( );
970    }
971}
972
973
974void
975TrMainWindow :: refreshPref( int key )
976{
977    bool b;
978    int i;
979    QString str;
980
981    switch( key )
982    {
983        case Prefs::STATUSBAR_STATS:
984            str = myPrefs.getString( key );
985            ui.action_TotalRatio->setChecked     ( str == "total-ratio" );
986            ui.action_TotalTransfer->setChecked  ( str == "total-transfer" );
987            ui.action_SessionRatio->setChecked   ( str == "session-ratio" );
988            ui.action_SessionTransfer->setChecked( str == "session-transfer" );
989            refreshStatusBar( );
990            break;
991
992        case Prefs::SORT_REVERSED:
993            ui.action_ReverseSortOrder->setChecked( myPrefs.getBool( key ) );
994            break;
995
996        case Prefs::SORT_MODE:
997            i = myPrefs.get<SortMode>(key).mode( );
998            ui.action_SortByActivity->setChecked ( i == SortMode::SORT_BY_ACTIVITY );
999            ui.action_SortByAge->setChecked      ( i == SortMode::SORT_BY_AGE );
1000            ui.action_SortByETA->setChecked      ( i == SortMode::SORT_BY_ETA );
1001            ui.action_SortByName->setChecked     ( i == SortMode::SORT_BY_NAME );
1002            ui.action_SortByProgress->setChecked ( i == SortMode::SORT_BY_PROGRESS );
1003            ui.action_SortByRatio->setChecked    ( i == SortMode::SORT_BY_RATIO );
1004            ui.action_SortBySize->setChecked     ( i == SortMode::SORT_BY_SIZE );
1005            ui.action_SortByState->setChecked    ( i == SortMode::SORT_BY_STATE );
1006            ui.action_SortByTracker->setChecked  ( i == SortMode::SORT_BY_TRACKER );
1007            break;
1008
1009        case Prefs::DSPEED_ENABLED:
1010            (myPrefs.get<bool>(key) ? myDlimitOnAction : myDlimitOffAction)->setChecked( true );
1011            break;
1012
1013        case Prefs::DSPEED:
1014            myDlimitOnAction->setText( tr( "Limited at %1" ).arg( Utils::speedToString( Speed::fromKbps( myPrefs.get<int>(key) ) ) ) );
1015            break;
1016
1017        case Prefs::USPEED_ENABLED:
1018            (myPrefs.get<bool>(key) ? myUlimitOnAction : myUlimitOffAction)->setChecked( true );
1019            break;
1020
1021        case Prefs::USPEED:
1022            myUlimitOnAction->setText( tr( "Limited at %1" ).arg( Utils::speedToString( Speed::fromKbps( myPrefs.get<int>(key) ) ) ) );
1023            break;
1024
1025        case Prefs::RATIO_ENABLED:
1026            (myPrefs.get<bool>(key) ? myRatioOnAction : myRatioOffAction)->setChecked( true );
1027            break;
1028
1029        case Prefs::RATIO:
1030            myRatioOnAction->setText( tr( "Stop at Ratio (%1)" ).arg( Utils::ratioToString( myPrefs.get<double>(key) ) ) );
1031            break;
1032
1033        case Prefs::FILTER_MODE:
1034            i = myPrefs.get<FilterMode>(key).mode( );
1035            for( int j=0; j<FilterMode::NUM_MODES; ++j )
1036                myFilterButtons[j]->setChecked( i==j );
1037            break;
1038
1039        case Prefs::FILTERBAR:
1040            b = myPrefs.getBool( key );
1041            myFilterBar->setVisible( b );
1042            ui.action_Filterbar->setChecked( b );
1043            break;
1044
1045        case Prefs::STATUSBAR:
1046            b = myPrefs.getBool( key );
1047            myStatusBar->setVisible( b );
1048            ui.action_Statusbar->setChecked( b );
1049            break;
1050
1051        case Prefs::TOOLBAR:
1052            b = myPrefs.getBool( key );
1053            ui.toolBar->setVisible( b );
1054            ui.action_Toolbar->setChecked( b );
1055            break;
1056
1057        case Prefs::SHOW_TRAY_ICON:
1058            b = myPrefs.getBool( key );
1059            ui.action_TrayIcon->setChecked( b );
1060            myTrayIcon.setVisible( b );
1061            break;
1062
1063        case Prefs::COMPACT_VIEW:
1064            b = myPrefs.getBool( key );
1065            ui.action_CompactView->setChecked( b );
1066            ui.listView->setItemDelegate( b ? myTorrentDelegateMin : myTorrentDelegate );
1067            ui.listView->reset( ); // force the rows to resize
1068            break;
1069
1070        case Prefs::MAIN_WINDOW_X:
1071        case Prefs::MAIN_WINDOW_Y:
1072        case Prefs::MAIN_WINDOW_WIDTH:
1073        case Prefs::MAIN_WINDOW_HEIGHT:
1074            setGeometry( myPrefs.getInt( Prefs::MAIN_WINDOW_X ),
1075                         myPrefs.getInt( Prefs::MAIN_WINDOW_Y ),
1076                         myPrefs.getInt( Prefs::MAIN_WINDOW_WIDTH ),
1077                         myPrefs.getInt( Prefs::MAIN_WINDOW_HEIGHT ) );
1078            break;
1079
1080        case Prefs :: ALT_SPEED_LIMIT_ENABLED:
1081        case Prefs :: ALT_SPEED_LIMIT_UP:
1082        case Prefs :: ALT_SPEED_LIMIT_DOWN: {
1083            b = myPrefs.getBool( Prefs :: ALT_SPEED_LIMIT_ENABLED );
1084            myAltSpeedButton->setChecked( b );
1085            myAltSpeedButton->setIcon( b ? mySpeedModeOnIcon : mySpeedModeOffIcon );
1086            const QString fmt = b ? tr( "Click to disable Temporary Speed Limits\n(%1 down, %2 up)" )
1087                                  : tr( "Click to enable Temporary Speed Limits\n(%1 down, %2 up)" );
1088            const Speed d = Speed::fromKbps( myPrefs.getInt( Prefs::ALT_SPEED_LIMIT_DOWN ) );
1089            const Speed u = Speed::fromKbps( myPrefs.getInt( Prefs::ALT_SPEED_LIMIT_UP ) );
1090            myAltSpeedButton->setToolTip( fmt.arg( Utils::speedToString( d ) )
1091                                             .arg( Utils::speedToString( u ) ) );
1092            break;
1093        }
1094
1095        default:
1096            break;
1097    }
1098}
1099
1100/***
1101****
1102***/
1103
1104void
1105TrMainWindow :: newTorrent( )
1106{
1107    MakeDialog * dialog = new MakeDialog( mySession, this );
1108    dialog->show( );
1109}
1110
1111void
1112TrMainWindow :: openTorrent( )
1113{
1114    QFileDialog * myFileDialog;
1115    myFileDialog = new QFileDialog( this,
1116                                    tr( "Add Torrent" ),
1117                                    myPrefs.getString( Prefs::OPEN_DIALOG_FOLDER ),
1118                                    tr( "Torrent Files (*.torrent);;All Files (*.*)" ) );
1119    myFileDialog->setFileMode( QFileDialog::ExistingFiles );
1120
1121    QCheckBox * button = new QCheckBox( tr( "Show &options dialog" ) );
1122    button->setChecked( myPrefs.getBool( Prefs::OPTIONS_PROMPT ) );
1123    QGridLayout * layout = dynamic_cast<QGridLayout*>(myFileDialog->layout());
1124    layout->addWidget( button, layout->rowCount( ), 0, 1, -1, Qt::AlignLeft );
1125    myFileDialogOptionsCheck = button;
1126
1127    connect( myFileDialog, SIGNAL(filesSelected(const QStringList&)),
1128             this, SLOT(addTorrents(const QStringList&)));
1129
1130    myFileDialog->show( );
1131}
1132
1133void
1134TrMainWindow :: openURL( )
1135{
1136    QString tmp;
1137    openURL( tmp );
1138}
1139
1140void
1141TrMainWindow :: openURL( QString url )
1142{
1143    bool ok;
1144    const QString key = QInputDialog::getText( this,
1145                                               tr( "Add URL or Magnet Link" ),
1146                                               tr( "Add URL or Magnet Link" ),
1147                                               QLineEdit::Normal,
1148                                               url,
1149                                               &ok );
1150    if( ok && !key.isEmpty( ) )
1151        mySession.addTorrent( key );
1152}
1153
1154void
1155TrMainWindow :: addTorrents( const QStringList& filenames )
1156{
1157    foreach( const QString& filename, filenames )
1158        addTorrent( filename );
1159}
1160
1161void
1162TrMainWindow :: addTorrent( const QString& filename )
1163{
1164    if( !myFileDialogOptionsCheck->isChecked( ) ) {
1165        mySession.addTorrent( filename );
1166        QApplication :: alert ( this );
1167    } else {
1168        Options * o = new Options( mySession, myPrefs, filename, this );
1169        o->show( );
1170        QApplication :: alert( o );
1171    }
1172}
1173
1174void
1175TrMainWindow :: removeTorrents( const bool deleteFiles )
1176{
1177    QSet<int> ids;
1178    QMessageBox msgBox( this );
1179    QString primary_text, secondary_text;
1180    int incomplete = 0;
1181    int connected  = 0;
1182    int count;
1183
1184    foreach( QModelIndex index, ui.listView->selectionModel( )->selectedRows( ) )
1185    {
1186        const Torrent * tor( index.model()->data( index, TorrentModel::TorrentRole ).value<const Torrent*>( ) );
1187        ids.insert( tor->id( ) );
1188        if( tor->connectedPeers( ) )
1189            ++connected;
1190        if( !tor->isDone( ) )
1191            ++incomplete;
1192    }
1193
1194    if( ids.isEmpty() )
1195        return;
1196    count = ids.size();
1197
1198    if( !deleteFiles )
1199    {
1200        primary_text = ( count == 1 )
1201            ? tr( "Remove torrent?" )
1202            : tr( "Remove %1 torrents?" ).arg( count );
1203    }
1204    else
1205    {
1206        primary_text = ( count == 1 )
1207            ? tr( "Delete this torrent's downloaded files?" )
1208            : tr( "Delete these %1 torrents' downloaded files?" ).arg( count );
1209    }
1210
1211    if( !incomplete && !connected )
1212    {
1213        secondary_text = ( count == 1 )
1214            ? tr( "Once removed, continuing the transfer will require the torrent file or magnet link." )
1215            : tr( "Once removed, continuing the transfers will require the torrent files or magnet links." );
1216    }
1217    else if( count == incomplete )
1218    {
1219        secondary_text = ( count == 1 )
1220            ? tr( "This torrent has not finished downloading." )
1221            : tr( "These torrents have not finished downloading." );
1222    }
1223    else if( count == connected )
1224    {
1225        secondary_text = ( count == 1 )
1226            ? tr( "This torrent is connected to peers." )
1227            : tr( "These torrents are connected to peers." );
1228    }
1229    else
1230    {
1231        if( connected )
1232        {
1233            secondary_text = ( connected == 1 )
1234                ? tr( "One of these torrents is connected to peers." )
1235                : tr( "Some of these torrents are connected to peers." );
1236        }
1237
1238        if( connected && incomplete )
1239        {
1240            secondary_text += "\n";
1241        }
1242
1243        if( incomplete )
1244        {
1245            secondary_text += ( incomplete == 1 )
1246                ? tr( "One of these torrents has not finished downloading." )
1247                : tr( "Some of these torrents have not finished downloading." );
1248        }
1249    }
1250
1251    msgBox.setWindowTitle( QString(" ") );
1252    msgBox.setText( QString( "<big><b>%1</big></b>" ).arg( primary_text ) );
1253    msgBox.setInformativeText( secondary_text );
1254    msgBox.setStandardButtons( QMessageBox::Ok | QMessageBox::Cancel );
1255    msgBox.setDefaultButton( QMessageBox::Cancel );
1256    msgBox.setIcon( QMessageBox::Question );
1257    /* hack needed to keep the dialog from being too narrow */
1258    QGridLayout* layout = (QGridLayout*)msgBox.layout();
1259    QSpacerItem* spacer = new QSpacerItem( 450, 0, QSizePolicy::Minimum, QSizePolicy::Expanding );
1260    layout->addItem( spacer, layout->rowCount(), 0, 1, layout->columnCount() );
1261
1262    if( msgBox.exec() == QMessageBox::Ok )
1263    {
1264        ui.listView->selectionModel()->clear();
1265        mySession.removeTorrents( ids, deleteFiles );
1266    }
1267}
1268
1269/***
1270****
1271***/
1272
1273void
1274TrMainWindow :: updateNetworkIcon( )
1275{
1276    const time_t now = time( NULL );
1277    const int period = 3;
1278    const bool isSending = now - myLastSendTime <= period;
1279    const bool isReading = now - myLastReadTime <= period;
1280    const char * key;
1281
1282    if( isSending && isReading )
1283        key = "network-transmit-receive";
1284    else if( isSending )
1285        key = "network-transmit";
1286    else if( isReading )
1287        key = "network-receive";
1288    else
1289        key = "network-idle";
1290
1291    QIcon icon = getStockIcon( key, QStyle::SP_DriveNetIcon );
1292    QPixmap pixmap = icon.pixmap ( 16, 16 );
1293    myNetworkLabel->setPixmap( pixmap );
1294    myNetworkLabel->setToolTip( isSending || isReading
1295        ? tr( "Transmission server is responding" )
1296        : tr( "Last response from server was %1 ago" ).arg( Utils::timeToString( now-std::max(myLastReadTime,myLastSendTime))));
1297}
1298
1299void
1300TrMainWindow :: onNetworkTimer( )
1301{
1302    updateNetworkIcon( );
1303}
1304
1305void
1306TrMainWindow :: dataReadProgress( )
1307{
1308    myLastReadTime = time( NULL );
1309    updateNetworkIcon( );
1310}
1311
1312void
1313TrMainWindow :: dataSendProgress( )
1314{
1315    myLastSendTime = time( NULL );
1316    updateNetworkIcon( );
1317}
1318
1319void
1320TrMainWindow :: wrongAuthentication( )
1321{
1322    mySession.stop( );
1323    mySessionDialog->show( );
1324}
Note: See TracBrowser for help on using the repository browser.