Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/smile.py
blob: 81a56c92f625f0500c3ffeca93c021178e0b4d1c (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
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#!/usr/bin/env python

# test ambulantglue

import os, sys, time
import subprocess
from datetime import datetime
from time import strftime

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

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'
DATEFORMAT = "%Y-%m-%d %H:%M:%S"
SELECTTOOLBAR = 1

class SmileActivity(activity.Activity):

    def __init__ (self, handle):
        activity.Activity.__init__(self, handle)
        self.set_title("Smile")
        self.connect("destroy", self.set_exit)
        self.readflag = False
        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.activate_cb)
        self.entry.show()
        selectTB = SelectToolBar(self)
        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()
        self.scrn.show()
        box.pack_start(self.scrn, True, True, 0)
        self.playerscreen = box
        self.playerscreen.show_all()
        self.current_screen = self.playerscreen
        self.screenbox = gtk.VBox()
        self.screenbox.pack_start(self.current_screen, True, True, 0)
        self.screenbox.show_all()
        print 'set_canvas'
        self.doc = 'no doc'
        self.set_canvas(self.screenbox)
        print 'canvas_set', self.doc

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

    def set_exit(self, widget):
        #self.exit = True
        print 'set_exit', widget

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

    def start_smil(self, pth):
        print 'start smil'
        self.set_screen(self.playerscreen)
        print 'playerscreen set'
        self.player = ambulantglue.Glue(pth , self.scrn)
        print 'self.player', type(self.player)

    def set_screen(self, screen):
        if not self.current_screen == screen:
            self.screenbox.remove(self.current_screen)
            self.current_screen = screen
            self.current_screen.show_all()
            self.screenbox.pack_start(self.current_screen, True, True, 0)

    def play_cb(self, widget):
        #self.start_smil(BUNDLEPATH / self.doc)
        #print 'smil started in play_cb', self.doc
        print 'play_cb'
        if self.player:
            self.player.play()

    def pause_cb(self, widget):
        print 'pause_cb'
        if self.player:
            self.player.pause()

    def stop_cb(self, widget):
        print 'stop_cb'
        if self.player:
            self.player.stop()

    def activate_cb(self, widget):
        #ignore
        pass

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

    def read_file(self, file_path):
        print 'read_file:', file_path, self.readflag
        if self.readflag:
            print 'read_file called twice'
        else:
            self.readflag = True
        filepath = path(file_path)
        pos = file_path.find('(')
        pos1 = file_path[pos:].find(')')
        filepath = path(file_path[:pos] + file_path[pos+pos1+1:])
        print 'filepath', path(file_path).name, filepath.name
        #set up bundle folder
        d = BUNDLEPATH
        if not d.exists:
            d.mkdir()
        else:
            for f in d.files():
                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 == '.ogg' or filepath.ext == '.oga':
            self.doc = 'audioogg.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'audio.ogg'
            path.copy(src, dst)
        elif filepath.ext == '.m4a':
            self.doc = 'audiom4a.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'audio.m4a'
            path.copy(src, dst)
        elif filepath.ext == '.mp3':
            self.doc = 'audiomp3.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'audio.mp3'
            path.copy(src, dst)
        elif filepath.ext == '.wav':
            self.doc = 'audiowav.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'audio.wav'
            path.copy(src, dst)
        elif filepath.ext == '.wma':
            self.doc = 'audiowma.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'audio.wma'
            path.copy(src, dst)
        elif filepath.ext == '.mpg' or filepath.ext == '.mpeg':
            self.doc = 'videompg.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'video.mpg'
            path.copy(src, dst)
        elif filepath.ext == '.mov':
            self.doc = 'videomov.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'video.mov'
            path.copy(src, dst)
        elif filepath.ext == '.wmv':
            self.doc = 'videowmv.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'video.wmv'
            path.copy(src, dst)
        elif filepath.ext == '.avi':
            self.doc = 'videoavi.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'video.avi'
            path.copy(src, dst)
        elif filepath.ext == '.ogv':
            self.doc = 'videoogg.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'video.ogg'
            path.copy(src, dst)
        elif filepath.ext == '.mp4':
            self.doc = 'videomp4.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'video.mp4'
            path.copy(src, dst)
        elif filepath.ext == '.flv':
            self.doc = 'videoflv.smil'
            src = SMILPATH / self.doc
            dst = BUNDLEPATH
            path.copy(src, dst)
            src = filepath
            dst = BUNDLEPATH / 'video.flv'
            path.copy(src, dst)
        elif filepath.ext == ".txt":
            self.doc = 'text.smil'
            src = SMILPATH / 'textpln.smil'
            f = open(src, 'r')
            skeleton = f.read()
            f.close()
            f = open(filepath, 'r')
            txt = f.read()
            f.close()
            pos1 = skeleton.find('*****')
            pos2 = skeleton[pos1+5:].find('*****')
            tsmil = skeleton[:pos1] + txt + skeleton[pos1+pos2+9:]
            dst = BUNDLEPATH / 'text.smil'
            f = open(dst, 'w')
            f.write(tsmil)
            f.close()
        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.entry.set_text(filepath.name)
        print 'ready to play', self.doc, filepath.name
        self.start()
        self.start_smil(BUNDLEPATH / self.doc)
        print 'flush events started, calling play_cb'
        self.play_cb(self.doc)

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

class SelectToolBar(gtk.Toolbar):

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

        #get mount points
        ds_mounts = datastore.mounts()
        pendrive = -1
        for i in range(0, len(ds_mounts), 1):
            print 'mount', i, ds_mounts[i]
            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', activity.play_cb)
        self.playbutton.show()
        self.insert(self.playbutton, -1)

        self.pausebutton = ToolButton('gtk-media-pause')
        self.pausebutton.connect('clicked', activity.pause_cb)
        self.pausebutton.show()
        self.insert(self.pausebutton, -1)

        self.stopbutton = ToolButton('gtk-media-stop')
        self.playbutton.connect('clicked', activity.stop_cb)
        self.stopbutton.show()
        self.insert(self.stopbutton, -1)

        activity.stopbutton = self.stopbutton
        activity.pausebutton = self.pausebutton
        activity.playbutton = self.playbutton

        self.toolitem = gtk.ToolItem()
        self.toolitem.set_expand(True)
        self.toolitem.show()
        self.toolitem.add(entry)
        self.insert(self.toolitem, -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("Media", rendererText, text=2)
        column.set_sort_column_id(2)
        treeView.append_column(column)
        rendererText = gtk.CellRendererText()
        column = gtk.TreeViewColumn("Date", rendererText, text=3)
        column.set_sort_order(gtk.SORT_DESCENDING)
        column.set_sort_column_id(3)
        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, str)
        #get objects from the local datastore
        media_list = []
        media_list.append('application/x-smile')
        media_list.append('application/zip')
        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')
        media_list.append('audio/mp4')
        media_list.append('video/x-flv')
        media_list.append('application/ogg')
        media_list.append('audio/ogg')
        media_list.append('video/ogg')
        media_list.append('audio/mpeg')
        media_list.append('audio/x-wav')
        media_list.append('audio/x-ms-wma')
        media_list.append('video/mpeg')
        media_list.append('video/quicktime')
        media_list.append('video/x-ms-wmv')
        media_list.append('video/x-msvideo')
        media_list.append('video/mp4')
        media_list.append('text/plain')
        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']
            mime_type = f.metadata['mime_type']
            date = f.metadata['mtime'].replace('T',' ') 
            if not mime_type == 'application/zip' or path(f).ext == '.smxo':
                store.append([object, title, mime_type, date])
            f.destroy()
        print 'return smxo store', len(store)
        return store