Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/view/home/activitiesring.py
blob: 4566823e6f817e8282805e16890bfa53faccaff9 (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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
# Copyright (C) 2006-2007 Red Hat, Inc.
# Copyright (C) 2008 One Laptop Per Child
#
# 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 2 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, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

import os
import logging
import signal
from gettext import gettext as _
import re
import math

import gobject
import gtk
import hippo
import dbus

from hardware import hardwaremanager
from sugar.graphics import style
from sugar.graphics.palette import Palette
from sugar.graphics.icon import Icon, CanvasIcon
from sugar.graphics.xocolor import XoColor
from sugar.graphics.menuitem import MenuItem
from sugar.profile import get_profile
from sugar import env
from sugar import activity

from view.palettes import JournalPalette, CurrentActivityPalette
from view.home.MyIcon import MyIcon
from model.shellmodel import ShellModel
from hardware import schoolserver

_logger = logging.getLogger('ActivitiesRing')

class ActivitiesRing(hippo.CanvasBox, hippo.CanvasItem):
    __gtype_name__ = 'SugarActivitiesRing'

    def __init__(self, shell):
        hippo.CanvasBox.__init__(self, background_color=style.COLOR_WHITE.get_int())

        self._shell = shell
        shell.get_model().connect('notify::state', self._shell_state_changed_cb)

        self._my_icon = _MyIcon(shell, style.XLARGE_ICON_SIZE)
        self.append(self._my_icon, hippo.PACK_FIXED)

        self._current_activity = CurrentActivityIcon(shell)
        self.append(self._current_activity, hippo.PACK_FIXED)

        self.set_layout(RingLayout())

        registry = activity.get_registry()
        registry.get_activities_async(reply_handler=self._get_activities_cb)
        registry.connect('activity-added', self.__activity_added_cb)
        registry.connect('activity-removed', self.__activity_removed_cb)
        registry.connect('activity-changed', self.__activity_changed_cb)

    def _get_activities_cb(self, activity_list):
        for info in activity_list:
            if info.favorite and info.bundle_id != "org.laptop.JournalActivity":
                self.append(ActivityIcon(self._shell, info))

    def __activity_added_cb(self, activity_registry, activity_info):
        if activity_info.favorite and \
                activity_info.bundle_id != "org.laptop.JournalActivity":
            self.append(ActivityIcon(self._shell, activity_info))

    def _find_activity_icon(self, bundle_id, version):
        for icon in self.get_children():
            if isinstance(icon, ActivityIcon) and \
                    icon.bundle_id == bundle_id and icon.version == version:
                return icon
        return None

    def __activity_removed_cb(self, activity_registry, activity_info):
        icon = self._find_activity_icon(activity_info.bundle_id,
                activity_info.version)
        if icon is not None:
            self.remove(icon)

    def __activity_changed_cb(self, activity_registry, activity_info):
        if activity_info.bundle_id == "org.laptop.JournalActivity":
            return
        icon = self._find_activity_icon(activity_info.bundle_id,
                activity_info.version)
        if icon is not None and not activity_info.favorite:
            self.remove(icon)
        elif icon is None and activity_info.favorite:
            self.append(ActivityIcon(self._shell, activity_info))

    def _shell_state_changed_cb(self, model, pspec):
        # FIXME implement this
        if model.props.state == ShellModel.STATE_SHUTDOWN:
            pass

    def do_allocate(self, width, height, origin_changed):
        hippo.CanvasBox.do_allocate(self, width, height, origin_changed)

        [my_icon_width, my_icon_height] = self._my_icon.get_allocation()
        x = (width - my_icon_width) / 2
        y = (height - my_icon_height - style.GRID_CELL_SIZE) / 2
        self.set_position(self._my_icon, x, y)

        [icon_width, icon_height] = self._current_activity.get_allocation()
        x = (width - icon_width) / 2
        y = (height + my_icon_height + style.DEFAULT_PADDING - style.GRID_CELL_SIZE) / 2
        self.set_position(self._current_activity, x, y)

    def enable_xo_palette(self):
        self._my_icon.enable_palette()

class ActivityIcon(CanvasIcon):
    def __init__(self, shell, activity_info):
        CanvasIcon.__init__(self, cache=True, file_name=activity_info.icon)
        self._shell = shell
        self._activity_info = activity_info
        self.set_palette(ActivityPalette(shell, activity_info))
        self.connect('hovering-changed', self.__hovering_changed_event_cb)
        self.connect('button-release-event', self.__button_release_event_cb)

        self.props.stroke_color = style.COLOR_BUTTON_GREY.get_svg()
        self.props.fill_color = style.COLOR_TRANSPARENT.get_svg()

    def __hovering_changed_event_cb(self, icon, event):
        if event:
            self.props.xo_color = get_profile().color
        else:
            self.props.stroke_color = style.COLOR_BUTTON_GREY.get_svg()
            self.props.fill_color = style.COLOR_TRANSPARENT.get_svg()

    def __button_release_event_cb(self, icon, event):
        self._shell.start_activity(self._activity_info.bundle_id)

    def get_bundle_id(self):
        return self._activity_info.bundle_id
    bundle_id = property(get_bundle_id, None)

    def get_version(self):
        return self._activity_info.version
    version = property(get_version, None)

class ActivityPalette(Palette):
    def __init__(self, shell, activity_info):
        activity_icon = Icon(file=activity_info.icon,
                             xo_color=get_profile().color,
                             icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR)
        
        Palette.__init__(self, None, None, primary_text=activity_info.name,
                         icon=activity_icon)

        self._shell = shell
        self._activity_info = activity_info

        menu_item = MenuItem(_('Start'), 'activity-start')
        menu_item.connect('activate', self.__start_activate_cb)
        self.menu.append(menu_item)
        menu_item.show()

        menu_item = MenuItem(_('Start with'), 'activity-start')
        menu_item.props.sensitive = False
        #menu_item.connect('activate', self.__start_with_activate_cb)
        self.menu.append(menu_item)
        menu_item.show()

    def __start_activate_cb(self, menu_item):
        self._shell.start_activity(self._activity_info.bundle_id)

class CurrentActivityIcon(CanvasIcon, hippo.CanvasItem):
    def __init__(self, shell):
        CanvasIcon.__init__(self, cache=True)
        self._home_model = shell.get_model().get_home()

        if self._home_model.get_pending_activity() is not None:
            self._update(self._home_model.get_pending_activity())

        self._home_model.connect('pending-activity-changed',
                self.__pending_activity_changed_cb)

        self.connect('button-release-event', self.__button_release_event_cb)

    def __button_release_event_cb(self, icon, event):
        self._home_model.get_pending_activity().get_window().activate(1)

    def _update(self, home_activity):
        _logger.debug('CurrentActivityIcon._update')
        self.props.file_name = home_activity.get_icon_path()
        self.props.xo_color = home_activity.get_icon_color()
        self.props.size = style.STANDARD_ICON_SIZE

        if home_activity.get_type() == "org.laptop.JournalActivity":
            palette = JournalPalette(self, home_activity)
        else:
            palette = CurrentActivityPalette(self, home_activity)
        self.set_palette(palette)

    def __pending_activity_changed_cb(self, home_model, home_activity):
        self._update(home_activity)

class RingLayout(gobject.GObject, hippo.CanvasLayout):
    __gtype_name__ = 'SugarRingLayout'
    def __init__(self):
        gobject.GObject.__init__(self)
        self._box = None

    def do_set_box(self, box):
        self._box = box

    def do_get_height_request(self, for_width):
        return 0, gtk.gdk.screen_height() - style.GRID_CELL_SIZE

    def do_get_width_request(self):
        return 0, gtk.gdk.screen_width()

    def _calculate_radius_and_icon_size(self, children_count):
        minimum_radius = style.XLARGE_ICON_SIZE / 2 + style.DEFAULT_SPACING + \
                style.STANDARD_ICON_SIZE * 2
        maximum_radius = (gtk.gdk.screen_height() - style.GRID_CELL_SIZE) / 2 - \
                style.STANDARD_ICON_SIZE - style.DEFAULT_SPACING
        angle = 2 * math.pi / children_count

        _logger.debug('minimum_radius %r maximum_radius %r angle %r' % \
                (minimum_radius, maximum_radius, angle))

        # what's the radius required without downscaling?
        distance = style.STANDARD_ICON_SIZE + style.DEFAULT_SPACING
        icon_size = style.STANDARD_ICON_SIZE
        
        if children_count == 1:
            radius = 0
        else:
            radius = math.sqrt(distance ** 2 /
                    (math.sin(angle) ** 2 + (math.cos(angle) - 1) ** 2))

        _logger.debug('radius 1 %r' % radius)
        
        if radius < minimum_radius:
            # we can upscale, if we want
            icon_size += style.STANDARD_ICON_SIZE * \
                    (0.5 * (minimum_radius - radius)/minimum_radius)
            radius = minimum_radius
        elif radius > maximum_radius:
            radius = maximum_radius
            # need to downscale. what's the icon size required?
            distance = math.sqrt((radius * math.sin(angle)) ** 2 + \
                    (radius * (math.cos(angle) - 1)) ** 2)
            icon_size = distance - style.DEFAULT_SPACING

        _logger.debug('radius 2 %r icon_size %r' % (radius, icon_size))
        
        return radius, icon_size

    def _calculate_position(self, radius, icon_size, index, children_count):
        width, height = self._box.get_allocation()
        angle = index * (2 * math.pi / children_count) - math.pi/2
        x = radius * math.cos(angle) + (width - icon_size) / 2
        y = radius * math.sin(angle) + (height - icon_size - style.GRID_CELL_SIZE) / 2
        return x, y

    def do_allocate(self, x, y, width, height, req_width, req_height,
                    origin_changed):
        _logger.debug('RingLayout.do_allocate: %r %r %r %r %r %r %r' % (x, y,
                width, height, req_width, req_height, origin_changed))

        children = self._box.get_layout_children()
        if not children:
            return

        radius, icon_size = self._calculate_radius_and_icon_size(len(children))

        for n in range(len(children)):
            child = children[n]
            # TODO: We get here a glib warning and I don't know why.
            child.item.props.size = icon_size

            x, y = self._calculate_position(radius, icon_size, n, len(children))

            # We need to always get requests to not confuse hippo
            min_w, child_width = child.get_width_request()
            min_h, child_height = child.get_height_request(child_width)

            child.allocate(int(x),
                           int(y),
                           child_width,
                           child_height,
                           origin_changed)

class _MyIcon(MyIcon):
    def __init__(self, shell, scale):
        MyIcon.__init__(self, scale)

        self._power_manager = None
        self._shell = shell
        self._profile = get_profile()

    def enable_palette(self):
        palette_icon = Icon(icon_name='computer-xo', 
                            icon_size=gtk.ICON_SIZE_LARGE_TOOLBAR,
                            xo_color=self._profile.color)
        palette = Palette(self._profile.nick_name,
                          #secondary_text='Sample secondary label',
                          icon=palette_icon)

        item = MenuItem(_('About this XO'))

        icon = Icon(icon_name='computer-xo', icon_size=gtk.ICON_SIZE_MENU,
                xo_color=self._profile.color)
        item.set_image(icon)
        icon.show()

        item.connect('activate', self._about_activate_cb)
        palette.menu.append(item)
        item.show()

        item = MenuItem(_('Restart'), 'system-restart')
        item.connect('activate', self._reboot_activate_cb)
        palette.menu.append(item)
        item.show()

        item = MenuItem(_('Shutdown'), 'system-shutdown')
        item.connect('activate', self._shutdown_activate_cb)
        palette.menu.append(item)
        item.show()

        if not self._profile.is_registered():
            item = MenuItem(_('Register'), 'media-record')
            item.connect('activate', self._register_activate_cb)
            palette.menu.append(item)
            item.show()
 
        self.set_palette(palette)

    def _reboot_activate_cb(self, menuitem):
        model = self._shell.get_model()
        model.props.state = ShellModel.STATE_SHUTDOWN

        pm = self._get_power_manager()

        hw_manager = hardwaremanager.get_manager()
        hw_manager.shutdown()

        if env.is_emulator():
            self._close_emulator()
        else:
            pm.Reboot()

    def _shutdown_activate_cb(self, menuitem):
        model = self._shell.get_model()
        model.props.state = ShellModel.STATE_SHUTDOWN

        pm = self._get_power_manager()

        hw_manager = hardwaremanager.get_manager()
        hw_manager.shutdown()

        if env.is_emulator():
            self._close_emulator()
        else:
            pm.Shutdown()

    def _register_activate_cb(self, menuitem):
        schoolserver.register_laptop()
        if self._profile.is_registered():
            self.get_palette().menu.remove(menuitem)

    def _about_activate_cb(self, menuitem):        
        dialog = gtk.Dialog(_('About this XO'),
                            self.palette,
                            gtk.DIALOG_MODAL | 
                            gtk.DIALOG_DESTROY_WITH_PARENT,
                            (gtk.STOCK_OK, gtk.RESPONSE_OK))

        not_available = _('Not available')
        build = self._read_file('/boot/olpc_build')
        if build is None:
            build = not_available
        label_build = gtk.Label('Build: %s' % build)
        label_build.set_alignment(0, 0.5)
        label_build.show()
        dialog.vbox.pack_start(label_build)
                
        firmware = self._read_file('/ofw/openprom/model')
        if firmware is None:
            firmware = not_available
        else:
            firmware = re.split(" +", firmware)
            if len(firmware) == 3:
                firmware = firmware[1]
        label_firmware = gtk.Label('Firmware: %s' % firmware)
        label_firmware.set_alignment(0, 0.5)
        label_firmware.show()
        dialog.vbox.pack_start(label_firmware)
                
        serial = self._read_file('/ofw/serial-number')
        if serial is None:
            serial = not_available
        label_serial = gtk.Label('Serial Number: %s' % serial)
        label_serial.set_alignment(0, 0.5)
        label_serial.show()
        dialog.vbox.pack_start(label_serial)

        dialog.set_default_response(gtk.RESPONSE_OK)
        dialog.connect('response', self._response_cb)
        dialog.show()

    def _read_file(self, path):
        if os.access(path, os.R_OK) == 0:
            _logger.error('read_file() No such file or directory: %s', path)
            return None
        
        fd = open(path, 'r')
        value = fd.read()
        fd.close()            
        if value:
            value = value.strip('\n')
            return value
        else:
            _logger.error('read_file() No information in file or directory: %s', path)
            return None

    def _response_cb(self, widget, response_id):
        if response_id == gtk.RESPONSE_OK:            
            widget.destroy()

    def _close_emulator(self):
        if os.environ.has_key('SUGAR_EMULATOR_PID'):
            pid = int(os.environ['SUGAR_EMULATOR_PID'])
            os.kill(pid, signal.SIGTERM)

    def _get_power_manager(self):
        if self._power_manager is None:
            bus = dbus.SystemBus()
            proxy = bus.get_object('org.freedesktop.Hal', 
                                '/org/freedesktop/Hal/devices/computer')
            self._power_manager = dbus.Interface(proxy, \
                            'org.freedesktop.Hal.Device.SystemPowerManagement') 

        return self._power_manager