1 var EXPORTED_SYMBOLS = [ "fburl" ]; 2 3 const Cc = Components.classes; 4 const Ci = Components.interfaces; 5 const Cu = Components.utils; 6 7 Cu.import("resource://calendar/modules/calUtils.jsm"); 8 Cu.import("resource:///modules/mailServices.js"); 9 Cu.import("resource://gre/modules/XPCOMUtils.jsm"); 10 11 if ("undefined" == typeof(fburl)) { 12 var fburl = {}; 13 }; 14 15 const calIFreeBusyInterval = Ci.calIFreeBusyInterval; 16 const calIDateTime = Ci.calIDateTime; 17 18 fburl.fbUrlProvider = function(cal) { 19 this.cal = cal; 20 this._query = null; 21 this._context = null; 22 this._result = null; 23 this._searched = false; 24 this._error = null; 25 }; 26 27 fburl.fbUrlProvider.prototype = { 28 classID: Components.ID("{11291d94-b457-4322-bfba-ae9df4b6a3c1}"), 29 30 getFreeBusyIntervals: function (aCalId, aRangeStart, aRangeEnd, aBusyTypes, aListener) { 31 32 var start = this.cal.createDateTime("20140923T100000Z"); 33 var end = this.cal.createDateTime("20140923T120000Z"); 34 35 var periods = []; 36 var interval = new this.cal.FreeBusyInterval( 37 aCalId, 38 calIFreeBusyInterval.BUSY, // fbType 39 start, end); 40 periods.push(interval); 41 aListener.onResult(null, periods); 42 }, 43 44 getLDAPAddressBooks: function () { 45 46 var abManager = Cc["@mozilla.org/abmanager;1"].getService(Ci.nsIAbManager); 47 var allAddressBooks = abManager.directories; 48 var books = []; 49 50 while (allAddressBooks.hasMoreElements()) { 51 var addressBook = allAddressBooks.getNext().QueryInterface(Ci.nsIAbDirectory); 52 if (addressBook instanceof Ci.nsIAbLDAPDirectory) { 53 books.push(addressBook); 54 } 55 } 56 57 return books; 58 }, 59 60 startSearch: function (aCalId) { 61 62 var service = Cc["@mozilla.org/network/ldap-service;1"].getService(Ci.nsILDAPService); 63 var books = this.getLDAPAddressBooks(); 64 65 this._result = null; 66 aCalId = aCalId.replace(/^mailto:/i, ""); 67 68 for each (var book in books) { 69 var ldap = book.QueryInterface(Ci.nsIAbLDAPDirectory); 70 var attrmap = Cc["@mozilla.org/addressbook/ldap-attribute-map;1"].createInstance(Ci.nsIAbLDAPAttributeMap); 71 var args = Cc["@mozilla.org/addressbook/directory/query-arguments;1"].createInstance(Ci.nsIAbDirectoryQueryArguments); 72 73 attrmap.setAttributeList("PrimaryEmail", "mail", true); 74 attrmap.setAttributeList("FBURL", "calFbUrl", true); 75 args.filter = service.createFilter(aCalId.length * 2, "(%a=%v)", "", "", "mail", aCalId); 76 args.typeSpecificArg = attrmap; 77 args.querySubDirectories = true; 78 79 this._query = Cc["@mozilla.org/addressbook/ldap-directory-query;1"].createInstance(Ci.nsIAbDirectoryQuery); 80 this._context = this._query.doQuery(ldap, args, this, ldap.maxHits, 0); 81 this._searched = true; 82 83 // NOTE: Do only one query for now. 84 85 break; 86 } 87 }, 88 89 stopSearch: function stopSearch() { 90 if (this._context) { 91 this._query.stopQuery(this._context); 92 this._context = null; 93 } 94 }, 95 96 onSearchFinished: function (aResult, aErrorMsg) { 97 this._context = null; 98 this._error = aErrorMsg; 99 }, 100 101 onSearchFoundCard: function (aCard) { 102 103 // NOTE: Will eventually use this to retrieve the free/busy document, 104 // NOTE: extract the intervals, and then notify the listener. 105 106 this._result = aCard; 107 }, 108 109 QueryInterface: XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsIAbDirSearchListener]) 110 }; 111 112 fburl.initProvider = function() { 113 cal.getFreeBusyService().addProvider(new fburl.fbUrlProvider(cal)); 114 } 115 116 fburl.test = function(aCalId) { 117 var fbp = new fburl.fbUrlProvider(cal); 118 fbp.startSearch(aCalId); 119 return fbp; 120 } 121 122 const NSGetFactory = XPCOMUtils.generateNSGetFactory([fburl.fbUrlProvider]);