# Path: plugins/sessionsaver/sessionsaver/store/xmlsessionstore.py
# class XMLSessionStore(SessionStore):
#     def __init__(self, filename = None):
#         super(XMLSessionStore, self).__init__()
# 
#         if filename is None:
#             self.filename = os.path.join(GLib.get_user_config_dir(), 'gedit/saved-sessions.xml')
#         else:
#             self.filename = filename
# 
#         self.load()
# 
#     def _escape(self, string):
#         return string.replace('&', '&amp;') \
#                      .replace('<', '&lt;')  \
#                      .replace('>', '&gt;')  \
#                      .replace('"', '&quot;')
# 
#     def _dump_session(self, session):
#         files = ''.join(['  <file path="%s"/>\n' % self._escape(location.get_uri())
#                             for location in session.files])
#         session_name = self._escape(str(session.name))
#         return '<session name="%s">\n%s</session>\n' % (session_name, files)
# 
#     def dump(self):
#         dump = [self._dump_session(session) for session in self]
#         return '<saved-sessions>\n%s</saved-sessions>\n' % ''.join(dump)
# 
#     def save(self):
#         dirname = os.path.dirname(self.filename)
#         if not os.path.isdir(dirname):
#             os.makedirs(dirname)
# 
#         fp = open(self.filename, "wb")
#         fp.write(bytes('<?xml version="1.0" encoding="UTF-8"?>\n','UTF-8'))
#         fp.write(bytes(self.dump(),'UTF-8'))
#         fp.close()
# 
#     def load(self):
#         if not os.path.isfile(self.filename):
#             return
# 
#         parser = expat.ParserCreate('UTF-8')
#         parser.buffer_text = True
#         parser.StartElementHandler = self._expat_start_handler
#         parser.EndElementHandler = self._expat_end_handler
# 
#         self._current_session = None
#         try:
#             parser.ParseFile(open(self.filename, 'rb'))
#         except:
#             return
#         del self._current_session
# 
#     def _expat_start_handler(self, tag, attr):
#         if tag == 'file':
#             assert self._current_session is not None
#             self._current_session.add_file(str(attr['path']))
#         elif tag == 'session':
#             assert self._current_session is None
#             self._current_session = Session(attr['name'])
# 
#     def _expat_end_handler(self, tag):
#         if tag == 'session':
#             self.add(self._current_session)
#             self._current_session = None


from gi.repository import GObject, Gio, Gedit
from .store.xmlsessionstore import XMLSessionStore
    import gettext


class SessionSaverAppActivatable(GObject.Object, Gedit.AppActivatable):

    app = GObject.Property(type=Gedit.App)
    __instance = None

    def __init__(self):
        GObject.Object.__init__(self)
        SessionSaverAppActivatable.__instance = self

    @classmethod
    def get_instance(cls):
        return cls.__instance

    def do_activate(self):
        self._insert_session_menu()

    def do_deactivate(self):
        self.menu_ext = None

    def _insert_session_menu(self):
        self.menu_ext = self.extend_menu("tools-section")

        item = Gio.MenuItem.new(_("_Manage Saved Sessions…"), "win.managedsession")
        self.menu_ext.append_menu_item(item)

        item = Gio.MenuItem.new(_("_Save Session…"), "win.savesession")
        self.menu_ext.append_menu_item(item)


# Path: plugins/sessionsaver/sessionsaver/store/xmlsessionstore.py
# class XMLSessionStore(SessionStore):
#     def __init__(self, filename = None):
#         super(XMLSessionStore, self).__init__()
# 
#         if filename is None:
#             self.filename = os.path.join(GLib.get_user_config_dir(), 'gedit/saved-sessions.xml')
#         else:
#             self.filename = filename
# 
#         self.load()
# 
#     def _escape(self, string):
#         return string.replace('&', '&amp;') \
#                      .replace('<', '&lt;')  \
#                      .replace('>', '&gt;')  \
#                      .replace('"', '&quot;')
# 
#     def _dump_session(self, session):
#         files = ''.join(['  <file path="%s"/>\n' % self._escape(location.get_uri())
#                             for location in session.files])
#         session_name = self._escape(str(session.name))
#         return '<session name="%s">\n%s</session>\n' % (session_name, files)
# 
#     def dump(self):
#         dump = [self._dump_session(session) for session in self]
#         return '<saved-sessions>\n%s</saved-sessions>\n' % ''.join(dump)
# 
#     def save(self):
#         dirname = os.path.dirname(self.filename)
#         if not os.path.isdir(dirname):
#             os.makedirs(dirname)
# 
#         fp = open(self.filename, "wb")
#         fp.write(bytes('<?xml version="1.0" encoding="UTF-8"?>\n','UTF-8'))
#         fp.write(bytes(self.dump(),'UTF-8'))
#         fp.close()
# 
#     def load(self):
#         if not os.path.isfile(self.filename):
#             return
# 
#         parser = expat.ParserCreate('UTF-8')
#         parser.buffer_text = True
#         parser.StartElementHandler = self._expat_start_handler
#         parser.EndElementHandler = self._expat_end_handler
# 
#         self._current_session = None
#         try:
#             parser.ParseFile(open(self.filename, 'rb'))
#         except:
#             return
#         del self._current_session
# 
#     def _expat_start_handler(self, tag, attr):
#         if tag == 'file':
#             assert self._current_session is not None
#             self._current_session.add_file(str(attr['path']))
#         elif tag == 'session':
#             assert self._current_session is None
#             self._current_session = Session(attr['name'])
# 
#     def _expat_end_handler(self, tag):
#         if tag == 'session':
#             self.add(self._current_session)
#             self._current_session = None


# Path: plugins/sessionsaver/sessionsaver/appactivatable.py
from gi.repository import GObject, Gio, Gedit
from .store.xmlsessionstore import XMLSessionStore
    import gettext



class SessionSaverAppActivatable(GObject.Object, Gedit.AppActivatable):

    app = GObject.Property(type=Gedit.App)
    __instance = None

    def __init__(self):
        GObject.Object.__init__(self)
        SessionSaverAppActivatable.__instance = self

    @classmethod
    def get_instance(cls):
        return cls.__instance

    def do_activate(self):
        self._insert_session_menu()

    def do_deactivate(self):
        self.menu_ext = None

    def _insert_session_menu(self):
        self.menu_ext = self.extend_menu("tools-section")

        item = Gio.MenuItem.new(_("_Manage Saved Sessions…"), "win.managedsession")
        self.menu_ext.append_menu_item(item)

        item = Gio.MenuItem.new(_("_Save Session…"), "win.savesession")
        self.menu_ext.append_menu_item(item)


        self.sessions = XMLSessionStore()