Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/jarabe/journal/objectmodel.py
blob: 621f261c9351796c22f5d56ea34cbfa22a3afd85 (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
# Copyright (C) 2009, 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 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 gtk
import cairo
import gobject
import logging

from sugar import util

from jarabe.journal import misc
from jarabe.journal.source import Source
from jarabe.journal.browse.lazymodel import LazyModel

class ObjectModel(LazyModel):
    def __init__(self):
        LazyModel.__init__(self, Source.FIELDS_BASE, Source.FIELDS_CALC)
        self._fetch_queue = []
        self._object_delayed_fetch_handle = None

    def on_calc_value(self, row, column):
        if column == Source.FIELD_MODIFY_TIME:
            return util.timestamp_to_elapsed_string(
                    int(row[Source.FIELD_TIMESTAMP]) or 0)

        if column == Source.FIELD_THUMB:
            row = self.fetch_metadata(row)
            if row is not None:
                return 
            return None

        return None

    def fetch_metadata(self, row, force=False):
        if row.has_key(Source.FIELD_FETCHED):
            return row

        if row not in self._fetch_queue:
            self._fetch_queue.append(row)
            if len(self._fetch_queue) == 1:
                gobject.idle_add(self.__idle_cb, force)

    def __idle_cb(self, force):
        while len(self._fetch_queue):
            row = self._fetch_queue[0]
            if force or self.in_frame(row.path[0]):
                self.source.get_object(row, self.__get_object_cb)
                break
            del self._fetch_queue[0]
        return False

    def __get_object_cb(self, metadata):
        row = self._fetch_queue[0]
        del self._fetch_queue[0]

        if metadata is not None:
            row[Source.FIELD_THUMB] = misc.load_preview(metadata)
            row.metadata.update(metadata)
            row[Source.FIELD_FETCHED] = True
            self.emit('row-changed', row.path, row.iter)

        if len(self._fetch_queue):
            self.__idle_cb(False)