From d2ecf8a4335de7f093c2b8de44494c431fe57936 Mon Sep 17 00:00:00 2001 From: Zach Tibbitts Date: Fri, 9 Feb 2007 21:41:58 +0000 Subject: [PATCH] preference dialog values saved and loaded --- glade/dgtkpref.glade | 244 ++++++++++++++++++++++--------------------- src/dcommon.py | 40 +++++-- src/delugegtk.py | 51 +++++++-- 3 files changed, 201 insertions(+), 134 deletions(-) diff --git a/glade/dgtkpref.glade b/glade/dgtkpref.glade index a6ab33cda..19b69c24d 100644 --- a/glade/dgtkpref.glade +++ b/glade/dgtkpref.glade @@ -44,17 +44,6 @@ - - - True - Enable system tray icon - True - - - - 2 - - True @@ -68,6 +57,18 @@ 12 + + + True + Enable system tray icon + True + True + + + + 2 + + @@ -94,15 +95,13 @@ 2 2 - - GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER + True + Ask me where to save each download + True - 1 2 - 1 - 2 @@ -118,13 +117,15 @@ - + True - Ask me where to save each download - True + GTK_FILE_CHOOSER_ACTION_SELECT_FOLDER + 1 2 + 1 + 2 @@ -153,6 +154,14 @@ True 1 2 + + + True + Stop seeding torrents when +their share ratio reaches: + True + + True @@ -167,14 +176,6 @@ 2 - - - True - Stop seeding torrents when -their share ratio reaches: - True - - @@ -268,51 +269,13 @@ their share ratio reaches: - + True - Try from: - - - - - True - to: - - - 2 - 3 - - - - - True - True - 0 0 100 1 10 10 - - - 1 - 2 - - - - - True - True - 0 0 100 1 10 10 + Test Port 3 4 - - - - - True - Active port: - - - 1 - 2 1 2 @@ -330,17 +293,55 @@ their share ratio reaches: - + True - Test Port + Active port: + + + 1 + 2 + 1 + 2 + + + + + True + True + 0 0 100 1 10 10 3 4 - 1 - 2 + + + True + True + 0 0 100 1 10 10 + + + 1 + 2 + + + + + True + to: + + + 2 + 3 + + + + + True + Try from: + + @@ -367,20 +368,47 @@ their share ratio reaches: 4 2 - + True - True - 0 0 100 1 10 10 + 0 + Maximum Upload Rate: + + + + + True + 0 + Maximum number of Uploads: + + + 1 + 2 + + + + + True + 0 + Maximum Download Rate: + + + 2 + 3 + + + + + True + 0 + Maximum number of Downloads: - 1 - 2 3 4 - + True True 0 0 100 1 10 10 @@ -388,8 +416,6 @@ their share ratio reaches: 1 2 - 2 - 3 @@ -406,7 +432,7 @@ their share ratio reaches: - + True True 0 0 100 1 10 10 @@ -414,48 +440,23 @@ their share ratio reaches: 1 2 - - - - - True - 0 - Maximum number of Downloads: - - - 3 - 4 - - - - - True - 0 - Maximum Download Rate: - - 2 3 - + True - 0 - Maximum number of Uploads: + True + 0 0 100 1 10 10 - 1 - 2 + 1 + 2 + 3 + 4 - - - True - 0 - Maximum Upload Rate: - - @@ -520,6 +521,7 @@ their share ratio reaches: True gtk-ok True + 1 1 @@ -555,16 +557,11 @@ their share ratio reaches: 2 2 - + True - False - GTK_WRAP_WORD - 1 - 2 - 10 - 10 + 2 @@ -599,11 +596,16 @@ their share ratio reaches: - + True + False + GTK_WRAP_WORD - 2 + 1 + 2 + 10 + 10 diff --git a/src/dcommon.py b/src/dcommon.py index 9f864ef4d..ca47f8f38 100644 --- a/src/dcommon.py +++ b/src/dcommon.py @@ -34,8 +34,29 @@ class DelugePreferences: def set(self, key, value): self.pref[key] = value - def get(self, key): - return self.pref[key] + def get(self, key, kind=None): + result = self.pref[key] + if kind == None: + return result + elif kind == bool: + # Python interprets bool("False") as True, so we must compensate for this + if isinstance(result, str): + return (result.lower() == "true") + elif isinstance(result, int): + return not (result == 0) + else: + return False + elif kind == str: + return str(result) + elif kind == int: + try: + return int(result) + except ValueError: + return int(float(result)) + elif kind == float: + return float(result) + else: + return result def keys(self): return self.pref.keys() @@ -43,18 +64,23 @@ class DelugePreferences: def load_from_file(self, filename): f = open(filename, mode='r') for line in f: - (key, value) = line.split("=") - key = key.strip(" \n") - value = value.strip(" \n") - self.pref[key] = value + try: + (key, value) = line.split("=") + key = key.strip(" \n") + value = value.strip(" \n") + self.pref[key] = value + print 'Read option %s with a value of %s'%(key, value) + except ValueError: + print "Empty Line" f.close() def save_to_file(self, filename): f = open(filename, mode='w') + f.write('#%s preferences file\n\n'%PROGRAM_NAME) for key in self.pref.keys(): f.write(key) f.write(' = ') - f.write(self.pref[key]) + f.write(str(self.pref[key])) f.write('\n') f.flush() f.close() diff --git a/src/delugegtk.py b/src/delugegtk.py index 19423e4b8..741ce089e 100755 --- a/src/delugegtk.py +++ b/src/delugegtk.py @@ -211,9 +211,46 @@ class DelugeGTK(dbus.service.Object): self.abt.hide_all() def show_pref_dialog(self, arg=None): + #Try to get current settings from pref, if an error occurs, the default settings will be used: + try: + # Page 1 + self.prf_glade.get_widget("chk_use_tray").set_active(self.pref.get("enable_system_tray", bool)) + self.prf_glade.get_widget("chk_min_on_close").set_active(self.pref.get("close_to_tray", bool)) + self.prf_glade.get_widget("radio_ask_save").set_active(self.pref.get("ask_download_dir_each_torrent", bool)) + self.prf_glade.get_widget("download_path_button").set_filename(self.pref.get("default_download_path", str)) + self.prf_glade.get_widget("chk_autoseed").set_active(self.pref.get("auto_end_seeding", bool)) + self.prf_glade.get_widget("ratio_spinner").set_value(self.pref.get("end_seed_ratio", float)) + self.prf_glade.get_widget("chk_compact").set_active(self.pref.get("use_compact_storage", bool)) + # Page 2 + self.prf_glade.get_widget("spin_port_min").set_value(self.pref.get("tcp_port_range_lower", int)) + self.prf_glade.get_widget("spin_port_max").set_value(self.pref.get("tcp_port_range_upper", int)) + self.prf_glade.get_widget("spin_max_upload").set_value(self.pref.get("max_upload_rate", int)) + self.prf_glade.get_widget("spin_num_upload").set_value(self.pref.get("max_number_uploads", int)) + self.prf_glade.get_widget("spin_max_download").set_value(self.pref.get("max_download_rate", int)) + self.prf_glade.get_widget("spin_num_download").set_value(self.pref.get("max_number_downloads", int)) + except KeyError: + pass self.prf.show_all() - self.prf.run() + result = self.prf.run() self.prf.hide_all() + print result + if result == 1: + self.pref.set("enable_system_tray", self.prf_glade.get_widget("chk_use_tray").get_active()) + self.pref.set("close_to_tray", self.prf_glade.get_widget("chk_min_on_close").get_active()) + self.pref.set("ask_download_dir_each_torrent", self.prf_glade.get_widget("radio_ask_save").get_active()) + self.pref.set("default_download_path", self.prf_glade.get_widget("download_path_button").get_filename()) + self.pref.set("auto_end_seeding", self.prf_glade.get_widget("chk_autoseed").get_active()) + self.pref.set("end_seed_ratio", self.prf_glade.get_widget("ratio_spinner").get_value()) + self.pref.set("use_compact_storage", self.prf_glade.get_widget("chk_compact").get_active()) + + self.pref.set("tcp_port_range_lower", self.prf_glade.get_widget("spin_port_min").get_value()) + self.pref.set("tcp_port_range_upper", self.prf_glade.get_widget("spin_port_max").get_value()) + self.pref.set("max_upload_rate", self.prf_glade.get_widget("spin_max_upload").get_value()) + self.pref.set("max_number_uploads", self.prf_glade.get_widget("spin_num_upload").get_value()) + self.pref.set("max_download_rate", self.prf_glade.get_widget("spin_max_download").get_value()) + self.pref.set("max_number_downloads", self.prf_glade.get_widget("spin_num_download").get_value()) + + self.pref.save_to_file(self.conf_file) self.apply_prefs() def show_plugin_dialog(self, arg=None): @@ -221,9 +258,9 @@ class DelugeGTK(dbus.service.Object): def tray_toggle(self, obj): if obj.get_active(): - self.wtree.get_widget("chk_min_on_close").set_sensitive(True) + self.prf_glade.get_widget("chk_min_on_close").set_sensitive(True) else: - self.wtree.get_widget("chk_min_on_close").set_sensitive(False) + self.prf_glade.get_widget("chk_min_on_close").set_sensitive(False) @@ -321,9 +358,11 @@ class DelugeGTK(dbus.service.Object): self.store.remove(itr) if not self.store.iter_is_valid(itr): itr = None - if tab == 0: #Details Paneself.text_summary_seeders = self.wtree.get_widget("summary_seeders") - - state = self.manager.get_torrent_state(self.get_selected_torrent()) + if tab == 0: #Details Pane + try: + state = self.manager.get_torrent_state(self.get_selected_torrent()) + except deluge.InvalidUniqueIDError: + return True self.text_summary_title.set_text(str(state["name"])) self.text_summary_total_size.set_text(dcommon.fsize(state["total_size"])) self.text_summary_pieces.set_text(str(state["pieces"]))