home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 November / maximum-cd-2010-11.iso / DiscContents / xbmc-9.11.exe / scripts / AppleMovieTrailers / resources / lib / gui.py < prev    next >
Encoding:
Python Source  |  2009-11-03  |  52.8 KB  |  973 lines

  1. """
  2. Main GUI for Apple Movie Trailers
  3. """
  4.  
  5. import sys
  6. import xbmcgui
  7.  
  8. dialog = xbmcgui.DialogProgress()
  9. def _progress_dialog( count=0, msg="" ):
  10.     if ( count is None ):
  11.         dialog.create( __scriptname__ )
  12.     elif ( count > 0 ):
  13.         percent = int( count * ( float( 100 ) / ( len( modules ) + 1 ) ) )
  14.         dialog.update( percent, _( 50 ), _( 51 ), msg )
  15.     else:
  16.         dialog.close()
  17.     
  18. _ = sys.modules[ "__main__" ].__language__
  19. __scriptname__ = sys.modules[ "__main__" ].__scriptname__
  20. __version__ = sys.modules[ "__main__" ].__version__
  21. __svn_revision__ = sys.modules[ "__main__" ].__svn_revision__
  22.  
  23. try:
  24.     _progress_dialog( None )
  25.     modules = ( "os", "xbmc", "utilities", "trailers", "database", "context_menu", "cacheurl", "datetime", "urllib", )
  26.     for count, module in enumerate( modules ):
  27.         _progress_dialog( count + 1, "%s %s" % ( _( 52 ), module, ) )
  28.         if ( module == "utilities" ):
  29.             exec "from %s import *" % module
  30.         else:
  31.             exec "import %s" % module
  32. except Exception, e:
  33.     print str( e )
  34.     _progress_dialog( -1 )
  35.     xbmcgui.Dialog().ok( __scriptname__, _( 81 ) )
  36.     raise
  37.  
  38.  
  39. class GUI( xbmcgui.WindowXML ):
  40.     # control id's
  41.     CONTROL_TITLE_LABEL = 20
  42.     CONTROL_BUTTON_GROUP_START = 103
  43.     CONTROL_BUTTON_GROUP_END = 109
  44.     CONTROL_TRAILER_LIST_START = 50
  45.     CONTROL_TRAILER_LIST_END = 59
  46.     CONTROL_TRAILER_LIST_PAGE_START = 2050
  47.     CONTROL_TRAILER_LIST_PAGE_END = 2059
  48.     CONTROL_TRAILER_LIST_PAGE_GROUP_START = 2550
  49.     CONTROL_TRAILER_LIST_PAGE_GROUP_END = 2579
  50.     CONTROL_TRAILER_LIST_COUNT = 2150
  51.     CONTROL_CATEGORY_LIST = 60
  52.     CONTROL_CATEGORY_LIST_PAGE = 2060
  53.     CONTROL_CATEGORY_LIST_COUNT = 2160
  54.     CONTROL_CATEGORY_LIST_PAGE_GROUP = ( 2650, 2660, 2661, )
  55.     CONTROL_CAST_LIST = 70
  56.     CONTROL_CAST_LIST_PAGE = 2070
  57.     CONTROL_CAST_BUTTON = 170
  58.     CONTROL_PLOT_TEXTBOX = 75
  59.     CONTROL_PLOT_BUTTON = 175
  60.     CONTROL_TRAILER_POSTER = 201
  61.     CONTROL_OVERLAY_RATING = 202
  62.     CONTROL_OVERLAY_FAVORITE = 203
  63.     CONTROL_OVERLAY_WATCHED = 204
  64.     CONTROL_OVERLAY_SAVED = 205
  65.     CONTROL_TRAILER_TITLE_LABEL = 206
  66.     CONTROL_TRAILER_LIST_GROUP = 3000
  67.     CONTROL_CATEGORY_LIST_GROUP = 4000
  68.     
  69.     def __init__( self, *args, **kwargs ):
  70.         xbmcgui.WindowXML.__init__( self, *args, **kwargs )
  71.         self.startup = True
  72.         ##Enable once we figure out why it crashes sometimes#################################
  73.         ##self.videoplayer_resolution = int( xbmc.executehttpapi( "getguisetting(0,videoplayer.displayresolution)" ).replace("<li>","") )
  74.         ######################################################################
  75.         ##self.Timer = None
  76.         self._get_settings()
  77.         self._get_showtimes_scraper()
  78.         self._get_custom_sql()
  79.         self._setup_variables()
  80.         self._set_startup_choices()
  81.  
  82.     def onInit( self ):
  83.         self._set_shortcut_properties()
  84.         self._set_info_properties()
  85.         if ( self.startup ):
  86.             self.startup = False
  87.             self.getControl( self.CONTROL_TRAILER_LIST_GROUP ).setVisible( False )
  88.             self._set_startup_category()
  89.             if ( INSTALL_PLUGIN ):
  90.                 install_plugin( plugin=range( 0, 2 ), message=True)
  91.         else:
  92.             if ( self.trailer >= 0 ):
  93.                 self.markAsWatched( self.trailers.movies[ self.trailer ].watched + 1, self.trailer )            
  94.             self.showTrailers( self.sql, self.params, self.trailer_pos, 2 )
  95.  
  96.     def _get_settings( self ):
  97.         self.settings = Settings().get_settings()
  98.  
  99.     def _get_showtimes_scraper( self ):
  100.         sys.path.append( os.path.join( BASE_RESOURCE_PATH, "showtimes_scrapers", self.settings[ "showtimes_scraper" ] ) )
  101.  
  102.     def _get_custom_sql( self ):
  103.         self.search_sql = get_custom_sql()
  104.  
  105.     def _set_video_resolution( self, default=False ):
  106.         """
  107.         if ( self.settings[ "videoplayer_displayresolution" ] != 10 and not default ):
  108.             # set the videoplayers resolution to AMT setting
  109.             xbmc.executehttpapi( "SetGUISetting(0,videoplayer.displayresolution,%d)" % ( self.settings[ "videoplayer_displayresolution" ], ) )
  110.         else:
  111.             # set the videoplayers resolution back to XBMC setting
  112.             xbmc.executehttpapi( "SetGUISetting(0,videoplayer.displayresolution,%d)" % ( self.videoplayer_resolution, ) )
  113.         """
  114.         pass
  115.  
  116.     def _setup_variables( self ):
  117.         self.trailers = trailers.Trailers()
  118.         self.query= database.Query()
  119.         self.skin = self.settings[ "skin" ]
  120.         self.flat_cache = ()
  121.         self.sql = ""
  122.         self.params = None
  123.         self.display_info = False
  124.         ##self.dummy()
  125.         ##self.MyPlayer = MyPlayer( xbmc.PLAYER_CORE_MPLAYER, function=self.myPlayerChanged )
  126.         self.update_method = 0
  127.         #self.list_control_pos = [ 0, 0, 0, 0 ]
  128.         self.search_keywords = ""
  129.  
  130.     # dummy() and self.Timer are currently used for the Player() subclass so when an onPlayback* event occurs, 
  131.     # it calls myPlayerChanged() immediately.
  132.     ##def dummy( self ):
  133.     ##    self.Timer = threading.Timer( 60*60*60, self.dummy,() )
  134.     ##    self.Timer.start()
  135.  
  136.     ##def myPlayerChanged( self, event ):
  137.     ##    pass
  138.         #if ( event == 0 and self.currently_playing_movie[1] >= 0 ):
  139.         #    self.markAsWatched( self.currently_playing_movie[0] + 1, self.currently_playing_movie[1], self.currently_playing_movie[2] )
  140.         #elif ( event == 2 ):
  141.         #    self.currently_playing_movie = -1
  142.         #    self.currently_playing_genre = -1
  143.         
  144.     def _set_startup_choices( self ):
  145.         self.sql_category = ""
  146.         self.params_category = None
  147.         self.main_category = GENRES
  148.         self.genres = self.trailers.categories
  149.         self.current_display = [ [ GENRES , 0 ], [ 0, 1 ] ]
  150.  
  151.     def _set_startup_category( self ):
  152.         startup_button = "Shortcut1"
  153.         if ( self.settings[ "startup_category_id" ] == self.settings[ "shortcut2" ] ): startup_button = "Shortcut2"
  154.         elif ( self.settings[ "startup_category_id" ] == self.settings[ "shortcut3" ] ): startup_button = "Shortcut3"
  155.         self.setCategory( self.settings[ "startup_category_id" ], 1 )
  156.  
  157.     def setCategory( self, category_id=GENRES, list_category=0 ):
  158.         self.category_id = category_id
  159.         self.list_category = list_category
  160.         if ( list_category > 0 ):
  161.             if ( category_id == FAVORITES ):
  162.                 sql = self.query[ "favorites" ]
  163.                 params = ( 1, )
  164.             elif ( category_id == DOWNLOADED ):
  165.                 sql = self.query[ "downloaded" ]
  166.                 params = ( "", )
  167.             elif ( category_id == HD_TRAILERS ):
  168.                 sql = self.query[ "hd_trailers" ]
  169.                 params = ( "%p.mov%", )
  170.             elif ( category_id == NO_TRAILER_URLS ):
  171.                 sql = self.query[ "no_trailer_urls" ]
  172.                 params = ( "[]", )
  173.             elif ( category_id == WATCHED ):
  174.                 sql = self.query[ "watched" ]
  175.                 params = ( 0, )
  176.             elif ( category_id == RECENTLY_ADDED ):
  177.                 sql = self.query[ "recently_added" ]
  178.                 params = None
  179.             elif ( category_id == MULTIPLE_TRAILERS ):
  180.                 sql = self.query[ "multiple_trailers" ]
  181.                 params = None
  182.             elif ( category_id == CUSTOM_SEARCH ):
  183.                 sql = self.search_sql
  184.                 params = None
  185.             elif ( list_category == 1 ):
  186.                 sql = self.query[ "movies_by_genre_id" ]
  187.                 params = ( self.genres[category_id].id, )
  188.             elif ( list_category == 2 ):
  189.                 sql = self.query[ "movies_by_studio_name" ]
  190.                 params = ( self.trailers.categories[category_id].title, )
  191.             elif ( list_category == 3 ):
  192.                 sql = self.query[ "movies_by_actor_name" ]
  193.                 names = self.actor.split( " " )[:2]
  194.                 if ( len( names ) == 1 ):
  195.                     params = ( "%%%s%%" % ( names[0], ), )
  196.                 else:
  197.                     params = ( "%%%s %s%%" % ( names[0], names[1], ), )
  198.             self.current_display = [ self.current_display[ 0 ], [ category_id, list_category ] ]
  199.             self.showTrailers( sql, params )
  200.         else:
  201.             self.main_category = category_id
  202.             if ( category_id == GENRES ):
  203.                 sql = self.query[ "genre_category_list" ]
  204.             elif ( category_id == STUDIOS ):
  205.                 sql = self.query[ "studio_category_list" ]
  206.             elif ( category_id == ACTORS ):
  207.                 sql = self.query[ "actor_category_list" ]
  208.             self.current_display = [ [ category_id, list_category ], self.current_display[ 1 ] ]
  209.             self.showCategories( sql )
  210.         self.showControls( self.category_id <= GENRES and self.category_id > FAVORITES )
  211.     
  212.     def showCategories( self, sql, params=None, choice=0, force_update=False ):
  213.         try:
  214.             self.setCategoryLabel()
  215.             if ( sql != self.sql_category or params != self.params_category or force_update ):
  216.                 #self.list_control_pos[ self.list_category ] = choice
  217.                 self.trailers.getCategories( sql, params )
  218.                 xbmcgui.lock()
  219.                 self.sql_category = sql
  220.                 self.params_category = params
  221.                 self.getControl( self.CONTROL_CATEGORY_LIST ).reset()
  222.                 if ( len( self.trailers.categories ) ):
  223.                     for category in self.trailers.categories:
  224.                         if ( self.main_category == GENRES ):
  225.                             title = category.title.replace( "Newest", _( 150 ) ).replace( "Exclusives", _( 151 ) )
  226.                         else:
  227.                             title = category.title
  228.                         thumbnail = "amt-generic-%s%s.png" % ( ( "genre", "studio", "actor", )[ abs( self.category_id ) - 1 ], ( "-i", "", )[ category.completed ], )
  229.                         if ( self.main_category == ACTORS ):
  230.                             actor_path = os.path.join( xbmc.translatePath( "special://profile/" ), "Thumbnails", "Video", xbmc.getCacheThumbName( "actor" + category.title )[ 0 ], xbmc.getCacheThumbName( "actor" + category.title ) )
  231.                             thumbnail = ( thumbnail, actor_path, )[ os.path.isfile( actor_path ) ]
  232.                         count = "(%d)" % ( category.count, )
  233.                         list_item = xbmcgui.ListItem( title, count, thumbnail, thumbnail )
  234.                         ##list_item.setInfo( "video", { "Genre": self.category } )
  235.                         list_item.select( not category.completed )
  236.                         self.getControl( self.CONTROL_CATEGORY_LIST ).addItem( list_item )
  237.                     self._set_selection( self.CONTROL_CATEGORY_LIST, choice )#self.list_control_pos[ self.list_category ] )
  238.         except:
  239.             LOG( LOG_ERROR, self.__class__.__name__, "[%s]", sys.exc_info()[ 1 ] )
  240.         xbmcgui.unlock()
  241.  
  242.     def showTrailers( self, sql, params=None, choice=0, force_update=False ):
  243.         try:
  244.             self.trailer = -1
  245.             self.setCategoryLabel()
  246.             if ( sql != self.sql or params != self.params or force_update ):
  247.                 #self.list_control_pos[ self.list_category ] = choice
  248.                 if ( force_update != 2 ):
  249.                     updated = self.trailers.getMovies( sql, params )
  250.                     if ( updated ):
  251.                         self.sql_category = ""
  252.                 xbmcgui.lock()
  253.                 self.sql = sql
  254.                 self.params = params
  255.                 self.clearList()
  256.                 if ( self.trailers.movies ):
  257.                     for movie in self.trailers.movies: # now fill the list control
  258.                         thumbnail, poster = self._get_thumbnail( movie )
  259.                         total_trailers = ( "", " (x%d)" % len( movie.trailer_urls ), )[ len( movie.trailer_urls ) > 1 ]
  260.                         urls = ( "(%s)", "%%s%s" % total_trailers, )[ len( movie.trailer_urls ) > 0 ]
  261.                         #rating = ( "[%s]" % movie.rating, "", )[ not movie.rating ]
  262.                         list_item = xbmcgui.ListItem( urls % ( movie.title, ), movie.rating, poster, thumbnail )
  263.                         list_item.select( movie.favorite )
  264.                         plot = ( movie.plot, _( 400 ), )[ not movie.plot ]
  265.                         overlay = ( xbmcgui.ICON_OVERLAY_NONE, xbmcgui.ICON_OVERLAY_HD, )[ "720p.mov" in repr( movie.trailer_urls ) or "1080p.mov" in repr( movie.trailer_urls ) ]
  266.                         # release date and year
  267.                         try:
  268.                             year = int( movie.release_date[ -5 ] )
  269.                         except:
  270.                             year = 0
  271.                         date_added = "%s-%s-%s" % ( movie.date_added[ 8 : ], movie.date_added[ 5 : 7 ], movie.date_added[ : 4 ], )
  272.                         list_item.setInfo( "video", { "Title": movie.title, "Date": date_added, "Overlay": overlay, "Plot": plot, "MPAA": movie.rating, "Year": year, "Studio": movie.studio, "Genre": movie.genres, "Count": movie.watched } )
  273.                         format = xbmc.getRegion( "datelong" ).replace( "DDDD, ", "" ).replace( "MMMM", "%B" ).replace( "D", "%d" ).replace( "YYYY", "%Y" )
  274.                         # set date added property
  275.                         date_added = datetime.date( int( movie.date_added[ : 4 ] ), int( movie.date_added[ 5 : 7 ] ), int( movie.date_added[ 8 : ] ) ).strftime( format )
  276.                         list_item.setProperty( "dateadded", date_added )
  277.                         # set release date property
  278.                         list_item.setProperty( "releasedate", movie.release_date )
  279.                         # set watched date property
  280.                         try:
  281.                             watched_date = datetime.date( int( movie.watched_date[ : 4 ] ), int( movie.watched_date[ 5 : 7 ] ), int( movie.watched_date[ 8 : ] ) ).strftime( format )
  282.                         except:
  283.                             watched_date = ""
  284.                         list_item.setProperty( "watcheddate", watched_date )
  285.                         self.addItem( list_item )
  286.                     self._set_selection( self.CONTROL_TRAILER_LIST_START, choice + ( choice == -1 ) )
  287.                 else: self.clearTrailerInfo()
  288.         except:
  289.             LOG( LOG_ERROR, self.__class__.__name__, "[%s]", sys.exc_info()[ 1 ] )
  290.         xbmcgui.unlock()
  291.  
  292.     def _get_thumbnail( self, movie ):
  293.         poster = ( movie.poster, "amt-blank-poster.png", )[ not movie.poster ]
  294.         if ( not movie.poster and self.settings[ "thumbnail_display" ] == 0 ):
  295.             thumbnail = poster
  296.         else:
  297.             thumbnail = ( ( movie.thumbnail, movie.thumbnail_watched )[ movie.watched and self.settings[ "fade_thumb" ] ], "amt-generic-trailer%s.png" % ( ( "", "-w", )[ movie.watched > 0 ], ), "", )[ self.settings[ "thumbnail_display" ] ]
  298.         return thumbnail, poster
  299.  
  300.     def _set_selection( self, list_control, pos=0 ):
  301.         if ( list_control == self.CONTROL_TRAILER_LIST_START ): 
  302.             self.setCurrentListPosition( pos )
  303.             self.showTrailerInfo()
  304.         elif ( list_control == self.CONTROL_CATEGORY_LIST ):
  305.             self.getControl( self.CONTROL_CATEGORY_LIST ).selectItem( pos )
  306.             choice = self._set_count_label( self.CONTROL_CATEGORY_LIST )
  307.  
  308.     def showControls( self, category ):
  309.         xbmcgui.lock()
  310.         self.getControl( self.CONTROL_CATEGORY_LIST_GROUP ).setVisible( category )
  311.         self.getControl( self.CONTROL_TRAILER_LIST_GROUP ).setVisible( not category )
  312.         xbmcgui.unlock()
  313.         #self.showPlotCastControls( category )
  314.         self.setFocus( self.getControl( ( self.CONTROL_CATEGORY_LIST_GROUP, self.CONTROL_TRAILER_LIST_GROUP, )[ not category ] ) )
  315.  
  316.     def _toggle_trailer_info( self ):
  317.         self.display_info = not self.display_info
  318.         self._set_info_properties()
  319.         xbmc.sleep( 20 )
  320.         if ( self.display_info ): self.setFocus( self.getControl( self.CONTROL_PLOT_BUTTON ) )
  321.         else: self.setFocus( self.getControl( self.CONTROL_CAST_BUTTON ) )
  322.  
  323.     def setCategoryLabel( self ):
  324.         category= u""
  325.         if ( self.category_id == GENRES ):
  326.             category = _( 113 )
  327.         elif ( self.category_id == STUDIOS ):
  328.             category = _( 114 )
  329.         elif ( self.category_id == ACTORS ):
  330.             category = _( 115 )
  331.         elif ( self.category_id == FAVORITES ):
  332.             category = _( 152 )
  333.         elif ( self.category_id == DOWNLOADED ):
  334.             category = _( 153 )
  335.         elif ( self.category_id == HD_TRAILERS ):
  336.             category = _( 160 )
  337.         elif ( self.category_id == NO_TRAILER_URLS ):
  338.             category = _( 161 )
  339.         elif ( self.category_id == CUSTOM_SEARCH ):
  340.             category = _( 162 )
  341.         elif ( self.category_id == WATCHED ):
  342.             category = _( 163 )
  343.         elif ( self.category_id == RECENTLY_ADDED ):
  344.             category = _( 164 )
  345.         elif ( self.category_id == MULTIPLE_TRAILERS ):
  346.             category = _( 165 )
  347.         elif ( self.category_id >= 0 ):
  348.             if ( self.list_category == 3 ):
  349.                 category = self.actor
  350.             elif ( self.list_category == 2 ):
  351.                 category = self.trailers.categories[ self.category_id ].title
  352.             elif ( self.list_category == 1 ):
  353.                 category = self.genres[ self.category_id ].title.replace( "Newest", _( 150 ) ).replace( "Exclusives", _( 151 ) )
  354.         self.category = category
  355.         self.setProperty( "Category", self.category )
  356.  
  357.     def _set_count_label( self, list_control ):
  358.         separator = ( _( 96 ) )
  359.         if ( list_control == self.CONTROL_TRAILER_LIST_START ):
  360.             pos = self.getCurrentListPosition()
  361.             self.getControl( self.CONTROL_TRAILER_LIST_COUNT ).setLabel( "%d %s %d" % ( pos + 1, separator, len( self.trailers.movies ), ) )
  362.         else:
  363.             pos = self.getControl( self.CONTROL_CATEGORY_LIST ).getSelectedPosition()
  364.             self.getControl( self.CONTROL_CATEGORY_LIST_COUNT ).setLabel( "%d %s %d" % ( pos + 1, separator, len( self.trailers.categories ), ) )#self.getControl( self.CONTROL_CATEGORY_LIST ).size()
  365.         return pos
  366.     
  367.     def clearTrailerInfo( self ):
  368.         self.getControl( self.CONTROL_OVERLAY_RATING ).setImage( "" )
  369.         self.getControl( self.CONTROL_CAST_LIST ).reset()
  370.         self.getControl( self.CONTROL_TRAILER_LIST_COUNT ).setLabel( "" )
  371.         self.showOverlays()
  372.         
  373.     def showTrailerInfo( self ):
  374.         xbmcgui.lock()
  375.         try:
  376.             self.trailer_pos = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  377.             #hack until panel control is a native python control
  378.             if ( self.trailer_pos == -1 ): 
  379.                 self.setCurrentListPosition( len( self.trailers.movies ) - 1 )
  380.                 self.trailer_pos = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  381.             self.getControl( self.CONTROL_TRAILER_TITLE_LABEL ).setEnabled( not self.trailers.movies[ self.trailer_pos ].favorite )
  382.             if ( xbmc.skinHasImage( "%s/%s.png" % ( __scriptname__, self.trailers.movies[ self.trailer_pos ].rating, ) ) ):
  383.                 self.getControl( self.CONTROL_OVERLAY_RATING ).setImage( "%s/%s.png" % ( __scriptname__, self.trailers.movies[ self.trailer_pos ].rating, ) )
  384.             else:
  385.                 self.getControl( self.CONTROL_OVERLAY_RATING ).setImage( self.trailers.movies[ self.trailer_pos ].rating_url )
  386.             # Cast
  387.             self.getControl( self.CONTROL_CAST_LIST ).reset()
  388.             self.cast_exists = ( len( self.trailers.movies[ self.trailer_pos ].cast ) > 0 )
  389.             thumbnail = "amt-generic-%sactor.png" % ( "no", "" )[ self.trailers.movies[ self.trailer_pos ].cast != [] ]
  390.             if ( self.cast_exists ):
  391.                 for actor in self.trailers.movies[ self.trailer_pos ].cast:
  392.                     actor_path = os.path.join( xbmc.translatePath( "special://profile/" ), "Thumbnails", "Video", xbmc.getCacheThumbName( "actor" + actor[ 0 ] )[ 0 ], xbmc.getCacheThumbName( "actor" + actor[ 0 ] ) )
  393.                     actor_thumbnail = ( thumbnail, actor_path, )[ os.path.isfile( actor_path ) ]
  394.                     actual_icon = ( "", actor_thumbnail, )[ actor_thumbnail != thumbnail ]
  395.                     self.getControl( self.CONTROL_CAST_LIST ).addItem( xbmcgui.ListItem( actor[ 0 ], "", actual_icon, actor_thumbnail ) )
  396.             else: 
  397.                 self.getControl( self.CONTROL_CAST_LIST ).addItem( xbmcgui.ListItem( _( 401 ), "", "", thumbnail ) )
  398.             #self.showPlotCastControls( False )
  399.             self.showOverlays( self.trailer_pos )
  400.         except:
  401.             LOG( LOG_ERROR, self.__class__.__name__, "[%s]", sys.exc_info()[ 1 ] )
  402.         xbmcgui.unlock()
  403.  
  404.     def showOverlays( self, trailer=-1 ):
  405.         if ( trailer != -1 ):
  406.             self.getControl( self.CONTROL_OVERLAY_FAVORITE ).setVisible( self.trailers.movies[ trailer ].favorite )
  407.             self.getControl( self.CONTROL_OVERLAY_WATCHED ).setVisible( self.trailers.movies[ trailer ].watched )
  408.             self.getControl( self.CONTROL_OVERLAY_SAVED ).setVisible( self.trailers.movies[ trailer ].saved != [] )
  409.         else:
  410.             self.getControl( self.CONTROL_OVERLAY_FAVORITE ).setVisible( False )
  411.             self.getControl( self.CONTROL_OVERLAY_WATCHED ).setVisible( False )
  412.             self.getControl( self.CONTROL_OVERLAY_SAVED ).setVisible( False )
  413.             
  414.     def getTrailerGenre( self ):
  415.         genre = self.getControl( self.CONTROL_CATEGORY_LIST ).getSelectedPosition()
  416.         if ( self.main_category == STUDIOS ): 
  417.             list_category = 2
  418.         elif ( self.main_category == ACTORS ): 
  419.             list_category = 3
  420.             self.actor = unicode( self.getControl( self.CONTROL_CATEGORY_LIST ).getSelectedItem().getLabel(), "utf-8" )
  421.         else: list_category = 1
  422.         self.setCategory( genre, list_category )
  423.  
  424.     def getActorChoice( self ):
  425.         choice = self.getControl( self.CONTROL_CAST_LIST ).getSelectedPosition()
  426.         self.actor = unicode( self.getControl( self.CONTROL_CAST_LIST ).getSelectedItem().getLabel(), "utf-8" )
  427.         self.setCategory( choice, 3 )
  428.  
  429.     def _get_trailer_url( self, trailer ):
  430.         title = self.trailers.movies[ trailer ].title
  431.         trailer_urls = self.trailers.movies[ trailer ].trailer_urls
  432.         saved = self.trailers.movies[ trailer ].saved
  433.         items = ()
  434.         urls = []
  435.         for trailers in trailer_urls:
  436.             # get intial choice
  437.             choice = ( self.settings[ "trailer_quality" ], len( trailers ) - 1, )[ self.settings[ "trailer_quality" ] >= len( trailers ) ]
  438.             # if quality is non progressive
  439.             if ( self.settings[ "trailer_quality" ] <= 2 ):
  440.                 # select the correct non progressive trailer
  441.                 while ( trailers[ choice ].endswith( "p.mov" ) and choice != -1 ): choice -= 1
  442.             # quality is progressive
  443.             else:
  444.                 # select the proper progressive quality
  445.                 quality = ( "480p", "720p", "1080p", )[ self.settings[ "trailer_quality" ] - 3 ]
  446.                 # select the correct progressive trailer
  447.                 while ( quality not in trailers[ choice ] and trailers[ choice ].endswith( "p.mov" ) and choice != -1 ): choice -= 1
  448.             # if we have a valid choice add the url to our list
  449.             if ( choice >= 0 ):
  450.                 urls += [ trailers[ choice ] ]
  451.         # if there are selections and there is more than one trailer, let user choose
  452.         if len( urls ):
  453.             trailer = 0
  454.             if ( len( urls ) > 1 ):
  455.                 # sort the urls, hopefully they will then be in the order of release
  456.                 urls.sort()
  457.                 # let the user choose
  458.                 trailer = self._get_trailer( title, urls )
  459.             # if the user did not cancel the dialog
  460.             if ( trailer is not None ):
  461.                 # if play all was selected, get the filepath for each url
  462.                 if ( trailer == len( urls ) ):
  463.                     for c, url in enumerate( urls ):
  464.                         t = self.get_filepath( "%s%s" % ( title, os.path.splitext( url )[ 1 ], ), c + 1, len( urls ) > 1 )
  465.                         items += ( ( t, url, c + 1, ), )
  466.                 else:
  467.                     # we only want the trailer selected
  468.                     t = self.get_filepath( "%s%s" % ( title, os.path.splitext( urls[ trailer ] )[ 1 ], ), trailer + 1, len( urls ) > 1 )
  469.                     items = ( ( t, urls[ trailer ], trailer + 1, ), )
  470.         return items
  471.  
  472.     def get_filepath( self, title, count, multiple ):
  473.         # add our trailer number if there is more than one trailer
  474.         filepath = "%s%s%s" % ( os.path.splitext( title )[ 0 ], ( "", "_%d" % ( count, ), )[ multiple ], os.path.splitext( title )[ 1 ], )
  475.         # now make it legal
  476.         filepath = make_legal_filepath( filepath, save_end=multiple )
  477.         return filepath
  478.  
  479.     def _get_trailer( self, title, urls ):
  480.         # if Auto play all trailers, return the play all selection
  481.         if ( self.settings[ "auto_play_all" ] ):
  482.             return len( urls )
  483.         import chooser
  484.         force_fallback = self.skin != "Default"
  485.         choices = [ "%s %d%s - (%s)" % ( _( 99 ), c + 1, ( "", " - [HD]", )[ "720p.mov" in url or "1080p.mov" in url ], os.path.splitext( os.path.basename( url ) )[ 0 ] ) for c, url in enumerate( urls ) ]
  486.         choices += [ _( 39 ) ]
  487.         ch = chooser.GUI( "script-%s-chooser.xml" % ( __scriptname__.replace( " ", "_" ), ), os.getcwd(), self.skin, force_fallback, choices=choices, descriptions=[ "" for x in range( len( choices ) ) ], original=-1, selection=0, list_control=1, title=title )
  488.         selection = ch.selection
  489.         del ch
  490.         return selection
  491.  
  492.     def playTrailer( self ):
  493.         try:
  494.             self.trailer = self.getCurrentListPosition()
  495.             if ( len( self.trailers.movies[ self.trailer ].trailer_urls ) ):
  496.                 items = self._get_trailer_url( self.trailer )
  497.                 if ( items ):
  498.                     playlist = xbmc.PlayList( xbmc.PLAYLIST_VIDEO )
  499.                     playlist.clear()
  500.                     for count, ( title, url, selected ) in enumerate( items ):
  501.                         LOG( LOG_DEBUG, self.__class__.__name__, "[url: %s]", repr( url ) )
  502.                         filename = None
  503.                         for saved in self.trailers.movies[ self.trailer ].saved:
  504.                             #if ( title in saved[ 0 ] ):
  505.                             if ( url == saved[ 1 ] ):
  506.                                 filename = saved[ 0 ]
  507.                                 self.url = saved[ 1 ]
  508.                                 break
  509.                         if ( filename is None or not os.path.isfile( filename ) ):
  510.                             #if ( url.endswith( "p.mov" ) ):
  511.                                 #self.core = xbmc.PLAYER_CORE_DVDPLAYER
  512.                             #else:
  513.                             #    self.core = xbmc.PLAYER_CORE_MPLAYER
  514.                             self.url = url
  515.                             if ( self.settings[ "mode" ] == 0 ):
  516.                                 filename = url + "?|User-Agent=%s" % ( urllib.quote_plus( cacheurl.__useragent__ ), )
  517.                             else:
  518.                                 if ( self.settings[ "mode" ] == 1 ):
  519.                                     if ( not self.check_cache( self.trailers.movies[ self.trailer ].title ) ):
  520.                                         self.flat_cache = ()
  521.                                     fetcher = cacheurl.HTTPProgressSave( save_title=title, clear_cache_folder=not self.flat_cache )
  522.                                     filename = fetcher.urlretrieve( url )
  523.                                     if ( filename and not self.check_cache( filename, 1 ) ):
  524.                                         self.flat_cache += ( ( self.trailers.movies[ self.trailer ].title, filename, ), )
  525.                                 elif ( self.settings[ "mode" ] >= 2 ):
  526.                                     fetcher = cacheurl.HTTPProgressSave( self.settings[ "save_folder" ], title )
  527.                                     filename = fetcher.urlretrieve( url )
  528.                                     if ( filename is not None ):
  529.                                         poster = ( self.trailers.movies[ self.trailer ].poster, "amt-blank-poster.png", )[ not self.trailers.movies[ self.trailer ].poster ]
  530.                                         self.saveThumbnail( filename, self.trailer, poster )
  531.                         if ( filename is not None ):
  532.                             listitem = xbmcgui.ListItem( self.trailers.movies[ self.trailer ].title, thumbnailImage=self.trailers.movies[ self.trailer ].poster )
  533.                             if ( len( items ) > 1 ): s = count + 1
  534.                             else: s = selected
  535.                             plot = ( self.trailers.movies[ self.trailer ].plot, _( 400 ), )[ not self.trailers.movies[ self.trailer ].plot ]
  536.                             t = "%s%s" % ( self.trailers.movies[ self.trailer ].title, ( "", " (%s %d)" % ( _( 99 ), s, ), )[ len( self.trailers.movies[ self.trailer ].trailer_urls ) > 1 ] )
  537.                             try:
  538.                                 year = int( self.trailers.movies[ self.trailer ].release_date[ -5 : ] )
  539.                             except:
  540.                                 year = 0
  541.                             listitem.setInfo( "video", { "Title": t, "Year": year, "PlotOutline": plot, "Plot": plot, "Studio": self.trailers.movies[ self.trailer ].studio, "Genre": self.trailers.movies[ self.trailer ].genres } )
  542.                             LOG( LOG_DEBUG, self.__class__.__name__, "[filename: %s]", repr( filename ) )
  543.                             playlist.add( filename, listitem )
  544.                     if ( len( playlist ) ):
  545.                         self._set_video_resolution()
  546.                         ##xbmc.Player( self.core ).play( playlist )
  547.                         xbmc.Player().play( playlist )
  548.         except:
  549.             LOG( LOG_ERROR, self.__class__.__name__, "[%s]", sys.exc_info()[ 1 ] )
  550.  
  551.     def check_cache( self, title, pos=0 ):
  552.         exists = False
  553.         for item in self.flat_cache:
  554.             if ( title == item[ pos ] ):
  555.                 exists = True
  556.                 break
  557.         return exists
  558.  
  559.     def saveThumbnail( self, filename, trailer, poster ):
  560.         try: 
  561.             new_filename = "%s.tbn" % ( os.path.splitext( filename )[0], )
  562.             if ( not os.path.isfile( new_filename ) ):
  563.                 xbmc.executehttpapi("FileCopy(%s,%s)" % ( poster, new_filename, ) )
  564.             if ( not self.check_cache( filename, 1 ) ):# not in repr( self.trailers.movies[ trailer ].saved ) ):
  565.                 self.trailers.movies[ trailer ].saved += [ ( filename, self.url, ) ]
  566.                 success = self.trailers.updateRecord( "movies", ( "saved", ), ( repr( self.trailers.movies[ trailer ].saved ), self.trailers.movies[ trailer ].idMovie, ), "idMovie" )
  567.                 #if ( success ):
  568.                 #    self.trailers.movies[ trailer ].saved = filename
  569.                 #    ##self.showOverlays( trailer )
  570.         except:
  571.             LOG( LOG_ERROR, self.__class__.__name__, "[%s]", sys.exc_info()[ 1 ] )
  572.  
  573.     def showContextMenu( self ):
  574.         if ( self.controlId == self.CONTROL_CATEGORY_LIST or self.controlId == self.CONTROL_CAST_LIST ):
  575.             selection = self.getControl( self.controlId ).getSelectedPosition()
  576.             #controlId= self.controlId
  577.         else:
  578.             selection = self.getCurrentListPosition()
  579.         controlId = self.getFocusId()#self.CONTROL_TRAILER_LIST_START
  580.         x, y = self.getControl( controlId ).getPosition()
  581.         w = self.getControl( controlId ).getWidth()
  582.         h = self.getControl( controlId ).getHeight()# - self.getControl( controlId ).getItemHeight()
  583.         labels = ()
  584.         functions = ()
  585.         if ( self.CONTROL_TRAILER_LIST_START <= controlId <= self.CONTROL_TRAILER_LIST_END ):
  586.             if ( len( self.trailers.movies[ selection ].trailer_urls ) ):
  587.                 labels += ( _( 501 ), )
  588.                 functions += ( self.playTrailer, )
  589.             labels += ( _( 502 + self.trailers.movies[ selection ].favorite ), )
  590.             functions += ( self.toggleAsFavorite, )
  591.             if ( self.trailers.movies[ selection ].watched ): watched_lbl = "  (%d)" % ( self.trailers.movies[ selection ].watched, )
  592.             else: watched_lbl = ""
  593.             labels += ( "%s" % ( _( 504 + ( self.trailers.movies[ selection ].watched > 0 ) ) + watched_lbl, ), )
  594.             functions += ( self.toggleAsWatched, )
  595.             if ( self.category_id != MULTIPLE_TRAILERS ):
  596.                 labels += ( _( 506 ), )
  597.                 functions += ( self.refreshTrailerInfo, )
  598.             if ( self.category_id == NO_TRAILER_URLS ):
  599.                 labels += ( _( 507 ), )
  600.                 functions += ( self.refreshAllTrailersInfo, )
  601.             if ( self.category_id >= 0 and self.list_category == 1 ):
  602.                 labels += ( _( 512 ), )
  603.                 functions += ( self.refreshCurrentGenre, )
  604.             if ( self.trailers.movies[ selection ].saved != [] ):
  605.                 labels += ( _( 509 ), )
  606.                 functions += ( self.deleteSavedTrailer, )
  607.             elif ( self.check_cache( self.trailers.movies[ selection ].title ) ):
  608.                 labels += ( _( 508 ), )
  609.                 functions += ( self.saveCachedMovie, )
  610.             if ( self.settings[ "showtimes_local" ] ):
  611.                 labels += ( _( 510 ), )
  612.                 functions += ( self.get_showtimes, )
  613.         elif ( controlId == self.CONTROL_CATEGORY_LIST ):
  614.             functions += ( self.getTrailerGenre, )
  615.             if ( self.category_id == GENRES ):
  616.                 labels += ( _( 511 ), )
  617.                 labels += ( _( 512 ), )
  618.                 functions += ( self.refreshGenre, )
  619.                 labels += ( _( 513 ), )
  620.                 functions += ( self.refreshAllGenres, )
  621.             elif ( self.category_id ==  STUDIOS ):
  622.                 labels += ( _( 521 ), )
  623.             elif ( self.category_id ==  ACTORS ):
  624.                 labels += ( _( 531 ), )
  625.         elif ( controlId == self.CONTROL_CAST_LIST and self.cast_exists ):
  626.                 labels += ( _( 531 ), )
  627.                 functions += ( self.getActorChoice, )
  628.         if ( not self.trailers.complete ): 
  629.             labels += ( _( 550 ), )
  630.             functions += ( self.force_full_update, )
  631.         force_fallback = self.skin != "Default"
  632.         cm = context_menu.GUI( "script-%s-context.xml" % ( __scriptname__.replace( " ", "_" ), ), os.getcwd(), self.skin, force_fallback, area=( x, y, w, h, ), labels=labels )
  633.         if ( cm.selection is not None ):
  634.             functions[ cm.selection ]()
  635.         del cm
  636.  
  637.     def force_full_update( self ):
  638.         updated = self.trailers.fullUpdate()
  639.         if ( self.list_category > 0 ):
  640.             self.sql_category = ""
  641.             trailer = self.getCurrentListPosition()
  642.             self.showTrailers( self.sql, self.params, choice=trailer, force_update=2 )
  643.         else:
  644.             self.sql = ""
  645.             if ( self.main_category == GENRES ):
  646.                 genre = self.getControl( self.CONTROL_CATEGORY_LIST ).getSelectedPosition()
  647.                 self.showCategories( self.sql_category, choice=genre, force_update=True )
  648.  
  649.     def markAsWatched( self, watched, trailer ):
  650.         date = ( datetime.date.today(), "", )[ not watched ]
  651.         success = self.trailers.updateRecord( "movies", ( "times_watched", "last_watched", ), ( watched, date, self.trailers.movies[ trailer ].idMovie, ), "idMovie" )
  652.         if ( success ):
  653.             self.trailers.movies[ trailer ].watched = watched
  654.             self.trailers.movies[ trailer ].watched_date = str( date )
  655.         else:
  656.             LOG( LOG_ERROR, self.__class__.__name__, "[failed]" )
  657.  
  658.     def perform_search( self ):
  659.         self.search_sql = ""
  660.         if ( self.settings[ "use_simple_search" ] ):
  661.             keyword = get_keyboard( default=self.search_keywords, heading= _( 95 ) )
  662.             xbmc.sleep(10)
  663.             if ( keyword ):
  664.                 keywords = keyword.split()
  665.                 self.search_keywords = keyword
  666.                 where = ""
  667.                 compare = False
  668.                 # this may be faster than regex, but not as accurate
  669.                 #where += "(LIKE '%% %s %%' OR title LIKE '%% %s.%%' OR title LIKE '%% %s,%%' OR title LIKE '%% %s:%%' OR title LIKE '%% %s!%%' OR title LIKE '%% %s-%%' OR title LIKE '%% %s?%%' OR title LIKE '%s %%' OR title LIKE '%s.%%' OR title LIKE '%s,%%' OR title LIKE '%s:%%' OR title LIKE '%s!%%' OR title LIKE '%s-%%' OR title LIKE '%s?%%' OR title LIKE '%% %s' OR title LIKE '%s' OR title LIKE '%%(%s' OR title LIKE '%% %s)' OR " % ( ( word, ) * 18 )
  670.                 pattern = ( "LIKE '%%%s%%'", "regexp('\\b%s\\b')", )[ self.settings[ "match_whole_words" ] ]
  671.                 for word in keywords:
  672.                     if ( word.upper() == "AND" or word.upper() == "OR" ):
  673.                         where += " %s " % word.upper()
  674.                         compare = False
  675.                         continue
  676.                     elif ( word.upper() == "NOT" ):
  677.                         where += "NOT "
  678.                         continue
  679.                     elif ( compare ):
  680.                         where += " AND "
  681.                         compare = False
  682.                     where += "(title %s OR " % ( pattern % ( word, ), )
  683.                     where += "plot %s OR " % ( pattern % ( word, ), )
  684.                     where += "actor %s OR " % ( pattern % ( word, ), )
  685.                     where += "studio %s OR " % ( pattern % ( word, ), )
  686.                     where += "genre %s)" % ( pattern % ( word, ), )
  687.                     compare = True
  688.                 self.search_sql = self.query[ "simple_search" ] % ( where, )
  689.         else:
  690.             import search
  691.             force_fallback = self.skin != "Default"
  692.             s = search.GUI( "script-%s-search.xml" % ( __scriptname__.replace( " ", "_" ), ), os.getcwd(), self.skin, force_fallback )
  693.             s.doModal()
  694.             if ( s.query ):
  695.                 self.search_sql = s.query
  696.             del s
  697.         if ( self.search_sql ):
  698.             self.setCategory( CUSTOM_SEARCH, 1 )
  699.  
  700.     def changeSettings( self ):
  701.         import settings
  702.         force_fallback = self.skin != "Default"
  703.         settings = settings.GUI( "script-%s-settings.xml" % ( __scriptname__.replace( " ", "_" ), ), os.getcwd(), self.skin, force_fallback, skin=self.skin, genres=self.genres )
  704.         settings.doModal()
  705.         if ( settings.changed ):
  706.             self._get_settings()
  707.             ok = False
  708.             if ( settings.restart ):
  709.                 ok = xbmcgui.Dialog().yesno( __scriptname__, _( 240 ), "", _( 241 ), _( 271 ), _( 270 ) )
  710.             if ( not ok ):
  711.                 self._set_shortcut_properties()
  712.                 self._set_info_properties()
  713.                 if ( settings.refresh and self.category_id not in ( GENRES, STUDIOS, ACTORS, ) ):
  714.                     self.sql_category = ""
  715.                     trailer = self.getCurrentListPosition()
  716.                     self.showTrailers( self.sql, self.params, choice=trailer, force_update=2 )
  717.                 elif ( settings.refresh ):
  718.                     self.sql = ""
  719.                     genre = self.getControl( self.CONTROL_CATEGORY_LIST ).getSelectedPosition()
  720.                     self.showCategories( self.sql_category, self.params_category, choice=genre, force_update=2 )
  721.                 #else: self.sql=""
  722.             else: self.exitScript( True )
  723.         del settings
  724.  
  725.     def _set_shortcut_properties( self ):
  726.         shortcuts = { FAVORITES: _( 152 ), DOWNLOADED: _( 153 ), HD_TRAILERS: _( 160 ), NO_TRAILER_URLS: _( 161 ),
  727.                             CUSTOM_SEARCH: _( 162 ), WATCHED: _( 163 ), RECENTLY_ADDED: _( 164 ), MULTIPLE_TRAILERS: _( 165 ), }
  728.         self.setProperty( "shortcut1", shortcuts.get( self.settings[ "shortcut1" ], self.genres[ self.settings[ "shortcut1" ] ].title.replace( "Newest", _( 150 ) ).replace( "Exclusives", _( 151 ) ) ) )
  729.         self.setProperty( "shortcut2", shortcuts.get( self.settings[ "shortcut2" ], self.genres[ self.settings[ "shortcut2" ] ].title.replace( "Newest", _( 150 ) ).replace( "Exclusives", _( 151 ) ) ) )
  730.         self.setProperty( "shortcut3", shortcuts.get( self.settings[ "shortcut3" ], self.genres[ self.settings[ "shortcut3" ] ].title.replace( "Newest", _( 150 ) ).replace( "Exclusives", _( 151 ) ) ) )
  731.  
  732.     def _set_info_properties( self ):
  733.         self.setProperty( "showinfo", ( "", "1", )[ self.display_info ] )
  734.  
  735.     def showCredits( self ):
  736.         """ shows a credit window """
  737.         import credits
  738.         force_fallback = self.skin != "Default"
  739.         c = credits.GUI( "script-%s-credits.xml" % ( __scriptname__.replace( " ", "_" ), ), os.getcwd(), self.skin, force_fallback )
  740.         del c
  741.  
  742.     def updateScript( self ):
  743.         import update
  744.         updt = update.Update()
  745.         del updt
  746.  
  747.     def toggleAsWatched( self ):
  748.         trailer = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  749.         watched = not ( self.trailers.movies[ trailer ].watched > 0 )
  750.         self.markAsWatched( watched, trailer )
  751.  
  752.     def toggleAsFavorite( self ):
  753.         trailer = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  754.         favorite = not self.trailers.movies[ trailer ].favorite
  755.         success = self.trailers.updateRecord( "movies", ( "favorite", ), ( favorite, self.trailers.movies[ trailer ].idMovie, ), "idMovie" )
  756.         if ( success ):
  757.             self.trailers.movies[ trailer ].favorite = favorite
  758.             self.getListItem( trailer ).select( favorite )
  759.             if ( self.category_id == FAVORITES ):
  760.                 self.trailers.movies.pop( trailer )
  761.                 self.removeItem( trailer )
  762.             if ( not len( self.trailers.movies ) ): self.clearTrailerInfo()
  763.             else: self.showTrailerInfo()
  764.  
  765.     def refreshAllGenres( self ):
  766.         self.sql = ""
  767.         genres = range( len( self.genres ) )
  768.         self.trailers.refreshGenre( genres, refresh_trailers=self.settings[ "refresh_trailers" ] )
  769.         if ( self.category_id == GENRES ):
  770.             sql = self.query[ "genre_category_list" ]
  771.             genre = self.getControl( self.CONTROL_CATEGORY_LIST ).getSelectedPosition()
  772.             self.sql_category = ""
  773.             self.showCategories( sql, choice=genre, force_update=True )
  774.         
  775.     def refreshGenre( self ):
  776.         genre = self.getControl( self.CONTROL_CATEGORY_LIST ).getSelectedPosition()
  777.         if ( self.current_display[ 1 ][ 0 ] == genre ):
  778.             self.sql = ""
  779.         self.trailers.refreshGenre( ( genre, ), refresh_trailers=self.settings[ "refresh_trailers" ] )
  780.         self.sql_category = ""
  781.         sql = self.query[ "genre_category_list" ]
  782.         self.showCategories( sql, choice=genre, force_update=True )
  783.         #count = "(%d)" % self.trailers.categories[ genre ].count
  784.         #self.getControl( self.CONTROL_CATEGORY_LIST ).getSelectedItem().setLabel2( count )
  785.  
  786.     def refreshCurrentGenre( self ):
  787.         trailer = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  788.         self.sql_category = ""
  789.         self.trailers.refreshGenre( ( self.category_id, ), refresh_trailers=self.settings[ "refresh_trailers" ] )
  790.         self.showTrailers( self.sql, params=self.params, choice=trailer, force_update=True )
  791.  
  792.     def refreshTrailerInfo( self ):
  793.         trailer = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  794.         #self.getControl( self.CONTROL_TRAILER_POSTER ).setImage( "" )
  795.         self.getControl( self.CONTROL_OVERLAY_RATING ).setImage( "" )
  796.         self.trailers.refreshTrailerInfo( ( trailer, ) )
  797.         self.showTrailers( self.sql, params=self.params, choice=trailer, force_update=True )
  798.  
  799.     def refreshAllTrailersInfo( self ):
  800.         ##### add a progress dialog
  801.         trailer = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  802.         self.getControl( self.CONTROL_OVERLAY_RATING ).setImage( "" )
  803.         trailers = range( len( self.trailers.movies ) )
  804.         self.trailers.refreshTrailerInfo( trailers )
  805.         self.showTrailers( self.sql, params=self.params, choice=trailer, force_update=True )
  806.  
  807.     def saveCachedMovie( self ):
  808.         try:
  809.             dialog = xbmcgui.DialogProgress()
  810.             dialog.create( _( 56 ) )
  811.             trailer = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  812.             environment = os.environ.get( "OS", "xbox" )
  813.             dirname = self.settings[ "save_folder" ]
  814.             if ( environment == "xbox" or environment == "win32" ):
  815.                 dirname = dirname.replace( "\\", "/" )
  816.             for count, filename in enumerate( self.flat_cache ):
  817.                 percent = int( ( count + 1 ) * ( float( 100 ) / len( self.flat_cache ) ) )
  818.                 new_filename = "%s%s" % ( dirname, os.path.basename( filename[ 1 ] ), )
  819.                 dialog.update( percent, "%s %s" % ( _( 1008 ), new_filename, ) )
  820.                 if ( not os.path.isfile( new_filename ) ):
  821.                     xbmc.executehttpapi("FileCopy(%s,%s)" % ( filename[ 1 ], new_filename, ) )
  822.                     if ( not new_filename.startswith( "smb://" ) ):
  823.                         xbmc.executehttpapi("FileCopy(%s.conf,%s.conf)" % ( filename[ 1 ], new_filename, ) )
  824.                     poster = ( self.trailers.movies[ trailer ].poster, "amt-blank-poster.png", )[ not self.trailers.movies[ trailer ].poster ]
  825.                     self.saveThumbnail( new_filename, trailer, poster )
  826.             self.showOverlays( trailer )
  827.             dialog.close()
  828.         except:
  829.             LOG( LOG_ERROR, self.__class__.__name__, "[%s]", sys.exc_info()[ 1 ] )
  830.             dialog.close()
  831.             xbmcgui.Dialog().ok( _( 56 ), _( 90 ) )
  832.                 
  833.     def deleteSavedTrailer( self ):
  834.         trailer = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  835.         if ( xbmcgui.Dialog().yesno( "%s?" % ( _( 509 ), ), _( 82 ), self.trailers.movies[ trailer ].title ) ):
  836.             for saved in self.trailers.movies[ trailer ].saved:
  837.                 if ( os.path.isfile( saved[ 0 ] ) ):
  838.                     os.remove( saved[ 0 ] )
  839.                 if ( os.path.isfile( "%s.conf" % ( saved[ 0 ], ) ) ):
  840.                     os.remove( "%s.conf" % ( saved[ 0 ], ) )
  841.                 if ( os.path.isfile( "%s.tbn" % ( os.path.splitext( saved[ 0 ] )[ 0 ], ) ) ):
  842.                     os.remove( "%s.tbn" % ( os.path.splitext( saved[ 0 ] )[ 0 ], ) )
  843.                 success = self.trailers.updateRecord( "Movies", ( "saved", ), ( "[]", self.trailers.movies[ trailer ].idMovie, ), "idMovie" )
  844.                 if ( success ):
  845.                     self.trailers.movies[ trailer ].saved = []
  846.                     if ( self.category_id == DOWNLOADED ):
  847.                         self.trailers.movies.pop( trailer )
  848.                         self.removeItem( trailer )
  849.                     if ( not len( self.trailers.movies ) ): self.clearTrailerInfo()
  850.                     else: self.showTrailerInfo()
  851.  
  852.     def get_showtimes( self ):
  853.         trailer = self._set_count_label( self.CONTROL_TRAILER_LIST_START )
  854.         import showtimes
  855.         force_fallback = self.skin != "Default"
  856.         s = showtimes.GUI( "script-%s-showtimes.xml" % ( __scriptname__.replace( " ", "_" ), ), os.getcwd(), self.skin, force_fallback, title=self.trailers.movies[ trailer ].title, location=self.settings[ "showtimes_local" ] )
  857.         del s
  858.  
  859.     def exitScript( self, restart=False ):
  860.         ##if ( self.Timer is not None ): self.Timer.cancel()
  861.         self._set_video_resolution( True )
  862.         self.close()
  863.         if ( restart ): xbmc.executebuiltin( "XBMC.RunScript(%s)" % ( os.path.join( os.getcwd(), "default.py" ), ) )
  864.  
  865.     def onClick( self, controlId ):
  866.         try:
  867.             if ( controlId == 100 ):
  868.                 self.setCategory( self.settings[ "shortcut1" ], 1 )
  869.             elif ( controlId == 101 ):
  870.                 self.setCategory( self.settings[ "shortcut2" ], 1 )
  871.             elif ( controlId == 102 ):
  872.                 self.setCategory( self.settings[ "shortcut3" ], 1 )
  873.             elif ( controlId == 103 ):
  874.                 self.setCategory( GENRES, 0 )
  875.             elif ( controlId == 104 ):
  876.                 self.setCategory( STUDIOS, 0 )
  877.             elif ( controlId == 105 ):
  878.                 self.setCategory( ACTORS, 0 )
  879.             elif ( controlId == 106 ):
  880.                 self.perform_search()
  881.             elif ( controlId == 107 ):
  882.                 self.changeSettings()
  883.             elif ( controlId == 108 ):
  884.                 self.showCredits()
  885.             elif ( controlId == 109 ):
  886.                 self.updateScript()
  887.             elif ( controlId in ( self.CONTROL_PLOT_BUTTON, self.CONTROL_CAST_BUTTON ) ):
  888.                 self._toggle_trailer_info()
  889.             elif ( self.CONTROL_TRAILER_LIST_START <= controlId <= self.CONTROL_TRAILER_LIST_END ):
  890.                 self.playTrailer()
  891.             elif ( controlId == self.CONTROL_CATEGORY_LIST ):
  892.                 self.getTrailerGenre()
  893.             elif ( controlId == self.CONTROL_CAST_LIST and self.cast_exists ):
  894.                 self.getActorChoice()
  895.         except:
  896.             LOG( LOG_ERROR, self.__class__.__name__, "[%s]", sys.exc_info()[ 1 ] )
  897.  
  898.     def onFocus( self, controlId ):
  899.         #xbmc.sleep( 10 )
  900.         if ( controlId == self.CONTROL_TRAILER_LIST_GROUP ):
  901.             self.controlId = self.CONTROL_TRAILER_LIST_START
  902.         elif ( controlId == self.CONTROL_CATEGORY_LIST_GROUP ):
  903.             self.controlId = self.CONTROL_CATEGORY_LIST
  904.         else:
  905.             self.controlId = controlId
  906.  
  907.     def onAction( self, action ):
  908.         try:
  909.             if ( action in ACTION_EXIT_SCRIPT ):
  910.                 self.exitScript()
  911.             elif ( action in ACTION_TOGGLE_DISPLAY ):
  912.                 self.setCategory( self.current_display[ self.list_category == 0 ][ 0 ], self.current_display[ self.list_category == 0 ][ 1 ] )
  913.             else:
  914.                 if ( self.CONTROL_TRAILER_LIST_START <= self.controlId <= self.CONTROL_TRAILER_LIST_END ):
  915.                     if ( action in ACTION_CONTEXT_MENU ):
  916.                         self.showContextMenu()
  917.                     ##elif ( action in ACTION_SELECT_ITEM ):
  918.                     ##    self.playTrailer()
  919.                     ###############################################################
  920.                     elif ( action.getButtonCode() in ( 262, 263, ) or action in ACTION_MOVEMENT ):
  921.                         self.showTrailerInfo()
  922.                     ###############################################################
  923.                 elif ( self.CONTROL_TRAILER_LIST_PAGE_START <= self.controlId <= self.CONTROL_TRAILER_LIST_PAGE_END ):
  924.                     if ( action in ACTION_MOVEMENT ):
  925.                         self.showTrailerInfo()
  926.                 elif ( self.controlId == self.CONTROL_CATEGORY_LIST ):
  927.                     if ( action in ACTION_CONTEXT_MENU ):
  928.                         self.showContextMenu()
  929.                     #elif ( action.getButtonCode() in SELECT_ITEM ):
  930.                     #    self.getTrailerGenre()
  931.                     elif ( action in ACTION_MOVEMENT_UP + ACTION_MOVEMENT_DOWN ):
  932.                         choice = self._set_count_label( self.CONTROL_CATEGORY_LIST )
  933.                 elif ( self.controlId == self.CONTROL_CATEGORY_LIST_PAGE ):
  934.                     if ( action in ACTION_MOVEMENT_UP + ACTION_MOVEMENT_DOWN ):
  935.                         self._set_count_label( self.CONTROL_CATEGORY_LIST )
  936.                 elif ( self.controlId == self.CONTROL_CAST_LIST ):
  937.                     if ( action in ACTION_CONTEXT_MENU ):
  938.                         self.showContextMenu()
  939.                 elif ( ( self.CONTROL_TRAILER_LIST_PAGE_GROUP_START <= self.controlId <= self.CONTROL_TRAILER_LIST_PAGE_GROUP_END ) and action in ACTION_SELECT_ITEM ):
  940.                     self.showTrailerInfo()
  941.                 elif ( self.controlId in self.CONTROL_CATEGORY_LIST_PAGE_GROUP and action in ACTION_SELECT_ITEM ):
  942.                     self._set_count_label( self.CONTROL_CATEGORY_LIST )
  943.                     #elif ( action.getButtonCode() in SELECT_ITEM ):
  944.                     #    self.getActorChoice()
  945.         except:
  946.             LOG( LOG_ERROR, self.__class__.__name__, "[%s]", sys.exc_info()[ 1 ] )
  947.  
  948. def main():
  949.     _progress_dialog( len( modules ) + 1, _( 55 ) )
  950.     settings = Settings().get_settings()
  951.     force_fallback = settings[ "skin" ] != "Default"
  952.     ui = GUI( "script-%s-main.xml" % ( __scriptname__.replace( " ", "_" ), ), os.getcwd(), settings[ "skin" ], force_fallback )
  953.     _progress_dialog( -1 )
  954.     ui.doModal()
  955.     del ui
  956. main()
  957.  
  958. """
  959. ## Thanks Thor918 for this class ##
  960. class MyPlayer( xbmc.Player ):
  961.     def  __init__( self, *args, **kwargs ):
  962.         xbmc.Player.__init__( self, *args, **kwargs )
  963.         self.function = kwargs["function"]
  964.  
  965.     def onPlayBackStopped( self ):
  966.         self.function( 0 )
  967.     
  968.     def onPlayBackEnded( self ):
  969.         self.function( 1 )
  970.     
  971.     def onPlayBackStarted( self ):
  972.         self.function( 2 )
  973. """