diff --git a/src/modules/FiretrayInstantMessaging.jsm b/src/modules/FiretrayInstantMessaging.jsm index c2edb12..a4d78ad 100644 --- a/src/modules/FiretrayInstantMessaging.jsm +++ b/src/modules/FiretrayInstantMessaging.jsm @@ -19,7 +19,8 @@ firetray.InstantMessaging = { } F.LOG("Enabling InstantMessaging"); - firetray.Utils.addObservers(firetray.InstantMessaging, [ // "*" for debugging + firetray.Utils.addObservers(firetray.InstantMessaging, [ + // "*" // debugging "idle-time-changed", "new-directed-incoming-message", "new-text", "new-ui-conversation", "status-changed", "unread-im-count-changed", "visited-status-resolution" @@ -38,16 +39,13 @@ firetray.InstantMessaging = { }, observe: function(subject, topic, data) { - F.WARN("RECEIVED InstantMessaging:"); + F.LOG("RECEIVED InstantMessaging: "+topic+" subject="+subject+" data="+data); switch (topic) { case "unread-im-count-changed": - F.WARN("received unread-im-count-changed: "+subject+" "+data); break; case "new-directed-incoming-message": // when PM or cited in channel: new-directed-incoming-message: [xpconnect wrapped (nsISupports, nsIClassInfo, prplIMessage)] null - F.WARN("new-directed-incoming-message: "+subject+" "+data); break; case "visited-status-resolution": - F.WARN("visited-status-resolution: "+subject+" "+data); break; case "status-changed": case "idle-time-changed": diff --git a/src/modules/FiretrayMessaging.jsm b/src/modules/FiretrayMessaging.jsm index dfd7acf..b908253 100644 --- a/src/modules/FiretrayMessaging.jsm +++ b/src/modules/FiretrayMessaging.jsm @@ -27,6 +27,7 @@ firetray.Messaging = { initialized: false, cleaningTimer: null, currentMsgCount: null, + observedTopics: {}, init: function() { if (this.initialized) { @@ -35,16 +36,14 @@ firetray.Messaging = { } F.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); - F.LOG(this.cleaningTimer+"="+FIRETRAY_DELAY_PREF_CLEANING_MILLISECONDS); + firetray.Utils.addObservers(firetray.Messaging, [ "account-added", + "account-removed"]); let that = this; MailServices.mailSession.AddFolderListener(that.mailSessionListener, that.mailSessionListener.notificationFlags); - if (Services.prefs.getBoolPref("mail.chat.enabled")) + if (Services.prefs.getBoolPref("mail.chat.enabled") && this.existsIMAccount()) firetray.InstantMessaging.init(); this.initialized = true; @@ -54,14 +53,44 @@ firetray.Messaging = { if (!this.initialized) return; F.LOG("Disabling Messaging"); + firetray.InstantMessaging.shutdown(); + MailServices.mailSession.RemoveFolderListener(this.mailSessionListener); firetray.Handler.setIconImageDefault(); - this.cleaningTimer.cancel(); + Services.obs.removeAllObservers(firetray.Messaging); this.initialized = false; }, + existsIMAccount: function() { + let accounts = new this.Accounts(); + for (let accountServer in accounts) + if (accountServer.type === 'im') { + F.LOG("found im server: "+accountServer.prettyName); + return true; + } + + return false; + }, + + observe: function(subject, topic, data) { + F.LOG("RECEIVED Messaging: "+topic+" subject="+subject+" data="+data); + switch (topic) { + case "account-removed": + this.cleanExcludedAccounts(); + if (subject.QueryInterface(Ci.imIAccount) && !this.existsIMAccount()) + firetray.InstantMessaging.shutdown(); + break; + case "account-added": + if (subject.QueryInterface(Ci.imIAccount) && !firetray.InstantMessaging.initialized) + firetray.InstantMessaging.init(); + break; + default: + F.WARN("unhandled topic: "+topic); + } + }, + /* removes removed accounts from excludedAccounts pref. NOTE: Can't be called at shutdown because MailServices.accounts no longer available */ cleanExcludedAccounts: function() {