Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/view/home/activitieslist.py
blob: 2fa404d5e5a80d563227e1dd1e30d2b9bdb2edc4 (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
# 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 logging

import gobject
import hippo

from sugar import profile
from sugar import activity
from sugar.graphics import style
from sugar.graphics.icon import CanvasIcon

class ActivitiesList(hippo.CanvasScrollbars):
    __gtype_name__ = 'SugarActivitiesList'

    def __init__(self, shell):
        hippo.CanvasScrollbars.__init__(self)
        self.set_policy(hippo.ORIENTATION_HORIZONTAL, hippo.SCROLLBAR_NEVER)
        
        self._shell = shell
        self._box = hippo.CanvasBox(background_color=style.COLOR_WHITE.get_int())
        self.set_root(self._box)

        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)

    def _get_activities_cb(self, activity_list):
        for info in activity_list:
            if info.bundle_id != 'org.laptop.JournalActivity':
                self._add_activity(info)

    def __activity_added_cb(self, activity_registry, activity_info):
        self._add_activity(activity_info)

    def __activity_removed_cb(self, activity_registry, activity_info):
        for entry in self.get_children():
            if entry.get_bundle_id() == activity_info.bundle_id:
                self.remove(entry)
                return

    def _add_activity(self, activity_info):
        self._box.append(ActivityEntry(self._shell, activity_info))

class ActivityEntry(hippo.CanvasBox, hippo.CanvasItem):
    __gtype_name__ = 'SugarActivityEntry'

    _TITLE_COL_WIDTH   = style.GRID_CELL_SIZE * 3
    _VERSION_COL_WIDTH = style.GRID_CELL_SIZE * 1
    _DATE_COL_WIDTH    = style.GRID_CELL_SIZE * 5

    def __init__(self, shell, activity_info):
        hippo.CanvasBox.__init__(self, spacing=style.DEFAULT_SPACING,
                                 padding_top=style.DEFAULT_PADDING,
                                 padding_bottom=style.DEFAULT_PADDING,
                                 padding_left=style.DEFAULT_PADDING * 2,
                                 padding_right=style.DEFAULT_PADDING * 2,
                                 box_height=style.GRID_CELL_SIZE,
                                 orientation=hippo.ORIENTATION_HORIZONTAL)

        self._shell = shell
        self._activity_info = activity_info

        favorite_icon = FavoriteIcon(self._activity_info.favorite)
        favorite_icon.connect('notify::favorite', self.__favorite_changed_cb)
        self.append(favorite_icon)

        self.icon = CanvasIcon(size=style.STANDARD_ICON_SIZE, cache=True,
                file_name=activity_info.icon,
                stroke_color=style.COLOR_BUTTON_GREY.get_svg(),
                fill_color=style.COLOR_TRANSPARENT.get_svg())
        self.icon.connect('hovering-changed',
                          self.__icon_hovering_changed_event_cb)
        self.icon.connect('button-release-event',
                          self.__icon_button_release_event_cb)

        self.append(self.icon)

        title = hippo.CanvasText(text=activity_info.name,
                                 xalign=hippo.ALIGNMENT_START,
                                 font_desc=style.FONT_BOLD.get_pango_desc(),
                                 box_width=ActivityEntry._TITLE_COL_WIDTH)
        self.append(title)

        version = hippo.CanvasText(text=activity_info.version,
                                   xalign=hippo.ALIGNMENT_END,
                                   font_desc=style.FONT_NORMAL.get_pango_desc(),
                                   box_width=ActivityEntry._VERSION_COL_WIDTH)
        self.append(version)

        expander = hippo.CanvasBox()
        self.append(expander, hippo.PACK_EXPAND)

        date = hippo.CanvasText(text='3 weeks ago',
                                xalign=hippo.ALIGNMENT_START,
                                font_desc=style.FONT_NORMAL.get_pango_desc(),
                                box_width=ActivityEntry._DATE_COL_WIDTH)
        self.append(date)

    def __favorite_changed_cb(self, favorite_icon, pspec):
        registry = activity.get_registry()
        registry.set_activity_favorite(self._activity_info.bundle_id,
                self._activity_info.version, favorite_icon.props.favorite)

    def __icon_hovering_changed_event_cb(self, icon, event):
        if event:
            self.icon.props.xo_color = profile.get_color()
        else:
            self.icon.props.stroke_color = style.COLOR_BUTTON_GREY.get_svg()
            self.icon.props.fill_color = style.COLOR_TRANSPARENT.get_svg()

    def __icon_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

class FavoriteIcon(CanvasIcon):
    __gproperties__ = {
        'favorite' : (bool, None, None, False,
                  gobject.PARAM_READWRITE)
    }

    def __init__(self, favorite):
        CanvasIcon.__init__(self, icon_name='emblem-favorite',
                            box_width=style.GRID_CELL_SIZE*3/5,
                            size=style.SMALL_ICON_SIZE)
        self._favorite = None
        self._set_favorite(favorite)
        self.connect('button-release-event', self.__release_event_cb)

    def _set_favorite(self, favorite):
        if favorite == self._favorite:
            return

        self._favorite = favorite
        if favorite:
            self.props.xo_color = profile.get_color()
        else:
            self.props.stroke_color = style.COLOR_BUTTON_GREY.get_svg()
            self.props.fill_color = style.COLOR_WHITE.get_svg()

    def do_set_property(self, pspec, value):
        if pspec.name == 'favorite':
            self._set_favorite(value)
        else:
            CanvasIcon.do_set_property(self, pspec, value)

    def do_get_property(self, pspec):
        if pspec.name == 'favorite':
            return self._favorite
        else:
            return CanvasIcon.do_get_property(self, pspec)

    def __release_event_cb(self, icon, event):
        self.props.favorite = not self.props.favorite