1.1 --- a/modules/fburl.js Sat Sep 27 17:43:21 2014 +0200
1.2 +++ b/modules/fburl.js Sun Sep 28 00:12:15 2014 +0200
1.3 @@ -5,6 +5,8 @@
1.4 const Cu = Components.utils;
1.5
1.6 Cu.import("resource://calendar/modules/calUtils.jsm");
1.7 +Cu.import("resource:///modules/mailServices.js");
1.8 +Cu.import("resource://gre/modules/XPCOMUtils.jsm");
1.9
1.10 if ("undefined" == typeof(fburl)) {
1.11 var fburl = {};
1.12 @@ -13,12 +15,17 @@
1.13 const calIFreeBusyInterval = Ci.calIFreeBusyInterval;
1.14 const calIDateTime = Ci.calIDateTime;
1.15
1.16 -const fbUrlProviderClassID = Components.ID("{11291d94-b457-4322-bfba-ae9df4b6a3c1}");
1.17 -
1.18 -fburl.fbUrlProvider = function(cal) { this.cal = cal; };
1.19 +fburl.fbUrlProvider = function(cal) {
1.20 + this.cal = cal;
1.21 + this._query = null;
1.22 + this._context = null;
1.23 + this._result = null;
1.24 + this._searched = false;
1.25 + this._error = null;
1.26 +};
1.27
1.28 fburl.fbUrlProvider.prototype = {
1.29 - classID: fbUrlProviderClassID,
1.30 + classID: Components.ID("{11291d94-b457-4322-bfba-ae9df4b6a3c1}"),
1.31
1.32 getFreeBusyIntervals: function (aCalId, aRangeStart, aRangeEnd, aBusyTypes, aListener) {
1.33
1.34 @@ -32,9 +39,84 @@
1.35 start, end);
1.36 periods.push(interval);
1.37 aListener.onResult(null, periods);
1.38 - }
1.39 + },
1.40 +
1.41 + getLDAPAddressBooks: function () {
1.42 +
1.43 + var abManager = Cc["@mozilla.org/abmanager;1"].getService(Ci.nsIAbManager);
1.44 + var allAddressBooks = abManager.directories;
1.45 + var books = [];
1.46 +
1.47 + while (allAddressBooks.hasMoreElements()) {
1.48 + var addressBook = allAddressBooks.getNext().QueryInterface(Ci.nsIAbDirectory);
1.49 + if (addressBook instanceof Ci.nsIAbLDAPDirectory) {
1.50 + books.push(addressBook);
1.51 + }
1.52 + }
1.53 +
1.54 + return books;
1.55 + },
1.56 +
1.57 + startSearch: function (aCalId) {
1.58 +
1.59 + var service = Cc["@mozilla.org/network/ldap-service;1"].getService(Ci.nsILDAPService);
1.60 + var books = this.getLDAPAddressBooks();
1.61 +
1.62 + this._result = null;
1.63 + aCalId = aCalId.replace(/^mailto:/i, "");
1.64 +
1.65 + for each (var book in books) {
1.66 + var ldap = book.QueryInterface(Ci.nsIAbLDAPDirectory);
1.67 + var attrmap = Cc["@mozilla.org/addressbook/ldap-attribute-map;1"].createInstance(Ci.nsIAbLDAPAttributeMap);
1.68 + var args = Cc["@mozilla.org/addressbook/directory/query-arguments;1"].createInstance(Ci.nsIAbDirectoryQueryArguments);
1.69 +
1.70 + attrmap.setAttributeList("PrimaryEmail", "mail", true);
1.71 + attrmap.setAttributeList("FBURL", "calFbUrl", true);
1.72 + args.filter = service.createFilter(aCalId.length * 2, "(%a=%v)", "", "", "mail", aCalId);
1.73 + args.typeSpecificArg = attrmap;
1.74 + args.querySubDirectories = true;
1.75 +
1.76 + this._query = Cc["@mozilla.org/addressbook/ldap-directory-query;1"].createInstance(Ci.nsIAbDirectoryQuery);
1.77 + this._context = this._query.doQuery(ldap, args, this, ldap.maxHits, 0);
1.78 + this._searched = true;
1.79 +
1.80 + // NOTE: Do only one query for now.
1.81 +
1.82 + break;
1.83 + }
1.84 + },
1.85 +
1.86 + stopSearch: function stopSearch() {
1.87 + if (this._context) {
1.88 + this._query.stopQuery(this._context);
1.89 + this._context = null;
1.90 + }
1.91 + },
1.92 +
1.93 + onSearchFinished: function (aResult, aErrorMsg) {
1.94 + this._context = null;
1.95 + this._error = aErrorMsg;
1.96 + },
1.97 +
1.98 + onSearchFoundCard: function (aCard) {
1.99 +
1.100 + // NOTE: Will eventually use this to retrieve the free/busy document,
1.101 + // NOTE: extract the intervals, and then notify the listener.
1.102 +
1.103 + this._result = aCard;
1.104 + },
1.105 +
1.106 + QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsIAbDirSearchListener])
1.107 };
1.108
1.109 fburl.initProvider = function() {
1.110 cal.getFreeBusyService().addProvider(new fburl.fbUrlProvider(cal));
1.111 }
1.112 +
1.113 +fburl.test = function(aCalId) {
1.114 + var fbp = new fburl.fbUrlProvider(cal);
1.115 + fbp.startSearch(aCalId);
1.116 + return fbp;
1.117 +}
1.118 +
1.119 +const NSGetFactory = XPCOMUtils.generateNSGetFactory([fburl.fbUrlProvider]);