Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/measure.py
blob: 5788a8e8b520db0dac5ebd9a1e0d267f9ca49ae6 (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
#!/usr/bin/python
#
#    Written by Arjun Sarwal <arjun@laptop.org>
#    Copyright (C) 2007, Arjun Sarwal
#    Copyright (C) 2009, Walter Bender
#    Copyright (C) 2009, Benjamin Berg, Sebastian Berg
#    	
#    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., 675 Mass Ave, Cambridge, MA 02139, USA.


import pygst
pygst.require("0.10")
import pygtk
import gtk
import gobject
import time
import dbus
import config 		#This has all the globals
import os, sys
import tempfile
from os import environ
from os.path import join

from journal import JournalInteraction
import audiograb
from drawwaveform import DrawWaveform
from toolbar_side import SideToolbar
from toolbar_top import Toolbar
from textbox import TextBox

from sugar.activity import activity
from sugar.datastore import datastore

# Initialize logging.
import logging
log = logging.getLogger('Measure')
log.setLevel(logging.DEBUG)
logging.basicConfig()

def _get_hardware():
    # This is so horribly convoluted :-(
    bus = dbus.SystemBus()
 
    comp_obj = bus.get_object('org.freedesktop.Hal', \
            '/org/freedesktop/Hal/devices/computer')
    dev = dbus.Interface (comp_obj, 'org.freedesktop.Hal.Device')
    if dev.PropertyExists('system.hardware.vendor') and \
            dev.PropertyExists('system.hardware.version'):
        if dev.GetProperty ('system.hardware.vendor') == 'OLPC':
            if dev.GetProperty('system.hardware.version') == '1.5':
                return 'xo1.5'
            elif dev.GetProperty('system.hardware.version') == '1.0':
                return 'xo1'
    elif 'olpc' in dev.GetProperty('system.kernel.version'): # this is not good
        return 'xo1'
    else:
        return 'unknown'

class MeasureActivity(activity.Activity):

    def __init__(self, handle):

        activity.Activity.__init__(self, handle)

        try:
            tmp_dir = os.path.join(activity.get_activity_root(), "data")
        except:
            # Early versions of Sugar (e.g., 656) didn't support
            # get_activity_root()
            tmp_dir = os.path.join( \
                os.environ['HOME'], \
                ".sugar/default/org.laptop.MeasureActivity/data")

        self.active_status = True
        self.ACTIVE = True
        self.connect( "notify::active", self._activeCb )
        self.connect("destroy", self.on_quit)	

        if self._jobject.file_path:
	        #logging.debug('1.0 Launched from journal')
	        self.existing = True
        else: 
	        #logging.debug('1.1 Launched from frame or from Mesh View')
	        self._jobject.file_path =str(tempfile.mkstemp(dir=tmp_dir)[1]) 
	        os.chmod(self._jobject.file_path, 0777)
	        self.existing = False	

        self.ji = JournalInteraction(self._jobject.file_path, self.existing)
        self.wave = DrawWaveform()
        
        hw = _get_hardware()
        if hw == 'xo1.5':
            self.audiograb = \
                audiograb.AudioGrab_XO_1_5(self.wave.new_buffer, self.ji)
        elif hw == 'xo1':
            self.audiograb = \
                audiograb.AudioGrab_XO_1(self.wave.new_buffer, self.ji)
        else:
            self.audiograb = \
                audiograb.AudioGrab_XO_1(self.wave.new_buffer, self.ji)
            # log.error('Sorry, we do not support your hardware yet.')

        self.side_toolbar = SideToolbar(self.wave)
        self.text_box = TextBox()

        self.box3 = gtk.HBox(False, 0)
        self.box3.pack_start(self.wave,True,True,0)
        self.box3.pack_start(self.side_toolbar.box1,False,True,0)	

        self.box1 = gtk.VBox(False,0)
        self.box1.pack_start(self.box3, True, True, 0)
        self.box1.pack_start(self.text_box.box_main, False, True, 0)

        self.set_canvas(self.box1)		

        toolbox = Toolbar(self, self.wave, self.audiograb, self.ji, \
                          self.text_box)
        self.set_toolbox(toolbox)
        toolbox.show()

        self.show_all()		

        self.first = True

        self.wave.set_active(True)

    def set_show_hide_windows(self, window_id=1):
        """Shows the appropriate window identified by the window_id
        1 --> sound
        2 --> sensors
        """
        if window_id==1: 
            #self.box3.pack_start(self.wave,True,True,0)
            #self.box3.pack_start(self.side_toolbar.box1,False,True,0)	
            #self.box3.show_all()
            self.wave.set_context_on()
            self.side_toolbar.box1.show_all()
            self.active_context_status = 1
            return
	
        elif window_id==2:
            #self.box3.pack_start(self.wave,True,True,0)
            #self.box3.show_all()
            self.wave.set_context_on()
            self.side_toolbar.box1.hide_all()
            self.active_context_status = 2
            return

    def get_show_hide_windows(self):
        """Gets which window is being shown
        If 0 is returned means waveform window is being shown
        If 1 is returned means camera context"""
        return self.active_context_status

    def on_quit(self,data=None):	
        self.audiograb.on_activity_quit()	
        self.ji.on_quit()

    def _activeCb( self, widget, pspec ):
        if(self.first == True):
	        self.audiograb.start_grabbing()
	        self.first = False
        if (not self.props.active and self.ACTIVE):
	        self.audiograb.pause_grabbing()
	        self.active_status = False 
        elif (self.props.active and not self.ACTIVE):
	        self.audiograb.resume_grabbing()
	        self.active_status = True

        self.ACTIVE = self.props.active
        self.wave.set_active(self.ACTIVE)

    """
    Write the project to the Journal
    """
    def write_file(self, file_path):
        print "write file"

    """
    Read a project in and then run it
    """
    def read_file(self, file_path):
        print "read file"

gtk.gdk.threads_init()