Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/secuencia.py
blob: 04fdc7c12784b54a487435489f87df09c7fb1f64 (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
from sugar.activity import activity
from sugar.graphics.toolbarbox import ToolbarBox
from sugar.graphics.objectchooser import ObjectChooser
from sugar.datastore import datastore
from sugar import mime
from sugar.logger import logging
from gettext import gettext as _
import gtk
import os
import statvfs
import zipfile
import gio


ACTION_ICONS = {
    'sound' : gtk.STOCK_MEDIA_PLAY,
    'video' : gtk.STOCK_MEDIA_PLAY,
    'next'  : gtk.STOCK_GO_FORWARD,
}

class Secuencia(activity.Activity):
    def __init__(self, handle):
        activity.Activity.__init__(self, handle)

        self.toolbox = activity.ActivityToolbox(self)
        self.set_toolbox(self.toolbox)
        self.toolbox.show()

        main_box = gtk.VBox()

        frame = gtk.Frame()
        main_box.pack_start(frame)
        frame.show()

        frame_vbox = gtk.VBox()
        frame.add(frame_vbox)
        frame_vbox.show()

        self.text = gtk.Label()
        frame_vbox.pack_start(self.text)
        self.text.show()

        self.image = gtk.Image()
        frame_vbox.pack_start(self.image)
        self.image.show()

        self.action_icon = gtk.Image()
        main_box.pack_start(self.action_icon, False)
        self.action_icon.show()

        self.set_canvas(main_box)
        main_box.show()

        # A list of (filename, mimetype) tuples
        self.list = []
        self.current_pos = -1
        self.executed_action = True

        if self.shared_activity:
            pass
        elif handle.object_id is not None:
            self.list = self.metadata['files']
        else:
            self._show_journal_object_picker()

        self.connect('button-press-event', lambda w, e: self._next())
        self.show_all()
        self._next()

    def _next(self):
        if not self.executed_action:
            card = self.list[self.current_pos]
            self._execute(card)
            self.executed_action = True
        else:
            self.current_pos += 1
            try:
                card = self.list[self.current_pos]
            except IndexError:
                self.current_pos = -1
                self._next()
            else:
                self._show_card(card)
            self.executed_action = False

    def _execute(self, card):
        if 'sound' in card:
            # Play sound
            pass
        elif 'video' in card:
            # Play video
            pass

    def _show_card(self, card):
        if 'image' in card:
            self.image.set_from_file(os.path.join(os.environ['SUGAR_ACTIVITY_ROOT'], 'tmp', card['image']))
        else:
            self.image.set_from_stock(gtk.STOCK_MISSING_IMAGE)

        if 'action' in card:
           self.action_icon.set_from_stock(ACTION_ICONS[card['action']], gtk.ICON_SIZE_LARGE_TOOLBAR) 
        else:
            self.action_icon.set_from_stock(ACTION_ICONS['next'], gtk.ICON_SIZE_LARGE_TOOLBAR)

        self.text.set_text(card['title'])

    def _show_journal_object_picker(self):
        """Show the journal object picker to load a document.

        This is for if Read is launched without a document.
        """
        chooser = ObjectChooser(_('Choose document'), self, 
                                gtk.DIALOG_MODAL | 
                                gtk.DIALOG_DESTROY_WITH_PARENT,
                                what_filter=mime.GENERIC_TYPE_TEXT) #FIXME: Filter by this activity's type
        try:
            result = chooser.run()
            if result == gtk.RESPONSE_ACCEPT:
                logging.debug('ObjectChooser: %r' % 
                              chooser.get_selected_object())
                jobject = chooser.get_selected_object()
                if jobject and jobject.file_path:
                    self.list = jobject.metadata['files']
                    self.read_file(jobject.file_path)
        finally:
            chooser.destroy()
            del chooser

    def read_file(self, path):
        zip = zipfile.ZipFile(path)
        info = zip.infolist()
        size = reduce(lambda s, i: s+i.file_size, info, 0)
        stat = os.stavfs(os.environ['SUGAR_ACTIVITY_ROOT'])
        if stat[statvfs.F_BSIZE]*stat[statvfs.F_BAVAIL] < size:
            #TODO: Alert: Not enough free space
        for member in zip.infolist():
            try:
                data = member.read()
                self.list.append((member.filename, gio.content_type_guess(None, data))
                f = open(os.path.join(os.environ['SUGAR_ACTIVITY_ROOT'], 'tmp', member.filename), 'w')
                f.write(data)
                del data
            finally:
                f.close()

    def write_file(self, path):
        pass