2
* Xmission - a cross-platform bittorrent client
3
* Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; version 2 of the License.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
18
* $Id: xmission.cc 4495 2008-01-05 18:17:23Z charles $
29
#include <wx/artprov.h>
30
#include <wx/bitmap.h>
31
#include <wx/cmdline.h>
32
#include <wx/config.h>
33
#include <wx/dcmemory.h>
35
#include <wx/filename.h>
38
#include <wx/listctrl.h>
39
#include <wx/notebook.h>
40
#include <wx/snglinst.h>
41
#include <wx/splitter.h>
42
#include <wx/statline.h>
43
#include <wx/taskbar.h>
44
#include <wx/tglbtn.h>
45
#include <wx/toolbar.h>
47
#if wxCHECK_VERSION(2,8,0)
48
#include <wx/aboutdlg.h>
53
#include <libtransmission/transmission.h>
54
#include <libtransmission/utils.h>
56
#include <images/play.xpm>
57
#include <images/stop.xpm>
58
#include <images/plus.xpm>
59
#include <images/minus.xpm>
61
#include <images/systray.xpm>
62
#include <images/transmission.xpm>
66
#include "speed-stats.h"
68
#include "torrent-list.h"
69
#include "torrent-stats.h"
77
int bestDecimal( double num ) {
78
if ( num < 10 ) return 2;
79
if ( num < 100 ) return 1;
83
wxString toWxStr( const char * s )
85
return wxString( s, wxConvUTF8 );
88
std::string toStr( const wxString& xstr )
90
return std::string( xstr.mb_str( *wxConvCurrent ) );
93
wxString getReadableSize( uint64_t size )
96
static const char *sizestrs[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
97
for ( i=0; size>>10; ++i ) size = size>>10;
99
snprintf( buf, sizeof(buf), "%.*f %s", bestDecimal(size), (double)size, sizestrs[i] );
100
return toWxStr( buf );
103
wxString getReadableSize( float f )
105
return getReadableSize( (uint64_t)f );
108
wxString getReadableSpeed( float kib_sec )
110
wxString xstr = getReadableSize(1024*kib_sec);
118
const wxCmdLineEntryDesc cmdLineDesc[] =
120
{ wxCMD_LINE_SWITCH, _T("p"), _("pause"), _("pauses all the torrents on startup"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
121
{ wxCMD_LINE_NONE, NULL, NULL, NULL, wxCMD_LINE_VAL_STRING, 0 }
129
class MyApp : public wxApp
131
virtual bool OnInit();
133
wxSingleInstanceChecker * myChecker;
138
tr_handle * handle = NULL;
140
typedef std::vector<tr_torrent*> torrents_v;
143
class MyFrame : public wxFrame, public TorrentListCtrl::Listener
146
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, bool paused);
150
void OnExit( wxCommandEvent& );
151
void OnAbout( wxCommandEvent& );
152
void OnOpen( wxCommandEvent& );
154
void OnStart( wxCommandEvent& );
155
void OnStartUpdate( wxUpdateUIEvent& );
157
void OnStop( wxCommandEvent& );
158
void OnStopUpdate( wxUpdateUIEvent& );
160
void OnRemove( wxCommandEvent& );
161
void OnRemoveUpdate( wxUpdateUIEvent& );
163
void OnRecheck( wxCommandEvent& );
164
void OnRecheckUpdate( wxUpdateUIEvent& );
166
void OnSelectAll( wxCommandEvent& );
167
void OnSelectAllUpdate( wxUpdateUIEvent& );
168
void OnDeselectAll( wxCommandEvent& );
169
void OnDeselectAllUpdate( wxUpdateUIEvent& );
171
void OnFilterChanged( wxCommandEvent& );
173
void OnPulse( wxTimerEvent& );
175
virtual void OnTorrentListSelectionChanged( TorrentListCtrl*, const std::set<tr_torrent*>& );
178
void RefreshFilterCounts( );
179
void ApplyCurrentFilter( );
183
wxTimer myPulseTimer;
186
TorrentListCtrl * myTorrentList;
187
TorrentStats * myTorrentStats;
188
wxListCtrl * myFilters;
189
wxTaskBarIcon * myTrayIcon;
191
wxIcon myTrayIconIcon;
192
SpeedStats * mySpeedStats;
193
torrents_v myTorrents;
194
torrents_v mySelectedTorrents;
196
std::string mySavePath;
198
wxToggleButton* myFilterButtons[TorrentFilter::N_FILTERS];
201
DECLARE_EVENT_TABLE()
209
ID_SHOW_DEBUG_WINDOW,
214
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
215
EVT_COMMAND_RANGE( ID_Filter, ID_Filter+TorrentFilter::N_FILTERS, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, MyFrame::OnFilterChanged )
216
EVT_MENU ( wxID_ABOUT, MyFrame::OnAbout )
217
EVT_TIMER ( ID_Pulse, MyFrame::OnPulse )
218
EVT_MENU ( wxID_EXIT, MyFrame::OnExit )
219
EVT_MENU ( wxID_OPEN, MyFrame::OnOpen )
220
EVT_MENU ( ID_START, MyFrame::OnStart )
221
EVT_UPDATE_UI( ID_START, MyFrame::OnStartUpdate )
222
EVT_MENU ( wxID_STOP, MyFrame::OnStop )
223
EVT_UPDATE_UI( wxID_STOP, MyFrame::OnStopUpdate )
224
EVT_MENU ( wxID_REFRESH, MyFrame::OnRecheck )
225
EVT_UPDATE_UI( wxID_REFRESH, MyFrame::OnRecheckUpdate )
226
EVT_MENU ( wxID_REMOVE, MyFrame::OnRemove )
227
EVT_UPDATE_UI( wxID_REMOVE, MyFrame::OnRemoveUpdate )
228
EVT_MENU ( wxID_SELECTALL, MyFrame::OnSelectAll )
229
EVT_UPDATE_UI( wxID_SELECTALL, MyFrame::OnSelectAllUpdate )
230
EVT_MENU ( ID_DESELECTALL, MyFrame::OnDeselectAll )
231
EVT_UPDATE_UI( ID_DESELECTALL, MyFrame::OnDeselectAllUpdate )
238
MyFrame :: OnFilterChanged( wxCommandEvent& e )
240
static bool ignore = false;
244
myFilter = e.GetId() - ID_Filter;
245
std::cerr << " OnFilterChanged, myFilter is now " << myFilter << std::endl;
246
ApplyCurrentFilter ( );
249
for( int i=0; i<TorrentFilter::N_FILTERS; ++i )
250
myFilterButtons[i]->SetValue( i==myFilter );
256
MyFrame :: OnSelectAll( wxCommandEvent& )
258
myTorrentList->SelectAll( );
262
MyFrame :: OnSelectAllUpdate( wxUpdateUIEvent& event )
264
event.Enable( mySelectedTorrents.size() < myTorrents.size() );
268
MyFrame :: OnDeselectAll( wxCommandEvent& )
270
myTorrentList->DeselectAll ( );
274
MyFrame :: OnDeselectAllUpdate( wxUpdateUIEvent& event )
276
event.Enable( !mySelectedTorrents.empty() );
283
MyFrame :: OnStartUpdate( wxUpdateUIEvent& event )
286
foreach( torrents_v, mySelectedTorrents, it )
287
if( tr_torrentStatCached(*it)->status == TR_STATUS_STOPPED )
289
event.Enable( enable );
292
MyFrame :: OnStart( wxCommandEvent& WXUNUSED(unused) )
294
foreach( torrents_v, mySelectedTorrents, it )
295
if( tr_torrentStatCached(*it)->status == TR_STATUS_STOPPED )
296
tr_torrentStart( *it );
303
MyFrame :: OnStopUpdate( wxUpdateUIEvent& event )
306
foreach( torrents_v, mySelectedTorrents, it )
307
if( tr_torrentStatCached(*it)->status != TR_STATUS_STOPPED )
309
event.Enable( enable );
312
MyFrame :: OnStop( wxCommandEvent& WXUNUSED(unused) )
314
foreach( torrents_v, mySelectedTorrents, it )
315
if( tr_torrentStat(*it)->status != TR_STATUS_STOPPED )
316
tr_torrentStop( *it );
323
MyFrame :: OnRemoveUpdate( wxUpdateUIEvent& event )
325
event.Enable( !mySelectedTorrents.empty() );
328
MyFrame :: OnRemove( wxCommandEvent& WXUNUSED(unused) )
330
foreach( torrents_v, mySelectedTorrents, it ) {
331
tr_torrentRemoveSaved( *it );
332
tr_torrentClose( *it );
340
MyFrame :: OnRecheckUpdate( wxUpdateUIEvent& event )
342
event.Enable( !mySelectedTorrents.empty() );
345
MyFrame :: OnRecheck( wxCommandEvent& WXUNUSED(unused) )
347
foreach( torrents_v, mySelectedTorrents, it )
348
tr_torrentRecheck( *it );
354
void MyFrame :: OnOpen( wxCommandEvent& WXUNUSED(event) )
356
const wxString key = _T("prev-directory");
358
myConfig->Read( key, &directory );
359
wxFileDialog * w = new wxFileDialog( this, _T("message"),
361
_T(""), /* default file */
362
_T("Torrent files|*.torrent"),
365
if( w->ShowModal() == wxID_OK )
368
w->GetPaths( paths );
369
size_t nPaths = paths.GetCount();
370
for( size_t i=0; i<nPaths; ++i )
372
const std::string filename = toStr( paths[i] );
373
tr_ctor * ctor = tr_ctorNew( handle );
374
tr_ctorSetMetainfoFromFile( ctor, filename.c_str() );
375
tr_ctorSetDestination( ctor, TR_FALLBACK, mySavePath.c_str() );
376
tr_torrent * tor = tr_torrentNew( handle, ctor, NULL );
380
myTorrents.push_back( tor );
382
ApplyCurrentFilter( );
384
myConfig->Write( key, w->GetDirectory() );
394
handle = tr_init( "wx" );
396
wxCmdLineParser cmdParser( cmdLineDesc, argc, argv );
397
if( cmdParser.Parse ( ) )
400
const wxString name = wxString::Format( _T("Xmission-%s"), wxGetUserId().c_str());
401
myChecker = new wxSingleInstanceChecker( name );
402
if ( myChecker->IsAnotherRunning() ) {
403
wxLogError(_("An instance of Xmission is already running."));
407
const bool paused = cmdParser.Found( _("p") );
409
MyFrame * frame = new MyFrame( _("Xmission"),
415
SetTopWindow( frame );
429
MyFrame :: RefreshFilterCounts( )
431
int hits[ TorrentFilter :: N_FILTERS ];
432
TorrentFilter::CountHits( myTorrents, hits );
433
for( int i=0; i<TorrentFilter::N_FILTERS; ++i )
434
myFilterButtons[i]->SetLabel( TorrentFilter::GetName( i, hits[i] ) );
438
MyFrame :: ApplyCurrentFilter( )
440
torrents_v tmp( myTorrents );
441
TorrentFilter :: RemoveFailures( myFilter, tmp );
442
myTorrentList->Assign( tmp );
446
MyFrame :: OnTorrentListSelectionChanged( TorrentListCtrl* list,
447
const std::set<tr_torrent*>& torrents )
449
assert( list == myTorrentList );
450
mySelectedTorrents.assign( torrents.begin(), torrents.end() );
452
tr_torrent * tor = mySelectedTorrents.empty() ? NULL : mySelectedTorrents.front();
453
mySpeedStats->SetTorrent( tor );
457
MyFrame :: OnPulse(wxTimerEvent& WXUNUSED(event) )
460
if ( !tr_torrentCount(handle) || myExitTime<time(0) ) {
468
RefreshFilterCounts( );
469
ApplyCurrentFilter( );
471
mySpeedStats->Pulse( handle );
474
tr_torrentRates( handle, &down, &up );
475
wxString xstr = _("Total DL: ");
476
xstr += getReadableSpeed( down );
477
SetStatusText( xstr, 1 );
478
xstr = _("Total UL: ");
479
xstr += getReadableSpeed( up );
480
SetStatusText( xstr, 2 );
482
xstr = _("Download: ");
483
xstr += getReadableSpeed( down );
485
xstr +=_("Upload: ");
486
xstr += getReadableSpeed( up );
487
myTrayIcon->SetIcon( myTrayIconIcon, xstr );
489
myTorrentList->Refresh ( );
494
myTorrentList->RemoveListener( this );
495
delete myTorrentList;
500
MyFrame :: MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, bool paused):
501
wxFrame((wxFrame*)NULL,-1,title,pos,size),
502
myConfig( new wxConfig( _T("xmission") ) ),
503
myPulseTimer( this, ID_Pulse ),
504
myLogoIcon( transmission_xpm ),
505
myTrayIconIcon( systray_xpm ),
507
myFilter( TorrentFilter::ALL ),
510
myTrayIcon = new wxTaskBarIcon;
511
SetIcon( myLogoIcon );
518
wxString key = _T("port");
519
if( !myConfig->Read( key, &port, TR_DEFAULT_PORT ) )
520
myConfig->Write( key, port );
521
tr_setBindPort( handle, port );
523
key = _T("save-path");
525
if( !myConfig->Read( key, &wxstr, wxFileName::GetHomeDir() ) )
526
myConfig->Write( key, wxstr );
527
mySavePath = toStr( wxstr );
529
const wxString default_colors[SpeedStats::N_COLORS] = {
530
_T("#000000"), // background
531
_T("#32cd32"), // frame
532
_T("#ff0000"), // all up
533
_T("#ff00ff"), // all down
534
_T("#ffff00"), // torrent up
535
_T("#00ff88") // torrent down
537
wxColor speedColors[SpeedStats::N_COLORS];
538
for( int i=0; i<SpeedStats::N_COLORS; ++i )
540
key = SpeedStats::GetColorName( i );
541
myConfig->Read( key, &wxstr, _T("") );
542
std::string str = toStr( wxstr );
545
|| !isxdigit(str[1]) || !isxdigit(str[2])
546
|| !isxdigit(str[3]) || !isxdigit(str[4])
547
|| !isxdigit(str[5]) || !isxdigit(str[6]))
549
myConfig->Write( key, default_colors[i] );
550
str = toStr( default_colors[i] );
553
sscanf( str.c_str()+1, "%02x%02x%02x", &r, &g, &b );
554
speedColors[i].Set( r, g, b );
561
wxMenuBar *menuBar = new wxMenuBar;
563
wxMenu * m = new wxMenu;
564
m->Append( wxID_OPEN, _T("&Open") );
565
m->Append( ID_START, _T("&Start") );
566
m->Append( wxID_STOP, _T("Sto&p") ) ;
567
m->Append( wxID_REFRESH, _T("Re&check") );
568
m->Append( wxID_REMOVE, _T("&Remove") );
569
m->AppendSeparator();
570
m->Append( wxID_NEW, _T("Create &New Torrent") );
571
m->AppendSeparator();
572
m->Append( wxID_CLOSE, _T("&Close") );
573
m->Append( wxID_EXIT, _T("&Exit") );
574
menuBar->Append( m, _T("&File") );
577
m->Append( wxID_SELECTALL, _T("Select &All") );
578
m->Append( ID_DESELECTALL, _T("&Deselect All") );
579
m->AppendSeparator();
580
m->Append( wxID_PREFERENCES, _T("Edit &Preferences") );
581
menuBar->Append( m, _T("&Edit") );
584
m->Append( ID_SHOW_DEBUG_WINDOW, _T("Show &Debug Window") );
585
m->AppendSeparator();
586
m->Append( wxID_ABOUT, _T("&About Xmission") );
587
menuBar->Append( m, _T("&Help") );
595
wxIcon open_icon( plus_xpm );
596
wxIcon exec_icon( play_xpm );
597
wxIcon stop_icon( stop_xpm );
598
wxIcon drop_icon( minus_xpm );
601
wxToolBar* toolbar = CreateToolBar( wxTB_FLAT );
602
toolbar->SetToolBitmapSize( wxSize( 24, 24 ) );
603
bitmap.CopyFromIcon( open_icon );
604
toolbar->AddTool( wxID_OPEN, _T("Open"), bitmap );
605
bitmap.CopyFromIcon( exec_icon );
606
toolbar->AddTool( ID_START, _T("Start"), bitmap );
607
bitmap.CopyFromIcon( stop_icon );
608
toolbar->AddTool( wxID_STOP, _T("Stop"), bitmap );
609
bitmap.CopyFromIcon( drop_icon );
610
toolbar->AddTool( wxID_REMOVE, _T("Remove"), bitmap );
617
wxSplitterWindow * hsplit = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_3DSASH );
618
#if wxCHECK_VERSION(2,5,4)
619
hsplit->SetSashGravity( 0.8 );
622
wxPanel * panel_1 = new wxPanel( hsplit, wxID_ANY );
624
wxBoxSizer * buttonSizer = new wxBoxSizer( wxVERTICAL );
626
for( int i=0; i<TorrentFilter::N_FILTERS; ++i ) {
627
wxToggleButton * b = new wxToggleButton( panel_1, ID_Filter+i, TorrentFilter::GetName(i), wxDefaultPosition, wxDefaultSize, (i==0)?wxRB_GROUP:0 );
629
myFilterButtons[i] = b;
630
buttonSizer->Add( b, 1, wxGROW, 0 );
633
myTorrentList = new TorrentListCtrl( handle, myConfig, panel_1 );
634
myTorrentList->AddListener( this );
636
wxBoxSizer * panelSizer = new wxBoxSizer( wxHORIZONTAL );
637
panelSizer->Add( buttonSizer, 0, wxALL, 5 );
638
panelSizer->Add( myTorrentList, 1, wxGROW|wxALL, 5 );
640
panel_1->SetSizer( panelSizer );
643
wxNotebook * notebook = new wxNotebook( hsplit, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNB_TOP );
644
myTorrentStats = new TorrentStats( notebook );
645
notebook->AddPage( myTorrentStats, _T("General"), false );
646
wxButton * tmp = new wxButton( notebook, wxID_ANY, _T("Hello &World"));
647
notebook->AddPage( tmp, _T("Peers") );
648
tmp = new wxButton( notebook, wxID_ANY, _T("&Hello World"));
649
notebook->AddPage( tmp, _T("Pieces") );
650
tmp = new wxButton( notebook, wxID_ANY, _T("Hello World"));
651
notebook->AddPage( tmp, _T("Files") );
652
mySpeedStats = new SpeedStats( notebook, wxID_ANY );
653
for( int i=0; i<SpeedStats::N_COLORS; ++i )
654
mySpeedStats->SetColor( i, speedColors[i] );
655
notebook->AddPage( mySpeedStats, _T("Speed"), true );
656
tmp = new wxButton( notebook, wxID_ANY, _T("Hello World"));
657
notebook->AddPage( tmp, _T("Logger") );
659
hsplit->SplitHorizontally( panel_1, notebook );
665
const int widths[] = { -1, 150, 150 };
666
wxStatusBar * statusBar = CreateStatusBar( WXSIZEOF(widths) );
667
SetStatusWidths( WXSIZEOF(widths), widths );
668
const int styles[] = { wxSB_FLAT, wxSB_NORMAL, wxSB_NORMAL };
669
statusBar->SetStatusStyles( WXSIZEOF(widths), styles );
675
myPulseTimer.Start( 500 );
678
*** Load the torrents
682
tr_ctor * ctor = tr_ctorNew( handle );
683
tr_ctorSetPaused( ctor, TR_FORCE, paused );
684
tr_ctorSetDestination( ctor, TR_FALLBACK, mySavePath.c_str() );
685
tr_torrent ** torrents = tr_loadTorrents ( handle, ctor, &count );
686
myTorrents.insert( myTorrents.end(), torrents, torrents+count );
695
MyFrame :: OnExit( wxCommandEvent& WXUNUSED( event ) )
700
mySelectedTorrents.clear( );
701
ApplyCurrentFilter( );
705
/* give the connections a max of 10 seconds to shut themselves down */
706
myExitTime = time(0) + 10;
709
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
711
wxIcon ico( transmission_xpm );
713
#if wxCHECK_VERSION(2,8,0)
714
wxAboutDialogInfo info;
715
info.SetName(_T("Xmission"));
716
info.SetVersion(_T(LONG_VERSION_STRING));
717
info.SetCopyright(_T("Copyright 2005-2007 The Transmission Project"));
718
info.SetDescription(_T("A fast and easy BitTorrent client"));
719
info.SetWebSite( _T( "http://www.transmissionbt.com/" ) );
721
info.AddDeveloper( _T("Charles Kerr (Back-end, GTK+, wxWidgets)") );
722
info.AddDeveloper( _T("Mitchell Livingston (Back-end; OS X)") );
723
info.AddDeveloper( _T("Eric Petit (Back-end; OS X)") );
724
info.AddDeveloper( _T("Josh Elsasser (Back-end; GTK+)") );
725
info.AddDeveloper( _T("Bryan Varner (BeOS)") );
728
wxMessageBox(_T("Xmission " LONG_VERSION_STRING "\n"
729
"Copyright 2005-2007 The Transmission Project"),
730
_T("About Xmission"),
731
wxOK|wxICON_INFORMATION, this);