]> granicus.if.org Git - transmission/commitdiff
#4201 Automatic limit can't be stopped
authorMitchell Livingston <livings124@transmissionbt.com>
Tue, 18 Sep 2012 00:58:10 +0000 (00:58 +0000)
committerMitchell Livingston <livings124@transmissionbt.com>
Tue, 18 Sep 2012 00:58:10 +0000 (00:58 +0000)
libtransmission/session.c
libtransmission/session.h

index 04cc1fe576ba4306ebcfe46217d80b8b3a99c4d1..9a010616ee5aca29a8ad16a0c6e5529b5c2190d6 100644 (file)
@@ -1325,9 +1325,7 @@ useAltSpeed( tr_session * s, struct tr_turtle_info * t,
  * @param changed whether that's different from the previous minute
  */
 static void
-testTurtleTime( const struct tr_turtle_info * t,
-                bool * enabled,
-                bool * changed )
+testTurtleTime( const struct tr_turtle_info * t, bool * enabled )
 {
     bool e;
     struct tm tm;
@@ -1345,28 +1343,31 @@ testTurtleTime( const struct tr_turtle_info * t,
     e = tr_bitfieldHas( &t->minutes, minute_of_the_week );
     if( enabled != NULL )
         *enabled = e;
+}
 
-    if( changed != NULL )
-    {
-        const size_t prev = minute_of_the_week > 0 ? minute_of_the_week - 1
-                                                   : MINUTES_PER_WEEK - 1;
-        *changed = e != tr_bitfieldHas( &t->minutes, prev );
-    }
+static inline tr_auto_switch_state_t
+autoSwitchState( bool enabled )
+{
+    return enabled ? TR_AUTO_SWITCH_ON : TR_AUTO_SWITCH_OFF;
 }
 
 static void
 turtleCheckClock( tr_session * s, struct tr_turtle_info * t )
 {
     bool enabled;
-    bool changed;
+    bool alreadySwitched;
+    tr_auto_switch_state_t newAutoTurtleState;
 
     assert( t->isClockEnabled );
 
-    testTurtleTime( t, &enabled, &changed );
+    testTurtleTime( t, &enabled );
+    newAutoTurtleState = autoSwitchState( enabled );
+    alreadySwitched = ( t->autoTurtleState == newAutoTurtleState );
 
-    if( changed )
+    if( !alreadySwitched )
     {
         tr_inf( "Time to turn %s turtle mode!", (enabled?"on":"off") );
+        t->autoTurtleState = newAutoTurtleState;
         useAltSpeed( s, t, enabled, false );
     }
 }
@@ -1378,15 +1379,20 @@ static void
 turtleBootstrap( tr_session * session, struct tr_turtle_info * turtle )
 {
     turtle->changedByUser = false;
+    turtle->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
 
     tr_bitfieldConstruct( &turtle->minutes, MINUTES_PER_WEEK );
 
     turtleUpdateTable( turtle );
 
     if( turtle->isClockEnabled )
-        testTurtleTime( turtle, &turtle->isEnabled, NULL );
+    {
+        testTurtleTime( turtle, &turtle->isEnabled );
+        turtle->autoTurtleState = autoSwitchState( turtle->isEnabled );
+    }
 
     altSpeedToggled( session );
+
 }
 
 /***
@@ -1484,13 +1490,16 @@ userPokedTheClock( tr_session * s, struct tr_turtle_info * t )
 {
     tr_dbg( "Refreshing the turtle mode clock due to user changes" );
 
+    t->autoTurtleState = TR_AUTO_SWITCH_UNUSED;
+
     turtleUpdateTable( t );
 
     if( t->isClockEnabled )
     {
-        bool enabled, changed;
-        testTurtleTime( t, &enabled, &changed );
+        bool enabled;
+        testTurtleTime( t, &enabled );
         useAltSpeed( s, t, enabled, true );
+        t->autoTurtleState = autoSwitchState( enabled );
     }
 }
 
index c112eaa3e695b517356dbc6b42fb89ab5517508a..022d16f80cff275ae8408bb70d76bb83254a1b4c 100644 (file)
 
 typedef enum { TR_NET_OK, TR_NET_ERROR, TR_NET_WAIT } tr_tristate_t;
 
+typedef enum {
+    TR_AUTO_SWITCH_UNUSED,
+    TR_AUTO_SWITCH_ON,
+    TR_AUTO_SWITCH_OFF,
+} tr_auto_switch_state_t;
+
 enum
 {
   PEER_ID_LEN = 20
@@ -87,6 +93,9 @@ struct tr_turtle_info
      * Each bit's value indicates whether the scheduler wants turtle
      * limits on or off at that given minute in the week. */
     tr_bitfield minutes;
+
+    /* recent action that was done by turtle's automatic switch */
+    tr_auto_switch_state_t autoTurtleState;
 };
 
 /** @brief handle to an active libtransmission session */