init commit
[profile-appoint] / switch_backend.py
1 #!/usr/bin/run-standalone.sh python2.5
2
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')
7 import debug
8 debug = debug.debug
9
10 sys.path.insert(0, '/home/user')
11 dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)
12
13 class backend:
14         def __init__( self ):
15                 #debug( 'init' )
16                 self.bus = dbus.SessionBus()
17                 self.sleep_duration = 10000
18                 self.timeout_id = None
19                 self.loop = None
20                 
21                 try:
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:
30                         traceback.print_exc()
31                         sys.exit(1)
32
33                 self.start()
34
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)
47                 if retcode != 0:
48                         raise SystemExit('got error code '+str(retcode)+' when setting volume')
49
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) )
57                 return ans
58
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()
66                 if ans != None:
67                         next_timeout = ans [0]
68                 else:
69                         next_timeout = None
70                 debug( 'when_next_meeting returns: '+str(next_timeout) )
71                 return next_timeout
72                 
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) )
81                 return next_timeout
82                 
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)
86                 else:
87                         self.sleep_duration = 5000
88                 debug("calc_next_duration returns: "+str(self.sleep_duration))
89                         
90         def set_timer( self, current_profile ):
91                 if current_profile == 'general' :
92                         self.calc_next_duration( self.when_next_meeting() )
93                 else :
94                         self.calc_next_duration( self.when_end_of_meeting() )
95                         
96         def calender_changed( self, arg1 , arg2 ):
97                 debug('calender_changed called with arg1: '+arg1+' arg2: '+arg2 )
98                 self.remove_timeout()
99                 self.update_profile()
100                 self.mainloop()
101                 debug ('end of calender_changed')
102                 
103         def timer_callback( self ):
104                 debug('timer_callback timeout_id: '+str(self.timeout_id))
105                 self.remove_timeout()
106                 self.update_profile()
107                 self.mainloop()
108                 #commented by merlin 1991 advice self.mainloop()
109                 debug('end of timer_callback')
110                         
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) )
116
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' )
122                 else:
123                         self.set_profile( 'general' )
124                         self.set_timer( 'general' )
125                         
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) )
130
131                 if self.loop == None :
132                         self.loop = gobject.MainLoop()
133                         self.loop.run()
134                         debug('new loop started')
135                 debug ('end of mainloop')
136                 
137         def start ( self ):
138                 ''' update the profile and set a timer loop'''
139                 debug( 'starting' )
140                 self.update_profile()
141                 self.mainloop()
142                 
143         def stop_mainloop( self ):
144                 ''' stop nain loop'''
145                 debug( 'stopping main loop' )
146                 if self.loop != None and self.loop.is_running():
147                         self.loop.quit()
148                 
149         def status( self ):
150                 '''check the status of the switch backend'''
151                 if self.loop != None:
152                         status = self.loop.is_running()
153                 else:
154                         status = None
155                 debug( 'status is: '+status)
156                 return status
157
158 if __name__ == "__main__":
159         backend = backend()