summaryrefslogtreecommitdiffstats
path: root/mailnews/base/src/newMailNotificationService.js
blob: 4fe72f5543414134f692cdbe7ae87670a92d937c (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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
/* -*- Mode: Java; 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/. */

/* platform-independent code to count new and unread messages and pass the information to
 * platform-specific notification modules
 *
 * Logging for this module uses the TB version of log4moz. Default logging is at the Warn
 * level. Other possibly interesting messages are at Error, Info and Debug. To configure, set the
 * preferences "mail.notification.logging.console" (for the error console) or
 * "mail.notification.logging.dump" (for stderr) to the string indicating the level you want.
 */

var Cc = Components.classes;
var Ci = Components.interfaces;
var Cu = Components.utils;

Cu.import("resource:///modules/gloda/log4moz.js");
Cu.import("resource:///modules/iteratorUtils.jsm");
Cu.import("resource:///modules/mailServices.js");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");

var NMNS = Ci.mozINewMailNotificationService;

var countInboxesPref = "mail.notification.count.inbox_only";
// Old name for pref
var countNewMessagesPref = "mail.biff.use_new_count_in_mac_dock";
// When we go cross-platform we should migrate to
// const countNewMessagesPref = "mail.notification.count.new";

// Helper function to retrieve a boolean preference with a default
function getBoolPref(pref, defaultValue) {
  try {
    return Services.prefs.getBoolPref(pref);
  }
  catch(e) {
    return defaultValue;
  }
}


// constructor
function NewMailNotificationService() {
  this._mUnreadCount = 0;
  this._mNewCount = 0;
  this._listeners = [];
  this.wrappedJSObject = this;

  this._log = Log4Moz.getConfiguredLogger("mail.notification",
                                          Log4Moz.Level.Warn,
                                          Log4Moz.Level.Warn,
                                          Log4Moz.Level.Warn);

  // Listen for mail-startup-done to do the rest of our setup after folders are initialized
  Services.obs.addObserver(this, "mail-startup-done", false);
}

NewMailNotificationService.prototype = {
  classDescription: "Maintain counts of new and unread messages",
  classID:              Components.ID("{740880E6-E299-4165-B82F-DF1DCAB3AE22}"),
  contractID:           "@mozilla.org/newMailNotificationService;1",
  QueryInterface:       XPCOMUtils.generateQI([Ci.nsIObserver, Ci.nsIFolderListener, Ci.mozINewMailNotificationService]),
  _xpcom_factory: XPCOMUtils.generateSingletonFactory(NewMailNotificationService),
  
  _mUnreadCount: 0,
  _mNewCount: 0,
  _listeners: null,
  _log: null,

  get countNew() {
    return getBoolPref(countNewMessagesPref, false);
  },

  observe: function NMNS_Observe(aSubject, aTopic, aData) {
    // Set up to catch updates to unread count
    this._log.info("NMNS_Observe: " + aTopic);

    try {
      if (aTopic == "mail-startup-done") {
        try {
          Services.obs.removeObserver(this, "mail-startup-done");
        }
        catch (e) {
          this._log.error("NMNS_Observe: unable to deregister mail-startup-done listener: " + e);
        }
        Services.obs.addObserver(this, "profile-before-change", false);
        MailServices.mailSession.AddFolderListener(this, Ci.nsIFolderListener.intPropertyChanged |
                                                         Ci.nsIFolderListener.added |
                                                         Ci.nsIFolderListener.removed |
                                                         Ci.nsIFolderListener.propertyFlagChanged);
        this._initUnreadCount();
      }
      else if (aTopic == "profile-before-change") {
        try {
          MailServices.mailSession.RemoveFolderListener(this);
          Services.obs.removeObserver(this, "profile-before-change");
        }
        catch (e) {
          this._log.error("NMNS_Observe: unable to deregister listeners at shutdown: " + e);
        }
      }
    } catch (error) {
      this._log.error("NMNS_Observe failed: " + error);
    }
  },

  _initUnreadCount: function NMNS_initUnreadCount() {
    let total = 0;
    let allServers = MailServices.accounts.allServers;
    for (let i = 0; i < allServers.length; i++) {
      let currentServer = allServers.queryElementAt(i, Ci.nsIMsgIncomingServer);
      this._log.debug("NMNS_initUnread: server " + currentServer.prettyName + " type " + currentServer.type);
      // Don't bother counting RSS or NNTP servers
      let type = currentServer.type;
      if (type == "rss" || type == "nntp")
        continue;

      let rootFolder = currentServer.rootFolder;
      if (rootFolder) {
        total += this._countUnread(rootFolder);
      }
    }
    this._mUnreadCount = total;
    if (!this.countNew) {
      this._log.info("NMNS_initUnread notifying listeners: " + total + " total unread messages");
      this._notifyListeners(NMNS.count, "onCountChanged", total);
    }
  },

  // Count all the unread messages below the given folder
  _countUnread: function NMNS_countUnread(folder) {
    this._log.trace("NMNS_countUnread: parent folder " + folder.URI);
    let unreadCount = 0;

    if (this.confirmShouldCount(folder)) {
      let count = folder.getNumUnread(false);
      this._log.debug("NMNS_countUnread: folder " + folder.URI + ", " + count + " unread");
      if (count > 0)
        unreadCount += count;
    }

    let allFolders = folder.descendants;
    for (let folder in fixIterator(allFolders, Ci.nsIMsgFolder)) {
      if (this.confirmShouldCount(folder)) {
        let count = folder.getNumUnread(false);
        this._log.debug("NMNS_countUnread: folder " + folder.URI + ", " + count + " unread");
        if (count > 0)
          unreadCount += count;
      }
    }
    return unreadCount;
  },

  // Filter out special folders and then ask for observers to see if
  // we should monitor unread messages in this folder
  confirmShouldCount: function NMNS_confirmShouldCount(aFolder) {
    let shouldCount = Cc['@mozilla.org/supports-PRBool;1'].createInstance(Ci.nsISupportsPRBool);
    shouldCount.data = true;
    this._log.trace("NMNS_confirmShouldCount: folder " + aFolder.URI + " flags " + aFolder.flags);
    let srv = null;

    // If it's not a mail folder we don't count it by default
    if (!(aFolder.flags & Ci.nsMsgFolderFlags.Mail))
      shouldCount.data = false;

    // For whatever reason, RSS folders have the 'Mail' flag
    else if ((srv = aFolder.server) && (srv.type == "rss"))
      shouldCount.data = false;

    // If it's a special folder *other than the inbox* we don't count it by default
    else if ((aFolder.flags & Ci.nsMsgFolderFlags.SpecialUse)
        && !(aFolder.flags & Ci.nsMsgFolderFlags.Inbox))
      shouldCount.data = false;

    else if (aFolder.flags & Ci.nsMsgFolderFlags.Virtual)
      shouldCount.data = false;

    // if we're only counting inboxes and it's not an inbox...
    else
      try {
      // If we can't get this pref, just leave it as the default
      let onlyCountInboxes = Services.prefs.getBoolPref(countInboxesPref);
      if (onlyCountInboxes && !(aFolder.flags & Ci.nsMsgFolderFlags.Inbox))
        shouldCount.data = false;
      } catch (error) {}

    this._log.trace("NMNS_confirmShouldCount: before observers " + shouldCount.data);
    Services.obs.notifyObservers(shouldCount, "before-count-unread-for-folder", aFolder.URI);
    this._log.trace("NMNS_confirmShouldCount: after observers " + shouldCount.data);

    return shouldCount.data;
  },

  OnItemIntPropertyChanged: function NMNS_OnItemIntPropertyChanged(folder, property, oldValue, newValue) {
    try {
      if (property == "FolderSize")
        return;
      this._log.trace("NMNS_OnItemIntPropertyChanged: folder " + folder.URI + " " + property + " " + oldValue + " " + newValue);
      if (property == "BiffState") {
        this._biffStateChanged(folder, oldValue, newValue);
      }
      else if (property == "TotalUnreadMessages") {
        this._updateUnreadCount(folder, oldValue, newValue);
      }
      else if (property == "NewMailReceived") {
        this._newMailReceived(folder, oldValue, newValue);
      }
    } catch (error) {
      this._log.error("NMNS_OnItemIntPropertyChanged: exception " + error);
    }
  },

  _biffStateChanged: function NMNS_biffStateChanged(folder, oldValue, newValue) {
    if (newValue == Ci.nsIMsgFolder.nsMsgBiffState_NewMail) {
      if (folder.server && !folder.server.performingBiff) {
        this._log.debug("NMNS_biffStateChanged: folder " + folder.URI + " notified, but server not performing biff");
        return;
      }

      // Biff notifications come in for the top level of the server, we need to look for
      // the folder that actually contains the new mail

      let allFolders = folder.descendants;
      let numFolders = allFolders.length;

      this._log.trace("NMNS_biffStateChanged: folder " + folder.URI + " New mail, " + numFolders + " subfolders");
      let newCount = 0;

      if (this.confirmShouldCount(folder)) {
        let folderNew = folder.getNumNewMessages(false);
        this._log.debug("NMNS_biffStateChanged: folder " + folder.URI + " new messages: " + folderNew);
        if (folderNew > 0)
          newCount += folderNew;
      }

      for (let folder in fixIterator(allFolders, Ci.nsIMsgFolder)) {
        if (this.confirmShouldCount(folder)) {
          let folderNew = folder.getNumNewMessages(false);
          this._log.debug("NMNS_biffStateChanged: folder " + folder.URI + " new messages: " + folderNew);
          if (folderNew > 0)
            newCount += folderNew;
        }
      }
      if (newCount > 0) {
        this._mNewCount += newCount;
        this._log.debug("NMNS_biffStateChanged: " + folder.URI + " New mail count " + this._mNewCount);
        if (this.countNew)
          this._notifyListeners(NMNS.count, "onCountChanged", this._mNewCount);
      }
    }
    else if (newValue == Ci.nsIMsgFolder.nsMsgBiffState_NoMail) {
      // Dodgy - when any folder tells us it has no mail, clear all unread mail
      this._mNewCount = 0;
      this._log.debug("NMNS_biffStateChanged: " + folder.URI + " New mail count 0");
      if (this.countNew)
        this._notifyListeners(NMNS.count, "onCountChanged", this._mNewCount);
    }
  },

  _newMailReceived: function NMNS_newMailReceived(folder, oldValue, newValue) {
    if (!this.confirmShouldCount(folder))
      return;

    if (!oldValue || (oldValue < 0))
      oldValue = 0;
    let oldTotal = this._mNewCount;
    this._mNewCount += (newValue - oldValue);
    this._log.debug("NMNS_newMailReceived: " + folder.URI +
                    " Old folder " + oldValue + " New folder " + newValue +
                    " Old total " + oldTotal + " New total " + this._mNewCount);
    if (this.countNew)
      this._notifyListeners(NMNS.count, "onCountChanged", this._mNewCount);
  },

  _updateUnreadCount: function NMNS_updateUnreadCount(folder, oldValue, newValue) {
    if (!this.confirmShouldCount(folder))
      return;

    // treat "count unknown" as zero
    if (oldValue < 0)
      oldValue = 0;
    if (newValue < 0)
      newValue = 0;

    this._mUnreadCount += (newValue - oldValue);
    if (!this.countNew) {
      this._log.info("NMNS_updateUnreadCount notifying listeners: unread count " + this._mUnreadCount);
      this._notifyListeners(NMNS.count, "onCountChanged", this._mUnreadCount);
    }
  },

  OnItemAdded: function NMNS_OnItemAdded(parentItem, item) {
    if (item instanceof Ci.nsIMsgDBHdr) {
      if (this.confirmShouldCount(item.folder)) {
        this._log.trace("NMNS_OnItemAdded: item " + item.folder.getUriForMsg(item) + " added to " + item.folder.folderURL);
      }
    }
  },

  OnItemPropertyFlagChanged: function NMNS_OnItemPropertyFlagChanged(item,
                                                                    property,
                                                                    oldFlag,
                                                                    newFlag) {
    if (item instanceof Ci.nsIMsgDBHdr) {
      if ((oldFlag & Ci.nsMsgMessageFlags.New)
          && !(newFlag & Ci.nsMsgMessageFlags.New)) {
        this._log.trace("NMNS_OnItemPropertyFlagChanged: item " + item.folder.getUriForMsg(item) + " marked read");
      }
      else if (newFlag & Ci.nsMsgMessageFlags.New) {
        this._log.trace("NMNS_OnItemPropertyFlagChanged: item " + item.folder.getUriForMsg(item) + " marked unread");
      }
    }
  },

  OnItemRemoved: function NMNS_OnItemRemoved(parentItem, item) {
    if (item instanceof Ci.nsIMsgDBHdr && !item.isRead) {
      this._log.trace("NMNS_OnItemRemoved: unread item " + item.folder.getUriForMsg(item) + " removed from " + item.folder.folderURL);
    }
  },
  

  // Implement mozINewMailNotificationService

  get messageCount() {
    if (this.countNew)
      return this._mNewCount;
    return this._mUnreadCount;
  },

  addListener: function NMNS_addListener(aListener, flags) {
    this._log.trace("NMNS_addListener: listener " + aListener.toSource + " flags " + flags);
    for (let i = 0; i < this._listeners.length; i++) {
      let l = this._listeners[i];
      if (l.obj === aListener) {
        l.flags = flags;
        return;
      }
    }
    // If we get here, the listener wasn't already in the list
    this._listeners.push({obj: aListener, flags: flags});
  },

  removeListener: function NMNS_removeListener(aListener) {
    this._log.trace("NMNS_removeListener: listener " + aListener.toSource);
    for (let i = 0; i < this._listeners.length; i++) {
      let l = this._listeners[i];
      if (l.obj === aListener) {
        this._listeners.splice(i, 1);
        return;
      }
    }
  },

  _listenersForFlag: function NMNS_listenersForFlag(flag) {
    this._log.trace("NMNS_listenersForFlag " + flag + " length " + this._listeners.length + " " + this._listeners.toSource());
    let list = [];
    for (let i = 0; i < this._listeners.length; i++) {
      let l = this._listeners[i];
      if (l.flags & flag) {
        list.push(l.obj);
      }
    }
    return list;
  },

  _notifyListeners: function NMNS_notifyListeners(flag, func, value) {
    let list = this._listenersForFlag(flag);
    for (let i = 0; i < list.length; i++) {
      this._log.debug("NMNS_notifyListeners " + flag + " " + func + " " + value);
      list[i][func].call(list[i], value);
    }
  }
};

var NSGetFactory = XPCOMUtils.generateNSGetFactory([NewMailNotificationService]);