~bkidwell/zim/pyzim-win-installer

« back to all changes in this revision

Viewing changes to zim/plugins/trayicon.py

  • Committer: Jaap Karssenberg
  • Date: 2011-04-19 18:38:01 UTC
  • Revision ID: pardus@cpan.org-20110419183801-x3typoc0dt246pl6
Last minute fix to the tray icon

Show diffs side-by-side

added added

removed removed

Lines of Context:
7
7
 
8
8
from zim.plugins import PluginClass
9
9
from zim.config import data_file, config_file
10
 
from zim.notebook import get_notebook_list
 
10
from zim.notebook import get_notebook_list, NotebookInfo, NotebookInfoList
11
11
from zim.gui.widgets import gtk_window_set_default_icon
12
12
 
13
13
 
150
150
 
151
151
                return menu
152
152
 
153
 
        def list_configured_notebooks(self):
154
 
                # returns (name, uri) pairs
155
 
                list = get_notebook_list()
156
 
                return [(info.name, info.uri) for info in list]
157
 
 
158
153
        def list_open_notebooks(self):
 
154
                '''Returns a list of open notebook.
 
155
 
 
156
                This method is to be implemented in child classes.
 
157
 
 
158
                @returns: a list of L{NotebookInfo} objects
 
159
                '''
159
160
                # should return (name, uri) pairs
160
161
                raise NotImplementedError
161
162
 
162
163
        def list_all_notebooks(self):
 
164
                '''Returns a list of all notebooks known in the current context
 
165
 
 
166
                This method mixes notebooks from L{list_open_notebooks()} with
 
167
                input from L{get_notebook_list()}. Open notebooks will have
 
168
                the C{active} attribute set.
 
169
 
 
170
                @returns: a list of L{NotebookInfo} objects
 
171
                '''
163
172
                uris = set()
164
 
                notebooks = []
165
 
                for nlist in (
166
 
                        self.list_configured_notebooks(),
167
 
                        self.list_open_notebooks()
168
 
                ):
169
 
                        for name, uri in nlist:
170
 
                                if not uri in uris:
171
 
                                        uris.add(uri)
172
 
                                        notebooks.append((name, uri))
173
 
                notebooks.sort()
 
173
                notebooks = [info for info in get_notebook_list()]
 
174
                for info in self.list_open_notebooks():
 
175
                        if info in notebooks:
 
176
                                # info from notebook list is updated already
 
177
                                i = notebooks.index(info)
 
178
                                notebooks[i].active = True
 
179
                        else:
 
180
                                info.update()
 
181
                                info.active = True
 
182
                                notebooks.append(info)
 
183
 
 
184
                for info in notebooks:
 
185
                        if not info.active:
 
186
                                info.active = False # None -> False
 
187
 
174
188
                return notebooks
175
189
 
176
190
        def populate_menu_with_notebooks(self, menu, notebooks):
180
194
                item.set_sensitive(False)
181
195
                menu.append(item)
182
196
 
183
 
                for name, uri in notebooks:
184
 
                        #~ print '>>>', name, uri
185
 
                        item = gtk.MenuItem('  ' + name) # Hack - using '  ' to indent visually
186
 
                        item.connect('activate', lambda o, u: self.do_activate_notebook(u), uri)
 
197
                if isinstance(notebooks, NotebookInfoList):
 
198
                        notebooks = [info for info in notebooks] # copy
 
199
 
 
200
                notebooks.sort(key=lambda info: info.name)
 
201
 
 
202
                for info in notebooks:
 
203
                        #~ print '>>>', info
 
204
                        item = gtk.MenuItem('  ' + info.name)
 
205
                                # Hack - using '  ' to indent visually
 
206
                        if info.active:
 
207
                                child = item.get_child()
 
208
                                if isinstance(child, gtk.Label):
 
209
                                        # FIXME this doesn't seem to work in Ubuntu menu :(
 
210
                                        child.set_markup('  <b>' + info.name + '</b>')
 
211
                                                # Hack - using '  ' to indent visually
 
212
                        item.connect('activate', lambda o, u: self.do_activate_notebook(u), info.uri)
187
213
                        menu.append(item)
188
214
 
189
215
        def do_activate_notebook(self, uri):
225
251
                        # No open notebooks, open default or prompt full list
226
252
                        notebooks = get_notebook_list()
227
253
                        if notebooks.default:
228
 
                                self.do_activate_notebook(notebooks.uri)
 
254
                                self.do_activate_notebook(notebooks.default)
229
255
                        else:
230
 
                                self.do_popup_menu_notebooks([info.uri for info in notebooks])
 
256
                                self.do_popup_menu_notebooks(notebooks)
231
257
                elif len(open_notebooks) == 1:
232
258
                        # Only one open notebook - present it
233
 
                        self.do_activate_notebook(open_notebooks[0][1])
 
259
                        self.do_activate_notebook(open_notebooks[0].uri)
234
260
                else:
235
261
                        # Let the user choose from the open notebooks
236
262
                        self.do_popup_menu_notebooks(open_notebooks)
270
296
        def list_open_notebooks(self):
271
297
                # No daemon, so we only know one open notebook
272
298
                notebook = self.ui.notebook
273
 
                return [(notebook.name, notebook.uri)]
 
299
                info = NotebookInfo(notebook.uri, name=notebook.name)
 
300
                info.active = True
 
301
                return [ info ]
274
302
 
275
303
        def do_activate_notebook(self, uri):
276
304
                # Open a notebook using the ui object
302
330
                gtk.main_quit()
303
331
 
304
332
        def list_open_notebooks(self):
305
 
                list = get_notebook_list()
306
 
                names = dict((info.uri, info.name) for info in list)
307
333
                for uri in self.daemon.list_notebooks():
308
 
                        name = names[uri] or uri
309
 
                        yield name, uri
 
334
                        info = NotebookInfo(uri)
 
335
                        info.active = True
 
336
                        yield info
310
337
 
311
338
        def do_activate_notebook(self, uri):
312
339
                self.daemon.get_notebook(uri).toggle_present()