home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2007 September / PCWSEP07.iso / Software / Linux / Linux Mint 3.0 Light / LinuxMint-3.0-Light.iso / casper / filesystem.squashfs / usr / lib / mozilla-thunderbird / components / nsMailDefaultHandler.js < prev    next >
Encoding:
Text File  |  2007-04-03  |  12.0 KB  |  354 lines

  1. /* ***** BEGIN LICENSE BLOCK *****
  2.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  3.  *
  4.  * The contents of this file are subject to the Mozilla Public License Version
  5.  * 1.1 (the "License"); you may not use this file except in compliance with
  6.  * the License. You may obtain a copy of the License at
  7.  * http://www.mozilla.org/MPL/
  8.  *
  9.  * Software distributed under the License is distributed on an "AS IS" basis,
  10.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  11.  * for the specific language governing rights and limitations under the
  12.  * License.
  13.  *
  14.  * The Original Code is the Mozilla Firefox browser.
  15.  *
  16.  * The Initial Developer of the Original Code is
  17.  * Benjamin Smedberg <benjamin@smedbergs.us>
  18.  *
  19.  * Portions created by the Initial Developer are Copyright (C) 2004
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *
  24.  * Alternatively, the contents of this file may be used under the terms of
  25.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  26.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  27.  * in which case the provisions of the GPL or the LGPL are applicable instead
  28.  * of those above. If you wish to allow use of your version of this file only
  29.  * under the terms of either the GPL or the LGPL, and not to allow others to
  30.  * use your version of this file under the terms of the MPL, indicate your
  31.  * decision by deleting the provisions above and replace them with the notice
  32.  * and other provisions required by the GPL or the LGPL. If you do not delete
  33.  * the provisions above, a recipient may use your version of this file under
  34.  * the terms of any one of the MPL, the GPL or the LGPL.
  35.  *
  36.  * ***** END LICENSE BLOCK ***** */
  37.  
  38. const nsISupports              = Components.interfaces.nsISupports;
  39.  
  40. const nsICommandLine           = Components.interfaces.nsICommandLine;
  41. const nsICommandLineHandler    = Components.interfaces.nsICommandLineHandler;
  42. const nsIDOMWindowInternal     = Components.interfaces.nsIDOMWindowInternal;
  43. const nsIFactory               = Components.interfaces.nsIFactory;
  44. const nsISupportsString        = Components.interfaces.nsISupportsString;
  45. const nsIWindowMediator        = Components.interfaces.nsIWindowMediator;
  46. const nsIWindowWatcher         = Components.interfaces.nsIWindowWatcher;
  47.  
  48. const NS_ERROR_ABORT = Components.results.NS_ERROR_ABORT;
  49.  
  50. function mayOpenURI(uri)
  51. {
  52.   var ext = Components.classes["@mozilla.org/uriloader/external-protocol-service;1"]
  53.     .getService(Components.interfaces.nsIExternalProtocolService);
  54.  
  55.   return ext.isExposedProtocol(uri.scheme);
  56. }
  57.  
  58. function openURI(uri)
  59. {
  60.   if (!mayOpenURI(uri))
  61.     throw Components.results.NS_ERROR_FAILURE;
  62.  
  63.   var io = Components.classes["@mozilla.org/network/io-service;1"]
  64.                      .getService(Components.interfaces.nsIIOService);
  65.   var channel = io.newChannelFromURI(uri);
  66.   var loader = Components.classes["@mozilla.org/uriloader;1"]
  67.                          .getService(Components.interfaces.nsIURILoader);
  68.   var listener = {
  69.     onStartURIOpen: function(uri) { return false; },
  70.     doContent: function(ctype, preferred, request, handler) { return false; },
  71.     isPreferred: function(ctype, desired) { return false; },
  72.     canHandleContent: function(ctype, preferred, desired) { return false; },
  73.     loadCookie: null,
  74.     parentContentListener: null,
  75.     getInterface: function(iid) {
  76.       if (iid.equals(Components.interfaces.nsIURIContentListener))
  77.         return this;
  78.  
  79.       throw Components.results.NS_ERROR_NO_INTERFACE;
  80.     }
  81.   };
  82.   loader.openURI(channel, true, listener);
  83. }
  84.  
  85. var nsMailDefaultHandler = {
  86.   /* nsISupports */
  87.  
  88.   QueryInterface : function mdh_QI(iid) {
  89.     if (iid.equals(nsICommandLineHandler) ||
  90.         iid.equals(nsIFactory) ||
  91.         iid.equals(nsISupports))
  92.       return this;
  93.  
  94.     throw Components.results.NS_ERROR_NO_INTERFACE;
  95.   },
  96.  
  97.   /* nsICommandLineHandler */
  98.  
  99.   handle : function mdh_handle(cmdLine) {
  100.     var uri;
  101.  
  102.     try {
  103.       var remoteCommand = cmdLine.handleFlagWithParam("remote", true);
  104.     }
  105.     catch (e) {
  106.       throw NS_ERROR_ABORT;
  107.     }
  108.  
  109.     if (remoteCommand != null) {
  110.       try {
  111.         var a = /^\s*(\w+)\(([^\)]*)\)\s*$/.exec(remoteCommand);
  112.         var remoteVerb = a[1].toLowerCase();
  113.         var remoteParams = a[2].split(",");
  114.  
  115.         switch (remoteVerb) {
  116.         case "openurl":
  117.           var xuri = cmdLine.resolveURI(remoteParams[0]);
  118.           openURI(xuri);
  119.           break;
  120.  
  121.         case "mailto":
  122.           var xuri = cmdLine.resolveURI("mailto:" + remoteParams[0]);
  123.           openURI(xuri);
  124.           break;
  125.  
  126.         case "xfedocommand":
  127.           // xfeDoCommand(openBrowser)
  128.           switch (remoteParams[0].toLowerCase()) {
  129.           case "openinbox":
  130.             var wm = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  131.                                .getService(Components.classes.nsIWindowMediator);
  132.             var win = wm.getMostRecentWindow("mail:3pane");
  133.             if (win) {
  134.               win.focus();
  135.             }
  136.             else {
  137.               var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  138.                                      .getService(nsIWindowWatcher);
  139.  
  140.               // Bug 277798 - we have to pass an argument to openWindow(), or
  141.               // else it won't honor the dialog=no instruction.
  142.               var argstring = Components.classes["@mozilla.org/supports-string;1"]
  143.                                         .createInstance(nsISupportsString);
  144.               wwatch.openWindow(null, "chrome://messenger/content/", "_blank",
  145.                                 "chrome,dialog=no,all", argstring);
  146.             }
  147.             break;
  148.  
  149.           case "composemessage":
  150.             var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  151.                                    .getService(nsIWindowWatcher);
  152.             var argstring = Components.classes["@mozilla.org/supports-string;1"]
  153.                                       .createInstance(nsISupportsString);
  154.             remoteParams.shift();
  155.             argstring.data = remoteParams.join(",");
  156.             wwatch.openWindow(null, "chrome://messenger/content/messengercompose/messengercompose.xul", "_blank",
  157.                               "chrome,dialog=no,all", argstring);
  158.             break;
  159.  
  160.           default:
  161.             throw Components.results.NS_ERROR_ABORT;
  162.           }
  163.           break;
  164.  
  165.         default:
  166.           // Somebody sent us a remote command we don't know how to process:
  167.           // just abort.
  168.           throw Components.results.NS_ERROR_ABORT;
  169.         }
  170.  
  171.         cmdLine.preventDefault = true;
  172.       }
  173.       catch (e) {
  174.         // If we had a -remote flag but failed to process it, throw
  175.         // NS_ERROR_ABORT so that the xremote code knows to return a failure
  176.         // back to the handling code.
  177.         dump(e);
  178.         throw Components.results.NS_ERROR_ABORT;
  179.       }
  180.     }
  181.  
  182.     var chromeParam = cmdLine.handleFlagWithParam("chrome", false);
  183.     if (chromeParam) {
  184.       var features = "chrome,dialog=no,all";
  185.       var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  186.                              .getService(nsIWindowWatcher);
  187.       var argstring = Components.classes["@mozilla.org/supports-string;1"]
  188.                                 .createInstance(nsISupportsString);
  189.       wwatch.openWindow(null, chromeParam, "_blank",
  190.                         "chrome,dialog=no,all", argstring);
  191.       cmdLine.preventDefault = true;
  192.     }
  193.     
  194.     var count = cmdLine.length;
  195.     if (count) {
  196.       var i = 0;
  197.       while (i < count) {
  198.         var curarg = cmdLine.getArgument(i);
  199.         if (!curarg.match(/^-/))
  200.           break;
  201.  
  202.         dump ("Warning: unrecognized command line flag " + curarg + "\n");
  203.         // To emulate the pre-nsICommandLine behavior, we ignore the
  204.         // argument after an unrecognized flag.
  205.         i += 2;
  206.         // xxxbsmedberg: make me use the console service!
  207.       }
  208.  
  209.       if (i < count) {
  210.         uri = cmdLine.getArgument(i);
  211.  
  212.         // mailto: URIs are frequently passed with spaces in them. They should be
  213.         // escaped into %20, but we hack around bad clients, see bug 231032
  214.         if (uri.match(/^mailto:/)) {
  215.           while (++i < count) {
  216.             var testarg = cmdLine.getArgument(i);
  217.             if (testarg.match(/^-/))
  218.               break;
  219.  
  220.             uri += " " + testarg;
  221.           }
  222.         }
  223.       }
  224.     }
  225.  
  226.     if (!uri && cmdLine.preventDefault)
  227.       return;
  228.  
  229.     // xxxbsmedberg: This should be using nsIURILoader.openURI, which is what
  230.     // the 1.0 branch does (see nsAppShellService.cpp, revision 1.212.6.6).
  231.     // However, nsIURILoader.openURI is async, which means that the event loop
  232.     // sometimes is not run when it is supposed to, and other badness.
  233.  
  234.     if (cmdLine.state != nsICommandLine.STATE_INITIAL_LAUNCH) {
  235.       if (uri) {
  236.         openURI(cmdLine.resolveURI(uri));
  237.         return;
  238.       }
  239.       else {
  240.         try {
  241.           var wmed = Components.classes["@mozilla.org/appshell/window-mediator;1"]
  242.                                .getService(nsIWindowMediator);
  243.  
  244.           var wlist = wmed.getEnumerator("mail:3pane");
  245.           if (wlist.hasMoreElements()) {
  246.             var window = wlist.getNext().QueryInterface(nsIDOMWindowInternal);
  247.             window.focus();
  248.             return;
  249.           }
  250.         }
  251.         catch (e) {
  252.           dump(e);
  253.         }
  254.       }
  255.     }
  256.  
  257.     var wwatch = Components.classes["@mozilla.org/embedcomp/window-watcher;1"]
  258.                            .getService(nsIWindowWatcher);
  259.  
  260.     var argstring = Components.classes["@mozilla.org/supports-string;1"]
  261.                               .createInstance(nsISupportsString);
  262.  
  263.     var chromeURI = "chrome://messenger/content/";
  264.  
  265.     if (uri) {
  266.       argstring.data = uri;
  267.  
  268.       if (uri.match(/^mailto:/)) {
  269.         chromeURI = "chrome://messenger/content/messengercompose/messengercompose.xul";
  270.       }
  271.     }
  272.  
  273.     wwatch.openWindow(null, chromeURI, "_blank",
  274.                       "chrome,dialog=no,all", argstring);
  275.   },
  276.  
  277.   helpInfo : "",
  278.  
  279.   /* nsIFactory */
  280.  
  281.   createInstance : function mdh_CI(outer, iid) {
  282.     if (outer != null)
  283.       throw Components.results.NS_ERROR_NO_AGGREGATION;
  284.  
  285.     return this.QueryInterface(iid);
  286.   },
  287.  
  288.   lockFactory : function mdh_lock(lock) {
  289.     /* no-op */
  290.   }
  291. };
  292.  
  293. const mdh_contractID = "@mozilla.org/mail/clh;1";
  294. const mdh_CID = Components.ID("{44346520-c5d2-44e5-a1ec-034e04d7fac4}");
  295.  
  296. var Module = {
  297.   /* nsISupports */
  298.  
  299.   QueryInterface : function QI(iid) {
  300.     if (iid.equals(Components.interfaces.nsIModule) &&
  301.         iid.equals(Components.interfaces.nsISupports))
  302.       return this;
  303.  
  304.     throw Components.results.NS_ERROR_NO_INTERFACE;
  305.   },
  306.  
  307.   /* nsIModule */
  308.   getClassObject : function (compMgr, cid, iid) {
  309.     if (cid.equals(mdh_CID))
  310.       return nsMailDefaultHandler.QueryInterface(iid);
  311.  
  312.     throw Components.results.NS_ERROR_FAILURE;
  313.   },
  314.     
  315.   registerSelf: function mod_regself(compMgr, fileSpec, location, type) {
  316.     var compReg =
  317.       compMgr.QueryInterface( Components.interfaces.nsIComponentRegistrar );
  318.  
  319.     compReg.registerFactoryLocation(mdh_CID,
  320.                                     "nsMailDefaultHandler",
  321.                                     mdh_contractID,
  322.                                     fileSpec,
  323.                                     location,
  324.                                     type );
  325.  
  326.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  327.                            .getService(Components.interfaces.nsICategoryManager);
  328.  
  329.     catMan.addCategoryEntry("command-line-handler",
  330.                             "x-default",
  331.                             mdh_contractID, true, true);
  332.   },
  333.     
  334.   unregisterSelf : function mod_unregself(compMgr, location, type) {
  335.     var compReg = compMgr.QueryInterface(nsIComponentRegistrar);
  336.     compReg.unregisterFactoryLocation(mdh_CID, location);
  337.  
  338.     var catMan = Components.classes["@mozilla.org/categorymanager;1"]
  339.                            .getService(Components.interfaces.nsICategoryManager);
  340.  
  341.     catMan.deleteCategoryEntry("command-line-handler",
  342.                                "y-default", true);
  343.   },
  344.  
  345.   canUnload: function(compMgr) {
  346.     return true;
  347.   }
  348. }
  349.  
  350. // NSGetModule: Return the nsIModule object.
  351. function NSGetModule(compMgr, fileSpec) {
  352.   return Module;
  353. }
  354.