1
0
mirror of https://github.com/moparisthebest/FireTray synced 2024-11-11 03:25:07 -05:00

refactoring + make server_type a preference

This commit is contained in:
foudfou 2011-10-29 22:27:55 +02:00
parent b6a54fb005
commit aa9e13a366
3 changed files with 49 additions and 54 deletions

View File

@ -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

View File

@ -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;

View File

@ -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) {