git-svn-id: svn://euphorik.ch/pompage@45 02bbb61a-6d21-0410-aba0-cb053bdfd66a
[pompage.git] / xpi / components / handleProtocol.js
index 30db1f1..8150a6c 100644 (file)
-
-
-// components defined in this file
+// voir : http://www.nexgenmedia.net/docs/protocol/\r
+\r
+// 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";\r
+// les composants utilisés\r
+const URI_CONTRACTID          = "@mozilla.org/network/simple-uri;1";
+const kIOSERVICE_CONTRACTID   = "@mozilla.org/network/io-service;1";\r
 
-// 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;
 
+\r
+/******* COPIER-COLLER de pref.js dans /chrome/content *******/\r
+/******* TODO : trouver un moyen d'inclure pref.js *******/\r
+function divxlistGetPreferencesService()\r
+{\r
+   return Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch("");\r
+}\r
+function divxlistGetRoot()\r
+{\r
+   return divxlistGetPreferencesService().getCharPref("divxlist.root");\r
+}\r
+function divxlistSetRoot(root)\r
+{\r
+   return divxlistGetPreferencesService().setCharPref("divxlist.root", root);\r
+}\r
+function divxlistGetCommandes()\r
+{\r
+   var commandes = divxlistGetPreferencesService().getCharPref("divxlist.commandes");\r
+   var regex = /'[^']*'/g\r
+   var res = commandes.match(regex);\r
+   var commandesTab = new Array()\r
+   if (res != null)\r
+      for(var i = 0; i < res.length; i += 2)\r
+      {\r
+         var motifCommande = new Array(2);\r
+         motifCommande[0] = res[i].slice(1, res[i].length-1)\r
+         motifCommande[1] = res[i+1].slice(1, res[i+1].length-1)\r
+         commandesTab.push(motifCommande);       \r
+      }\r
+   return commandesTab;\r
+}\r
+function divxlistSetCommandes(commandes)\r
+{\r
+   var commandesStr = "{"\r
+   for (var i = 0; i < commandes.length; i++)\r
+   {   \r
+      if (i != 0) commandesStr += ", "\r
+      commandesStr += "'" + commandes[i][0] + "' => "\r
+      commandesStr += "'" + commandes[i][1] + "'"\r
+   }\r
+   commandesStr += "}"\r
+   \r
+   return divxlistGetPreferencesService().setCharPref("divxlist.commandes", commandesStr);\r
+}\r
 
-/***** ProtocolHandler *****/
-
-function Ten70ProtocolHandler(scheme)
+/**\r
+  * Classe ProtocolHandler.\r
+  */
+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;
+\r
+/**\r
+  * est-ce qu'un port est autorisé \r
+  */
+DivxProtocolHandler.prototype.allowPort = function(aPort, aScheme)
 {
     return false;
 }
-
-Ten70ProtocolHandler.prototype.newURI = function(aSpec, aCharset, aBaseURI)
+\r
+/**\r
+  * Renvoie le nouvel URI.\r
+  */
+DivxProtocolHandler.prototype.newURI = function(aSpec, aCharset, aBaseURI)
 {    \r
    var uri = Components.classes[URI_CONTRACTID].createInstance(nsIURI);\r
    uri.spec = aSpec;\r
    return uri;
-}
-
-Ten70ProtocolHandler.prototype.newChannel = function(aURI)
+}\r
+\r
+/**\r
+  * Renvoie le nouveau channel.\r
+  */
+DivxProtocolHandler.prototype.newChannel = function(aURI)
 {\r
    var chemin = aURI.spec;\r
    \r
-   var root = "F:/Films/";\r
-   chemin = chemin.replace("divx://", root)\r
+   // détermine quel commande utilisé en comparant le motif et le nom du fichier\r
+   var commandes = divxlistGetCommandes();\r
+   var commande;\r
+   for (var i = 0; i < commandes.length; i++)\r
+   {\r
+      var regexp = new RegExp(commandes[i][0], "i");\r
+      if (regexp.test(chemin))\r
+      {\r
+         commande = commandes[i][1]\r
+         break;\r
+      }\r
+   }\r
+   \r
+   // ajoute le chemin (spécifié dans les options)\r
+   var root = divxlistGetRoot();\r
+   // décodage UTF8->latin1 : http://ecmanaut.blogspot.com/2006/07/encoding-decoding-utf8-in-javascript.html\r
+   chemin = decodeURIComponent(chemin.replace("divx://", root + "/"))\r
+   \r
+   // extrait le chemin de l'application de la commande\r
+   // puis l'enlève de la commande\r
+   var application;\r
+   var regexp = /^\s*"([^"]+)"\s*/;\r
+   var res = regexp.exec(commande);\r
+   if (res == null)\r
+   {\r
+      regexp = /^\s*(\S+)\s*/;\r
+      res = regexp.exec(commande);\r
+   }\r
+   application = res[1]\r
+   commande = commande.replace(regexp, "");\r
    \r
-   dump(chemin);\r
-      \r
-   // create an nsILocalFile for the executable\r
+   // sépare les paramètres restant, %f est remplacé par le chemin du fichier\r
+   var parametres = commande.split(' ');\r
+   for (var i = 0; i < parametres.length; i++)\r
+   {\r
+      parametres[i] = parametres[i].replace("%f", chemin)\r
+   }\r
+   \r
+   // crée un nouveau fichier\r
    var file = Components.classes["@mozilla.org/file/local;1"].createInstance(Components.interfaces.nsILocalFile);\r
-   file.initWithPath("C:\\Program Files\\VideoLAN\\VLC\\vlc.exe");\r
+   file.initWithPath(application);\r
 \r
-   // create an nsIProcess\r
+   // crée un nouveau processus\r
    var process = Components.classes["@mozilla.org/process/util;1"].createInstance(Components.interfaces.nsIProcess);\r
    process.init(file);\r
 \r
-   // Run the process.\r
-   // If first param is true, calling thread will be blocked until\r
-   // called process terminates.\r
-   // Second and third params are used to pass command-line arguments\r
-   // to the process.\r
-   var args = [chemin, "-f"];\r
-   process.run(false, args, args.length);\r
-   \r
-   \r
+   // lance le processus. false pour ne pas être blockant\r
+   process.run(false, parametres, parametres.length);\r
+     \r
+   // trouver une méthode plus simple pour qu'il ne fasse rien..\r
+   var finalURL = ""\r
    var ios = Components.classes[kIOSERVICE_CONTRACTID].getService(nsIIOService);\r
-\r
    return ios.newChannel("javascript:document.location='" + finalURL + "'", null, null);
 }
-
-
-/***** DIVXProtocolHandlerFactory *****/
-
-function Ten70ProtocolHandlerFactory(scheme)
+\r
+/**\r
+  * Classe DivxProtocolHandlerFactory.\r
+  */
+function DivxProtocolHandlerFactory(scheme)
 {
     this.scheme = scheme;
-}
-
-Ten70ProtocolHandlerFactory.prototype.createInstance = function(outer, iid)
+}\r
+\r
+/**\r
+  * Méthode permettant la création d'objet de type 'DivxProtocolHandler'.\r
+  */
+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);
+}\r
+\r
+// la factory
+var factory_divx = new DivxProtocolHandlerFactory("divx");
 
+/**\r
+  * Notre module.\r
+  */
 var DIVXModule = new Object();
-
+\r
+/**\r
+  * Appelé lors de l'enregistrement du module.\r
+  */
 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);
 }
-
+\r
+/**\r
+  * Appelé lors du déchargement du module.\r
+  */
 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);
 }
-
+\r
+/**\r
+  * Demande la factory pour construire le protocole handler.\r
+  */
 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;
 }
-
+\r
+/**\r
+  * Est-ce que le module peut être déchargé\r
+  */
 DIVXModule.canUnload = function(compMgr)
 {
     return true;
 }
 
-/***** Entrypoint *****/
-
+/**\r
+  * Le point d'entré, retourne le module.\r
+  */
 function NSGetModule(compMgr, fileSpec)
 {
     return DIVXModule;