set emacs indentation level
[lightning-exchange-provider:ianmartins-lightning-exchange-provider.git] / components / calExchangeModule.js
1 /* -*- espresso-indent-level: 8; -*- */
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 Microsoft Exchange Calendar Provider code.
16  *
17  * The Initial Developer of the Original Code is
18  *   Andrea Bittau <a.bittau@cs.ucl.ac.uk>, University College London
19  * Portions created by the Initial Developer are Copyright (C) 2009
20  * the Initial Developer. All Rights Reserved.
21  *
22  * Alternatively, the contents of this file may be used under the terms of
23  * either the GNU General Public License Version 2 or later (the "GPL"), or
24  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
25  * in which case the provisions of the GPL or the LGPL are applicable instead
26  * of those above. If you wish to allow use of your version of this file only
27  * under the terms of either the GPL or the LGPL, and not to allow others to
28  * use your version of this file under the terms of the MPL, indicate your
29  * decision by deleting the provisions above and replace them with the notice
30  * and other provisions required by the GPL or the LGPL. If you do not delete
31  * the provisions above, a recipient may use your version of this file under
32  * the terms of any one of the MPL, the GPL or the LGPL.
33  *
34  * ***** END LICENSE BLOCK ***** */
35
36 const CLASS_ID    = Components.ID("{201a6b60-1dfc-11de-8c30-0800200c9a66}");
37 const CLASS_NAME  = "Microsoft Exchange Calendar Data Provider";
38 const CONTRACT_ID = "@mozilla.org/calendar/calendar;1?type=exchange";
39
40 var _tbver = 0;
41
42 var calExchangeFactory = {
43         createInstance: function(aOuter, aIID)
44         {
45                 if (aOuter != null)
46                         throw Components.results.NS_ERROR_NO_AGGREGATION;
47
48                 return (new calExchange()).QueryInterface(aIID);
49         }
50 };
51
52 var calExchangeModule = {
53         mUtilsLoaded: false,    
54
55         registerSelf: function(aCompMgr, aFileSpec, aLocation, aType)
56         {
57                 aCompMgr = aCompMgr.QueryInterface(
58                         Components.interfaces.nsIComponentRegistrar);
59
60                 aCompMgr.registerFactoryLocation(CLASS_ID, CLASS_NAME,
61                                                  CONTRACT_ID, aFileSpec,
62                                                  aLocation, aType);
63         },
64
65         unregisterSelf: function(aCompMgr, aLocation, aType)
66         {
67                 aCompMgr = aCompMgr.QueryInterface(
68                         Components.interfaces.nsIComponentRegistrar);
69
70                 aCompMgr.unregisterFactoryLocation(CLASS_ID, aLocation);
71         },
72
73         getClassObject: function(aCompMgr, aCID, aIID)
74         {
75                 if (!aIID.equals(Components.interfaces.nsIFactory))
76                         throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
77
78                 this.loadUtils();
79
80                 if (aCID.equals(CLASS_ID))
81                         return calExchangeFactory;
82
83                 throw Components.results.NS_ERROR_NO_INTERFACE;
84         },
85
86         canUnload: function(aCompMgr)
87         {
88                 return true;
89         },
90
91         loadUtils: function()
92         {
93                 if (this.mUtilsLoaded)
94                         return;
95
96                 this.mScripts = ["compat.js", "soapout.js", "soapin.js", "calExchange.js"];
97
98                 this.getVersion();
99
100                 if (_tbver == 2)
101                         this.doLoadUtils2();
102                 else
103                         this.doLoadUtils3();
104
105                 this.mUtilsLoaded = true;
106         },
107
108         getVersion: function()
109         {
110                 _tbver = 3;
111
112                 var appInfo = Components.classes["@mozilla.org/xre/app-info;1"]
113                             .getService(Components.interfaces.nsIXULAppInfo);
114
115                 var ver = appInfo.version[0];
116
117                 _tbver = parseInt(ver, 10);
118 //              dump("Version: " + _tbver + "\n");
119
120                 if (_tbver == 0)
121                         _tbver = 2;
122         },
123
124         doLoadUtils3: function()
125         {
126                 Components.utils.import("resource://calendar/modules/calUtils.jsm");
127                 Components.utils.import("resource://calendar/modules/calProviderUtils.jsm");
128                 Components.utils.import("resource://calendar/modules/calAuthUtils.jsm");
129                 cal.loadScripts(["calUtils.js"], this.__parent__);
130
131                 let thisDir = __LOCATION__.parent.parent.clone();
132                 thisDir.append("js");
133
134                 cal.loadScripts(this.mScripts,
135                                 this.__parent__,
136                                 thisDir);
137         },
138
139         doLoadUtils2: function()
140         {
141                 this.mScripts = ["compat2.js"].concat(this.mScripts);
142
143                 const kLIGHTNING_UID = "{e2fda1a4-762b-4020-b5ad-a41df1933103}";
144                 const baseScripts = ["calUtils.js", "calAuthUtils.js",
145                                      "calProviderBase.js",
146                                      "calProviderUtils.js"];
147
148                 var extman = Components.classes["@mozilla.org/extensions/manager;1"]
149                               .getService(Components.interfaces.nsIExtensionManager);
150                 try {
151                         var appdir = extman.getInstallLocation(kLIGHTNING_UID)
152                                       .getItemLocation(kLIGHTNING_UID);
153                         appdir.append("js");
154                 } catch (e) {
155                         Components.utils.reportError("Error getting Lightning Extension Directory");
156                         throw(e);
157                 }
158
159                 for each (var script in baseScripts) {
160                         var scriptFile = appdir.clone();
161                         scriptFile.append(script);
162             
163                         ComponentsUtilsImport(scriptFile);
164                 }
165
166                 appdir = __LOCATION__.parent.parent;
167                 appdir.append("js");
168
169                 for each (var script in this.mScripts) {
170                         var scriptFile = appdir.clone();
171                         scriptFile.append(script);
172
173                         ComponentsUtilsImport(scriptFile);
174                 }
175         }
176 };
177
178 // XXX v2 only
179 function ComponentsUtilsImport(aFile) {
180     var iosvc = Components.classes["@mozilla.org/network/io-service;1"]
181                           .getService(Components.interfaces.nsIIOService);
182
183     var loader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"]
184                            .getService(Components.interfaces.mozIJSSubScriptLoader);
185
186     try {
187         var scriptUri = iosvc.newFileURI(aFile);
188         loader.loadSubScript(scriptUri.spec, null);
189     } catch (e) {
190         Components.utils.reportError("Error while loading " + aFile.path);
191         throw e;
192     }
193 }
194
195 function NSGetModule(aComponentManager, aFileSpec)
196 {
197         return calExchangeModule;
198 }