1
# -*- coding: utf-8 -*-
3
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
4
#Copyright (c) 2006 The PIDA Project
6
#Permission is hereby granted, free of charge, to any person obtaining a copy
7
#of this software and associated documentation files (the "Software"), to deal
8
#in the Software without restriction, including without limitation the rights
9
#to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
#copies of the Software, and to permit persons to whom the Software is
11
#furnished to do so, subject to the following conditions:
13
#The above copyright notice and this permission notice shall be included in
14
#all copies or substantial portions of the Software.
16
#THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
#IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
#FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
#AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
#LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
#OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
29
from pida.core import service
30
from pida.model import attrtypes as types
31
from pida.core.service import definitions as defs
33
def get_documents_from_file(filename):
34
f = open(filename, 'r')
36
filename = line.strip()
40
def save_documents_to_file(documents, filename):
41
f = open(filename, 'w')
44
f.write('%s\n' % doc.filename)
48
__order__ = ['sessions']
49
class sessions(defs.optiongroup):
50
"""Session management."""
51
__order__ = ['automatically_load_last_session',
52
'start_with_new_file']
54
class automatically_load_last_session(defs.option):
55
"""Whether the session will be reloaded from closing PIDA."""
58
label = 'Automatically load last session'
59
class start_with_new_file(defs.option):
60
"""Whether a new file will be opened on startup if none other is
61
specified on the command-line on by a session."""
64
label = 'Start PIDA with a new file'
67
return 'Session Manager'
70
class SessionManager(service.service):
72
display_name = 'Session Management'
74
config_definition = SessionConfig
77
self.__session_loaded = False
79
def bnd_editormanager_started(self):
80
if not self.__session_loaded:
81
self.__session_loaded = True
83
for filename in self.boss.positional_args:
84
filepath = os.path.abspath(filename)
85
self.boss.call_command('buffermanager',
86
'open_file', filename=filepath, quiet=True)
87
docsloaded = docsloaded + 1
89
opts = self.options.sessions
90
if not docsloaded and opts.automatically_load_last_session:
91
most_recent = os.path.join(self.boss.pida_home,
92
'most-recent.session')
94
if os.path.exists(most_recent):
95
docsloaded = (docsloaded +
96
self.cmd_load_session(session_filename=most_recent))
98
if not docsloaded and opts.start_with_new_file:
99
self.boss.call_command('buffermanager', 'new_file')
101
def act_save_session(self, action):
102
"""Saves the current session"""
103
fdialog = gtk.FileChooserDialog('Please select the session file',
104
parent=self.boss.get_main_window(),
105
action=gtk.FILE_CHOOSER_ACTION_SAVE,
106
buttons=(gtk.STOCK_OK,
109
gtk.RESPONSE_REJECT))
110
def response(dialog, response):
111
if response == gtk.RESPONSE_ACCEPT:
112
self.cmd_save_session(dialog.get_filename())
114
fdialog.connect('response', response)
117
def act_sessions(self, action):
118
"""Sub menu place holder"""
120
def act_load_session(self, action):
121
"""Loads another session"""
122
fdialog = gtk.FileChooserDialog('Please select the session file',
123
parent=self.boss.get_main_window(),
124
action=gtk.FILE_CHOOSER_ACTION_OPEN,
125
buttons=(gtk.STOCK_OK,
128
gtk.RESPONSE_REJECT))
129
def response(dialog, response):
130
if response == gtk.RESPONSE_ACCEPT:
131
self.cmd_load_session(dialog.get_filename())
133
fdialog.connect('response', response)
136
def cmd_save_session(self, session_filename):
138
docs = self.boss.call_command('buffermanager',
139
'get_documents').values()
140
docs.sort(lambda x, y: cmp(x.unique_id, y.unique_id))
141
save_documents_to_file(docs, session_filename)
143
def cmd_load_session(self, session_filename):
145
for filename in get_documents_from_file(session_filename):
146
docsloaded = docsloaded + 1
150
# silently ignore not opened files
151
buffermanager = self.boss.get_service('buffermanager')
152
buffermanager.cmd_open_file(filename=filename,
155
self.log.warn("Error opening file %s: %s" % (filename, e))
157
gobject.idle_add(_o, filename)
158
gobject.timeout_add(500, _p, filename)
162
most_recent = os.path.join(self.boss.pida_home, 'most-recent.session')
163
self.call('save_session', session_filename=most_recent)
165
def get_menu_definition(self):
169
<menu name="base_file" action="base_file_menu">
170
<placeholder name="ExtrasFileMenu">
172
<menu action="sessionmanager+sessions">
173
<menuitem name="savesess" action="sessionmanager+save_session" />
174
<menuitem name="loadsess" action="sessionmanager+load_session" />
183
Service = SessionManager