X-Git-Url: http://git.euphorik.ch/?p=pompage.git;a=blobdiff_plain;f=xpi%2Fcomponents%2FhandleProtocol.js;fp=xpi%2Fcomponents%2FhandleProtocol.js;h=d10fa53b690771930ba4e4ffe48518bab0afd7ba;hp=96562cca340e13275075742f2ecf8f0e5889112a;hb=360b03e976f53bd285b4f88e41bedf72e644898b;hpb=14d8b43a06d6c0bd18c40d79509b6afe2047af0d diff --git a/xpi/components/handleProtocol.js b/xpi/components/handleProtocol.js index 96562cc..d10fa53 100644 --- a/xpi/components/handleProtocol.js +++ b/xpi/components/handleProtocol.js @@ -1,32 +1,78 @@ -// Le composant définit pour cette extension +// 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}"); -// Les composants utilisés +// les composants utilisés const URI_CONTRACTID = "@mozilla.org/network/simple-uri;1"; const kIOSERVICE_CONTRACTID = "@mozilla.org/network/io-service;1"; -// Les interfaces utilisées +// les interfaces utilisées const nsIProtocolHandler = Components.interfaces.nsIProtocolHandler; const nsIURI = Components.interfaces.nsIURI; const nsISupports = Components.interfaces.nsISupports; const nsIIOService = Components.interfaces.nsIIOService; + +/******* 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); +} /** * Classe ProtocolHandler. */ -function ProtocolHandler(scheme) +function DivxProtocolHandler(scheme) { this.scheme = scheme; } -// attributs par défaut +// attributs par défaut DivxProtocolHandler.prototype.defaultPort = -1; DivxProtocolHandler.prototype.protocolFlags = nsIProtocolHandler.URI_NORELATIVE; /** - * est-ce qu'un port est autorisé. + * est-ce qu'un port est autorisé */ DivxProtocolHandler.prototype.allowPort = function(aPort, aScheme) { @@ -50,24 +96,56 @@ 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(); + chemin = 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); + // 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 + // 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); - // crée un nouveau processus + // crée un nouveau processus var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess); process.init(file); // lance le processus. false pour ne pas être blockant - var args = [chemin, "-f"]; - process.run(false, args, args.length); - - // trouver une méthode plus simple pour qu'il ne fasse rien.. + 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); } @@ -140,7 +218,7 @@ DIVXModule.getClassObject = function(compMgr, cid, iid) } /** - * Est-ce que le module peut être déchargé ? + * Est-ce que le module peut être déchargé */ DIVXModule.canUnload = function(compMgr) { @@ -148,7 +226,7 @@ DIVXModule.canUnload = function(compMgr) } /** - * Le point d'entré, retourne le module. + * Le point d'entré, retourne le module. */ function NSGetModule(compMgr, fileSpec) {