/* void removeIdentity (in nsIMsgIdentity identity); */ NS_IMETHODIMP nsMsgAccount::RemoveIdentity(nsIMsgIdentity * aIdentity) { NS_ENSURE_ARG_POINTER(aIdentity); NS_ENSURE_TRUE(m_identities, NS_ERROR_FAILURE); PRUint32 count =0; m_identities->Count(&count); NS_ENSURE_TRUE(count > 1, NS_ERROR_FAILURE); // you must have at least one identity nsCString key; nsresult rv = aIdentity->GetKey(key); // remove our identity m_identities->RemoveElement(aIdentity); count--; // clear out the actual pref values associated with the identity aIdentity->ClearAllValues(); // if we just deleted the default identity, clear it out so we pick a new one if (m_defaultIdentity == aIdentity) m_defaultIdentity = nsnull; // now rebuild the identity pref nsCAutoString identitiesKeyPref("mail.account."); identitiesKeyPref.Append(m_accountKey); identitiesKeyPref.Append(".identities"); nsCAutoString newIdentityList; // iterate over the remaining identities for (PRUint32 index = 0; index < count; index++) { nsCOMPtr<nsIMsgIdentity> identity = do_QueryElementAt(m_identities, index, &rv); if (identity) { identity->GetKey(key); if (!index) newIdentityList = key; else { newIdentityList.Append(','); newIdentityList.Append(key); } } } m_prefs->SetCharPref(identitiesKeyPref.get(), newIdentityList.get()); return rv; }
/* * set up the m_identities array * do not call this more than once or we'll leak. */ nsresult nsMsgAccount::createIdentities() { NS_ENSURE_TRUE(!m_accountKey.IsEmpty(), NS_ERROR_NOT_INITIALIZED); if (m_identities) return NS_ERROR_FAILURE; NS_NewISupportsArray(getter_AddRefs(m_identities)); // get the pref // ex) mail.account.myaccount.identities = "joe-home,joe-work" nsCAutoString identitiesKeyPref("mail.account."); identitiesKeyPref.Append(m_accountKey); identitiesKeyPref.Append(".identities"); nsCString identityKey; nsresult rv; rv = getPrefService(); NS_ENSURE_SUCCESS(rv, rv); m_prefs->GetCharPref(identitiesKeyPref.get(), getter_Copies(identityKey)); if (identityKey.IsEmpty()) // not an error if no identities, but return NS_OK; // strtok will be unhappy // get the server from the account manager nsCOMPtr<nsIMsgAccountManager> accountManager = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv); NS_ENSURE_SUCCESS(rv, rv); char* newStr = identityKey.BeginWriting(); char* token = NS_strtok(",", &newStr); // temporaries used inside the loop nsCOMPtr<nsIMsgIdentity> identity; nsCAutoString key; // iterate through id1,id2, etc while (token) { key = token; key.StripWhitespace(); // create the account rv = accountManager->GetIdentity(key, getter_AddRefs(identity)); if (NS_SUCCEEDED(rv)) { // ignore error from addIdentityInternal() - if it fails, it fails. rv = addIdentityInternal(identity); NS_ASSERTION(NS_SUCCEEDED(rv), "Couldn't create identity"); } // advance to next key, if any token = NS_strtok(",", &newStr); } return rv; }
/* void addIdentity (in nsIMsgIdentity identity); */ NS_IMETHODIMP nsMsgAccount::AddIdentity(nsIMsgIdentity *identity) { // hack hack - need to add this to the list of identities. // for now just treat this as a Setxxx accessor // when this is actually implemented, don't refcount the default identity nsresult rv; nsCString key; rv = identity->GetKey(key); if (NS_SUCCEEDED(rv)) { nsCAutoString identitiesKeyPref("mail.account."); identitiesKeyPref.Append(m_accountKey); identitiesKeyPref.Append(".identities"); nsCString identityList; m_prefs->GetCharPref(identitiesKeyPref.get(), getter_Copies(identityList)); nsCAutoString newIdentityList(identityList); nsCAutoString testKey; // temporary to strip whitespace PRBool foundIdentity = PR_FALSE; // if the input identity is found if (!identityList.IsEmpty()) { char *newStr = identityList.BeginWriting(); char *token = NS_strtok(",", &newStr); // look for the identity key that we're adding while (token) { testKey = token; testKey.StripWhitespace(); if (testKey.Equals(key)) foundIdentity = PR_TRUE; token = NS_strtok(",", &newStr); } } // if it didn't already exist, append it if (!foundIdentity) { if (newIdentityList.IsEmpty()) newIdentityList = key; else { newIdentityList.Append(','); newIdentityList.Append(key); } } m_prefs->SetCharPref(identitiesKeyPref.get(), newIdentityList.get()); } // now add it to the in-memory list rv = addIdentityInternal(identity); if (!m_defaultIdentity) SetDefaultIdentity(identity); return rv; }
/* * set up the m_identities array * do not call this more than once or we'll leak. */ nsresult nsMsgAccount::createIdentities() { NS_ASSERTION(!m_identities, "only call createIdentities() once!"); if (m_identities) return NS_ERROR_FAILURE; NS_ENSURE_TRUE((const char*)m_accountKey, NS_ERROR_NOT_INITIALIZED); NS_NewISupportsArray(getter_AddRefs(m_identities)); // get the pref // ex) mail.account.myaccount.identities = "joe-home,joe-work" nsCAutoString identitiesKeyPref("mail.account."); identitiesKeyPref.Append(m_accountKey); identitiesKeyPref.Append(".identities"); nsXPIDLCString identityKey; nsresult rv; rv = getPrefService(); if (NS_FAILED(rv)) return rv; rv = m_prefs->GetCharPref(identitiesKeyPref.get(), getter_Copies(identityKey)); if (NS_FAILED(rv)) return rv; if (identityKey.IsEmpty()) // not an error if no identities, but return NS_OK; // nsCRT::strtok will be unhappy #ifdef DEBUG_alecf printf("%s's identities: %s\n", (const char*)m_accountKey, (const char*)identityKey); #endif // get the server from the account manager nsCOMPtr<nsIMsgAccountManager> accountManager = do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv); if (NS_FAILED(rv)) return rv; // const-casting because nsCRT::strtok whacks the string, // but safe because identityKey is a copy char* newStr; char* rest = identityKey.BeginWriting(); char* token = nsCRT::strtok(rest, ",", &newStr); // temporaries used inside the loop nsCOMPtr<nsIMsgIdentity> identity; nsCAutoString key; // iterate through id1,id2, etc while (token) { key = token; key.StripWhitespace(); // create the account rv = accountManager->GetIdentity(key.get(), getter_AddRefs(identity)); if (NS_SUCCEEDED(rv)) { // ignore error from addIdentityInternal() - if it fails, it fails. rv = addIdentityInternal(identity); NS_ASSERTION(NS_SUCCEEDED(rv), "Couldn't create identity"); } // advance to next key, if any token = nsCRT::strtok(newStr, ",", &newStr); } return rv; }