Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/plugin/launcher.py
blob: d55117eda125dc2db1e795764e881425a53bb567 (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
# 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 logging
from gettext import gettext as _

import gtk
import wnck
import gconf
import gobject

from sugar_network import launch

from sugar import wm
from sugar.graphics.xocolor import XoColor
from sugar.activity.activityfactory import create_activity_id

from jarabe.view.launcher import LaunchWindow
from jarabe.journal import model
from jarabe.model import shell
from jarabe.plugins.sn import get_client, get_registry, add_alert, get_browser


_logger = logging.getLogger('plugins.sn.launcher')


class Launcher(object):

    def __init__(self):
        self._launches = {}
        self._screen = wnck.screen_get_default()
        self._screen.connect('window-opened', self.__window_opened_cb)
        get_client().connect_to_signal('Event', self.__Event_cb)

    def launch(self, bundle, activity_id=None, object_id=None, uri=None,
            color=None, invited=None, args=None):
        if activity_id:
            activity = shell.get_model().get_activity_by_id(activity_id)
            if activity is not None:
                _logger.debug('Resume %r activity', activity_id)
                activity.get_window().activate(gtk.get_current_event_time())
                return

        def found_jobject(props):
            self._launch(bundle,
                    props.get('activity_id') or activity_id,
                    props.get('object_id') or object_id,
                    uri,
                    XoColor(props['icon-color']) if 'icon-color' in props
                            else color,
                    args)

        def not_found_jobject(error):
            _logger.exception('Failed to launch %r: %s',
                    bundle.get_bundle_id(), error)

        # pylint: disable-msg=W0212
        if activity_id and not object_id:
            _logger.debug('Look for jobject for %r activity_id', activity_id)
            model._get_datastore().find({'activity_id': activity_id}, ['uid'],
                    reply_handler=lambda jobjects, total:
                            found_jobject(jobjects[0] if total else {}),
                    error_handler=not_found_jobject, byte_arrays=True)
        elif object_id and not activity_id:
            _logger.debug('Look for %r jobject', object_id)
            model._get_datastore().get_properties(object_id,
                    reply_handler=found_jobject,
                    error_handler=not_found_jobject, byte_arrays=True)
        else:
            self._launch(bundle, activity_id, object_id, uri, color, args)

    def _launch(self, bundle, activity_id, object_id, uri, color, extra_args):
        if not activity_id:
            activity_id = create_activity_id()
        if color is None:
            gc = gconf.client_get_default()
            color = XoColor(gc.get_string('/desktop/sugar/user/color'))

        args = ['-b', bundle.get_bundle_id()]
        if activity_id:
            args.extend(['-a', activity_id])
        if object_id:
            args.extend(['-o', object_id])
        if uri:
            args.extend(['-u', uri])
        if extra_args:
            args.extend(extra_args)

        _logger.info('Starting %r: activity_id=%r object_id=%r uri=%r',
                bundle.get_bundle_id(), activity_id, object_id, uri)

        pipe = launch(bundle.mountpoint, bundle.get_bundle_id(), 'activity',
                args)
        gobject.io_add_watch(pipe.fileno(), gobject.IO_IN | gobject.IO_HUP,
                self.__progress_cb, pipe, activity_id)

        window = LaunchWindow(activity_id, bundle.get_icon(), color)
        window.connect('realize', self.__window_realize_cb,
                bundle.get_bundle_id(), activity_id)
        window.show()
        self._launches[activity_id] = window

    def _stop_launcher(self, activity_id):
        if activity_id not in self._launches:
            return
        _logger.debug('Stop %r launcher', activity_id)
        window = self._launches.pop(activity_id)
        window.destroy()

    def _failure_report(self, event):
        kwargs = {'context': event.get('context')}
        if 'implementation' in event:
            kwargs['implementation'] = event['implementation']
        if 'log_path' in event:
            kwargs['filename'] = event['log_path']
        get_browser().open_report(**kwargs)

    def __window_opened_cb(self, screen, window):
        if window.get_window_type() != wnck.WINDOW_NORMAL or \
                wm.get_sugar_window_type(window) == 'launcher':
            return
        activity_id = wm.get_activity_id(window)
        if activity_id:
            self._stop_launcher(activity_id)

    def __window_realize_cb(self, widget, bundle_id, activity_id):
        wm.set_activity_id(widget.window, str(activity_id))
        widget.window.property_change('_SUGAR_WINDOW_TYPE', 'STRING', 8,
                gtk.gdk.PROP_MODE_REPLACE, 'launcher')
        wm.set_bundle_id(widget.window, str(bundle_id))

    def __Event_cb(self, event):
        if event.get('event') != 'launch':
            return
        bundle = get_registry().get_bundle(
                event['context'], event['mountpoint'])
        if bundle is None:
            add_alert('error', msg=_('Cannot find %s activity to launch') %
                    event['context'])
        else:
            self.launch(bundle, object_id=event['object_id'], uri=event['uri'],
                    args=event['args'])

    def __progress_cb(self, source, cb_condition, pipe, activity_id):
        event = pipe.read()
        if event is None:
            return False
        _logger.debug('Execution progress for %r: %r', activity_id, event)
        try:
            if event['state'] == 'failure':
                _logger.warning('Activity %r failed', activity_id)
                self._stop_launcher(activity_id)
                self._failure_report(event)
        except Exception:
            _logger.exception('Failed to process event')
        return True