home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2010 May / maximum-cd-2010-05.iso / DiscContents / boxee-0.9.20.10711.exe / scripts / Lyrics / resources / lib / settings.py < prev    next >
Encoding:
Python Source  |  2009-07-20  |  10.1 KB  |  251 lines

  1. """
  2. Settings module
  3.  
  4. Nuka1195
  5. """
  6.  
  7. import sys
  8. import os
  9. import xbmc
  10. import xbmcgui
  11.  
  12. from resources.lib.utilities import *
  13.  
  14. _ = sys.modules[ "__main__" ].__language__
  15. __scriptname__ = sys.modules[ "__main__" ].__scriptname__
  16. __version__ = sys.modules[ "__main__" ].__version__
  17. __svn_revision__ = sys.modules[ "__main__" ].__svn_revision__
  18.  
  19.  
  20. class GUI( xbmcgui.WindowXMLDialog ):
  21.     """ Settings module: used for changing settings """
  22.     def __init__( self, *args, **kwargs ):
  23.         xbmcgui.WindowXMLDialog.__init__( self )
  24.  
  25.     def onInit( self ):
  26.         self._get_settings()
  27.         self._set_labels()
  28.         self._set_functions()
  29.         self._setup_special()
  30.         self._set_restart_required()
  31.         self._set_controls_values()
  32.  
  33.     def _get_settings( self, defaults=False ):
  34.         """ reads settings """
  35.         self.settings = Settings().get_settings( defaults=defaults )
  36.  
  37.     def _set_labels( self ):
  38.         xbmcgui.lock()
  39.         try:
  40.             self.getControl( 30 ).setLabel( "%s: %s-%s" % ( _( 1006 ), __version__, __svn_revision__, ) )
  41.             ## setEnabled( False ) if not used
  42.             #self.getControl( 253 ).setVisible( False )
  43.             #self.getControl( 253 ).setEnabled( False )
  44.         except: pass
  45.         xbmcgui.unlock()
  46.  
  47.     def _set_functions( self ):
  48.         self.functions = {}
  49.         self.functions[ 250 ] = self._save_settings
  50.         self.functions[ 251 ] = self._close_dialog
  51.         self.functions[ 252 ] = self._update_script
  52.         self.functions[ 253 ] = self._show_credits
  53.         self.functions[ 254 ] = self._play_music
  54.         self.functions[ 255 ] = self._get_defaults
  55.         for x in range( 1, len( self.settings ) ):
  56.             self.functions[ 200 + x ] = eval( "self._change_setting%d" % x )
  57.  
  58. ##### Special defs, script dependent, remember to call them from _setup_special #################
  59.     
  60.     def _setup_special( self ):
  61.         """ calls any special defs """
  62.         self._setup_scrapers()
  63.         self._setup_filename_format()
  64.  
  65.     def _setup_scrapers( self ):
  66.         """ special def for setting up scraper choices """
  67.         import re
  68.         self.scrapers_title = []
  69.         pattern = """__title__.*?["'](.*?)["']"""
  70.         base_path = os.path.join( BASE_RESOURCE_PATH, "scrapers" )
  71.         self.scrapers = []
  72.         scrapers = os.listdir( base_path )
  73.         for scraper in scrapers:
  74.             if ( os.path.isdir( os.path.join( base_path, scraper ) ) ):
  75.                 try:
  76.                     self.scrapers += [ scraper ]
  77.                     file_object = open( os.path.join( base_path, scraper, "lyricsScraper.py" ), "r" )
  78.                     file_data = file_object.read()
  79.                     file_object.close()
  80.                     title = re.findall( pattern, file_data )
  81.                     if ( not title ): raise
  82.                 except:
  83.                     title = [ scraper ]
  84.                 self.scrapers_title += title
  85.         try: self.current_scraper = self.scrapers.index( self.settings[ "scraper" ] )
  86.         except: self.current_scraper = 0
  87.  
  88.     def _setup_filename_format( self ):
  89.         self.filename_format = ( _( 2070 ), _( 2071 ), _( 2072 ), )
  90.     
  91.     def _set_restart_required( self ):
  92.         """ copies self.settings and adds any settings that require a restart on change """
  93.         self.settings_original = self.settings.copy()
  94.         self.settings_restart = ( "scraper", )
  95.         self.settings_refresh = ( "save_lyrics", "lyrics_path", "use_filename", "filename_format", )
  96.  
  97. ###### End of Special defs #####################################################
  98.  
  99.     def _set_controls_values( self ):
  100.         """ sets the value labels """
  101.         xbmcgui.lock()
  102.         try:
  103.             self.getControl( 201 ).setLabel( self.getControl( 201 ).getLabel(), label2=self.scrapers_title[ self.current_scraper ] )
  104.             self.getControl( 202 ).setSelected( self.settings[ "save_lyrics" ] )
  105.             self.getControl( 203 ).setLabel( self.getControl( 203 ).getLabel(), label2=self.settings[ "lyrics_path" ] )
  106.             self.getControl( 203 ).setEnabled( self.settings[ "save_lyrics" ] )
  107.             self.getControl( 204 ).setSelected( self.settings[ "smooth_scrolling" ] )
  108.             self.getControl( 205 ).setSelected( self.settings[ "show_viz" ] )
  109.             self.getControl( 206 ).setSelected( self.settings[ "use_filename" ] )
  110.             self.getControl( 207 ).setLabel( self.getControl( 207 ).getLabel(), label2=self.filename_format[ self.settings[ "filename_format" ] ] )
  111.             self.getControl( 208 ).setLabel( self.getControl( 208 ).getLabel(), label2=self.settings[ "music_path" ] )
  112.             self.getControl( 209 ).setSelected( self.settings[ "shuffle" ] )
  113.             self.getControl( 209 ).setEnabled( self.settings[ "music_path" ] != "" )
  114.             self.getControl( 210 ).setSelected( self.settings[ "compatible" ] )
  115.             self.getControl( 210 ).setEnabled( self.settings[ "save_lyrics" ] )
  116.             self.getControl( 211 ).setSelected( self.settings[ "use_extension" ] )
  117.             self.getControl( 211 ).setEnabled( self.settings[ "save_lyrics" ] )
  118.             self.getControl( 250 ).setEnabled( self.settings_original != self.settings )
  119.             self.getControl( 254 ).setEnabled( not xbmc.Player().isPlayingAudio() )
  120.         except:
  121.             pass
  122.         xbmcgui.unlock()
  123.  
  124.     def _change_setting1( self ):
  125.         """ changes settings #1 """
  126.         self.current_scraper += 1
  127.         if ( self.current_scraper == len( self.scrapers ) ): self.current_scraper = 0
  128.         self.settings[ "scraper" ] = self.scrapers[ self.current_scraper ]
  129.         self._set_controls_values()
  130.  
  131.     def _change_setting2( self ):
  132.         """ changes settings #2 """
  133.         self.settings[ "save_lyrics" ] = not self.settings[ "save_lyrics" ]
  134.         self._set_controls_values()
  135.  
  136.     def _change_setting3( self ):
  137.         """ changes settings #3 """
  138.         self.settings[ "lyrics_path" ] = get_browse_dialog( self.settings[ "lyrics_path" ], _( self.controlId ), 3 )
  139.         self._set_controls_values()
  140.  
  141.     def _change_setting4( self ):
  142.         """ changes settings #4 """
  143.         self.settings[ "smooth_scrolling" ] = not self.settings[ "smooth_scrolling" ]
  144.         self._set_controls_values()
  145.  
  146.     def _change_setting5( self ):
  147.         """ changes settings #5 """
  148.         self.settings[ "show_viz" ] = not self.settings[ "show_viz" ]
  149.         self._set_controls_values()
  150.  
  151.     def _change_setting6( self ):
  152.         """ changes settings #6 """
  153.         self.settings[ "use_filename" ] = not self.settings[ "use_filename" ]
  154.         self._set_controls_values()
  155.  
  156.     def _change_setting7( self ):
  157.         """ changes settings #7 """
  158.         self.settings[ "filename_format" ] += 1
  159.         if ( self.settings[ "filename_format" ] == len( self.filename_format ) ):
  160.             self.settings[ "filename_format" ] = 0
  161.         self._set_controls_values()
  162.  
  163.     def _change_setting8( self ):
  164.         """ changes settings #8 """
  165.         self.settings[ "music_path" ] = get_browse_dialog( self.settings[ "music_path" ], _( self.controlId ), 0 )
  166.         self._set_controls_values()
  167.  
  168.     def _change_setting9( self ):
  169.         """ changes settings #9 """
  170.         self.settings[ "shuffle" ] = not self.settings[ "shuffle" ]
  171.         self._set_controls_values()
  172.  
  173.     def _change_setting10( self ):
  174.         """ changes settings #10 """
  175.         self.settings[ "compatible" ] = not self.settings[ "compatible" ]
  176.         self._set_controls_values()
  177.  
  178.     def _change_setting11( self ):
  179.         """ changes settings #11 """
  180.         self.settings[ "use_extension" ] = not self.settings[ "use_extension" ]
  181.         self._set_controls_values()
  182.  
  183. ##### End of unique defs ######################################################
  184.     
  185.     def _save_settings( self ):
  186.         """ saves settings """
  187.         ok = Settings().save_settings( self.settings )
  188.         if ( not ok ):
  189.             ok = xbmcgui.Dialog().ok( __scriptname__, _( 230 ) )
  190.         else:
  191.             self._check_for_restart()
  192.  
  193.     def _check_for_restart( self ):
  194.         """ checks for any changes that require a restart to take effect """
  195.         restart = False
  196.         refresh = False
  197.         for setting in self.settings_restart:
  198.             if ( self.settings_original[ setting ] != self.settings[ setting ] ):
  199.                 restart = True
  200.                 break
  201.         for setting in self.settings_refresh:
  202.             if ( self.settings_original[ setting ] != self.settings[ setting ] ):
  203.                 refresh = True
  204.                 break
  205.         self._close_dialog( True, restart, refresh )
  206.     
  207.     def _update_script( self ):
  208.         """ checks for updates to the script """
  209.         import resources.lib.update as update
  210.         updt = update.Update()
  211.         del updt
  212.  
  213.     def _show_credits( self ):
  214.         """ shows a credit window """
  215.         show_credits()
  216.  
  217.     def _play_music( self ):
  218.         """ plays a folder of music """
  219.         if ( self.settings_original != self.settings ):
  220.             self._save_settings()
  221.         else:
  222.             self._close_dialog()
  223.         import resources.lib.playlist as playlist
  224.         playlist = playlist.create_playlist( ( self.settings[ "music_path" ], ), self.settings[ "shuffle" ] )
  225.         xbmc.Player().play( playlist )
  226.         xbmc.executebuiltin( "RunScript(%s)" % os.path.join( os.getcwd(), "default.py" ) )
  227.         
  228.     def _get_defaults( self ):
  229.         """ resets values to defaults """
  230.         self._get_settings( defaults=True )
  231.         self._set_controls_values()
  232.  
  233.     def _close_dialog( self, changed=False, restart=False, refresh=False ):
  234.         """ closes this dialog window """
  235.         self.changed = changed
  236.         self.restart = restart
  237.         self.refresh = refresh
  238.         self.close()
  239.  
  240.     def onClick( self, controlId ):
  241.         #xbmc.sleep(5)
  242.         self.functions[ controlId ]()
  243.  
  244.     def onFocus( self, controlId ):
  245.         xbmc.sleep( 5 )
  246.         self.controlId = self.getFocusId()
  247.  
  248.     def onAction( self, action ):
  249.         if ( action in ACTION_CANCEL_DIALOG ):
  250.             self._close_dialog()
  251.