diff options
-rw-r--r-- | grc/gui/ActionHandler.py | 6 | ||||
-rw-r--r-- | grc/gui/Bars.py | 2 | ||||
-rw-r--r-- | grc/gui/MainWindow.py | 2 | ||||
-rw-r--r-- | grc/gui/Preferences.py | 60 |
4 files changed, 42 insertions, 28 deletions
diff --git a/grc/gui/ActionHandler.py b/grc/gui/ActionHandler.py index ebf701553d..b9135418a3 100644 --- a/grc/gui/ActionHandler.py +++ b/grc/gui/ActionHandler.py @@ -120,7 +120,7 @@ class ActionHandler: Actions.XML_PARSER_ERRORS_DISPLAY.set_sensitive(True) if not self.init_file_paths: - self.init_file_paths = filter(os.path.exists, Preferences.files_open()) + self.init_file_paths = filter(os.path.exists, Preferences.get_open_files()) if not self.init_file_paths: self.init_file_paths = [''] for file_path in self.init_file_paths: if file_path: self.main_window.new_page(file_path) #load pages from file paths @@ -484,7 +484,7 @@ class ActionHandler: if file_paths: #open a new page for each file, show only the first for i,file_path in enumerate(file_paths): self.main_window.new_page(file_path, show=(i==0)) - Preferences.files_recent_add(file_path) + Preferences.add_recent_file(file_path) self.main_window.tool_bar.refresh_submenus() self.main_window.menu_bar.refresh_submenus() @@ -517,7 +517,7 @@ class ActionHandler: if file_path is not None: self.get_page().set_file_path(file_path) Actions.FLOW_GRAPH_SAVE() - Preferences.files_recent_add(file_path) + Preferences.add_recent_file(file_path) self.main_window.tool_bar.refresh_submenus() self.main_window.menu_bar.refresh_submenus() elif action == Actions.FLOW_GRAPH_SCREEN_CAPTURE: diff --git a/grc/gui/Bars.py b/grc/gui/Bars.py index 8a04b1c8f7..19f041f508 100644 --- a/grc/gui/Bars.py +++ b/grc/gui/Bars.py @@ -198,7 +198,7 @@ class SubMenuCreator(object): """menu showing recent flow-graphs""" import Preferences menu = gtk.Menu() - recent_files = Preferences.files_recent() + recent_files = Preferences.get_recent_files() if len(recent_files) > 0: for i, file_name in enumerate(recent_files): item = gtk.MenuItem("%d. %s" % (i+1, file_name)) diff --git a/grc/gui/MainWindow.py b/grc/gui/MainWindow.py index 08cef11639..a340bcc817 100644 --- a/grc/gui/MainWindow.py +++ b/grc/gui/MainWindow.py @@ -224,7 +224,7 @@ class MainWindow(gtk.Window): break if self.notebook.get_n_pages(): return False #save state before closing - Preferences.files_open(open_files) + Preferences.set_open_files(open_files) Preferences.file_open(open_file) Preferences.main_window_size(self.get_size()) Preferences.reports_window_position(self.flow_graph_vpaned.get_position()) diff --git a/grc/gui/Preferences.py b/grc/gui/Preferences.py index 54dab38282..3ebee24345 100644 --- a/grc/gui/Preferences.py +++ b/grc/gui/Preferences.py @@ -17,6 +17,7 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA """ +import os import sys import ConfigParser @@ -94,36 +95,49 @@ def file_open(filename=None): return entry('file_open', filename, default='') -def files_lists(key, files=None): - if files is not None: - _config_parser.remove_section(key) # clear section - _config_parser.add_section(key) - for i, filename in enumerate(files): - _config_parser.set(key, '%s_%d' % (key, i), filename) +def set_file_list(key, files): + _config_parser.remove_section(key) # clear section + _config_parser.add_section(key) + for i, filename in enumerate(files): + _config_parser.set(key, '%s_%d' % (key, i), filename) + + +def get_file_list(key): + try: + files = [value for name, value in _config_parser.items(key) + if name.startswith('%s_' % key)] + except ConfigParser.Error: + files = [] + return files + + +def get_open_files(): + return get_file_list('files_open') + + +def set_open_files(files): + return set_file_list('files_open', files) + + +def get_recent_files(): + """ Gets recent files, removes any that do not exist and re-saves it """ + files = filter(os.path.exists, get_file_list('files_recent')) + set_recent_files(files) + return files - else: - try: - files = [value for name, value in _config_parser.items(key) - if name.startswith('%s_' % key)] - except ConfigParser.Error: - files = [] - return files -def files_open(files=None): - return files_lists('files_open', files) +def set_recent_files(files): + return set_file_list('files_recent', files) -def files_recent(files=None): - return files_lists('files_recent', files) -def files_recent_add(file_name): - import os +def add_recent_file(file_name): # double check file_name if os.path.exists(file_name): - recent_files = files_recent() + recent_files = get_recent_files() if file_name in recent_files: - recent_files.remove(file_name) # attempt removal - recent_files.insert(0, file_name) # insert at start - files_recent(recent_files[:10]) # keep up to 10 files + recent_files.remove(file_name) # Attempt removal + recent_files.insert(0, file_name) # Insert at start + set_recent_files(recent_files[:10]) # Keep up to 10 files def reports_window_position(pos=None): |