torrent filters work now... we finally have a working feature! ;)

This commit is contained in:
Charles Kerr 2007-07-23 23:59:13 +00:00
parent d07439f793
commit 243772f194
3 changed files with 84 additions and 16 deletions

View File

@ -301,8 +301,8 @@ TorrentListCtrl :: RefreshTorrent( tr_torrent_t * tor,
// if the torrent's in the list already, update that row. // if the torrent's in the list already, update that row.
// otherwise, add a new row. // otherwise, add a new row.
if( row < 0 ) { if( row < 0 ) {
str2int_t::const_iterator it = myHashToRow.find( info->hashString ); str2int_t::const_iterator it = myHashToItem.find( info->hashString );
if( it != myHashToRow.end() ) { if( it != myHashToItem.end() ) {
row = it->second; row = it->second;
} }
} }
@ -312,7 +312,7 @@ TorrentListCtrl :: RefreshTorrent( tr_torrent_t * tor,
else { else {
row = InsertItem( GetItemCount(), xstr ); row = InsertItem( GetItemCount(), xstr );
col = 1; col = 1;
myHashToRow[info->hashString] = row; myHashToItem[info->hashString] = row;
SetItemData( row, myTorrents_index ); SetItemData( row, myTorrents_index );
} }
} }
@ -479,7 +479,7 @@ TorrentListCtrl :: Resort( )
const tr_info_t* info = tr_torrentInfo( myTorrents[idx] ); const tr_info_t* info = tr_torrentInfo( myTorrents[idx] );
tmp[info->hashString] = i; tmp[info->hashString] = i;
} }
myHashToRow.swap( tmp ); myHashToItem.swap( tmp );
uglyHack = NULL; uglyHack = NULL;
} }
@ -504,7 +504,7 @@ void
TorrentListCtrl :: Repopulate () TorrentListCtrl :: Repopulate ()
{ {
DeleteAllItems(); DeleteAllItems();
myHashToRow.clear (); myHashToItem.clear ();
const int_v cols = getTorrentColumns( myConfig ); const int_v cols = getTorrentColumns( myConfig );
int i = 0; int i = 0;
@ -519,7 +519,7 @@ void
TorrentListCtrl :: Rebuild() TorrentListCtrl :: Rebuild()
{ {
ClearAll( ); ClearAll( );
myHashToRow.clear (); myHashToItem.clear ();
int i = 0; int i = 0;
const int_v cols = getTorrentColumns( myConfig ); const int_v cols = getTorrentColumns( myConfig );
@ -557,11 +557,56 @@ TorrentListCtrl :: Rebuild()
Repopulate (); Repopulate ();
} }
typedef std::set<tr_torrent_t*> torrent_set;
void void
TorrentListCtrl :: Add( const torrents_t& add ) TorrentListCtrl :: Assign( const torrents_t& torrents )
{ {
myTorrents.insert( myTorrents.end(), add.begin(), add.end() ); torrent_set prev, cur, removed;
Repopulate (); torrents_v added;
prev.insert( myTorrents.begin(), myTorrents.end() );
cur.insert( torrents.begin(), torrents.end() );
std::set_difference (prev.begin(), prev.end(), cur.begin(), cur.end(), inserter(removed, removed.begin()));
std::set_difference (cur.begin(), cur.end(), prev.begin(), prev.end(), inserter(added, added.begin()));
Remove( removed );
Add( added );
Refresh ();
} }
void
TorrentListCtrl :: Add( const torrents_v& add )
{
const int_v cols = getTorrentColumns( myConfig );
int i = myTorrents.size();
myTorrents.insert( myTorrents.end(), add.begin(), add.end() );
for( torrents_v::const_iterator it(add.begin()), end(add.end()); it!=end; ++it )
RefreshTorrent( *it, i++, cols );
Resort( );
}
void
TorrentListCtrl :: Remove( const torrent_set& remove )
{
torrents_v vtmp;
str2int_t htmp;
for( int item=0; item<GetItemCount(); )
{
tr_torrent_t * tor = myTorrents[GetItemData(item)];
const tr_info_t* info = tr_torrentInfo( tor );
if( remove.count( tor ) )
{
DeleteItem( item );
continue;
}
vtmp.push_back( tor );
SetItemData( item, vtmp.size()-1 );
htmp[ info->hashString ] = item;
++item;
}
myHashToItem.swap( htmp );
myTorrents.swap( vtmp );
}

View File

@ -76,18 +76,20 @@ class TorrentListCtrl: public wxListCtrl
void Refresh (); void Refresh ();
public: public:
typedef std::vector<tr_torrent_t*> torrents_t; typedef std::vector<tr_torrent_t*> torrents_v;
void Add( const torrents_t& add ); void Add( const torrents_v& torrents );
void Assign( const torrents_v& torrents );
private: private:
void Sort( int column ); void Sort( int column );
void Resort( ); void Resort( );
void RefreshTorrent( tr_torrent_t*, int, const std::vector<int>& ); void RefreshTorrent( tr_torrent_t*, int, const std::vector<int>& );
void Remove( const std::set<tr_torrent_t*>& );
static int Compare( long, long, long ); static int Compare( long, long, long );
/** torrent hash -> the torrent's row in myTorrentList */ /** torrent hash -> the torrent's row in myTorrentList */
typedef std::map<std::string,int> str2int_t; typedef std::map<std::string,int> str2int_t;
str2int_t myHashToRow; str2int_t myHashToItem;
private: private:
void OnSort( wxListEvent& ); void OnSort( wxListEvent& );
@ -97,7 +99,7 @@ class TorrentListCtrl: public wxListCtrl
private: private:
tr_handle_t * myHandle; tr_handle_t * myHandle;
wxConfig * myConfig; wxConfig * myConfig;
torrents_t myTorrents; torrents_v myTorrents;
int prevSortCol; int prevSortCol;
private: private:

View File

@ -51,8 +51,6 @@ class MyApp : public wxApp
virtual bool OnInit(); virtual bool OnInit();
}; };
IMPLEMENT_APP(MyApp)
namespace namespace
{ {
tr_handle_t * handle = NULL; tr_handle_t * handle = NULL;
@ -71,6 +69,7 @@ public:
void OnAbout( wxCommandEvent& ); void OnAbout( wxCommandEvent& );
void OnOpen( wxCommandEvent& ); void OnOpen( wxCommandEvent& );
void OnTimer( wxTimerEvent& ); void OnTimer( wxTimerEvent& );
void OnItemSelected( wxListEvent& );
private: private:
void RefreshFilterCounts( ); void RefreshFilterCounts( );
@ -86,6 +85,9 @@ private:
wxIcon * myLogoIcon; wxIcon * myLogoIcon;
wxIcon * myTrayLogo; wxIcon * myTrayLogo;
torrents_v myTorrents; torrents_v myTorrents;
private:
DECLARE_EVENT_TABLE()
}; };
enum enum
@ -99,9 +101,16 @@ enum
ID_SHOW_DEBUG_WINDOW, ID_SHOW_DEBUG_WINDOW,
ID_ABOUT, ID_ABOUT,
ID_Pulse, ID_Pulse,
ID_Filter,
N_IDS N_IDS
}; };
BEGIN_EVENT_TABLE(MyFrame, wxFrame)
EVT_LIST_ITEM_SELECTED( ID_Filter, MyFrame::OnItemSelected )
END_EVENT_TABLE()
IMPLEMENT_APP(MyApp)
void MyFrame :: OnOpen( wxCommandEvent& event ) void MyFrame :: OnOpen( wxCommandEvent& event )
{ {
const wxString key = _T("prev-directory"); const wxString key = _T("prev-directory");
@ -201,6 +210,15 @@ MyFrame :: RefreshFilterCounts( )
} }
} }
void
MyFrame :: OnItemSelected( wxListEvent& event )
{
const int item = event.GetIndex ();
torrents_v tmp( myTorrents );
TorrentFilter :: RemoveFailures( item, tmp );
myTorrentList->Assign( tmp );
}
void void
MyFrame :: OnTimer(wxTimerEvent& event) MyFrame :: OnTimer(wxTimerEvent& event)
{ {
@ -310,7 +328,7 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size):
/* Filters */ /* Filters */
myFilters = new wxListCtrl( row1, wxID_ANY, wxDefaultPosition, wxSize(120,-1), myFilters = new wxListCtrl( row1, ID_Filter, wxDefaultPosition, wxSize(120,-1),
wxLC_REPORT|wxLC_SINGLE_SEL|wxLC_NO_HEADER ); wxLC_REPORT|wxLC_SINGLE_SEL|wxLC_NO_HEADER );
myFilters->InsertColumn( wxLIST_FORMAT_LEFT, _("Filters"), wxLIST_FORMAT_LEFT, 120 ); myFilters->InsertColumn( wxLIST_FORMAT_LEFT, _("Filters"), wxLIST_FORMAT_LEFT, 120 );
for( int i=0; i<TorrentFilter::N_FILTERS; ++i ) for( int i=0; i<TorrentFilter::N_FILTERS; ++i )
@ -367,6 +385,9 @@ MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size):
myTorrents.insert( myTorrents.end(), torrents, torrents+count ); myTorrents.insert( myTorrents.end(), torrents, torrents+count );
myTorrentList->Add( myTorrents ); myTorrentList->Add( myTorrents );
tr_free( torrents ); tr_free( torrents );
wxTimerEvent dummy;
OnTimer( dummy );
} }
void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event)) void MyFrame::OnQuit(wxCommandEvent& WXUNUSED(event))