summaryrefslogtreecommitdiffstats
path: root/comm/calendar/base/src/CalFreeBusyService.jsm
blob: 822f37acda34db37e8b90444605dfb8f9ddcbc23 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

var EXPORTED_SYMBOLS = ["CalFreeBusyService"];

var { cal } = ChromeUtils.import("resource:///modules/calendar/calUtils.jsm");

function CalFreeBusyListener(numOperations, finalListener) {
  this.mFinalListener = finalListener;
  this.mNumOperations = numOperations;

  this.opGroup = new cal.data.OperationGroup(() => {
    this.notifyResult(null);
  });
}
CalFreeBusyListener.prototype = {
  QueryInterface: ChromeUtils.generateQI(["calIGenericOperationListener"]),

  mFinalListener: null,
  mNumOperations: 0,
  opGroup: null,

  notifyResult(result) {
    let listener = this.mFinalListener;
    if (listener) {
      if (!this.opGroup.isPending) {
        this.mFinalListener = null;
      }
      listener.onResult(this.opGroup, result);
    }
  },

  // calIGenericOperationListener:
  onResult(aOperation, aResult) {
    if (this.mFinalListener) {
      if (!aOperation || !aOperation.isPending) {
        --this.mNumOperations;
        if (this.mNumOperations <= 0) {
          this.opGroup.notifyCompleted();
        }
      }
      let opStatus = aOperation ? aOperation.status : Cr.NS_OK;
      if (Components.isSuccessCode(opStatus) && aResult && Array.isArray(aResult)) {
        this.notifyResult(aResult);
      } else {
        this.notifyResult([]);
      }
    }
  },
};

function CalFreeBusyService() {
  this.wrappedJSObject = this;
  this.mProviders = new Set();
}
CalFreeBusyService.prototype = {
  QueryInterface: ChromeUtils.generateQI(["calIFreeBusyProvider", "calIFreeBusyService"]),
  classID: Components.ID("{29c56cd5-d36e-453a-acde-0083bd4fe6d3}"),

  mProviders: null,

  // calIFreeBusyProvider:
  getFreeBusyIntervals(aCalId, aRangeStart, aRangeEnd, aBusyTypes, aListener) {
    let groupListener = new CalFreeBusyListener(this.mProviders.size, aListener);
    if (this.mProviders.size == 0) {
      groupListener.onResult(null, []);
    }
    for (let provider of this.mProviders.values()) {
      let operation = provider.getFreeBusyIntervals(
        aCalId,
        aRangeStart,
        aRangeEnd,
        aBusyTypes,
        groupListener
      );
      groupListener.opGroup.add(operation);
    }
    return groupListener.opGroup;
  },

  // calIFreeBusyService:
  addProvider(aProvider) {
    this.mProviders.add(aProvider.QueryInterface(Ci.calIFreeBusyProvider));
  },
  removeProvider(aProvider) {
    this.mProviders.delete(aProvider.QueryInterface(Ci.calIFreeBusyProvider));
  },
};