home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 35 Internet / 35-Internet.zip / mozil06.zip / bin / chrome / messenger.jar / content / messenger / AccountWizard.js < prev    next >
Text File  |  2001-02-14  |  26KB  |  781 lines

  1. /* -*- Mode: Java; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2.  * The contents of this file are subject to the Netscape Public
  3.  * License Version 1.1 (the "License"); you may not use this file
  4.  * except in compliance with the License. You may obtain a copy of
  5.  * the License at http://www.mozilla.org/NPL/
  6.  * 
  7.  * Software distributed under the License is distributed on an "AS
  8.  * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
  9.  * implied. See the License for the specific language governing
  10.  * rights and limitations under the License.
  11.  * 
  12.  * The Original Code is Mozilla Communicator client code, released
  13.  * March 31, 1998.
  14.  * 
  15.  * The Initial Developer of the Original Code is Netscape
  16.  * Communications Corporation. Portions created by Netscape are
  17.  * Copyright (C) 1998-1999 Netscape Communications Corporation. All
  18.  * Rights Reserved.
  19.  */
  20.  
  21. /* The account wizard creates new accounts */
  22.  
  23. /*
  24.   data flow into the account wizard like this:
  25.  
  26.   For new accounts:
  27.   * pageData -> Array -> createAccount -> finishAccount
  28.   
  29.   For accounts coming from the ISP setup:
  30.   * RDF  -> Array -> pageData -> Array -> createAccount -> finishAccount
  31.   
  32.   for "unfinished accounts" 
  33.   * account -> Array -> pageData -> Array -> finishAccount
  34.   
  35.   Where:
  36.   pageData - the actual pages coming out of the Widget State Manager
  37.   RDF      - the ISP datasource
  38.   Array    - associative array of attributes, that very closely
  39.              resembles the nsIMsgAccount/nsIMsgIncomingServer/nsIMsgIdentity
  40.              structure
  41.   createAccount() - creates an account from the above Array
  42.   finishAccount() - fills an existing account with data from the above Array 
  43.  
  44. */
  45.  
  46. /* 
  47.    the account wizard path is something like:
  48.    
  49.    accounttype -> identity -> server -> login -> accname -> done
  50.                              \-> newsserver ----/
  51.  
  52.    where the accounttype determines which path to take
  53.    (server vs. newsserver)
  54. */
  55.  
  56. var gWizardMap = {
  57.     accounttype: { next: "identity" },
  58.     identity: { previous: "accounttype"}, // don't define next: server/newsserver
  59.     server:   { next: "login", previous: "identity"},
  60.     newsserver: { next: "accname", previous: "identity"},
  61.     login:    { next: "accname", previous: "server"}, 
  62.     accname:  { next: "done", }, // don't define previous: login/newsserver
  63.     done:     { previous: "accname", finish: true }
  64. }
  65.  
  66. var pagePrefix="chrome://messenger/content/aw-";
  67. var pagePostfix=".xul";
  68.  
  69. var currentPageTag;
  70.  
  71. var contentWindow;
  72.  
  73. var smtpService;
  74. var am;
  75.  
  76. var nsIMsgIdentity = Components.interfaces.nsIMsgIdentity;
  77. var nsIMsgIncomingServer = Components.interfaces.nsIMsgIncomingServer;
  78.  
  79. // the current nsIMsgAccount
  80. var gCurrentAccount;
  81.  
  82. // the current associative array that
  83. // will eventually be dumped into the account
  84. var gCurrentAccountData;
  85.  
  86. // event handlers
  87. function onLoad() {
  88.  
  89.     // wizard stuff
  90.     // instantiate the Wizard Manager
  91.     wizardManager = new WizardManager( "wizardContents", null, null,
  92.                                        gWizardMap );
  93.     wizardManager.URL_PagePrefix = "chrome://messenger/content/aw-";
  94.     wizardManager.URL_PagePostfix = ".xul"; 
  95.     wizardManager.SetHandlers(null, null, onFinish, onCancel, null, null);
  96.  
  97.     // load up the SMTP service for later
  98.     if (!smtpService) {
  99.         smtpService =
  100.             Components.classes["@mozilla.org/messengercompose/smtp;1"].getService(Components.interfaces.nsISmtpService);
  101.     }
  102.  
  103.     checkForInvalidAccounts();
  104.     var pageData = GetPageData();
  105.     updateMap(pageData, gWizardMap);
  106.  
  107.     // skip the first page if we have an account
  108.     if (gCurrentAccount) {
  109.         // skip past first pane
  110.         gWizardMap.identity.previous = null;
  111.         wizardManager.LoadPage("identity", false);
  112.     }
  113.     else
  114.         wizardManager.LoadPage("accounttype", false);
  115. }
  116.  
  117.     
  118. function onCancel() 
  119. {
  120.     var firstInvalidAccount = getFirstInvalidAccount();
  121.  
  122.     // if the user cancels the the wizard when it pops up because of 
  123.     // an invalid account (example, a webmail account that activation started)
  124.     // we just force create it by setting some values and calling the FinishAccount()
  125.     // see bug #47521 for the full discussion
  126.     if (firstInvalidAccount) {
  127.         var pageData = GetPageData();
  128.  
  129.         // set the fullName if it doesn't exist
  130.         if (!pageData.identity.fullName || !pageData.identity.fullName.value) {
  131.             setPageData(pageData, "identity", "fullName", "");
  132.         }
  133.  
  134.         // set the email if it doesn't exist
  135.         if (!pageData.identity.email || !pageData.identity.email.value) {
  136.             setPageData(pageData, "identity", "email", "nospam@nospam");
  137.         }
  138.     
  139.         // call FinishAccount() and not onFinish(), since the "finish"
  140.         // button may be disabled
  141.         FinishAccount();
  142.     }
  143.     else {
  144.         // since this is not an invalid account
  145.         // really cancel if the user hits the "cancel" button
  146.         window.close();
  147.     }
  148. }
  149.  
  150. function onFinish() {
  151.     if( !wizardManager.wizardMap[wizardManager.currentPageTag].finish )
  152.         return;
  153.  
  154.     FinishAccount();
  155. }
  156.     
  157. function FinishAccount() {
  158.     var pageData = GetPageData();
  159.  
  160.     dump(parent.wizardManager.WSM);
  161.  
  162.     var accountData= gCurrentAccountData;
  163.     
  164.     if (!accountData)
  165.         accountData = new Object;
  166.     
  167.     PageDataToAccountData(pageData, accountData);
  168.  
  169.     FixupAccountDataForIsp(accountData);
  170.     
  171.     // we might be simply finishing another account
  172.     if (!gCurrentAccount)
  173.         gCurrentAccount = createAccount(accountData);
  174.  
  175.     // transfer all attributes from the accountdata
  176.     finishAccount(gCurrentAccount, accountData);
  177.     
  178.     verifyLocalFoldersAccount(gCurrentAccount);
  179.  
  180.     // hack hack - save the prefs file NOW in case we crash
  181.     try {
  182.         var prefs = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
  183.         prefs.SavePrefFile();
  184.     } catch (ex) {
  185.         dump("Error saving prefs!\n");
  186.         dump("ex = " + ex + "\n");
  187.     }
  188.     window.close();
  189. }
  190.  
  191.  
  192. // prepopulate pageData with stuff from accountData
  193. // use: to prepopulate the wizard with account information
  194. function AccountDataToPageData(accountData, pageData)
  195. {
  196.     if (!accountData) {
  197.         dump("null account data! clearing..\n");
  198.         // handle null accountData as if it were an empty object
  199.         // so that we clear-out any old pagedata from a
  200.         // previous accountdata. The trick is that
  201.         // with an empty object, accountData.identity.slot is undefined,
  202.         // so this will clear out the prefill data in setPageData
  203.         
  204.         accountData = new Object;
  205.         accountData.incomingServer = new Object;
  206.         accountData.identity = new Object;
  207.         accountData.smtp = new Object;
  208.     }
  209.     
  210.     var server = accountData.incomingServer;
  211.  
  212.     if (server.type == undefined) {
  213.         // clear out the old server data
  214.         //setPageData(pageData, "accounttype", "mailaccount", undefined);
  215.         //        setPageData(pageData, "accounttype", "newsaccount", undefined);
  216.         setPageData(pageData, "server", "servertype", undefined);
  217.         setPageData(pageData, "server", "hostname", undefined);
  218.         
  219.     } else {
  220.         
  221.         if (server.type == "nntp") {
  222.             setPageData(pageData, "accounttype", "newsaccount", true);
  223.             setPageData(pageData, "newsserver", "hostname", server.hostName);
  224.         }
  225.         
  226.         else {
  227.             setPageData(pageData, "accounttype", "mailaccount", true);
  228.             setPageData(pageData, "server", "servertype", server.type);
  229.             setPageData(pageData, "server", "hostname", server.hostName);
  230.         }
  231.     }
  232.     
  233.     setPageData(pageData, "login", "username", server.username);
  234.     setPageData(pageData, "login", "password", server.password);
  235.     setPageData(pageData, "login", "rememberPassword", server.rememberPassword);
  236.     setPageData(pageData, "accname", "prettyName", server.prettyName);
  237.     
  238.     var identity;
  239.     
  240.     if (accountData.identity) {
  241.         dump("This is an accountdata\n");
  242.         identity = accountData.identity;
  243.     }
  244.     else if (accountData.identities) {
  245.         identity = accountData.identities.QueryElementAt(0, Components.interfaces.nsIMsgIdentity);
  246.         dump("this is an account, id= " + identity + "\n");
  247.     } 
  248.  
  249.     setPageData(pageData, "identity", "email", identity.email);
  250.     setPageData(pageData, "identity", "fullName", identity.fullName);
  251.  
  252.     var smtp;
  253.     
  254.     if (accountData.smtp) {
  255.         smtp = accountData.smtp;
  256.         setPageData(pageData, "server", "smtphostname",
  257.                     smtp.hostname);
  258.     }
  259. }
  260.  
  261. // take data from each page of pageData and dump it into accountData
  262. // use: to put results of wizard into a account-oriented object
  263. function PageDataToAccountData(pageData, accountData)
  264. {
  265.     if (!accountData.identity)
  266.         accountData.identity = new Object;
  267.     if (!accountData.incomingServer)
  268.         accountData.incomingServer = new Object;
  269.     if (!accountData.smtp)
  270.         accountData.smtp = new Object;
  271.     
  272.     var identity = accountData.identity;
  273.     var server = accountData.incomingServer;
  274.     var smtp = accountData.smtp;
  275.  
  276.     dump("Setting identity for " + pageData.identity.email.value + "\n");
  277.     identity.email = pageData.identity.email.value;
  278.     identity.fullName = pageData.identity.fullName.value;
  279.  
  280.     server.type = getCurrentServerType(pageData);
  281.     server.hostName = getCurrentHostname(pageData);
  282.  
  283.     if (serverIsNntp(pageData)) {
  284.         // this stuff probably not relevant
  285.         dump("not setting username/password/rememberpassword/etc\n");
  286.     } else {
  287.         if (pageData.login) {
  288.             if (pageData.login.username)
  289.                 server.username = pageData.login.username.value;
  290.             if (pageData.login.password)
  291.                 server.password = pageData.login.password.value;
  292.             if (pageData.login.rememberPassword)
  293.                 server.rememberPassword = pageData.login.rememberPassword.value;
  294.         }
  295.  
  296.         dump("pageData.server = " + pageData.server + "\n");
  297.         if (pageData.server) {
  298.             dump("pageData.server.smtphostname.value = " + pageData.server.smtphostname + "\n");
  299.             if (pageData.server.smtphostname &&
  300.                 pageData.server.smtphostname.value)
  301.                 smtp.hostname = pageData.server.smtphostname.value;
  302.         }
  303.     }
  304.  
  305.     if (pageData.accname) {
  306.         if (pageData.accname.prettyName)
  307.             server.prettyName = pageData.accname.prettyName.value;
  308.     }
  309.  
  310. }
  311.  
  312. // given an accountData structure, create an account
  313. // (but don't fill in any fields, that's for finishAccount()
  314. function createAccount(accountData)
  315. {
  316.  
  317.     var server = accountData.incomingServer;
  318.     dump("am.createIncomingServer(" + server.username + "," +
  319.                                       server.hostName + "," +
  320.                                       server.type + ")\n");
  321.     var server = am.createIncomingServer(server.username,
  322.                                          server.hostName,
  323.                                          server.type);
  324.     
  325.     dump("am.createIdentity()\n");
  326.     var identity = am.createIdentity();
  327.     
  328.     /* new nntp identities should use plain text by default
  329.      * we want that GNKSA (The Good Net-Keeping Seal of Approval) */
  330.     if (server.type == "nntp") {
  331.             identity.composeHtml = false;
  332.     }
  333.  
  334.     dump("am.createAccount()\n");
  335.     var account = am.createAccount();
  336.     account.addIdentity(identity);
  337.     account.incomingServer = server;
  338.     return account;
  339. }
  340.  
  341. // given an accountData structure, copy the data into the
  342. // given account, incoming server, and so forth
  343. function finishAccount(account, accountData) {
  344.  
  345.     if (accountData.incomingServer) {
  346.  
  347.         var destServer = account.incomingServer;
  348.         var srcServer = accountData.incomingServer;
  349.         copyObjectToInterface(destServer, srcServer);
  350.  
  351.         // see if there are any protocol-specific attributes
  352.         // if so, we use the type to get the IID, QueryInterface
  353.         // as appropriate, then copy the data over
  354.         dump("srcServer.ServerType-" + srcServer.type + " = " +
  355.              srcServer["ServerType-" + srcServer.type] + "\n");
  356.         if (srcServer["ServerType-" + srcServer.type]) {
  357.             // handle server-specific stuff
  358.             var IID = getInterfaceForType(srcServer.type);
  359.             if (IID) {
  360.                 destProtocolServer = destServer.QueryInterface(IID);
  361.                 srcProtocolServer = srcServer["ServerType-" + srcServer.type];
  362.  
  363.                 dump("Copying over " + srcServer.type + "-specific data\n");
  364.                 copyObjectToInterface(destProtocolServer, srcProtocolServer);
  365.             }
  366.         }
  367.         account.incomingServer.valid=true;
  368.     }
  369.  
  370.     // copy identity info
  371.     var destIdentity =
  372.         account.identities.QueryElementAt(0, nsIMsgIdentity);
  373.     
  374.     if (accountData.identity && destIdentity) {
  375.  
  376.         // fixup the email address if we have a default domain
  377.         var emailArray = accountData.identity.email.split('@');
  378.         if (emailArray.length < 2 && accountData.domain) {
  379.             accountData.identity.email += '@' + accountData.domain;
  380.         }
  381.  
  382.         copyObjectToInterface(destIdentity,
  383.                               accountData.identity);
  384.         destIdentity.valid=true;
  385.     }
  386.  
  387.     // don't try to create an smtp server if we already have one.
  388.     if (!destIdentity.smtpServerKey)
  389.     {
  390.       var smtpServer = smtpService.defaultServer;
  391.         
  392.       if (accountData.smtpCreateNewServer)
  393.           smtpServer = smtpService.createSmtpServer();
  394.  
  395.       dump("Copying smtpServer (" + smtpServer + ") to accountData\n");
  396.       copyObjectToInterface(smtpServer, accountData.smtp);
  397.  
  398.       // some identities have 'preferred' 
  399.       if (accountData.smtpUsePreferredServer && destIdentity)
  400.           destIdentity.smtpServerKey = smtpServer.key;
  401.      }
  402.  
  403.      if (this.FinishAccountHook != undefined) {
  404.          FinishAccountHook(accountData.domain);
  405.      }
  406. }
  407.  
  408.  
  409. // copy over all attributes from dest into src that already exist in src
  410. // the assumption is that src is an XPConnect interface full of attributes
  411. function copyObjectToInterface(dest, src) {
  412.     if (!dest) return;
  413.     if (!src) return;
  414.  
  415.     var i;
  416.     for (i in src) {
  417.         try {
  418.             if (dest[i] != src[i])
  419.                 dest[i] = src[i];
  420.         }
  421.         catch (ex) {
  422.             dump("Error copying the " +
  423.                  i + " attribute: " + ex + "\n");
  424.             dump("(This is ok if this is a ServerType-* attribute)\n");
  425.         }
  426.     }
  427. }
  428.  
  429. // check if there already is a "Local Folders"
  430. // if not, create it.
  431. function verifyLocalFoldersAccount(account) {
  432.     
  433.     dump("Looking for Local Folders.....\n");
  434.     var localMailServer = null;
  435.     try {
  436.         localMailServer = am.localFoldersServer;
  437.     }
  438.     catch (ex) {
  439.         // dump("exception in findserver: " + ex + "\n");
  440.         localMailServer = null;
  441.     }
  442.  
  443.     try {
  444.     var server = account.incomingServer;
  445.     var identity = account.identities.QueryElementAt(0, Components.interfaces.nsIMsgIdentity);
  446.  
  447.     // use server type to get the protocol info
  448.     var protocolinfo = Components.classes["@mozilla.org/messenger/protocol/info;1?type=" + server.type].getService(Components.interfaces.nsIMsgProtocolInfo);
  449.     // for this protocol, do we default the folder prefs to this server, or to the "Local Folders" server
  450.     defaultCopiesAndFoldersPrefsToServer = protocolinfo.defaultCopiesAndFoldersPrefsToServer;
  451.  
  452.     if (!localMailServer) {
  453.             // dump("Creating local mail account\n");
  454.         // creates a copy of the identity you pass in
  455.             messengerMigrator = Components.classes["@mozilla.org/messenger/migrator;1"].getService(Components.interfaces.nsIMessengerMigrator);
  456.         messengerMigrator.createLocalMailAccount(false /* false, since we are not migrating */);
  457.         try {
  458.             localMailServer = am.localFoldersServer;
  459.         }
  460.         catch (ex) {
  461.             dump("error!  we should have found the local mail server after we created it.\n");
  462.             localMailServer = null;
  463.         }    
  464.         }
  465.  
  466.     var copiesAndFoldersServer = null;
  467.     if (defaultCopiesAndFoldersPrefsToServer) {
  468.         copiesAndFoldersServer = server;
  469.     }
  470.     else {
  471.         if (!localMailServer) {
  472.             dump("error!  we should have a local mail server at this point\n");
  473.             return;
  474.         }
  475.         copiesAndFoldersServer = localMailServer;
  476.     }
  477.     
  478.     setDefaultCopiesAndFoldersPrefs(identity, copiesAndFoldersServer);
  479.  
  480.     } catch (ex) {
  481.         // return false (meaning we did not create the account)
  482.         // on any error
  483.         dump("Error creating local mail: " + ex + "\n");
  484.         return false;
  485.     }
  486.     return true;
  487. }
  488.  
  489. function setDefaultCopiesAndFoldersPrefs(identity, server)
  490. {
  491.     dump("finding folders on server = " + server.hostName + "\n");
  492.  
  493.     var rootFolder = server.RootFolder;
  494.  
  495.     // we need to do this or it is possible that the server's draft,
  496.     // stationery fcc folder will not be in rdf
  497.     //
  498.     // this can happen in a couple cases
  499.     // 1) the first account we create, creates the local mail.  since
  500.     // local mail was just created, it obviously hasn't been opened,
  501.     // or in rdf..
  502.     // 2) the account we created is of a type where
  503.     // defaultCopiesAndFoldersPrefsToServer is true
  504.     // this since we are creating the server, it obviously hasn't been
  505.     // opened, or in rdf.
  506.     //
  507.     // this makes the assumption that the server's draft, stationery fcc folder
  508.     // are at the top level (ie subfolders of the root folder.)  this works
  509.     // because we happen to be doing things that way, and if the user changes
  510.     // that, it will work because to change the folder, it must be in rdf,
  511.     // coming from the folder cache, in the worst case.
  512.     var folders = rootFolder.GetSubFolders();
  513.     var msgFolder = rootFolder.QueryInterface(Components.interfaces.nsIMsgFolder);
  514.     var numFolders = new Object();
  515.  
  516.     // these hex values come from nsMsgFolderFlags.h
  517.     var draftFolder = msgFolder.getFoldersWithFlag(0x0400, 1, numFolders);
  518.     var stationeryFolder = msgFolder.getFoldersWithFlag(0x400000, 1, numFolders);
  519.     var fccFolder = msgFolder.getFoldersWithFlag(0x0200, 1, numFolders);
  520.  
  521.     if (draftFolder) identity.draftFolder = draftFolder.URI;
  522.     if (stationeryFolder) identity.stationeryFolder = stationeryFolder.URI;
  523.     if (fccFolder) identity.fccFolder = fccFolder.URI;
  524.  
  525.     dump("fccFolder = " + identity.fccFolder + "\n");
  526.     dump("draftFolder = " + identity.draftFolder + "\n");
  527.     dump("stationeryFolder = " + identity.stationeryFolder + "\n");
  528.  
  529. }
  530.  
  531. function AccountExists(userName,hostName,serverType)
  532. {
  533.   dump("AccountExists("+userName+","+hostName+","+serverType+")\n");
  534.   var accountExists = false;
  535.   var accountManager = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
  536.   try {
  537.         var server = accountManager.FindServer(userName,hostName,serverType);
  538.         if (server) {
  539.         dump("account exists\n");
  540.                 accountExists = true;
  541.         }
  542.   }
  543.   catch (ex) {
  544.     dump("AccountExists() failed: "+ex+"\n");
  545.         accountExists = false;
  546.   }
  547.   return accountExists;
  548. }
  549.  
  550. function getFirstInvalidAccount()
  551. {
  552.     am = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
  553.  
  554.     var invalidAccounts = getInvalidAccounts(am.accounts);
  555.   
  556.     if (invalidAccounts.length > 0)
  557.         return invalidAccounts[0];
  558.     else
  559.         return null;
  560. }
  561.  
  562. function checkForInvalidAccounts()
  563. {
  564.     var firstInvalidAccount = getFirstInvalidAccount();
  565.  
  566.     if (firstInvalidAccount) {
  567.         var pageData = GetPageData();
  568.         dump("We have an invalid account, " + firstInvalidAccount + ", let's use that!\n");
  569.         gCurrentAccount = firstInvalidAccount;
  570.  
  571.         // there's a possibility that the invalid account has ISP defaults
  572.         // as well.. so first pre-fill accountData with ISP info, then
  573.         // overwrite it with the account data
  574.  
  575.  
  576.         var identity =
  577.             firstInvalidAccount.identities.QueryElementAt(0, nsIMsgIdentity);
  578.  
  579.         dump("Invalid account: trying to get ISP data for " + identity.email + "\n");
  580.         var accountData = getIspDefaultsForEmail(identity.email);
  581.         dump("Invalid account: Got " + accountData + "\n");
  582.         
  583.         // account -> accountData -> pageData
  584.         accountData = AccountToAccountData(firstInvalidAccount, accountData);
  585.         
  586.         AccountDataToPageData(accountData, pageData);
  587.  
  588.         gCurrentAccountData = accountData;
  589.         
  590.         dump(parent.wizardManager.WSM);
  591.     }
  592. }
  593.  
  594. function AccountToAccountData(account, defaultAccountData)
  595. {
  596.     dump("AccountToAccountData(" + account + ", " +
  597.          defaultAccountData + ")\n");
  598.     var accountData = defaultAccountData;
  599.     if (!accountData)
  600.         accountData = new Object;
  601.     
  602.     accountData.incomingServer = account.incomingServer;
  603.     accountData.identity = account.identities.QueryElementAt(0, nsIMsgIdentity);
  604.     accountData.smtp = smtpService.defaultServer;
  605.  
  606.     return accountData;
  607. }
  608.  
  609.  
  610. // sets the page data, automatically creating the arrays as necessary
  611. function setPageData(pageData, tag, slot, value) {
  612.     if (!pageData[tag]) pageData[tag] = [];
  613.  
  614.     if (value == undefined) {
  615.         // clear out this slot
  616.         if (pageData[tag][slot]) delete pageData[tag][slot];
  617.     } else {
  618.         // pre-fill this slot
  619.         if (!pageData[tag][slot]) pageData[tag][slot] = [];
  620.         pageData[tag][slot].id = slot;
  621.         pageData[tag][slot].value = value;
  622.     }
  623. }
  624.  
  625. // value of checkbox on the first page
  626. function serverIsNntp(pageData) {
  627.     if (pageData.accounttype.newsaccount)
  628.         return pageData.accounttype.newsaccount.value;
  629.     return false;
  630. }
  631.  
  632. function getUsernameFromEmail(email)
  633. {
  634.     var emailData = email.split("@");
  635.     return emailData[0];
  636. }
  637.  
  638. function getCurrentUserName(pageData)
  639. {
  640.     var userName = "";
  641.  
  642.     if (pageData.login) {
  643.         if (pageData.login.username) {
  644.             userName = pageData.login.username.value;
  645.         }
  646.     }
  647.     if (userName == "") {
  648.         var email = pageData.identity.email.value;
  649.         userName = getUsernameFromEmail(email); 
  650.     }
  651.     return userName;
  652. }
  653.  
  654. function getCurrentServerType(pageData) {
  655.     var servertype = "pop3";    // hopefully don't resort to default!
  656.     if (serverIsNntp(pageData))
  657.         servertype = "nntp";
  658.     else if (pageData.server && pageData.server.servertype)
  659.         servertype = pageData.server.servertype.value;
  660.     return servertype;
  661. }
  662.  
  663. function getCurrentHostname(pageData) {
  664.     if (serverIsNntp(pageData))
  665.         return pageData.newsserver.hostname.value;
  666.     else
  667.         return pageData.server.hostname.value;
  668. }
  669.  
  670. function UpdateWizardMap() {
  671.     updateMap(GetPageData(), gWizardMap);
  672. }
  673.  
  674. // updates the map based on various odd states
  675. // conditions handled right now:
  676. // - 
  677. function updateMap(pageData, wizardMap) {
  678.     dump("Updating wizard map..\n");
  679.     if (pageData.accounttype) {
  680.         var ismailaccount = pageData.accounttype.mailaccount;
  681.         dump("Accounttype is mail: " + (ismailaccount && ismailaccount.value) + "\n");
  682.         // set up default account stuff
  683.         wizardMap.identity.next = "server";
  684.         wizardMap.done.previous = "accname";
  685.         
  686.         if (pageData.accounttype.mailaccount &&
  687.             pageData.accounttype.mailaccount.value) {
  688.  
  689.             wizardMap.accname.previous = "login";
  690.             
  691.             if (gCurrentAccountData && gCurrentAccountData.wizardSkipPanels) {
  692.                 wizardMap.identity.next = "done";
  693.                 wizardMap.done.previous = "identity";
  694.             }
  695.         }
  696.  
  697.         else if (pageData.accounttype.newsaccount &&
  698.                  pageData.accounttype.newsaccount.value) {
  699.             wizardMap.identity.next = "newsserver";
  700.             wizardMap.accname.previous = "newsserver";
  701.         }
  702.         else {
  703.             dump("Handle other types (" + pageData.accounttype + ") here?\n");
  704.         }
  705.     }
  706.  
  707. }
  708.  
  709. function GetPageData()
  710. {
  711.     return parent.wizardManager.WSM.PageData;
  712. }
  713.     
  714.  
  715. function PrefillAccountForIsp(ispName)
  716. {
  717.     dump("AccountWizard.prefillAccountForIsp(" + ispName + ")\n");
  718.  
  719.     var ispData = getIspDefaultsForUri(ispName);
  720.     
  721.     var pageData = GetPageData();
  722.  
  723.  
  724.     // prefill the rest of the wizard
  725.     dump("PrefillAccountForISP: filling with " + ispData + "\n");
  726.     SetCurrentAccountData(ispData);
  727.     AccountDataToPageData(ispData, pageData);
  728. }
  729.  
  730. // does any cleanup work for the the account data
  731. // - sets the username from the email address if it's not already set
  732. // - anything else?
  733. function FixupAccountDataForIsp(accountData)
  734. {
  735.     var email = accountData.identity.email;
  736.     var username;
  737.  
  738.     if (email) {
  739.         username = getUsernameFromEmail(email);
  740.     }
  741.     
  742.     // fix up the username
  743.     if (!accountData.incomingServer.username) {
  744.         accountData.incomingServer.username = username;
  745.     }
  746.  
  747.     if (!accountData.smtp.username &&
  748.         accountData.smtpRequiresUsername) {
  749.         accountData.smtp.username = username;
  750.     }
  751. }
  752.  
  753. function SetCurrentAccountData(accountData)
  754. {
  755.     //    dump("Setting current account data (" + gCurrentAccountData + ") to " + accountData + "\n");
  756.     gCurrentAccountData = accountData;
  757. }
  758.  
  759. function getInterfaceForType(type) {
  760.     try {
  761.         var protocolInfoContractIDPrefix = "@mozilla.org/messenger/protocol/info;1?type=";
  762.         
  763.         var thisContractID = protocolInfoContractIDPrefix + type;
  764.         
  765.         var protoInfo = Components.classes[thisContractID].getService(Components.interfaces.nsIMsgProtocolInfo);
  766.         
  767.         return protoInfo.serverIID;
  768.     } catch (ex) {
  769.         dump("could not get IID for " + type + ": " + ex + "\n");
  770.         return undefined;
  771.     }
  772. }
  773.  
  774. // flush the XUL cache - just for debugging purposes - not called
  775. function onFlush() {
  776.         var prefs = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
  777.         prefs.SetBoolPref("nglayout.debug.disable_xul_cache", true);
  778.         prefs.SetBoolPref("nglayout.debug.disable_xul_cache", false);
  779.  
  780. }
  781.