Pastebin is 300% more awesome when you are logged in. Sign Up, it's FREE!
Guest

launcher_plugin.py

By: a guest on Oct 7th, 2010  |  syntax: Python  |  size: 47.04 KB  |  hits: 97  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. # This program is free software; you can redistribute it and/or modify
  2. # it under the terms of the GNU General Public License as published by
  3. # the Free Software Foundation; version 2 of the License.
  4. #
  5. # This program is distributed in the hope that it will be useful,
  6. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  7. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  8. # GNU General Public License for more details.
  9.  
  10.  
  11. """
  12.    Plugin for Launching an applications
  13. """
  14.  
  15. # main imports
  16. import sys
  17. import os
  18. import xbmc
  19. import xbmcgui
  20. import xbmcplugin
  21.  
  22. import time
  23. import re
  24. import urllib
  25.  
  26. BASE_PATH = os.getcwd()
  27. print BASE_PATH
  28.  
  29. try:
  30.     from xbmcaddon import Addon
  31.  
  32.     # source path for launchers data
  33.     PLUGIN_DATA_PATH = xbmc.translatePath( os.path.join( "special://profile/addon_data", "plugin.program.launcher") )
  34.  
  35.     __settings__ = Addon( id="plugin.program.launcher" )
  36.     __language__ = __settings__.getLocalizedString
  37.     print "Mode AddOn ON"
  38.     print PLUGIN_DATA_PATH
  39.  
  40.  
  41. except :
  42.     # source path for launchers data
  43.     PLUGIN_DATA_PATH = xbmc.translatePath( os.path.join( "special://profile/plugin_data", "programs", sys.modules[ "__main__" ].__plugin__) )
  44.  
  45.     __settings__ = xbmcplugin
  46.     __language__ = __language__
  47.     print "Mode plugin ON"
  48.     print PLUGIN_DATA_PATH
  49.  
  50. # source path for launchers data
  51. BASE_CURRENT_SOURCE_PATH = os.path.join( PLUGIN_DATA_PATH , "launchers.xml" )
  52. SHORTCUT_FILE = os.path.join( PLUGIN_DATA_PATH , "shortcut.cut" )
  53. THUMBS_PATH = os.path.join( PLUGIN_DATA_PATH , "thumbs" )
  54.  
  55. REMOVE_COMMAND = "%%REMOVE%%"
  56. ADD_COMMAND = "%%ADD%%"
  57. IMPORT_COMMAND = "%%IMPORT%%"
  58. SCAN_COMMAND = "%%SCAN%%"
  59. RENAME_COMMAND = "%%RENAME%%"
  60. SET_THUMB_COMMAND = "%%SETTHUMB%%"
  61. WAIT_TOGGLE_COMMAND = "%%WAIT_TOGGLE%%"
  62. COMMAND_ARGS_SEPARATOR = "^^"
  63. SEARCH_COMMAND = "%%SEARCH%%"#search
  64. SEARCH_ALL_COMMAND = "%%SEARCH_ALL%%"#search all
  65.  
  66. # breaks the plugin partly
  67. # when using
  68. # xbmc.executebuiltin("ReplaceWindow(Programs,%s?%s/%s/%s)" % (self._path, launcherName, search, SEARCH_COMMAND))
  69. # for example
  70. #pDialog = xbmcgui.DialogProgress()
  71. #pDialog.create( sys.modules[ "__main__" ].__plugin__ )
  72.  
  73. class Main:
  74.     BASE_CACHE_PATH = xbmc.translatePath(os.path.join( "special://profile/Thumbnails", "Pictures" ))
  75.     launchers = {}
  76.  
  77.     ''' initializes plugin and run the requiered action
  78.        arguments:
  79.            argv[0] - the path of the plugin (supplied by XBMC)
  80.            argv[1] - the handle of the plugin (supplied by XBMC)
  81.            argv[2] - one of the following (__language__( 30000 ) and 'rom' can be any launcher name or rom name created with the plugin) :
  82.                /launcher - open the specific launcher (if exists) and browse its roms
  83.                            if the launcher is standalone - run it.
  84.                /launcher/rom - run the specifiec rom using it's launcher.
  85.                                 ignore command if doesn't exists.
  86.                /launcher/%%REMOVE%% - remove the launcher
  87.                /launcher/%%ADD%% - add a new rom (open wizard)
  88.                /launcher/rom/%%REMOVE%% - remove the rom
  89.                /launcher/%%IMPORT%% - import roms from rom path into launcher
  90.                /launcher/%%SCAN%% - scan for launcher & roms data from the internet
  91.                /launcher/rom/%%SCAN%% - scan for rom data from the internet
  92.                /launcher/%%WAIT_TOGGLE%% - toggle wait state
  93.                /%%ADD%% - add a new launcher (open wizard)
  94.                
  95.                (blank)     - open a list of the available launchers. if no launcher exists - open the launcher creation wizard.
  96.    '''                        
  97.     def __init__( self ):
  98.         # store an handle pointer
  99.         self._handle = int(sys.argv[ 1 ])
  100.         print self._handle
  101.                    
  102.         self._path = sys.argv[ 0 ]
  103.        
  104.         # get users preference
  105.         self._get_settings()
  106.         self._load_launchers(self.get_xml_source())
  107.  
  108.         # if a commmand is passed as parameter
  109.         param = sys.argv[ 2 ]
  110.         if param:
  111.             param = param[1:]
  112.             command = param.split(COMMAND_ARGS_SEPARATOR)
  113.             dirname = os.path.dirname(command[0])
  114.             basename = os.path.basename(command[0])
  115.            
  116.             # check the action needed
  117.             if (dirname):
  118.                 launcher = dirname
  119.                 rom = basename
  120.                 if (rom == REMOVE_COMMAND):
  121.                     # check if it is a single rom or a launcher
  122.                     if (not os.path.dirname(launcher)):
  123.                         self._remove_launcher(launcher)
  124.                     else:
  125.                         self._remove_rom(os.path.dirname(launcher), os.path.basename(launcher))
  126.                 if (rom == RENAME_COMMAND):
  127.                     # check if it is a single rom or a launcher
  128.                     if (not os.path.dirname(launcher)):
  129.                         self._rename_launcher(launcher)
  130.                     else:
  131.                         self._rename_rom(os.path.dirname(launcher), os.path.basename(launcher))
  132.                 elif (rom == SCAN_COMMAND):
  133.                     # check if it is a single rom scan or a launcher scan
  134.                     if (not os.path.dirname(launcher)):
  135.                         self._scan_launcher(launcher)
  136.                     else:
  137.                         romname = os.path.basename(launcher)
  138.                         launcher = os.path.dirname(launcher)
  139.                         self._search_thumb(launcher, romname)
  140.                 elif (rom == SET_THUMB_COMMAND):
  141.                     thumb = command[1]
  142.                     # check if it is a single rom or a launcher
  143.                     if (not os.path.dirname(launcher)):
  144.                         self._set_thumb(launcher, "", thumb)
  145.                     else:
  146.                         romname = os.path.basename(launcher)
  147.                         launcher = os.path.dirname(launcher)
  148.                         self._set_thumb(launcher, romname, thumb)
  149.                 elif (rom == ADD_COMMAND):
  150.                     self._add_new_rom(launcher)
  151.                 elif (rom == IMPORT_COMMAND):
  152.                     self._import_roms(launcher)
  153.                 elif (rom == WAIT_TOGGLE_COMMAND):
  154.                     self._toggle_wait(launcher)
  155.                 elif (rom == SEARCH_COMMAND):#search
  156.                     # check if we need to get user input or search the rom list
  157.                     if (not os.path.dirname(launcher)):
  158.                         self._find_roms(launcher)
  159.                     else:
  160.                         romname = os.path.basename(launcher)
  161.                         launcher = os.path.dirname(launcher)
  162.                         self._find_add_roms(launcher, romname)
  163.                 elif (rom == SEARCH_ALL_COMMAND):#search all
  164.                     # check if we need to get user input or search the rom list
  165.                     self._find_add_all_roms(launcher)
  166.                 else:
  167.                     self._run_rom(launcher, rom)
  168.             else:
  169.                 launcher = basename
  170.  
  171.                 if (launcher == SEARCH_ALL_COMMAND):#search all
  172.                     # check if we need to get user input or search the rom list
  173.                     self._find_all_roms()
  174.                
  175.                 # if it's an add command
  176.                 elif (launcher == ADD_COMMAND):
  177.                     self._add_new_launcher()
  178.                 else:
  179.                     # if there is no rompath (a standalone launcher)
  180.                     if (self.launchers[launcher]["rompath"] == ""):
  181.                         # launch it
  182.                         self._run_launcher(launcher)
  183.                     else:
  184.                         # otherwise, list the roms
  185.                         self._get_roms(launcher)                    
  186.         else:
  187.             # otherwise get the list of the programs in the current folder
  188.             if (not self._get_launchers()):
  189.                 # if no launcher found - attempt to add a new one
  190.                 if (self._add_new_launcher()):
  191.                     self._get_launchers()
  192.                 else:
  193.                     xbmcplugin.endOfDirectory( handle=int( self._handle ), succeeded=False , cacheToDisc=False)
  194.                    
  195.     def _remove_rom(self, launcher, rom):        
  196.         dialog = xbmcgui.Dialog()
  197.         ret = dialog.yesno(__language__( 30000 ), __language__( 30010 ) % rom)
  198.         if (ret):
  199.             self.launchers[launcher]["roms"].pop(rom)
  200.             self._save_launchers()
  201.             xbmc.executebuiltin("ReplaceWindow(Programs,%s?%s)" % (self._path, launcher))
  202.            
  203.     def _remove_launcher(self, launcherName):
  204.         dialog = xbmcgui.Dialog()
  205.         ret = dialog.yesno(__language__( 30000 ), __language__( 30010 ) % launcherName)
  206.         if (ret):
  207.             self.launchers.pop(launcherName)
  208.             self._save_launchers()
  209.             xbmc.executebuiltin("ReplaceWindow(Programs,%s)" % (self._path))
  210.            
  211.     def _rename_rom(self, launcher, rom):        
  212.         keyboard = xbmc.Keyboard(self.launchers[launcher]["roms"][rom]["name"], __language__( 30018 ))
  213.         keyboard.doModal()
  214.         if (keyboard.isConfirmed()):
  215.             self.launchers[launcher]["roms"][rom]["name"] = keyboard.getText()
  216.             self._save_launchers()
  217.             xbmc.executebuiltin("ReplaceWindow(Programs,%s?%s)" % (self._path, launcher))
  218.        
  219.     def _rename_launcher(self, launcherName):
  220.         keyboard = xbmc.Keyboard(self.launchers[launcherName]["name"], __language__( 30025 ))
  221.         keyboard.doModal()
  222.         if (keyboard.isConfirmed()):
  223.             self.launchers[launcherName]["name"] = keyboard.getText()
  224.             self._save_launchers()
  225.             xbmc.executebuiltin("ReplaceWindow(Programs,%s)" % (self._path))
  226.            
  227.     def _run_launcher(self, launcherName):
  228.         if (self.launchers.has_key(launcherName)):
  229.             launcher = self.launchers[launcherName]
  230.             if (os.environ.get( "OS", "xbox" ) == "xbox"):
  231.                 xbmc.executebuiltin('XBMC.Runxbe(' + launcher["application"] + ')')
  232.             else:
  233.                 if (sys.platform == 'win32'):
  234.                     if (launcher["wait"] == "true"):
  235.                         cmd = "System.ExecWait"
  236.                     else:
  237.                         cmd = "System.Exec"
  238.                     #this minimizes xbmc some apps seems to need it
  239.                     xbmc.executehttpapi("Action(199)")
  240.                     xbmc.executebuiltin("%s(\\\"%s\\\" \\\"%s\\\")" % (cmd, launcher["application"], launcher["args"]))
  241.                     #this brings xbmc back
  242.                     xbmc.executehttpapi("Action(199)")
  243.                 elif (sys.platform.startswith('linux')):
  244.                     #this minimizes xbmc some apps seems to need it
  245.                     xbmc.executehttpapi("Action(199)")
  246.                     os.system("%s %s" % (launcher["application"], launcher["args"]))
  247.                     #this brings xbmc back
  248.                     xbmc.executehttpapi("Action(199)")
  249.                 elif (sys.platform.startswith('darwin')):
  250.                     #this minimizes xbmc some apps seems to need it
  251.                     xbmc.executehttpapi("Action(199)")
  252.                     os.system("\"%s\" %s" % (launcher["application"], launcher["args"]))
  253.                     #this brings xbmc back
  254.                     xbmc.executehttpapi("Action(199)")
  255.                 else:
  256.                     pass;
  257.                     # unsupported platform
  258.                              
  259.  
  260.     def _run_rom(self, launcherName, romName):
  261.         if (self.launchers.has_key(launcherName)):
  262.             launcher = self.launchers[launcherName]
  263.             if (launcher["roms"].has_key(romName)):
  264.                 rom = self.launchers[launcherName]["roms"][romName]
  265.                 if (os.environ.get( "OS", "xbox" ) == "xbox"):
  266.                     f=open(SHORTCUT_FILE, "wb")
  267.                     f.write("<shortcut>\n")
  268.                     f.write("    <path>" + launcher["application"] + "</path>\n")
  269.                     f.write("    <custom>\n")
  270.                     f.write("       <game>" + rom["filename"] + "</game>\n")
  271.                     f.write("    </custom>\n")
  272.                     f.write("</shortcut>\n")
  273.                     f.close()
  274.                     xbmc.executebuiltin('XBMC.Runxbe(' + SHORTCUT_FILE + ')')                    
  275.                 else:
  276.                     if (sys.platform == 'win32'):
  277.                         if (launcher["wait"] == "true"):
  278.                             cmd = "System.ExecWait"
  279.                         else:
  280.                             cmd = "System.Exec"
  281.                         xbmc.executebuiltin("%s(\\\"%s\\\" %s \\\"%s\\\")" % (cmd, launcher["application"], launcher["args"], rom["filename"]))
  282.                     elif (sys.platform.startswith('linux')):
  283.                         #this minimizes xbmc some apps seems to need it
  284.                         xbmc.executehttpapi("Action(199)")                   
  285.                         os.system("\"%s\" %s \"%s\"" % (launcher["application"], launcher["args"], rom["filename"]))
  286.                         #this brings xbmc back
  287.                         xbmc.executehttpapi("Action(199)")
  288.                     elif (sys.platform.startswith('darwin')):
  289.                         os.system("\"%s\" %s \"%s\"" % (launcher["application"], launcher["args"], rom["filename"]))
  290.                     else:
  291.                         pass;
  292.                         # unsupported platform
  293.  
  294.     ''' get an xml data from an xml file '''
  295.     def get_xml_source( self ):
  296.         try:
  297.             usock = open( BASE_CURRENT_SOURCE_PATH, 'r' )
  298.             # read source
  299.             xmlSource = usock.read()
  300.             # close socket
  301.             usock.close()
  302.             ok = True
  303.         except:
  304.             # oops print error message
  305.             print "ERROR: %s::%s (%d) - %s" % ( self.__class__.__name__, sys.exc_info()[ 2 ].tb_frame.f_code.co_name, sys.exc_info()[ 2 ].tb_lineno, sys.exc_info()[ 1 ], )
  306.             ok = False
  307.         if ( ok ):
  308.             # return the xml string without \n\r (newline)
  309.             return xmlSource.replace("\n","").replace("\r","")
  310.         else:
  311.             return ""
  312.  
  313.     def _save_launchers (self):
  314.         # make settings directory if doesn't exists
  315.         if (not os.path.isdir(os.path.dirname(BASE_CURRENT_SOURCE_PATH))):
  316.             os.makedirs(os.path.dirname(BASE_CURRENT_SOURCE_PATH));
  317.            
  318.         usock = open( BASE_CURRENT_SOURCE_PATH, 'w' )
  319.         usock.write("<?xml version=\"1.0\" encoding=\"utf-8\" standalone=\"yes\"?>\n")
  320.         usock.write("<launchers>\n")
  321.         for launcherIndex in self.launchers:
  322.             launcher = self.launchers[launcherIndex]
  323.             usock.write("\t<launcher>\n")
  324.             usock.write("\t\t<name>"+launcher["name"]+"</name>\n")
  325.             usock.write("\t\t<application>"+launcher["application"]+"</application>\n")
  326.             usock.write("\t\t<args>"+launcher["args"]+"</args>\n")
  327.             usock.write("\t\t<rompath>"+launcher["rompath"]+"</rompath>\n")
  328.             usock.write("\t\t<romext>"+launcher["romext"]+"</romext>\n")
  329.             usock.write("\t\t<thumb>"+launcher["thumb"]+"</thumb>\n")
  330.             usock.write("\t\t<fanart>"+launcher["fanart"]+"</fanart>\n")
  331.             usock.write("\t\t<genre>"+launcher["genre"]+"</genre>\n")
  332.             usock.write("\t\t<release>"+launcher["release"]+"</release>\n")
  333.             usock.write("\t\t<publisher>"+launcher["company"]+"</publisher>\n")
  334.             usock.write("\t\t<launcherplot>"+launcher["plot"]+"</launcherplot>\n")
  335.             usock.write("\t\t<wait>"+launcher["wait"]+"</wait>\n")
  336.             usock.write("\t\t<roms>\n")
  337.             for romIndex in launcher["roms"]:
  338.                 romdata = launcher["roms"][romIndex]
  339.                 usock.write("\t\t\t<rom>\n")
  340.                 usock.write("\t\t\t\t<name>"+romdata["name"]+"</name>\n")
  341.                 usock.write("\t\t\t\t<filename>"+romdata["filename"]+"</filename>\n")
  342.                 usock.write("\t\t\t\t<thumb>"+romdata["thumb"]+"</thumb>\n")
  343.                 usock.write("\t\t\t\t<fanart>"+romdata["fanart"]+"</fanart>\n")
  344.                 usock.write("\t\t\t\t<genre>"+romdata["genre"]+"</genre>\n")
  345.                 usock.write("\t\t\t\t<release>"+romdata["release"]+"</release>\n")
  346.                 usock.write("\t\t\t\t<publisher>"+romdata["company"]+"</publisher>\n")
  347.                 usock.write("\t\t\t\t<gameplot>"+romdata["plot"]+"</gameplot>\n")
  348.                 usock.write("\t\t\t</rom>\n")
  349.             usock.write("\t\t</roms>\n")
  350.             usock.write("\t</launcher>\n")            
  351.         usock.write("</launchers>")
  352.         usock.close()
  353.        
  354.     ''' read the list of launchers and roms from launchers.xml file '''
  355.     def _load_launchers( self , xmlSource):
  356.         launchers = re.findall( "<launcher>(.*?)</launcher>", xmlSource )
  357.         print "Launcher: found %d launchers" % ( len(launchers) )
  358.         for launcher in launchers:
  359.             name = re.findall( "<name>(.*?)</name>", launcher )
  360.             application = re.findall( "<application>(.*?)</application>", launcher )
  361.             args = re.findall( "<args>(.*?)</args>", launcher )
  362.             rompath = re.findall( "<rompath>(.*?)</rompath>", launcher )
  363.             romext = re.findall( "<romext>(.*?)</romext>", launcher )
  364.             thumb = re.findall( "<thumb>(.*?)</thumb>", launcher )
  365.             fanart = re.findall( "<fanart>(.*?)</fanart>", launcher )
  366.             genre = re.findall( "<genre>(.*?)</genre>", launcher )
  367.             release = re.findall( "<release>(.*?)</release>", launcher )
  368.             company = re.findall( "<company>(.*?)</company>", launcher )
  369.             plot = re.findall( "<launcherplot>(.*?)</launcherplot>", launcher )
  370.             wait = re.findall( "<wait>(.*?)</wait>", launcher )
  371.             romsxml = re.findall( "<rom>(.*?)</rom>", launcher )
  372.  
  373.             if len(name) > 0 : name = name[0]
  374.             else: name = "unknown"
  375.  
  376.             if len(application) > 0 : application = application[0]
  377.             else: application = ""
  378.  
  379.             if len(args) > 0 : args = args[0]
  380.             else: args = ""
  381.  
  382.             if len(rompath) > 0 : rompath = rompath[0]
  383.             else: rompath = ""
  384.  
  385.             if len(romext) > 0: romext = romext[0]
  386.             else: romext = ""
  387.  
  388.             if len(thumb) > 0: thumb = thumb[0]
  389.             else: thumb = ""
  390.  
  391.             if len(fanart) > 0: fanart = fanart[0]
  392.             else: fanart = ""
  393.  
  394.             if len(genre) > 0: genre = genre[0]
  395.             else: genre = ""
  396.  
  397.             if len(release) > 0: release = release[0]
  398.             else: release = ""
  399.  
  400.             if len(company) > 0: company = company[0]
  401.             else: company = ""
  402.  
  403.             if len(plot) > 0: plot = plot[0]
  404.             else: plot = ""
  405.  
  406.             if len(wait) > 0: wait = wait[0]
  407.             else: wait = ""
  408.            
  409.             roms = {}
  410.             for rom in romsxml:
  411.                 romname = re.findall( "<name>(.*?)</name>", rom )
  412.                 romfilename = re.findall( "<filename>(.*?)</filename>", rom )
  413.                 romthumb = re.findall( "<thumb>(.*?)</thumb>", rom )
  414.                 romfanart = re.findall( "<fanart>(.*?)</fanart>", rom )
  415.                 romgenre = re.findall( "<genre>(.*?)</genre>", rom )
  416.                 romrelease = re.findall( "<release>(.*?)</release>", rom )
  417.                 romcompany = re.findall( "<publisher>(.*?)</publisher>", rom )
  418.                 romplot = re.findall( "<gameplot>(.*?)</gameplot>", rom )
  419.  
  420.                 if len(romname) > 0 : romname = romname[0]
  421.                 else: romname = "unknown"
  422.  
  423.                 if len(romfilename) > 0 : romfilename = romfilename[0]
  424.                 else: romfilename = ""
  425.  
  426.                 if len(romthumb) > 0 : romthumb = romthumb[0]
  427.                 else: romthumb = ""
  428.  
  429.                 if len(romfanart) > 0 : romfanart = romfanart[0]
  430.                 else: romfanart = ""
  431.  
  432.                 if len(romgenre) > 0 : romgenre = romgenre[0]
  433.                 else: romgenre = ""
  434.  
  435.                 if len(romrelease) > 0 : romrelease = romrelease[0]
  436.                 else: romrelease = ""
  437.  
  438.                 if len(romcompany) > 0 : romcompany = romcompany[0]
  439.                 else: romcompany = ""
  440.  
  441.                 if len(romplot) > 0 : romplot = romplot[0]
  442.                 else: romplot = ""
  443.  
  444.                 # prepare rom object data
  445.                 romdata = {}
  446.                 romdata["name"] = romname
  447.                 romdata["filename"] = romfilename
  448.                 romdata["thumb"] = romthumb
  449.                 romdata["fanart"] = romfanart
  450.                 romdata["genre"] = romgenre
  451.                 romdata["release"] = romrelease
  452.                 romdata["company"] = romcompany
  453.                 romdata["plot"] = romplot
  454.  
  455.                 # add rom to the roms list (using name as index)
  456.                 roms[romname] = romdata
  457.  
  458.             # prepare launcher object data
  459.             launcherdata = {}
  460.             launcherdata["name"] = name
  461.             launcherdata["application"] = application
  462.             launcherdata["args"] = args
  463.             launcherdata["rompath"] = rompath
  464.             launcherdata["romext"] = romext
  465.             launcherdata["thumb"] = thumb
  466.             launcherdata["fanart"] = fanart
  467.             launcherdata["genre"] = genre
  468.             launcherdata["release"] = release
  469.             launcherdata["company"] = company
  470.             launcherdata["plot"] = plot
  471.             launcherdata["wait"] = wait
  472.             launcherdata["roms"] = roms
  473.  
  474.             # add launcher to the launchers list (using name as index)
  475.             self.launchers[name] = launcherdata
  476.    
  477.     def _get_launchers( self ):
  478.         if (len(self.launchers) > 0):
  479.             for key in sorted(self.launchers.iterkeys()):
  480.                 self._add_launcher(self.launchers[key]["name"], self.launchers[key]["application"], self.launchers[key]["rompath"], self.launchers[key]["romext"], self.launchers[key]["thumb"], self.launchers[key]["fanart"], self.launchers[key]["genre"], self.launchers[key]["release"], self.launchers[key]["company"], self.launchers[key]["plot"], self.launchers[key]["wait"], self.launchers[key]["roms"], len(self.launchers))
  481.             xbmcplugin.endOfDirectory( handle=int( self._handle ), succeeded=True, cacheToDisc=False )
  482.             return True  
  483.         else:
  484.             return False
  485.  
  486.     def _get_roms( self, launcherName ):
  487.         if (self.launchers.has_key(launcherName)):
  488.             selectedLauncher = self.launchers[launcherName]
  489.             roms = selectedLauncher["roms"]
  490.             print "Launcher: %s : found %d roms " % (launcherName, len(roms))
  491.             if (len(roms) > 0) :
  492.                 for key in sorted(roms.iterkeys()):
  493.                     self._add_rom(launcherName, roms[key]["name"], roms[key]["filename"], roms[key]["thumb"], roms[key]["fanart"], roms[key]["genre"], roms[key]["release"], roms[key]["company"], roms[key]["plot"], len(roms))
  494.             else:
  495.                 dialog = xbmcgui.Dialog()
  496.                 ret = dialog.yesno(__language__( 30000 ), __language__( 30013 ))
  497.                 if (ret):
  498.                     self._import_roms(launcherName, addRoms = True)
  499.             xbmcplugin.endOfDirectory( handle=int( self._handle ), succeeded=True, cacheToDisc=False )
  500.             return True
  501.         else:
  502.             return False
  503.  
  504.     def _search_thumb(self, launcherName, romname):
  505.         search_engine = self._get_search_engine()
  506.         pDialog.update( 0, __language__( 30031 ) % (launcherName, romname, self.settings[ "search_engine" ]) )
  507.         if (romname) :
  508.             search_string = "%s %s" % (romname, launcherName)
  509.         else:
  510.             search_string = "%s" % (launcherName)
  511.         results = search_engine.search(search_string)
  512.         stopSearch = False
  513.         while (len(results)==0 and not stopSearch):
  514.             keyboard = xbmc.Keyboard(search_string, __language__( 30034 ))
  515.             keyboard.doModal()
  516.             if (keyboard.isConfirmed()):
  517.                 search_string = keyboard.getText()
  518.                 results = search_engine.search(search_string)
  519.             else:
  520.                 stopSearch = True
  521.                
  522.         if (len(results)>0):
  523.             dialog = xbmcgui.Dialog()
  524.             thumbs = []
  525.             total = len(results)
  526.             for result in results:
  527.                 thumbnail = self._get_thumbnail(result["thumb"])
  528.                 listitem = xbmcgui.ListItem( "%s (%s)" % (result["title"], result["url"]), iconImage="DefaultProgram.png", thumbnailImage=thumbnail )
  529.                 xbmcplugin.addDirectoryItem( handle=int( self._handle ), url="%s?%s/%s/%s%s%s"  % (self._path, launcherName, romname, SET_THUMB_COMMAND, COMMAND_ARGS_SEPARATOR, result["url"]), listitem=listitem, isFolder=False, totalItems=total)
  530.  
  531.         xbmcplugin.endOfDirectory( handle=int( self._handle ), succeeded=True, cacheToDisc=False )
  532.  
  533.     def _set_thumb(self, launcherName, romname, url):
  534.         self.url = url
  535.         # download thumb
  536.         urllib.urlretrieve( url, None, self._report_hook )
  537.  
  538.         # copy it into thumbs path
  539.         path = self.settings[ "thumbs_path" ]
  540.         filepath = os.path.join(path, os.path.basename(url))
  541.        
  542.         pDialog.update( 100, __language__( 30032 ))
  543.         xbmc.sleep( 50 )
  544.         xbmc.executehttpapi( "FileCopy(%s,%s)" % (url, filepath, ) )
  545.  
  546.         launcher = self.launchers[launcherName]
  547.  
  548.         if (romname):
  549.             rom = launcher["roms"][romname]
  550.             rom["thumb"] = filepath
  551.         else:
  552.             launcher["thumb"] = filepath
  553.                
  554.         self._save_launchers()
  555.  
  556.         # returning back to the previous window
  557.         if (romname):
  558.             xbmc.executebuiltin("ReplaceWindow(Programs,%s?%s)" % (self._path, launcherName))
  559.         else:
  560.             xbmc.executebuiltin("ReplaceWindow(Programs,%s)" % (self._path))
  561.  
  562.     def _report_hook( self, count, blocksize, totalsize ):
  563.          percent = int( float( count * blocksize * 100) / totalsize )
  564.          msg1 = __language__( 30033 )  % ( os.path.split( self.url )[ 1 ], )
  565.          pDialog.update( percent, msg1 )
  566.          if ( pDialog.iscanceled() ): raise
  567.        
  568.     def _scan_launcher(self, launchername):
  569.         self._search_thumb(launchername, "")
  570.         self._save_launchers()
  571.  
  572.     def _import_roms(self, launcherName, addRoms = False):
  573.         dialog = xbmcgui.Dialog()
  574.         romsCount = 0
  575.         filesCount = 0
  576.         skipCount = 0
  577.         selectedLauncher = self.launchers[launcherName]
  578.         pDialog = xbmcgui.DialogProgress()
  579.         path = selectedLauncher["rompath"]
  580.         exts = selectedLauncher["romext"]
  581.         roms = selectedLauncher["roms"]
  582.        
  583.         #remove dead entries
  584.         if (len(roms) > 0):
  585.             i = 0
  586.             removedRoms = 0
  587.             ret = pDialog.create(__language__( 30000 ), __language__( 30501 ) % (path));
  588.            
  589.             for key in sorted(roms.iterkeys()):
  590.                 pDialog.update(i * 100 / len(roms))
  591.                 i += 1
  592.                 if (not os.path.isfile(roms[key]["filename"])):
  593.                     del roms[key]
  594.                     removedRoms += 1
  595.             pDialog.close()
  596.             if not (removedRoms == 0):
  597.                 xbmc.executebuiltin("XBMC.Notification(%s,%s, 6000)" % (__language__( 30000 ), __language__( 30502 ) % (removedRoms)))
  598.        
  599.         ret = pDialog.create(__language__( 30000 ), __language__( 30014 ) % (path));
  600.        
  601.         files = os.listdir(path)
  602.         for f in files:
  603.             pDialog.update(filesCount * 100 / len(files))
  604.             fullname = os.path.join(path, f)
  605.             for ext in exts.split("|"):
  606.                 romadded = False
  607.                 if f.upper().endswith("." + ext.upper()):
  608.                     romname =  f[:-len(ext)-1].capitalize()
  609.                     if (not roms.has_key(romname)):
  610.                         # prepare rom object data
  611.                         romdata = {}
  612.                         romname =  f[:-len(ext)-1].capitalize()
  613.                         romdata["name"] = romname
  614.                         romdata["filename"] = fullname
  615.                         #try the filename of the rom with different extension to get a valid thumbnail
  616.                         thumb = self._get_local_thumb(fullname)
  617.                         if thumb:
  618.                             romdata["thumb"] = thumb
  619.                         else:
  620.                             romdata["thumb"] = ""
  621.                         romdata["fanart"] = ""
  622.                         romdata["genre"] = ""
  623.                         romdata["release"] = ""
  624.                         romdata["company"] = ""
  625.                         romdata["plot"] = ""
  626.                         # add rom to the roms list (using name as index)
  627.                         roms[romname] = romdata
  628.                         romsCount = romsCount + 1
  629.                        
  630.                         if (addRoms):
  631.                             self._add_rom(launcherName, romdata["name"], romdata["filename"], romdata["thumb"], romdata["fanart"], romdata["genre"], romdata["release"], romdata["company"], romdata["plot"], len(files))
  632.                             romadded = True
  633.                 if not romadded:
  634.                     skipCount = skipCount + 1
  635.                
  636.             filesCount = filesCount + 1    
  637.         pDialog.close()
  638.         self._save_launchers()
  639.         if (skipCount == 0):
  640.             xbmc.executebuiltin("XBMC.Notification(%s,%s, 6000)" % (__language__( 30000 ), __language__( 30015 ) % (romsCount) + " " + __language__( 30050 )))
  641.             #dialog.ok(__language__( 30000 ), (__language__( 30015 )+ "\n" + __language__( 30050 )) % (romsCount))
  642.         else:
  643.             xbmc.executebuiltin("XBMC.Notification(%s,%s, 12000)" % (__language__( 30000 ), __language__( 30016 ) % (romsCount, skipCount) + " " + __language__( 30050 )))
  644.             #dialog.ok(__language__( 30000 ), (__language__( 30016 )+ "\n" + __language__( 30050 )) % (romsCount, skipCount))
  645.  
  646.     def _get_thumbnail( self, thumbnail_url ):
  647.         # make the proper cache filename and path so duplicate caching is unnecessary
  648.         if ( not thumbnail_url.startswith( "http://" ) ): return thumbnail_url
  649.         try:
  650.             filename = xbmc.getCacheThumbName( thumbnail_url )
  651.             filepath = xbmc.translatePath( os.path.join( self.BASE_CACHE_PATH, filename[ 0 ], filename ) )
  652.             # if the cached thumbnail does not exist fetch the thumbnail
  653.             if ( not os.path.isfile( filepath ) ):
  654.                 # fetch thumbnail and save to filepath
  655.                 info = urllib.urlretrieve( thumbnail_url, filepath )
  656.                 # cleanup any remaining urllib cache
  657.                 urllib.urlcleanup()
  658.             return filepath
  659.         except:
  660.             # return empty string if retrieval failed
  661.             print "ERROR: %s::%s (%d) - %s" % ( self.__class__.__name__, sys.exc_info()[ 2 ].tb_frame.f_code.co_name, sys.exc_info()[ 2 ].tb_lineno, sys.exc_info()[ 1 ], )
  662.             return ""
  663.        
  664.     def _get_thumb(self, displayName, fileName):
  665.         exts = ['png', 'jpg', 'gif', 'bmp']
  666.         for ext in exts:
  667.             thumbfilename = os.path.join(self.settings[ "thumbs_path" ], "%s.%s" % (displayName, ext))
  668.             if (os.path.isfile(thumbfilename)):
  669.                 return thumbfilename
  670.             thumbfilename = os.path.join(self.settings[ "thumbs_path" ], "%s.%s" % (os.path.basename(fileName).split(".")[0], ext))
  671.             if (os.path.isfile(thumbfilename)):
  672.                 return thumbfilename
  673.        
  674.     def _add_launcher(self, name, cmd, path, ext, thumb, fanart, genre, release, company, plot, wait, roms, total) :
  675.         commands = []
  676.         commands.append((__language__( 30512 ), "XBMC.RunPlugin(%s?%s)"    % (self._path, SEARCH_ALL_COMMAND) , ))#search all
  677.         commands.append((__language__( 30511 ), "XBMC.RunPlugin(%s?%s/%s)" % (self._path, name, SEARCH_COMMAND) , ))#search
  678.  
  679.         if (path == ""):
  680.             folder = False
  681.             icon = "DefaultProgram.png"
  682.         else:
  683.             folder = True
  684.             icon = "DefaultFolder.png"
  685.             commands.append((__language__( 30105 ), "XBMC.RunPlugin(%s?%s/%s)" % (self._path, name, IMPORT_COMMAND) , ))
  686.             commands.append((__language__( 30106 ), "XBMC.RunPlugin(%s?%s/%s)" % (self._path, name, ADD_COMMAND) , ))
  687.         #not working anymore
  688.         #commands.append((__language__( 30102 ), "ReplaceWindow(Programs,%s?%s/%s)" % (self._path, name, SCAN_COMMAND) , ))
  689.         commands.append((__language__( 30107 ), "XBMC.RunPlugin(%s?%s/%s)" % (self._path, name, RENAME_COMMAND) , ))
  690.         if (sys.platform == "win32"):
  691.             commands.append((__language__( 30103 ), "XBMC.RunPlugin(%s?%s/%s)" % (self._path, name, WAIT_TOGGLE_COMMAND) , ))
  692.  
  693.         commands.append((__language__( 30101 ), "XBMC.RunPlugin(%s?%s)" % (self._path, ADD_COMMAND) , ))
  694.         commands.append((__language__( 30104 ), "XBMC.RunPlugin(%s?%s/%s)" % (self._path, name, REMOVE_COMMAND) , ))
  695.  
  696.         if (thumb):
  697.             thumbnail = thumb
  698.         else:
  699.             thumbnail = self._get_thumb(name, cmd)
  700.            
  701.         if (thumbnail):
  702.             listitem = xbmcgui.ListItem( name, iconImage=icon, thumbnailImage=thumbnail)
  703.         else:
  704.             listitem = xbmcgui.ListItem( name, iconImage=icon )
  705.  
  706.         listitem.setProperty("fanart_image", fanart)
  707.         listitem.setProperty( "releasedate", release )
  708.         listitem.setInfo( "video", { "Title": name, "Plot" : plot , "Studio" : company , "Genre" : genre } )
  709.         listitem.addContextMenuItems( commands )
  710.         xbmcplugin.addDirectoryItem( handle=int( self._handle ), url="%s?%s"  % (self._path, name), listitem=listitem, isFolder=folder, totalItems=total)
  711.  
  712.     def _add_rom( self, launcher, name, cmd , thumb, romfanart, romgenre, romrelease, romcompany, romplot, total):
  713.         if (thumb):
  714.             thumbnail = thumb
  715.         else:
  716.             thumbnail = self._get_thumb(name, cmd)
  717.         icon = "DefaultProgram.png"
  718.         if (thumbnail):
  719.             listitem = xbmcgui.ListItem( name, iconImage=icon, thumbnailImage=thumbnail)
  720.         else:
  721.             listitem = xbmcgui.ListItem( name, iconImage=icon )
  722.         listitem.setProperty("fanart_image", romfanart)
  723.         listitem.setProperty( "releasedate", romrelease )
  724.         listitem.setInfo( "video", { "Title": name, "Plot" : romplot , "Studio" : romcompany , "Genre" : romgenre } )
  725.  
  726.         commands = []
  727.         commands.append(( __language__( 30511 ), "XBMC.RunPlugin(%s?%s/%s)" % (self._path, launcher, SEARCH_COMMAND) , ))#search
  728.         #not working anymore
  729.         #commands.append(( __language__( 30102 ), "ReplaceWindow(Programs,%s?%s/%s/%s)" % (self._path, launcher, name, SCAN_COMMAND) , ))
  730.         commands.append(( __language__( 30107 ), "XBMC.RunPlugin(%s?%s/%s/%s)" % (self._path, launcher, name, RENAME_COMMAND) , ))
  731.         commands.append(( __language__( 30104 ), "XBMC.RunPlugin(%s?%s/%s/%s)" % (self._path, launcher, name, REMOVE_COMMAND) , ))
  732.         listitem.addContextMenuItems( commands )
  733.         xbmcplugin.addDirectoryItem( handle=int( self._handle ), url="%s?%s/%s"  % (self._path, launcher, name), listitem=listitem, isFolder=False, totalItems=total)
  734.  
  735.     def _add_new_rom ( self , launcherName) :
  736.         dialog = xbmcgui.Dialog()
  737.         launcher = self.launchers[launcherName]
  738.         ext = launcher["romext"]
  739.         roms = launcher["roms"]
  740.         rompath = launcher["rompath"]
  741.        
  742.         romfile = dialog.browse(1, __language__( 30017 ),"files", "."+ext, False, False, rompath)
  743.         if (romfile):
  744.             title=os.path.basename(romfile).split(".")[0].capitalize()
  745.             keyboard = xbmc.Keyboard(title, __language__( 30018 ))
  746.             keyboard.doModal()
  747.             if (keyboard.isConfirmed()):
  748.                 title = keyboard.getText()
  749.  
  750.                 # prepare rom object data
  751.                 romdata = {}
  752.                 romdata["name"] = title
  753.                 romdata["filename"] = romfile
  754.                 romdata["thumb"] = ""
  755.  
  756.                 # add rom to the roms list (using name as index)
  757.                 roms[title] = romdata
  758.  
  759.                 xbmc.executebuiltin("XBMC.Notification(%s,%s, 6000)" % (__language__( 30000 ), __language__( 30019 ) + " " + __language__( 30050 )))
  760.                 #xbmcgui.Dialog().ok(__language__( 30000 ), __language__( 30019 )+ "\n" + __language__( 30050 ))
  761.         self._save_launchers()
  762.  
  763.     def _add_new_launcher ( self ) :
  764.         dialog = xbmcgui.Dialog()
  765.         type = dialog.select(__language__( 30020 ), [__language__( 30021 ), __language__( 30022 )])
  766.         if (os.environ.get( "OS", "xbox" ) == "xbox"):
  767.             filter = ".xbe|.cut"
  768.         else:
  769.             if (sys.platform == "win32"):
  770.                 filter = ".bat|.exe|.cmd"
  771.             else:
  772.                 filter = ""
  773.            
  774.         if (type == 0):
  775.             app = xbmcgui.Dialog().browse(1,__language__( 30023 ),"files",filter)
  776.             if (app):
  777.                 argkeyboard = xbmc.Keyboard("", __language__( 30024 ))
  778.                 argkeyboard.doModal()
  779.                 if (argkeyboard.isConfirmed()):
  780.                     args = argkeyboard.getText();
  781.                     title = os.path.basename(app).split(".")[0].capitalize()
  782.                     keyboard = xbmc.Keyboard(title, __language__( 30025 ))
  783.                     keyboard.doModal()
  784.                     if (keyboard.isConfirmed()):
  785.                         title = keyboard.getText()                    
  786.                         # prepare launcher object data
  787.                         launcherdata = {}
  788.                         launcherdata["name"] = title
  789.                         launcherdata["application"] = app
  790.                         launcherdata["args"] = args
  791.                         launcherdata["rompath"] = ""
  792.                         launcherdata["romext"] = ""
  793.                         #try the filename with different extension to get a valid thumbnail
  794.                         thumb = self._get_local_thumb(app)
  795.                         if thumb:
  796.                             launcherdata["thumb"] = thumb
  797.                         else:
  798.                             launcherdata["thumb"] = ""
  799.                         launcherdata["fanart"] = ""
  800.                         launcherdata["genre"] = ""
  801.                         launcherdata["release"] = ""
  802.                         launcherdata["company"] = ""
  803.                         launcherdata["plot"] = ""
  804.                         launcherdata["wait"] = "true"
  805.                         launcherdata["roms"] = {}
  806.                    
  807.                         # add launcher to the launchers list (using name as index)
  808.                         self.launchers[title] = launcherdata
  809.                         self._save_launchers()
  810.  
  811.                         xbmc.executebuiltin("ReplaceWindow(Programs,%s)" % (self._path))
  812.                         return True
  813.         elif (type == 1):
  814.             app = xbmcgui.Dialog().browse(1,__language__( 30023 ),"files",filter)
  815.             if (app):
  816.                 argkeyboard = xbmc.Keyboard("", __language__( 30024 ))
  817.                 argkeyboard.doModal()
  818.                 if (argkeyboard.isConfirmed()):
  819.                     args = argkeyboard.getText();
  820.                     path = xbmcgui.Dialog().browse(0,__language__( 30027 ),"files", "", False, False, os.path.dirname(app))
  821.                     if (path):
  822.                         extkey = xbmc.Keyboard("", __language__( 30028 ))
  823.                         extkey.doModal()
  824.                         if (extkey.isConfirmed()):
  825.                             ext = extkey.getText()
  826.                             title = os.path.basename(app).split(".")[0].capitalize()
  827.                             keyboard = xbmc.Keyboard(title, __language__( 30025 ))
  828.                             keyboard.doModal()
  829.                             if (keyboard.isConfirmed()):
  830.                                 title = keyboard.getText()
  831.                                 # prepare launcher object data
  832.                                 launcherdata = {}
  833.                                 launcherdata["name"] = title
  834.                                 launcherdata["application"] = app
  835.                                 launcherdata["args"] = args
  836.                                 launcherdata["rompath"] = path
  837.                                 launcherdata["romext"] = ext
  838.                                 #try the filename and path with different extension to get a valid thumbnail
  839.                                 thumb = self._get_local_thumb(path[:-1] + ".dir")
  840.                                 print "thumbs_path: " + path
  841.                                 print "thumbs_app: " + app
  842.                                 if thumb:
  843.                                     launcherdata["thumb"] = thumb
  844.                                 else:
  845.                                     thumb = self._get_local_thumb(app)
  846.                                     if thumb:
  847.                                         launcherdata["thumb"] = thumb
  848.                                     else:
  849.                                         launcherdata["thumb"] = ""
  850.                                 launcherdata["fanart"] = ""
  851.                                 launcherdata["genre"] = ""
  852.                                 launcherdata["release"] = ""
  853.                                 launcherdata["company"] = ""
  854.                                 launcherdata["plot"] = ""
  855.                                 launcherdata["wait"] = "true"
  856.                                 launcherdata["roms"] = {}
  857.                        
  858.                                 # add launcher to the launchers list (using name as index)
  859.                                 self.launchers[title] = launcherdata
  860.                                 self._save_launchers()
  861.                                 xbmc.executebuiltin("ReplaceWindow(Programs,%s)" % (self._path))
  862.                                 return True
  863.         return False
  864.  
  865.     def _toggle_wait( self, launcher ):
  866.         # toggle wait state
  867.         if (self.launchers[launcher]["wait"] == "true"):
  868.             self.launchers[launcher]["wait"] = "false"
  869.             xbmc.executebuiltin("XBMC.Notification(%s,%s, 6000)" % (__language__( 30000 ), __language__( 30029 )  ))
  870.             #xbmcgui.Dialog().ok(__language__( 30000 ), __language__( 30029 ))
  871.         else:
  872.             self.launchers[launcher]["wait"] = "true"
  873.             xbmc.executebuiltin("XBMC.Notification(%s,%s, 6000)" % (__language__( 30000 ), __language__( 30030 )  ))
  874.             #xbmcgui.Dialog().ok(__language__( 30000 ), __language__( 30030 ))
  875.         self._save_launchers()
  876.  
  877.        
  878.  
  879.     def _get_search_engine( self ):
  880.         exec "import resources.search_engines.%s.search_engine as search_engine" % ( self.settings[ "search_engine" ], )
  881.         return search_engine.SearchEngine()
  882.                                
  883.     def _get_settings( self ):
  884.         self.settings = {}
  885.         self.settings[ "thumbs_path" ]     =  THUMBS_PATH
  886.        
  887.         try:
  888.             self.settings[ "search_engine" ]   =  __settings__.getSetting( int(sys.argv[1]),"search_engine" )
  889.         except:
  890.             self.settings[ "search_engine" ]   =  __settings__.getSetting( "search_engine" )
  891.        
  892.         if (not os.path.isdir(os.path.dirname(self.settings[ "thumbs_path" ]))):
  893.             os.makedirs(os.path.dirname(self.settings[ "thumbs_path" ]));
  894.  
  895.     def _get_local_thumb( self, filename ):
  896.         exts = ['png', 'jpg', 'gif', 'bmp']
  897.         for ext in exts:
  898.             #try with chnaged extension
  899.             thumb = filename[:-len(os.path.splitext(filename)[-1])] + "." + ext
  900.             if (os.path.isfile(thumb)):
  901.                 return thumb
  902.             #also try with original extension
  903.             thumb = filename + "." + ext
  904.             if (os.path.isfile(thumb)):
  905.                 return thumb
  906.  
  907.     def _find_all_roms( self ):
  908.         #get user input to search for
  909.         keyboard = xbmc.Keyboard("", __language__( 30024 ))
  910.         keyboard.doModal()
  911.         if (keyboard.isConfirmed()):
  912.             search = keyboard.getText()
  913.             xbmc.executebuiltin("ReplaceWindow(Programs,%s?%s/%s)" % (self._path, search, SEARCH_ALL_COMMAND))
  914.  
  915.     def _find_add_all_roms( self, search ):
  916.         #sorted by name
  917.         if (len(self.launchers) > 0):
  918.             rl = {}
  919.             for key in sorted(self.launchers.iterkeys()):
  920.                 launcherName = self.launchers[key]["name"]
  921.                 selectedLauncher = self.launchers[launcherName]
  922.                 roms = selectedLauncher["roms"]
  923.                 text = search.lower();
  924.                 print "Launcher: %s : searching for %s " % (launcherName, text)
  925.                 if (len(roms) > 0) :
  926.                     #go through rom list and search for user input
  927.                     for keyr in sorted(roms.iterkeys()):
  928.                         romname = roms[keyr]["name"].lower()
  929.                         if (not romname.find(text) == -1):
  930.                             rl[keyr] = roms[keyr]
  931.                             rl[keyr]["launcherName"] = launcherName
  932.                             print roms[keyr]["filename"]
  933.         #print the list sorted
  934.         print "Launcher: (ALL) : search found %d roms" % (len(rl))
  935.         for key in sorted(rl.iterkeys()):
  936.             self._add_rom(rl[key]["launcherName"], rl[key]["name"], rl[key]["filename"], rl[key]["thumb"], rl[key]["fanart"], rl[key]["genre"], rl[key]["release"], rl[key]["company"], rl[key]["plot"], len(rl))
  937.             print "Launcher: %s : add %s" % (rl[key]["launcherName"], rl[key]["name"])
  938.        
  939.         xbmcplugin.endOfDirectory( handle=int( self._handle ), succeeded=True, cacheToDisc=False )
  940.  
  941. #        #sorted by launcher
  942. #        if (len(self.launchers) > 0):
  943. #            i = 0
  944. #            for key in sorted(self.launchers.iterkeys()):
  945. #                i += 1
  946. #                self._find_add_roms(self.launchers[key]["name"], search, (len(self.launchers) == i))
  947.  
  948.     def _find_roms( self, launcherName ):
  949.         #get user input to search for
  950.         keyboard = xbmc.Keyboard("", __language__( 30024 ))
  951.         keyboard.doModal()
  952.         if (keyboard.isConfirmed()):
  953.             search = keyboard.getText()
  954.             xbmc.executebuiltin("ReplaceWindow(Programs,%s?%s/%s/%s)" % (self._path, launcherName, search, SEARCH_COMMAND))
  955.  
  956.     def _find_add_roms ( self, launcherName, search, lastone=True ):
  957.         if (self.launchers.has_key(launcherName)):
  958.             selectedLauncher = self.launchers[launcherName]
  959.             roms = selectedLauncher["roms"]
  960.             text = search.lower();
  961.             print "Launcher: %s : searching for %s " % (launcherName, text)
  962.             if (len(roms) > 0) :
  963.                 #go through rom list and search for user input
  964.                 for key in sorted(roms.iterkeys()):
  965.                     romname = roms[key]["name"].lower()
  966.                     if (not romname.find(text) == -1):
  967.                         self._add_rom(launcherName, roms[key]["name"], roms[key]["filename"], roms[key]["thumb"], roms[key]["fanart"], roms[key]["genre"], roms[key]["release"], roms[key]["company"], roms[key]["plot"], len(roms))
  968.  
  969.             if (lastone):
  970.                 xbmcplugin.endOfDirectory( handle=int( self._handle ), succeeded=True, cacheToDisc=False )
  971.             return True
  972.         else:
  973.             return False