summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/activity/Activity.jsm
blob: 1b23efe1c7412247a26a10639a32cfe9d591c836 (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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
/* 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 = ["ActivityProcess", "ActivityEvent", "ActivityWarning"];

// Base class for ActivityProcess and ActivityEvent objects

function Activity() {
  this._initLogging();
  this._listeners = [];
  this._subjects = [];
}

Activity.prototype = {
  id: -1,
  bindingName: "",
  iconClass: "",
  groupingStyle: Ci.nsIActivity.GROUPING_STYLE_BYCONTEXT,
  facet: "",
  displayText: "",
  initiator: null,
  contextType: "",
  context: "",
  contextObj: null,

  _initLogging() {
    this.log = console.createInstance({
      prefix: "mail.activity",
      maxLogLevel: "Warn",
      maxLogLevelPref: "mail.activity.loglevel",
    });
  },

  addListener(aListener) {
    this._listeners.push(aListener);
  },

  removeListener(aListener) {
    for (let i = 0; i < this._listeners.length; i++) {
      if (this._listeners[i] == aListener) {
        this._listeners.splice(i, 1);
        break;
      }
    }
  },

  addSubject(aSubject) {
    this._subjects.push(aSubject);
  },

  getSubjects() {
    return this._subjects.slice();
  },
};

function ActivityProcess() {
  Activity.call(this);
  this.bindingName = "activity-process-item";
  this.groupingStyle = Ci.nsIActivity.GROUPING_STYLE_BYCONTEXT;
}

ActivityProcess.prototype = {
  __proto__: Activity.prototype,

  percentComplete: -1,
  lastStatusText: "",
  workUnitComplete: 0,
  totalWorkUnits: 0,
  startTime: Date.now(),
  _cancelHandler: null,
  _pauseHandler: null,
  _retryHandler: null,
  _state: Ci.nsIActivityProcess.STATE_INPROGRESS,

  init(aDisplayText, aInitiator) {
    this.displayText = aDisplayText;
    this.initiator = aInitiator;
  },

  get state() {
    return this._state;
  },

  set state(val) {
    if (val == this._state) {
      return;
    }

    // test validity of the new state
    //
    if (
      this._state == Ci.nsIActivityProcess.STATE_INPROGRESS &&
      !(
        val == Ci.nsIActivityProcess.STATE_COMPLETED ||
        val == Ci.nsIActivityProcess.STATE_CANCELED ||
        val == Ci.nsIActivityProcess.STATE_WAITINGFORRETRY ||
        val == Ci.nsIActivityProcess.STATE_WAITINGFORINPUT ||
        val == Ci.nsIActivityProcess.STATE_PAUSED
      )
    ) {
      throw Components.Exception("", Cr.NS_ERROR_ILLEGAL_VALUE);
    }

    // we cannot change the state after the activity is completed,
    // or it is canceled.
    if (
      this._state == Ci.nsIActivityProcess.STATE_COMPLETED ||
      this._state == Ci.nsIActivityProcess.STATE_CANCELED
    ) {
      throw Components.Exception("", Cr.NS_ERROR_ILLEGAL_VALUE);
    }

    if (
      this._state == Ci.nsIActivityProcess.STATE_PAUSED &&
      !(
        val == Ci.nsIActivityProcess.STATE_COMPLETED ||
        val == Ci.nsIActivityProcess.STATE_INPROGRESS ||
        val == Ci.nsIActivityProcess.STATE_WAITINGFORRETRY ||
        val == Ci.nsIActivityProcess.STATE_WAITINGFORINPUT ||
        val == Ci.nsIActivityProcess.STATE_CANCELED
      )
    ) {
      throw Components.Exception("", Cr.NS_ERROR_ILLEGAL_VALUE);
    }

    if (
      this._state == Ci.nsIActivityProcess.STATE_WAITINGFORINPUT &&
      !(
        val == Ci.nsIActivityProcess.STATE_INPROGRESS ||
        val == Ci.nsIActivityProcess.STATE_CANCELED
      )
    ) {
      throw Components.Exception("", Cr.NS_ERROR_ILLEGAL_VALUE);
    }

    if (
      this._state == Ci.nsIActivityProcess.STATE_WAITINGFORRETRY &&
      !(
        val == Ci.nsIActivityProcess.STATE_INPROGRESS ||
        val == Ci.nsIActivityProcess.STATE_CANCELED
      )
    ) {
      throw Components.Exception("", Cr.NS_ERROR_ILLEGAL_VALUE);
    }

    let oldState = this._state;
    this._state = val;

    // let the listeners know about the change
    this.log.debug("Notifying onStateChanged listeners");
    for (let value of this._listeners) {
      try {
        value.onStateChanged(this, oldState);
      } catch (e) {
        this.log.error("Exception thrown by onStateChanged listener: " + e);
      }
    }
  },

  setProgress(aStatusText, aWorkUnitsComplete, aTotalWorkUnits) {
    if (aTotalWorkUnits == 0) {
      this.percentComplete = -1;
      this.workUnitComplete = 0;
      this.totalWorkUnits = 0;
    } else {
      this.percentComplete = parseInt(
        (100.0 * aWorkUnitsComplete) / aTotalWorkUnits
      );
      this.workUnitComplete = aWorkUnitsComplete;
      this.totalWorkUnits = aTotalWorkUnits;
    }
    this.lastStatusText = aStatusText;

    // notify listeners
    for (let value of this._listeners) {
      try {
        value.onProgressChanged(
          this,
          aStatusText,
          aWorkUnitsComplete,
          aTotalWorkUnits
        );
      } catch (e) {
        this.log.error("Exception thrown by onProgressChanged listener: " + e);
      }
    }
  },

  get cancelHandler() {
    return this._cancelHandler;
  },

  set cancelHandler(val) {
    this._cancelHandler = val;

    // let the listeners know about the change
    this.log.debug("Notifying onHandlerChanged listeners");
    for (let value of this._listeners) {
      try {
        value.onHandlerChanged(this);
      } catch (e) {
        this.log.error("Exception thrown by onHandlerChanged listener: " + e);
      }
    }
  },

  get pauseHandler() {
    return this._pauseHandler;
  },

  set pauseHandler(val) {
    this._pauseHandler = val;

    // let the listeners know about the change
    this.log.debug("Notifying onHandlerChanged listeners");
    for (let value of this._listeners) {
      value.onHandlerChanged(this);
    }
  },

  get retryHandler() {
    return this._retryHandler;
  },

  set retryHandler(val) {
    this._retryHandler = val;

    // let the listeners know about the change
    this.log.debug("Notifying onHandlerChanged listeners");
    for (let value of this._listeners) {
      value.onHandlerChanged(this);
    }
  },

  QueryInterface: ChromeUtils.generateQI(["nsIActivityProcess", "nsIActivity"]),
};

function ActivityEvent() {
  Activity.call(this);
  this.bindingName = "activity-event-item";
  this.groupingStyle = Ci.nsIActivity.GROUPING_STYLE_STANDALONE;
}

ActivityEvent.prototype = {
  __proto__: Activity.prototype,

  statusText: "",
  startTime: 0,
  completionTime: 0,
  _undoHandler: null,

  init(aDisplayText, aInitiator, aStatusText, aStartTime, aCompletionTime) {
    this.displayText = aDisplayText;
    this.statusText = aStatusText;
    this.startTime = aStartTime;
    if (aCompletionTime) {
      this.completionTime = aCompletionTime;
    } else {
      this.completionTime = Date.now();
    }
    this.initiator = aInitiator;
    this._completionTime = aCompletionTime;
  },

  get undoHandler() {
    return this._undoHandler;
  },

  set undoHandler(val) {
    this._undoHandler = val;

    // let the listeners know about the change
    this.log.debug("Notifying onHandlerChanged listeners");
    for (let value of this._listeners) {
      value.onHandlerChanged(this);
    }
  },

  QueryInterface: ChromeUtils.generateQI(["nsIActivityEvent", "nsIActivity"]),
};

function ActivityWarning() {
  Activity.call(this);
  this.bindingName = "activity-warning-item";
  this.groupingStyle = Ci.nsIActivity.GROUPING_STYLE_BYCONTEXT;
}

ActivityWarning.prototype = {
  __proto__: Activity.prototype,

  recoveryTipText: "",
  _time: 0,
  _recoveryHandler: null,

  init(aWarningText, aInitiator, aRecoveryTipText) {
    this.displayText = aWarningText;
    this.initiator = aInitiator;
    this.recoveryTipText = aRecoveryTipText;
    this._time = Date.now();
  },

  get recoveryHandler() {
    return this._recoveryHandler;
  },

  set recoveryHandler(val) {
    this._recoveryHandler = val;

    // let the listeners know about the change
    this.log.debug("Notifying onHandlerChanged listeners");
    for (let value of this._listeners) {
      value.onHandlerChanged(this);
    }
  },

  get time() {
    return this._time;
  },

  QueryInterface: ChromeUtils.generateQI(["nsIActivityWarning", "nsIActivity"]),
};