Changeset 6677 for trunk/gtk/main.c


Ignore:
Timestamp:
Sep 1, 2008, 4:47:02 AM (14 years ago)
Author:
muks
Message:

Add simple scheduled bandwidth control for the GTK client (bug #964)

This is similar to how the Mac OS X version of Transmission
does it.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/gtk/main.c

    r6561 r6677  
    4242#endif
    4343
     44#include <libtransmission/transmission.h>
     45#include <libtransmission/utils.h>
    4446#include <libtransmission/version.h>
    4547
     
    477479}
    478480
     481static gboolean
     482updateScheduledLimits(gpointer data)
     483{
     484    tr_handle *tr = (tr_handle *) data;
     485    static gboolean last_state = FALSE;
     486    gboolean in_sched_state = FALSE;
     487
     488    if( !pref_flag_get( PREF_KEY_SCHED_LIMIT_ENABLED ) )
     489    {
     490        in_sched_state = FALSE;
     491    }
     492    else
     493    {
     494        time_t t;
     495        struct tm *tm;
     496        int begin_hour, begin_minute, end_hour, end_minute;
     497        int begin_time, end_time, cur_time;
     498
     499        begin_hour = pref_int_get( PREF_KEY_SCHED_BEGIN_HOUR );
     500        begin_minute = pref_int_get( PREF_KEY_SCHED_BEGIN_MINUTE );
     501        end_hour = pref_int_get( PREF_KEY_SCHED_END_HOUR );
     502        end_minute = pref_int_get( PREF_KEY_SCHED_END_MINUTE );
     503
     504        time( &t );
     505        tm = localtime (&t);
     506        cur_time = (tm->tm_hour * 60) + tm->tm_min;
     507        begin_time = (begin_hour * 60) + begin_minute;
     508        end_time = (end_hour * 60) + end_minute;
     509
     510        if( (end_time - begin_time) >= 0 )
     511        {
     512            if( (cur_time >= begin_time) && (cur_time <= end_time) )
     513                in_sched_state = TRUE;
     514        }
     515        else
     516        {
     517            if ( (cur_time >= begin_time) || (cur_time <= end_time) )
     518                in_sched_state = TRUE;
     519        }
     520    }
     521
     522    if( last_state != in_sched_state )
     523    {
     524        if( in_sched_state )
     525        {
     526            int limit;
     527
     528            tr_inf ( _( "Enabling scheduled bandwidth limits" ) );
     529
     530            tr_sessionSetSpeedLimitEnabled( tr, TR_DOWN, TRUE );
     531            limit = pref_int_get( PREF_KEY_SCHED_DL_LIMIT );
     532            tr_sessionSetSpeedLimit( tr, TR_DOWN, limit );
     533            tr_sessionSetSpeedLimitEnabled( tr, TR_UP, TRUE );
     534            limit = pref_int_get( PREF_KEY_SCHED_UL_LIMIT );
     535            tr_sessionSetSpeedLimit( tr, TR_UP, limit );
     536        }
     537        else
     538        {
     539            gboolean b;
     540            int limit;
     541
     542            tr_inf ( _( "Disabling scheduled bandwidth limits" ) );
     543
     544            b = pref_flag_get( PREF_KEY_DL_LIMIT_ENABLED );
     545            tr_sessionSetSpeedLimitEnabled( tr, TR_DOWN, b );
     546            limit = pref_int_get( PREF_KEY_DL_LIMIT );
     547            tr_sessionSetSpeedLimit( tr, TR_DOWN, limit );
     548            b = pref_flag_get( PREF_KEY_UL_LIMIT_ENABLED );
     549            tr_sessionSetSpeedLimitEnabled( tr, TR_UP, b );
     550            limit = pref_int_get( PREF_KEY_UL_LIMIT );
     551            tr_sessionSetSpeedLimit( tr, TR_UP, limit );
     552        }
     553
     554        last_state = in_sched_state;
     555    }
     556
     557    return TRUE;
     558}
     559
    479560static void
    480561appsetup( TrWindow * wind, GSList * torrentFiles,
     
    525606    cbdata->timer = g_timeout_add( UPDATE_INTERVAL, updatemodel, cbdata );
    526607    updatemodel( cbdata );
     608
     609    /* start scheduled rate timer */
     610    updateScheduledLimits (tr_core_handle( cbdata->core ));
     611    g_timeout_add_seconds( 60, updateScheduledLimits, tr_core_handle( cbdata->core ) );
    527612
    528613    /* either show the window or iconify it */
Note: See TracChangeset for help on using the changeset viewer.