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