Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/journal/listmodel.py
blob: 833c8232552d7df9b388eae207475cfc1df7e1f0 (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
# Copyright (C) 2009, Tomeu Vizoso
#
# 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 simplejson
import gobject
import gtk
from gettext import gettext as _

from sugar.graphics.xocolor import XoColor
from sugar.graphics import style
from sugar import util

from jarabe.journal import model
from jarabe.journal import misc


DS_DBUS_SERVICE = 'org.laptop.sugar.DataStore'
DS_DBUS_INTERFACE = 'org.laptop.sugar.DataStore'
DS_DBUS_PATH = '/org/laptop/sugar/DataStore'


class ListModel(gtk.GenericTreeModel, gtk.TreeDragSource):
    __gtype_name__ = 'JournalListModel'

    __gsignals__ = {
        'ready': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
        'progress': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([])),
        'select': (gobject.SIGNAL_RUN_FIRST, gobject.TYPE_NONE, ([bool, bool]))
    }

    COLUMN_UID = 0
    COLUMN_FAVORITE = 1
    COLUMN_ICON = 2
    COLUMN_ICON_COLOR = 3
    COLUMN_TITLE = 4
    COLUMN_TIMESTAMP = 5
    COLUMN_CREATION_TIME = 6
    COLUMN_FILESIZE = 7
    COLUMN_PROGRESS = 8
    COLUMN_BUDDY_1 = 9
    COLUMN_BUDDY_2 = 10
    COLUMN_BUDDY_3 = 11
    COLUMN_SELECT = 12

    _COLUMN_TYPES = {
        COLUMN_UID: str,
        COLUMN_FAVORITE: bool,
        COLUMN_ICON: str,
        COLUMN_ICON_COLOR: object,
        COLUMN_TITLE: str,
        COLUMN_TIMESTAMP: str,
        COLUMN_CREATION_TIME: str,
        COLUMN_FILESIZE: str,
        COLUMN_PROGRESS: int,
        COLUMN_BUDDY_1: object,
        COLUMN_BUDDY_3: object,
        COLUMN_BUDDY_2: object,
        COLUMN_SELECT: bool
    }

    _PAGE_SIZE = 500

    def __init__(self, query):
        gobject.GObject.__init__(self)

        self._last_requested_index = None
        self._cached_row = None
        self._result_set = model.find(query, ListModel._PAGE_SIZE)
        self._temp_drag_file_path = None

        # Multi-selection stuff
        self._selection = set()
        self._query_set_cache = set()

        # HACK: The view will tell us that it is resizing so the model can
        # avoid hitting D-Bus and disk.
        self.view_is_resizing = False

        self._result_set.ready.connect(self.__result_set_ready_cb)
        self._result_set.progress.connect(self.__result_set_progress_cb)

    def __result_set_ready_cb(self, **kwargs):
        self.emit('ready')

    def __result_set_progress_cb(self, **kwargs):
        self.emit('progress')

    def setup(self):
        self._result_set.setup()

    def stop(self):
        self._result_set.stop()

    def get_metadata(self, path):
        return model.get(self[path][ListModel.COLUMN_UID])

    def on_get_n_columns(self):
        return len(ListModel._COLUMN_TYPES)

    def on_get_column_type(self, index):
        return ListModel._COLUMN_TYPES[index]

    def on_iter_n_children(self, iterator):
        if iterator == None:
            return self._result_set.length
        else:
            return 0

    def on_get_value(self, index, column):
        if self.view_is_resizing:
            return None

        if index == self._last_requested_index:
            # HACK: avoid redrawing the whole view just for one row
            selected = (self._cached_row[ListModel.COLUMN_UID] \
                       in self._selection)
            self._cached_row[ListModel.COLUMN_SELECT] = selected

            return self._cached_row[column]

        if index >= self._result_set.length:
            return None

        self._result_set.seek(index)
        metadata = self._result_set.read()

        self._last_requested_index = index
        self._cached_row = []
        self._cached_row.append(metadata['uid'])
        self._cached_row.append(metadata.get('keep', '0') == '1')
        self._cached_row.append(misc.get_icon_name(metadata))

        if misc.is_activity_bundle(metadata):
            xo_color = XoColor('%s,%s' % (style.COLOR_BUTTON_GREY.get_svg(),
                                          style.COLOR_TRANSPARENT.get_svg()))
        else:
            xo_color = misc.get_icon_color(metadata)
        self._cached_row.append(xo_color)

        title = gobject.markup_escape_text(metadata.get('title',
                                           _('Untitled')))
        self._cached_row.append('<b>%s</b>' % (title, ))

        try:
            timestamp = float(metadata.get('timestamp', 0))
        except (TypeError, ValueError):
            timestamp_content = _('Unknown')
        else:
            timestamp_content = util.timestamp_to_elapsed_string(timestamp)
        self._cached_row.append(timestamp_content)

        try:
            creation_time = float(metadata.get('creation_time'))
        except (TypeError, ValueError):
            self._cached_row.append(_('Unknown'))
        else:
            self._cached_row.append(
                util.timestamp_to_elapsed_string(float(creation_time)))

        try:
            size = int(metadata.get('filesize'))
        except (TypeError, ValueError):
            size = None
        self._cached_row.append(util.format_size(size))

        try:
            progress = int(float(metadata.get('progress', 100)))
        except (TypeError, ValueError):
            progress = 100
        self._cached_row.append(progress)

        buddies = []
        if metadata.get('buddies'):
            try:
                buddies = simplejson.loads(metadata['buddies']).values()
            except simplejson.decoder.JSONDecodeError, exception:
                logging.warning('Cannot decode buddies for %r: %s',
                                metadata['uid'], exception)

        if not isinstance(buddies, list):
            logging.warning('Content of buddies for %r is not a list: %r',
                            metadata['uid'], buddies)
            buddies = []

        for n_ in xrange(0, 3):
            if buddies:
                try:
                    nick, color = buddies.pop(0)
                except (AttributeError, ValueError), exception:
                    logging.warning('Malformed buddies for %r: %s',
                                    metadata['uid'], exception)
                else:
                    self._cached_row.append((nick, XoColor(color)))
                    continue

            self._cached_row.append(None)
        selected = (metadata['uid'] in self._selection)
        self._cached_row.append(selected)


        return self._cached_row[column]

    def on_iter_nth_child(self, iterator, n):
        return n

    def on_get_path(self, iterator):
        return (iterator)

    def on_get_iter(self, path):
        return path[0]

    def on_iter_next(self, iterator):
        if iterator != None:
            if iterator >= self._result_set.length - 1:
                return None
            return iterator + 1
        return None

    def on_get_flags(self):
        return gtk.TREE_MODEL_ITERS_PERSIST | gtk.TREE_MODEL_LIST_ONLY

    def on_iter_children(self, iterator):
        return None

    def on_iter_has_child(self, iterator):
        return False

    def on_iter_parent(self, iterator):
        return None

    def do_drag_data_get(self, path, selection):
        uid = self[path][ListModel.COLUMN_UID]
        if selection.target == 'text/uri-list':
            # Get hold of a reference so the temp file doesn't get deleted
            self._temp_drag_file_path = model.get_file(uid)
            logging.debug('putting %r in selection', self._temp_drag_file_path)
            selection.set(selection.target, 8, self._temp_drag_file_path)
            return True
        elif selection.target == 'journal-object-id':
            selection.set(selection.target, 8, uid)
            return True

        return False

    def _get_query_set(self):
        if self._query_set_cache:
            return self._query_set_cache
        query_set = set()
        def collect(model, path, iter):
            query_set.add(model[path][ListModel.COLUMN_UID])
        self.foreach(collect)
        self._query_set_cache = query_set
        return query_set

    def get_selection(self):
        return self._selection.copy()

    def add_selection(self, selection):
        if type(selection) is set:
            query_set = self._get_query_set()
            selection = selection.intersection(query_set)
            self._selection = self._selection.union(selection)
        self._emit_select()

    def set_selection_all(self):
        query_set = self._get_query_set()
        self._selection = self._selection.union(query_set)
        self._emit_select(refresh_view=True)

    def set_selection_none(self):
        self._selection = set()
        self._emit_select(refresh_view=True)

    def toggle_selection(self, path):
        uid = self[path][ListModel.COLUMN_UID]
        if uid in self._selection:
           self._selection.discard(uid)
        else:
            self._selection.add(uid)
        self._emit_select()

    def _emit_select(self, refresh_view=False):
        status = not (not self._selection)
        self.emit('select', status, refresh_view)