Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/smile.py
blob: f6c6913a2c6c56624aab32203f85a768862d2af6 (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
#!/usr/bin/env python

# test ambulantglue

import os, sys, time

import pygtk
pygtk.require('2.0')
import gtk

from sugar.activity import activity
from sugar.datastore import datastore
from sugar.graphics.toolbutton import ToolButton
import ambulantglue
from path import path
import zipfile
import subprocess

BASE = path("/media/0BF7-0F5D") / 'sandbox'
DATASTORE = '/home/olpc/.sugar/default/datastore/store'
DATAPATH = path(activity.get_activity_root()) / "data"
ACTIVITYPATH = path(activity.get_bundle_path())
SMILPATH = ACTIVITYPATH / 'smils'
BUNDLEPATH = DATAPATH / 'bundle'
SELECTTOOLBAR = 1

class SmileActivity(activity.Activity):

    def __init__ (self, handle):
        activity.Activity.__init__(self, handle)
        self.set_title("Smile")
        self.connect("destroy", self.destroy)
        self.exit = False
        #setup chooser screen
        box = gtk.VBox(homogeneous=False)
        self.chooser = Smxoview(self.on_activated)
        self.chooser.show()
        lbl = gtk.Label("Chooserscreen")
        lbl.show()
        box.pack_start(lbl, False, False, 0)
        box.pack_start(self.chooser, True, True, 0)
        self.chooserscreen = box
        # Create the standard activity toolbox; add our toolbar
        self.entry = gtk.Entry()
        self.entry.connect("activate", self.set_smil)
        self.entry.show()
        selectTB = SelectToolBar(self.chooser, self.play_cb, self.entry, self.choosebundle, self.destroy)
        self.toolbox = activity.ActivityToolbox(self)
        self.toolbox.add_toolbar("Select", selectTB)
        # Open with slideshow toolbar
        self.toolbox.set_current_toolbar(SELECTTOOLBAR)
        self.toolbox.show()
        self.set_toolbox(self.toolbox)
        # layout the player screen
        box = gtk.VBox()
        lbl = gtk.Label("Playerscreen")
        lbl.show()
        box.pack_start(lbl, False, False, 0)
        self.scrn = gtk.DrawingArea()
        box.pack_start(self.scrn, True, True, 0)
        self.playerscreen = box
        self.playerscreen.show_all()
        self.set_canvas(self.playerscreen)

    def flush_events(self):
        while gtk.events_pending():
            gtk.main_iteration()

    def destroy(self):
        self.exit = True

    def start(self):
        while self.exit == False:
            self.flush_events()

    def play_cb(self, widget):
        self.player.play()
        self.destroy()

    def choosebundle(self, widget, source, pth):
        treeview = self.chooser.get_treeView()
        treeview.set_model(self.chooser.set_store(source, pth))
        treeview.show()
        self.set_canvas(self.chooserscreen)
        self.chooserscreen.show_all()
        print 'choosebundle done'

    def set_smil(self, widget):
        print self.entry.get_text()
        pth = BASE / self.entry.get_text()
        print 'pth=', pth
        self.start_smil(pth)

    def read_file(self, file_path):
        print 'read_file:', file_path
        filepath = path(file_path)
        #set up bundle folder
        d = BUNDLEPATH
        if not d.exists:
            d.mkdir()
        else:
            for f in d.files('*.py'):
                f.remove()
        #move smil file and content to BUNDLEPATH
        if filepath.ext == '.png':
            self.doc = 'imagepng.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'image.png'
            path.copy(src, dst)
        elif filepath.ext == '.jpg' or filepath.ext == '.jpeg':
            self.doc = 'imagejpg.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'image.jpg'
            path.copy(src, dst)
        elif filepath.ext == '.svg':
            self.doc = 'imagesvg.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'image.svg'
            path.copy(src, dst)
        elif filepath.ext == '.gif':
            self.doc = 'imagegif.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'image.gif'
            path.copy(src, dst)
        elif filepath.ext == '.tiff':
            self.doc = 'imagetiff.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'image.tiff'
            path.copy(src, dst)
        elif filepath.ext == '.bmp':
            self.doc = 'imagebmp.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'image.bmp'
            path.copy(src, dst)
        elif filepath.ext == '.smxo':
            z = zipfile.ZipFile(file_path, "r")
            for i in z.infolist():
                f = open(os.path.join(BUNDLEPATH, i.filename), "wb")
                f.write(z.read(i.filename))
                if path(i.filename).ext == ".smil":
                    self.doc = i.filename
                f.close()
            z.close()
            print 'bundle unzipped'
            #convert src to absolute path
            f = open(BUNDLEPATH / self.doc, 'r')
            t = f.read()
            f.close()
            tc = t.replace('src = "', 'src = "' + BUNDLEPATH + '/')
            tcc = tc.replace('src="', 'src = "' + BUNDLEPATH + '/')
            f = open(BUNDLEPATH / self.doc, 'w')
            f.write(tcc)
            f.close()
            print 'smil=', tcc
        else:
            print 'unknown extension', filepath.ext, filepath
        #play smil document
        self.start_smil(BUNDLEPATH / self.doc)

    def start_smil(self, pth):
        self.set_canvas(self.playerscreen)
        self.playerscreen.show()
        self.player = ambulantglue.Glue(pth , self.scrn)
        self.start()

    def on_activated(self, widget, row, col):
        print 'smxo on_activated'
        model = widget.get_model()
        print 'row', model[row][0], model[row][1], model[row][2]
        title = model[row][1]
        timestamp = model[row][2]
        object = datastore.get(model[row][0])
        fn = object.file_path
        print 'object filename', path(fn).exists(), fn
        #open slideshow, set Navigation toolbar current
        self.read_file(path(fn))

class SelectToolBar(gtk.Toolbar):

    def __init__(self, chooser, play_cb, entry, choosebundle, destroy):
        gtk.Toolbar.__init__(self)
        self.chooser = chooser

        #get mount points
        ds_mounts = datastore.mounts()
        pendrive = -1
        for i in range(0, len(ds_mounts), 1):
            print 'mount', i, ds_mounts[i]['uri'], ds_mounts[i]['title'], ds_mounts[i]['id']
            if ds_mounts[i]['uri'].find('datastore') > 0:
                journal = i
            else:
                pendrive = i

        self.journalbtn = ToolButton('activity-journal')
        self.journalbtn.set_tooltip("Choose bundle")
        self.journalbtn.connect('clicked', choosebundle, ds_mounts[journal]['id'], DATASTORE)
        self.insert(self.journalbtn, -1)
        self.journalbtn.show()

        #show pendrive button only if pendrive is mounted
        if pendrive > -1:
            self.pendrivebutton = ToolButton('media-flash-usb')
            self.pendrivebutton.set_tooltip("Choose bundle")
            self.pendrivebutton.connect('clicked', choosebundle, ds_mounts[pendrive]['id'], ds_mounts[pendrive]['title'])
            self.insert(self.pendrivebutton, -1)
            self.pendrivebutton.show()
        self.show()

        self.playbutton = ToolButton('gtk-media-play')
        self.playbutton.connect('clicked', play_cb)
        self.playbutton.show()
        self.insert(self.playbutton, -1)

        self.toolitem = gtk.ToolItem()
        self.toolitem.set_expand(True)
        self.toolitem.show()
        self.toolitem.add(entry)
        self.insert(self.toolitem, -1)

        self.quitbutton = ToolButton('ooops')
        self.quitbutton.connect('clicked', destroy)
        self.quitbutton.show()
        self.insert(self.quitbutton, -1)

    def choosebundle(self, widget, source, pth):
        treeview = self.chooser.get_treeView()
        treeview.set_model(self.chooser.set_store(source, pth))
        print 'choosebundle done'

class Smxoview(gtk.VBox):
    def __init__(self, on_activated):
        print 'smxoview init'
        gtk.VBox.__init__(self)
        lbl = gtk.Label("smxoview")
        lbl.show()
        self.pack_start(lbl, False, False, 0)
        sw = gtk.ScrolledWindow()
        sw.set_shadow_type(gtk.SHADOW_ETCHED_IN)
        sw.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
        self.treeView = gtk.TreeView()
        self.treeView.connect("row-activated", on_activated)
        self.treeView.set_rules_hint(True)
        sw.add(self.treeView)
        self.create_columns(self.treeView)
        sw.show()
        self.pack_start(sw, True, True, 0)

    def create_columns(self, treeView):

        rendererText = gtk.CellRendererText()
        column = gtk.TreeViewColumn("Title", rendererText, text=1)
        column.set_sort_column_id(1)
        treeView.append_column(column)
        rendererText = gtk.CellRendererText()
        column = gtk.TreeViewColumn("Date", rendererText, text=2)
        column.set_sort_order(gtk.SORT_DESCENDING)
        column.set_sort_column_id(2)
        treeView.append_column(column)

    def get_treeView(self):
        return self.treeView

    def set_store(self, mountpoint, pth):
        print 'set_store', mountpoint, pth
        store = gtk.ListStore(str, str, str)
        #get objects from the local datastore
        media_list = []
        media_list.append('application/x-smile')
        media_list.append('image/png')
        media_list.append('image/jpeg')
        media_list.append('image/svg+xml')
        media_list.append('image/bmp')
        media_list.append('image/gif')
        media_list.append('image/tiff')
        ds_objects, num_objects = datastore.find({'mountpoints':[mountpoint], 'mime_type': media_list})
        for f in ds_objects:
            object = f.object_id
            title = f.metadata['title']
            try:
                t = f.metadata['timestamp']
            except:
                t = 0
            #timestamp = datetime.fromtimestamp(t)
            timestamp = t
            store.append([object, title, timestamp])
            f.destroy()
        print 'return smxo store', len(store)
        return store