diff --git a/src/core.py b/src/core.py index 67b9ddeff..4f9e5c196 100644 --- a/src/core.py +++ b/src/core.py @@ -41,6 +41,7 @@ import os, os.path, shutil, statvfs import pickle import time import gettext +import pref # Constants @@ -52,28 +53,17 @@ DHT_FILENAME = "dht.state" CACHED_DATA_EXPIRATION = 1 # seconds, like the output of time.time() -# "max_half_open" : -1, -DEFAULT_PREFS = { - "max_uploads" : 2, # a.k.a. upload slots - "listen_on" : [6881,9999], - "max_connections" : 80, - "use_DHT" : True, - "max_active_torrents" : -1, - "auto_seed_ratio" : -1, - "max_download_rate" : -1, - "max_upload_rate" : -1 - } - PREF_FUNCTIONS = { "max_uploads" : deluge_core.set_max_uploads, "listen_on" : deluge_core.set_listen_on, "max_connections" : deluge_core.set_max_connections, - "use_DHT" : None, # not a normal pref in that is is applied only on start + "enable_dht" : None, # not a normal pref in that is is applied only on start "max_active_torrents" : None, # no need for a function, applied constantly "auto_seed_ratio" : None, # no need for a function, applied constantly - "max_download_rate" : deluge_core.set_download_rate_limit, - "max_upload_rate" : deluge_core.set_upload_rate_limit - } + "max_download_rate_bps" : deluge_core.set_download_rate_limit, + "max_upload_rate_bps" : deluge_core.set_upload_rate_limit +} + STATE_MESSAGES = ( "Queued", "Checking", "Connecting", @@ -84,7 +74,6 @@ STATE_MESSAGES = ( "Queued", "Allocating" ) - # Exceptions class DelugeError(Exception): @@ -206,21 +195,14 @@ class Manager: self.saved_core_torrent_file_infos = {} # unique_ID -> torrent_state - # Unpickle the preferences, or create a new one - self.prefs = DEFAULT_PREFS - if not blank_slate: - try: - pkl_file = open(os.path.join(self.base_dir, PREFS_FILENAME), 'rb') - self.prefs = pickle.load(pkl_file) - pkl_file.close() - except IOError: - pass + # Load the preferences + self.config = pref.Preferences(os.path.join(self.base_dir, PREFS_FILENAME)) # Apply preferences. Note that this is before any torrents are added self.apply_prefs() # Apply DHT, if needed. Note that this is before any torrents are added - if self.get_pref('use_DHT'): + if self.get_pref('enable_dht'): if not blank_slate: deluge_core.start_DHT(os.path.join(self.base_dir, DHT_FILENAME)) else: @@ -251,10 +233,8 @@ class Manager: self.pre_quitting() # Pickle the prefs - print "Pickling prefs..." - output = open(os.path.join(self.base_dir, PREFS_FILENAME), 'wb') - pickle.dump(self.prefs, output) - output.close() + print "Saving prefs..." + self.config.save() # Pickle the state print "Pickling state..." @@ -267,7 +247,7 @@ class Manager: self.save_fastresume_data() # Stop DHT, if needed - if self.get_pref('use_DHT'): + if self.get_pref('enable_dht'): print "Stopping DHT..." deluge_core.stop_DHT(os.path.join(self.base_dir, DHT_FILENAME)) @@ -284,24 +264,20 @@ class Manager: # Preference management functions + def get_config(self): + # This returns the preference object + return self.config + def get_pref(self, key): - # If we have a value, return, else fallback on default_prefs, else raise an error - # the fallback is useful if the source has newer prefs than the existing pref state, - # which was created by an old version of the source - if key in self.prefs.keys(): - return self.prefs[key] - elif key in DEFAULT_PREFS: - self.prefs[key] = DEFAULT_PREFS[key] - return self.prefs[key] - else: - raise DelugeError("Asked for a pref that doesn't exist: " + key) + # Get the value from the preferences object + return self.config.get(key) def set_pref(self, key, value): # Make sure this is a valid key - if key not in DEFAULT_PREFS.keys(): + if key not in pref.DEFAULT_PREFS.keys(): raise DelugeError("Asked to change a pref that isn't valid: " + key) - self.prefs[key] = value + self.config.set(key, value) # Apply the pref, if applicable if PREF_FUNCTIONS[key] is not None: @@ -373,7 +349,7 @@ class Manager: # Get additional data from our level ret['is_listening'] = deluge_core.is_listening() ret['port'] = deluge_core.listening_port() - if self.get_pref('use_DHT'): + if self.get_pref('enable_dht'): ret['DHT_nodes'] = deluge_core.get_DHT_info() return ret @@ -728,7 +704,6 @@ class Manager: def apply_prefs(self): print "Applying preferences" - assert(len(PREF_FUNCTIONS) == len(DEFAULT_PREFS)) for pref in PREF_FUNCTIONS.keys(): if PREF_FUNCTIONS[pref] is not None: diff --git a/src/interface.py b/src/interface.py index 7a435ae74..bbc1d4375 100644 --- a/src/interface.py +++ b/src/interface.py @@ -85,15 +85,6 @@ class DelugeGTK: self.is_running = False self.ipc_manager = ipc_manager.Manager(self) self.torrent_file_queue = [] - #Load up a config file: - self.conf_file = os.path.join(common.CONFIG_DIR, 'deluge.conf') - if os.path.isdir(self.conf_file): - print 'Weird, the file I was trying to write to, %s, is an existing directory'%(self.conf_file) - sys.exit(0) - if not os.path.isfile(self.conf_file): - f = open(self.conf_file, mode='w') - f.flush() - f.close() #Start the Deluge Manager: self.manager = core.Manager(common.CLIENT_CODE, common.CLIENT_VERSION, '%s %s'%(common.PROGRAM_NAME, common.PROGRAM_VERSION), common.CONFIG_DIR) @@ -103,7 +94,7 @@ class DelugeGTK: if os.path.isdir(os.path.join(common.CONFIG_DIR , 'plugins')): self.plugins.add_plugin_dir(os.path.join(common.CONFIG_DIR, 'plugins')) self.plugins.scan_for_plugins() - self.config = pref.Preferences(self.conf_file, DEFAULT_PREFS) + self.config = self.manager.get_config() #Set up the interface: self.wtree = gtk.glade.XML(common.get_glade_file("delugegtk.glade"), domain=APP) self.window = self.wtree.get_widget("main_window") @@ -515,7 +506,7 @@ class DelugeGTK: if self.window.get_property("visible"): self.preferences_dialog.show() self.apply_prefs() - self.config.save_to_file() + self.config.save() else: if self.config.get("lock_tray", bool, default=False) == True: @@ -544,8 +535,8 @@ class DelugeGTK: auto_seed_ratio = -1 self.tray_icon.set_visible(self.config.get("enable_system_tray", bool, default=True)) self.manager.set_pref("listen_on", ports) - self.manager.set_pref("max_upload_rate", ulrate) - self.manager.set_pref("max_download_rate", dlrate) + self.manager.set_pref("max_upload_rate_bps", ulrate) + self.manager.set_pref("max_download_rate_bps", dlrate) self.manager.set_pref("max_uploads", self.config.get("max_number_uploads", int, default=-1)) self.manager.set_pref("max_connections", self.config.get("max_number_downloads", int, default=-1)) self.manager.set_pref("auto_seed_ratio", auto_seed_ratio) @@ -1076,7 +1067,7 @@ class DelugeGTK: enabled_plugins = ':'.join(self.plugins.get_enabled_plugins()) self.config.set('enabled_plugins', enabled_plugins) self.save_window_settings() - self.config.save_to_file(self.conf_file) + self.config.save() self.plugins.shutdown_all_plugins() self.manager.quit() gtk.main_quit() diff --git a/src/pref.py b/src/pref.py index 7366f8fa5..d0470067b 100644 --- a/src/pref.py +++ b/src/pref.py @@ -22,12 +22,65 @@ # Preferences is basically a wrapper around a simple Python dictionary # object. However, this class provides a few extra features on top of # the built in class that Deluge can take advantage of. + +import pickle +import common + +DEFAULT_PREFS = { + "encin_state" : common.EncState.enabled, + "encout_state" : common.EncState.enabled, + "enclevel_type" : common.EncLevel.both, + "pref_rc4" : True, + "auto_end_seeding" : False, + "auto_seed_ratio" : -1, + "close_to_tray" : False, + "lock_tray" : False, + "tray_passwd" : "", + "default_download_path" : "", + "dht_connections" : 80, + "enable_dht" : True, + "enable_system_tray" : True, + "enabled_plugins" : "", + "end_seed_ratio" : 0.0, + "gui_update_interval" : 1.0, + "listen_on" : [6881,9999], + "max_active_torrents" : -1, + "max_connections" : 80, + "max_download_rate" : -1.0, + "max_download_rate_bps": -1.0, + "max_number_downloads" : -1.0, + "max_number_torrents" : -1.0, + "max_number_uploads" : -1.0, + "max_upload_rate" : -1.0, + "max_upload_rate_bps" : -1.0, + "max_uploads" : 2, + "queue_seeds_to_bottom" : False, + "show_dl" : True, + "show_eta" : True, + "show_infopane" : True, + "show_peers" : True, + "show_seeders" : True, + "show_share" : True, + "show_size" : True, + "show_status" : True, + "show_toolbar" : True, + "show_ul" : True, + "tcp_port_range_lower" : 6881, + "tcp_port_range_upper" : 6889, + "use_compact_storage" : False, + "use_default_dir" : False, + "window_height" : 480, + "window_width" : 640, + "window_x_pos" : 0, + "window_y_pos" : 0, +} class Preferences: def __init__(self, filename=None, defaults=None): - self.mapping = {} + self.mapping = DEFAULT_PREFS + print self.mapping.keys() self.config_file = filename if self.config_file is not None: - self.load_from_file(self.config_file) + self.load(self.config_file) if defaults is not None: for key in defaults.keys(): self.mapping.setdefault(key, defaults[key]) @@ -53,38 +106,32 @@ class Preferences: def keys(self): return self.mapping.keys() def values(self): return self.mapping.values() - def save_to_file(self, filename=None): + def save(self, filename=None): if filename is None: filename = self.config_file - f = open(filename, mode='w') - keys = self.mapping.keys() - keys.sort() - for key in keys: - f.write(key) - f.write(' = ') - f.write(str(self.mapping[key])) - f.write('\n') - f.flush() - f.close() - self.config_file = filename - - def load_from_file(self, filename=None): + try: + pkl_file = open(filename, 'wb') + pickle.dump(self.mapping, pkl_file) + pkl_file.close() + except IOError: + pass + + def load(self, filename=None): if filename is None: filename = self.config_file - f = open(filename, mode='r') - for line in f: - try: - (key, value) = line.split('=', 1) - key = key.strip(' \n') - value = value.strip(' \n') - self.mapping[key] = value - except ValueError: - pass - f.close() - self.config_file = filename + try: + pkl_file = open(filename, 'rb') + self.dump = pickle.load(pkl_file) + self.mapping.update(self.dump) + pkl_file.close() + except IOError: + pass + except EOFError: + pkl_file.close() + pass def set(self, key, value): - self.mapping[key] = value + self.mapping[key] = value def get(self, key, kind=None, default=None): if not key in self.mapping.keys():