From ea6353db9b937b2f82949499b05cdf28d1b50e06 Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Thu, 19 Jul 2007 20:35:01 +0000 Subject: [PATCH] make the torrent list columns configurable. --- wx/xmission.cc | 163 ++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 147 insertions(+), 16 deletions(-) diff --git a/wx/xmission.cc b/wx/xmission.cc index 62c255a2c..ece055e1b 100755 --- a/wx/xmission.cc +++ b/wx/xmission.cc @@ -1,3 +1,17 @@ +/* + * This file Copyright (C) 2007 Charles Kerr + * + * 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 +#include +#include +#include #include #include #include @@ -31,6 +45,10 @@ public: protected: wxConfig * myConfig; + +private: + void rebuildTorrentList(); + wxListCtrl * myTorrentList; }; enum @@ -95,6 +113,125 @@ bool MyApp::OnInit() **** ***/ + +namespace +{ + enum + { + COL_NUMBER, + COL_DONE, + COL_DOWNLOAD_SPEED, + COL_ETA, + COL_HASH, + COL_NAME, + COL_PEERS, + COL_RATIO, + COL_RECEIVED, + COL_REMAINING, + COL_SEEDS, + COL_SEND, + COL_SIZE, + COL_STATE, + COL_STATUS, + COL_TOTAL, + COL_UPLOAD_SPEED, + N_COLS + }; + + int getTorrentColumn( const wxString& wxKey ) + { + const std::string key ( wxKey.c_str() ); + typedef std::map string2key_t; + static string2key_t columns; + + if( columns.empty() ) + { + columns["#"] = COL_NUMBER; + columns["done"] = COL_DONE; + columns["dspeed"] = COL_DOWNLOAD_SPEED; + columns["eta"] = COL_ETA; + columns["hash"] = COL_HASH; + columns["name"] = COL_NAME; + columns["peers"] = COL_PEERS; + columns["ratio"] = COL_RATIO; + columns["received"] = COL_RECEIVED; + columns["remaining"] = COL_REMAINING; + columns["seeds"] = COL_SEEDS; + columns["sent"] = COL_SEND; + columns["size"] = COL_SIZE; + columns["state"] = COL_STATE; + columns["status"] = COL_STATUS; + columns["total"] = COL_TOTAL; + columns["uspeed"] = COL_UPLOAD_SPEED; + } + + int i = -1; + string2key_t::const_iterator it = columns.find( key ); + if( it != columns.end() ) + i = it->second; + + return i; + } + + std::vector getTorrentColumns( wxConfig * config ) + { + const wxString key = _T("TorrentListColumns"); + wxString columnStr; + if( !config->Read( key, &columnStr ) ) + columnStr = _T("name|#|size|done|status|seeds|peers|eta|uspeed|dspeed"); + + std::vector cols; + while( !columnStr.IsEmpty() ) + { + const wxString key = columnStr.BeforeFirst(_T('|')); + columnStr.Remove( 0, key.Len() + 1 ); + cols.push_back( getTorrentColumn( key ) ); + } + return cols; + } +} + +void +MyFrame :: rebuildTorrentList() +{ + myTorrentList->ClearAll( ); + + int i = 0; + const std::vector cols = getTorrentColumns( myConfig ); + for( std::vector::const_iterator it(cols.begin()), end(cols.end()); it!=end; ++it ) + { + wxString h; + + switch( *it ) + { + case COL_NUMBER: h = _T("#"); break; + case COL_DONE: h = _T("Done"); break; + case COL_DOWNLOAD_SPEED: h = _T("Download"); break; + case COL_ETA: h = _T("ETA"); break; + case COL_HASH: h = _T("SHA1 Hash"); break; + case COL_NAME: h = _T("Name"); break; + case COL_PEERS: h = _T("Peers"); break; + case COL_RATIO: h = _T("Ratio"); break; + case COL_RECEIVED: h = _T("Received"); break; + case COL_REMAINING: h = _T("Remaining"); break; + case COL_SEEDS: h = _T("Seeds"); break; + case COL_SEND: h = _T("Send"); break; + case COL_SIZE: h = _T("Size"); break; + case COL_STATE: h = _T("State"); break; + case COL_STATUS: h = _T("Status"); break; + case COL_TOTAL: h = _T("Total"); break; + case COL_UPLOAD_SPEED: h = _T("Upload"); break; + default: h = _T("Error"); break; + } + + myTorrentList->InsertColumn( i++, h ); + } +} + +/*** +**** +***/ + MyFrame::~MyFrame() { delete myConfig; @@ -193,24 +330,18 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size): *** Torrent List **/ - wxListCtrl * torrents = new wxListCtrl( row1, wxID_ANY, wxDefaultPosition, wxDefaultSize, - wxLC_REPORT|wxLC_SINGLE_SEL ); - torrents->InsertColumn( 0, _T("Name") ); - torrents->InsertColumn( 1, _T("#") ); - torrents->InsertColumn( 2, _T("Size") ); - torrents->InsertColumn( 3, _T("Done") ); - torrents->InsertColumn( 4, _T("Status") ); - torrents->InsertColumn( 5, _T("Seeds") ); - torrents->InsertColumn( 6, _T("Peers") ); - row_sizer->Add( torrents, wxSizerFlags().Expand() ); + myTorrentList = new wxListCtrl( row1, wxID_ANY, wxDefaultPosition, wxDefaultSize, + wxLC_REPORT|wxLC_SINGLE_SEL ); + rebuildTorrentList(); + row_sizer->Add( myTorrentList, wxSizerFlags().Expand() ); row_sizer->AddGrowableCol( 1, 1 ); - i = torrents->InsertItem( 0, _T("Fedora.iso") ); - torrents->SetItem( i, 1, _T("*")); - torrents->SetItem( i, 2, _T("4.4 GiB")); - torrents->SetItem( i, 3, _T("50%")); - torrents->SetItem( i, 4, _T("0 (77)")); - torrents->SetItem( i, 5, _T("1 (128)")); + i = myTorrentList->InsertItem( 0, _T("Fedora.iso") ); + myTorrentList->SetItem( i, 1, _T("*")); + myTorrentList->SetItem( i, 2, _T("4.4 GiB")); + myTorrentList->SetItem( i, 3, _T("50%")); + myTorrentList->SetItem( i, 4, _T("0 (77)")); + myTorrentList->SetItem( i, 5, _T("1 (128)")); wxNotebook * notebook = new wxNotebook( hsplit, wxID_ANY, wxDefaultPosition, wxDefaultSize, wxNB_TOP );