# Copyright (C) 2007 - Nando Vieira # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, or (at your option) # any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. import gedit import gtk import gtk.gdk import gconf import os import os.path from gettext import gettext as _ GCONF_KEY_BASE = '/apps/gedit-2/plugins/session_saver' GCONF_KEY_FILES = '/apps/gedit-2/plugins/session_saver/files' DEBUG_NAME = 'SS_DEBUG' DEBUG_TITLE = 'session_saver' def debug(text, level=1): if os.environ.has_key(DEBUG_NAME): try: required_level = int(os.environ[DEBUG_NAME]) if required_level >= level: print "[%s] %s" % (DEBUG_TITLE, text) except: print "[%s] debug error" % DEBUG_TITLE class SessionSaverPlugin(gedit.Plugin): def __init__(self): gedit.Plugin.__init__(self) self.instances = {} def activate(self, window): debug('activating plugin') self.instances[window] = SessionSaverWindowHelper(self, window) def deactivate(self, window): debug('deactivating plugin') self.instances[window].deactivate() del self.instances[window] def update_ui(self, window): debug('updating ui') self.instances[window].update_ui() class SessionSaverWindowHelper: handlers = {} def __init__(self, plugin, window): self.window = window self.plugin = plugin self.window.connect('delete-event', self.save_session_on_exit) self.open_session() def deactivate(self): debug('deactivate function called') gconf_set_str(GCONF_KEY_FILES, '') self.window.disconnect_by_func(self.save_session_on_exit) self.window = None self.plugin = None def update_ui(self): pass def update(self): pass def open_session(self): files = gconf_get_str(GCONF_KEY_FILES, '') if files == '': return files = files.split("\n") debug("files found:\n- %s" % "\n- ".join(files)) os.system('gedit %s' % " ".join(files)) def save_session_on_exit(self, window, data): debug('saving session on exit') docs = window.get_documents() uris = [] debug('documents found: %d' % len(docs)) for buffer in docs: uri = buffer.get_uri() if uri is None: self.close_empty_document(window, buffer) else: uris.append(uri) files = "\n".join(uris) debug("documents in this session:\n- %s" % "\n- ".join(uris)) gconf_set_str(GCONF_KEY_FILES, files) def close_empty_document(self, window, buffer): debug('closing empty unsaved documents') start_iter = buffer.get_start_iter() end_iter = buffer.get_end_iter() text = buffer.get_text(start_iter, end_iter) if text == '': debug('closing empty document') tab = gedit.tab_get_from_document(buffer) window.close_tab(tab) else: debug('document not empty') def gconf_get_bool(key, default = False): val = gconf.client_get_default().get(key) if val is not None and val.type == gconf.VALUE_BOOL: return val.get_bool() else: return default def gconf_set_bool(key, value): v = gconf.Value(gconf.VALUE_BOOL) v.set_bool(value) gconf.client_get_default().set(key, v) def gconf_get_str(key, default = ''): val = gconf.client_get_default().get(key) if val is not None and val.type == gconf.VALUE_STRING: return val.get_string() else: return default def gconf_set_str(key, value): v = gconf.Value(gconf.VALUE_STRING) v.set_string(value) gconf.client_get_default().set(key, v)