SystemTrayIcon

From MythTV Official Wiki
Jump to: navigation, search

If you'd like a simple icon in your system tray / notification area that indicates whether mythtv is currently recording or not, then this python script is for you.

Now I always know whether it's okay to reboot or not, and when to expect my disk activity to be high.

(It's my first python script, so be kind. -- harvest316)


PythonIcon.png SystemTrayIcon

#!/usr/bin/python
"""Script to display a system-tray icon showing mythtv recording status

Based on code from:
	http://blog.vrplumber.com/scripts/recordingdevices.py
Which was based on code from:
	http://www.mythtv.info/moin.cgi/SystemLedsRecordingStatusHowTo

Change Log:
	12/12/08 Harvest316 removed sudo dependency by using mythtv-status instead of /dev/video*, and expanded tooltip
"""
import commands, re, time

def getRecordingStatus( ):
	"""Retrieve recording status from mythtv-status"""
	global mythtv_status_output
	mythtv_status_output = commands.getoutput('nice -19 mythtv-status --auto-expire')

	patternprog = re.compile( "Recording Now:" )
	a_match = patternprog.search( mythtv_status_output )
	if ( a_match ):
		return "actively recording"
	

if __name__ == "__main__":
	import wx
	class RDApp( wx.App ):
		duration = 60 # seconds
		def OnInit( self ):
			"""Initialise, kicking off our operations"""
			self.frame = wx.Frame( None, -1, 'RecordingDevicesApp' )
			self.icon = wx.TaskBarIcon()
			wx.EVT_TASKBAR_LEFT_DCLICK( self.icon, self.OnClickIcon )
			class MyTimer( wx.Timer ):
				def Notify( self, event=None ):
					wx.GetApp().MainOperation(event)
			self.timer = MyTimer(  )
			
			# fire every self.duration seconds 
			self.timer.Start( self.duration * 1000, False ) 
			self.frame.Show( False )
			self.SetTopWindow( self.frame )
			self.MainOperation(None)
			return True
		def MainOperation( self, event ):
			"""Check every few seconds for current status"""
			self.setStatus( getRecordingStatus() )
		def setStatus( self, recording ):
			"""Set the icon's current representation"""
			width = 14
			bitmap = wx.EmptyBitmap(width, width)
			dc = wx.MemoryDC( )
			dc.SelectObject( bitmap )
			try:
				dc.SetBackground( wx.BLUE_BRUSH )
				dc.Clear()
				if recording:
					brush = wx.RED_BRUSH
				else:
					brush = wx.GREEN_BRUSH
				dc.SetBrush( brush )
				dc.DrawCircle( width/2,width/2,width/2-2 )
			finally:
				dc.SelectObject( wx.NullBitmap )
			bitmap.SetMaskColour( wx.BLUE )
			icon = wx.IconFromBitmap(bitmap)
			self.icon.SetIcon( icon, mythtv_status_output )
		def OnClickIcon( self, event ):
			"""Handle double-click of icon (close)"""
			self.icon.RemoveIcon()
			self.frame.Close()

	app = RDApp()
	app.MainLoop()