<commit_before>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

(function (aGlobal) {
  var Ci = Components.interfaces;
  var Cc = Components.classes;
  const Cu = Components.utils;
  var { Promise } = Cu.import("resource://gre/modules/Promise.jsm", {});

  var tbBug766495 = {
    collectDraftFolders: function() {
      return new Promise((function(aResolve, aReject) {
        var accountManager = MailServices.accounts;
        var allServers = accountManager.allServers;
        var folders = [];
        var expectedFoldersCount = 0;
        for (var i = 0,  maxi = allServers.length; i < maxi; ++i) {
          let currentServer = allServers.queryElementAt(i, Ci.nsIMsgIncomingServer);
          if (currentServer.type == "none")
            continue;

          var folderURI = currentServer.rootFolder.server.serverURI + "/" + "Drafts";
          var rdf = Cc['@mozilla.org/rdf/rdf-service;1']
                .getService(Ci.nsIRDFService);
          var folder = rdf.GetResource(folderURI).QueryInterface(Ci.nsIMsgFolder);
          expectedFoldersCount++;
          this.ensureUpdated(folder).then(function(folder) {
            folders.push(folder);
            if (folders.length >= expectedFoldersCount) {
              aResolve(folders);
            }
          });
        }
        if (expectedFoldersCount == 0) {
          aResolve(folders);
        }
      }).bind(this));
    },
    ensureUpdated: function(aFolder) {
      if (aFolder instanceof Ci.nsIMsgImapMailFolder) {
        return new Promise((function(aResolve, aReject) {
          var listener = this.createURLListenerForFolder(function () {
            aResolve(aFolder);
          });
          var imapDraftFolder = aFolder.QueryInterface(Ci.nsIMsgImapMailFolder);
          aFolder.updateFolderWithListener(msgWindow, listener);
        }).bind(this));
      } else {
        return Promise.resolve(aFolder);
      }
    },
    // nsIUrlListener
    createURLListenerForFolder: function createURLListenerForFolder(aCallBack) {
      return {
        OnStartRunningUrl: function OnStartRunningUrl(aURL) {
        },
        OnStopRunningUrl: function OnStopRunningUrl(aURL, aExitCode) {
          aCallBack();
        },
      };
    }
  };

  document.addEventListener("DOMContentLoaded", function onDOMContentLoaded(aEvent) {
    document.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
    tbBug766495.collectDraftFolders();
  });
  aGlobal.tbBug766495 = tbBug766495;
})(this);
<commit_msg>Count draft folder message(s) at startup<commit_after>/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

(function (aGlobal) {
  var Ci = Components.interfaces;
  var Cc = Components.classes;
  const Cu = Components.utils;
  var { Promise } = Cu.import("resource://gre/modules/Promise.jsm", {});

  var tbBug766495 = {
    collectDraftFolders: function() {
      return new Promise((function(aResolve, aReject) {
        var accountManager = MailServices.accounts;
        var allServers = accountManager.allServers;
        var folders = [];
        var expectedFoldersCount = 0;
        for (var i = 0,  maxi = allServers.length; i < maxi; ++i) {
          let currentServer = allServers.queryElementAt(i, Ci.nsIMsgIncomingServer);
          if (currentServer.type == "none")
            continue;

          var folderURI = currentServer.rootFolder.server.serverURI + "/" + "Drafts";
          var rdf = Cc['@mozilla.org/rdf/rdf-service;1']
                .getService(Ci.nsIRDFService);
          var folder = rdf.GetResource(folderURI).QueryInterface(Ci.nsIMsgFolder);
          expectedFoldersCount++;
          this.ensureUpdated(folder).then(function(folder) {
            folders.push(folder);
            if (folders.length >= expectedFoldersCount) {
              aResolve(folders);
            }
          });
        }
        if (expectedFoldersCount == 0) {
          aResolve(folders);
        }
      }).bind(this));
    },
    ensureUpdated: function(aFolder) {
      if (aFolder instanceof Ci.nsIMsgImapMailFolder) {
        return new Promise((function(aResolve, aReject) {
          var listener = this.createURLListenerForFolder(function () {
            aResolve(aFolder);
          });
          var imapDraftFolder = aFolder.QueryInterface(Ci.nsIMsgImapMailFolder);
          aFolder.updateFolderWithListener(msgWindow, listener);
        }).bind(this));
      } else {
        return Promise.resolve(aFolder);
      }
    },
    // nsIUrlListener
    createURLListenerForFolder: function createURLListenerForFolder(aCallBack) {
      return {
        OnStartRunningUrl: function OnStartRunningUrl(aURL) {
        },
        OnStopRunningUrl: function OnStopRunningUrl(aURL, aExitCode) {
          aCallBack();
        },
      };
    }
  };

  document.addEventListener("DOMContentLoaded", function onDOMContentLoaded(aEvent) {
    document.removeEventListener("DOMContentLoaded", onDOMContentLoaded);
    tbBug766495.collectDraftFolders().then(function(folders) {
      var draftMessagesCount = 0;
      folders.forEach(function(folder) {
        draftMessagesCount += folder.getTotalMessages(true);
      });
    });
  });
  aGlobal.tbBug766495 = tbBug766495;
})(this);

{'fraction_non_alphanumeric': 0.11361570623522996, 'fraction_numerical': 0.011816033448463915, 'mean_word_length': 2.631683168316832, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6173140', 'n_tokens_mistral': 1671, 'n_tokens_neox': 1625, 'n_words': 317}