| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | # Copyright (c) 2007 The PIDA Project |
|---|
| 4 | |
|---|
| 5 | #Permission is hereby granted, free of charge, to any person obtaining a copy |
|---|
| 6 | #of this software and associated documentation files (the "Software"), to deal |
|---|
| 7 | #in the Software without restriction, including without limitation the rights |
|---|
| 8 | #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|---|
| 9 | #copies of the Software, and to permit persons to whom the Software is |
|---|
| 10 | #furnished to do so, subject to the following conditions: |
|---|
| 11 | |
|---|
| 12 | #The above copyright notice and this permission notice shall be included in |
|---|
| 13 | #all copies or substantial portions of the Software. |
|---|
| 14 | |
|---|
| 15 | #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|---|
| 16 | #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|---|
| 17 | #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|---|
| 18 | #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|---|
| 19 | #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|---|
| 20 | #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|---|
| 21 | #SOFTWARE. |
|---|
| 22 | |
|---|
| 23 | import gtk |
|---|
| 24 | |
|---|
| 25 | # PIDA Imports |
|---|
| 26 | from pida.core.service import Service |
|---|
| 27 | from pida.core.features import FeaturesConfig |
|---|
| 28 | from pida.core.commands import CommandsConfig |
|---|
| 29 | from pida.core.events import EventsConfig |
|---|
| 30 | from pida.core.actions import ActionsConfig |
|---|
| 31 | from pida.core.actions import TYPE_NORMAL, TYPE_MENUTOOL, TYPE_RADIO, TYPE_TOGGLE |
|---|
| 32 | from pida.core.environment import get_uidef_path |
|---|
| 33 | |
|---|
| 34 | |
|---|
| 35 | CONTEXT_TYPES = [ |
|---|
| 36 | 'file-menu', |
|---|
| 37 | 'dir-menu', |
|---|
| 38 | 'url-menu', |
|---|
| 39 | ] |
|---|
| 40 | |
|---|
| 41 | class ContextFeaturesConfig(FeaturesConfig): |
|---|
| 42 | |
|---|
| 43 | def create(self): |
|---|
| 44 | for context in CONTEXT_TYPES: |
|---|
| 45 | self.publish(context) |
|---|
| 46 | |
|---|
| 47 | class ContextCommandsConfig(CommandsConfig): |
|---|
| 48 | |
|---|
| 49 | def get_menu(self, context, **kw): |
|---|
| 50 | return self.svc.get_menu(context, **kw) |
|---|
| 51 | |
|---|
| 52 | def popup_menu(self, context, event=None, **kw): |
|---|
| 53 | menu = self.get_menu(context, **kw) |
|---|
| 54 | menu.show_all() |
|---|
| 55 | if event is None: |
|---|
| 56 | button = 3 |
|---|
| 57 | time = gtk.get_current_event_time() |
|---|
| 58 | else: |
|---|
| 59 | button = event.button |
|---|
| 60 | time = event.time |
|---|
| 61 | menu.popup(None, None, None, button, time) |
|---|
| 62 | |
|---|
| 63 | class ContextEventsConfig(EventsConfig): |
|---|
| 64 | |
|---|
| 65 | def subscribe_all_foreign(self): |
|---|
| 66 | self.subscribe_foreign('plugins', 'plugin_started', |
|---|
| 67 | self.plugins_changed) |
|---|
| 68 | self.subscribe_foreign('plugins', 'plugin_stopped', |
|---|
| 69 | self.plugins_changed) |
|---|
| 70 | |
|---|
| 71 | def plugins_changed(self, plugin): |
|---|
| 72 | self.svc.create_uims() |
|---|
| 73 | |
|---|
| 74 | |
|---|
| 75 | # Service class |
|---|
| 76 | class Contexts(Service): |
|---|
| 77 | """Describe your Service Here""" |
|---|
| 78 | |
|---|
| 79 | features_config = ContextFeaturesConfig |
|---|
| 80 | commands_config = ContextCommandsConfig |
|---|
| 81 | events_config = ContextEventsConfig |
|---|
| 82 | |
|---|
| 83 | def start(self): |
|---|
| 84 | self.create_uims() |
|---|
| 85 | |
|---|
| 86 | def create_uims(self): |
|---|
| 87 | self._uims = {} |
|---|
| 88 | for context in CONTEXT_TYPES: |
|---|
| 89 | uim = self._uims[context] = gtk.UIManager() |
|---|
| 90 | uim.add_ui_from_file(self.get_base_ui_definition_path(context)) |
|---|
| 91 | for ag, uidef in self.features[context]: |
|---|
| 92 | uim.insert_action_group(ag, 0) |
|---|
| 93 | uidef_path = get_uidef_path(uidef) |
|---|
| 94 | uim.add_ui_from_file(uidef_path) |
|---|
| 95 | |
|---|
| 96 | def get_base_ui_definition_path(self, context): |
|---|
| 97 | file_name = '%s.xml' % context |
|---|
| 98 | return get_uidef_path(file_name) |
|---|
| 99 | |
|---|
| 100 | def get_menu(self, context, **kw): |
|---|
| 101 | for group in self._uims[context].get_action_groups(): |
|---|
| 102 | for action in group.list_actions(): |
|---|
| 103 | action.contexts_kw = kw |
|---|
| 104 | menu = self._uims[context].get_toplevels('popup')[0] |
|---|
| 105 | return menu |
|---|
| 106 | |
|---|
| 107 | |
|---|
| 108 | |
|---|
| 109 | # Required Service attribute for service loading |
|---|
| 110 | Service = Contexts |
|---|
| 111 | |
|---|
| 112 | |
|---|
| 113 | |
|---|
| 114 | # vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: |
|---|