Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/expandedentry.py
blob: 230b3c0cc449a6553c0505f1db2ab59486fd54b6 (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
# Copyright (C) 2007, 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
from gettext import gettext as _
import StringIO

import hippo
import cairo
import gobject
import gtk
import json

from sugar.graphics import style
from sugar.graphics.icon import CanvasIcon
from sugar.graphics.xocolor import XoColor
from sugar.graphics.entry import CanvasEntry
from sugar.graphics.combobox import ComboBox
from sugar import activity
from sugar.datastore import datastore

from tagbox import TagBox
from keepicon import KeepIcon
import misc

class Separator(hippo.CanvasBox, hippo.CanvasItem):
    def __init__(self, orientation):
        hippo.CanvasBox.__init__(self,
                                 background_color=style.COLOR_PANEL_GREY.get_int())

        if orientation == hippo.ORIENTATION_VERTICAL:
            self.props.box_width = style.LINE_WIDTH
        else:
            self.props.box_height = style.LINE_WIDTH

class CanvasTextView(hippo.CanvasWidget):
    def __init__(self, text, **kwargs):
        hippo.CanvasWidget.__init__(self, **kwargs)
        text_view = gtk.TextView()
        text_view.props.buffer.props.text = text
        self.props.widget = text_view

class BuddyList(hippo.CanvasBox):
    def __init__(self, model):
        hippo.CanvasBox.__init__(self, xalign=hippo.ALIGNMENT_START)

        for buddy in model:
            nick, color = buddy
            hbox = hippo.CanvasBox(orientation=hippo.ORIENTATION_HORIZONTAL)
            icon = CanvasIcon(icon_name='computer-xo',
                              xo_color=XoColor(color),
                              size=style.STANDARD_ICON_SIZE)
            hbox.append(icon)
            
            label = hippo.CanvasText(text=nick,
                                     font_desc=style.FONT_NORMAL.get_pango_desc())
            hbox.append(label)
            
            self.append(hbox)

class ExpandedEntry(hippo.CanvasBox):
    def __init__(self, object_id):
        hippo.CanvasBox.__init__(self)
        self.props.orientation = hippo.ORIENTATION_HORIZONTAL
        self.props.background_color = style.COLOR_WHITE.get_int()
        self.props.spacing = style.DEFAULT_SPACING
        self.props.padding = style.DEFAULT_PADDING

        self._jobject = datastore.get(object_id)
        self._update_title_sid = None

        # Create three columns
        first_column = hippo.CanvasBox(orientation=hippo.ORIENTATION_VERTICAL,
                                       padding=style.DEFAULT_PADDING,
                                       spacing=style.DEFAULT_SPACING)
        self.append(first_column)

        second_column = hippo.CanvasBox(orientation=hippo.ORIENTATION_VERTICAL,
                                        padding_right=style.GRID_CELL_SIZE,
                                        padding=style.DEFAULT_PADDING,
                                        spacing=style.DEFAULT_SPACING)
        self.append(second_column, hippo.PACK_EXPAND)

        self.append(Separator(hippo.ORIENTATION_VERTICAL))
        
        third_column = hippo.CanvasBox(orientation=hippo.ORIENTATION_VERTICAL,
                                       box_width=style.GRID_CELL_SIZE * 4)
        self.append(third_column)

        # First column        
        self._keep_icon = self._create_keep_icon()
        first_column.append(self._keep_icon)

        # Second column
        header = hippo.CanvasBox(orientation=hippo.ORIENTATION_HORIZONTAL,
                                 spacing=style.DEFAULT_SPACING)
        second_column.append(header)
        
        self._icon = self._create_icon()
        header.append(self._icon)

        self._title = self._create_title()
        header.append(self._title, hippo.PACK_EXPAND)

        hbox = hippo.CanvasBox(orientation=hippo.ORIENTATION_HORIZONTAL)
        second_column.append(hbox)
        
        self._preview = self._create_preview()
        hbox.append(self._preview)

        self._buddy_list = self._create_buddy_list()
        hbox.append(self._buddy_list)

        description_box, self._description = self._create_description()
        second_column.append(description_box)

        tags_box, self._tags = self._create_tags()
        second_column.append(tags_box)

        # Third column
        self._version_list = self._create_version_list()
        third_column.append(self._version_list)

    def _create_keep_icon(self):
        keep = self._jobject.metadata.has_key('keep') and \
               self._jobject.metadata['keep'] == 1
        keep_icon = KeepIcon(keep)
        keep_icon.connect('activated', self._keep_icon_activated_cb)
        return keep_icon

    def _create_icon(self):
        icon = CanvasIcon(file_name=misc.get_icon_name(self._jobject))

        if self._jobject.is_activity_bundle():
            icon.props.fill_color=style.COLOR_TRANSPARENT.get_svg()
            icon.props.stroke_color=style.COLOR_BLACK.get_svg()
        else:
            if self._jobject.metadata.has_key('icon-color') and \
                   self._jobject.metadata['icon-color']:
                icon.props.xo_color = XoColor( \
                    self._jobject.metadata['icon-color'])
            
        return icon

    def _create_title(self):
        title = CanvasEntry()
        title.set_background(style.COLOR_WHITE.get_html())

        text = self._jobject.metadata.get('title', _('Untitled'))
        if self._jobject.is_activity_bundle():
            title.props.text = text + _(' Activity')
        else:    
            title.props.text = text
            
        title.props.widget.connect('focus-out-event',
                                   self._title_focus_out_event_cb)
        return title

    def _create_preview(self):
        width = style.zoom(320)
        height = style.zoom(240)
        box = hippo.CanvasBox()

        if self._jobject.metadata.has_key('preview') and \
                len(self._jobject.metadata['preview']) > 4:
            
            if self._jobject.metadata['preview'][1:4] == 'PNG':
                preview_data = self._jobject.metadata['preview']
            else:
                import base64
                preview_data = base64.b64decode(self._jobject.metadata['preview'])
            

            png_file = StringIO.StringIO(preview_data)
            try:
                surface = cairo.ImageSurface.create_from_png(png_file)
                has_preview = True
            except Exception, e:
                logging.error('Error while loading the preview: %r' % e)
                has_preview = False
        else:
            has_preview = False

        if has_preview:
            preview_box = hippo.CanvasImage(image=surface,
                                            border=style.LINE_WIDTH,
                                            border_color=style.COLOR_BLACK.get_int(),
                                            xalign=hippo.ALIGNMENT_CENTER,
                                            yalign=hippo.ALIGNMENT_CENTER,
                                            scale_width=width,
                                            scale_height=height)
        else:
            preview_box = hippo.CanvasText(text=_('No preview'),
                                           font_desc=style.FONT_NORMAL.get_pango_desc(),
                                           xalign=hippo.ALIGNMENT_CENTER,
                                           yalign=hippo.ALIGNMENT_CENTER,
                                           border=style.LINE_WIDTH,
                                           border_color=style.COLOR_BLACK.get_int(),
                                           box_width=width,
                                           box_height=height)
        box.append(preview_box)
        return box

    def _create_buddy_list(self):
        if self._jobject.metadata.has_key('buddies') and \
                self._jobject.metadata['buddies']:
            # json cannot read unicode strings
            buddies_str = self._jobject.metadata['buddies'].encode('utf8')
            buddies = json.read(buddies_str).values()
            return BuddyList(buddies)
        else:
            return BuddyList([])

    def _create_description(self):
        vbox = hippo.CanvasBox()
        #vbox.props.spacing = style.DEFAULT_SPACING
        vbox.append(hippo.CanvasText(text=_('Description:'),
                                     xalign=hippo.ALIGNMENT_START,
                                     font_desc=style.FONT_NORMAL.get_pango_desc()))
        
        description = self._jobject.metadata.get('description', '')
        text_view = CanvasTextView(description, box_height=style.GRID_CELL_SIZE * 2)
        vbox.append(text_view, hippo.PACK_EXPAND)

        text_view.props.widget.props.accepts_tab = False
        text_view.props.widget.connect('focus-out-event',
                                   self._description_focus_out_event_cb)

        return vbox, text_view

    def _create_tags(self):
        vbox = hippo.CanvasBox()
        #vbox.props.spacing = style.DEFAULT_SPACING
        vbox.append(hippo.CanvasText(text=_('Tags:'),
                                     xalign=hippo.ALIGNMENT_START,
                                     font_desc=style.FONT_NORMAL.get_pango_desc()))
        
        tags = self._jobject.metadata.get('tags', '')
        text_view = CanvasTextView(tags, box_height=style.GRID_CELL_SIZE * 2)
        vbox.append(text_view, hippo.PACK_EXPAND)

        text_view.props.widget.props.accepts_tab = False
        text_view.props.widget.connect('focus-out-event',
                                   self._tags_focus_out_event_cb)
        
        return vbox, text_view

    def _create_version_list(self):
        vbox = hippo.CanvasBox()
        vbox.props.spacing = style.DEFAULT_SPACING
        # TODO: Enable again when we have versions in the DS
        """
        jobjects, count = datastore.find({'uid': self._jobject.object_id},
                                         sorting=['-mtime'])
        for jobject in jobjects:
            hbox = hippo.CanvasBox(orientation=hippo.ORIENTATION_HORIZONTAL)
            hbox.props.spacing = style.DEFAULT_SPACING

            icon = CanvasIcon(file_name=misc.get_icon_name(jobject),
                              size=style.SMALL_ICON_SIZE)
            if jobject.metadata.has_key('icon-color') and \
                    jobject.metadata['icon-color']:
                icon.props.xo_color = XoColor(jobject.metadata['icon-color'])
            hbox.append(icon)
            
            date = hippo.CanvasText(text=misc.get_date(jobject),
                                    xalign=hippo.ALIGNMENT_START,
                                    font_desc=style.FONT_NORMAL.get_pango_desc())
            hbox.append(date)
            
            vbox.append(hbox)
        """
        return vbox

    def _title_notify_text_cb(self, entry, pspec):
        if not self._update_title_sid:
            self._update_title_sid = gobject.timeout_add(1000, self._update_title_cb)

    def _datastore_write_cb(self):
        pass

    def _datastore_write_error_cb(self, error):
        logging.error('ExpandedEntry._datastore_write_error_cb: %r' % error)

    def _title_focus_out_event_cb(self, entry, event):
        self._update_entry()

    def _description_focus_out_event_cb(self, text_view, event):
        self._update_entry()

    def _tags_focus_out_event_cb(self, text_view, event):
        self._update_entry()

    def _update_entry(self):
        needs_update = False

        old_title = self._jobject.metadata.get('title', None)
        if old_title != self._title.props.text:
            self._jobject.metadata['title'] = self._title.props.text
            self._jobject.metadata['title_set_by_user'] = '1'
            needs_update = True

        old_tags = self._jobject.metadata.get('tags', None)
        new_tags = self._tags.props.widget.props.buffer.props.text
        if old_tags != new_tags:
            self._jobject.metadata['tags'] = new_tags
            needs_update = True

        old_description = self._jobject.metadata.get('description', None)
        new_description = self._description.props.widget.props.buffer.props.text
        if old_description != new_description:
            self._jobject.metadata['description'] = new_description
            needs_update = True

        if needs_update:
            datastore.write(self._jobject, update_mtime=False,
                            reply_handler=self._datastore_write_cb,
                            error_handler=self._datastore_write_error_cb)
 
        self._update_title_sid = None
 
    def get_keep(self):
        return self._jobject.metadata.has_key('keep') and \
               self._jobject.metadata['keep'] == 1

    def _keep_icon_activated_cb(self, keep_icon):
        if self.get_keep():
            self._jobject.metadata['keep'] = 0
        else:
            self._jobject.metadata['keep'] = 1
        datastore.write(self._jobject, update_mtime=False)

        keep_icon.props.keep = self.get_keep()