Changeset 11446


Ignore:
Timestamp:
Dec 1, 2010, 5:41:58 AM (11 years ago)
Author:
charles
Message:

(trunk gtk, qt) #3770 "GTK and Qt clients should sort-by-progress as the Mac client does" -- fixed.

Location:
trunk
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/tr-core.c

    r11401 r11446  
    287287
    288288static int
     289compareUint64( uint64_t a, uint64_t b )
     290{
     291    if( a < b ) return -1;
     292    if( a > b ) return 1;
     293    return 0;
     294}
     295
     296static int
     297compareInt_( int a, int b )
     298{
     299    if( a < b ) return -1;
     300    if( a > b ) return 1;
     301    return 0;
     302}
     303
     304static int
    289305compareRatio( double a, double b )
    290306{
     
    304320
    305321static int
    306 compareByRatio( GtkTreeModel  * model,
    307                 GtkTreeIter   * a,
    308                 GtkTreeIter   * b,
    309                 gpointer        user_data UNUSED )
    310 {
     322compareByName( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data UNUSED )
     323{
     324    int ret = 0;
     325
     326    if( !ret ) {
     327        char *ca, *cb;
     328        gtk_tree_model_get( m, a, MC_NAME_COLLATED, &ca, -1 );
     329        gtk_tree_model_get( m, b, MC_NAME_COLLATED, &cb, -1 );
     330        ret = gtr_strcmp0( ca, cb );
     331        g_free( cb );
     332        g_free( ca );
     333    }
     334
     335    if( !ret ) {
     336        tr_torrent * t;
     337        const tr_info *ia, *ib;
     338        gtk_tree_model_get( m, a, MC_TORRENT_RAW, &t, -1 );
     339        ia = tr_torrentInfo( t );
     340        gtk_tree_model_get( m, b, MC_TORRENT_RAW, &t, -1 );
     341        ib = tr_torrentInfo( t );
     342        ret = memcmp( ia->hash, ib->hash, SHA_DIGEST_LENGTH );
     343    }
     344
     345    return ret;
     346}
     347
     348static int
     349compareByRatio( GtkTreeModel* m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     350{
     351    int ret = 0;
    311352    tr_torrent *ta, *tb;
    312353    const tr_stat *sa, *sb;
    313354
    314     gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 );
    315     gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 );
    316 
     355    gtk_tree_model_get( m, a, MC_TORRENT_RAW, &ta, -1 );
    317356    sa = tr_torrentStatCached( ta );
     357    gtk_tree_model_get( m, b, MC_TORRENT_RAW, &tb, -1 );
    318358    sb = tr_torrentStatCached( tb );
    319359
    320     return compareRatio( sa->ratio, sb->ratio );
     360    if( !ret ) ret = compareRatio( sa->ratio, sb->ratio );
     361    if( !ret ) ret = compareByName( m, a, b, user_data );
     362    return ret;
    321363}
    322364
    323365static int
    324 compareByActivity( GtkTreeModel * model,
    325                    GtkTreeIter  * a,
    326                    GtkTreeIter  * b,
    327                    gpointer       user_data UNUSED )
    328 {
    329     int i;
     366compareByActivity( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     367{
     368    int ret = 0;
    330369    tr_torrent *ta, *tb;
    331370    const tr_stat *sa, *sb;
    332371    double aUp, aDown, bUp, bDown;
    333372
    334     gtk_tree_model_get( model, a, MC_SPEED_UP, &aUp,
    335                                   MC_SPEED_DOWN, &aDown,
    336                                   MC_TORRENT_RAW, &ta,
    337                                   -1 );
    338     gtk_tree_model_get( model, b, MC_SPEED_UP, &bUp,
    339                                   MC_SPEED_DOWN, &bDown,
    340                                   MC_TORRENT_RAW, &tb,
    341                                   -1 );
    342 
    343     if(( i = compareDouble( aUp+aDown, bUp+bDown )))
    344         return i;
    345 
     373    gtk_tree_model_get( m, a, MC_SPEED_UP, &aUp,
     374                              MC_SPEED_DOWN, &aDown,
     375                              MC_TORRENT_RAW, &ta,
     376                              -1 );
     377    gtk_tree_model_get( m, b, MC_SPEED_UP, &bUp,
     378                              MC_SPEED_DOWN, &bDown,
     379                              MC_TORRENT_RAW, &tb,
     380                              -1 );
    346381    sa = tr_torrentStatCached( ta );
    347382    sb = tr_torrentStatCached( tb );
    348     if( sa->uploadedEver != sb->uploadedEver )
    349         return sa->uploadedEver < sa->uploadedEver ? -1 : 1;
    350 
    351     return 0;
     383
     384    if( !ret ) ret = compareDouble( aUp+aDown, bUp+bDown );
     385    if( !ret ) ret = compareUint64( sa->uploadedEver, sb->uploadedEver );
     386    if( !ret ) ret = compareByName( m, a, b, user_data );
     387    return ret;
    352388}
    353389
    354390static int
    355 compareByName( GtkTreeModel *             model,
    356                GtkTreeIter *              a,
    357                GtkTreeIter *              b,
    358                gpointer         user_data UNUSED )
    359 {
    360     int   ret;
    361     char *ca, *cb;
    362 
    363     gtk_tree_model_get( model, a, MC_NAME_COLLATED, &ca, -1 );
    364     gtk_tree_model_get( model, b, MC_NAME_COLLATED, &cb, -1 );
    365     ret = gtr_strcmp0( ca, cb );
    366 
    367     g_free( cb );
    368     g_free( ca );
     391compareByAge( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     392{
     393    int ret = 0;
     394    tr_torrent *ta, *tb;
     395
     396    gtk_tree_model_get( m, a, MC_TORRENT_RAW, &ta, -1 );
     397    gtk_tree_model_get( m, b, MC_TORRENT_RAW, &tb, -1 );
     398
     399    if( !ret ) ret = compareTime( tr_torrentStatCached( ta )->addedDate, tr_torrentStatCached( tb )->addedDate );
     400    if( !ret ) ret = compareByName( m, a, b, user_data );
    369401    return ret;
    370402}
    371403
    372404static int
    373 compareByAge( GtkTreeModel * model,
    374               GtkTreeIter  * a,
    375               GtkTreeIter  * b,
    376               gpointer       user_data UNUSED )
    377 {
    378     tr_torrent *ta, *tb;
    379 
    380     gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 );
    381     gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 );
    382     return compareTime( tr_torrentStatCached( ta )->addedDate,
    383                         tr_torrentStatCached( tb )->addedDate );
    384 }
    385 
    386 static int
    387 compareBySize( GtkTreeModel * model,
    388                GtkTreeIter  * a,
    389                GtkTreeIter  * b,
    390                gpointer       user_data UNUSED )
    391 {
     405compareBySize( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     406{
     407    int ret = 0;
    392408    tr_torrent *t;
    393409    const tr_info *ia, *ib;
    394410
    395     gtk_tree_model_get( model, a, MC_TORRENT_RAW, &t, -1 );
     411    gtk_tree_model_get( m, a, MC_TORRENT_RAW, &t, -1 );
    396412    ia = tr_torrentInfo( t );
    397     gtk_tree_model_get( model, b, MC_TORRENT_RAW, &t, -1 );
     413    gtk_tree_model_get( m, b, MC_TORRENT_RAW, &t, -1 );
    398414    ib = tr_torrentInfo( t );
    399415
    400     if( ia->totalSize < ib->totalSize ) return 1;
    401     if( ia->totalSize > ib->totalSize ) return -1;
    402     return 0;
     416    if( !ret ) ret = compareUint64( ia->totalSize, ib->totalSize );
     417    if( !ret ) ret = compareByName( m, a, b, user_data );
     418    return ret;
    403419}
    404420
    405421static int
    406 compareByProgress( GtkTreeModel *             model,
    407                    GtkTreeIter *              a,
    408                    GtkTreeIter *              b,
    409                    gpointer         user_data UNUSED )
    410 {
    411     int ret;
     422compareByProgress( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     423{
     424    int ret = 0;
    412425    tr_torrent * t;
    413426    const tr_stat *sa, *sb;
    414427
    415     gtk_tree_model_get( model, a, MC_TORRENT_RAW, &t, -1 );
     428    gtk_tree_model_get( m, a, MC_TORRENT_RAW, &t, -1 );
    416429    sa = tr_torrentStatCached( t );
    417     gtk_tree_model_get( model, b, MC_TORRENT_RAW, &t, -1 );
     430    gtk_tree_model_get( m, b, MC_TORRENT_RAW, &t, -1 );
    418431    sb = tr_torrentStatCached( t );
    419     ret = compareDouble( sa->percentDone, sb->percentDone );
    420     if( !ret )
    421         ret = compareRatio( sa->ratio, sb->ratio );
     432
     433    if( !ret ) ret = compareDouble( sa->percentComplete, sb->percentComplete );
     434    if( !ret ) ret = compareDouble( sa->seedRatioPercentDone, sb->seedRatioPercentDone );
     435    if( !ret ) ret = compareByRatio( m, a, b, user_data );
    422436    return ret;
    423437}
    424438
    425439static int
    426 compareByETA( GtkTreeModel * model,
    427               GtkTreeIter  * a,
    428               GtkTreeIter  * b,
    429               gpointer       user_data UNUSED )
    430 {
     440compareByETA( GtkTreeModel * m, GtkTreeIter  * a, GtkTreeIter  * b, gpointer user_data )
     441{
     442    int ret = 0;
    431443    tr_torrent *ta, *tb;
    432444
    433     gtk_tree_model_get( model, a, MC_TORRENT_RAW, &ta, -1 );
    434     gtk_tree_model_get( model, b, MC_TORRENT_RAW, &tb, -1 );
    435 
    436     return compareETA( tr_torrentStatCached( ta )->eta,
    437                        tr_torrentStatCached( tb )->eta );
     445    gtk_tree_model_get( m, a, MC_TORRENT_RAW, &ta, -1 );
     446    gtk_tree_model_get( m, b, MC_TORRENT_RAW, &tb, -1 );
     447
     448    if( !ret ) ret = compareETA( tr_torrentStatCached( ta )->eta, tr_torrentStatCached( tb )->eta );
     449    if( !ret ) ret = compareByName( m, a, b, user_data );
     450    return ret;
    438451}
    439452
    440453static int
    441 compareByState( GtkTreeModel * model,
    442                 GtkTreeIter *  a,
    443                 GtkTreeIter *  b,
    444                 gpointer       user_data )
    445 {
    446     int sa, sb, ret;
    447 
    448     /* first by state */
    449     gtk_tree_model_get( model, a, MC_ACTIVITY, &sa, -1 );
    450     gtk_tree_model_get( model, b, MC_ACTIVITY, &sb, -1 );
    451     ret = sa - sb;
    452 
    453     /* second by progress */
    454     if( !ret )
    455         ret = compareByProgress( model, a, b, user_data );
    456 
     454compareByState( GtkTreeModel * m, GtkTreeIter * a, GtkTreeIter * b, gpointer user_data )
     455{
     456    int ret = 0;
     457    int sa, sb;
     458
     459    gtk_tree_model_get( m, a, MC_ACTIVITY, &sa, -1 );
     460    gtk_tree_model_get( m, b, MC_ACTIVITY, &sb, -1 );
     461
     462    if( !ret ) ret = compareInt_( sa, sb );
     463    if( !ret ) ret = compareByProgress( m, a, b, user_data );
    457464    return ret;
    458465}
  • trunk/qt/torrent-filter.cc

    r11240 r11446  
    8080TorrentFilter :: lessThan( const QModelIndex& left, const QModelIndex& right ) const
    8181{
     82    int val = 0;
    8283    const Torrent * a = sourceModel()->data( left, TorrentModel::TorrentRole ).value<const Torrent*>();
    8384    const Torrent * b = sourceModel()->data( right, TorrentModel::TorrentRole ).value<const Torrent*>();
    84     int less = 0;
    8585
    8686    switch( myPrefs.get<SortMode>(Prefs::SORT_MODE).mode() )
    8787    {
    8888        case SortMode :: SORT_BY_SIZE:
    89             less = compare( a->sizeWhenDone(), b->sizeWhenDone() );
     89            if( !val ) val = compare( a->sizeWhenDone(), b->sizeWhenDone() );
    9090            break;
    9191        case SortMode :: SORT_BY_ACTIVITY:
    92             less = compare( a->downloadSpeed() + a->uploadSpeed(), b->downloadSpeed() + b->uploadSpeed() );
    93             if( !less )
    94                 less = compare( a->uploadedEver(), b->uploadedEver() );
     92            if( !val ) val = compare( a->downloadSpeed() + a->uploadSpeed(), b->downloadSpeed() + b->uploadSpeed() );
     93            if( !val ) val = compare( a->uploadedEver(), b->uploadedEver() );
    9594            break;
    9695        case SortMode :: SORT_BY_AGE:
    97             less = compare( a->dateAdded().toTime_t(), b->dateAdded().toTime_t() );
     96            val = compare( a->dateAdded().toTime_t(), b->dateAdded().toTime_t() );
    9897            break;
    9998        case SortMode :: SORT_BY_ID:
    100             less = compare( a->id(), b->id() );
     99            if( !val ) val = compare( a->id(), b->id() );
    101100            break;
    102101        case SortMode :: SORT_BY_STATE:
    103             if( a->hasError() != b->hasError() )
    104                 less = a->hasError();
    105             else
    106                 less = compare( a->getActivity(), b->getActivity() );
    107             if( less )
    108                 break;
     102            if( !val ) val = compare( a->hasError(), b->hasError() );
     103            if( !val ) val = compare( a->getActivity(), b->getActivity() );
     104            // fall through
    109105        case SortMode :: SORT_BY_PROGRESS:
    110             less = compare( a->percentDone(), b->percentDone() );
    111             if( less )
    112                 break;
     106            if( !val ) val = compare( a->percentComplete(), b->percentComplete() );
     107            if( !val ) val = a->compareSeedRatio( *b );
     108            // fall through
    113109        case SortMode :: SORT_BY_RATIO:
    114             less = a->compareRatio( *b );
     110            if( !val ) val = a->compareRatio( *b );
    115111            break;
    116112        case SortMode :: SORT_BY_ETA:
    117             less = a->compareETA( *b );
     113            if( !val ) val = a->compareETA( *b );
    118114            break;
    119115        default:
    120116            break;
    121117    }
    122     if( less == 0 )
    123         less = -a->name().compare( b->name(), Qt::CaseInsensitive );
    124     if( less == 0 )
    125         less = compare( a->hashString(), b->hashString() );
    126     return less < 0;
     118    if( val == 0 )
     119        val = -a->name().compare( b->name(), Qt::CaseInsensitive );
     120    if( val == 0 )
     121        val = compare( a->hashString(), b->hashString() );
     122    return val < 0;
    127123}
    128124
  • trunk/qt/torrent.cc

    r11240 r11446  
    367367            return true;
    368368    return false;
     369}
     370
     371int
     372Torrent :: compareSeedRatio( const Torrent& that ) const
     373{
     374    double a;
     375    double b;
     376    const bool has_a = getSeedRatio( a );
     377    const bool has_b = that.getSeedRatio( b );
     378    if( !has_a && !has_b ) return 0;
     379    if( !has_a || !has_b ) return has_a ? -1 : 1;
     380    if( a < b ) return -1;
     381    if( a > b ) return 1;
     382    return 0;
    369383}
    370384
  • trunk/qt/torrent.h

    r11194 r11446  
    269269        uint64_t failedEver( ) const { return getSize( FAILED_EVER ); }
    270270        int compareTracker( const Torrent& ) const;
     271        int compareSeedRatio( const Torrent& ) const;
    271272        int compareRatio( const Torrent& ) const;
    272273        int compareETA( const Torrent& ) const;
Note: See TracChangeset for help on using the changeset viewer.