home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 January / 01_02.iso / software / netscape62win / mail.xpi / bin / chrome / messenger.jar / content / messenger / accountUtils.js < prev    next >
Text File  |  2001-10-09  |  7KB  |  193 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public
  4.  * License Version 1.1 (the "License"); you may not use this file
  5.  * except in compliance with the License. You may obtain a copy of
  6.  * the License at http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the License is distributed on an "AS
  9.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  10.  * implied. See the License for the specific language governing
  11.  * rights and limitations under the License.
  12.  *
  13.  * The Original Code is mozilla.org code.
  14.  *
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation.  Portions created by Netscape are
  17.  * Copyright (C) 1998 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  *
  20.  * Contributor(s):
  21.  * Alec Flett <alecf@netscape.com>
  22.  */
  23. // The returnmycall is used as a global variable that is set during a callback.
  24. var returnmycall=false;
  25. var accountManagerContractID   = "@mozilla.org/messenger/account-manager;1";
  26. var messengerMigratorContractID   = "@mozilla.org/messenger/migrator;1";
  27.  
  28. // returns the first account with an invalid server or identity
  29.  
  30. function getInvalidAccounts(accounts)
  31. {
  32.     var numAccounts = accounts.Count();
  33.     var invalidAccounts = new Array;
  34.     for (var i=0; i<numAccounts; i++) {
  35.         var account = accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount);
  36.         try {
  37.             if (!account.incomingServer.valid) {
  38.                 invalidAccounts[invalidAccounts.length] = account;
  39.                 // skip to the next account
  40.                 continue;
  41.             }
  42.         } catch (ex) {
  43.             // this account is busted, just keep going
  44.             continue;
  45.         }
  46.  
  47.         var identities = account.identities;
  48.         var numIdentities = identities.Count();
  49.  
  50.         for (var j=0; j<numIdentities; j++) {
  51.             var identity = identities.QueryElementAt(j, Components.interfaces.nsIMsgIdentity);
  52.             if (!identity.valid) {
  53.                 invalidAccounts[invalidAccounts.length] = account;
  54.                 continue;
  55.             }
  56.         }
  57.     }
  58.  
  59.     return invalidAccounts;
  60. }
  61.  
  62. // This function gets called from verifyAccounts.
  63. // We do not have to do anything on
  64. // unix and mac but on windows we have to bring up a 
  65. // dialog based on the settings the user has.
  66. // This will bring up the dialog only once per session and only if we
  67. // are not the default mail client.
  68. function showMailIntegrationDialog() {
  69.     try {
  70.         var mapiRegistry = Components.classes[ "@mozilla.org/mapiregistry;1" ].
  71.                        getService( Components.interfaces.nsIMapiRegistry );
  72.     }
  73.     catch (ex) { 
  74.         mapiRegistry = null;
  75.     }
  76.     // showDialog is TRUE only if we did not bring up this dialog already
  77.     // and we are not the default mail client
  78.     var prefLocked = false;
  79.     if (mapiRegistry && mapiRegistry.showDialog) {
  80.         const prefbase = "system.windows.lock_ui.";
  81.         try {
  82.             var prefService = Components.classes["@mozilla.org/preferences-service;1"]
  83.                           .getService()
  84.                           .QueryInterface(Components.interfaces.nsIPrefService);
  85.             var prefBranch = prefService.getBranch(prefbase);
  86.         
  87.             if (prefBranch && prefBranch.prefIsLocked("defaultMailClient")) {
  88.                 prefLocked = true;
  89.                 if (prefBranch.getBoolPref("defaultMailClient"))
  90.                     mapiRegistry.setDefaultMailClient();
  91.                 else
  92.                     mapiRegistry.unsetDefaultMailClient();
  93.             }
  94.         }
  95.         catch (ex) {}
  96.         if (!prefLocked && !mapiRegistry.isDefaultMailClient)
  97.             mapiRegistry.showMailIntegrationDialog(window /* parent window */);
  98.     }
  99. }
  100.  
  101. function verifyAccounts(wizardcallback) {
  102. //check to see if the function is called with the callback and if so set the global variable returnmycall to true
  103.     if(wizardcallback)
  104.         returnmycall = true;
  105.     var openWizard = false;
  106.     var prefillAccount;
  107.     var state=true;
  108.     var ret = true;
  109.     
  110.     try {
  111.         var am = Components.classes[accountManagerContractID].getService(Components.interfaces.nsIMsgAccountManager);
  112.  
  113.         var accounts = am.accounts;
  114.  
  115.         // as long as we have some accounts, we're fine.
  116.         var accountCount = accounts.Count();
  117.         var invalidAccounts = getInvalidAccounts(accounts);
  118.         if (invalidAccounts.length > 0) {
  119.             prefillAccount = invalidAccounts[0];
  120.         } else {
  121.         }
  122.  
  123.         // if there are no accounts, or all accounts are "invalid"
  124.         // then kick off the account migration
  125.         if (accountCount == invalidAccounts.length) {
  126.             try {
  127.                   var messengerMigrator = Components.classes[messengerMigratorContractID].getService(Components.interfaces.nsIMessengerMigrator); 
  128.                   messengerMigrator.UpgradePrefs();
  129.                   // if there is a callback mechanism then inform parent window to shut itself down
  130.                   if (wizardcallback){
  131.                       state = false;
  132.                       WizCallback(state);
  133.                   }
  134.                   ret = false;
  135.             }
  136.             catch (ex) {
  137.                   // upgrade prefs failed, so open account wizard
  138.                   openWizard = true;
  139.             }
  140.         }
  141.  
  142.         if (openWizard || prefillAccount) {
  143.             MsgAccountWizard(prefillAccount);
  144.                 ret = false;
  145.         }
  146.  
  147.         // hack, set a time out to do this, so that the window can load first
  148.         setTimeout("showMailIntegrationDialog();",0);
  149.         return ret;
  150.     }
  151.     catch (ex) {
  152.         dump("error verifying accounts " + ex + "\n");
  153.         return false;
  154.     }
  155. }
  156.  
  157. // we do this from a timer because if this is called from the onload=
  158. // handler, then the parent window doesn't appear until after the wizard
  159. // has closed, and this is confusing to the user
  160. function MsgAccountWizard()
  161. {
  162.     setTimeout("msgOpenAccountWizard();", 0);
  163. }
  164.  
  165. function msgOpenAccountWizard()
  166. {
  167. // Check to see if the verify accounts function was called with callback or not.
  168.   if (returnmycall)
  169.       window.openDialog("chrome://messenger/content/AccountWizard.xul",
  170.                         "AccountWizard", "chrome,modal,titlebar,resizable", {okCallback:WizCallback});
  171.   else
  172.       window.openDialog("chrome://messenger/content/AccountWizard.xul",
  173.                         "AccountWizard", "chrome,modal,titlebar,resizable");
  174.  
  175. }
  176.  
  177. // selectPage: the xul file name for the viewing page, 
  178. // null for the account main page, other pages are
  179. // 'am-server.xul', 'am-copies.xul', 'am-offline.xul', 
  180. // 'am-addressing.xul','am-advanced.xul', 'am-smtp.xul'
  181. function MsgAccountManager(selectPage)
  182. {
  183.     var server;
  184.     try {
  185.         var folderURI = GetSelectedFolderURI();
  186.         server = GetServer(folderURI);
  187.     } catch (ex) { /* functions might not be defined */}
  188.     
  189.     window.openDialog("chrome://messenger/content/AccountManager.xul",
  190.                       "AccountManager", "chrome,modal,titlebar,resizable",
  191.                       { server: server, selectPage: selectPage });
  192. }
  193.