[Pkg-mozext-commits] [firetray] 50/399: refactoring: use Accounts iterator

David Prévot taffit at alioth.debian.org
Tue Oct 29 18:23:12 UTC 2013


This is an automated email from the git hooks/post-receive script.

taffit pushed a commit to branch dfsg-clean
in repository firetray.

commit acbbff4f7ba9456b91e8524a9304ca14b4192eb0
Author: foudfou <foudil.newbie+git at gmail.com>
Date:   Fri Sep 23 00:41:02 2011 +0200

    refactoring: use Accounts iterator
---
 src/chrome/content/options.js |   22 ++++++--------
 src/modules/MoztMessaging.jsm |   64 ++++++++++++++++++++++++++++++++---------
 2 files changed, 58 insertions(+), 28 deletions(-)

diff --git a/src/chrome/content/options.js b/src/chrome/content/options.js
index 5e5dd3b..f5fcb47 100644
--- a/src/chrome/content/options.js
+++ b/src/chrome/content/options.js
@@ -29,27 +29,20 @@ mozt.UIOptions = {
     // the DOM parent where we do appendChild
     let targetNode = document.getElementById(parentId);
 
-    // accounts_to_exclude preference is a stringified Array containing the
-    // keys of the accounts to exclude
-    let accountsExcluded = mozt.Utils.prefService
-      .getCharPref('accounts_to_exclude').split(',');
-
     // TODO: sort servers by type, name
-    let accounts = MailServices.accounts.accounts;
-    for (let i = 0; i < accounts.Count(); i++) {
-      let account = accounts.QueryElementAt(i, Ci.nsIMsgAccount);
-      let accountServer = account.incomingServer;
-      if (mozt.Messaging.SERVER_TYPES_EXCLUDED.indexOf(accountServer.type) >= 0)
-        continue;
+    let exclCond = function(account) {
+      return (mozt.Messaging.SERVER_TYPES_EXCLUDED.indexOf(account.type) >= 0);
+    };
 
+    let accounts = new mozt.Messaging.Accounts(exclCond);
+    for (let accountServer in accounts) {
       let nodeAccount = document.createElement("checkbox");
       let accountServerKey = accountServer.key.toString();
       nodeAccount.setAttribute('id', accountServerKey);
       nodeAccount.setAttribute('label', accountServer.rootFolder.name);
       nodeAccount.setAttribute('checked',
-                               (accountsExcluded.indexOf(accountServerKey) >= 0));
-      nodeAccount.setAttribute(
-        'oncommand',
+        (mozt.Messaging.getPrefAccountsExcluded().indexOf(accountServerKey) >= 0));
+      nodeAccount.setAttribute('oncommand',
         'mozt.UIOptions.updateMailAccountsExcluded(mozt.UIOptions.accountBoxId)');
       targetNode.appendChild(nodeAccount);
     }
@@ -67,6 +60,7 @@ mozt.UIOptions = {
         prefValue.push(targetNode.childNodes[i].getAttribute('id'));
     }
 
+    LOG("accounts_to_exclude:"+prefValue);
     mozt.Utils.prefService.setCharPref('accounts_to_exclude', prefValue.toString());
 
     mozt.Messaging.updateUnreadMsgCount();
diff --git a/src/modules/MoztMessaging.jsm b/src/modules/MoztMessaging.jsm
index 4b3e2ec..7545571 100644
--- a/src/modules/MoztMessaging.jsm
+++ b/src/modules/MoztMessaging.jsm
@@ -28,6 +28,7 @@ if ("undefined" == typeof(mozt)) {
   var mozt = {};
 };
 
+
 mozt.Messaging = {
   // TODO: turn into pref
   SERVER_TYPES_EXCLUDED: ["nntp","rss","movemail"], // keep "pop3","imap","none"
@@ -65,7 +66,7 @@ mozt.Messaging = {
      * @param oldFlag: Old header flag (long).
      * @param newFlag: New header flag (long).
      */
-    // TODO: check count correctly updated if folder/account creation/deletion
+    // TODO: check if count correctly updated if folder/account creation/deletion
     OnItemIntPropertyChanged: function(folder, property, oldValue, newValue) {
       if (property.toString() === "TotalUnreadMessages" &&
           !(folder.flags & FLDR_UNINTERESTING)) {
@@ -76,6 +77,14 @@ mozt.Messaging = {
   },
 
   /**
+   * gets the accounts_to_exclude preference which is a stringified Array
+   * containing the keys of the accounts to exclude
+   */
+  getPrefAccountsExcluded: function() {
+    return mozt.Utils.prefService.getCharPref('accounts_to_exclude').split(',') || [];
+  },
+
+  /**
    * computes total unread message count
    * TODO: check news accounts shouldn't be considered
    */
@@ -84,19 +93,14 @@ mozt.Messaging = {
 
     this._unreadMsgCount = 0;   // reset
     try {
-      let accountsExcluded = mozt.Utils.prefService
-        .getCharPref('accounts_to_exclude').split(',');
-
-      let accounts = MailServices.accounts.accounts;
-      for (let i = 0; i < accounts.Count(); i++) {
-        let account = accounts.QueryElementAt(i, Ci.nsIMsgAccount);
-        let accountServer = account.incomingServer;
-        LOG("ACCOUNT: "+account.incomingServer.prettyName+" type: "+accountServer.type);
-        if ( (this.SERVER_TYPES_EXCLUDED.indexOf(accountServer.type) >= 0)
-           || (accountsExcluded.indexOf(accountServer.key) >= 0) )
-          continue;
-
-        let rootFolder = account.incomingServer.rootFolder; // nsIMsgFolder
+      let exclCond = function(account) {
+        return ( (mozt.Messaging.SERVER_TYPES_EXCLUDED.indexOf(account.type) >= 0)
+          || (mozt.Messaging.getPrefAccountsExcluded().indexOf(account.key) >= 0) );
+      };
+
+      let accounts = new this.Accounts(exclCond);
+      for (let accountServer in accounts) {
+        let rootFolder = accountServer.rootFolder; // nsIMsgFolder
         if (rootFolder.hasSubFolders) {
           let subFolders = rootFolder.subFolders; // nsIMsgFolder
           while(subFolders.hasMoreElements()) {
@@ -127,6 +131,38 @@ mozt.Messaging = {
       ERROR("negative unread messages' count ?"); // should never happen
       throw "negative message count"; // should never happen
     }
+
+  },
+
+  /**
+   * Accounts constructor for iterating over account servers
+   * @param exclusionCondition: a function which expresses a condition for excluding accounts
+   */
+  Accounts: function(exclusionCondition) {
+    if (typeof(exclusionCondition) == "undefined") {
+      this.exclusionCondition = function(){return false;};
+      return;
+    } else if (typeof(exclusionCondition) != "function") {
+      throw "arg must be a function";
+      return;
+    } else
+      this.exclusionCondition = exclusionCondition;
   }
 
 };
+
+/**
+ * make Accounts a Iterator/Generator
+ */
+mozt.Messaging.Accounts.prototype.__iterator__ = function() {
+  let accounts = MailServices.accounts.accounts;
+  for (let i = 0; i < accounts.Count(); i++) {
+    let account = accounts.QueryElementAt(i, Ci.nsIMsgAccount);
+    let accountServer = account.incomingServer;
+    LOG("ACCOUNT: "+accountServer.prettyName+" type: "+accountServer.type);
+    if ( this.exclusionCondition.call(this, accountServer) )
+      continue;
+
+     yield accountServer;
+  }
+}

-- 
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-mozext/firetray.git



More information about the Pkg-mozext-commits mailing list