From 756367230cd6c20fa3f28c96019c9869ad45a2dc Mon Sep 17 00:00:00 2001 From: foudfou Date: Tue, 20 Mar 2012 22:06:19 +0100 Subject: [PATCH] periodically clean prefs by removing removed accounts from excludedAccounts --- src/modules/FiretrayMessaging.jsm | 47 ++++++++++++++++++++++++++++--- src/modules/commons.js | 3 ++ 2 files changed, 46 insertions(+), 4 deletions(-) diff --git a/src/modules/FiretrayMessaging.jsm b/src/modules/FiretrayMessaging.jsm index 2728eae..b2928a6 100644 --- a/src/modules/FiretrayMessaging.jsm +++ b/src/modules/FiretrayMessaging.jsm @@ -23,14 +23,20 @@ const FLDRS_UNINTERESTING = { firetray.Messaging = { initialized: false, + cleaningTimer: null, init: function() { if (this.initialized) { - firetray.WARN("Messaging already initialized"); + firetray.LOG("Messaging already initialized"); return; } firetray.LOG("Enabling Messaging"); + // there is no means to detect account-removed event + this.cleaningTimer = firetray.Utils.timer(firetray.Messaging.cleanExcludedAccounts, + FIRETRAY_DELAY_PREF_CLEANING_MILLISECONDS, Ci.nsITimer.TYPE_REPEATING_SLACK); + firetray.WARN(this.cleaningTimer+"="+FIRETRAY_DELAY_PREF_CLEANING_MILLISECONDS); + let that = this; MailServices.mailSession.AddFolderListener(that.mailSessionListener, that.mailSessionListener.notificationFlags); @@ -43,15 +49,48 @@ firetray.Messaging = { return; firetray.LOG("Disabling Messaging"); + this.cleaningTimer.cancel(); + MailServices.mailSession.RemoveFolderListener(this.mailSessionListener); firetray.Handler.setIconImageDefault(); this.initialized = false; }, - /** - * http://mxr.mozilla.org/comm-central/source/mailnews/base/public/nsIFolderListener.idl - */ + /* removes removed accounts from excludedAccounts pref. NOTE: Can't be called + at shutdown because MailServices.accounts no longer available */ + cleanExcludedAccounts: function() { + try { + firetray.LOG("* cleaning *"); + let mailAccounts = firetray.Utils.getObjPref('mail_accounts'); + let excludedAccounts = mailAccounts["excludedAccounts"]; + + // build current list of account server keys + let accounts = MailServices.accounts.accounts; + let accountServerKeys = []; + for (let i=0, len=accounts.Count(); i= 0) + newExcludedAccounts.push(excludedAccount); + else + cleaningNeeded += 1; + } + + if (cleaningNeeded) { + firetray.LOG("cleaning excluded accounts"); + let prefObj = {"serverTypes":mailAccounts["serverTypes"], "excludedAccounts":newExcludedAccounts}; + firetray.Utils.setObjPref('mail_accounts', prefObj); + } + } catch(x) { firetray.ERROR(x); } + }, + + /* http://mxr.mozilla.org/comm-central/source/mailnews/base/public/nsIFolderListener.idl */ mailSessionListener: { notificationFlags: // Ci.nsIFolderListener.propertyChanged | diff --git a/src/modules/commons.js b/src/modules/commons.js index d45ace7..9230421 100644 --- a/src/modules/commons.js +++ b/src/modules/commons.js @@ -8,6 +8,7 @@ var EXPORTED_SYMBOLS = "FIRETRAY_NOTIFICATION_NEWMAIL_ICON", "FIRETRAY_NOTIFICATION_CUSTOM_ICON", "FIRETRAY_DELAY_BROWSER_STARTUP_MILLISECONDS", "FIRETRAY_DELAY_NOWAIT_MILLISECONDS", "FIRETRAY_MESSAGE_COUNT_TYPE_UNREAD", + "FIRETRAY_DELAY_PREF_CLEANING_MILLISECONDS", "FIRETRAY_MESSAGE_COUNT_TYPE_NEW" ]; const Cc = Components.classes; @@ -26,6 +27,7 @@ const FIRETRAY_NOTIFICATION_CUSTOM_ICON = 3; const FIRETRAY_DELAY_BROWSER_STARTUP_MILLISECONDS = 500; const FIRETRAY_DELAY_NOWAIT_MILLISECONDS = 0; +const FIRETRAY_DELAY_PREF_CLEANING_MILLISECONDS = 15*60*1000; const FIRETRAY_MESSAGE_COUNT_TYPE_UNREAD = 0; const FIRETRAY_MESSAGE_COUNT_TYPE_NEW = 1; @@ -174,6 +176,7 @@ firetray.Utils = { var timer = Cc["@mozilla.org/timer;1"].createInstance(Ci.nsITimer); timer.initWithCallback({ notify: callback }, delay, timerType); + return timer; }, tryCloseLibs: function(libs) {