home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / components / nsSidebar.js < prev    next >
Text File  |  2001-02-14  |  16KB  |  439 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 STD_URL_CONTRACTID   = "@mozilla.org/network/standard-url;1"
  45. const NETSEARCH_CONTRACTID = "@mozilla.org/rdf/datasource;1?name=internetsearch"
  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 nsISecurityCheckedComponent = Components.interfaces.nsISecurityCheckedComponent;
  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.  
  68. nsSidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
  69.  
  70. nsSidebar.prototype.setWindow =
  71. function (aWindow)
  72. {    
  73.     this.window = aWindow;    
  74. }
  75.  
  76. nsSidebar.prototype.isPanel =
  77. function (aContentURL)
  78. {
  79.     var container = 
  80.         Components.classes[CONTAINER_CONTRACTID].createInstance(nsIRDFContainer);
  81.  
  82.     container.Init(this.datasource, this.rdf.GetResource(this.resource));
  83.     
  84.     /* Create a resource for the new panel and add it to the list */
  85.     var panel_resource = 
  86.         this.rdf.GetResource("urn:sidebar:3rdparty-panel:" + aContentURL);
  87.  
  88.     return (container.IndexOf(panel_resource) != -1);
  89. }
  90.  
  91. function sidebarURLSecurityCheck(url)
  92. {
  93.     if (url.search(/(^http:|^ftp:|^https:)/) == -1)
  94.         throw "Script attempted to add sidebar panel from illegal source";
  95. }
  96.  
  97. /* decorate prototype to provide ``class'' methods and property accessors */
  98. nsSidebar.prototype.addPanel =
  99. function (aTitle, aContentURL, aCustomizeURL)
  100. {
  101.     debug("addPanel(" + aTitle + ", " + aContentURL + ", " +
  102.           aCustomizeURL + ")");
  103.  
  104.     if (!this.window)
  105.     {
  106.         debug ("no window object set, bailing out.");
  107.         throw Components.results.NS_ERROR_NOT_INITIALIZED;
  108.     }
  109.  
  110.     sidebarURLSecurityCheck(aContentURL);
  111.  
  112.     // Create a "container" wrapper around the current panels to
  113.     // manipulate the RDF:Seq more easily.
  114.     var panel_list = this.datasource.GetTarget(this.rdf.GetResource(this.resource), this.rdf.GetResource(nsSidebar.prototype.nc+"panel-list"), true);
  115.     if (panel_list) {
  116.         panel_list.QueryInterface(Components.interfaces.nsIRDFResource);
  117.     } else {
  118.         // Datasource is busted. Start over.
  119.         debug("Sidebar datasource is busted\n");
  120.   }
  121.  
  122.     var container = Components.classes[CONTAINER_CONTRACTID].createInstance(nsIRDFContainer);
  123.     container.Init(this.datasource, panel_list);
  124.  
  125.     /* Create a resource for the new panel and add it to the list */
  126.     var panel_resource = 
  127.         this.rdf.GetResource("urn:sidebar:3rdparty-panel:" + aContentURL);
  128.     var panel_index = container.IndexOf(panel_resource);
  129.     if (panel_index != -1)
  130.     {
  131.         var titleMessage, dialogMessage;
  132.         try {
  133.             var stringBundle = getStringBundle("chrome://communicator/locale/sidebar/sidebar.properties");
  134.             if (stringBundle) {
  135.                 titleMessage = stringBundle.GetStringFromName("dupePanelAlertTitle");
  136.                 dialogMessage = stringBundle.GetStringFromName("dupePanelAlertMessage");
  137.                 dialogMessage = dialogMessage.replace(/%url%/, aContentURL);
  138.             }
  139.         }
  140.         catch (e) {
  141.             titleMessage = "My Sidebar";
  142.             dialogMessage = aContentURL + " already exists in My Sidebar.";
  143.         }
  144.           
  145.         var cDlgService = Components.classes["@mozilla.org/appshell/commonDialogs;1"].getService();
  146.         if (cDlgService) 
  147.             cDlgService = cDlgService.QueryInterface(Components.interfaces.nsICommonDialogs);
  148.         cDlgService.Alert(this.window, titleMessage, dialogMessage);
  149.  
  150.         return;
  151.     }
  152.     
  153.     var titleMessage, dialogMessage;
  154.     try {
  155.         var stringBundle = getStringBundle("chrome://communicator/locale/sidebar/sidebar.properties");
  156.         if (stringBundle) {
  157.             titleMessage = stringBundle.GetStringFromName("addPanelConfirmTitle");
  158.             dialogMessage = stringBundle.GetStringFromName("addPanelConfirmMessage");
  159.             dialogMessage = dialogMessage.replace(/%title%/, aTitle);
  160.             dialogMessage = dialogMessage.replace(/%url%/, aContentURL);
  161.             dialogMessage = dialogMessage.replace(/#/g, "\n");
  162.         }
  163.     }
  164.     catch (e) {
  165.         titleMessage = "Add Tab to My Sidebar";
  166.         dialogMessage = "Add the Tab '" + aTitle + "' to My Sidebar?\n\n" + "Source: " + aContentURL;
  167.     }
  168.           
  169.     var cDlgService = Components.classes["@mozilla.org/appshell/commonDialogs;1"].getService();
  170.     if (cDlgService) 
  171.         cDlgService = cDlgService.QueryInterface(Components.interfaces.nsICommonDialogs);
  172.     var rv = cDlgService.Confirm(this.window, titleMessage, dialogMessage);
  173.       
  174.     if (!rv)
  175.         return;
  176.  
  177.     /* Now make some sidebar-ish assertions about it... */
  178.     this.datasource.Assert(panel_resource,
  179.                            this.rdf.GetResource(this.nc + "title"),
  180.                            this.rdf.GetLiteral(aTitle),
  181.                            true);
  182.     this.datasource.Assert(panel_resource,
  183.                            this.rdf.GetResource(this.nc + "content"),
  184.                            this.rdf.GetLiteral(aContentURL),
  185.                            true);
  186.     if (aCustomizeURL)
  187.         this.datasource.Assert(panel_resource,
  188.                                this.rdf.GetResource(this.nc + "customize"),
  189.                                this.rdf.GetLiteral(aCustomizeURL),
  190.                                true);
  191.         
  192.     container.AppendElement(panel_resource);
  193.  
  194.     // Use an assertion to pass a "refresh" event to all the sidebars.
  195.     // They use observers to watch for this assertion (in sidebarOverlay.js).
  196.     this.datasource.Assert(this.rdf.GetResource(this.resource),
  197.                            this.rdf.GetResource(this.nc + "refresh"),
  198.                            this.rdf.GetLiteral("true"),
  199.                            true);
  200.     this.datasource.Unassert(this.rdf.GetResource(this.resource),
  201.                              this.rdf.GetResource(this.nc + "refresh"),
  202.                              this.rdf.GetLiteral("true"));
  203.  
  204.     /* Write the modified panels out. */
  205.     this.datasource.QueryInterface(nsIRDFRemoteDataSource).Flush();
  206.  
  207. }
  208.  
  209. /* decorate prototype to provide ``class'' methods and property accessors */
  210. nsSidebar.prototype.addSearchEngine =
  211. function (engineURL, iconURL, suggestedTitle, suggestedCategory)
  212. {
  213.     debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
  214.           suggestedCategory + ", " + suggestedTitle + ")");
  215.  
  216.     if (!this.window)
  217.     {
  218.         debug ("no window object set, bailing out.");
  219.         throw Components.results.NS_ERROR_NOT_INITIALIZED;
  220.     }
  221.  
  222.     try
  223.     {
  224.         // make sure using HTTP (for both engine as well as icon URLs)
  225.  
  226.         if (engineURL.search(/^http:\/\//i) == -1)
  227.         {
  228.             debug ("must use HTTP to fetch search engine file");
  229.             throw Components.results.NS_ERROR_INVALID_ARG;
  230.         }
  231.  
  232.         if (iconURL.search(/^http:\/\//i) == -1)
  233.         {
  234.             debug ("must use HTTP to fetch search icon file");
  235.             throw Components.results.NS_ERROR_INVALID_ARG;
  236.         }
  237.  
  238.         // make sure engineURL refers to a .src file
  239.         if (engineURL.search(/\.src$/i) == -1)
  240.         {
  241.             debug ("engineURL doesn't reference a .src file");
  242.             throw Components.results.NS_ERROR_INVALID_ARG;
  243.         }
  244.  
  245.         // make sure iconURL refers to a .gif/.jpg/.jpeg/.png file
  246.         if (iconURL.search(/\.(gif|jpg|jpeg|png)$/i) == -1)
  247.         {
  248.             debug ("iconURL doesn't reference a supported image file");
  249.             throw Components.results.NS_ERROR_INVALID_ARG;
  250.         }
  251.  
  252.     }
  253.     catch(ex)
  254.     {
  255.         this.window.alert("Failed to add the search engine.\n");
  256.         throw Components.results.NS_ERROR_INVALID_ARG;
  257.     }
  258.  
  259.     var titleMessage, dialogMessage;
  260.     try {
  261.         var stringBundle = getStringBundle("chrome://communicator/locale/sidebar/sidebar.properties");
  262.         if (stringBundle) {
  263.             titleMessage = stringBundle.GetStringFromName("addEngineConfirmTitle");
  264.             dialogMessage = stringBundle.GetStringFromName("addEngineConfirmMessage");
  265.             dialogMessage = dialogMessage.replace(/%title%/, suggestedTitle);
  266.             dialogMessage = dialogMessage.replace(/%category%/, suggestedCategory);
  267.             dialogMessage = dialogMessage.replace(/%url%/, engineURL);
  268.             dialogMessage = dialogMessage.replace(/#/g, "\n");
  269.         }
  270.     }
  271.     catch (e) {
  272.         titleMessage = "Add Search Engine";
  273.         dialogMessage = "Add the following search engine?\n\nName: " + suggestedTitle;
  274.         dialogMessage += "\nSearch Category: " + suggestedCategory;
  275.         dialogMessage += "\nSource: " + engineURL;
  276.     }
  277.           
  278.     var cDlgService = Components.classes["@mozilla.org/appshell/commonDialogs;1"].getService();
  279.     if (cDlgService) 
  280.         cDlgService = cDlgService.QueryInterface(Components.interfaces.nsICommonDialogs);
  281.     var rv = cDlgService.Confirm(this.window, titleMessage, dialogMessage);
  282.       
  283.     if (!rv)
  284.         return;
  285.  
  286.     var internetSearch = Components.classes[NETSEARCH_CONTRACTID].getService();
  287.     if (internetSearch)    
  288.         internetSearch = internetSearch.QueryInterface(nsIInternetSearchService);
  289.     if (internetSearch)
  290.     {
  291.         internetSearch.AddSearchEngine(engineURL, iconURL, suggestedTitle,
  292.                                        suggestedCategory);
  293.     }
  294. }
  295.  
  296. // method of nsISecurityCheckedComponent
  297. nsSidebar.prototype.canCreateWrapper =
  298. function (iid) {
  299.     return "NoAccess";
  300. }
  301.  
  302. // method of nsISecurityCheckedComponent
  303. nsSidebar.prototype.canCallMethod =
  304. function (iid, methodName) {
  305.     if (iid.equals(nsISidebar) &&
  306.         (methodName == "setWindow" ||
  307.          methodName == "addPanel" ||
  308.          methodName == "addSearchEngine")) {
  309.         return "AllAccess";
  310.     } else {
  311.         return "NoAccess";
  312.     }
  313. }
  314.  
  315. // method of nsISecurityCheckedComponent
  316. nsSidebar.prototype.canGetProperty =
  317. function (iid, propertyName) {
  318.     return "NoAccess";
  319. }
  320.  
  321. // method of nsISecurityCheckedComponent
  322. nsSidebar.prototype.canSetProperty =
  323. function (iid, propertyName) {
  324.     return "NoAccess";
  325. }
  326.  
  327. nsSidebar.prototype.QueryInterface =
  328. function (iid) {
  329.     if (!iid.equals(nsISidebar) && 
  330.         !iid.equals(nsISecurityCheckedComponent) &&
  331.         !iid.equals(nsISupports))
  332.         throw Components.results.NS_ERROR_NO_INTERFACE;
  333.     return this;
  334. }
  335.  
  336. var sidebarModule = new Object();
  337.  
  338. sidebarModule.registerSelf =
  339. function (compMgr, fileSpec, location, type)
  340. {
  341.     debug("registering (all right -- a JavaScript module!)");
  342.     compMgr.registerComponentWithType(SIDEBAR_CID, "Sidebar JS Component",
  343.                                       SIDEBAR_CONTRACTID, fileSpec, location,
  344.                                       true, true, type);
  345. }
  346.  
  347. sidebarModule.getClassObject =
  348. function (compMgr, cid, iid) {
  349.     if (!cid.equals(SIDEBAR_CID))
  350.         throw Components.results.NS_ERROR_NO_INTERFACE;
  351.     
  352.     if (!iid.equals(Components.interfaces.nsIFactory))
  353.         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
  354.     
  355.     return sidebarFactory;
  356. }
  357.  
  358. sidebarModule.canUnload =
  359. function(compMgr)
  360. {
  361.     debug("Unloading component.");
  362.     return true;
  363. }
  364.     
  365. /* factory object */
  366. sidebarFactory = new Object();
  367.  
  368. sidebarFactory.createInstance =
  369. function (outer, iid) {
  370.     debug("CI: " + iid);
  371.     if (outer != null)
  372.         throw Components.results.NS_ERROR_NO_AGGREGATION;
  373.  
  374.     return (new nsSidebar()).QueryInterface(iid);
  375. }
  376.  
  377. /* entrypoint */
  378. function NSGetModule(compMgr, fileSpec) {
  379.     return sidebarModule;
  380. }
  381.  
  382. /* static functions */
  383. if (DEBUG)
  384.     debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
  385. else
  386.     debug = function (s) {}
  387.  
  388. function getSidebarDatasourceURI(panels_file_id)
  389. {
  390.     try 
  391.     {
  392.         /* use the fileLocator to look in the profile directory 
  393.          * to find 'panels.rdf', which is the
  394.          * database of the user's currently selected panels. */
  395.         var directory_service = Components.classes[DIR_SERV_CONTRACTID].getService();
  396.         if (directory_service)
  397.             directory_service = directory_service.QueryInterface(Components.interfaces.nsIProperties);
  398.  
  399.         /* if <profile>/panels.rdf doesn't exist, get will copy
  400.          *bin/defaults/profile/panels.rdf to <profile>/panels.rdf */
  401.         var sidebar_file = directory_service.get(panels_file_id, Components.interfaces.nsIFile);
  402.  
  403.         if (!sidebar_file.exists())
  404.         {
  405.             /* this should not happen, as GetFileLocation() should copy
  406.              * defaults/panels.rdf to the users profile directory */
  407.             debug("sidebar file does not exist");
  408.             return null;
  409.         }
  410.  
  411.         var file_url = Components.classes[STD_URL_CONTRACTID].createInstance(Components.interfaces.nsIFileURL);
  412.         file_url.file = sidebar_file;
  413.  
  414.         debug("sidebar uri is " + file_url.spec);
  415.         return file_url.spec;
  416.     }
  417.     catch (ex)
  418.     {
  419.         /* this should not happen */
  420.         debug("caught " + ex + " getting sidebar datasource uri");
  421.         return null;
  422.     }
  423. }
  424.  
  425. function getStringBundle(aURL) 
  426. {
  427.   var stringBundleService = Components.classes["@mozilla.org/intl/stringbundle;1"].getService();
  428.   stringBundleService = stringBundleService.QueryInterface(Components.interfaces.nsIStringBundleService);
  429.   var appLocale;
  430.   var localeService = Components.classes["@mozilla.org/intl/nslocaleservice;1"].getService();
  431.   if (localeService)
  432.     localeService = localeService.QueryInterface(Components.interfaces.nsILocaleService);
  433.   if (localeService)
  434.     appLocale = localeService.GetApplicationLocale();
  435.   var stringBundle = stringBundleService.CreateBundle(aURL, appLocale);
  436.   if (stringBundle)
  437.     return stringBundle.QueryInterface(Components.interfaces.nsIStringBundle);
  438. }
  439.