X-Git-Url: http://git.euphorik.ch/?a=blobdiff_plain;f=xpi%2Fcomponents%2FhandleProtocol.js;h=8150a6c424750c82ca328e8bbb96f2a85ff13d11;hb=c3b0deb3d8c9f439739c79806e915c29bc1d4b84;hp=30db1f190f08b1a6ed8a326b863ebba45c102a8e;hpb=92c4a7fa969d325828d932c09d6686f4e6f52171;p=pompage.git diff --git a/xpi/components/handleProtocol.js b/xpi/components/handleProtocol.js index 30db1f1..8150a6c 100644 --- a/xpi/components/handleProtocol.js +++ b/xpi/components/handleProtocol.js @@ -1,141 +1,234 @@ - - -// components defined in this file +// voir : http://www.nexgenmedia.net/docs/protocol/ + +// le composant pour cette extension const DIVXPROT_HANDLER_CONTRACTID = "@mozilla.org/network/protocol;1?name=divx"; -const DIVXPROT_HANDLER_CID = Components.ID("{65aa548e-1dda-11dc-8314-0800200c9a66}"); +const DIVXPROT_HANDLER_CID = Components.ID("{65aa548e-1dda-11dc-8314-0800200c9a66}"); -// components used in this file -const NS_IOSERVICE_CID = "{9ac9e770-18bc-11d3-9337-00104ba0fd40}"; -const NS_PREFSERVICE_CONTRACTID = "@mozilla.org/preferences-service;1"; -const URI_CONTRACTID = "@mozilla.org/network/simple-uri;1"; -const NS_WINDOWWATCHER_CONTRACTID = "@mozilla.org/embedcomp/window-watcher;1"; -const STREAMIOCHANNEL_CONTRACTID = "@mozilla.org/network/stream-io-channel;1"; -const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1"; +// les composants utilisés +const URI_CONTRACTID = "@mozilla.org/network/simple-uri;1"; +const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1"; -// interfaces used in this file +// les interfaces utilisées const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler; const nsIURI = Components.interfaces.nsIURI; const nsISupports = Components.interfaces.nsISupports; const nsIIOService = Components.interfaces.nsIIOService; -const nsIPrefService = Components.interfaces.nsIPrefService; -const nsIWindowWatcher = Components.interfaces.nsIWindowWatcher; -const nsIChannel = Components.interfaces.nsIChannel; + +/******* COPIER-COLLER de pref.js dans /chrome/content *******/ +/******* TODO : trouver un moyen d'inclure pref.js *******/ +function divxlistGetPreferencesService() +{ + return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch(""); +} +function divxlistGetRoot() +{ + return divxlistGetPreferencesService().getCharPref("divxlist.root"); +} +function divxlistSetRoot(root) +{ + return divxlistGetPreferencesService().setCharPref("divxlist.root", root); +} +function divxlistGetCommandes() +{ + var commandes = divxlistGetPreferencesService().getCharPref("divxlist.commandes"); + var regex = /'[^']*'/g + var res = commandes.match(regex); + var commandesTab = new Array() + if (res != null) + for(var i = 0; i < res.length; i += 2) + { + var motifCommande = new Array(2); + motifCommande[0] = res[i].slice(1, res[i].length-1) + motifCommande[1] = res[i+1].slice(1, res[i+1].length-1) + commandesTab.push(motifCommande); + } + return commandesTab; +} +function divxlistSetCommandes(commandes) +{ + var commandesStr = "{" + for (var i = 0; i < commandes.length; i++) + { + if (i != 0) commandesStr += ", " + commandesStr += "'" + commandes[i][0] + "' => " + commandesStr += "'" + commandes[i][1] + "'" + } + commandesStr += "}" + + return divxlistGetPreferencesService().setCharPref("divxlist.commandes", commandesStr); +} -/***** ProtocolHandler *****/ - -function Ten70ProtocolHandler(scheme) +/** + * Classe ProtocolHandler. + */ +function DivxProtocolHandler(scheme) { this.scheme = scheme; } -// attribute defaults -Ten70ProtocolHandler.prototype.defaultPort = -1; -Ten70ProtocolHandler.prototype.protocolFlags = nsIProtocolHandler.URI_NORELATIVE; - -Ten70ProtocolHandler.prototype.allowPort = function(aPort, aScheme) +// attributs par défaut +DivxProtocolHandler.prototype.defaultPort = -1; +DivxProtocolHandler.prototype.protocolFlags = nsIProtocolHandler.URI_NORELATIVE; + +/** + * est-ce qu'un port est autorisé + */ +DivxProtocolHandler.prototype.allowPort = function(aPort, aScheme) { return false; } - -Ten70ProtocolHandler.prototype.newURI = function(aSpec, aCharset, aBaseURI) + +/** + * Renvoie le nouvel URI. + */ +DivxProtocolHandler.prototype.newURI = function(aSpec, aCharset, aBaseURI) { var uri = Components.classes[URI_CONTRACTID].createInstance(nsIURI); uri.spec = aSpec; return uri; -} - -Ten70ProtocolHandler.prototype.newChannel = function(aURI) +} + +/** + * Renvoie le nouveau channel. + */ +DivxProtocolHandler.prototype.newChannel = function(aURI) { var chemin = aURI.spec; - var root = "F:/Films/"; - chemin = chemin.replace("divx://", root) + // détermine quel commande utilisé en comparant le motif et le nom du fichier + var commandes = divxlistGetCommandes(); + var commande; + for (var i = 0; i < commandes.length; i++) + { + var regexp = new RegExp(commandes[i][0], "i"); + if (regexp.test(chemin)) + { + commande = commandes[i][1] + break; + } + } + + // ajoute le chemin (spécifié dans les options) + var root = divxlistGetRoot(); + // décodage UTF8->latin1 : http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html + chemin = decodeURIComponent(chemin.replace("divx://", root + "/")) + + // extrait le chemin de l'application de la commande + // puis l'enlève de la commande + var application; + var regexp = /^\s*"([^"]+)"\s*/; + var res = regexp.exec(commande); + if (res == null) + { + regexp = /^\s*(\S+)\s*/; + res = regexp.exec(commande); + } + application = res[1] + commande = commande.replace(regexp, ""); - dump(chemin); - - // create an nsILocalFile for the executable + // sépare les paramètres restant, %f est remplacé par le chemin du fichier + var parametres = commande.split(' '); + for (var i = 0; i < parametres.length; i++) + { + parametres[i] = parametres[i].replace("%f", chemin) + } + + // crée un nouveau fichier var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile); - file.initWithPath("C:\\Program Files\\VideoLAN\\VLC\\vlc.exe"); + file.initWithPath(application); - // create an nsIProcess + // crée un nouveau processus var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess); process.init(file); - // Run the process. - // If first param is true, calling thread will be blocked until - // called process terminates. - // Second and third params are used to pass command-line arguments - // to the process. - var args = [chemin, "-f"]; - process.run(false, args, args.length); - - + // lance le processus. false pour ne pas être blockant + process.run(false, parametres, parametres.length); + + // trouver une méthode plus simple pour qu'il ne fasse rien.. + var finalURL = "" var ios = Components.classes[kIOSERVICE_CONTRACTID].getService(nsIIOService); - return ios.newChannel("javascript:document.location='" + finalURL + "'", null, null); } - - -/***** DIVXProtocolHandlerFactory *****/ - -function Ten70ProtocolHandlerFactory(scheme) + +/** + * Classe DivxProtocolHandlerFactory. + */ +function DivxProtocolHandlerFactory(scheme) { this.scheme = scheme; -} - -Ten70ProtocolHandlerFactory.prototype.createInstance = function(outer, iid) +} + +/** + * Méthode permettant la création d'objet de type 'DivxProtocolHandler'. + */ +DivxProtocolHandlerFactory.prototype.createInstance = function(outer, iid) { if(outer != null) throw Components.results.NS_ERROR_NO_AGGREGATION; if(!iid.equals(nsIProtocolHandler) && !iid.equals(nsISupports)) throw Components.results.NS_ERROR_INVALID_ARG; - return new Ten70ProtocolHandler(this.scheme); -} - -var factory_divx = new Ten70ProtocolHandlerFactory("divx"); - -/***** DIVXModule *****/ + return new DivxProtocolHandler(this.scheme); +} + +// la factory +var factory_divx = new DivxProtocolHandlerFactory("divx"); +/** + * Notre module. + */ var DIVXModule = new Object(); - + +/** + * Appelé lors de l'enregistrement du module. + */ DIVXModule.registerSelf = function(compMgr, fileSpec, location, type) { compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); - // register protocol handlers + // on enregistre le protocole handler compMgr.registerFactoryLocation(DIVXPROT_HANDLER_CID, - "DivX", + "divx", DIVXPROT_HANDLER_CONTRACTID, fileSpec, location, type); } - + +/** + * Appelé lors du déchargement du module. + */ DIVXModule.unregisterSelf = function(compMgr, fileSpec, location) { compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar); - // unregister our components + // on dés-enregistre notre protocole handler compMgr.unregisterFactoryLocation(DIVXPROT_HANDLER_CID, fileSpec); } - + +/** + * Demande la factory pour construire le protocole handler. + */ DIVXModule.getClassObject = function(compMgr, cid, iid) { - if(!iid.equals(Components.interfaces.nsIFactory)) - throw Components.results.NS_ERROR_NOT_IMPLEMENTED; + if(!iid.equals(Components.interfaces.nsIFactory)) + throw Components.results.NS_ERROR_NOT_IMPLEMENTED; - if(cid.equals(DIVXPROT_HANDLER_CID)) return factory_divx; + if(cid.equals(DIVXPROT_HANDLER_CID)) return factory_divx; - throw Components.results.NS_ERROR_NO_INTERFACE; + throw Components.results.NS_ERROR_NO_INTERFACE; } - + +/** + * Est-ce que le module peut être déchargé + */ DIVXModule.canUnload = function(compMgr) { return true; } -/***** Entrypoint *****/ - +/** + * Le point d'entré, retourne le module. + */ function NSGetModule(compMgr, fileSpec) { return DIVXModule;