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/
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.
12
* The Original Code is mozilla.org code.
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
19
* Contributor(s): Stephen Lamm <slamm@netscape.com>
20
* Robert John Churchill <rjc@netscape.com>
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++).
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.)
37
const DEBUG = false; /* set to false to suppress debug messages */
38
const PANELS_RDF_FILE = "UPnls"; /* directory services property to find panels.rdf */
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;
58
const RDF_CONTRACTID = "@mozilla.org/rdf/rdf-service;1";
59
const nsIRDFService = Components.interfaces.nsIRDFService;
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);
67
const PROMPTSERVICE_CONTRACTID = "@mozilla.org/embedcomp/prompt-service;1";
68
const nsIPromptService = Components.interfaces.nsIPromptService;
70
Components.classes[PROMPTSERVICE_CONTRACTID].getService(nsIPromptService);
73
nsSidebar.prototype.nc = "http://home.netscape.com/NC-rdf#";
75
nsSidebar.prototype.isPanel =
76
function (aContentURL)
79
Components.classes[CONTAINER_CONTRACTID].createInstance(nsIRDFContainer);
81
container.Init(this.datasource, this.rdf.GetResource(this.resource));
83
/* Create a resource for the new panel and add it to the list */
85
this.rdf.GetResource("urn:sidebar:3rdparty-panel:" + aContentURL);
87
return (container.IndexOf(panel_resource) != -1);
90
function sidebarURLSecurityCheck(url)
92
if (url.search(/(^http:|^ftp:|^https:)/) == -1)
93
throw "Script attempted to add sidebar panel from illegal source";
96
/* decorate prototype to provide ``class'' methods and property accessors */
97
nsSidebar.prototype.addPanel =
98
function (aTitle, aContentURL, aCustomizeURL)
100
debug("addPanel(" + aTitle + ", " + aContentURL + ", " +
101
aCustomizeURL + ")");
103
return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, false);
106
nsSidebar.prototype.addPersistentPanel =
107
function(aTitle, aContentURL, aCustomizeURL)
109
debug("addPersistentPanel(" + aTitle + ", " + aContentURL + ", " +
110
aCustomizeURL + ")\n");
112
return this.addPanelInternal(aTitle, aContentURL, aCustomizeURL, true);
115
nsSidebar.prototype.addPanelInternal =
116
function (aTitle, aContentURL, aCustomizeURL, aPersist)
118
sidebarURLSecurityCheck(aContentURL);
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);
124
panel_list.QueryInterface(Components.interfaces.nsIRDFResource);
126
// Datasource is busted. Start over.
127
debug("Sidebar datasource is busted\n");
130
var container = Components.classes[CONTAINER_CONTRACTID].createInstance(nsIRDFContainer);
131
container.Init(this.datasource, panel_list);
133
/* Create a resource for the new panel and add it to the list */
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)
141
stringBundle = srGetStrBundle("chrome://communicator/locale/sidebar/sidebar.properties");
142
brandStringBundle = srGetStrBundle("chrome://global/locale/brand.properties");
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);
152
titleMessage = "Sidebar";
153
dialogMessage = aContentURL + " already exists in Sidebar. No string bundle";
156
this.promptService.alert(null, titleMessage, dialogMessage);
162
stringBundle = srGetStrBundle("chrome://communicator/locale/sidebar/sidebar.properties");
163
brandStringBundle = srGetStrBundle("chrome://global/locale/brand.properties");
165
sidebarName = brandStringBundle.GetStringFromName("sidebarName");
166
titleMessage = stringBundle.GetStringFromName("addPanelConfirmTitle");
167
dialogMessage = stringBundle.GetStringFromName("addPanelConfirmMessage");
170
var warning = stringBundle.GetStringFromName("persistentPanelWarning");
171
dialogMessage += "\n" + warning;
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);
180
titleMessage = "Add Tab to Sidebar";
181
dialogMessage = "No string bundle. Add the Tab '" + aTitle + "' to Sidebar?\n\n" + "Source: " + aContentURL;
184
var rv = this.promptService.confirm(null, titleMessage, dialogMessage);
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),
194
this.datasource.Assert(panel_resource,
195
this.rdf.GetResource(this.nc + "content"),
196
this.rdf.GetLiteral(aContentURL),
199
this.datasource.Assert(panel_resource,
200
this.rdf.GetResource(this.nc + "customize"),
201
this.rdf.GetLiteral(aCustomizeURL),
203
var persistValue = aPersist ? "true" : "false";
204
this.datasource.Assert(panel_resource,
205
this.rdf.GetResource(this.nc + "persist"),
206
this.rdf.GetLiteral(persistValue),
209
container.AppendElement(panel_resource);
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"),
217
this.datasource.Unassert(this.rdf.GetResource(this.resource),
218
this.rdf.GetResource(this.nc + "refresh"),
219
this.rdf.GetLiteral("true"));
221
/* Write the modified panels out. */
222
this.datasource.QueryInterface(nsIRDFRemoteDataSource).Flush();
226
/* decorate prototype to provide ``class'' methods and property accessors */
227
nsSidebar.prototype.addSearchEngine =
228
function (engineURL, iconURL, suggestedTitle, suggestedCategory)
230
debug("addSearchEngine(" + engineURL + ", " + iconURL + ", " +
231
suggestedCategory + ", " + suggestedTitle + ")");
235
// make sure using HTTP (for both engine as well as icon URLs)
237
if (engineURL.search(/^http:\/\//i) == -1)
239
debug ("must use HTTP to fetch search engine file");
240
throw Components.results.NS_ERROR_INVALID_ARG;
243
if (iconURL.search(/^http:\/\//i) == -1)
245
debug ("must use HTTP to fetch search icon file");
246
throw Components.results.NS_ERROR_INVALID_ARG;
249
// make sure engineURL refers to a .src file
250
if (engineURL.search(/\.src$/i) == -1)
252
debug ("engineURL doesn't reference a .src file");
253
throw Components.results.NS_ERROR_INVALID_ARG;
256
// make sure iconURL refers to a .gif/.jpg/.jpeg/.png file
257
if (iconURL.search(/\.(gif|jpg|jpeg|png)$/i) == -1)
259
debug ("iconURL doesn't reference a supported image file");
260
throw Components.results.NS_ERROR_INVALID_ARG;
266
this.promptService.alert(null, "Failed to add the search engine.");
267
throw Components.results.NS_ERROR_INVALID_ARG;
270
var titleMessage, dialogMessage;
272
var stringBundle = srGetStrBundle("chrome://communicator/locale/sidebar/sidebar.properties");
273
var brandStringBundle = srGetStrBundle("chrome://global/locale/brand.properties");
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);
286
titleMessage = "Add Search Engine";
287
dialogMessage = "Add the following search engine?\n\nName: " + suggestedTitle;
288
dialogMessage += "\nSearch Category: " + suggestedCategory;
289
dialogMessage += "\nSource: " + engineURL;
292
var rv = this.promptService.confirm(null, titleMessage, dialogMessage);
297
var internetSearch = Components.classes[NETSEARCH_CONTRACTID].getService();
299
internetSearch = internetSearch.QueryInterface(nsIInternetSearchService);
302
internetSearch.AddSearchEngine(engineURL, iconURL, suggestedTitle,
307
// property of nsIClassInfo
308
nsSidebar.prototype.flags = nsIClassInfo.DOM_OBJECT;
310
// property of nsIClassInfo
311
nsSidebar.prototype.classDescription = "Sidebar";
313
// method of nsIClassInfo
314
nsSidebar.prototype.getInterfaces = function(count) {
315
var interfaceList = [nsISidebar, nsIClassInfo];
316
count.value = interfaceList.length;
317
return interfaceList;
320
// method of nsIClassInfo
321
nsSidebar.prototype.getHelperForLanguage = function(count) {return null;}
323
nsSidebar.prototype.QueryInterface =
325
if (!iid.equals(nsISidebar) &&
326
!iid.equals(nsIClassInfo) &&
327
!iid.equals(nsISupports))
328
throw Components.results.NS_ERROR_NO_INTERFACE;
332
var sidebarModule = new Object();
334
sidebarModule.registerSelf =
335
function (compMgr, fileSpec, location, type)
337
debug("registering (all right -- a JavaScript module!)");
338
compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
340
compMgr.registerFactoryLocation(SIDEBAR_CID,
341
"Sidebar JS Component",
347
const CATMAN_CONTRACTID = "@mozilla.org/categorymanager;1";
348
const nsICategoryManager = Components.interfaces.nsICategoryManager;
349
var catman = Components.classes[CATMAN_CONTRACTID].
350
getService(nsICategoryManager);
352
const JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY = "JavaScript global property";
353
catman.addCategoryEntry(JAVASCRIPT_GLOBAL_PROPERTY_CATEGORY,
360
sidebarModule.getClassObject =
361
function (compMgr, cid, iid) {
362
if (!cid.equals(SIDEBAR_CID))
363
throw Components.results.NS_ERROR_NO_INTERFACE;
365
if (!iid.equals(Components.interfaces.nsIFactory))
366
throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
368
return sidebarFactory;
371
sidebarModule.canUnload =
374
debug("Unloading component.");
379
var sidebarFactory = new Object();
381
sidebarFactory.createInstance =
382
function (outer, iid) {
385
throw Components.results.NS_ERROR_NO_AGGREGATION;
387
return (new nsSidebar()).QueryInterface(iid);
391
function NSGetModule(compMgr, fileSpec) {
392
return sidebarModule;
395
/* static functions */
397
debug = function (s) { dump("-*- sidebar component: " + s + "\n"); }
399
debug = function (s) {}
401
function getSidebarDatasourceURI(panels_file_id)
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);
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);
414
if (!sidebar_file.exists())
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");
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);
430
/* this should not happen */
431
debug("caught " + ex + " getting sidebar datasource uri");
437
var strBundleService = null;
438
function srGetStrBundle(path)
440
var strBundle = null;
441
if (!strBundleService) {
444
Components.classes["@mozilla.org/intl/stringbundle;1"].getService();
446
strBundleService.QueryInterface(Components.interfaces.nsIStringBundleService);
448
dump("\n--** strBundleService failed: " + ex + "\n");
452
strBundle = strBundleService.createBundle(path);
454
dump("\n--** strBundle createInstance failed **--\n");