diff options
author | Aleksey Lim <alsroot@sugarlabs.org> | 2012-07-13 21:51:07 (GMT) |
---|---|---|
committer | Aleksey Lim <alsroot@sugarlabs.org> | 2012-07-13 21:59:07 (GMT) |
commit | 23e7099ff6734d9273aaabe2b2935c5fe8a2de27 (patch) | |
tree | 09b44a1eff5824d06a5e7c59f6d325a71f45f0d6 /plugin/__init__.py |
Initial commit
Diffstat (limited to 'plugin/__init__.py')
-rw-r--r-- | plugin/__init__.py | 173 |
1 files changed, 173 insertions, 0 deletions
diff --git a/plugin/__init__.py b/plugin/__init__.py new file mode 100644 index 0000000..5fe5cf7 --- /dev/null +++ b/plugin/__init__.py @@ -0,0 +1,173 @@ +# Copyright (C) 2012 Aleksey Lim +# +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +import os +import subprocess +from gettext import gettext as _ + +import gobject + +from sugar.graphics.alert import NotifyAlert, ErrorAlert +from sugar_network import sugar, GlibClient, api_url, server_mode +from active_toolkit.options import Option + + +ORDER = 5 + +SN_BROWSER_NAME = 'sugar-network-browser' + +SN_MASER_URL = 'http://api-testing.network.sugarlabs.org' + +sugar_network = Option( + 'enable Sugar Network support in Sugar Shell', + default=False, type_cast=Option.bool_cast, + action='store_true', name='sugar-network') + + +_ALERT_SEVERITIES = { + # severity: (alert_class, alert_message) + 'error': (ErrorAlert, _('Sugar Network error')), + None: (NotifyAlert, _('Sugar Network')), + } + +_bundleregistry = None +_launcher = None +_client = None +_browser = None + +Option.seek('sweets', [sugar_network]) +Option.seek('local', [api_url, server_mode]) + + +def init(): + global _bundleregistry + + if not sugar_network.value: + return + + if api_url.value and api_url.value != SN_MASER_URL: + # For now, keep only global SN server to make testing more relibale + api_url.value = SN_MASER_URL + Option.save() + + if not os.fork(): + args = ['sugar-network-service', '--webui', '--delayed-start', 'start'] + os.execvp(args[0], args) + + import jarabe.model.bundleregistry + from .bundleregistry import BundleRegistry + + _bundleregistry = BundleRegistry() + jarabe.model.bundleregistry.get_registry = lambda: _bundleregistry + + from jarabe.model.shell import Activity + from jarabe.view import palettes + from .browser import Palette + + Activity.get_bundle_path = _get_bundle_path + palettes.predefined[SN_BROWSER_NAME] = Palette + + +def enable(): + global _launcher, _client + + if not sugar_network.value: + return + + _client = GlibClient() + _client.connect('alert', + lambda sender, severity, message: add_alert(severity, msg=message)) + + _bundleregistry.populate() + + from jarabe.journal import misc + from .launcher import Launcher + + _launcher = Launcher() + misc.launch = _launcher.launch + + from jarabe.model.shell import get_model + shell = get_model() + + def delayed_start(): + _client.publish('delayed-start') + get_browser() + + def activity_added_cb(model, activity): + if activity.is_journal(): + shell.disconnect_by_func(activity_added_cb) + gobject.idle_add(delayed_start) + + shell.connect('activity-added', activity_added_cb) + + +def disable(): + pass + + +def get_registry(): + return _bundleregistry + + +def get_client(): + return _client + + +def get_browser(): + global _browser + + if not sugar_network.value: + return + + if _browser is None: + from .browser import Browser + _browser = Browser() + + return _browser + + +def add_alert(severity, callback=None, **kwargs): + if sugar_network.value: + window = get_browser() + else: + from jarabe.journal.journalactivity import get_journal + window = get_journal() + + def response_cb(alert, response_id): + window.remove_alert(alert) + if callback is not None: + callback() + del alert + + cls, title = _ALERT_SEVERITIES.get(severity) or _ALERT_SEVERITIES[None] + if 'title' not in kwargs: + kwargs['title'] = title + alert = cls(**kwargs) + alert.connect('response', response_cb) + + window.add_alert(alert) + window.reveal() + + +def _get_bundle_path(self): + # pylint: disable-msg=W0212 + if not self._windows: + return None + + pid = self._windows[0].get_pid() + with file('/proc/%d/environ' % pid) as f: + for env in f.read().split('\0'): + if env.startswith('SUGAR_BUNDLE_PATH='): + return env.split('=', 1)[-1] |