1 #!/usr/bin/run-standalone.sh python2.5
3 # Import global modules
4 import sys, traceback, gobject, dbus, dbus.mainloop.glib ,os , subprocess
5 import time, sqlite3,mafw
6 sys.path.insert(0, '/usr/lib/switchProfByMeeting')
10 sys.path.insert(0, '/home/user')
11 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
16 self.bus = dbus.SessionBus()
17 self.sleep_duration = 10000
18 self.timeout_id = None
22 self.profiled_object = self.bus.get_object( 'com.nokia.profiled', '/com/nokia/profiled' )
23 self.calender_object = self.bus.get_object( 'com.nokia.calendar', '/com/nokia/calendar' )
24 self.calender_object.connect_to_signal( 'dbChange', \
25 self.calender_changed, dbus_interface = 'com.nokia.calendar' )
26 self.mafw_object = self.bus.get_object( \
27 'com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer' \
28 ,'/com/nokia/mafw/renderer/gstrenderer' )
29 except dbus.DBusException:
35 def set_profile( self, prof ):
36 debug ('in set_profile with '+prof)
37 #save speaker volume as profile change effects it
38 volume_level = self.mafw_object.get_extension_property( 'volume' \
39 , dbus_interface = 'com.nokia.mafw.extension')[1]
40 self.profiled_object.set_profile(prof, dbus_interface='com.nokia.profiled')
41 #restore volume level due to profile change
42 retcode = subprocess.call("dbus-send --type=method_call \
43 --dest=com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer \
44 /com/nokia/mafw/renderer/gstrenderer \
45 com.nokia.mafw.extension.set_extension_property \
46 string:volume variant:uint32:%d"%volume_level, shell=True)
48 raise SystemExit('got error code '+str(retcode)+' when setting volume')
50 def is_meeting_now( self ):
51 cal_db = sqlite3.connect(os.path.expanduser("~/.calendar/calendardb"))
52 query = "SELECT Summary FROM Components WHERE ComponentType='1' \
53 AND AllDay='0' AND strftime('%s','now') >= DateStart AND \
54 strftime('%s','now') < DateEnd limit 1"
55 ans = cal_db.execute(query).fetchone()
56 debug( 'is_meeting_now returns: '+str(ans) )
59 def when_next_meeting( self ):
60 cal_db = sqlite3.connect(os.path.expanduser("~/.calendar/calendardb"))
61 query = "SELECT DateStart FROM Components WHERE ComponentType='1' \
62 and AllDay='0' and strftime('%s','now') < DateStart AND \
63 DateStart < DateEnd ORDER BY DateStart limit 1"
64 #here is the place to make sure the meeting is leagal non zero duration and event
65 ans = cal_db.execute(query).fetchone()
67 next_timeout = ans [0]
70 debug( 'when_next_meeting returns: '+str(next_timeout) )
73 def when_end_of_meeting( self ):
74 cal_db = sqlite3.connect(os.path.expanduser("~/.calendar/calendardb"))
75 query = "SELECT DateEnd FROM Components WHERE ComponentType='1' \
76 and AllDay='0' and strftime('%s','now') < DateEnd and \
77 DateStart < DateEnd ORDER BY DateEnd limit 1"
78 #I also need to adress overlapping meetings that the other finishes later
79 next_timeout = cal_db.execute(query).fetchone() [0]
80 debug( 'when_end_of_meeting returns: ' +str(next_timeout) )
83 def calc_next_duration( self, next_meeting_time ):
84 if next_meeting_time != None:
85 self.sleep_duration = int ((next_meeting_time - time.time())*1000)
87 self.sleep_duration = 5000
88 debug("calc_next_duration returns: "+str(self.sleep_duration))
90 def set_timer( self, current_profile ):
91 if current_profile == 'general' :
92 self.calc_next_duration( self.when_next_meeting() )
94 self.calc_next_duration( self.when_end_of_meeting() )
96 def calender_changed( self, arg1 , arg2 ):
97 debug('calender_changed called with arg1: '+arg1+' arg2: '+arg2 )
101 debug ('end of calender_changed')
103 def timer_callback( self ):
104 debug('timer_callback timeout_id: '+str(self.timeout_id))
105 self.remove_timeout()
106 self.update_profile()
108 #commented by merlin 1991 advice self.mainloop()
109 debug('end of timer_callback')
111 def remove_timeout( self ):
112 debug ( 'remove timeout called' )
113 if self.timeout_id != None:
114 gobject.source_remove(self.timeout_id)
115 debug( 'removed timeout: '+str(self.timeout_id) )
117 def update_profile( self ):
118 debug ( 'begining of update_profile' )
119 if self.is_meeting_now() :
120 self.set_profile( 'silent' )
121 self.set_timer( 'silent' )
123 self.set_profile( 'general' )
124 self.set_timer( 'general' )
126 def mainloop( self ):
127 debug('going to run loop with sleep_duration: '+str(self.sleep_duration))
128 self.timeout_id = gobject.timeout_add(self.sleep_duration, self.timer_callback )
129 debug( ' new timeou_id is: '+str(self.timeout_id) )
131 if self.loop == None :
132 self.loop = gobject.MainLoop()
134 debug('new loop started')
135 debug ('end of mainloop')
138 ''' update the profile and set a timer loop'''
140 self.update_profile()
143 def stop_mainloop( self ):
144 ''' stop nain loop'''
145 debug( 'stopping main loop' )
146 if self.loop != None and self.loop.is_running():
150 '''check the status of the switch backend'''
151 if self.loop != None:
152 status = self.loop.is_running()
155 debug( 'status is: '+status)
158 if __name__ == "__main__":