~ubuntu-branches/ubuntu/precise/pida/precise

« back to all changes in this revision

Viewing changes to pida/services/sessionmanager.py

  • Committer: Bazaar Package Importer
  • Author(s): Jan Luebbe
  • Date: 2007-04-17 16:08:06 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20070417160806-3ttlb6igf94x9i03
Tags: 0.4.4-1
* New upstream release (closes: #419129)
* Add dependency on python-glade2 (closes: #418716)
* Update copyright

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# -*- coding: utf-8 -*- 
 
2
 
 
3
# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79:
 
4
#Copyright (c) 2006 The PIDA Project 
 
5
 
 
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:
 
12
 
 
13
#The above copyright notice and this permission notice shall be included in
 
14
#all copies or substantial portions of the Software.
 
15
 
 
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
 
22
#SOFTWARE.
 
23
 
 
24
import os
 
25
 
 
26
import gtk
 
27
import gobject
 
28
 
 
29
from pida.core import service
 
30
from pida.model import attrtypes as types
 
31
from pida.core.service import definitions as defs
 
32
 
 
33
def get_documents_from_file(filename):
 
34
    f = open(filename, 'r')
 
35
    for line in f:
 
36
        filename = line.strip()
 
37
        yield filename
 
38
    f.close()
 
39
 
 
40
def save_documents_to_file(documents, filename):
 
41
    f = open(filename, 'w')
 
42
    for doc in documents:
 
43
        if not doc.is_new:
 
44
            f.write('%s\n' % doc.filename)
 
45
    f.close()
 
46
 
 
47
class SessionConfig:
 
48
    __order__ = ['sessions']
 
49
    class sessions(defs.optiongroup):
 
50
        """Session management."""
 
51
        __order__ = ['automatically_load_last_session',
 
52
                     'start_with_new_file']
 
53
        label = 'Sessions'
 
54
        class automatically_load_last_session(defs.option):
 
55
            """Whether the session will be reloaded from closing PIDA."""
 
56
            rtype = types.boolean
 
57
            default = True
 
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."""
 
62
            rtype = types.boolean
 
63
            default = True
 
64
            label = 'Start PIDA with a new file'
 
65
    
 
66
    def __markup__(self):
 
67
        return 'Session Manager'
 
68
 
 
69
 
 
70
class SessionManager(service.service):
 
71
 
 
72
    display_name = 'Session Management'
 
73
 
 
74
    config_definition = SessionConfig
 
75
 
 
76
    def init(self):
 
77
        self.__session_loaded = False
 
78
 
 
79
    def bnd_editormanager_started(self):
 
80
        if not self.__session_loaded:
 
81
            self.__session_loaded = True
 
82
            docsloaded = 0
 
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
 
88
            
 
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')
 
93
                                           
 
94
                if os.path.exists(most_recent):
 
95
                    docsloaded = (docsloaded +
 
96
                    self.cmd_load_session(session_filename=most_recent))
 
97
            
 
98
            if not docsloaded and opts.start_with_new_file:
 
99
                self.boss.call_command('buffermanager', 'new_file')
 
100
 
 
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,
 
107
                                          gtk.RESPONSE_ACCEPT,
 
108
                                          gtk.STOCK_CANCEL,
 
109
                                          gtk.RESPONSE_REJECT))
 
110
        def response(dialog, response):
 
111
            if response == gtk.RESPONSE_ACCEPT:
 
112
                self.cmd_save_session(dialog.get_filename())
 
113
            dialog.destroy()
 
114
        fdialog.connect('response', response)
 
115
        fdialog.run()
 
116
 
 
117
    def act_sessions(self, action):
 
118
        """Sub menu place holder"""
 
119
 
 
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,
 
126
                                          gtk.RESPONSE_ACCEPT,
 
127
                                          gtk.STOCK_CANCEL,
 
128
                                          gtk.RESPONSE_REJECT))
 
129
        def response(dialog, response):
 
130
            if response == gtk.RESPONSE_ACCEPT:
 
131
                self.cmd_load_session(dialog.get_filename())
 
132
            dialog.destroy()
 
133
        fdialog.connect('response', response)
 
134
        fdialog.run()
 
135
 
 
136
    def cmd_save_session(self, session_filename):
 
137
        # a copy to sort
 
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)
 
142
 
 
143
    def cmd_load_session(self, session_filename):
 
144
        docsloaded = 0
 
145
        for filename in get_documents_from_file(session_filename):
 
146
            docsloaded = docsloaded + 1
 
147
            def _p(filename):
 
148
                def _o(filename):
 
149
                    try:
 
150
                        # silently ignore not opened files
 
151
                        buffermanager = self.boss.get_service('buffermanager')
 
152
                        buffermanager.cmd_open_file(filename=filename,
 
153
                                                    quiet=True)
 
154
                    except OSError, e:
 
155
                        self.log.warn("Error opening file %s: %s" % (filename, e))
 
156
 
 
157
                gobject.idle_add(_o, filename)
 
158
            gobject.timeout_add(500, _p, filename)
 
159
        return docsloaded
 
160
 
 
161
    def stop(self):
 
162
        most_recent = os.path.join(self.boss.pida_home, 'most-recent.session')
 
163
        self.call('save_session', session_filename=most_recent)
 
164
 
 
165
    def get_menu_definition(self):
 
166
        return  """
 
167
                <ui>
 
168
                <menubar>
 
169
                <menu name="base_file" action="base_file_menu">
 
170
                <placeholder name="ExtrasFileMenu">
 
171
                <separator />
 
172
                <menu action="sessionmanager+sessions">
 
173
                <menuitem name="savesess" action="sessionmanager+save_session" />
 
174
                <menuitem name="loadsess" action="sessionmanager+load_session" />
 
175
                </menu>
 
176
                <separator />
 
177
                </placeholder>
 
178
                </menu>
 
179
                </menubar>
 
180
                </ui>
 
181
                """
 
182
 
 
183
Service = SessionManager