Bug 736870 - Convert mailnews/base/search/content/ to Services.jsm and MailServices.js. r=mbanner
authoraceman <acelists@atlas.sk>
Thu, 19 Apr 2012 18:57:55 -0400
changeset 11679 770e16975c842a1813435b46289c7e7738a3c965
parent 11678 6b5b713fc9ec55af56c5111f7b59a679fd4867e0
child 11680 acc7a04221a0d697a25791fbda356d1d0d38b99a
push idunknown
push userunknown
push dateunknown
reviewersmbanner
bugs736870
Bug 736870 - Convert mailnews/base/search/content/ to Services.jsm and MailServices.js. r=mbanner
mailnews/base/search/content/CustomHeaders.js
mailnews/base/search/content/FilterEditor.js
mailnews/base/search/content/searchWidgets.xml
--- a/mailnews/base/search/content/CustomHeaders.js
+++ b/mailnews/base/search/content/CustomHeaders.js
@@ -33,39 +33,29 @@
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 Components.utils.import("resource://gre/modules/Services.jsm");
 
-var gPrefs;
 var gAddButton;
 var gOKButton;
 var gRemoveButton;
 var gHeaderInputElement;
 var gArrayHdrs;
 var gHdrsList;
 var gContainer;
 var gFilterBundle=null;
 var gCustomBundle=null;
 
 function onLoad()
 {
-    gPrefs = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefBranch);
-    var hdrs;
-    try
-    {
-       hdrs = gPrefs.getCharPref("mailnews.customHeaders");
-    }
-    catch(ex)
-    {
-      hdrs =null;
-    }
+    let hdrs = Services.prefs.getCharPref("mailnews.customHeaders");
     gHeaderInputElement = document.getElementById("headerInput");
     gHeaderInputElement.focus();
 
     gHdrsList = document.getElementById("headerList");
     gArrayHdrs = new Array();
     gAddButton = document.getElementById("addButton");
     gRemoveButton = document.getElementById("removeButton");
     gOKButton = document.getElementById("ok");
@@ -122,28 +112,23 @@ function onOk()
 {
   if (gArrayHdrs.length)
   {
     var hdrs;
     if (gArrayHdrs.length == 1)
       hdrs = gArrayHdrs;
     else
       hdrs = gArrayHdrs.join(": ");
-    gPrefs.setCharPref("mailnews.customHeaders", hdrs);
+    Services.prefs.setCharPref("mailnews.customHeaders", hdrs);
     // flush prefs to disk, in case we crash, to avoid dataloss and problems with filters that use the custom headers
-    var prefService = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService);
-    prefService.savePrefFile(null);
+    Services.prefs.savePrefFile(null);
   }
   else
   {
-    try
-    {
-      gPrefs.clearUserPref("mailnews.customHeaders"); //clear the pref, no custom headers 
-    }
-    catch(ex) {}  //will throw an exception if there is no "mailnews.customHeaders" in prefs.js
+    Services.prefs.clearUserPref("mailnews.customHeaders"); //clear the pref, no custom headers
   }
   window.close();
 }
 
 function customHeaderOverflow()
 {
   var nsMsgSearchAttrib = Components.interfaces.nsMsgSearchAttrib;
   if (gArrayHdrs.length >= (nsMsgSearchAttrib.kNumMsgSearchAttributes - nsMsgSearchAttrib.OtherHeader - 1))
--- a/mailnews/base/search/content/FilterEditor.js
+++ b/mailnews/base/search/content/FilterEditor.js
@@ -37,30 +37,30 @@
  * decision by deleting the provisions above and replace them with the notice
  * and other provisions required by the GPL or the LGPL. If you do not delete
  * the provisions above, a recipient may use your version of this file under
  * the terms of any one of the MPL, the GPL or the LGPL.
  *
  * ***** END LICENSE BLOCK ***** */
 
 Components.utils.import("resource://gre/modules/Services.jsm");
+Components.utils.import("resource:///modules/mailServices.js");
 
 // The actual filter that we're editing if it is a _saved_ filter or prefill;
 // void otherwise.
 var gFilter;
 // cache the key elements we need
 var gFilterList;
 // The filter name as it appears in the "Filter Name" field of dialog.
 var gFilterNameElement;
 var gFilterContext;
 var gFilterBundle;
 var gPreFillName;
 var nsMsgSearchScope = Components.interfaces.nsMsgSearchScope;
-var gPrefBranch;
-var gMailSession = null;
+var gSessionFolderListenerAdded = false;
 var gFilterActionList;
 var gCustomActions = null;
 var gFilterType;
 
 var gFilterActionStrings = ["none", "movemessage", "setpriorityto", "deletemessage",
                             "markasread", "ignorethread", "watchthread", "markasflagged",
                             "label", "replytomessage", "forwardmessage", "stopexecution",
                             "deletefrompopserver",  "leaveonpopserver", "setjunkscore",
@@ -72,17 +72,16 @@ var nsMsgFilterAction = Components.inter
 var gFilterEditorMsgWindow = null;
 
 function filterEditorOnLoad()
 {
   getCustomActions();
   initializeSearchWidgets();
   initializeFilterWidgets();
 
-  gPrefBranch = Components.classes["@mozilla.org/preferences-service;1"].getService(Components.interfaces.nsIPrefService).getBranch(null);
   gFilterBundle = document.getElementById("bundle_filter");
 
   if ("arguments" in window && window.arguments[0])
   {
     var args = window.arguments[0];
 
     if ("filterList" in args)
     {
@@ -159,18 +158,18 @@ function filterEditorOnLoad()
 
   gFilterNameElement.select();
   // This call is required on mac and linux.  It has no effect under win32.  See bug 94800.
   gFilterNameElement.focus();
 }
 
 function filterEditorOnUnload()
 {
-  if (gMailSession)
-    gMailSession.RemoveFolderListener(gFolderListener);
+  if (gSessionFolderListenerAdded)
+    MailServices.mailSession.RemoveFolderListener(gFolderListener);
 }
 
 function onEnterInSearchTerm()
 {
   // do nothing.  onOk() will get called since this is a dialog
 }
 
 function onAccept()
@@ -339,19 +338,17 @@ function saveFilter()
 
     // the term might be an offscreen one that we haven't initialized yet
     let searchTerm = obj.searchTerm;
     if (!searchTerm && !gSearchTerms[index].initialized)
       continue;
 
     if (isNaN(obj.searchattribute.value)) // is this a custom term?
     {
-      let filterService = Components.classes["@mozilla.org/messenger/services/filters;1"]
-          .getService(Components.interfaces.nsIMsgFilterService);
-      let customTerm = filterService.getCustomTerm(obj.searchattribute.value);
+      let customTerm = MailServices.filters.getCustomTerm(obj.searchattribute.value);
       if (!customTerm)
       { 
         allValid = false;
         Components.utils.reportError("filter not saved because custom term not found");
       }
       else
       {
         allValid = customTerm.getAvailable(obj.searchScope, obj.searchattribute.value);
@@ -529,28 +526,27 @@ function SearchNewFolderOkCallback(name,
   var imapFolder = null;
   try
   {
     imapFolder = msgFolder.QueryInterface(Components.interfaces.nsIMsgImapMailFolder);
   }
   catch(ex) {}
   if (imapFolder) //imapFolder creation is asynchronous.
   {
-    if (!gMailSession)
-      gMailSession = Components.classes["@mozilla.org/messenger/services/session;1"]
-                               .getService(Components.interfaces.nsIMsgMailSession);
-    try
-    {
-      var nsIFolderListener = Components.interfaces.nsIFolderListener;
-      var notifyFlags = nsIFolderListener.event;
-      gMailSession.AddFolderListener(gFolderListener, notifyFlags);
-    }
-    catch (ex)
-    {
-      dump("Error adding to session: " +ex + "\n");
+    if (!gSessionFolderListenerAdded) {
+      try
+      {
+        let notifyFlags = Components.interfaces.nsIFolderListener.event;
+        MailServices.mailSession.AddFolderListener(gFolderListener, notifyFlags);
+        gSessionFolderListenerAdded = true;
+      }
+      catch (ex)
+      {
+        Components.utils.reportError("Error adding to session: " + ex + "\n");
+      }
     }
   }
 
   var msgWindow = GetFilterEditorMsgWindow();
 
   if (imapFolder)
     SetBusyCursor(window, true);
 
@@ -602,21 +598,17 @@ function doHelpButton()
   openHelp("mail-filters");
 }
 
 function getCustomActions()
 {
   if (!gCustomActions)
   {
     gCustomActions = [];
-    var filterService = Components.classes[
-                        "@mozilla.org/messenger/services/filters;1"]
-                        .getService(Components.interfaces.nsIMsgFilterService);
-
-    var customActionsEnum = filterService.getCustomActions();
+    let customActionsEnum = MailServices.filters.getCustomActions();
     while (customActionsEnum.hasMoreElements())
       gCustomActions.push(customActionsEnum.getNext().QueryInterface(
                            Components.interfaces.nsIMsgFilterCustomAction));
   }
 }
 
 function updateFilterType()
 {
--- a/mailnews/base/search/content/searchWidgets.xml
+++ b/mailnews/base/search/content/searchWidgets.xml
@@ -299,16 +299,17 @@
           ]]>
         </body>
       </method>
 
       <method name="initWithAction">
         <parameter name="aFilterAction"/>
         <body>
           <![CDATA[
+            Components.utils.import("resource://gre/modules/Services.jsm", this);
             var filterActionStr;
             var actionTarget = document.getAnonymousNodes(this)[1];
             var actionItem = document.getAnonymousNodes(actionTarget);
             var nsMsgFilterAction = Components.interfaces.nsMsgFilterAction;
             switch (aFilterAction.type)
             {
               case nsMsgFilterAction.Custom:
                 filterActionStr = aFilterAction.customId;
@@ -332,25 +333,23 @@
                   var menuitem = document.createElementNS(
                       "http://www.mozilla.org/keymaster/gatekeeper/there.is.only.xul",
                       "xul:menuitem");
                   menuitem.setAttribute("label",
                       gFilterBundle.getString("filterMissingCustomAction"));
                   menuitem.setAttribute("value", filterActionStr);
                   menuitem.disabled = true;
                   this.mRuleActionType.menulist.menupopup.appendChild(menuitem);
-                  var consoleService = Components.classes["@mozilla.org/consoleservice;1"]
-                      .getService(Components.interfaces.nsIConsoleService);
                   var scriptError = Components.classes["@mozilla.org/scripterror;1"]
                       .createInstance(Components.interfaces.nsIScriptError);
                   scriptError.init("Missing custom action " + filterActionStr,
                       null, null, 0, 0,
                       Components.interfaces.nsIScriptError.errorFlag,
                       "component javascript");
-                  consoleService.logMessage(scriptError);
+                  Services.console.logMessage(scriptError);
                 }
                 break;
               case nsMsgFilterAction.MoveToFolder:
               case nsMsgFilterAction.CopyToFolder:
                 actionItem[0].value = aFilterAction.targetFolderUri;
                 break;
               case nsMsgFilterAction.Reply:
               case nsMsgFilterAction.Forward:
@@ -538,21 +537,20 @@
         <xul:menupopup>
         </xul:menupopup>
       </xul:menulist>
     </content>
 
     <implementation>
       <constructor>
         <![CDATA[
-            var menuPopup = document.getAnonymousNodes(this)[0].menupopup;
-            var tagService = Components.classes["@mozilla.org/messenger/tagservice;1"]
-                                       .getService(Components.interfaces.nsIMsgTagService);
-            var tagArray = tagService.getAllTags({});
-            for (var i = 0; i < tagArray.length; ++i)
+            Components.utils.import("resource:///modules/mailServices.js", this);
+            let menuPopup = document.getAnonymousNodes(this)[0].menupopup;
+            let tagArray = MailServices.tags.getAllTags({});
+            for (let i = 0; i < tagArray.length; ++i)
             {
               var taginfo = tagArray[i];
               var newMenuItem = document.createElement('menuitem');
               newMenuItem.setAttribute('label', taginfo.tag);
               newMenuItem.setAttribute('value', taginfo.key);
               menuPopup.appendChild(newMenuItem);
             }
           // propagating a pre-existing hack to make the tag get displayed correctly in the menulist
@@ -597,28 +595,28 @@
         <xul:menupopup>
         </xul:menupopup>
       </xul:menulist>
     </content>
 
     <implementation>
       <constructor>
         <![CDATA[
-            var accountManager = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
-            var identity = accountManager.getFirstIdentityForServer(gFilterList.folder.server);
+            Components.utils.import("resource:///modules/mailServices.js", this);
+            let identity = MailServices.accounts.getFirstIdentityForServer(gFilterList.folder.server);
             if (!identity) // typically if this is Local Folders
-              identity = accountManager.defaultAccount.defaultIdentity;
+              identity = MailServices.accounts.defaultAccount.defaultIdentity;
             var rdfService = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
             var resource = rdfService.GetResource(identity.stationeryFolder);
             var msgFolder = resource.QueryInterface(Components.interfaces.nsIMsgFolder);
             var msgWindow = GetFilterEditorMsgWindow();
             var enumerator = msgFolder.msgDatabase.EnumerateMessages();
             var templateListPopup = document.getAnonymousNodes(this)[0].menupopup;
 
-            if ( enumerator )
+            if (enumerator)
             {
               while (enumerator.hasMoreElements())
               {
                 var header = enumerator.getNext();
                 if (header instanceof Components.interfaces.nsIMsgDBHdr)
                 {
                   var msgTemplateUri = msgFolder.URI + "?messageId=" + header.messageId + '&subject=' + header.mime2DecodedSubject;
                   var newItem = document.getAnonymousNodes(this)[0].appendItem(header.mime2DecodedSubject, msgTemplateUri);