Changeset 2459


Ignore:
Timestamp:
Jul 22, 2007, 7:37:43 PM (16 years ago)
Author:
charles
Message:
  • add code to refresh the torrent list, and a timer to call it.
  • beautify some of the torrent list's columns
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wx/xmission.cc

    r2456 r2459  
    4747    void OnAbout(wxCommandEvent& event);
    4848    void OnOpen(wxCommandEvent& event);
     49    void OnTimer(wxTimerEvent& event);
    4950
    5051protected:
    5152    wxConfig * myConfig;
     53    wxTimer myPulseTimer;
    5254
    5355private:
    5456    void rebuildTorrentList();
    5557    void repopulateTorrentList ();
     58    void refreshTorrentList ();
    5659    wxListCtrl * myTorrentList;
    5760    typedef std::vector<tr_torrent_t*> torrents_t;
    5861    torrents_t myTorrents;
    59     void insertTorrent( tr_torrent_t*, const std::vector<int>& );
     62    void refreshTorrent( tr_torrent_t*, int, const std::vector<int>& );
     63
     64    typedef std::map<std::string,int> str2int_t;
     65
     66    /** torrent hash -> the torrent's row in myTorrentList */
     67    str2int_t myHashToRow;
    6068};
    6169
     
    7078    ID_SHOW_DEBUG_WINDOW,
    7179    ID_ABOUT,
     80    ID_Pulse,
    7281    N_IDS
    7382};
     
    112121    frame->Connect( wxID_EXIT, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction) &MyFrame::OnQuit );
    113122    frame->Connect( wxID_ABOUT, wxEVT_COMMAND_MENU_SELECTED, (wxObjectEventFunction) &MyFrame::OnAbout );
     123    frame->Connect( ID_Pulse, wxEVT_TIMER, (wxObjectEventFunction) &MyFrame::OnTimer );
    114124
    115125    frame->Show( true );
     
    188198        wxString columnStr;
    189199        if( !config->Read( key, &columnStr ) )
    190             columnStr = _T("name|#|size|done|status|seeds|peers|eta|uspeed|dspeed");
     200            columnStr = _T("name|dspeed|uspeed|eta|peers|size|done|status|seeds");
    191201
    192202        int_v cols;
     
    246256/***
    247257****
     258****  PEERS LIST
     259****
     260****
    248261***/
    249262
     263
     264/***
     265****
     266****  TORRENT LIST
     267****
     268****
     269***/
     270
    250271void
    251 MyFrame :: insertTorrent( tr_torrent_t * tor,
    252                           const int_v        & cols )
     272MyFrame :: refreshTorrent( tr_torrent_t  * tor,
     273                           int             myTorrents_index,
     274                           const int_v   & cols )
    253275{
    254276    int row = -1;
     
    259281    const tr_info_t* info = tr_torrentInfo( tor );
    260282
    261 
    262283    for( int_v::const_iterator it(cols.begin()), end(cols.end()); it!=end; ++it )
    263284    {
     
    272293
    273294            case COL_DONE:
    274                 snprintf( buf, sizeof(buf), "%%%.1f", s->percentDone );
     295                snprintf( buf, sizeof(buf), "%d%%", (int)(s->percentDone*100.0) );
    275296                xstr = toWxStr( buf );
    276297                break;
     
    281302
    282303            case COL_ETA:
    283                 xstr = toWxStr( getReadableTime( s->eta ) );
     304                if( (int)(s->percentDone*100) >= 100 )
     305                    xstr = wxString ();
     306                else if( s->eta < 0 )
     307                    xstr = toWxStr( "\xE2\x88\x9E" ); /* infinity, in utf-8 */
     308                else
     309                    xstr = toWxStr( getReadableTime( s->eta ) );
    284310                break;
    285311               
     
    344370        }
    345371
    346         if( row < 0 )
    347             row = myTorrentList->InsertItem( myTorrentList->GetItemCount(), xstr );
    348         else
    349             myTorrentList->SetItem( row, ++col, xstr );
    350     }
    351 }
    352 
     372        if( col ) {
     373            myTorrentList->SetItem( row, col++, xstr );
     374        }
     375        else {
     376            // first column... find the right row to put the info in.
     377            // if the torrent's in the list already, update that row.
     378            // otherwise, add a new row.
     379            if( row < 0 ) {
     380                str2int_t::const_iterator it = myHashToRow.find( info->hashString );
     381                if( it != myHashToRow.end() ) {
     382                    row = it->second;
     383                }
     384            }
     385            if( row >= 0 ) {
     386                myTorrentList->SetItem( row, col++, xstr );
     387            }
     388            else {
     389                row = myTorrentList->InsertItem( myTorrentList->GetItemCount(), xstr );
     390                col = 1;
     391                myHashToRow[info->hashString] = row;
     392                myTorrentList->SetItemData( row, myTorrents_index );
     393            }
     394        }
     395    }
     396}
     397
     398void
     399MyFrame :: refreshTorrentList ()
     400{
     401    const int_v  cols = getTorrentColumns( myConfig );
     402    const int rowCount = myTorrentList->GetItemCount();
     403    for( int row=0; row<rowCount; ++row )
     404    {
     405        int array_index = myTorrentList->GetItemData( row );
     406        tr_torrent_t * tor = myTorrents[array_index];
     407        refreshTorrent( tor, array_index, cols );
     408    }
     409}
    353410
    354411void
    355412MyFrame :: repopulateTorrentList ()
    356413{
     414std::cerr << __FILE__ << ':' << __LINE__ << " clearing all items from list" << std::endl;
    357415    myTorrentList->DeleteAllItems();
     416    myHashToRow.clear ();
    358417
    359418    const int_v cols = getTorrentColumns( myConfig );
     419    int i = 0;
    360420    for( torrents_t::const_iterator it(myTorrents.begin()),
    361421                                   end(myTorrents.end()); it!=end; ++it )
    362         insertTorrent( *it, cols );
     422        refreshTorrent( *it, i++, cols );
    363423}
    364424
     
    367427{
    368428    myTorrentList->ClearAll( );
     429    myHashToRow.clear ();
    369430
    370431    int i = 0;
     
    372433    for( int_v ::const_iterator it(cols.begin()), end(cols.end()); it!=end; ++it )
    373434    {
     435        int format = wxLIST_FORMAT_LEFT;
     436        int width = -1;
    374437        wxString h;
    375438
    376439        switch( *it )
    377440        {
    378             case COL_NUMBER:          h = _T("#"); break;
    379             case COL_DONE:            h = _T("Done"); break;
     441            case COL_NUMBER:          h = _T("#"); format = wxLIST_FORMAT_CENTRE; break;
     442            case COL_DONE:            h = _T("Done"); format = wxLIST_FORMAT_RIGHT; break;
    380443            case COL_DOWNLOAD_SPEED:  h = _T("Download"); break;
    381             case COL_ETA:             h = _T("ETA"); break;
     444            case COL_ETA:             h = _T("ETA"); format = wxLIST_FORMAT_RIGHT; break;
    382445            case COL_HASH:            h = _T("SHA1 Hash"); break;
    383             case COL_NAME:            h = _T("Name"); break;
    384             case COL_PEERS:           h = _T("Peers"); break;
    385             case COL_RATIO:           h = _T("Ratio"); break;
    386             case COL_RECEIVED:        h = _T("Received"); break;
    387             case COL_REMAINING:       h = _T("Remaining"); break;
    388             case COL_SEEDS:           h = _T("Seeds"); break;
    389             case COL_SENT:            h = _T("Sent"); break;
    390             case COL_SIZE:            h = _T("Size"); break;
     446            case COL_NAME:            h = _T("Name"); width = 500; break;
     447            case COL_PEERS:           h = _T("Peers"); format = wxLIST_FORMAT_RIGHT; break;
     448            case COL_RATIO:           h = _T("Ratio"); format = wxLIST_FORMAT_RIGHT; break;
     449            case COL_RECEIVED:        h = _T("Received"); format = wxLIST_FORMAT_RIGHT; break;
     450            case COL_REMAINING:       h = _T("Remaining"); format = wxLIST_FORMAT_RIGHT; break;
     451            case COL_SEEDS:           h = _T("Seeds"); format = wxLIST_FORMAT_RIGHT; break;
     452            case COL_SENT:            h = _T("Sent"); format = wxLIST_FORMAT_RIGHT; break;
     453            case COL_SIZE:            h = _T("Size");  format = wxLIST_FORMAT_RIGHT; break;
    391454            case COL_STATE:           h = _T("State"); break;
    392455            case COL_STATUS:          h = _T("Status"); break;
    393456            case COL_TOTAL:           h = _T("Total"); break;
    394             case COL_UPLOAD_SPEED:    h = _T("Upload"); break;
     457            case COL_UPLOAD_SPEED:    h = _T("Upload"); format = wxLIST_FORMAT_RIGHT;break;
    395458            default:                  h = _T("Error"); break;
    396459        }
    397460
    398         myTorrentList->InsertColumn( i++, h );
     461        myTorrentList->InsertColumn( i++, h, format, width );
    399462    }
    400463
     
    405468****
    406469***/
     470
     471void
     472MyFrame :: OnTimer(wxTimerEvent& event)
     473{
     474    refreshTorrentList ();
     475}
    407476
    408477MyFrame::~MyFrame()
     
    413482MyFrame::MyFrame(const wxString& title, const wxPoint& pos, const wxSize& size):
    414483    wxFrame((wxFrame*)NULL,-1,title,pos,size),
    415     myConfig( new wxConfig( _T("xmission") ) )
     484    myConfig( new wxConfig( _T("xmission") ) ),
     485    myPulseTimer( this, ID_Pulse )
    416486{
    417487    wxImage::AddHandler( new wxPNGHandler );
     
    426496
    427497    const int flags = TR_FLAG_PAUSED;
    428     const char * destination = "/tmp/asdf";
     498    const char * destination = "/home/charles/torrents";
    429499    int count = 0;
    430500    tr_torrent_t ** torrents = tr_loadTorrents ( handle, destination, flags, &count );
     
    521591    row_sizer->Add( myTorrentList, wxSizerFlags().Expand() );
    522592    row_sizer->AddGrowableCol( 1, 1 );
    523     repopulateTorrentList ();
    524593
    525594
     
    545614    CreateStatusBar();
    546615    SetStatusText(_T("Welcome to Xmission!"));
     616
     617    /**
     618    ***  Refresh
     619    **/
     620
     621    myPulseTimer.Start( 1500 );
    547622}
    548623
Note: See TracChangeset for help on using the changeset viewer.