rv = nsComponentManager::CreateInstance(kMsgIdentityCID, nsnull, nsCOMTypeInfo<nsIMsgIdentity>::GetIID(), getter_AddRefs(identity)); identity->SetKey("identity1");
rv = createKeyedIdentity("identity1", getter_AddRefs(identity)); if (NS_FAILED(rv)) return rv;
nsMsgAccountManager::upgradePrefs(){    nsresult rv;    PRInt32 oldMailType;    PRInt32 numAccounts = 0;    char *oldstr = nsnull;    PRBool oldbool;    rv = getPrefService();    if (NS_FAILED(rv)) return rv;        rv = m_prefs->GetIntPref("mail.server_type", &oldMailType);    if (NS_FAILED(rv)) {#ifdef DEBUG_ACCOUNTMANAGER        printf("Tried to upgrade old prefs, but couldn't find server type!\n");#endif        return rv;    }    nsCOMPtr<nsIMsgIdentity> identity;    rv = nsComponentManager::CreateInstance(kMsgIdentityCID,                                            nsnull,                                            nsCOMTypeInfo<nsIMsgIdentity>::GetIID(),                                            getter_AddRefs(identity));    identity->SetKey("identity1");    // identity stuff    rv = m_prefs->CopyCharPref("mail.identity.useremail", &oldstr);    if (NS_SUCCEEDED(rv)) {      if (oldstr) {      	identity->SetEmail(oldstr);      }      else {	identity->SetEmail("");      }      PR_FREEIF(oldstr);      oldstr = nsnull;    }        rv = m_prefs->CopyCharPref("mail.identity.username", &oldstr);    if (NS_SUCCEEDED(rv)) {      if (oldstr) {      	identity->SetFullName(oldstr);      }      else {	identity->SetFullName("");      }      PR_FREEIF(oldstr);      oldstr = nsnull;    }        rv = m_prefs->CopyCharPref("mail.identity.reply_to", &oldstr);    if (NS_SUCCEEDED(rv)) {      if (oldstr) {      	identity->SetReplyTo(oldstr);      }      else {	identity->SetReplyTo("");      }      PR_FREEIF(oldstr);      oldstr = nsnull;    }        rv = m_prefs->CopyCharPref("mail.identity.organization", &oldstr);    if (NS_SUCCEEDED(rv)) {      if (oldstr) {      	identity->SetOrganization(oldstr);      }      else {	identity->SetOrganization("");      }      PR_FREEIF(oldstr);      oldstr = nsnull;    }        rv = m_prefs->GetBoolPref("mail.compose_html", &oldbool);    if (NS_SUCCEEDED(rv)) {      identity->SetComposeHtml(oldbool);    }        rv = m_prefs->CopyCharPref("network.hosts.smtp_server", &oldstr);    if (NS_SUCCEEDED(rv)) {      if (oldstr) {	identity->SetSmtpHostname(oldstr);      }      else {	identity->SetSmtpHostname("");      }      PR_FREEIF(oldstr);      oldstr = nsnull;    }        rv = m_prefs->CopyCharPref("mail.smtp_name", &oldstr);    if (NS_SUCCEEDED(rv)) {      if (oldstr) {      	identity->SetSmtpUsername(oldstr);      }      else {	identity->SetSmtpUsername("");      }      PR_FREEIF(oldstr);      oldstr = nsnull;    }        if ( oldMailType == 0) {      // POP      numAccounts += MigratePopAccounts(identity);	}    else if (oldMailType == 1) {  // IMAP      numAccounts += MigrateImapAccounts(identity);	}    else {#ifdef DEBUG_ACCOUNTMANAGER        printf("Unrecognized server type %d\n", oldMailType);#endif        return NS_ERROR_UNEXPECTED;    }	numAccounts += MigrateNewsAccounts(identity, numAccounts);    if (numAccounts == 0) return NS_ERROR_FAILURE;        // we still need to create these additional prefs.    // assume there is at least one account    m_prefs->SetCharPref(PREF_MAIL_ACCOUNTMANAGER_ACCOUNTS,"account1");    m_prefs->SetCharPref("mail.account.account1.identities","identity1");    m_prefs->SetCharPref("mail.account.account1.server","server1");    char *oldAccountsValueBuf=nsnull;    char newAccountsValueBuf[BUF_STR_LEN];    char prefNameBuf[BUF_STR_LEN];    char prefValueBuf[BUF_STR_LEN];    // handle the rest of the accounts.    for (PRInt32 i=2;i<=numAccounts;i++) {      rv = m_prefs->CopyCharPref(PREF_MAIL_ACCOUNTMANAGER_ACCOUNTS, &oldAccountsValueBuf);      if (NS_SUCCEEDED(rv)) {        PR_snprintf(newAccountsValueBuf, BUF_STR_LEN, "%s,account%d",oldAccountsValueBuf,i);        m_prefs->SetCharPref(PREF_MAIL_ACCOUNTMANAGER_ACCOUNTS,                                   newAccountsValueBuf);      }      PR_FREEIF(oldAccountsValueBuf);      oldAccountsValueBuf = nsnull;            PR_snprintf(prefNameBuf, BUF_STR_LEN, "mail.account.account%d.identities", i);      PR_snprintf(prefValueBuf, BUF_STR_LEN, "identity%d", i);      m_prefs->SetCharPref(prefNameBuf, prefValueBuf);      PR_snprintf(prefNameBuf, BUF_STR_LEN, "mail.account.account%d.server", i);      PR_snprintf(prefValueBuf, BUF_STR_LEN, "server%d", i);      m_prefs->SetCharPref(prefNameBuf, prefValueBuf);    }    return NS_OK;}