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 / search.py < prev    next >
Encoding:
Python Source  |  2008-08-06  |  17.3 KB  |  337 lines

  1. """
  2. Search module
  3.  
  4. Nuka1195
  5. """
  6.  
  7. import sys
  8. import os
  9. import xbmc
  10. import xbmcgui
  11.  
  12. from utilities import *
  13. import database
  14.  
  15. _ = sys.modules[ "__main__" ].__language__
  16. __scriptname__ = sys.modules[ "__main__" ].__scriptname__
  17. __version__ = sys.modules[ "__main__" ].__version__
  18. __svn_revision__ = sys.modules[ "__main__" ].__svn_revision__
  19.  
  20.  
  21. class GUI( xbmcgui.WindowXMLDialog ):
  22.     """ Settings module: used for changing settings """
  23.     CONTROL_GENRE_LIST = 100
  24.     CONTROL_STUDIO_LIST = 110
  25.     CONTROL_ACTOR_LIST = 120
  26.     CONTROL_RATING_LIST = 130
  27.     CONTROL_QUALITY_LIST = 140
  28.     CONTROL_EXTRA_LIST = 150
  29.     CONTROL_SQL_TEXTBOX = 75
  30.     CONTROL_TITLE_LABEL = 20
  31.     CONTROL_VERSION_LABEL = 30
  32.     CONTROL_SQL_RESULTS_LABEL = 40
  33.     CONTROL_BUTTONS = ( 250, 251, 257, 258, )
  34.     CONTROL_LISTS = ( CONTROL_GENRE_LIST, CONTROL_STUDIO_LIST, CONTROL_ACTOR_LIST, CONTROL_RATING_LIST, CONTROL_QUALITY_LIST, CONTROL_EXTRA_LIST, )
  35.     CONTROL_LISTS_NOT = ( CONTROL_GENRE_LIST + 2, CONTROL_STUDIO_LIST + 2, CONTROL_ACTOR_LIST + 2, CONTROL_RATING_LIST + 2, CONTROL_QUALITY_LIST + 2, CONTROL_EXTRA_LIST + 2, )
  36.  
  37.     def __init__( self, *args, **kwargs ):
  38.         xbmcgui.WindowXMLDialog.__init__( self, *args, **kwargs )
  39.         self.dialog = xbmcgui.DialogProgress()
  40.         self.dialog.create( _( 106 ), _( 97 ), _( 1005 ) )
  41.  
  42.     def onInit( self ):
  43.         xbmcgui.lock()
  44.         self._set_labels()
  45.         self._clear_variables()
  46.         self._set_functions()
  47.         self._setup_special()
  48.         xbmcgui.unlock()
  49.         self.dialog.close()
  50.  
  51.     def _set_labels( self ):
  52.         try:
  53.             self.getControl( self.CONTROL_TITLE_LABEL ).setLabel( __scriptname__ )
  54.             self.getControl( self.CONTROL_VERSION_LABEL ).setLabel( "%s: %s-%s" % ( _( 1006 ), __version__, __svn_revision__, ) )
  55.             self.getControl( self.CONTROL_SQL_RESULTS_LABEL ).setLabel( "%s:" % ( _( 93 ), ) )
  56.             self.getControl( 102 ).setLabel( _( 113 ) )
  57.             self.getControl( 112 ).setLabel( _( 114 ) )
  58.             self.getControl( 122 ).setLabel( _( 115 ) )
  59.             self.getControl( 132 ).setLabel( _( 116 ) )
  60.             self.getControl( 142 ).setLabel( _( 117 ) )
  61.             self.getControl( 152 ).setLabel( _( 118 ) )
  62.             for button in self.CONTROL_BUTTONS:
  63.                 self.getControl( button ).setLabel( _( button ) )
  64.         except: pass
  65.  
  66.     def _set_functions( self ):
  67.         self.functions = {}
  68.         self.functions[ self.CONTROL_BUTTONS[ 0 ] ] = self._perform_search
  69.         self.functions[ self.CONTROL_BUTTONS[ 1 ] ] = self._close_dialog
  70.         self.functions[ self.CONTROL_BUTTONS[ 2 ] ] = self._save_sql
  71.         self.functions[ self.CONTROL_BUTTONS[ 3 ] ] = self._clear_sql
  72.  
  73.     def _clear_variables( self ):
  74.         self.query = ""
  75.         self.query_genres = ""
  76.         self.query_studios = ""
  77.         self.query_actors = ""
  78.         self.query_ratings = ""
  79.         self.query_qualities = ""
  80.         self.query_genres_not = False
  81.         self.query_studios_not = False
  82.         self.query_actors_not = False
  83.         self.query_ratings_not = False
  84.         self.query_qualities_not = False
  85.         self.query_extras_not = False
  86.         self.query_search_incomplete = ""
  87.         self.query_search_favorites = ""
  88.         self.query_search_saved = ""
  89.         self.query_search_watched = ""
  90.  
  91.     def _setup_special( self ):
  92.         """ calls any special defs """
  93.         self._setup_trailer_quality()
  94.         self._setup_categories()
  95.  
  96.     def _setup_categories( self ):
  97.         query = database.Query()
  98.         records = database.Records()
  99.         self.genres = records.fetch( query[ "genre_category_list" ], all=True )
  100.         self.studios = records.fetch( query[ "studio_category_list" ], all=True )
  101.         self.actors = records.fetch( query[ "actor_category_list" ], all=True )
  102.         self.ratings = [ ( _( 92 ), ) ]
  103.         self.ratings += records.fetch( query[ "rating_category_list" ], all=True )
  104.         records.close()
  105.         self._get_custom_sql( True )
  106.  
  107.     def _fill_lists( self, fill=True, query="" ):
  108.         for count, genre in enumerate( self.genres ):
  109.             if ( fill ):
  110.                 self.getControl( self.CONTROL_GENRE_LIST ).addItem( genre[ 1 ].replace( "Newest", _( 150 ) ).replace( "Exclusives", _( 151 ) ) )
  111.             selected = ( "idGenre=%d\n" % genre[ 0 ] in query or "idGenre=%d)" % genre[ 0 ] in query or "idGenre!=%d\n" % genre[ 0 ] in query or "idGenre!=%d)" % genre[ 0 ] in query )
  112.             self.getControl( self.CONTROL_GENRE_LIST ).getListItem( count ).select( selected )
  113.         self.getControl( self.CONTROL_GENRE_LIST + 2 ).setSelected( "idGenre!=" in query )
  114.         for count, studio in enumerate( self.studios ):
  115.             if ( fill ):
  116.                 self.getControl( self.CONTROL_STUDIO_LIST ).addItem( studio[ 1 ] )
  117.             selected = ( "idStudio=%d\n" % studio[ 0 ] in query or "idStudio=%d)" % studio[ 0 ] in query or "idStudio!=%d\n" % studio[ 0 ] in query or "idStudio!=%d)" % studio[ 0 ] in query )
  118.             self.getControl( self.CONTROL_STUDIO_LIST ).getListItem( count ).select( selected )
  119.         self.getControl( self.CONTROL_STUDIO_LIST + 2 ).setSelected( "idStudio!=" in query )
  120.         for count, actor in enumerate( self.actors ):
  121.             if ( fill ):
  122.                 self.getControl( self.CONTROL_ACTOR_LIST ).addItem( actor[ 1 ] )
  123.             selected = ( "idActor=%d\n" % actor[ 0 ] in query or "idActor=%d)" % actor[ 0 ] in query or "idActor!=%d\n" % actor[ 0 ] in query or "idActor!=%d)" % actor[ 0 ] in query )
  124.             self.getControl( self.CONTROL_ACTOR_LIST ).getListItem( count ).select( selected )
  125.         self.getControl( self.CONTROL_ACTOR_LIST + 2 ).setSelected( "idActor!=" in query )
  126.         for count, rating in enumerate( self.ratings ):
  127.             if ( fill ):
  128.                 self.getControl( self.CONTROL_RATING_LIST ).addItem( rating[ 0 ] )
  129.             selected = ( "rating='%s'" % rating[ 0 ] in query or "rating!='%s'" % rating[ 0 ] in query )
  130.             self.getControl( self.CONTROL_RATING_LIST ).getListItem( count ).select( selected )
  131.         self.getControl( self.CONTROL_RATING_LIST + 2 ).setSelected( "rating!=" in query )
  132.         for count, quality in enumerate( self.quality ):
  133.             if ( fill ):
  134.                 self.getControl( self.CONTROL_QUALITY_LIST ).addItem( quality )
  135.             quality_text = ( "%320.mov%", "%480.mov%", "%640%.mov%", "%480p.mov%", "%720p.mov%", "%1080p.mov%", )[ count ]
  136.             selected = "trailer_urls LIKE '%s'" % quality_text in query
  137.             self.getControl( self.CONTROL_QUALITY_LIST ).getListItem( count ).select( selected )
  138.         self.getControl( self.CONTROL_QUALITY_LIST + 2 ).setSelected( "NOT (movies.trailer_urls" in query )
  139.         if ( fill ):
  140.             self.getControl( self.CONTROL_EXTRA_LIST ).addItem( _( 2150 ) )
  141.         selected = ( query != "" and "trailer_urls IS NOT NULL" not in query )
  142.         self.getControl( self.CONTROL_EXTRA_LIST ).getListItem( 0 ).select( selected )
  143.         if ( fill ):
  144.             self.getControl( self.CONTROL_EXTRA_LIST ).addItem( _( 2151 ) )
  145.         selected = "movies.favorite" in query
  146.         self.getControl( self.CONTROL_EXTRA_LIST ).getListItem( 1 ).select( selected )
  147.         if ( fill ):
  148.             self.getControl( self.CONTROL_EXTRA_LIST ).addItem( _( 2152 ) )
  149.         selected = "saved_location" in query
  150.         self.getControl( self.CONTROL_EXTRA_LIST ).getListItem( 2 ).select( selected )
  151.         if ( fill ):
  152.             self.getControl( self.CONTROL_EXTRA_LIST ).addItem( _( 2153 ) )
  153.         selected = "times_watched" in query
  154.         self.getControl( self.CONTROL_EXTRA_LIST ).getListItem( 3 ).select( selected )
  155.         self.getControl( self.CONTROL_EXTRA_LIST + 2 ).setSelected( "favorite!=" in query or "saved_location=" in query or "times_watched=" in query )
  156.         if ( query ): self._create_sql( force_build=True )
  157.  
  158.     def _setup_trailer_quality( self ):
  159.         self.quality = ( _( 2020 ), _( 2021 ), _( 2022 ), "480p", "720p", "1080p", )
  160.  
  161.     def _create_sql( self, controlId=-1, force_build=False ):
  162.         # set genre selections
  163.         if ( controlId in ( self.CONTROL_GENRE_LIST, self.CONTROL_GENRE_LIST + 2, ) or force_build ):
  164.             genres = ""
  165.             self.query_genres_not = self.getControl( self.CONTROL_GENRE_LIST + 2 ).getSelected()
  166.             for pos, genre in enumerate( self.genres ):
  167.                 if ( self.getControl( self.CONTROL_GENRE_LIST ).getListItem( pos ).isSelected() ):
  168.                     genres += "genre_link_movie.idGenre%s=%d\n %s " % ( ( "", "!", )[ self.query_genres_not ], genre[ 0 ], ( "OR", "AND", )[ self.query_genres_not ] )
  169.             if ( genres ):
  170.                 genres = "(genre_link_movie.idMovie=movies.idMovie\n AND (%s))" % ( genres[ : -5 ].strip(), )
  171.             self.query_genres = genres
  172.  
  173.         if ( controlId in ( self.CONTROL_STUDIO_LIST, self.CONTROL_STUDIO_LIST + 2, ) or force_build ):
  174.             # set studio selections
  175.             studios = ""
  176.             self.query_studios_not = self.getControl( self.CONTROL_STUDIO_LIST + 2 ).getSelected()
  177.             for pos, studio in enumerate( self.studios ):
  178.                 if ( self.getControl( self.CONTROL_STUDIO_LIST ).getListItem( pos ).isSelected() ):
  179.                     studios += "studio_link_movie.idStudio%s=%d\n %s " % ( ( "", "!", )[ self.query_studios_not ], studio[ 0 ], ( "OR", "AND", )[ self.query_studios_not ] )
  180.             if ( studios ):
  181.                 studios = "(studio_link_movie.idMovie=movies.idMovie\n AND (%s))" % ( studios[ : -5 ].strip(), )
  182.             self.query_studios = studios
  183.  
  184.         if ( controlId in ( self.CONTROL_ACTOR_LIST, self.CONTROL_ACTOR_LIST + 2, ) or force_build ):
  185.             # set actor selections
  186.             actors = ""
  187.             self.query_actors_not = self.getControl( self.CONTROL_ACTOR_LIST + 2 ).getSelected()
  188.             for pos, actor in enumerate( self.actors ):
  189.                 if ( self.getControl( self.CONTROL_ACTOR_LIST ).getListItem( pos ).isSelected() ):
  190.                     actors += "actor_link_movie.idActor%s=%d\n %s " % ( ( "", "!", )[ self.query_actors_not ], actor[ 0 ], ( "OR", "AND", )[ self.query_actors_not ] )
  191.             if ( actors ):
  192.                 actors = "(actor_link_movie.idMovie=movies.idMovie\n AND (%s))" % ( actors[ : -5 ].strip(), )
  193.             self.query_actors = actors
  194.  
  195.         if ( controlId in ( self.CONTROL_RATING_LIST, self.CONTROL_RATING_LIST + 2, ) or force_build ):
  196.             # set rating selections
  197.             ratings = ""
  198.             self.query_ratings_not = self.getControl( self.CONTROL_RATING_LIST + 2 ).getSelected()
  199.             for pos, rating in enumerate( self.ratings ):
  200.                 if ( self.getControl( self.CONTROL_RATING_LIST ).getListItem( pos ).isSelected() ):
  201.                     ratings += "movies.rating%s='%s'\n %s " % ( ( "", "!", )[ self.query_ratings_not ], ( rating[ 0 ], "", )[ rating[ 0 ] == _( 92 ) ], ( "OR", "AND", )[ self.query_ratings_not ] )
  202.             if ( ratings ):
  203.                 ratings = "(%s)" % ( ratings[ : -4 ].strip(), )
  204.             self.query_ratings = ratings
  205.  
  206.         if ( controlId in ( self.CONTROL_QUALITY_LIST, self.CONTROL_QUALITY_LIST + 2, ) or force_build ):
  207.             # set trailer quality selections
  208.             qualities = ""
  209.             self.query_qualities_not = self.getControl( self.CONTROL_QUALITY_LIST + 2 ).getSelected()
  210.             for pos, quality in enumerate( self.quality ):
  211.                 if ( self.getControl( self.CONTROL_QUALITY_LIST ).getListItem( pos ).isSelected() ):
  212.                     quality_text = ( "%320.mov%", "%480.mov%", "%640%.mov%", "%480p.mov%", "%720p.mov%", "%1080p.mov%", )[ pos ]
  213.                     qualities += "movies.trailer_urls LIKE '%s'\n %s " % ( quality_text, ( "OR", "AND", )[ self.query_qualities_not ] )
  214.             if ( qualities ):
  215.                 qualities = "%s(%s)" % ( ( "", "NOT ", )[ self.query_qualities_not ], qualities[ : -5 ].strip(), )
  216.             self.query_qualities = qualities
  217.  
  218.         search_incomplete = ""
  219.         if ( not self.getControl( self.CONTROL_EXTRA_LIST ).getListItem( 0 ).isSelected() ):
  220.             search_incomplete = "\n AND movies.trailer_urls IS NOT NULL"
  221.         self.query_search_incomplete = search_incomplete
  222.         if ( controlId in ( self.CONTROL_EXTRA_LIST, self.CONTROL_EXTRA_LIST + 2, ) or force_build ):
  223.             self.query_extras_not = self.getControl( self.CONTROL_EXTRA_LIST + 2 ).getSelected()
  224.             # set extra settings selections
  225.             search_favorites = ""
  226.             if ( self.getControl( self.CONTROL_EXTRA_LIST ).getListItem( 1 ).isSelected() ):
  227.                 search_favorites = "\n AND movies.favorite%s=1" % ( ( "", "!", )[ self.query_extras_not ], )
  228.             self.query_search_favorites = search_favorites
  229.             search_saved = ""
  230.             if ( self.getControl( self.CONTROL_EXTRA_LIST ).getListItem( 2 ).isSelected() ):
  231.                 search_saved = "\n AND movies.saved_location%s=''" % ( ( "!", "", )[ self.query_extras_not ], )
  232.             self.query_search_saved = search_saved
  233.             search_watched = ""
  234.             if ( self.getControl( self.CONTROL_EXTRA_LIST ).getListItem( 3 ).isSelected() ):
  235.                 search_watched = "\n AND movies.times_watched%s0" % ( ( ">", "=", )[ self.query_extras_not ], )
  236.             self.query_search_watched = search_watched
  237.  
  238.         self.query = ""
  239.         if ( self.query_genres or self.query_studios or self.query_actors or self.query_ratings or self.query_qualities ):
  240.             tables = "movies"
  241.             where = ""
  242.             if ( self.query_genres ):
  243.                 tables += ", genre_link_movie"
  244.                 where += self.query_genres
  245.                 where += "\n AND "
  246.             if ( self.query_studios ):
  247.                 tables += ", studio_link_movie"
  248.                 where += self.query_studios
  249.                 where += "\n AND "
  250.             if ( self.query_actors ):
  251.                 tables += ", actor_link_movie"
  252.                 where += self.query_actors
  253.                 where += "\n AND "
  254.             if ( self.query_ratings ):
  255.                 where += self.query_ratings
  256.                 where += "\n AND "
  257.             if ( self.query_qualities ):
  258.                 where += self.query_qualities
  259.                 where += "\n AND "
  260.             where = "\n WHERE " + where[ : -6 ]
  261.             self.query = "SELECT DISTINCT movies.*\n FROM " + tables + where
  262.             self.query += self.query_search_incomplete + self.query_search_favorites + self.query_search_saved + self.query_search_watched
  263.             self.query += "\n ORDER BY movies.title;"
  264.         self._set_sql()
  265.         if ( self.query ): self._run_sql()
  266.             
  267.     def _set_sql( self ):
  268.         self.getControl( self.CONTROL_SQL_TEXTBOX ).reset()
  269.         if ( not self.query ):
  270.             self.getControl( self.CONTROL_SQL_TEXTBOX ).setText( _( 94 ) )
  271.         else:
  272.             self.getControl( self.CONTROL_SQL_TEXTBOX ).setText( self.query )
  273.         self.getControl( self.CONTROL_BUTTONS[ 0 ] ).setEnabled( self.query != "" )
  274.         self.getControl( self.CONTROL_BUTTONS[ 2 ] ).setEnabled( self.query != "" )
  275.         self._enable_load_button()
  276.  
  277.     def _run_sql( self ):
  278.         self.getControl( self.CONTROL_SQL_RESULTS_LABEL ).setLabel( "%s: %s" % ( _( 93 ), _( 85 ),) )
  279.         records = database.Records()
  280.         trailers = records.fetch( self.query, all=True )
  281.         records.close()
  282.         self.getControl( self.CONTROL_SQL_RESULTS_LABEL ).setLabel( "%s: %d" % ( _( 93 ), len( trailers ),) )
  283.  
  284.     def _clear_sql( self ):
  285.         xbmcgui.lock()
  286.         try:
  287.             if ( self.query ):
  288.                 self._clear_variables()
  289.                 self._set_sql()
  290.                 self._fill_lists( False, self.query )
  291.                 self.getControl( self.CONTROL_SQL_RESULTS_LABEL ).setLabel( "%s:" % ( _( 93 ), ) )
  292.             else: self._get_custom_sql()
  293.             self._enable_load_button()
  294.         except: pass
  295.         xbmcgui.unlock()
  296.  
  297.     def _save_sql( self ):
  298.         ok = save_custom_sql( self.query )
  299.  
  300.     def _get_custom_sql( self, fill=False ):
  301.         xbmcgui.lock()
  302.         try:
  303.             self.query = get_custom_sql()
  304.             self._set_sql()
  305.             self._fill_lists( fill, self.query )
  306.             if ( self.query ): self._run_sql()
  307.         except: pass
  308.         xbmcgui.unlock()
  309.  
  310.     def _enable_load_button( self ):
  311.         label = ( os.path.isfile( os.path.join( BASE_DATA_PATH, "custom.sql" ) ) and self.query == "" )
  312.         self.getControl( self.CONTROL_BUTTONS[ 3 ] ).setLabel( _( 258 + label ) )
  313.  
  314.     def _perform_search( self ):
  315.         self._close_dialog( self.query )
  316.  
  317.     def _close_dialog( self, query=None ):
  318.         """ closes this dialog window """
  319.         self.query = query
  320.         self.close()
  321.  
  322.     def onClick( self, controlId ):
  323.         if ( controlId in self.CONTROL_LISTS ):
  324.             self.getControl( controlId ).getSelectedItem().select( not self.getControl( controlId ).getSelectedItem().isSelected() )
  325.             self._create_sql( controlId )
  326.         elif ( controlId in self.CONTROL_LISTS_NOT ):
  327.             self._create_sql( controlId )
  328.         elif ( controlId in self.CONTROL_BUTTONS ):
  329.             self.functions[ controlId ]()
  330.  
  331.     def onFocus( self, controlId ):
  332.         pass
  333.  
  334.     def onAction( self, action ):
  335.         if ( action in ACTION_CANCEL_DIALOG ):
  336.             self._close_dialog()
  337.