Ticket #97: scheduler_additional_prefs.patch

File scheduler_additional_prefs.patch, 12.3 KB (added by ben@…, 16 years ago)

patchfile

  • plugins/Scheduler/plugin.py

    diff --git a/plugins/Scheduler/plugin.py b/plugins/Scheduler/plugin.py
    index 869fe0f..c43bd28 100644
    a b class plugin_Scheduler: 
    1313        self.config = deluge.pref.Preferences() 
    1414        self.button_state_temp = [[0] * 7 for dummy in xrange(24)] 
    1515        self.status = -1 
    16         self.prevact = None 
    1716 
    1817        #Load config 
    1918        self.button_state = None 
    20         self.dllimit = self.ullimit = None 
    21         self.dlmax = self.ulmax = None 
     19        self.settings_structure = [["low_down", "max_download_speed", float],  
     20                                   ["low_up", "max_upload_speed", float],  
     21                                   ["high_down", "max_download_speed", float],  
     22                                   ["high_up", "max_upload_speed", float], 
     23                                   ["low_activetorrents", "max_active_torrents", float], 
     24                                   ["low_numslots", "max_upload_slots_global", float], 
     25                                   ["low_maxconns", "max_connections_global", float], 
     26                                   ["high_activetorrents", "max_active_torrents", float], 
     27                                   ["high_numslots", "max_upload_slots_global", float], 
     28                                   ["high_maxconns", "max_connections_global", float]] 
     29                 
     30        self.settings = {} 
     31         
    2232        try: 
    2333            reader = open(self.conf_file, "rb") 
    2434            data = pickle.load(reader) 
     35             
    2536            self.button_state = data[0] 
    26             self.dllimit = float(data[1][0]) 
    27             self.ullimit = float(data[1][1]) 
    28             self.dlmax = float(data[1][2]) 
    29             self.ulmax = float(data[1][3]) 
     37            for i, item in enumerate(self.settings_structure): 
     38                self.settings[item[0]] = data[1][i] 
     39 
    3040            reader.close() 
    3141        except: 
    3242            if self.button_state is None: 
    3343                self.button_state = [[0] * 7 for dummy in xrange(24)] 
    34             gdl = self.config.get("max_download_speed") 
    35             gul = self.config.get("max_upload_speed") 
    36             if self.dllimit is None: 
    37                 self.dllimit = float(gdl) 
    38             if self.ullimit is None: 
    39                 self.ullimit = float(gul) 
    40             if self.dlmax is None: 
    41                 self.dlmax = float(gdl) 
    42             if self.ulmax is None: 
    43                 self.ulmax = float(gul) 
     44                 
     45            for item in self.settings_structure: 
     46                if item[0] not in self.settings: 
     47                    temp = self.config.get(item[1]) 
     48                     
     49                    if item[2] is not None: 
     50                        temp = item[2](temp) 
     51                     
     52                    self.settings[item[0]] = temp 
    4453 
    4554        now = time.localtime(time.time()) 
    4655        self.status = self.button_state[now[3]][now[6]] 
    class plugin_Scheduler: 
    4857 
    4958        # Force speed changes when the plugin loads 
    5059        self._state(self.status) 
    51  
     60     
    5261    def unload(self): 
    5362        self.status = -1 
    54         self.resume() 
    5563        self.unlimit() 
    5664 
    57     def _state(self,state): 
     65    def _state(self, state): 
    5866        if state == 0: 
    5967            self.unlimit() 
    6068        elif state == 1: 
    6169            self.limit() 
    6270        elif state == 2: 
    6371            self.pause() 
    64         # If we're moving from paused 
    65         if state < 2 and self.status == 2: 
    66             self.resume() 
    67         self.status = state 
     72         
    6873        # Update the settings 
    69         self.interface.apply_prefs() 
     74        self.status = state 
    7075 
    7176    def update(self): 
    7277        # Only do stuff if the status is valid 
    class plugin_Scheduler: 
    8085                self._state(self.button_state[now[3]][now[6]]) 
    8186 
    8287    def pause(self): 
    83         self.prevact = self.config.get("max_active_torrents") 
    8488        self.config.set("max_active_torrents", 0) 
    85         self.core.apply_queue() 
    86  
    87     def resume(self): 
    88         if self.prevact != None: 
    89             self.config.set("max_active_torrents", self.prevact) 
    90         self.core.apply_queue() 
    9189 
    9290    def limit(self): 
    93         self.config.set("max_download_speed", float(self.dllimit)) 
    94         self.config.set("max_upload_speed", float(self.ullimit)) 
     91        self.apply_configuration("low") 
    9592 
    9693    def unlimit(self): 
    97         self.config.set("max_download_speed", float(self.dlmax)) 
    98         self.config.set("max_upload_speed", float(self.ulmax)) 
     94        self.apply_configuration("high") 
    9995 
     96    def apply_configuration(self, type): 
     97        for item in self.settings_structure: 
     98            if item[0].find(type) == 0: 
     99                self.config.set(item[1], self.settings[item[0]]) 
     100         
     101        self.core.apply_queue() 
     102        self.interface.apply_prefs() 
     103     
    100104    #Configuration dialog 
    101105    def configure(self, window): 
    102106        global scheduler_select 
    103107 
    104108        self.button_state_temp = copy.deepcopy(self.button_state) 
    105  
     109         
     110        #data 
     111        spin = {} 
     112        boxen = [ 
     113                 ["down", _("Download limit:"), -1, 2048],  
     114                 ["up", _("Upload limit:"), -1, 1024], 
     115                 ["activetorrents", _("Active torrents:"), 0, 128], 
     116                 ["numslots", _("Upload Slots:"), 0, 128], 
     117                 ["maxconns", _("Max Connections:"), 0, 1024]] 
     118         
    106119        #dialog 
    107120        dialog = gtk.Dialog(_("Scheduler Settings")) 
    108121        dialog.set_default_size(600, 270) 
    class plugin_Scheduler: 
    114127        #text 
    115128        hover_text = gtk.Label() 
    116129 
    117         dlmax_label = gtk.Label(_("High download limit:")) 
    118         ulmax_label = gtk.Label(_("High upload limit:")) 
    119         dllimit_label = gtk.Label(_("Low download limit:")) 
    120         ullimit_label = gtk.Label(_("Low upload limit:")) 
    121  
    122130        #Select Widget 
    123131        drawing = scheduler_select(self.button_state_temp, hover_text, self.days) 
    124132 
    class plugin_Scheduler: 
    128136        vbox_sub = gtk.VBox() 
    129137        hbox_key = gtk.HBox() 
    130138        hbox_info = gtk.HBox() 
    131         # max boxen 
    132         hbox_max = gtk.HBox() 
    133         ebox_max = gtk.EventBox() 
    134         ebox_max.add(hbox_max) 
    135         ebrd_max = gtk.Frame() 
    136         ebrd_max.add(ebox_max) 
    137         ebrd_max.set_border_width(2) 
    138         hbox_max.set_border_width(2) 
    139         ebox_max.modify_bg(gtk.STATE_NORMAL,gtk.gdk.color_parse("#73D716")) 
    140         ebrd_max.modify_bg(gtk.STATE_NORMAL,gtk.gdk.color_parse("#53B700")) 
    141         # limit boxen 
    142         hbox_limit = gtk.HBox() 
    143         ebox_limit = gtk.EventBox() 
    144         ebox_limit.add(hbox_limit) 
    145         ebrd_limit = gtk.Frame() 
    146         ebrd_limit.add(ebox_limit) 
    147         ebrd_limit.set_border_width(2) 
    148         hbox_limit.set_border_width(2) 
     139        hbox_settings = gtk.HBox() 
     140        # high boxen 
     141        tbl_high = gtk.Table(len(boxen), 2) 
     142        ebox_high = gtk.EventBox() 
     143        ebox_high.add(tbl_high) 
     144        ebrd_high = gtk.Frame() 
     145        ebrd_high.add(ebox_high) 
     146        ebrd_high.set_border_width(2) 
     147        tbl_high.set_border_width(2) 
     148        ebox_high.modify_bg(gtk.STATE_NORMAL,gtk.gdk.color_parse("#73D716")) 
     149        ebrd_high.modify_bg(gtk.STATE_NORMAL,gtk.gdk.color_parse("#53B700")) 
     150        # low boxen 
     151        tbl_low = gtk.Table(len(boxen), 2) 
     152        ebox_low = gtk.EventBox() 
     153        ebox_low.add(tbl_low) 
     154        ebrd_low = gtk.Frame() 
     155        ebrd_low.add(ebox_low) 
     156        ebrd_low.set_border_width(2) 
     157        tbl_low.set_border_width(2) 
    149158        # Green 
    150159        # Yellow 
    151         ebox_limit.modify_bg(gtk.STATE_NORMAL,gtk.gdk.color_parse("#EDD400")) 
    152         ebrd_limit.modify_bg(gtk.STATE_NORMAL,gtk.gdk.color_parse("#CDB400")) 
     160        ebox_low.modify_bg(gtk.STATE_NORMAL,gtk.gdk.color_parse("#EDD400")) 
     161        ebrd_low.modify_bg(gtk.STATE_NORMAL,gtk.gdk.color_parse("#CDB400")) 
    153162 
    154163        #seperator 
    155164        sep = gtk.HSeparator() 
    156  
    157         # max spinbuttons 
    158         dminput = gtk.SpinButton() 
    159         dminput.set_numeric(True)     
    160         dminput.set_range(-1, 2048) 
    161         dminput.set_increments(1, 10) 
    162         dminput.set_value(float(self.dlmax)) 
    163         uminput = gtk.SpinButton() 
    164         uminput.set_numeric(True) 
    165         uminput.set_range(-1, 1024) 
    166         uminput.set_increments(1, 10) 
    167         uminput.set_value(float(self.ulmax)) 
    168  
    169         # limit spinbuttons 
    170         dlinput = gtk.SpinButton() 
    171         dlinput.set_numeric(True)     
    172         dlinput.set_range(-1, 2048) 
    173         dlinput.set_increments(1, 10) 
    174         dlinput.set_value(float(self.dllimit)) 
    175         ulinput = gtk.SpinButton() 
    176         ulinput.set_numeric(True) 
    177         ulinput.set_range(-1, 1024) 
    178         ulinput.set_increments(1, 10) 
    179         ulinput.set_value(float(self.ullimit)) 
    180  
     165         
    181166        #pack 
    182167        dialog.vbox.pack_start(vbox_main) 
    183168 
    class plugin_Scheduler: 
    186171        vbox_main.pack_start(hbox_key, False, True) 
    187172        vbox_main.pack_start(hbox_info, False, True) 
    188173        vbox_main.pack_start(sep, False, True) 
    189         vbox_main.pack_start(ebrd_max, False, True, 5) 
    190         vbox_main.pack_start(ebrd_limit, False, True, 5) 
     174        vbox_main.pack_start(hbox_settings) 
    191175 
    192176        hbox_main.pack_start(vbox_sub, False, True, 5) 
    193177        hbox_main.pack_start(drawing) 
    194178 
    195179        hbox_key.pack_start(gtk.Label(_("Green is the high limits, yellow is the low limits and red is stopped")), True, False) 
    196180        hbox_info.pack_start(gtk.Label(_("If a limit is set to -1, it is unlimitted.")), True, False) 
    197  
    198         hbox_max.pack_start(dlmax_label, True, False) 
    199         hbox_max.pack_start(dminput, True, False) 
    200         hbox_max.pack_start(ulmax_label, True, False) 
    201         hbox_max.pack_start(uminput, True, False) 
    202  
    203         hbox_limit.pack_start(dllimit_label, True, False) 
    204         hbox_limit.pack_start(dlinput, True, False) 
    205         hbox_limit.pack_start(ullimit_label, True, False) 
    206         hbox_limit.pack_start(ulinput, True, False) 
     181         
     182        hbox_settings.pack_start(ebrd_high, True, True, 5) 
     183        hbox_settings.pack_start(gtk.Label()) 
     184        hbox_settings.pack_start(ebrd_low, True, True, 5) 
     185                      
     186        for box in [tbl_high, tbl_low]: 
     187            for y, val in enumerate(boxen): 
     188                if box is tbl_high: type = "high" 
     189                else: type = "low" 
     190                key = type + "_" + val[0] 
     191                 
     192                label = gtk.Label(val[1]) 
     193                label.set_alignment(0.0, 0.6) 
     194                spin[key] = gtk.SpinButton() 
     195                spin[key].set_numeric(True)     
     196                spin[key].set_range(val[2], val[3]) 
     197                spin[key].set_increments(1, 10) 
     198                spin[key].set_value(self.settings[key]) 
     199                 
     200                box.attach(label, 0, 1, y, y + 1) 
     201                box.attach(spin[key], 1, 2, y, y + 1, False, False) 
    207202 
    208203        for index in xrange(len(self.days)): 
    209204            vbox_sub.pack_start(gtk.Label(self.days[index])) 
    class plugin_Scheduler: 
    219214                self.button_state = copy.deepcopy(drawing.button_state) 
    220215                changed = True 
    221216             
    222             if not self.dlmax == float(dminput.get_value()): 
    223                 self.dlmax = float(dminput.get_value()) 
    224                 changed = True 
    225  
    226             if not self.ulmax == float(uminput.get_value()): 
    227                 self.ulmax = float(uminput.get_value()) 
    228                 changed = True 
    229  
    230             if not self.dllimit == float(dlinput.get_value()): 
    231                 self.dllimit = float(dlinput.get_value()) 
    232                 changed = True 
    233  
    234             if not self.ullimit == float(ulinput.get_value()): 
    235                 self.ullimit = float(ulinput.get_value()) 
    236                 changed = True 
     217            for key in spin.keys(): 
     218                if not self.settings[key] == spin[key].get_value(): 
     219                    self.settings[key] = spin[key].get_value() 
     220                    changed = True 
    237221 
    238222            now = time.localtime(time.time()) 
    239223            if changed: 
    240224                self._state(self.button_state[now[3]][now[6]]) 
    241225 
    242226            writer = open(self.conf_file, "wb") 
    243             pickle.dump([drawing.button_state,[self.dllimit, self.ullimit, self.dlmax, self.ulmax]], writer) 
     227             
     228            out = [] 
     229             
     230            for item in self.settings_structure: 
     231                out.append(self.settings[item[0]]) 
     232                 
     233            pickle.dump([drawing.button_state, out], writer) 
    244234            writer.close() 
    245  
     235             
    246236        dialog.destroy() 
    247237 
    248238class scheduler_select(gtk.DrawingArea):