diff options
Diffstat (limited to 'comm/mail/components/activity/Activity.jsm')
-rw-r--r-- | comm/mail/components/activity/Activity.jsm | 322 |
1 files changed, 322 insertions, 0 deletions
diff --git a/comm/mail/components/activity/Activity.jsm b/comm/mail/components/activity/Activity.jsm new file mode 100644 index 0000000000..1b23efe1c7 --- /dev/null +++ b/comm/mail/components/activity/Activity.jsm @@ -0,0 +1,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"]), +}; |