transmission/wx/xmission.cc

684 lines
18 KiB
C++
Raw Normal View History

/*
* This file Copyright (C) 2007 Charles Kerr <charles@rebelbase.com>
*
* This file is licensed by the GPL version 2. Works owned by the
* Transmission project are granted a special exemption to clause 2(b)
* so that the bulk of its code can remain under the MIT license.
* This exemption does not extend to derived works not owned by
* the Transmission project.
*/
#include <set>
#include <map>
#include <string>
#include <vector>
#include <iostream>
#include <stdint.h>
#include <wx/artprov.h>
2007-07-23 17:36:34 +00:00
#include <wx/bitmap.h>
#include <wx/cmdline.h>
#include <wx/config.h>
#include <wx/dcmemory.h>
#include <wx/defs.h>
#include <wx/filename.h>
2007-07-23 17:36:34 +00:00
#include <wx/image.h>
2007-07-23 21:01:26 +00:00
#include <wx/intl.h>
2007-07-23 02:43:25 +00:00
#include <wx/listctrl.h>
2007-07-23 17:36:34 +00:00
#include <wx/notebook.h>
#include <wx/snglinst.h>
2007-07-23 17:36:34 +00:00
#include <wx/splitter.h>
2007-08-03 20:46:02 +00:00
#include <wx/statline.h>
#include <wx/taskbar.h>
#include <wx/tglbtn.h>
#include <wx/toolbar.h>
#include <wx/wx.h>
#if wxCHECK_VERSION(2,8,0)
#include <wx/aboutdlg.h>
#endif
extern "C"
{
#include <libtransmission/transmission.h>
#include <libtransmission/utils.h>
2007-08-03 18:52:01 +00:00
#include <images/play.xpm>
#include <images/stop.xpm>
2007-08-03 18:52:01 +00:00
#include <images/plus.xpm>
#include <images/minus.xpm>
#include <images/systray.xpm>
#include <images/transmission.xpm>
}
#include "foreach.h"
#include "speed-stats.h"
2007-07-23 21:01:26 +00:00
#include "torrent-filter.h"
#include "torrent-list.h"
#include "torrent-stats.h"
2007-07-25 05:37:12 +00:00
/***
****
***/
namespace
{
int bestDecimal( double num ) {
if ( num < 10 ) return 2;
if ( num < 100 ) return 1;
return 0;
}
wxString toWxStr( const char * s )
{
return wxString( s, wxConvUTF8 );
}
std::string toStr( const wxString& xstr )
{
return std::string( xstr.mb_str( *wxConvCurrent ) );
}
wxString getReadableSize( uint64_t size )
{
int i;
static const char *sizestrs[] = { "B", "KiB", "MiB", "GiB", "TiB", "PiB", "EiB" };
for ( i=0; size>>10; ++i ) size = size>>10;
char buf[512];
snprintf( buf, sizeof(buf), "%.*f %s", bestDecimal(size), (double)size, sizestrs[i] );
return toWxStr( buf );
}
wxString getReadableSize( float f )
{
return getReadableSize( (uint64_t)f );
}
wxString getReadableSpeed( float kib_sec )
2007-07-25 05:37:12 +00:00
{
wxString xstr = getReadableSize(1024*kib_sec);
2007-07-25 05:37:12 +00:00
xstr += _T("/s");
return xstr;
}
}
namespace
{
const wxCmdLineEntryDesc cmdLineDesc[] =
{
{ wxCMD_LINE_SWITCH, _T("p"), _("pause"), _("pauses all the torrents on startup"), wxCMD_LINE_VAL_STRING, wxCMD_LINE_PARAM_OPTIONAL },
{ wxCMD_LINE_NONE, NULL, NULL, NULL, wxCMD_LINE_VAL_STRING, 0 }
};
}
2007-07-25 05:37:12 +00:00
/***
****
***/
class MyApp : public wxApp
{
virtual bool OnInit();
virtual ~MyApp();
wxSingleInstanceChecker * myChecker;
};
namespace
{
tr_handle_t * handle = NULL;
2007-07-23 21:01:26 +00:00
typedef std::vector<tr_torrent_t*> torrents_v;
}
2007-07-24 04:36:36 +00:00
class MyFrame : public wxFrame, public TorrentListCtrl::Listener
{
public:
MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, bool paused);
virtual ~MyFrame();
2007-07-23 21:01:26 +00:00
public:
void OnExit( wxCommandEvent& );
void OnAbout( wxCommandEvent& );
void OnOpen( wxCommandEvent& );
void OnStart( wxCommandEvent& );
void OnStartUpdate( wxUpdateUIEvent& );
void OnStop( wxCommandEvent& );
void OnStopUpdate( wxUpdateUIEvent& );
void OnRemove( wxCommandEvent& );
void OnRemoveUpdate( wxUpdateUIEvent& );
void OnRecheck( wxCommandEvent& );
void OnRecheckUpdate( wxUpdateUIEvent& );
void OnSelectAll( wxCommandEvent& );
void OnSelectAllUpdate( wxUpdateUIEvent& );
void OnDeselectAll( wxCommandEvent& );
void OnDeselectAllUpdate( wxUpdateUIEvent& );
void OnFilterToggled( wxCommandEvent& );
void OnPulse( wxTimerEvent& );
2007-07-24 04:36:36 +00:00
virtual void OnTorrentListSelectionChanged( TorrentListCtrl*, const std::set<tr_torrent_t*>& );
2007-07-23 21:01:26 +00:00
private:
void RefreshFilterCounts( );
2007-07-25 05:37:12 +00:00
void ApplyCurrentFilter( );
2007-07-23 21:01:26 +00:00
protected:
wxConfig * myConfig;
wxTimer myPulseTimer;
private:
TorrentListCtrl * myTorrentList;
TorrentStats * myTorrentStats;
2007-07-23 21:01:26 +00:00
wxListCtrl * myFilters;
2007-07-28 00:41:35 +00:00
wxTaskBarIcon * myTrayIcon;
wxIcon myLogoIcon;
wxIcon myTrayIconIcon;
SpeedStats * mySpeedStats;
2007-07-23 21:01:26 +00:00
torrents_v myTorrents;
2007-07-24 04:36:36 +00:00
torrents_v mySelectedTorrents;
int myFilterFlags;
std::string mySavePath;
time_t myExitTime;
wxToggleButton * myFilterToggles[TorrentFilter::N_FILTERS];
private:
DECLARE_EVENT_TABLE()
};
enum
{
ID_START,
ID_DESELECTALL,
ID_EDIT_PREFS,
ID_SHOW_DEBUG_WINDOW,
ID_Pulse,
ID_Filter
};
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_COMMAND_RANGE( ID_Filter, ID_Filter+TorrentFilter::N_FILTERS, wxEVT_COMMAND_TOGGLEBUTTON_CLICKED, MyFrame::OnFilterToggled )
EVT_MENU ( wxID_ABOUT, MyFrame::OnAbout )
EVT_TIMER ( ID_Pulse, MyFrame::OnPulse )
EVT_MENU ( wxID_EXIT, MyFrame::OnExit )
EVT_MENU ( wxID_OPEN, MyFrame::OnOpen )
EVT_MENU ( ID_START, MyFrame::OnStart )
EVT_UPDATE_UI( ID_START, MyFrame::OnStartUpdate )
EVT_MENU ( wxID_STOP, MyFrame::OnStop )
EVT_UPDATE_UI( wxID_STOP, MyFrame::OnStopUpdate )
EVT_MENU ( wxID_REFRESH, MyFrame::OnRecheck )
EVT_UPDATE_UI( wxID_REFRESH, MyFrame::OnRecheckUpdate )
EVT_MENU ( wxID_REMOVE, MyFrame::OnRemove )
EVT_UPDATE_UI( wxID_REMOVE, MyFrame::OnRemoveUpdate )
EVT_MENU ( wxID_SELECTALL, MyFrame::OnSelectAll )
EVT_UPDATE_UI( wxID_SELECTALL, MyFrame::OnSelectAllUpdate )
EVT_MENU ( ID_DESELECTALL, MyFrame::OnDeselectAll )
EVT_UPDATE_UI( ID_DESELECTALL, MyFrame::OnDeselectAllUpdate )
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
void
MyFrame :: OnFilterToggled( wxCommandEvent& e )
{
const int index = e.GetId() - ID_Filter;
int flags = 1<<index;
const bool active = myFilterToggles[index]->GetValue ( );
if( active )
myFilterFlags |= flags;
else
myFilterFlags &= ~flags;
ApplyCurrentFilter ( );
}
void
MyFrame :: OnSelectAll( wxCommandEvent& )
{
myTorrentList->SelectAll( );
}
void
MyFrame :: OnSelectAllUpdate( wxUpdateUIEvent& event )
{
event.Enable( mySelectedTorrents.size() < myTorrents.size() );
}
void
MyFrame :: OnDeselectAll( wxCommandEvent& )
{
myTorrentList->DeselectAll ( );
}
void
MyFrame :: OnDeselectAllUpdate( wxUpdateUIEvent& event )
{
event.Enable( !mySelectedTorrents.empty() );
}
/**
**/
void
MyFrame :: OnStartUpdate( wxUpdateUIEvent& event )
{
unsigned long l = 0;
foreach( torrents_v, mySelectedTorrents, it )
l |= tr_torrentStat(*it)->status;
event.Enable( (l & TR_STATUS_INACTIVE)!=0 );
}
void
MyFrame :: OnStart( wxCommandEvent& WXUNUSED(unused) )
{
foreach( torrents_v, mySelectedTorrents, it )
if( tr_torrentStat(*it)->status & TR_STATUS_INACTIVE )
tr_torrentStart( *it );
}
/**
**/
void
MyFrame :: OnStopUpdate( wxUpdateUIEvent& event )
{
unsigned long l = 0;
foreach( torrents_v, mySelectedTorrents, it )
l |= tr_torrentStat(*it)->status;
event.Enable( (l & TR_STATUS_ACTIVE)!=0 );
}
void
MyFrame :: OnStop( wxCommandEvent& WXUNUSED(unused) )
{
foreach( torrents_v, mySelectedTorrents, it )
if( tr_torrentStat(*it)->status & TR_STATUS_ACTIVE )
tr_torrentStop( *it );
}
/**
**/
void
MyFrame :: OnRemoveUpdate( wxUpdateUIEvent& event )
{
event.Enable( !mySelectedTorrents.empty() );
}
void
MyFrame :: OnRemove( wxCommandEvent& WXUNUSED(unused) )
{
foreach( torrents_v, mySelectedTorrents, it ) {
tr_torrentRemoveSaved( *it );
tr_torrentClose( *it );
}
}
/**
**/
void
MyFrame :: OnRecheckUpdate( wxUpdateUIEvent& event )
{
event.Enable( !mySelectedTorrents.empty() );
}
void
MyFrame :: OnRecheck( wxCommandEvent& WXUNUSED(unused) )
{
foreach( torrents_v, mySelectedTorrents, it )
tr_torrentRecheck( *it );
}
/**
**/
2007-07-26 17:15:56 +00:00
void MyFrame :: OnOpen( wxCommandEvent& WXUNUSED(event) )
{
2007-07-23 15:33:04 +00:00
const wxString key = _T("prev-directory");
wxString directory;
myConfig->Read( key, &directory );
wxFileDialog * w = new wxFileDialog( this, _T("message"),
directory,
_T(""), /* default file */
_T("Torrent files|*.torrent"),
wxOPEN|wxMULTIPLE );
if( w->ShowModal() == wxID_OK )
{
wxArrayString paths;
w->GetPaths( paths );
size_t nPaths = paths.GetCount();
for( size_t i=0; i<nPaths; ++i )
{
2007-07-25 05:37:12 +00:00
const std::string filename = toStr( paths[i] );
tr_torrent_t * tor = tr_torrentInit( handle,
filename.c_str(),
mySavePath.c_str(),
2007-07-25 05:37:12 +00:00
0, NULL );
if( tor )
myTorrents.push_back( tor );
}
2007-07-25 05:37:12 +00:00
ApplyCurrentFilter( );
myConfig->Write( key, w->GetDirectory() );
}
delete w;
}
bool MyApp::OnInit()
{
handle = tr_init( "wx" );
wxCmdLineParser cmdParser( cmdLineDesc, argc, argv );
if( cmdParser.Parse ( ) )
return false;
const wxString name = wxString::Format( _T("Xmission-%s"), wxGetUserId().c_str());
myChecker = new wxSingleInstanceChecker( name );
if ( myChecker->IsAnotherRunning() ) {
wxLogError(_("An instance of Xmission is already running."));
return false;
}
const bool paused = cmdParser.Found( _("p") );
MyFrame * frame = new MyFrame( _("Xmission"),
wxPoint(50,50),
wxSize(900,600),
paused);
frame->Show( true );
SetTopWindow( frame );
return true;
}
MyApp :: ~MyApp()
{
delete myChecker;
}
/***
****
***/
2007-07-23 21:01:26 +00:00
void
MyFrame :: RefreshFilterCounts( )
{
int hits[ TorrentFilter :: N_FILTERS ];
TorrentFilter::CountHits( myTorrents, hits );
2007-07-23 21:01:26 +00:00
for( int i=0; i<TorrentFilter::N_FILTERS; ++i )
myFilterToggles[i]->SetLabel( TorrentFilter::GetName( i, hits[i] ) );
2007-07-23 21:01:26 +00:00
}
void
2007-07-25 05:37:12 +00:00
MyFrame :: ApplyCurrentFilter( )
{
torrents_v tmp( myTorrents );
TorrentFilter :: RemoveFailures( myFilterFlags, tmp );
myTorrentList->Assign( tmp );
}
2007-07-24 04:36:36 +00:00
void
MyFrame :: OnTorrentListSelectionChanged( TorrentListCtrl* list,
const std::set<tr_torrent_t*>& torrents )
{
assert( list == myTorrentList );
mySelectedTorrents.assign( torrents.begin(), torrents.end() );
}
void
MyFrame :: OnPulse(wxTimerEvent& WXUNUSED(event) )
{
if( myExitTime ) {
if ( !tr_torrentCount(handle) || myExitTime<time(0) ) {
2007-07-28 00:41:35 +00:00
delete myTrayIcon;
myTrayIcon = 0;
Destroy( );
return;
}
}
2007-07-23 21:01:26 +00:00
RefreshFilterCounts( );
2007-08-03 20:46:02 +00:00
ApplyCurrentFilter( );
2007-07-23 21:01:26 +00:00
mySpeedStats->Update( handle );
float down, up;
tr_torrentRates( handle, &down, &up );
wxString xstr = _("Total DL: ");
xstr += getReadableSpeed( down );
SetStatusText( xstr, 1 );
xstr = _("Total UL: ");
xstr += getReadableSpeed( up );
SetStatusText( xstr, 2 );
xstr = _("Download: ");
xstr += getReadableSpeed( down );
xstr += _T("\n");
xstr +=_("Upload: ");
xstr += getReadableSpeed( up );
2007-07-28 00:41:35 +00:00
myTrayIcon->SetIcon( myTrayIconIcon, xstr );
myTorrentList->Refresh ( );
}
MyFrame::~MyFrame()
{
2007-07-24 04:36:36 +00:00
myTorrentList->RemoveListener( this );
delete myTorrentList;
delete myConfig;
}
MyFrame :: MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size, bool paused):
wxFrame((wxFrame*)NULL,-1,title,pos,size),
myConfig( new wxConfig( _T("xmission") ) ),
myPulseTimer( this, ID_Pulse ),
myLogoIcon( transmission_xpm ),
2007-07-25 05:37:12 +00:00
myTrayIconIcon( systray_xpm ),
myFilterFlags( ~0 ),
myExitTime( 0 )
{
2007-07-28 00:41:35 +00:00
myTrayIcon = new wxTaskBarIcon;
SetIcon( myLogoIcon );
long port;
wxString key = _T("port");
if( !myConfig->Read( key, &port, 9090 ) )
myConfig->Write( key, port );
tr_setBindPort( handle, port );
key = _T("save-path");
wxString wxstr;
if( !myConfig->Read( key, &wxstr, wxFileName::GetHomeDir() ) )
myConfig->Write( key, wxstr );
mySavePath = toStr( wxstr );
std::cerr << __FILE__ << ':' << __LINE__ << " save-path is [" << mySavePath << ']' << std::endl;
/**
*** Menu
**/
wxMenuBar *menuBar = new wxMenuBar;
wxMenu * m = new wxMenu;
m->Append( wxID_OPEN, _T("&Open") );
m->Append( ID_START, _T("&Start") );
m->Append( wxID_STOP, _T("Sto&p") ) ;
m->Append( wxID_REFRESH, _T("Re&check") );
m->Append( wxID_REMOVE, _T("&Remove") );
m->AppendSeparator();
m->Append( wxID_NEW, _T("Create &New Torrent") );
m->AppendSeparator();
m->Append( wxID_CLOSE, _T("&Close") );
m->Append( wxID_EXIT, _T("&Exit") );
menuBar->Append( m, _T("&File") );
m = new wxMenu;
m->Append( wxID_SELECTALL, _T("Select &All") );
m->Append( ID_DESELECTALL, _T("&Deselect All") );
m->AppendSeparator();
m->Append( wxID_PREFERENCES, _T("Edit &Preferences") );
menuBar->Append( m, _T("&Edit") );
m = new wxMenu;
m->Append( ID_SHOW_DEBUG_WINDOW, _T("Show &Debug Window") );
m->AppendSeparator();
m->Append( wxID_ABOUT, _T("&About Xmission") );
menuBar->Append( m, _T("&Help") );
SetMenuBar(menuBar);
/**
*** Toolbar
**/
2007-08-03 18:52:01 +00:00
wxIcon open_icon( plus_xpm );
wxIcon exec_icon( play_xpm );
2007-07-23 17:36:34 +00:00
wxIcon stop_icon( stop_xpm );
2007-08-03 18:52:01 +00:00
wxIcon drop_icon( minus_xpm );
2007-07-23 17:36:34 +00:00
wxBitmap bitmap;
2007-08-03 20:46:02 +00:00
wxToolBar* toolbar = CreateToolBar( wxTB_FLAT );
toolbar->SetToolBitmapSize( wxSize( 24, 24 ) );
2007-07-23 17:36:34 +00:00
bitmap.CopyFromIcon( open_icon );
toolbar->AddTool( wxID_OPEN, _T("Open"), bitmap );
bitmap.CopyFromIcon( exec_icon );
toolbar->AddTool( ID_START, _T("Start"), bitmap );
bitmap.CopyFromIcon( stop_icon );
toolbar->AddTool( wxID_STOP, _T("Stop"), bitmap );
bitmap.CopyFromIcon( drop_icon );
toolbar->AddTool( wxID_REMOVE, _T("Remove"), bitmap );
toolbar->Realize();
/**
*** Row 1
**/
2007-08-03 20:46:02 +00:00
wxSplitterWindow * hsplit = new wxSplitterWindow( this, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxSP_3DSASH );
#if wxCHECK_VERSION(2,5,4)
2007-07-23 00:56:51 +00:00
hsplit->SetSashGravity( 0.8 );
#endif
wxPanel * panel_1 = new wxPanel( hsplit, wxID_ANY );
wxBoxSizer * buttonSizer = new wxBoxSizer( wxHORIZONTAL );
2007-08-03 16:22:52 +00:00
wxStaticText * text = new wxStaticText( panel_1, wxID_ANY, _("Show:") );
buttonSizer->Add( text, 0, wxALIGN_CENTER|wxLEFT|wxRIGHT, 3 );
int rightButtonSpacing[TorrentFilter::N_FILTERS] = { 0, 0, 10, 0, 10, 0, 0 };
for( int i=0; i<TorrentFilter::N_FILTERS; ++i ) {
wxToggleButton * tb = new wxToggleButton( panel_1, ID_Filter+i, TorrentFilter::GetName(i) );
tb->SetValue( true );
myFilterToggles[i] = tb;
2007-08-03 16:22:52 +00:00
//buttonSizer->Add( tb, 0, wxRIGHT, rightButtonSpacing[i] );
buttonSizer->Add( tb, 1, wxEXPAND|wxRIGHT, rightButtonSpacing[i] );
}
myTorrentList = new TorrentListCtrl( handle, myConfig, panel_1 );
2007-07-24 04:36:36 +00:00
myTorrentList->AddListener( this );
wxBoxSizer * panelSizer = new wxBoxSizer( wxVERTICAL );
2007-08-03 20:46:02 +00:00
panelSizer->Add( new wxStaticLine( panel_1 ), 0, wxEXPAND, 0 );
2007-08-03 18:52:01 +00:00
panelSizer->Add( buttonSizer, 0, 0, 0 );
2007-08-03 16:22:52 +00:00
panelSizer->Add( myTorrentList, 1, wxEXPAND, 0 );
panel_1->SetSizer( panelSizer );
2007-07-23 00:56:51 +00:00
wxNotebook * notebook = new wxNotebook( hsplit, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNB_TOP );
myTorrentStats = new TorrentStats( notebook );
notebook->AddPage( myTorrentStats, _T("General"), false );
wxButton * tmp = new wxButton( notebook, wxID_ANY, _T("Hello &World"));
notebook->AddPage( tmp, _T("Peers") );
tmp = new wxButton( notebook, wxID_ANY, _T("&Hello World"));
notebook->AddPage( tmp, _T("Pieces") );
tmp = new wxButton( notebook, wxID_ANY, _T("Hello World"));
notebook->AddPage( tmp, _T("Files") );
mySpeedStats = new SpeedStats( notebook, wxID_ANY );
notebook->AddPage( mySpeedStats, _T("Speed"), true );
tmp = new wxButton( notebook, wxID_ANY, _T("Hello World"));
notebook->AddPage( tmp, _T("Logger") );
hsplit->SplitHorizontally( panel_1, notebook );
/**
*** Statusbar
**/
const int widths[] = { -1, 150, 150 };
wxStatusBar * statusBar = CreateStatusBar( WXSIZEOF(widths) );
SetStatusWidths( WXSIZEOF(widths), widths );
const int styles[] = { wxSB_FLAT, wxSB_NORMAL, wxSB_NORMAL };
statusBar->SetStatusStyles( WXSIZEOF(widths), styles );
/**
*** Refresh
**/
myPulseTimer.Start( 1500 );
/**
*** Load the torrents
**/
int flags = 0;
if( paused )
flags |= TR_FLAG_PAUSED;
int count = 0;
tr_torrent_t ** torrents = tr_loadTorrents ( handle, mySavePath.c_str(), flags, &count );
2007-07-23 21:01:26 +00:00
myTorrents.insert( myTorrents.end(), torrents, torrents+count );
tr_free( torrents );
wxTimerEvent dummy;
OnPulse( dummy );
}
void MyFrame::OnExit(wxCommandEvent& WXUNUSED(event))
{
Enable( false );
foreach( torrents_v, myTorrents, it )
tr_torrentClose( *it );
myTorrents.clear ();
mySelectedTorrents.clear ();
ApplyCurrentFilter ();
/* give the connections a max of 10 seconds to shut themselves down */
myExitTime = time(0) + 10;
}
void MyFrame::OnAbout(wxCommandEvent& WXUNUSED(event))
{
2007-07-23 17:36:34 +00:00
wxIcon ico( transmission_xpm );
#if wxCHECK_VERSION(2,8,0)
wxAboutDialogInfo info;
info.SetName(_T("Xmission"));
info.SetVersion(_T(LONG_VERSION_STRING));
info.SetCopyright(_T("Copyright 2005-2007 The Transmission Project"));
info.SetDescription(_T("A fast, lightweight bittorrent client"));
info.SetWebSite( _T( "http://transmission.m0k.org/" ) );
info.SetIcon( ico );
2007-07-23 02:43:25 +00:00
info.AddDeveloper( _T("Josh Elsasser (Back-end; GTK+)") );
info.AddDeveloper( _T("Charles Kerr (Back-end, GTK+, wxWidgets)") );
info.AddDeveloper( _T("Mitchell Livingston (Back-end; OS X)") );
info.AddDeveloper( _T("Eric Petit (Back-end; OS X)") );
info.AddDeveloper( _T("Bryan Varner (BeOS)") );
wxAboutBox( info );
#else
wxMessageBox(_T("Xmission " LONG_VERSION_STRING "\n"
"Copyright 2005-2007 The Transmission Project"),
_T("About Xmission"),
wxOK|wxICON_INFORMATION, this);
#endif
}