home *** CD-ROM | disk | FTP | other *** search
/ PC World 2000 December / PCWorld_2000-12_cd.bin / Komunikace / mozilla / mozilla-win32-M18-mathml-svg-xslt.exe / chrome / messenger.jar / content / messenger / AccountWizard.js < prev    next >
Text File  |  2000-09-18  |  26KB  |  777 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.  
  404.  
  405. // copy over all attributes from dest into src that already exist in src
  406. // the assumption is that src is an XPConnect interface full of attributes
  407. function copyObjectToInterface(dest, src) {
  408.     if (!dest) return;
  409.     if (!src) return;
  410.  
  411.     var i;
  412.     for (i in src) {
  413.         try {
  414.             if (dest[i] != src[i])
  415.                 dest[i] = src[i];
  416.         }
  417.         catch (ex) {
  418.             dump("Error copying the " +
  419.                  i + " attribute: " + ex + "\n");
  420.             dump("(This is ok if this is a ServerType-* attribute)\n");
  421.         }
  422.     }
  423. }
  424.  
  425. // check if there already is a "Local Folders"
  426. // if not, create it.
  427. function verifyLocalFoldersAccount(account) {
  428.     
  429.     dump("Looking for Local Folders.....\n");
  430.     var localMailServer = null;
  431.     try {
  432.         localMailServer = am.localFoldersServer;
  433.     }
  434.     catch (ex) {
  435.         // dump("exception in findserver: " + ex + "\n");
  436.         localMailServer = null;
  437.     }
  438.  
  439.     try {
  440.     var server = account.incomingServer;
  441.     var identity = account.identities.QueryElementAt(0, Components.interfaces.nsIMsgIdentity);
  442.  
  443.     // use server type to get the protocol info
  444.     var protocolinfo = Components.classes["@mozilla.org/messenger/protocol/info;1?type=" + server.type].getService(Components.interfaces.nsIMsgProtocolInfo);
  445.     // for this protocol, do we default the folder prefs to this server, or to the "Local Folders" server
  446.     defaultCopiesAndFoldersPrefsToServer = protocolinfo.defaultCopiesAndFoldersPrefsToServer;
  447.  
  448.     if (!localMailServer) {
  449.             // dump("Creating local mail account\n");
  450.         // creates a copy of the identity you pass in
  451.             messengerMigrator = Components.classes["@mozilla.org/messenger/migrator;1"].getService(Components.interfaces.nsIMessengerMigrator);
  452.         messengerMigrator.createLocalMailAccount(false /* false, since we are not migrating */);
  453.         try {
  454.             localMailServer = am.localFoldersServer;
  455.         }
  456.         catch (ex) {
  457.             dump("error!  we should have found the local mail server after we created it.\n");
  458.             localMailServer = null;
  459.         }    
  460.         }
  461.  
  462.     var copiesAndFoldersServer = null;
  463.     if (defaultCopiesAndFoldersPrefsToServer) {
  464.         copiesAndFoldersServer = server;
  465.     }
  466.     else {
  467.         if (!localMailServer) {
  468.             dump("error!  we should have a local mail server at this point\n");
  469.             return;
  470.         }
  471.         copiesAndFoldersServer = localMailServer;
  472.     }
  473.     
  474.     setDefaultCopiesAndFoldersPrefs(identity, copiesAndFoldersServer);
  475.  
  476.     } catch (ex) {
  477.         // return false (meaning we did not create the account)
  478.         // on any error
  479.         dump("Error creating local mail: " + ex + "\n");
  480.         return false;
  481.     }
  482.     return true;
  483. }
  484.  
  485. function setDefaultCopiesAndFoldersPrefs(identity, server)
  486. {
  487.     dump("finding folders on server = " + server.hostName + "\n");
  488.  
  489.     var rootFolder = server.RootFolder;
  490.  
  491.     // we need to do this or it is possible that the server's draft,
  492.     // stationery fcc folder will not be in rdf
  493.     //
  494.     // this can happen in a couple cases
  495.     // 1) the first account we create, creates the local mail.  since
  496.     // local mail was just created, it obviously hasn't been opened,
  497.     // or in rdf..
  498.     // 2) the account we created is of a type where
  499.     // defaultCopiesAndFoldersPrefsToServer is true
  500.     // this since we are creating the server, it obviously hasn't been
  501.     // opened, or in rdf.
  502.     //
  503.     // this makes the assumption that the server's draft, stationery fcc folder
  504.     // are at the top level (ie subfolders of the root folder.)  this works
  505.     // because we happen to be doing things that way, and if the user changes
  506.     // that, it will work because to change the folder, it must be in rdf,
  507.     // coming from the folder cache, in the worst case.
  508.     var folders = rootFolder.GetSubFolders();
  509.     var msgFolder = rootFolder.QueryInterface(Components.interfaces.nsIMsgFolder);
  510.     var numFolders = new Object();
  511.  
  512.     // these hex values come from nsMsgFolderFlags.h
  513.     var draftFolder = msgFolder.getFoldersWithFlag(0x0400, 1, numFolders);
  514.     var stationeryFolder = msgFolder.getFoldersWithFlag(0x400000, 1, numFolders);
  515.     var fccFolder = msgFolder.getFoldersWithFlag(0x0200, 1, numFolders);
  516.  
  517.     if (draftFolder) identity.draftFolder = draftFolder.URI;
  518.     if (stationeryFolder) identity.stationeryFolder = stationeryFolder.URI;
  519.     if (fccFolder) identity.fccFolder = fccFolder.URI;
  520.  
  521.     dump("fccFolder = " + identity.fccFolder + "\n");
  522.     dump("draftFolder = " + identity.draftFolder + "\n");
  523.     dump("stationeryFolder = " + identity.stationeryFolder + "\n");
  524.  
  525. }
  526.  
  527. function AccountExists(userName,hostName,serverType)
  528. {
  529.   dump("AccountExists("+userName+","+hostName+","+serverType+")\n");
  530.   var accountExists = false;
  531.   var accountManager = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
  532.   try {
  533.         var server = accountManager.FindServer(userName,hostName,serverType);
  534.         if (server) {
  535.         dump("account exists\n");
  536.                 accountExists = true;
  537.         }
  538.   }
  539.   catch (ex) {
  540.     dump("AccountExists() failed: "+ex+"\n");
  541.         accountExists = false;
  542.   }
  543.   return accountExists;
  544. }
  545.  
  546. function getFirstInvalidAccount()
  547. {
  548.     am = Components.classes["@mozilla.org/messenger/account-manager;1"].getService(Components.interfaces.nsIMsgAccountManager);
  549.  
  550.     var invalidAccounts = getInvalidAccounts(am.accounts);
  551.   
  552.     if (invalidAccounts.length > 0)
  553.         return invalidAccounts[0];
  554.     else
  555.         return null;
  556. }
  557.  
  558. function checkForInvalidAccounts()
  559. {
  560.     var firstInvalidAccount = getFirstInvalidAccount();
  561.  
  562.     if (firstInvalidAccount) {
  563.         var pageData = GetPageData();
  564.         dump("We have an invalid account, " + firstInvalidAccount + ", let's use that!\n");
  565.         gCurrentAccount = firstInvalidAccount;
  566.  
  567.         // there's a possibility that the invalid account has ISP defaults
  568.         // as well.. so first pre-fill accountData with ISP info, then
  569.         // overwrite it with the account data
  570.  
  571.  
  572.         var identity =
  573.             firstInvalidAccount.identities.QueryElementAt(0, nsIMsgIdentity);
  574.  
  575.         dump("Invalid account: trying to get ISP data for " + identity.email + "\n");
  576.         var accountData = getIspDefaultsForEmail(identity.email);
  577.         dump("Invalid account: Got " + accountData + "\n");
  578.         
  579.         // account -> accountData -> pageData
  580.         accountData = AccountToAccountData(firstInvalidAccount, accountData);
  581.         
  582.         AccountDataToPageData(accountData, pageData);
  583.  
  584.         gCurrentAccountData = accountData;
  585.         
  586.         dump(parent.wizardManager.WSM);
  587.     }
  588. }
  589.  
  590. function AccountToAccountData(account, defaultAccountData)
  591. {
  592.     dump("AccountToAccountData(" + account + ", " +
  593.          defaultAccountData + ")\n");
  594.     var accountData = defaultAccountData;
  595.     if (!accountData)
  596.         accountData = new Object;
  597.     
  598.     accountData.incomingServer = account.incomingServer;
  599.     accountData.identity = account.identities.QueryElementAt(0, nsIMsgIdentity);
  600.     accountData.smtp = smtpService.defaultServer;
  601.  
  602.     return accountData;
  603. }
  604.  
  605.  
  606. // sets the page data, automatically creating the arrays as necessary
  607. function setPageData(pageData, tag, slot, value) {
  608.     if (!pageData[tag]) pageData[tag] = [];
  609.  
  610.     if (value == undefined) {
  611.         // clear out this slot
  612.         if (pageData[tag][slot]) delete pageData[tag][slot];
  613.     } else {
  614.         // pre-fill this slot
  615.         if (!pageData[tag][slot]) pageData[tag][slot] = [];
  616.         pageData[tag][slot].id = slot;
  617.         pageData[tag][slot].value = value;
  618.     }
  619. }
  620.  
  621. // value of checkbox on the first page
  622. function serverIsNntp(pageData) {
  623.     if (pageData.accounttype.newsaccount)
  624.         return pageData.accounttype.newsaccount.value;
  625.     return false;
  626. }
  627.  
  628. function getUsernameFromEmail(email)
  629. {
  630.     var emailData = email.split("@");
  631.     return emailData[0];
  632. }
  633.  
  634. function getCurrentUserName(pageData)
  635. {
  636.     var userName = "";
  637.  
  638.     if (pageData.login) {
  639.         if (pageData.login.username) {
  640.             userName = pageData.login.username.value;
  641.         }
  642.     }
  643.     if (userName == "") {
  644.         var email = pageData.identity.email.value;
  645.         userName = getUsernameFromEmail(email); 
  646.     }
  647.     return userName;
  648. }
  649.  
  650. function getCurrentServerType(pageData) {
  651.     var servertype = "pop3";    // hopefully don't resort to default!
  652.     if (serverIsNntp(pageData))
  653.         servertype = "nntp";
  654.     else if (pageData.server && pageData.server.servertype)
  655.         servertype = pageData.server.servertype.value;
  656.     return servertype;
  657. }
  658.  
  659. function getCurrentHostname(pageData) {
  660.     if (serverIsNntp(pageData))
  661.         return pageData.newsserver.hostname.value;
  662.     else
  663.         return pageData.server.hostname.value;
  664. }
  665.  
  666. function UpdateWizardMap() {
  667.     updateMap(GetPageData(), gWizardMap);
  668. }
  669.  
  670. // updates the map based on various odd states
  671. // conditions handled right now:
  672. // - 
  673. function updateMap(pageData, wizardMap) {
  674.     dump("Updating wizard map..\n");
  675.     if (pageData.accounttype) {
  676.         var ismailaccount = pageData.accounttype.mailaccount;
  677.         dump("Accounttype is mail: " + (ismailaccount && ismailaccount.value) + "\n");
  678.         // set up default account stuff
  679.         wizardMap.identity.next = "server";
  680.         wizardMap.done.previous = "accname";
  681.         
  682.         if (pageData.accounttype.mailaccount &&
  683.             pageData.accounttype.mailaccount.value) {
  684.  
  685.             wizardMap.accname.previous = "login";
  686.             
  687.             if (gCurrentAccountData && gCurrentAccountData.wizardSkipPanels) {
  688.                 wizardMap.identity.next = "done";
  689.                 wizardMap.done.previous = "identity";
  690.             }
  691.         }
  692.  
  693.         else if (pageData.accounttype.newsaccount &&
  694.                  pageData.accounttype.newsaccount.value) {
  695.             wizardMap.identity.next = "newsserver";
  696.             wizardMap.accname.previous = "newsserver";
  697.         }
  698.         else {
  699.             dump("Handle other types (" + pageData.accounttype + ") here?\n");
  700.         }
  701.     }
  702.  
  703. }
  704.  
  705. function GetPageData()
  706. {
  707.     return parent.wizardManager.WSM.PageData;
  708. }
  709.     
  710.  
  711. function PrefillAccountForIsp(ispName)
  712. {
  713.     dump("AccountWizard.prefillAccountForIsp(" + ispName + ")\n");
  714.  
  715.     var ispData = getIspDefaultsForUri(ispName);
  716.     
  717.     var pageData = GetPageData();
  718.  
  719.  
  720.     // prefill the rest of the wizard
  721.     dump("PrefillAccountForISP: filling with " + ispData + "\n");
  722.     SetCurrentAccountData(ispData);
  723.     AccountDataToPageData(ispData, pageData);
  724. }
  725.  
  726. // does any cleanup work for the the account data
  727. // - sets the username from the email address if it's not already set
  728. // - anything else?
  729. function FixupAccountDataForIsp(accountData)
  730. {
  731.     var email = accountData.identity.email;
  732.     var username;
  733.  
  734.     if (email) {
  735.         username = getUsernameFromEmail(email);
  736.     }
  737.     
  738.     // fix up the username
  739.     if (!accountData.incomingServer.username) {
  740.         accountData.incomingServer.username = username;
  741.     }
  742.  
  743.     if (!accountData.smtp.username &&
  744.         accountData.smtpRequiresUsername) {
  745.         accountData.smtp.username = username;
  746.     }
  747. }
  748.  
  749. function SetCurrentAccountData(accountData)
  750. {
  751.     //    dump("Setting current account data (" + gCurrentAccountData + ") to " + accountData + "\n");
  752.     gCurrentAccountData = accountData;
  753. }
  754.  
  755. function getInterfaceForType(type) {
  756.     try {
  757.         var protocolInfoContractIDPrefix = "@mozilla.org/messenger/protocol/info;1?type=";
  758.         
  759.         var thisContractID = protocolInfoContractIDPrefix + type;
  760.         
  761.         var protoInfo = Components.classes[thisContractID].getService(Components.interfaces.nsIMsgProtocolInfo);
  762.         
  763.         return protoInfo.serverIID;
  764.     } catch (ex) {
  765.         dump("could not get IID for " + type + ": " + ex + "\n");
  766.         return undefined;
  767.     }
  768. }
  769.  
  770. // flush the XUL cache - just for debugging purposes - not called
  771. function onFlush() {
  772.         var prefs = Components.classes["@mozilla.org/preferences;1"].getService(Components.interfaces.nsIPref);
  773.         prefs.SetBoolPref("nglayout.debug.disable_xul_cache", true);
  774.         prefs.SetBoolPref("nglayout.debug.disable_xul_cache", false);
  775.  
  776. }
  777.