!import
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
1 /* -*- Mode: Javascript; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 *
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 *
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
10 *
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
15 *
16 * The Original Code is mozilla.org code.
17 *
18 * The Initial Developer of the Original Code is
19 * Oracle Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 2005
21 * the Initial Developer. All Rights Reserved.
22 *
23 * Contributor(s):
24 * Dan Mosedale <dan.mosedale@oracle.com>
25 * Jeremy Laine <jeremy.laine@m4x.org>
26 *
27 * Alternatively, the contents of this file may be used under the terms of
28 * either of the GNU General Public License Version 2 or later (the "GPL"),
29 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
38 *
39 * ***** END LICENSE BLOCK ***** */
40
41 const NS_ABLDAPATTRIBUTEMAP_CID = Components.ID(
42 "{127b341a-bdda-4270-85e1-edff569a9b85}");
43 const NS_ABLDAPATTRIBUTEMAPSERVICE_CID = Components.ID(
44 "{4ed7d5e1-8800-40da-9e78-c4f509d7ac5e}");
45
nsAbLDAPAttributeMap
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
46 function nsAbLDAPAttributeMap() {}
47
48 nsAbLDAPAttributeMap.prototype = {
49 mPropertyMap: {},
50 mAttrMap: {},
51
getAttributeList
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
52 getAttributeList: function getAttributeList(aProperty) {
53
54 if (!(aProperty in this.mPropertyMap)) {
55 return null;
56 }
57
58 // return the joined list
59 return this.mPropertyMap[aProperty].join(",");
60 },
61
getAttributes
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
62 getAttributes: function getAttributes(aProperty, aCount, aAttrs) {
63
64 // fail if no entry for this
65 if (!(aProperty in this.mPropertyMap)) {
66 throw Components.results.NS_ERROR_FAILURE;
67 }
68
69 aAttrs = this.mPropertyMap[aProperty];
70 aCount = aAttrs.length;
71 return aAttrs;
72 },
73
getFirstAttribute
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
74 getFirstAttribute: function getFirstAttribute(aProperty) {
75
76 // fail if no entry for this
77 if (!(aProperty in this.mPropertyMap)) {
78 return null;
79 }
80
81 return this.mPropertyMap[aProperty][0];
82 },
83
setAttributeList
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
84 setAttributeList: function setAttributeList(aProperty, aAttributeList,
85 aAllowInconsistencies) {
86
87 var attrs = aAttributeList.split(",");
88
89 // check to make sure this call won't allow multiple mappings to be
90 // created, if requested
91 if (!aAllowInconsistencies) {
92 for each (var attr in attrs) {
93 if (attr in this.mAttrMap && this.mAttrMap[attr] != aProperty) {
94 throw Components.results.NS_ERROR_FAILURE;
95 }
96 }
97 }
98
99 // delete any attr mappings created by the existing property map entry
100 for each (attr in this.mPropertyMap[aProperty]) {
101 delete this.mAttrMap[attr];
102 }
103
104 // add these attrs to the attrmap
105 for each (attr in attrs) {
106 this.mAttrMap[attr] = aProperty;
107 }
108
109 // add them to the property map
110 this.mPropertyMap[aProperty] = attrs;
111 },
112
getProperty
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
113 getProperty: function getProperty(aAttribute) {
114
115 if (!(aAttribute in this.mAttrMap)) {
116 return null;
117 }
118
119 return this.mAttrMap[aAttribute];
120 },
121
getAllCardAttributes
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
122 getAllCardAttributes: function getAllCardAttributes() {
123 var attrs = [];
124 for each (var prop in this.mPropertyMap) {
125 attrs.push(prop);
126 }
127
128 if (!attrs.length) {
129 throw Components.results.NS_ERROR_FAILURE;
130 }
131
132 return attrs.join(",");
133 },
134
getAllCardProperties
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
135 getAllCardProperties: function getAllCardProperties(aCount) {
136
137 var props = [];
138 for (var prop in this.mPropertyMap) {
139 props.push(prop);
140 }
141
142 aCount.value = props.length;
143 return props;
144 },
145
setFromPrefs
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
146 setFromPrefs: function setFromPrefs(aPrefBranchName) {
147 var prefSvc = Components.classes["@mozilla.org/preferences-service;1"].
148 getService(Components.interfaces.nsIPrefService);
149
150 // get the right pref branch
151 var branch = prefSvc.getBranch(aPrefBranchName + ".");
152
153 // get the list of children
154 var childCount = {};
155 var children = branch.getChildList("", childCount);
156
157 // do the actual sets
158 for each (var child in children) {
159 this.setAttributeList(child, branch.getCharPref(child), true);
160 }
161
162 // ensure that everything is kosher
163 this.checkState();
164 },
165
setCardPropertiesFromLDAPMessage
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
166 setCardPropertiesFromLDAPMessage: function
167 setCardPropertiesFromLDAPMessage(aMessage, aCard) {
168
169 var cardValueWasSet = false;
170
171 var msgAttrCount = {};
172 var msgAttrs = aMessage.getAttributes(msgAttrCount);
173
174 // downcase the array for comparison
toLower
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
175 function toLower(a) { return a.toLowerCase(); }
176 msgAttrs = msgAttrs.map(toLower);
177
178 // deal with each addressbook property
179 for (var prop in this.mPropertyMap) {
180
181 // go through the list of possible attrs in precedence order
182 for each (var attr in this.mPropertyMap[prop]) {
183
184 attr = attr.toLowerCase();
185
186 // find the first attr that exists in this message
187 if (msgAttrs.indexOf(attr) != -1) {
188
189 try {
190 var values = aMessage.getValues(attr, {});
191 aCard.setCardValue(prop, values[0]);
192
193 cardValueWasSet = true;
194 break;
195 } catch (ex) {
196 // ignore any errors getting message values or setting card values
197 }
198 }
199 }
200 }
201
202 if (!cardValueWasSet) {
203 throw Components.results.NS_ERROR_FAILURE;
204 }
205
206 return;
207 },
208
checkState
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
209 checkState: function checkState() {
210
211 var attrsSeen = [];
212
213 for each (var attrArray in this.mPropertyMap) {
214
215 for each (var attr in attrArray) {
216
217 // multiple attributes that mapped to the empty string are permitted
218 if (!attr.length) {
219 continue;
220 }
221
222 // if we've seen this before, there's a problem
223 if (attrsSeen.indexOf(attr) != -1) {
224 throw Components.results.NS_ERROR_FAILURE;
225 }
226
227 // remember that we've seen it now
228 attrsSeen.push(attr);
229 }
230 }
231
232 return;
233 },
234
QueryInterface
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
235 QueryInterface: function QueryInterface(iid) {
236 if (!iid.equals(Components.interfaces.nsIAbLDAPAttributeMap) &&
237 !iid.equals(Components.interfaces.nsISupports)) {
238 throw Components.results.NS_ERROR_NO_INTERFACE;
239 }
240
241 return this;
242 }
243 }
244
nsAbLDAPAttributeMapService
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
245 function nsAbLDAPAttributeMapService() {
246 }
247
248 nsAbLDAPAttributeMapService.prototype = {
249
250 mAttrMaps: {},
251
getMapForPrefBranch
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
252 getMapForPrefBranch: function getMapForPrefBranch(aPrefBranchName) {
253
254 // if we've already got this map, return it
255 if (aPrefBranchName in this.mAttrMaps) {
256 return this.mAttrMaps[aPrefBranchName];
257 }
258
259 // otherwise, try and create it
260 var attrMap = new nsAbLDAPAttributeMap();
261 attrMap.setFromPrefs("ldap_2.servers.default.attrmap");
262 attrMap.setFromPrefs(aPrefBranchName + ".attrmap");
263
264 // cache
265 this.mAttrMaps[aPrefBranchName] = attrMap;
266
267 // and return
268 return attrMap;
269 },
270
QueryInterface
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
271 QueryInterface: function (iid) {
272 if (iid.equals(Components.interfaces.nsIAbLDAPAttributeMapService) ||
273 iid.equals(Components.interfaces.nsISupports))
274 return this;
275
276 Components.returnCode = Components.results.NS_ERROR_NO_INTERFACE;
277 return null;
278 }
279 }
280
281 var nsAbLDAPAttributeMapModule = {
registerSelf
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
282 registerSelf: function (compMgr, fileSpec, location, type) {
283 debug("*** Registering Addressbook LDAP Attribute Map components\n");
284 compMgr = compMgr.QueryInterface(
285 Components.interfaces.nsIComponentRegistrar);
286
287 compMgr.registerFactoryLocation(
288 NS_ABLDAPATTRIBUTEMAP_CID,
289 "Addressbook LDAP Attribute Map Component",
290 "@mozilla.org/addressbook/ldap-attribute-map;1",
291 fileSpec, location, type);
292
293 compMgr.registerFactoryLocation(
294 NS_ABLDAPATTRIBUTEMAPSERVICE_CID,
295 "Addressbook LDAP Attribute Map Service",
296 "@mozilla.org/addressbook/ldap-attribute-map-service;1",
297 fileSpec, location, type);
298 },
299
300 /*
301 * The GetClassObject method is responsible for producing Factory objects
302 */
getClassObject
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
303 getClassObject: function (compMgr, cid, iid) {
304 if (!iid.equals(Components.interfaces.nsIFactory))
305 throw Components.results.NS_ERROR_NOT_IMPLEMENTED;
306
307 if (cid.equals(NS_ABLDAPATTRIBUTEMAP_CID)) {
308 return this.nsAbLDAPAttributeMapFactory;
309 }
310
311 if (cid.equals(NS_ABLDAPATTRIBUTEMAPSERVICE_CID)) {
312 return this.nsAbLDAPAttributeMapServiceFactory;
313 }
314
315 throw Components.results.NS_ERROR_NO_INTERFACE;
316 },
317
318 /* factory objects */
319 nsAbLDAPAttributeMapFactory: {
320 /*
321 * Construct an instance of the interface specified by iid, possibly
322 * aggregating it with the provided outer. (If you don't know what
323 * aggregation is all about, you don't need to. It reduces even the
324 * mightiest of XPCOM warriors to snivelling cowards.)
325 */
createInstance
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
326 createInstance: function (outer, iid) {
327 if (outer != null)
328 throw Components.results.NS_ERROR_NO_AGGREGATION;
329
330 return (new nsAbLdapAttributeMap()).QueryInterface(iid);
331 }
332 },
333
334 nsAbLDAPAttributeMapServiceFactory: {
335 /*
336 * Construct an instance of the interface specified by iid, possibly
337 * aggregating it with the provided outer. (If you don't know what
338 * aggregation is all about, you don't need to. It reduces even the
339 * mightiest of XPCOM warriors to snivelling cowards.)
340 */
createInstance
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
341 createInstance: function (outer, iid) {
342 if (outer != null)
343 throw Components.results.NS_ERROR_NO_AGGREGATION;
344
345 return (new nsAbLDAPAttributeMapService()).QueryInterface(iid);
346 }
347 },
348
349 /*
350 * The canUnload method signals that the component is about to be unloaded.
351 * C++ components can return false to indicate that they don't wish to be
352 * unloaded, but the return value from JS components' canUnload is ignored:
353 * mark-and-sweep will keep everything around until it's no longer in use,
354 * making unconditional ``unload'' safe.
355 *
356 * You still need to provide a (likely useless) canUnload method, though:
357 * it's part of the nsIModule interface contract, and the JS loader _will_
358 * call it.
359 */
canUnload
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
360 canUnload: function(compMgr) {
361 return true;
362 }
363 };
364
NSGetModule
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
setCardPropertiesFromLDAPMessage
(0 calls, 0 incl. v-uS, 0 excl. v-uS)
365 function NSGetModule(compMgr, fileSpec) {
366 return nsAbLDAPAttributeMapModule;
367 }