"""
$Id: mythtvupcoming.py,v 1.8 2007/05/17 01:43:02 frooby Exp $
Copyright (C) 2005 Tom Warkentin <tom@ixionstudios.com>
"""
from datetime import datetime
from mythtvgui import Dialog
from singleton import getInstance
from time import strftime,strptime
import mythtv
import mythtvgui
import mythtvutil
import os
import sre
import time
import traceback
import xbmcgui
def debug( str ):
mythtvutil.debug( mythtvutil.DEBUG_GUI, str )
def compareDateAsc( x, y ):
if x.starttime() == y.starttime():
return cmp( x.chanid(), y.chanid() )
else:
return cmp( x.starttime(), y.starttime() )
def compareDateDesc( y, x ):
if x.starttime() == y.starttime():
return cmp( x.chanid(), y.chanid() )
else:
return cmp( x.starttime(), y.starttime() )
def compareTitleAsc( x, y ):
if x.title() == y.title():
return cmp( x.starttime(), y.starttime() )
else:
return cmp( x.title(), y.title() )
def compareTitleDesc( y, x ):
if x.title() == y.title():
return cmp( x.starttime(), y.starttime() )
else:
return cmp( x.title(), y.title() )
def showWindow(winDialog):
"""
Function to create the recorded show window.
"""
debug( "> mythtvupcoming.showWindow()" )
mythtvgui.checkSettings()
win = Window()
win.loadskin( "upcomingshows.xml" )
win.loadList()
winDialog.close()
win.doModal()
del win
debug( "< mythtvupcoming.showWindow()" )
class Window( mythtvgui.BaseWindow ):
VIEW_BY_MIN = 0 # must be smallest sequential value
VIEW_BY_DATE_ASC = 1
VIEW_BY_DATE_DESC = 2
VIEW_BY_TITLE_ASC = 3
VIEW_BY_TITLE_DESC = 4
VIEW_BY_MAX = 5 # must be largest sequential value
def __init__( self ):
mythtvgui.BaseWindow.__init__( self )
self.recordings = []
self.focusControl = None
self.viewByLabel = { \
self.VIEW_BY_DATE_ASC: 38, \
self.VIEW_BY_TITLE_ASC: 39, \
self.VIEW_BY_DATE_DESC: 69, \
self.VIEW_BY_TITLE_DESC: 70 \
}
try:
self.viewBy = \
int(getInstance( mythtv.Settings ).getSetting( \
"upcoming_view_by" ))
except IndexError, ie:
self.viewBy = self.VIEW_BY_DATE_DESC
getInstance( mythtv.Settings ).setSetting( \
"upcoming_view_by", \
"%d"%self.viewBy, \
shouldCreate=1 )
pass
def buildListEntry( self, row ):
debug( '> mythtvupcoming.Window.buildListEntry()' )
entry = strftime("%a %d/%m %I:%M%p",time.localtime(float(row.recstarttime())))
if row.cardid():
entry += " - Encoder %s" % row.cardid()
if row.channame():
entry += " - %s" % row.channame()
if row.title():
entry += " - %s"%row.title()
if row.subtitle() and not sre.match( '^\s+$', row.subtitle() ):
entry += "-%s" %row.subtitle()
# start = strptime( row.starttime(), "%Y%m%d%H%M%S" )
# entry = strftime( "%m/%d %I:%M %p", start ) + " "
# if row.title():
# entry += row.title()
# if row.subtitle() and not sre.match( '^\s+$', row.subtitle() ):
# entry += " - " + row.subtitle()
#
debug( '< mythtvupcoming.Window.buildListEntry()' )
return entry
def loadList( self ):
debug( '> mythtvupcoming.Window.loadList()' )
c = getInstance( mythtv.Connection )
recordings = c.getPendingRecordings()
if self.viewBy == self.VIEW_BY_DATE_ASC:
recordings.sort( compareDateAsc )
elif self.viewBy == self.VIEW_BY_DATE_DESC:
recordings.sort( compareDateDesc )
elif self.viewBy == self.VIEW_BY_TITLE_ASC:
recordings.sort( compareTitleAsc )
elif self.viewBy == self.VIEW_BY_TITLE_DESC:
recordings.sort( compareTitleDesc )
ctl = self.controls["show_list"].control
xbmcgui.lock()
try:
self.controls["view_by"].control.setLabel( \
mythtvutil.getLocalizedString( self.viewByLabel[self.viewBy] ) )
ctl.reset()
self.recordings = []
now = datetime.now().strftime( "%Y%m%d%H%M%S" )
for r in recordings:
if r.endtime() >= now:
ctl.addItem( self.buildListEntry( r ) )
self.recordings.append( r )
if len( self.recordings ) > 0:
self.populateShowDetails( 0 )
xbmcgui.unlock()
except:
xbmcgui.unlock()
raise
try:
freeSpace = getInstance( "mythtv.Connection" ).getFreeSpace()
self.controls['free_space'].control.setLabel( freeSpace[0] )
except Exception, ex:
self.controls['free_space'].control.setLabel( \
mythtvutil.getLocalizedString( 45 ) )
debug( '< mythtvupcoming.Window.loadList()' )
def onActionHook( self, action ):
debug( "> mythtvupcoming.Window.onActionHook( action=[%s] )"%action )
self.focusControl = self.getFocus()
if action == mythtvgui.ACTION_PREVIOUS_MENU:
getInstance( mythtv.Settings ).saveSettings()
debug( "< mythtvupcoming.Window.onActionHook()" )
return 0
def onActionPostHook( self, action ):
debug( "> mythtvupcoming.Window.onActionPostHook()" )
# check if the action was to move up or down
if action == mythtvgui.ACTION_MOVE_UP or \
action == mythtvgui.ACTION_MOVE_DOWN or \
action == mythtvgui.ACTION_SCROLL_UP or \
action == mythtvgui.ACTION_SCROLL_DOWN:
# check if the control in focus is the show list
id = self.getcontrolid( self.focusControl )
if id == "show_list":
# give gui time to update
time.sleep( 0.10 )
# get selected show and populate details
self.populateShowDetails(
self.focusControl.getSelectedPosition() )
debug( "< mythtvupcoming.Window.onActionPostHook()" )
def onControlHook( self, control ):
debug( "> mythtvupcoming.Window.onControlHook()" )
id = self.getcontrolid( control )
if id == "view_by":
self.viewBySelected()
elif id == "show_list":
self.showSelected( control )
elif id == "refresh":
self.loadList()
debug( "< mythtvupcoming.Window.onControlHook()" )
return 1
def populateShowDetails( self, pos ):
debug( "> mythtvupcoming.Window.populateShowDetails( pos=%d)"%pos )
if pos < len( self.recordings ):
row = self.recordings[pos]
# populate title
self.controls['show_title'].control.reset()
title = row.fullTitle()
self.controls['show_title'].control.addLabel( title )
debug( "show_title=[%s]"%title )
# populate air date
self.controls['show_air_date'].control.setLabel( \
row.formattedAirDate() )
debug( "show_air_date=[%s]"%row.formattedAirDate() )
# populate channel
self.controls['show_channel'].control.setLabel( \
row.formattedChannel() )
debug( "show_channel=[%s]"%row.formattedChannel() )
# populate orig air
self.controls['show_orig_air'].control.setLabel( \
row.formattedOrigAirDate() )
debug( "show_orig_air=[%s]"%row.formattedOrigAirDate() )
# populate description
self.controls['show_descr'].control.reset()
self.controls['show_descr'].control.addLabel( \
row.formattedDescription() )
debug( "show_descr=[%s]"%row.formattedDescription() )
# populate notes
self.controls['show_notes'].control.reset()
self.controls['show_notes'].control.addLabel( \
row.translatedRecStatus() )
debug( "show_notes=[%s]"%row.translatedRecStatus() )
debug( "< mythtvupcoming.Window.populateShowDetails( pos=%d)"%pos )
def showSelected( self, control ):
debug( "> mythtvupcoming.Window.showSelected()" )
pos = control.getSelectedPosition()
if pos < len( self.recordings ):
#rc = mythtvrecordedshowdetails.showWindow(self.recordings[pos])
rc = 0
# refresh list if show was deleted
if rc == 1:
self.loadList()
debug( "< mythtvupcoming.Window.showSelected()" )
def viewBySelected( self ):
debug( "> mythtvupcoming.Window.viewBySelected()" )
# switch to next view by
self.viewBy += 1
if self.viewBy >= self.VIEW_BY_MAX:
self.viewBy = self.VIEW_BY_MIN+1
# store the setting change
getInstance( mythtv.Settings ).setSetting( \
"upcoming_view_by", \
"%d"%self.viewBy, \
shouldCreate=1 )
# refresh the listing
self.loadList()
debug( "< mythtvupcoming.Window.viewBySelected()" )
if __name__ == "__main__":
try:
loadingWin = xbmcgui.DialogProgress()
loadingWin.create("Upcoming Shows","Loading Upcoming Shows","Please Wait...")
mythtvutil.debugSetLevel( mythtvutil.DEBUG_GUI | mythtvutil.DEBUG_MYTH )
mythtvutil.initialize()
showWindow(loadingWin)
except Exception, ex:
traceback.print_exc()
Dialog().ok( mythtvutil.getLocalizedString( 27 ), str( ex ) )
|