summaryrefslogtreecommitdiffstats
path: root/comm/mail/components/activity/modules/sendLater.jsm
blob: 37027d96f164e628e67128819021f450c6a44d6d (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
/* -*- Mode: JavaScript; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */

const EXPORTED_SYMBOLS = ["sendLaterModule"];

var nsActProcess = Components.Constructor(
  "@mozilla.org/activity-process;1",
  "nsIActivityProcess",
  "init"
);
var nsActEvent = Components.Constructor(
  "@mozilla.org/activity-event;1",
  "nsIActivityEvent",
  "init"
);
var nsActWarning = Components.Constructor(
  "@mozilla.org/activity-warning;1",
  "nsIActivityWarning",
  "init"
);

/**
 * This really, really, sucks. Due to mailnews widespread use of
 * nsIMsgStatusFeedback we're bound to the UI to get any sensible feedback of
 * mail sending operations. The current send later code can't hook into the
 * progress listener easily to get the state of messages being sent, so we'll
 * just have to do it here.
 */
var sendMsgProgressListener = {
  QueryInterface: ChromeUtils.generateQI([
    "nsIMsgStatusFeedback",
    "nsISupportsWeakReference",
  ]),

  showStatusString(aStatusText) {
    sendLaterModule.onMsgStatus(aStatusText);
  },

  startMeteors() {},

  stopMeteors() {},

  showProgress(aPercentage) {
    sendLaterModule.onMessageSendProgress(0, 0, aPercentage, 0);
  },
};

// This module provides a link between the send later service and the activity
// manager.
var sendLaterModule = {
  _sendProcess: null,
  _copyProcess: null,
  _identity: null,
  _subject: null,

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

  get activityMgr() {
    delete this.activityMgr;
    return (this.activityMgr = Cc["@mozilla.org/activity-manager;1"].getService(
      Ci.nsIActivityManager
    ));
  },

  get bundle() {
    delete this.bundle;
    return (this.bundle = Services.strings.createBundle(
      "chrome://messenger/locale/activity.properties"
    ));
  },

  QueryInterface: ChromeUtils.generateQI(["nsIMsgSendLaterListener"]),

  _displayTextForHeader(aLocaleStringBase, aSubject) {
    return aSubject
      ? this.bundle.formatStringFromName(aLocaleStringBase + "WithSubject", [
          aSubject,
        ])
      : this.bundle.GetStringFromName(aLocaleStringBase);
  },

  _newProcess(aLocaleStringBase, aAddSubject) {
    let process = new nsActProcess(
      this._displayTextForHeader(
        aLocaleStringBase,
        aAddSubject ? this._subject : ""
      ),
      this.activityMgr
    );

    process.iconClass = "sendMail";
    process.groupingStyle = Ci.nsIActivity.GROUPING_STYLE_BYCONTEXT;
    process.contextObj = this;
    process.contextType = "SendLater";
    process.contextDisplayText =
      this.bundle.GetStringFromName("sendingMessages");

    return process;
  },

  // Use this to group an activity by the identity if we have one.
  _applyIdentityGrouping(aActivity) {
    if (this._identity) {
      aActivity.groupingStyle = Ci.nsIActivity.GROUPING_STYLE_BYCONTEXT;
      aActivity.contextType = this._identity.key;
      aActivity.contextObj = this._identity;
      let contextDisplayText = this._identity.identityName;
      if (!contextDisplayText) {
        contextDisplayText = this._identity.email;
      }

      aActivity.contextDisplayText = contextDisplayText;
    } else {
      aActivity.groupingStyle = Ci.nsIActivity.GROUPING_STYLE_STANDALONE;
    }
  },

  // Replaces the process with an event that reflects a completed process.
  _replaceProcessWithEvent(aProcess) {
    this.activityMgr.removeActivity(aProcess.id);

    let event = new nsActEvent(
      this._displayTextForHeader("sentMessage", this._subject),
      this.activityMgr,
      "",
      aProcess.startTime,
      new Date()
    );

    event.iconClass = "sendMail";
    this._applyIdentityGrouping(event);

    this.activityMgr.addActivity(event);
  },

  // Replaces the process with a warning that reflects the failed process.
  _replaceProcessWithWarning(
    aProcess,
    aCopyOrSend,
    aStatus,
    aMsg,
    aMessageHeader
  ) {
    this.activityMgr.removeActivity(aProcess.id);

    let warning = new nsActWarning(
      this._displayTextForHeader("failedTo" + aCopyOrSend, this._subject),
      this.activityMgr,
      ""
    );

    warning.groupingStyle = Ci.nsIActivity.GROUPING_STYLE_STANDALONE;
    this._applyIdentityGrouping(warning);

    this.activityMgr.addActivity(warning);
  },

  onStartSending(aTotalMessageCount) {
    if (!aTotalMessageCount) {
      this.log.error("onStartSending called with zero messages\n");
    }
  },

  onMessageStartSending(
    aCurrentMessage,
    aTotalMessageCount,
    aMessageHeader,
    aIdentity
  ) {
    // We want to use the identity and subject later, so store them for now.
    this._identity = aIdentity;
    if (aMessageHeader) {
      this._subject = aMessageHeader.mime2DecodedSubject;
    }

    // Create the process to display the send activity.
    let process = this._newProcess("sendingMessage", true);
    this._sendProcess = process;
    this.activityMgr.addActivity(process);

    // Now the one for the copy process.
    process = this._newProcess("copyMessage", false);
    this._copyProcess = process;
    this.activityMgr.addActivity(process);
  },

  onMessageSendProgress(
    aCurrentMessage,
    aTotalMessageCount,
    aMessageSendPercent,
    aMessageCopyPercent
  ) {
    if (aMessageSendPercent < 100) {
      // Ensure we are in progress...
      if (this._sendProcess.state != Ci.nsIActivityProcess.STATE_INPROGRESS) {
        this._sendProcess.state = Ci.nsIActivityProcess.STATE_INPROGRESS;
      }

      // ... and update the progress.
      this._sendProcess.setProgress(
        this._sendProcess.lastStatusText,
        aMessageSendPercent,
        100
      );
    } else if (aMessageSendPercent == 100) {
      if (aMessageCopyPercent == 0) {
        // Set send state to completed
        if (this._sendProcess.state != Ci.nsIActivityProcess.STATE_COMPLETED) {
          this._sendProcess.state = Ci.nsIActivityProcess.STATE_COMPLETED;
        }
        this._replaceProcessWithEvent(this._sendProcess);

        // Set copy state to in progress.
        if (this._copyProcess.state != Ci.nsIActivityProcess.STATE_INPROGRESS) {
          this._copyProcess.state = Ci.nsIActivityProcess.STATE_INPROGRESS;
        }

        // We don't know the progress of the copy, so just set to 0, and we'll
        // display an undetermined progress meter.
        this._copyProcess.setProgress(this._copyProcess.lastStatusText, 0, 0);
      } else if (aMessageCopyPercent >= 100) {
        // We need to set this to completed otherwise activity manager
        // complains.
        if (this._copyProcess) {
          this._copyProcess.state = Ci.nsIActivityProcess.STATE_COMPLETED;
          this.activityMgr.removeActivity(this._copyProcess.id);
          this._copyProcess = null;
        }

        this._sendProcess = null;
      }
    }
  },

  onMessageSendError(aCurrentMessage, aMessageHeader, aStatus, aMsg) {
    if (
      this._sendProcess &&
      this._sendProcess.state != Ci.nsIActivityProcess.STATE_COMPLETED
    ) {
      this._sendProcess.state = Ci.nsIActivityProcess.STATE_COMPLETED;
      this._replaceProcessWithWarning(
        this._sendProcess,
        "SendMessage",
        aStatus,
        aMsg,
        aMessageHeader
      );
      this._sendProcess = null;

      if (
        this._copyProcess &&
        this._copyProcess.state != Ci.nsIActivityProcess.STATE_COMPLETED
      ) {
        this._copyProcess.state = Ci.nsIActivityProcess.STATE_COMPLETED;
        this.activityMgr.removeActivity(this._copyProcess.id);
        this._copyProcess = null;
      }
    }
  },

  onMsgStatus(aStatusText) {
    this._sendProcess.setProgress(
      aStatusText,
      this._sendProcess.workUnitComplete,
      this._sendProcess.totalWorkUnits
    );
  },

  onStopSending(aStatus, aMsg, aTotalTried, aSuccessful) {},

  init() {
    // We should need to remove the listener as we're not being held by anyone
    // except by the send later instance.
    let sendLaterService = Cc[
      "@mozilla.org/messengercompose/sendlater;1"
    ].getService(Ci.nsIMsgSendLater);

    sendLaterService.addListener(this);

    // Also add the nsIMsgStatusFeedback object.
    let statusFeedback = Cc[
      "@mozilla.org/messenger/statusfeedback;1"
    ].createInstance(Ci.nsIMsgStatusFeedback);

    statusFeedback.setWrappedStatusFeedback(sendMsgProgressListener);

    sendLaterService.statusFeedback = statusFeedback;
  },
};