home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / firefox / components / nsSidebar.js < prev    next >
Encoding:
Text File  |  2006-07-05  |  16.3 KB  |  428 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * ***** BEGIN LICENSE BLOCK *****
  3.  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4.  *
  5.  * The contents of this file are subject to the Mozilla Public License Version
  6.  * 1.1 (the "License"); you may not use this file except in compliance with
  7.  * the License. You may obtain a copy of the License at
  8.  * http://www.mozilla.org/MPL/
  9.  *
  10.  * Software distributed under the License is distributed on an "AS IS" basis,
  11.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12.  * for the specific language governing rights and limitations under the
  13.  * License.
  14.  *
  15.  * The Original Code is mozilla.org code.
  16.  *
  17.  * The Initial Developer of the Original Code is
  18.  * Netscape Communications Corporation.
  19.  * Portions created by the Initial Developer are Copyright (C) 1999
  20.  * the Initial Developer. All Rights Reserved.
  21.  *
  22.  * Contributor(s):
  23.  *   Stephen Lamm            <slamm@netscape.com>
  24.  *   Robert John Churchill   <rjc@netscape.com>
  25.  *   David Hyatt             <hyatt@mozilla.org>
  26.  *   Christopher A. Aillon   <christopher@aillon.com>
  27.  *   Myk Melez               <myk@mozilla.org>
  28.  *   Pamela Greene           <pamg.bugs@gmail.com>
  29.  *
  30.  * Alternatively, the contents of this file may be used under the terms of
  31.  * either the GNU General Public License Version 2 or later (the "GPL"), or
  32.  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  33.  * in which case the provisions of the GPL or the LGPL are applicable instead
  34.  * of those above. If you wish to allow use of your version of this file only
  35.  * under the terms of either the GPL or the LGPL, and not to allow others to
  36.  * use your version of this file under the terms of the MPL, indicate your
  37.  * decision by deleting the provisions above and replace them with the notice
  38.  * and other provisions required by the GPL or the LGPL. If you do not delete
  39.  * the provisions above, a recipient may use your version of this file under
  40.  * the terms of any one of the MPL, the GPL or the LGPL.
  41.  *
  42.  * ***** END LICENSE BLOCK ***** */
  43.  
  44. /*
  45.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  46.  * If you must perform some initialization, and it could possibly fail (even
  47.  * due to an out-of-memory condition), you should use an Init method, which
  48.  * can convey failure appropriately (thrown exception in JS,
  49.  * NS_FAILED(nsresult) return in C++).
  50.  *
  51.  * In JS, you can actually cheat, because a thrown exception will cause the
  52.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  53.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  54.  * for portability reasons -- and even when you're building completely
  55.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  56.  */
  57.  
  58. const DEBUG = false; /* set to false to suppress debug messages */
  59.  
  60. const SIDEBAR_CONTRACTID            = "@mozilla.org/sidebar;1";
  61. const SIDEBAR_CID                   = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
  62. const NETSEARCH_CONTRACTID          = "@mozilla.org/rdf/datasource;1?name=internetsearch"
  63. const nsISupports                   = Components.interfaces.nsISupports;
  64. const nsIFactory                    = Components.interfaces.nsIFactory;
  65. const nsISidebar                    = Components.interfaces.nsISidebar;
  66. const nsISidebar_MOZILLA_1_8_BRANCH = Components.interfaces.nsISidebar_MOZILLA_1_8_BRANCH;
  67. const nsISidebarExternal            = Components.interfaces.nsISidebarExternal;
  68. const nsIInternetSearchService      = Components.interfaces.nsIInternetSearchService;
  69. const nsIClassInfo                  = Components.interfaces.nsIClassInfo;
  70.  
  71. // File extension for Sherlock search plugin description files
  72. const SHERLOCK_FILE_EXT_REGEXP = /\.src$/i;
  73.  
  74. function nsSidebar()
  75. {
  76.     const PROMPTSERVICE_CONTRACTID = "@mozilla.org/embedcomp/prompt-service;1";
  77.     const nsIPromptService = Components.interfaces.nsIPromptService;
  78.     this.promptService =
  79.         Components.classes[PROMPTSERVICE_CONTRACTID].getService(nsIPromptService);
  80.  
  81.     const SEARCHSERVICE_CONTRACTID = "@mozilla.org/browser/search-service;1";
  82.     const nsIBrowserSearchService = Components.interfaces.nsIBrowserSearchService;
  83.     this.searchService =
  84.       Components.classes[SEARCHSERVICE_CONTRACTID].getService(nsIBrowserSearchService);
  85. }
  86.  
  87. nsSidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
  88.  
  89. function sidebarURLSecurityCheck(url)
  90. {
  91.     if (!/^(https?:|ftp:)/i.test(url)) {
  92.         Components.utils.reportError("Invalid argument passed to window.sidebar.addPanel: Unsupported panel URL." );
  93.         return false;
  94.     }
  95.     return true;
  96. }
  97.  
  98. /* decorate prototype to provide ``class'' methods and property accessors */
  99. nsSidebar.prototype.addPanel =
  100. function (aTitle, aContentURL, aCustomizeURL)
  101. {
  102.     debug("addPanel(" + aTitle + ", " + aContentURL + ", " +
  103.           aCustomizeURL + ")");
  104.    
  105.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, false);
  106. }
  107.  
  108. nsSidebar.prototype.addPersistentPanel = 
  109. function(aTitle, aContentURL, aCustomizeURL)
  110. {
  111.     debug("addPersistentPanel(" + aTitle + ", " + aContentURL + ", " +
  112.            aCustomizeURL + ")\n");
  113.  
  114.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, true);
  115. }
  116.  
  117. nsSidebar.prototype.addPanelInternal =
  118. function (aTitle, aContentURL, aCustomizeURL, aPersist)
  119. {
  120.     var WINMEDSVC = Components.classes['@mozilla.org/appshell/window-mediator;1']
  121.                               .getService(Components.interfaces.nsIWindowMediator);
  122.     var win = WINMEDSVC.getMostRecentWindow( "navigator:browser" );
  123.                                                                                 
  124.     if (!sidebarURLSecurityCheck(aContentURL))
  125.       return;
  126.  
  127.     var dialogArgs = {
  128.       name: aTitle,
  129.       url: aContentURL,
  130.       bWebPanel: true
  131.     }
  132.     var features;
  133.     if (!/Mac/.test(win.navigator.platform))
  134.       features = "centerscreen,chrome,dialog,resizable,dependent";
  135.     else
  136.       features = "chrome,dialog,resizable,modal";
  137.     win.openDialog("chrome://browser/content/bookmarks/addBookmark2.xul", "",
  138.                    features, dialogArgs);
  139. }
  140.  
  141. nsSidebar.prototype.confirmSearchEngine =
  142. function (engineURL, suggestedTitle)
  143. {
  144.   var stringBundle = srGetStrBundle("chrome://browser/locale/sidebar/sidebar.properties");
  145.   var titleMessage = stringBundle.GetStringFromName("addEngineConfirmTitle");
  146.  
  147.   // With tentative title: Add "Somebody's Search" to the list...
  148.   // With no title:        Add a new search engine to the list...
  149.   var engineName;
  150.   if (suggestedTitle)
  151.     engineName = stringBundle.formatStringFromName("addEngineQuotedEngineName",
  152.                                                    [suggestedTitle], 1);
  153.   else
  154.     engineName = stringBundle.GetStringFromName("addEngineDefaultEngineName");
  155.  
  156.   // Display only the hostname portion of the URL.
  157.   try {
  158.     var ios = Components.classes["@mozilla.org/network/io-service;1"]
  159.                         .getService(Components.interfaces.nsIIOService);
  160.     var uri = ios.newURI(engineURL, null, null);
  161.     engineURL = uri.host;
  162.   }
  163.   catch (e) {
  164.     // If newURI fails, fall back to the full URL.
  165.   }
  166.  
  167.   var dialogMessage = stringBundle.formatStringFromName("addEngineConfirmText",
  168.                                                         [engineName, engineURL], 2);
  169.   var checkboxMessage = stringBundle.GetStringFromName("addEngineUseNowText");
  170.   var addButtonLabel = stringBundle.GetStringFromName("addEngineAddButtonLabel");
  171.  
  172.   const ps = this.promptService;
  173.   var buttonFlags = (ps.BUTTON_TITLE_IS_STRING * ps.BUTTON_POS_0) +
  174.                     (ps.BUTTON_TITLE_CANCEL    * ps.BUTTON_POS_1) +
  175.                      ps.BUTTON_POS_0_DEFAULT;
  176.  
  177.   var checked = {value: false};
  178.   // confirmEx returns the index of the button that was pressed.  Since "Add" is
  179.   // button 0, we want to return the negation of that value.
  180.   var confirm = !this.promptService.confirmEx(null,
  181.                                          titleMessage,
  182.                                          dialogMessage,
  183.                                          buttonFlags,
  184.                                          addButtonLabel,
  185.                                          "", "", // button 1 & 2 names not used
  186.                                          checkboxMessage,
  187.                                          checked);
  188.  
  189.   return {confirmed: confirm, useNow: checked.value};
  190. }
  191.  
  192. nsSidebar.prototype.validateSearchEngine =
  193. function (engineURL, iconURL)
  194. {
  195.   try
  196.   {
  197.     // Make sure we're using HTTP, HTTPS, or FTP.
  198.     if (! /^(https?|ftp):\/\//i.test(engineURL))
  199.       throw "Unsupported search engine URL";
  200.   
  201.     // Make sure we're using HTTP, HTTPS, or FTP and refering to a
  202.     // .gif/.jpg/.jpeg/.png/.ico file for the icon.
  203.     if (iconURL &&
  204.         ! /^(https?|ftp):\/\/.+\.(gif|jpg|jpeg|png|ico)$/i.test(iconURL))
  205.       throw "Unsupported search icon URL.";
  206.   }
  207.   catch(ex)
  208.   {
  209.     debug(ex);
  210.     Components.utils.reportError("Invalid argument passed to window.sidebar.addSearchEngine: " + ex);
  211.     
  212.     var searchBundle = srGetStrBundle("chrome://browser/locale/search.properties");
  213.     var brandBundle = srGetStrBundle("chrome://branding/locale/brand.properties");
  214.     var brandName = brandBundle.GetStringFromName("brandShortName");
  215.     var title = searchBundle.GetStringFromName("error_invalid_engine_title");
  216.     var msg = searchBundle.formatStringFromName("error_invalid_engine_msg",
  217.                                                 [brandName], 1);
  218.     var ww = Components.classes["@mozilla.org/embedcomp/window-watcher;1"].
  219.              getService(Components.interfaces.nsIWindowWatcher);
  220.     ww.getNewPrompter(null).alert(title, msg);
  221.     return false;
  222.   }
  223.   
  224.   return true;
  225. }
  226.  
  227. nsSidebar.prototype.addSearchEngine =
  228. function (engineURL, iconURL, suggestedTitle, suggestedCategory)
  229. {
  230.   debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
  231.         suggestedCategory + ", " + suggestedTitle + ")");
  232.  
  233.   if (!this.validateSearchEngine(engineURL, iconURL))
  234.     return;
  235.  
  236.   var confirmation = this.confirmSearchEngine(engineURL, suggestedTitle);
  237.   if (!confirmation.confirmed)
  238.     return;
  239.  
  240.   // OpenSearch files will likely be far more common than Sherlock files, and
  241.   // have less consistent suffixes, so we assume that ".src" is a Sherlock
  242.   // (text) file, and anything else is OpenSearch (XML).
  243.   var dataType;
  244.   if (SHERLOCK_FILE_EXT_REGEXP.test(engineURL))
  245.     dataType = Components.interfaces.nsISearchEngine.DATA_TEXT;
  246.   else
  247.     dataType = Components.interfaces.nsISearchEngine.DATA_XML;
  248.  
  249.   this.searchService.addEngine(engineURL, dataType, iconURL,
  250.                                confirmation.useNow);
  251. }
  252.  
  253. // This function exists largely to implement window.external.AddSearchProvider(),
  254. // to match other browsers' APIs.  The capitalization, although nonstandard here,
  255. // is therefore important.
  256. nsSidebar.prototype.AddSearchProvider =
  257. function (aDescriptionURL)
  258. {
  259.   // Get the favicon URL for the current page, or our best guess at the current
  260.   // page since we don't have easy access to the active document.  Most search
  261.   // engines will override this with an icon specified in the OpenSearch
  262.   // description anyway.
  263.   var WINMEDSVC = Components.classes['@mozilla.org/appshell/window-mediator;1']
  264.                             .getService(Components.interfaces.nsIWindowMediator);
  265.   var win = WINMEDSVC.getMostRecentWindow("navigator:browser");
  266.   var browser = win.document.getElementById("content");
  267.   var iconURL = "";
  268.   if (browser.shouldLoadFavIcon(browser.selectedBrowser.currentURI))
  269.     iconURL = win.gProxyFavIcon.getAttribute("src");
  270.   
  271.   if (!this.validateSearchEngine(aDescriptionURL, iconURL))
  272.     return;
  273.  
  274.   var confirmation = this.confirmSearchEngine(aDescriptionURL, "");
  275.   if (!confirmation.confirmed)
  276.     return;
  277.  
  278.   const typeXML = Components.interfaces.nsISearchEngine.DATA_XML;
  279.   this.searchService.addEngine(aDescriptionURL, typeXML, iconURL,
  280.                                confirmation.useNow);
  281. }
  282.  
  283. nsSidebar.prototype.addMicrosummaryGenerator =
  284. function (generatorURL)
  285. {
  286.     debug("addMicrosummaryGenerator(" + generatorURL + ")");
  287.  
  288.     var stringBundle = srGetStrBundle("chrome://browser/locale/sidebar/sidebar.properties");
  289.     var titleMessage = stringBundle.GetStringFromName("addMicsumGenConfirmTitle");
  290.     var dialogMessage = stringBundle.formatStringFromName("addMicsumGenConfirmText", [generatorURL], 1);
  291.       
  292.     if (!this.promptService.confirm(null, titleMessage, dialogMessage))
  293.         return;
  294.  
  295.     var ioService = Components.classes["@mozilla.org/network/io-service;1"].
  296.                     getService(Components.interfaces.nsIIOService);
  297.     var generatorURI = ioService.newURI(generatorURL, null, null);
  298.  
  299.     var microsummaryService = Components.classes["@mozilla.org/microsummary/service;1"].
  300.                               getService(Components.interfaces.nsIMicrosummaryService);
  301.     if (microsummaryService)
  302.       microsummaryService.addGenerator(generatorURI);
  303. }
  304.  
  305. // property of nsIClassInfo
  306. nsSidebar.prototype.flags = nsIClassInfo.DOM_OBJECT;
  307.  
  308. // property of nsIClassInfo
  309. nsSidebar.prototype.classDescription = "Sidebar";
  310.  
  311. // method of nsIClassInfo
  312. nsSidebar.prototype.getInterfaces = function(count) {
  313.     var interfaceList = [nsISidebar, nsISidebar_MOZILLA_1_8_BRANCH, 
  314.                          nsIClassInfo, nsISidebarExternal];
  315.     count.value = interfaceList.length;
  316.     return interfaceList;
  317. }
  318.  
  319. // method of nsIClassInfo
  320. nsSidebar.prototype.getHelperForLanguage = function(count) {return null;}
  321.  
  322. nsSidebar.prototype.QueryInterface =
  323. function (iid) {
  324.     if (!iid.equals(nsISidebar) && 
  325.         !iid.equals(nsIClassInfo) &&
  326.         !iid.equals(nsISupports) &&
  327.         !iid.equals(nsISidebar_MOZILLA_1_8_BRANCH) &&
  328.         !iid.equals(nsISidebarExternal))
  329.         throw Components.results.NS_ERROR_NO_INTERFACE;
  330.     return this;
  331. }
  332.  
  333. var sidebarModule = new Object();
  334.  
  335. sidebarModule.registerSelf =
  336. function (compMgr, fileSpec, location, type)
  337. {
  338.     debug("registering (all right -- a JavaScript module!)");
  339.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  340.  
  341.     compMgr.registerFactoryLocation(SIDEBAR_CID, 
  342.                                     "Sidebar JS Component",
  343.                                     SIDEBAR_CONTRACTID, 
  344.                                     fileSpec, 
  345.                                     location,
  346.                                     type);
  347.     const CATMAN_CONTRACTID = "@mozilla.org/categorymanager;1";
  348.     const nsICategoryManager = Components.interfaces.nsICategoryManager;
  349.     var catman = Components.classes[CATMAN_CONTRACTID].
  350.                             getService(nsICategoryManager);
  351.                             
  352.     const JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY = "JavaScript global property";
  353.     catman.addCategoryEntry(JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY,
  354.                             "sidebar",
  355.                             SIDEBAR_CONTRACTID,
  356.                             true,
  357.                             true);
  358.                             
  359.     catman.addCategoryEntry(JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY,
  360.                             "external",
  361.                             SIDEBAR_CONTRACTID,
  362.                             true,
  363.                             true);
  364. }
  365.  
  366. sidebarModule.getClassObject =
  367. function (compMgr, cid, iid) {
  368.     if (!cid.equals(SIDEBAR_CID))
  369.         throw Components.results.NS_ERROR_NO_INTERFACE;
  370.     
  371.     if (!iid.equals(Components.interfaces.nsIFactory))
  372.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  373.     
  374.     return sidebarFactory;
  375. }
  376.  
  377. sidebarModule.canUnload =
  378. function(compMgr)
  379. {
  380.     debug("Unloading component.");
  381.     return true;
  382. }
  383.     
  384. /* factory object */
  385. var sidebarFactory = new Object();
  386.  
  387. sidebarFactory.createInstance =
  388. function (outer, iid) {
  389.     debug("CI: " + iid);
  390.     if (outer != null)
  391.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  392.  
  393.     return (new nsSidebar()).QueryInterface(iid);
  394. }
  395.  
  396. /* entrypoint */
  397. function NSGetModule(compMgr, fileSpec) {
  398.     return sidebarModule;
  399. }
  400.  
  401. /* static functions */
  402. if (DEBUG)
  403.     debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
  404. else
  405.     debug = function (s) {}
  406.  
  407. var strBundleService = null;
  408. function srGetStrBundle(path)
  409. {
  410.    var strBundle = null;
  411.    if (!strBundleService) {
  412.        try {
  413.           strBundleService =
  414.           Components.classes["@mozilla.org/intl/stringbundle;1"].getService(); 
  415.           strBundleService = 
  416.           strBundleService.QueryInterface(Components.interfaces.nsIStringBundleService);
  417.        } catch (ex) {
  418.           dump("\n--** strBundleService failed: " + ex + "\n");
  419.           return null;
  420.       }
  421.    }
  422.    strBundle = strBundleService.createBundle(path); 
  423.    if (!strBundle) {
  424.        dump("\n--** strBundle createInstance failed **--\n");
  425.    }
  426.    return strBundle;
  427. }
  428.