diff --git a/src/chrome/content/options.js b/src/chrome/content/options.js index 63835a3..7ab6d70 100644 --- a/src/chrome/content/options.js +++ b/src/chrome/content/options.js @@ -56,9 +56,10 @@ firetray.UIOptions = { populateMailAccountTypes: function() { let targetTree = document.getElementById("ui_mail_account_types"); + let serverTypes = firetray.Utils.getObjPref('server_types'); - for (t in firetray.Messaging.SERVER_TYPES) { - let accType = firetray.Messaging.SERVER_TYPES[t]; + for (t in serverTypes) { + let accType = serverTypes[t]; let item = document.createElement('treeitem'); let row = document.createElement('treerow'); @@ -84,9 +85,10 @@ firetray.UIOptions = { // the DOM parent where we do appendChild let targetNode = document.getElementById(parentId); + let serverTypes = firetray.Utils.getObjPref('server_types'); let accounts = new firetray.Messaging.Accounts(true); for (let accountServer in accounts) { - if (firetray.Messaging.SERVER_TYPES[accountServer.type].excluded) + if (serverTypes[accountServer.type].excluded) continue; let nodeAccount = document.createElement("checkbox"); @@ -94,7 +96,7 @@ firetray.UIOptions = { nodeAccount.setAttribute('id', accountServerKey); nodeAccount.setAttribute('label', accountServer.rootFolder.name); nodeAccount.setAttribute('checked', - (firetray.Messaging.getPrefAccountsExcluded().indexOf(accountServerKey) >= 0)); + (firetray.Utils.getArrayPref('accounts_to_exclude').indexOf(accountServerKey) >= 0)); let that = this; nodeAccount.addEventListener('command', function(e){ that.updateMailAccountsExcluded(that.accountBoxId);}, true); @@ -115,7 +117,7 @@ firetray.UIOptions = { } LOG("accounts_to_exclude:"+prefValue); - firetray.Messaging.setPrefAccountsExcluded(prefValue); + firetray.Utils.setArrayPref('accounts_to_exclude', prefValue); firetray.Messaging.updateUnreadMsgCount(); }, @@ -130,8 +132,8 @@ firetray.UIOptions = { }, /* - * Save SERVER_TYPES to the "server_types" preference. - * This is called by the pref's system when the GUI element is altered. + * Save the "server_types" preference. This is called by the pref's system + * when the GUI element is altered. */ saveTreeServerTypes: function() { let tree = document.getElementById("ui_tree_server_types"); @@ -190,7 +192,10 @@ firetray.UIOptions = { cell.setAttribute('value',prefObj[serverTypeName].excluded); // CAUTION: removeEventListener in onQuit() cell.addEventListener( - 'DOMAttrModified', that._userChangeValueTreeServerTypes, true); + 'DOMAttrModified', function(e) { + that._userChangeValueTreeServerTypes(e); + firetray.Messaging.updateUnreadMsgCount(); + }, true); row.appendChild(cell); // server_type_name diff --git a/src/modules/FiretrayMessaging.jsm b/src/modules/FiretrayMessaging.jsm index a6d3bc4..c1b1543 100644 --- a/src/modules/FiretrayMessaging.jsm +++ b/src/modules/FiretrayMessaging.jsm @@ -30,19 +30,6 @@ if ("undefined" == typeof(firetray)) { firetray.Messaging = { - // TODO: turn into pref. - /* NOTE: definition checks not implemented on purpose (performance mainly) - should be well defined in default prefs, and new types are unlikely to - appear soon. */ - SERVER_TYPES: { - "pop3": { order: 1, excluded: false }, - "imap": { order: 1, excluded: false }, - "movemail": { order: 2, excluded: true }, - "none": { order: 3, excluded: false }, - "rss": { order: 4, excluded: true }, - "nntp": { order: 5, excluded: true } - }, - _unreadMsgCount: 0, enable: function() { @@ -85,46 +72,20 @@ firetray.Messaging = { } }, - /** - * get/set accounts_to_exclude preference which is a stringified Array - * containing the keys of the accounts to exclude - */ - getPrefAccountsExcluded: function() { - return JSON.parse( - firetray.Utils.prefService.getCharPref('accounts_to_exclude')); - }, - - setPrefAccountsExcluded: function(aArray) { - if (!isArray(aArray)) throw new TypeError(); - LOG(aArray); - firetray.Utils.prefService.setCharPref('accounts_to_exclude', - JSON.stringify(aArray)); - }, - - -// window.addEventListener('unload', function(e){filteredClipboard.saveList();}, false); // TRY DIFFERENT EVENTS -// var filteredClipboard = { -// prefix:"extensions.filteredclipboard.", -// saveList: function (){ -// var str = JSON.stringify(treeView.model); -// var prefManager = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch); -// prefManager.setCharPref(this.prefix + "jsondata", str); -// }, - - /** * computes total unread message count * TODO: check news accounts shouldn't be considered */ updateUnreadMsgCount: function() { LOG("unreadMsgCount"); + let serverTypes = firetray.Utils.getObjPref('server_types'); this._unreadMsgCount = 0; // reset try { let accounts = new this.Accounts(); for (let accountServer in accounts) { - if ( (this.SERVER_TYPES[accountServer.type].excluded) - || (this.getPrefAccountsExcluded().indexOf(accountServer.key) >= 0) ) + if ( (serverTypes[accountServer.type].excluded) + || (firetray.Utils.getArrayPref('accounts_to_exclude').indexOf(accountServer.key) >= 0) ) continue; let rootFolder = accountServer.rootFolder; // nsIMsgFolder @@ -191,13 +152,14 @@ firetray.Messaging.Accounts.prototype.__iterator__ = function() { accountServers[i] = accountServer; } + let serverTypes = firetray.Utils.getObjPref('server_types'); if (this.sortByTypeAndName) { accountServers.sort(function(a,b) { - if (firetray.Messaging.SERVER_TYPES[a.type].order - < firetray.Messaging.SERVER_TYPES[b.type].order) + if (serverTypes[a.type].order + < serverTypes[b.type].order) return -1; - if (firetray.Messaging.SERVER_TYPES[a.type].order - > firetray.Messaging.SERVER_TYPES[b.type].order) + if (serverTypes[a.type].order + > serverTypes[b.type].order) return 1; if (a.prettyName < b.prettyName) return -1; diff --git a/src/modules/commons.js b/src/modules/commons.js index 074e021..f5288a3 100644 --- a/src/modules/commons.js +++ b/src/modules/commons.js @@ -38,6 +38,34 @@ firetray.Utils = { prefService: Services.prefs.getBranch("extensions.firetray."), strings: Services.strings.createBundle("chrome://firetray/locale/overlay.properties"), + getObjPref: function(prefStr) { + try { + var objPref = JSON.parse( + firetray.Utils.prefService.getCharPref(prefStr)); + } catch (x) { + ERROR(x); + } + return objPref; + }, + setObjPref: function(prefStr, obj) { + LOG(obj); + try { + firetray.Utils.prefService.setCharPref(prefStr, JSON.stringify(obj)); + } catch (x) { + ERROR(x); + } + }, + + getArrayPref: function(prefStr) { + let arrayPref = this.getObjPref(prefStr); + if (!isArray(arrayPref)) throw new TypeError(); + return arrayPref; + }, + setArrayPref: function(prefStr, aArray) { + if (!isArray(aArray)) throw new TypeError(); + this.setObjPref(prefStr, aArray); + }, + dumpObj: function(obj) { let str = ""; for(i in obj) {