Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugin/__init__.py
blob: 5fe5cf7989cc92b27de12c6b5e648f2ffd5b386e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
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]