home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / messenger.jar / content / messenger / accountUtils.js < prev    next >
Text File  |  2001-02-14  |  5KB  |  132 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.  
  24. var accountManagerContractID   = "@mozilla.org/messenger/account-manager;1";
  25. var messengerMigratorContractID   = "@mozilla.org/messenger/migrator;1";
  26.  
  27. // returns the first account with an invalid server or identity
  28.  
  29. function getInvalidAccounts(accounts)
  30. {
  31.     var numAccounts = accounts.Count();
  32.     var invalidAccounts = new Array;
  33.     for (var i=0; i<numAccounts; i++) {
  34.         var account = accounts.QueryElementAt(i, Components.interfaces.nsIMsgAccount);
  35.         try {
  36.             if (!account.incomingServer.valid) {
  37.                 dump("Found invalid server " + account.incomingServer + " in " + account + "!\n");
  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.                 dump("Found invalid identity " + identity + " in " + account + "\n");
  54.                 invalidAccounts[invalidAccounts.length] = account;
  55.                 continue;
  56.             }
  57.         }
  58.     }
  59.  
  60.     return invalidAccounts;
  61. }
  62.  
  63. function verifyAccounts() {
  64.     var openWizard = false;
  65.     var prefillAccount;
  66.     
  67.     try {
  68.         var am = Components.classes[accountManagerContractID].getService(Components.interfaces.nsIMsgAccountManager);
  69.  
  70.         var accounts = am.accounts;
  71.  
  72.         // as long as we have some accounts, we're fine.
  73.         var accountCount = accounts.Count();
  74.         var invalidAccounts = getInvalidAccounts(accounts);
  75.         if (invalidAccounts.length > 0) {
  76.             prefillAccount = invalidAccounts[0];
  77.             dump("prefillAccount = " + prefillAccount + "\n");
  78.         } else {
  79.         }
  80.  
  81.         // if there are no accounts, or all accounts are "invalid"
  82.         // then kick off the account migration
  83.         if (accountCount == invalidAccounts.length) {
  84.             try {
  85.                 messengerMigrator = Components.classes[messengerMigratorContractID].getService(Components.interfaces.nsIMessengerMigrator);  
  86.                 dump("attempt to UpgradePrefs.  If that fails, open the account wizard.\n");
  87.                 messengerMigrator.UpgradePrefs();
  88.             }
  89.             catch (ex) {
  90.                 // upgrade prefs failed, so open account wizard
  91.                 openWizard = true;
  92.             }
  93.         }
  94.  
  95.         if (openWizard || prefillAccount) {
  96.             MsgAccountWizard(prefillAccount);
  97.         }
  98.  
  99.     }
  100.     catch (ex) {
  101.         dump("error verifying accounts " + ex + "\n");
  102.         return;
  103.     }
  104. }
  105.  
  106. // we do this from a timer because if this is called from the onload=
  107. // handler, then the parent window doesn't appear until after the wizard
  108. // has closed, and this is confusing to the user
  109. function MsgAccountWizard()
  110. {
  111.     setTimeout("msgOpenAccountWizard();", 0);
  112. }
  113.  
  114. function msgOpenAccountWizard()
  115. {
  116.     window.openDialog("chrome://messenger/content/AccountWizard.xul",
  117.                       "AccountWizard", "chrome,modal,titlebar,resizable");
  118. }
  119.  
  120. function MsgAccountManager()
  121. {
  122.     var server;
  123.     try {
  124.         folderURI = GetSelectedFolderURI();
  125.         server = GetServer(folderURI);
  126.     } catch (ex) { /* functions might not be defined */}
  127.     
  128.     window.openDialog("chrome://messenger/content/AccountManager.xul",
  129.                       "AccountManager", "chrome,modal,titlebar,resizable",
  130.                       { server: server });
  131. }
  132.