1 /* -*- Mode: javascript; tab-width: 20; 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 Oracle Corporation code.
16 *
17 * The Initial Developer of the Original Code is
18 * Oracle Corporation
19 * Portions created by the Initial Developer are Copyright (C) 2004
20 * the Initial Developer. All Rights Reserved.
21 *
22 * Contributor(s):
23 * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
24 * Mike Shaver <shaver@off.net>
25 *
26 * Alternatively, the contents of this file may be used under the terms of
27 * either the GNU General Public License Version 2 or later (the "GPL"), or
28 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
37 *
38 * ***** END LICENSE BLOCK ***** */
39
40 const componentData =
41 [
42 {cid: Components.ID("{1e3e33dc-445a-49de-b2b6-15b2a050bb9d}"),
43 contractid: "@mozilla.org/calendar/import;1?type=ics",
44 script: "calIcsImportExport.js",
45 constructor: "calIcsImporter",
46 category: "cal-importers",
47 categoryEntry: "cal-ics-importer",
48 service: false},
49 {cid: Components.ID("{a6a524ce-adff-4a0f-bb7d-d1aaad4adc60}"),
50 contractid: "@mozilla.org/calendar/export;1?type=ics",
51 constructor: "calIcsExporter",
52 category: "cal-exporters",
53 categoryEntry: "cal-ics-exporter",
54 service: false},
55
56 {cid: Components.ID("{72d9ab35-9b1b-442a-8cd0-ae49f00b159b}"),
57 contractid: "@mozilla.org/calendar/export;1?type=htmllist",
58 script: "calHtmlExport.js",
59 constructor: "calHtmlExporter",
60 category: "cal-exporters",
61 categoryEntry: "cal-html-list-exporter",
62 service: false},
63
64 {cid: Components.ID("{9ae04413-fee3-45b9-8bbb-1eb39a4cbd1b}"),
65 contractid: "@mozilla.org/calendar/printformatter;1?type=list",
66 script: "calListFormatter.js",
67 constructor: "calListFormatter",
68 category: "cal-print-formatters",
69 categoryEntry: "cal-list-printformatter",
70 service: false},
71 {cid: Components.ID("{f42d5132-92c4-487b-b5c8-38bf292d74c1}"),
72 contractid: "@mozilla.org/calendar/printformatter;1?type=monthgrid",
73 script: "calMonthGridPrinter.js",
74 constructor: "calMonthPrinter",
75 category: "cal-print-formatters",
76 categoryEntry: "cal-month-printer",
77 service: false},
78 {cid: Components.ID("{2d6ec97b-9109-4b92-89c5-d4b4806619ce}"),
79 contractid: "@mozilla.org/calendar/printformatter;1?type=weekplan",
80 script: "calWeekPrinter.js",
81 constructor: "calWeekPrinter",
82 category: "cal-print-formatters",
83 categoryEntry: "cal-week-planner-printer",
84 service: false},
85
86 {cid: Components.ID("{64a5d17a-0497-48c5-b54f-72b15c9e9a14}"),
87 contractid: "@mozilla.org/calendar/import;1?type=csv",
88 script: "calOutlookCSVImportExport.js",
89 constructor: "calOutlookCSVImporter",
90 category: "cal-importers",
91 categoryEntry: "cal-outlookcsv-importer",
92 service: false},
93 {cid: Components.ID("{48e6d3a6-b41b-4052-9ed2-40b27800bd4b}"),
94 contractid: "@mozilla.org/calendar/export;1?type=csv",
95 constructor: "calOutlookCSVExporter",
96 category: "cal-exporters",
97 categoryEntry: "cal-outlookcsv-exporter",
98 service: false},
99
100 {cid: null,
101 contractid: null,
102 script: "calUtils.js",
103 constructor: null,
104 category: null,
105 categoryEntry: null,
106 service: false},
107
108 ];
109
110 var calImportExportModule = {
111 mScriptsLoaded: false,
loadScripts
112 loadScripts: function () {
113 if (this.mScriptsLoaded)
114 return;
115
116 const jssslContractID = "@mozilla.org/moz/jssubscript-loader;1";
117 const jssslIID = Components.interfaces.mozIJSSubScriptLoader;
118
119 const dirsvcContractID = "@mozilla.org/file/directory_service;1";
120 const propsIID = Components.interfaces.nsIProperties;
121
122 const iosvcContractID = "@mozilla.org/network/io-service;1";
123 const iosvcIID = Components.interfaces.nsIIOService;
124
125 var loader = Components.classes[jssslContractID].getService(jssslIID);
126 var dirsvc = Components.classes[dirsvcContractID].getService(propsIID);
127 var iosvc = Components.classes[iosvcContractID].getService(iosvcIID);
128
129 // Note that unintuitively, __LOCATION__.parent == .
130 // We expect to find the subscripts in ./../js
131 var appdir = __LOCATION__.parent.parent;
132 appdir.append("js");
133
134 for (var i = 0; i < componentData.length; i++) {
135 var scriptName = componentData[i].script;
136 if (!scriptName)
137 continue;
138
139 var f = appdir.clone();
140 f.append(scriptName);
141
142 try {
143 var fileurl = iosvc.newFileURI(f);
144 loader.loadSubScript(fileurl.spec, null);
145 } catch (e) {
146 dump("Error while loading " + fileurl.spec + "\n");
147 throw e;
148 }
149 }
150
151 this.mScriptsLoaded = true;
152 },
153
registerSelf
154 registerSelf: function (compMgr, fileSpec, location, type) {
155 compMgr = compMgr.QueryInterface(Components.interfaces.nsIComponentRegistrar);
156
157 var catman = Components.classes["@mozilla.org/categorymanager;1"]
158 .getService(Components.interfaces.nsICategoryManager);
159 for (var i = 0; i < componentData.length; i++) {
160 var comp = componentData[i];
161 if (!comp.cid)
162 continue;
163 compMgr.registerFactoryLocation(comp.cid,
164 "",
165 comp.contractid,
166 fileSpec,
167 location,
168 type);
169
170 if (comp.category) {
171 var contractid;
172 if (comp.service)
173 contractid = "service," + comp.contractid;
174 else
175 contractid = comp.contractid;
176 catman.addCategoryEntry(comp.category, comp.categoryEntry,
177 contractid, true, true);
178 }
179 }
180 },
181
makeFactoryFor
182 makeFactoryFor: function(constructor) {
183 var factory = {
184 QueryInterface: function (aIID) {
185 if (!aIID.equals(Components.interfaces.nsISupports) &&
186 !aIID.equals(Components.interfaces.nsIFactory))
187 throw Components.results.NS_ERROR_NO_INTERFACE;
188 return this;
189 },
190
191 createInstance: function (outer, iid) {
192 if (outer != null)
193 throw Components.results.NS_ERROR_NO_AGGREGATION;
194 return (new constructor()).QueryInterface(iid);
195 }
196 };
197
198 return factory;
199 },
200
getClassObject
201 getClassObject: function (compMgr, cid, iid) {
202 if (!iid.equals(Components.interfaces.nsIFactory))
203 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
204
205 if (!this.mScriptsLoaded)
206 this.loadScripts();
207
208 for (var i = 0; i < componentData.length; i++) {
209 if (cid.equals(componentData[i].cid)) {
210 if (componentData[i].onComponentLoad) {
211 eval(componentData[i].onComponentLoad);
212 }
213 // eval to get usual scope-walking
214 return this.makeFactoryFor(eval(componentData[i].constructor));
215 }
216 }
217
218 throw Components.results.NS_ERROR_NO_INTERFACE;
219 },
220
canUnload
221 canUnload: function(compMgr) {
222 return true;
223 }
224 };
225
NSGetModule
226 function NSGetModule(compMgr, fileSpec) {
227 return calImportExportModule;
228 }