Active Topics

 



Notices


Reply
Thread Tools
Posts: 74 | Thanked: 34 times | Joined on Jan 2008
#1
my small contrib to the community
This will check the calender for meetings and when a meeting takes place will switch the profile to silent.
when the meeting ends it will switch the profile back to general

code is GPL

Code:
#!/usr/bin/run-standalone.sh python2.5

import sys, traceback, gobject, dbus, dbus.mainloop.glib ,os , subprocess 
import time, sqlite3,mafw
sys.path.insert(0, '/usr/lib/switchProfByMeeting')



sys.path.insert(0, '/home/user')
dbus.mainloop.glib.DBusGMainLoop(set_as_default=True)

class backend:
	def __init__( self ):

		self.bus = dbus.SessionBus()
		self.sleep_duration = 10000
		self.timeout_id = None
		self.loop = None
		
		try:
			self.profiled_object = self.bus.get_object( 'com.nokia.profiled', '/com/nokia/profiled' )
			self.calender_object = self.bus.get_object( 'com.nokia.calendar', '/com/nokia/calendar' )
			self.calender_object.connect_to_signal( 'dbChange', \
			self.calender_changed, dbus_interface = 'com.nokia.calendar' )
			self.mafw_object = self.bus.get_object( \
			'com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer' \
			,'/com/nokia/mafw/renderer/gstrenderer' )
		except dbus.DBusException:
			traceback.print_exc()
			sys.exit(1)

		self.start()
		
	def set_profile( self, prof ):

		#save speaker volume as profile change effects it
		volume_level = self.mafw_object.get_extension_property( 'volume' \
		, dbus_interface = 'com.nokia.mafw.extension')[1]
		self.profiled_object.set_profile(prof, dbus_interface='com.nokia.profiled')
		#restore volume level due to profile change
		retcode = subprocess.call("dbus-send --type=method_call \
		--dest=com.nokia.mafw.renderer.Mafw-Gst-Renderer-Plugin.gstrenderer \
		/com/nokia/mafw/renderer/gstrenderer \
		com.nokia.mafw.extension.set_extension_property \
		string:volume variant:uint32:%d"%volume_level, shell=True)
		if retcode != 0:
			raise SystemExit('got error code '+str(retcode)+' when setting volume')

	def is_meeting_now( self ):
		cal_db = sqlite3.connect(os.path.expanduser("~/.calendar/calendardb"))
		query = "SELECT Summary FROM Components WHERE ComponentType='1' \
		AND AllDay='0' AND strftime('%s','now') >= DateStart AND \
		strftime('%s','now') < DateEnd limit 1"
		ans = cal_db.execute(query).fetchone()

		return ans

	def when_next_meeting( self ):
		cal_db = sqlite3.connect(os.path.expanduser("~/.calendar/calendardb"))	
		query = "SELECT DateStart FROM Components WHERE ComponentType='1' \
		and AllDay='0' and strftime('%s','now') < DateStart AND \
		DateStart < DateEnd ORDER BY DateStart limit 1"
	#here is the place to make sure the meeting is leagal non zero duration and event 
		ans = cal_db.execute(query).fetchone()
		if ans != None:
			next_timeout = ans [0]
		else:
			next_timeout = None

		return next_timeout
		
	def when_end_of_meeting( self ):
		cal_db = sqlite3.connect(os.path.expanduser("~/.calendar/calendardb"))	
		query = "SELECT DateEnd FROM Components WHERE ComponentType='1' \
		and AllDay='0' and strftime('%s','now') < DateEnd and \
		DateStart < DateEnd ORDER BY DateEnd limit 1"
		#I also need to adress overlapping meetings that the other finishes later
		next_timeout = cal_db.execute(query).fetchone() [0]
		return next_timeout
		
	def calc_next_duration( self, next_meeting_time ):
		if next_meeting_time != None:
			self.sleep_duration = int ((next_meeting_time - time.time())*1000)
		else:
			self.sleep_duration = 5000
			
	def set_timer( self, current_profile ):
		if current_profile == 'general' :
			self.calc_next_duration( self.when_next_meeting() )
		else :
			self.calc_next_duration( self.when_end_of_meeting() )
			
	def calender_changed( self, arg1 , arg2 ):
		self.remove_timeout()
		self.update_profile()
		self.mainloop()
	
	def timer_callback( self ):
		self.remove_timeout()
		self.update_profile()
		self.mainloop()
		#commented by merlin 1991 advice self.mainloop()
			
	def remove_timeout( self ):
		if self.timeout_id != None:
			gobject.source_remove(self.timeout_id)

	def update_profile( self ):
		if self.is_meeting_now() :
			self.set_profile( 'silent' )
			self.set_timer( 'silent' )
		else:
			self.set_profile( 'general' )
			self.set_timer( 'general' )
			
	def mainloop( self ):
		self.timeout_id = gobject.timeout_add(self.sleep_duration, self.timer_callback )
		if self.loop == None :
			self.loop = gobject.MainLoop()
			self.loop.run()
		
	def start ( self ):
		''' update the profile and set a timer loop'''
		self.update_profile()
		self.mainloop()
		
	def stop_mainloop( self ):
		''' stop nain loop'''
		if self.loop != None and self.loop.is_running():
			self.loop.quit()
		
	def status( self ):
		'''check the status of the switch backend'''
		if self.loop != None:
			status = self.loop.is_running()
		else:
			status = None
		return status

if __name__ == "__main__":
	backend = backend()
Niv Vaizer
nivw2008 at fastmail dot fm

Last edited by niv; 2011-05-19 at 16:05. Reason: new version
 

The Following 4 Users Say Thank You to niv For This Useful Post:
joerg_rw's Avatar
Posts: 2,222 | Thanked: 12,651 times | Joined on Mar 2010 @ SOL 3
#2
apt-get install python-mafw (extras-devel)

http://maemo.org/packages/view/python-mafw/ + dpkg -i
__________________
Maemo Community Council member [2012-10, 2013-05, 2013-11, 2014-06 terms]
Hildon Foundation Council inaugural member.
MCe.V. foundation member

EX Hildon Foundation approved
Maemo Administration Coordinator (stepped down due to bullying 2014-04-05)
aka "techstaff" - the guys who keep your infra running - Devotion to Duty http://xkcd.com/705/

IRC(freenode): DocScrutinizer*
First USB hostmode fanatic, father of H-E-N
 
Posts: 569 | Thanked: 462 times | Joined on Jul 2010 @ USA
#3
This is interesting. Is it for the N900?
I didn't see a way to designate an event type as 'Meeting' in the N900 calendar.
 
Reply


 
Forum Jump


All times are GMT. The time now is 19:48.