home *** CD-ROM | disk | FTP | other *** search
/ PC World 2005 March / PCWorld_2005-03_cd.bin / komunikace / kmeleon / kmeleon09.exe / nsSidebar.js < prev    next >
Text File  |  2003-09-13  |  18KB  |  460 lines

  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * The contents of this file are subject to the Mozilla Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/MPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is mozilla.org code.
  13.  * 
  14.  * The Initial Developer of the Original Code is Netscape
  15.  * Communications Corporation.  Portions created by Netscape are
  16.  * Copyright (C) 1999 Netscape Communications Corporation.  All
  17.  * Rights Reserved.
  18.  * 
  19.  * Contributor(s): Stephen Lamm            <slamm@netscape.com>
  20.  *                 Robert John Churchill   <rjc@netscape.com>
  21.  */
  22.  
  23. /*
  24.  * No magic constructor behaviour, as is de rigeur for XPCOM.
  25.  * If you must perform some initialization, and it could possibly fail (even
  26.  * due to an out-of-memory condition), you should use an Init method, which
  27.  * can convey failure appropriately (thrown exception in JS,
  28.  * NS_FAILED(nsresult) return in C++).
  29.  *
  30.  * In JS, you can actually cheat, because a thrown exception will cause the
  31.  * CreateInstance call to fail in turn, but not all languages are so lucky.
  32.  * (Though ANSI C++ provides exceptions, they are verboten in Mozilla code
  33.  * for portability reasons -- and even when you're building completely
  34.  * platform-specific code, you can't throw across an XPCOM method boundary.)
  35.  */
  36.  
  37. const DEBUG = false; /* set to false to suppress debug messages */
  38. const PANELS_RDF_FILE  = "UPnls"; /* directory services property to find panels.rdf */
  39.  
  40. const SIDEBAR_CONTRACTID   = "@mozilla.org/sidebar;1";
  41. const SIDEBAR_CID      = Components.ID("{22117140-9c6e-11d3-aaf1-00805f8a4905}");
  42. const CONTAINER_CONTRACTID = "@mozilla.org/rdf/container;1";
  43. const DIR_SERV_CONTRACTID  = "@mozilla.org/file/directory_service;1"
  44. const NETSEARCH_CONTRACTID = "@mozilla.org/rdf/datasource;1?name=internetsearch"
  45. const IO_SERV_CONTRACTID   = "@mozilla.org/network/io-service;1";
  46. const nsISupports      = Components.interfaces.nsISupports;
  47. const nsIFactory       = Components.interfaces.nsIFactory;
  48. const nsISidebar       = Components.interfaces.nsISidebar;
  49. const nsIRDFContainer  = Components.interfaces.nsIRDFContainer;
  50. const nsIProperties    = Components.interfaces.nsIProperties;
  51. const nsIFileURL       = Components.interfaces.nsIFileURL;
  52. const nsIRDFRemoteDataSource = Components.interfaces.nsIRDFRemoteDataSource;
  53. const nsIInternetSearchService = Components.interfaces.nsIInternetSearchService;
  54. const nsIClassInfo = Components.interfaces.nsIClassInfo;
  55.  
  56. function nsSidebar()
  57. {
  58.     const RDF_CONTRACTID = "@mozilla.org/rdf/rdf-service;1";
  59.     const nsIRDFService = Components.interfaces.nsIRDFService;
  60.     
  61.     this.rdf = Components.classes[RDF_CONTRACTID].getService(nsIRDFService);
  62.     this.datasource_uri = getSidebarDatasourceURI(PANELS_RDF_FILE);
  63.     debug('datasource_uri is ' + this.datasource_uri);
  64.     this.resource = 'urn:sidebar:current-panel-list';
  65.     this.datasource = this.rdf.GetDataSource(this.datasource_uri);
  66.  
  67.     const PROMPTSERVICE_CONTRACTID = "@mozilla.org/embedcomp/prompt-service;1";
  68.     const nsIPromptService = Components.interfaces.nsIPromptService;
  69.     this.promptService =
  70.         Components.classes[PROMPTSERVICE_CONTRACTID].getService(nsIPromptService);
  71. }
  72.  
  73. nsSidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
  74.  
  75. nsSidebar.prototype.isPanel =
  76. function (aContentURL)
  77. {
  78.     var container = 
  79.         Components.classes[CONTAINER_CONTRACTID].createInstance(nsIRDFContainer);
  80.  
  81.     container.Init(this.datasource, this.rdf.GetResource(this.resource));
  82.     
  83.     /* Create a resource for the new panel and add it to the list */
  84.     var panel_resource = 
  85.         this.rdf.GetResource("urn:sidebar:3rdparty-panel:" + aContentURL);
  86.  
  87.     return (container.IndexOf(panel_resource) != -1);
  88. }
  89.  
  90. function sidebarURLSecurityCheck(url)
  91. {
  92.     if (url.search(/(^http:|^ftp:|^https:)/) == -1)
  93.         throw "Script attempted to add sidebar panel from illegal source";
  94. }
  95.  
  96. /* decorate prototype to provide ``class'' methods and property accessors */
  97. nsSidebar.prototype.addPanel =
  98. function (aTitle, aContentURL, aCustomizeURL)
  99. {
  100.     debug("addPanel(" + aTitle + ", " + aContentURL + ", " +
  101.           aCustomizeURL + ")");
  102.    
  103.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, false);
  104. }
  105.  
  106. nsSidebar.prototype.addPersistentPanel = 
  107. function(aTitle, aContentURL, aCustomizeURL)
  108. {
  109.     debug("addPersistentPanel(" + aTitle + ", " + aContentURL + ", " +
  110.            aCustomizeURL + ")\n");
  111.  
  112.     return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, true);
  113. }
  114.  
  115. nsSidebar.prototype.addPanelInternal =
  116. function (aTitle, aContentURL, aCustomizeURL, aPersist)
  117. {
  118.     sidebarURLSecurityCheck(aContentURL);
  119.  
  120.     // Create a "container" wrapper around the current panels to
  121.     // manipulate the RDF:Seq more easily.
  122.     var panel_list = this.datasource.GetTarget(this.rdf.GetResource(this.resource), this.rdf.GetResource(nsSidebar.prototype.nc+"panel-list"), true);
  123.     if (panel_list) {
  124.         panel_list.QueryInterface(Components.interfaces.nsIRDFResource);
  125.     } else {
  126.         // Datasource is busted. Start over.
  127.         debug("Sidebar datasource is busted\n");
  128.     }
  129.  
  130.     var container = Components.classes[CONTAINER_CONTRACTID].createInstance(nsIRDFContainer);
  131.     container.Init(this.datasource, panel_list);
  132.  
  133.     /* Create a resource for the new panel and add it to the list */
  134.     var panel_resource = 
  135.         this.rdf.GetResource("urn:sidebar:3rdparty-panel:" + aContentURL);
  136.     var panel_index = container.IndexOf(panel_resource);
  137.     var stringBundle, brandStringBundle, titleMessage, dialogMessage;
  138.     if (panel_index != -1)
  139.     {
  140.         try {
  141.             stringBundle = srGetStrBundle("chrome://communicator/locale/sidebar/sidebar.properties");
  142.             brandStringBundle = srGetStrBundle("chrome://global/locale/brand.properties");
  143.             if (stringBundle) {
  144.                 sidebarName = brandStringBundle.GetStringFromName("sidebarName");
  145.                 titleMessage = stringBundle.GetStringFromName("dupePanelAlertTitle");
  146.                 dialogMessage = stringBundle.GetStringFromName("dupePanelAlertMessage");
  147.                 dialogMessage = dialogMessage.replace(/%url%/, aContentURL);
  148.                 dialogMessage = dialogMessage.replace(/%name%/, sidebarName);
  149.             }
  150.         }
  151.         catch (e) {
  152.             titleMessage = "Sidebar";
  153.             dialogMessage = aContentURL + " already exists in Sidebar.  No string bundle";
  154.         }
  155.           
  156.         this.promptService.alert(null, titleMessage, dialogMessage);
  157.  
  158.         return;
  159.     }
  160.  
  161.     try {
  162.         stringBundle = srGetStrBundle("chrome://communicator/locale/sidebar/sidebar.properties");
  163.         brandStringBundle = srGetStrBundle("chrome://global/locale/brand.properties");
  164.         if (stringBundle) {
  165.             sidebarName = brandStringBundle.GetStringFromName("sidebarName");
  166.             titleMessage = stringBundle.GetStringFromName("addPanelConfirmTitle");
  167.             dialogMessage = stringBundle.GetStringFromName("addPanelConfirmMessage");
  168.             if (aPersist)
  169.             {
  170.                 var warning = stringBundle.GetStringFromName("persistentPanelWarning");
  171.                 dialogMessage += "\n" + warning;
  172.             }
  173.             dialogMessage = dialogMessage.replace(/%title%/, aTitle);
  174.             dialogMessage = dialogMessage.replace(/%url%/, aContentURL);
  175.             dialogMessage = dialogMessage.replace(/#/g, "\n");
  176.             dialogMessage = dialogMessage.replace(/%name%/g, sidebarName);
  177.         }
  178.     }
  179.     catch (e) {
  180.         titleMessage = "Add Tab to Sidebar";
  181.         dialogMessage = "No string bundle.  Add the Tab '" + aTitle + "' to Sidebar?\n\n" + "Source: " + aContentURL;
  182.     }
  183.           
  184.     var rv = this.promptService.confirm(null, titleMessage, dialogMessage);
  185.       
  186.     if (!rv)
  187.         return;
  188.  
  189.     /* Now make some sidebar-ish assertions about it... */
  190.     this.datasource.Assert(panel_resource,
  191.                            this.rdf.GetResource(this.nc + "title"),
  192.                            this.rdf.GetLiteral(aTitle),
  193.                            true);
  194.     this.datasource.Assert(panel_resource,
  195.                            this.rdf.GetResource(this.nc + "content"),
  196.                            this.rdf.GetLiteral(aContentURL),
  197.                            true);
  198.     if (aCustomizeURL)
  199.         this.datasource.Assert(panel_resource,
  200.                                this.rdf.GetResource(this.nc + "customize"),
  201.                                this.rdf.GetLiteral(aCustomizeURL),
  202.                                true);
  203.     var persistValue = aPersist ? "true" : "false";
  204.     this.datasource.Assert(panel_resource,
  205.                            this.rdf.GetResource(this.nc + "persist"),
  206.                            this.rdf.GetLiteral(persistValue),
  207.                            true);
  208.         
  209.     container.AppendElement(panel_resource);
  210.  
  211.     // Use an assertion to pass a "refresh" event to all the sidebars.
  212.     // They use observers to watch for this assertion (in sidebarOverlay.js).
  213.     this.datasource.Assert(this.rdf.GetResource(this.resource),
  214.                            this.rdf.GetResource(this.nc + "refresh"),
  215.                            this.rdf.GetLiteral("true"),
  216.                            true);
  217.     this.datasource.Unassert(this.rdf.GetResource(this.resource),
  218.                              this.rdf.GetResource(this.nc + "refresh"),
  219.                              this.rdf.GetLiteral("true"));
  220.  
  221.     /* Write the modified panels out. */
  222.     this.datasource.QueryInterface(nsIRDFRemoteDataSource).Flush();
  223.  
  224. }
  225.  
  226. /* decorate prototype to provide ``class'' methods and property accessors */
  227. nsSidebar.prototype.addSearchEngine =
  228. function (engineURL, iconURL, suggestedTitle, suggestedCategory)
  229. {
  230.     debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
  231.           suggestedCategory + ", " + suggestedTitle + ")");
  232.  
  233.     try
  234.     {
  235.         // make sure using HTTP (for both engine as well as icon URLs)
  236.  
  237.         if (engineURL.search(/^http:\/\//i) == -1)
  238.         {
  239.             debug ("must use HTTP to fetch search engine file");
  240.             throw Components.results.NS_ERROR_INVALID_ARG;
  241.         }
  242.  
  243.         if (iconURL.search(/^http:\/\//i) == -1)
  244.         {
  245.             debug ("must use HTTP to fetch search icon file");
  246.             throw Components.results.NS_ERROR_INVALID_ARG;
  247.         }
  248.  
  249.         // make sure engineURL refers to a .src file
  250.         if (engineURL.search(/\.src$/i) == -1)
  251.         {
  252.             debug ("engineURL doesn't reference a .src file");
  253.             throw Components.results.NS_ERROR_INVALID_ARG;
  254.         }
  255.  
  256.         // make sure iconURL refers to a .gif/.jpg/.jpeg/.png file
  257.         if (iconURL.search(/\.(gif|jpg|jpeg|png)$/i) == -1)
  258.         {
  259.             debug ("iconURL doesn't reference a supported image file");
  260.             throw Components.results.NS_ERROR_INVALID_ARG;
  261.         }
  262.  
  263.     }
  264.     catch(ex)
  265.     {
  266.         this.promptService.alert(null, "Failed to add the search engine.");
  267.         throw Components.results.NS_ERROR_INVALID_ARG;
  268.     }
  269.  
  270.     var titleMessage, dialogMessage;
  271.     try {
  272.         var stringBundle = srGetStrBundle("chrome://communicator/locale/sidebar/sidebar.properties");
  273.         var brandStringBundle = srGetStrBundle("chrome://global/locale/brand.properties");
  274.         if (stringBundle) {
  275.             sidebarName = brandStringBundle.GetStringFromName("sidebarName");            
  276.             titleMessage = stringBundle.GetStringFromName("addEngineConfirmTitle");
  277.             dialogMessage = stringBundle.GetStringFromName("addEngineConfirmMessage");
  278.             dialogMessage = dialogMessage.replace(/%title%/, suggestedTitle);
  279.             dialogMessage = dialogMessage.replace(/%category%/, suggestedCategory);
  280.             dialogMessage = dialogMessage.replace(/%url%/, engineURL);
  281.             dialogMessage = dialogMessage.replace(/#/g, "\n");
  282.             dialogMessage = dialogMessage.replace(/%name%/, sidebarName);
  283.         }
  284.     }
  285.     catch (e) {
  286.         titleMessage = "Add Search Engine";
  287.         dialogMessage = "Add the following search engine?\n\nName: " + suggestedTitle;
  288.         dialogMessage += "\nSearch Category: " + suggestedCategory;
  289.         dialogMessage += "\nSource: " + engineURL;
  290.     }
  291.           
  292.     var rv = this.promptService.confirm(null, titleMessage, dialogMessage);
  293.       
  294.     if (!rv)
  295.         return;
  296.  
  297.     var internetSearch = Components.classes[NETSEARCH_CONTRACTID].getService();
  298.     if (internetSearch)    
  299.         internetSearch = internetSearch.QueryInterface(nsIInternetSearchService);
  300.     if (internetSearch)
  301.     {
  302.         internetSearch.AddSearchEngine(engineURL, iconURL, suggestedTitle,
  303.                                        suggestedCategory);
  304.     }
  305. }
  306.  
  307. // property of nsIClassInfo
  308. nsSidebar.prototype.flags = nsIClassInfo.DOM_OBJECT;
  309.  
  310. // property of nsIClassInfo
  311. nsSidebar.prototype.classDescription = "Sidebar";
  312.  
  313. // method of nsIClassInfo
  314. nsSidebar.prototype.getInterfaces = function(count) {
  315.     var interfaceList = [nsISidebar, nsIClassInfo];
  316.     count.value = interfaceList.length;
  317.     return interfaceList;
  318. }
  319.  
  320. // method of nsIClassInfo
  321. nsSidebar.prototype.getHelperForLanguage = function(count) {return null;}
  322.  
  323. nsSidebar.prototype.QueryInterface =
  324. function (iid) {
  325.     if (!iid.equals(nsISidebar) && 
  326.         !iid.equals(nsIClassInfo) &&
  327.         !iid.equals(nsISupports))
  328.         throw Components.results.NS_ERROR_NO_INTERFACE;
  329.     return this;
  330. }
  331.  
  332. var sidebarModule = new Object();
  333.  
  334. sidebarModule.registerSelf =
  335. function (compMgr, fileSpec, location, type)
  336. {
  337.     debug("registering (all right -- a JavaScript module!)");
  338.     compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
  339.  
  340.     compMgr.registerFactoryLocation(SIDEBAR_CID, 
  341.                                     "Sidebar JS Component",
  342.                                     SIDEBAR_CONTRACTID, 
  343.                                     fileSpec, 
  344.                                     location,
  345.                                     type);
  346.  
  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.  
  360. sidebarModule.getClassObject =
  361. function (compMgr, cid, iid) {
  362.     if (!cid.equals(SIDEBAR_CID))
  363.         throw Components.results.NS_ERROR_NO_INTERFACE;
  364.     
  365.     if (!iid.equals(Components.interfaces.nsIFactory))
  366.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  367.     
  368.     return sidebarFactory;
  369. }
  370.  
  371. sidebarModule.canUnload =
  372. function(compMgr)
  373. {
  374.     debug("Unloading component.");
  375.     return true;
  376. }
  377.     
  378. /* factory object */
  379. var sidebarFactory = new Object();
  380.  
  381. sidebarFactory.createInstance =
  382. function (outer, iid) {
  383.     debug("CI: " + iid);
  384.     if (outer != null)
  385.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  386.  
  387.     return (new nsSidebar()).QueryInterface(iid);
  388. }
  389.  
  390. /* entrypoint */
  391. function NSGetModule(compMgr, fileSpec) {
  392.     return sidebarModule;
  393. }
  394.  
  395. /* static functions */
  396. if (DEBUG)
  397.     debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
  398. else
  399.     debug = function (s) {}
  400.  
  401. function getSidebarDatasourceURI(panels_file_id)
  402. {
  403.     try 
  404.     {
  405.         /* use the fileLocator to look in the profile directory 
  406.          * to find 'panels.rdf', which is the
  407.          * database of the user's currently selected panels. */
  408.         var directory_service = Components.classes[DIR_SERV_CONTRACTID].getService(Components.interfaces.nsIProperties);
  409.  
  410.         /* if <profile>/panels.rdf doesn't exist, get will copy
  411.          *bin/defaults/profile/panels.rdf to <profile>/panels.rdf */
  412.         var sidebar_file = directory_service.get(panels_file_id, Components.interfaces.nsIFile);
  413.  
  414.         if (!sidebar_file.exists())
  415.         {
  416.             /* this should not happen, as GetFileLocation() should copy
  417.              * defaults/panels.rdf to the users profile directory */
  418.             debug("sidebar file does not exist");
  419.             return null;
  420.         }
  421.  
  422.         var io_service = Components.classes[IO_SERV_CONTRACTID].getService(Components.interfaces.nsIIOService);
  423.         var file_handler = io_service.getProtocolHandler("file").QueryInterface(Components.interfaces.nsIFileProtocolHandler);
  424.         var sidebar_uri = file_handler.getURLSpecFromFile(sidebar_file);
  425.         debug("sidebar uri is " + sidebar_uri);
  426.         return sidebar_uri;
  427.     }
  428.     catch (ex)
  429.     {
  430.         /* this should not happen */
  431.         debug("caught " + ex + " getting sidebar datasource uri");
  432.         return null;
  433.     }
  434. }
  435.  
  436.  
  437. var strBundleService = null;
  438. function srGetStrBundle(path)
  439. {
  440.    var strBundle = null;
  441.    if (!strBundleService) {
  442.        try {
  443.           strBundleService =
  444.           Components.classes["@mozilla.org/intl/stringbundle;1"].getService(); 
  445.           strBundleService = 
  446.           strBundleService.QueryInterface(Components.interfaces.nsIStringBundleService);
  447.        } catch (ex) {
  448.           dump("\n--** strBundleService failed: " + ex + "\n");
  449.           return null;
  450.       }
  451.    }
  452.    strBundle = strBundleService.createBundle(path); 
  453.    if (!strBundle) {
  454.        dump("\n--** strBundle createInstance failed **--\n");
  455.    }
  456.    return strBundle;
  457. }
  458.  
  459.  
  460.