# HG changeset patch # User Paul Boddie # Date 1411855935 -7200 # Node ID dc2dbe0697209edc72f1e7f9973bec05646463a3 # Parent cb3fe97b64f689f79aa8b55d3ab0645ace5d4636 Added LDAP directory searching via the address book service, obtaining calFbUrl from the directory, if available. diff -r cb3fe97b64f6 -r dc2dbe069720 modules/fburl.js --- a/modules/fburl.js Sat Sep 27 17:43:21 2014 +0200 +++ b/modules/fburl.js Sun Sep 28 00:12:15 2014 +0200 @@ -5,6 +5,8 @@ const Cu = Components.utils; Cu.import("resource://calendar/modules/calUtils.jsm"); +Cu.import("resource:///modules/mailServices.js"); +Cu.import("resource://gre/modules/XPCOMUtils.jsm"); if ("undefined" == typeof(fburl)) { var fburl = {}; @@ -13,12 +15,17 @@ const calIFreeBusyInterval = Ci.calIFreeBusyInterval; const calIDateTime = Ci.calIDateTime; -const fbUrlProviderClassID = Components.ID("{11291d94-b457-4322-bfba-ae9df4b6a3c1}"); - -fburl.fbUrlProvider = function(cal) { this.cal = cal; }; +fburl.fbUrlProvider = function(cal) { + this.cal = cal; + this._query = null; + this._context = null; + this._result = null; + this._searched = false; + this._error = null; +}; fburl.fbUrlProvider.prototype = { - classID: fbUrlProviderClassID, + classID: Components.ID("{11291d94-b457-4322-bfba-ae9df4b6a3c1}"), getFreeBusyIntervals: function (aCalId, aRangeStart, aRangeEnd, aBusyTypes, aListener) { @@ -32,9 +39,84 @@ start, end); periods.push(interval); aListener.onResult(null, periods); - } + }, + + getLDAPAddressBooks: function () { + + var abManager = Cc["@mozilla.org/abmanager;1"].getService(Ci.nsIAbManager); + var allAddressBooks = abManager.directories; + var books = []; + + while (allAddressBooks.hasMoreElements()) { + var addressBook = allAddressBooks.getNext().QueryInterface(Ci.nsIAbDirectory); + if (addressBook instanceof Ci.nsIAbLDAPDirectory) { + books.push(addressBook); + } + } + + return books; + }, + + startSearch: function (aCalId) { + + var service = Cc["@mozilla.org/network/ldap-service;1"].getService(Ci.nsILDAPService); + var books = this.getLDAPAddressBooks(); + + this._result = null; + aCalId = aCalId.replace(/^mailto:/i, ""); + + for each (var book in books) { + var ldap = book.QueryInterface(Ci.nsIAbLDAPDirectory); + var attrmap = Cc["@mozilla.org/addressbook/ldap-attribute-map;1"].createInstance(Ci.nsIAbLDAPAttributeMap); + var args = Cc["@mozilla.org/addressbook/directory/query-arguments;1"].createInstance(Ci.nsIAbDirectoryQueryArguments); + + attrmap.setAttributeList("PrimaryEmail", "mail", true); + attrmap.setAttributeList("FBURL", "calFbUrl", true); + args.filter = service.createFilter(aCalId.length * 2, "(%a=%v)", "", "", "mail", aCalId); + args.typeSpecificArg = attrmap; + args.querySubDirectories = true; + + this._query = Cc["@mozilla.org/addressbook/ldap-directory-query;1"].createInstance(Ci.nsIAbDirectoryQuery); + this._context = this._query.doQuery(ldap, args, this, ldap.maxHits, 0); + this._searched = true; + + // NOTE: Do only one query for now. + + break; + } + }, + + stopSearch: function stopSearch() { + if (this._context) { + this._query.stopQuery(this._context); + this._context = null; + } + }, + + onSearchFinished: function (aResult, aErrorMsg) { + this._context = null; + this._error = aErrorMsg; + }, + + onSearchFoundCard: function (aCard) { + + // NOTE: Will eventually use this to retrieve the free/busy document, + // NOTE: extract the intervals, and then notify the listener. + + this._result = aCard; + }, + + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsIAbDirSearchListener]) }; fburl.initProvider = function() { cal.getFreeBusyService().addProvider(new fburl.fbUrlProvider(cal)); } + +fburl.test = function(aCalId) { + var fbp = new fburl.fbUrlProvider(cal); + fbp.startSearch(aCalId); + return fbp; +} + +const NSGetFactory = XPCOMUtils.generateNSGetFactory([fburl.fbUrlProvider]);