Back to index

lightning-sunbird  0.9+nobinonly
nsMsgIdentity.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Pierre Phaneuf <pp@ludusdesign.com>
00024  *   Seth Spitzer <sspitzer@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "msgCore.h" // for pre-compiled headers
00041 #include "nsMsgIdentity.h"
00042 #include "nsIPrefService.h"
00043 #include "nsXPIDLString.h"
00044 #include "nsReadableUtils.h"
00045 
00046 #include "nsMsgCompCID.h"
00047 #include "nsIRDFService.h"
00048 #include "nsIRDFResource.h"
00049 #include "nsRDFCID.h"
00050 #include "nsMsgFolderFlags.h"
00051 #include "nsIMsgFolder.h"
00052 #include "nsIMsgIncomingServer.h"
00053 #include "nsIMsgAccountManager.h"
00054 #include "nsMsgBaseCID.h"
00055 #include "prprf.h"
00056 #include "nsISupportsObsolete.h"
00057 #include "nsISupportsPrimitives.h"
00058 #include "nsMsgUtils.h"
00059 
00060 static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
00061 
00062 #define REL_FILE_PREF_SUFFIX NS_LITERAL_CSTRING("-rel")
00063 
00064 NS_IMPL_THREADSAFE_ISUPPORTS1(nsMsgIdentity,
00065                    nsIMsgIdentity)
00066 
00067 nsMsgIdentity::nsMsgIdentity():
00068   m_signature(0),
00069   m_identityKey(0),
00070   m_prefBranch(0)
00071 {
00072 }
00073 
00074 nsMsgIdentity::~nsMsgIdentity()
00075 {
00076   PR_FREEIF(m_identityKey);
00077   NS_IF_RELEASE(m_prefBranch);
00078 }
00079 
00080 nsresult
00081 nsMsgIdentity::getPrefService()
00082 {
00083   if (m_prefBranch)
00084     return NS_OK;
00085 
00086   return CallGetService(NS_PREFSERVICE_CONTRACTID, &m_prefBranch);
00087 }
00088 
00089 
00090 /*
00091  * accessors for pulling values directly out of preferences
00092  * instead of member variables, etc
00093  */
00094 
00095 /* convert an identity key and preference name
00096    to mail.identity.<identityKey>.<prefName>
00097 */
00098 char *
00099 nsMsgIdentity::getPrefName(const char *identityKey,
00100                            const char *prefName)
00101 {
00102   return PR_smprintf("mail.identity.%s.%s", identityKey, prefName);
00103 }
00104 
00105 // this will be slightly faster than the above, and allows
00106 // the "default" identity preference root to be set in one place
00107 char *
00108 nsMsgIdentity::getDefaultPrefName(const char *fullPrefName)
00109 {
00110   return PR_smprintf("mail.identity.default.%s", fullPrefName);
00111 }
00112 
00113 /* The following are equivalent to the nsIPref's Get/CopyXXXPref
00114    except they construct the preference name with the above function
00115 */
00116 nsresult
00117 nsMsgIdentity::getBoolPref(const char *prefname,
00118                            PRBool *val)
00119 {
00120   nsresult rv = getPrefService();
00121   if (NS_FAILED(rv)) return rv;
00122   
00123   char *fullPrefName = getPrefName(m_identityKey, prefname);
00124   rv = m_prefBranch->GetBoolPref(fullPrefName, val);
00125   PR_Free(fullPrefName);
00126 
00127   if (NS_FAILED(rv))
00128     rv = getDefaultBoolPref(prefname, val);
00129   
00130   return rv;
00131 }
00132 
00133 nsresult
00134 nsMsgIdentity::getDefaultBoolPref(const char *prefname,
00135                                         PRBool *val) {
00136   
00137   nsresult rv = getPrefService();
00138   if (NS_FAILED(rv)) return rv;
00139   
00140   char *fullPrefName = getDefaultPrefName(prefname);
00141   rv = m_prefBranch->GetBoolPref(fullPrefName, val);
00142   PR_Free(fullPrefName);
00143 
00144   if (NS_FAILED(rv)) {
00145     *val = PR_FALSE;
00146     rv = NS_OK;
00147   }
00148   return rv;
00149 }
00150 
00151 nsresult
00152 nsMsgIdentity::setBoolPref(const char *prefname,
00153                            PRBool val)
00154 {
00155   nsresult rv = getPrefService();
00156   if (NS_FAILED(rv)) return rv;
00157   
00158   char *prefName = getPrefName(m_identityKey, prefname);
00159   rv = m_prefBranch->SetBoolPref(prefName, val);
00160   PR_Free(prefName);
00161   return rv;
00162 }
00163 
00164 
00165 nsresult
00166 nsMsgIdentity::getUnicharPref(const char *prefname,
00167                               PRUnichar **val)
00168 {
00169   nsresult rv = getPrefService();
00170   if (NS_FAILED(rv)) return rv;
00171   
00172   char *fullPrefName = getPrefName(m_identityKey, prefname);
00173   nsCOMPtr<nsISupportsString> supportsString;
00174   rv = m_prefBranch->GetComplexValue(fullPrefName,
00175                                      NS_GET_IID(nsISupportsString),
00176                                      getter_AddRefs(supportsString));
00177   PR_Free(fullPrefName);
00178 
00179   if (NS_FAILED(rv))
00180     rv = getDefaultUnicharPref(prefname, val);
00181 
00182   if (supportsString)
00183     rv = supportsString->ToString(val);
00184 
00185   return rv;
00186 }
00187 
00188 nsresult
00189 nsMsgIdentity::getCharPref(const char *prefname,
00190                            char **val)
00191 {
00192   nsresult rv = getPrefService();
00193   if (NS_FAILED(rv)) return rv;
00194   
00195   char *fullPrefName = getPrefName(m_identityKey, prefname);
00196   rv = m_prefBranch->GetCharPref(fullPrefName, val);
00197   PR_Free(fullPrefName);
00198 
00199   if (NS_FAILED(rv))
00200     rv = getDefaultCharPref(prefname, val);
00201 
00202   return rv;
00203 }
00204 
00205 nsresult
00206 nsMsgIdentity::getDefaultUnicharPref(const char *prefname,
00207                                      PRUnichar **val)
00208 {
00209   nsresult rv = getPrefService();
00210   if (NS_FAILED(rv)) return rv;
00211   
00212   char *fullPrefName = getDefaultPrefName(prefname);
00213   nsCOMPtr<nsISupportsString> supportsString;
00214   rv = m_prefBranch->GetComplexValue(fullPrefName,
00215                                      NS_GET_IID(nsISupportsString),
00216                                      getter_AddRefs(supportsString));
00217   PR_Free(fullPrefName);
00218 
00219   if (NS_FAILED(rv) || !supportsString) {
00220     *val = nsnull;              // null is ok to return here
00221     return NS_OK;
00222   }
00223 
00224   return supportsString->ToString(val);
00225 }
00226 
00227 nsresult
00228 nsMsgIdentity::getDefaultCharPref(const char *prefname,
00229                                         char **val)
00230 {
00231   nsresult rv = getPrefService();
00232   if (NS_FAILED(rv)) return rv;
00233   
00234   char *fullPrefName = getDefaultPrefName(prefname);
00235   rv = m_prefBranch->GetCharPref(fullPrefName, val);
00236   PR_Free(fullPrefName);
00237 
00238   if (NS_FAILED(rv)) {
00239     *val = nsnull;              // null is ok to return here
00240     rv = NS_OK;
00241   }
00242   return rv;
00243 }
00244 
00245 nsresult
00246 nsMsgIdentity::setUnicharPref(const char *prefname,
00247                               const PRUnichar *val)
00248 {
00249   nsresult rv = getPrefService();
00250   if (NS_FAILED(rv)) return rv;
00251   
00252   rv = NS_OK;
00253   char *prefName = getPrefName(m_identityKey, prefname);
00254   if (val) {
00255     nsCOMPtr<nsISupportsString> supportsString =
00256       do_CreateInstance(NS_SUPPORTS_STRING_CONTRACTID, &rv);
00257     if (supportsString) {
00258       supportsString->SetData(nsDependentString(val));
00259       rv = m_prefBranch->SetComplexValue(prefName,
00260                                          NS_GET_IID(nsISupportsString),
00261                                          supportsString);
00262     }
00263   }
00264   else {
00265     m_prefBranch->ClearUserPref(prefName);
00266   }
00267   PR_Free(prefName);
00268   return rv;
00269 }
00270 
00271 nsresult
00272 nsMsgIdentity::setCharPref(const char *prefname,
00273                            const char *val)
00274 {
00275   nsresult rv = getPrefService();
00276   if (NS_FAILED(rv)) return rv;
00277   
00278   rv = NS_OK;
00279   char *prefName = getPrefName(m_identityKey, prefname);
00280   if (val) 
00281     rv = m_prefBranch->SetCharPref(prefName, val);
00282   else
00283     m_prefBranch->ClearUserPref(prefName);
00284   PR_Free(prefName);
00285   return rv;
00286 }
00287 
00288 nsresult
00289 nsMsgIdentity::getIntPref(const char *prefname,
00290                                 PRInt32 *val)
00291 {
00292   nsresult rv = getPrefService();
00293   if (NS_FAILED(rv)) return rv;
00294   
00295   char *fullPrefName = getPrefName(m_identityKey, prefname);
00296   rv = m_prefBranch->GetIntPref(fullPrefName, val);
00297   PR_Free(fullPrefName);
00298 
00299   if (NS_FAILED(rv))
00300        rv = getDefaultIntPref(prefname, val);
00301 
00302   return rv;
00303 }
00304 
00305 nsresult
00306 nsMsgIdentity::getDefaultIntPref(const char *prefname,
00307                                         PRInt32 *val) {
00308   
00309   nsresult rv = getPrefService();
00310   if (NS_FAILED(rv)) return rv;
00311   
00312   char *fullPrefName = getDefaultPrefName(prefname);
00313   rv = m_prefBranch->GetIntPref(fullPrefName, val);
00314   PR_Free(fullPrefName);
00315 
00316   if (NS_FAILED(rv)) {
00317     *val = 0;
00318     rv = NS_OK;
00319   }
00320   
00321   return rv;
00322 }
00323 
00324 nsresult
00325 nsMsgIdentity::setIntPref(const char *prefname,
00326                                  PRInt32 val)
00327 {
00328   nsresult rv = getPrefService();
00329   if (NS_FAILED(rv)) return rv;
00330   
00331   char *prefName = getPrefName(m_identityKey, prefname);
00332   rv = m_prefBranch->SetIntPref(prefName, val);
00333   PR_Free(prefName);
00334   return rv;
00335 }
00336 
00337 nsresult
00338 nsMsgIdentity::SetKey(const char* identityKey)
00339 {
00340   PR_FREEIF(m_identityKey);
00341   m_identityKey = PL_strdup(identityKey);
00342   return NS_OK;
00343 }
00344 
00345 nsresult
00346 nsMsgIdentity::GetIdentityName(PRUnichar **idName) {
00347   if (!idName) return NS_ERROR_NULL_POINTER;
00348 
00349   *idName = nsnull;
00350   nsresult rv = getUnicharPref("identityName",idName);
00351   if (NS_FAILED(rv)) return rv;
00352 
00353   if (!(*idName)) {
00354     nsXPIDLString fullName;
00355     rv = GetFullName(getter_Copies(fullName));
00356     if (NS_FAILED(rv)) return rv;
00357     
00358     nsXPIDLCString email;
00359     rv = GetEmail(getter_Copies(email));
00360     if (NS_FAILED(rv)) return rv;
00361 
00362     nsAutoString str;
00363     str += (const PRUnichar*)fullName;
00364     str.AppendLiteral(" <");
00365     str.AppendWithConversion((const char*)email);
00366     str.AppendLiteral(">");
00367     *idName = ToNewUnicode(str);
00368     rv = NS_OK;
00369   }
00370 
00371   return rv;
00372 }
00373 
00374 nsresult nsMsgIdentity::SetIdentityName(const PRUnichar *idName) {
00375   return setUnicharPref("identityName", idName);
00376 }
00377 
00378 NS_IMETHODIMP
00379 nsMsgIdentity::ToString(PRUnichar **aResult)
00380 {
00381   nsString idname(NS_LITERAL_STRING("[nsIMsgIdentity: "));
00382   idname.AppendWithConversion(m_identityKey);
00383   idname.AppendLiteral("]");
00384 
00385   *aResult = ToNewUnicode(idname);
00386   return NS_OK;
00387 }
00388 
00389 /* Identity attribute accessors */
00390 
00391 NS_IMETHODIMP
00392 nsMsgIdentity::GetSignature(nsILocalFile **sig) 
00393 {
00394   nsresult rv = getPrefService();
00395   if (NS_FAILED(rv)) return rv;
00396   
00397   char *prefName = getPrefName(m_identityKey, "sig_file");
00398   if (!prefName)
00399     return NS_ERROR_FAILURE;  
00400   nsCAutoString relPrefName(prefName);
00401   relPrefName.Append(REL_FILE_PREF_SUFFIX);
00402   
00403   PRBool gotRelPref;
00404   rv = NS_GetPersistentFile(relPrefName.get(), prefName, nsnull, gotRelPref, sig);
00405   if (NS_SUCCEEDED(rv) && !gotRelPref) 
00406   {
00407     rv = NS_SetPersistentFile(relPrefName.get(), prefName, *sig);
00408     NS_ASSERTION(NS_SUCCEEDED(rv), "Failed to write signature file pref.");
00409   }
00410   PR_Free(prefName);
00411   return NS_OK;
00412 }
00413 
00414 NS_IMETHODIMP
00415 nsMsgIdentity::SetSignature(nsILocalFile *sig)
00416 {
00417   nsresult rv = NS_OK;
00418   if (sig) 
00419   {
00420     char *prefName = getPrefName(m_identityKey, "sig_file");
00421     if (!prefName)
00422       return NS_ERROR_FAILURE;  
00423   
00424     nsCAutoString relPrefName(prefName);
00425     relPrefName.Append(REL_FILE_PREF_SUFFIX);
00426     rv = NS_SetPersistentFile(relPrefName.get(), prefName, sig);
00427   }
00428   return rv;
00429 }
00430 
00431 NS_IMETHODIMP
00432 nsMsgIdentity::ClearAllValues()
00433 {
00434     nsresult rv = getPrefService();
00435     NS_ENSURE_SUCCESS(rv, rv);
00436 
00437     nsCAutoString rootPref("mail.identity.");
00438     rootPref += m_identityKey;
00439     rootPref += '.';
00440 
00441     PRUint32 childCount;
00442     char**   childArray;
00443     rv = m_prefBranch->GetChildList(rootPref.get(), &childCount, &childArray);
00444     NS_ENSURE_SUCCESS(rv, rv);
00445 
00446     for (PRUint32 i = 0; i < childCount; ++i) {
00447         m_prefBranch->ClearUserPref(childArray[i]);
00448     }
00449 
00450     NS_FREE_XPCOM_ALLOCATED_POINTER_ARRAY(childCount, childArray);
00451 
00452     return NS_OK;
00453 }
00454 
00455 
00456 NS_IMPL_GETTER_STR(nsMsgIdentity::GetKey, m_identityKey)
00457 NS_IMPL_IDPREF_STR(EscapedVCard, "escapedVCard")
00458 NS_IMPL_IDPREF_STR(SmtpServerKey, "smtpServer")
00459 NS_IMPL_IDPREF_WSTR(FullName, "fullName")
00460 NS_IMPL_IDPREF_STR(Email, "useremail")
00461 NS_IMPL_IDPREF_STR(ReplyTo, "reply_to")
00462 NS_IMPL_IDPREF_WSTR(Organization, "organization")
00463 NS_IMPL_IDPREF_BOOL(ComposeHtml, "compose_html")
00464 NS_IMPL_IDPREF_BOOL(AttachVCard, "attach_vcard")
00465 NS_IMPL_IDPREF_BOOL(AttachSignature, "attach_signature")
00466 
00467 NS_IMPL_IDPREF_BOOL(AutoQuote, "auto_quote")
00468 NS_IMPL_IDPREF_INT(ReplyOnTop, "reply_on_top")
00469 NS_IMPL_IDPREF_BOOL(SigBottom, "sig_bottom")
00470 
00471 NS_IMPL_IDPREF_INT(SignatureDate,"sig_date")
00472 
00473 NS_IMPL_IDPREF_BOOL(DoFcc, "fcc")
00474 
00475 NS_IMPL_FOLDERPREF_STR(FccFolder, "fcc_folder")
00476 NS_IMPL_IDPREF_STR(FccFolderPickerMode, "fcc_folder_picker_mode")
00477 NS_IMPL_IDPREF_BOOL(FccReplyFollowsParent, "fcc_reply_follows_parent")
00478 NS_IMPL_IDPREF_STR(DraftsFolderPickerMode, "drafts_folder_picker_mode")
00479 NS_IMPL_IDPREF_STR(TmplFolderPickerMode, "tmpl_folder_picker_mode")
00480 
00481 NS_IMPL_IDPREF_BOOL(BccSelf, "bcc_self")
00482 NS_IMPL_IDPREF_BOOL(BccOthers, "bcc_other")
00483 NS_IMPL_IDPREF_STR (BccList, "bcc_other_list")
00484 
00485 NS_IMETHODIMP
00486 nsMsgIdentity::GetDoBcc(PRBool *aValue)
00487 {
00488   nsresult rv = getPrefService();
00489   NS_ENSURE_SUCCESS(rv,rv);
00490   
00491   char *prefName = getPrefName(m_identityKey, "doBcc");
00492   rv = m_prefBranch->GetBoolPref(prefName, aValue);
00493   PR_Free(prefName);
00494   
00495   if (NS_SUCCEEDED(rv))
00496     return GetBoolAttribute("doBcc", aValue);
00497 
00498   PRBool bccSelf = PR_FALSE;
00499   rv = GetBccSelf(&bccSelf);
00500   NS_ENSURE_SUCCESS(rv,rv);
00501 
00502   PRBool bccOthers = PR_FALSE;
00503   rv = GetBccOthers(&bccOthers);
00504   NS_ENSURE_SUCCESS(rv,rv);
00505 
00506   nsXPIDLCString others;
00507   rv = GetBccList(getter_Copies(others));
00508   NS_ENSURE_SUCCESS(rv,rv);
00509     
00510   *aValue = bccSelf || (bccOthers && !others.IsEmpty());
00511 
00512   return SetDoBcc(*aValue);  
00513 }
00514 
00515 NS_IMETHODIMP
00516 nsMsgIdentity::SetDoBcc(PRBool aValue)
00517 {
00518   return SetBoolAttribute("doBcc", aValue);
00519 }
00520 
00521 NS_IMETHODIMP
00522 nsMsgIdentity::GetDoBccList(char **aValue)
00523 {
00524   nsresult rv = getPrefService();
00525   NS_ENSURE_SUCCESS(rv,rv);
00526 
00527   char *prefName = getPrefName(m_identityKey, "doBccList");
00528   rv = m_prefBranch->GetCharPref(prefName, aValue);
00529   PR_Free(prefName);
00530 
00531   if (NS_SUCCEEDED(rv))
00532     return GetCharAttribute("doBccList", aValue);
00533   
00534   nsCAutoString result;
00535 
00536   PRBool bccSelf = PR_FALSE;
00537   rv = GetBccSelf(&bccSelf);
00538   NS_ENSURE_SUCCESS(rv,rv);
00539   
00540   if (bccSelf) {
00541     nsXPIDLCString email;
00542     GetEmail(getter_Copies(email));
00543     result += email; 
00544   }
00545   
00546   PRBool bccOthers = PR_FALSE;
00547   rv = GetBccOthers(&bccOthers);
00548   NS_ENSURE_SUCCESS(rv,rv);
00549 
00550   nsXPIDLCString others;
00551   rv = GetBccList(getter_Copies(others));
00552   NS_ENSURE_SUCCESS(rv,rv);
00553 
00554   if (bccOthers && !others.IsEmpty()) {
00555     if (bccSelf)
00556       result += ",";
00557     result += others;
00558   }
00559   
00560   *aValue = ToNewCString(result);
00561   
00562   return SetDoBccList(*aValue);  
00563 }
00564 
00565 NS_IMETHODIMP
00566 nsMsgIdentity::SetDoBccList(const char *aValue)
00567 {
00568   return SetCharAttribute("doBccList", aValue);
00569 }
00570 
00571 NS_IMPL_FOLDERPREF_STR (DraftFolder, "draft_folder")
00572 NS_IMPL_FOLDERPREF_STR (StationeryFolder, "stationery_folder")
00573 
00574 NS_IMPL_IDPREF_BOOL(ShowSaveMsgDlg, "showSaveMsgDlg")
00575 NS_IMPL_IDPREF_STR (DirectoryServer, "directoryServer")
00576 NS_IMPL_IDPREF_BOOL(OverrideGlobalPref, "overrideGlobal_Pref")
00577 NS_IMPL_IDPREF_BOOL(AutocompleteToMyDomain, "autocompleteToMyDomain")
00578 
00579 NS_IMPL_IDPREF_BOOL(Valid, "valid")
00580 
00581 nsresult 
00582 nsMsgIdentity::getFolderPref(const char *prefname, char **retval, PRBool mustHaveDefault)
00583 {
00584   nsresult rv = getCharPref(prefname, retval);
00585   if (!mustHaveDefault) return rv;
00586 
00587   // Use default value if fail to get or not set
00588   if (NS_FAILED(rv) || !*retval || !strlen(*retval))
00589   {
00590     PR_FREEIF(*retval);     // free the empty string
00591     rv = getDefaultCharPref(prefname, retval);
00592     if (NS_SUCCEEDED(rv) && *retval)
00593     {
00594       rv = setFolderPref(prefname, (const char *)*retval);
00595     }
00596   }
00597   // get the corresponding RDF resource
00598   // RDF will create the folder resource if it doesn't already exist
00599   nsCOMPtr<nsIRDFService> rdf(do_GetService(kRDFServiceCID, &rv));
00600   if (NS_FAILED(rv)) return rv;
00601   nsCOMPtr<nsIRDFResource> resource;
00602   rv = rdf->GetResource(nsDependentCString(*retval), getter_AddRefs(resource));
00603   if (NS_FAILED(rv)) 
00604     return rv;
00605 
00606   nsCOMPtr <nsIMsgFolder> folderResource;
00607   folderResource = do_QueryInterface(resource, &rv);
00608   if (NS_SUCCEEDED(rv) && folderResource) 
00609   {
00610     // don't check validity of folder - caller will handle creating it
00611     nsCOMPtr<nsIMsgIncomingServer> server; 
00612     //make sure that folder hierarchy is built so that legitimate parent-child relationship is established
00613     rv = folderResource->GetServer(getter_AddRefs(server));
00614     if (server)
00615     {
00616       nsCOMPtr <nsIMsgFolder> msgFolder;
00617       rv = server->GetMsgFolderFromURI(folderResource, *retval, getter_AddRefs(msgFolder));
00618       PR_Free(*retval);
00619       if (NS_SUCCEEDED(rv))
00620         return msgFolder->GetURI(retval);
00621     }
00622     else // if the server doesn't exist, fall back to the default pref.
00623     {
00624       PR_FREEIF(*retval);   // free the empty string
00625       rv = getDefaultCharPref(prefname, retval);
00626       if (NS_SUCCEEDED(rv) && *retval)
00627         rv = setFolderPref(prefname, (const char *)*retval);
00628     }
00629   }
00630   return rv;
00631 }
00632 
00633 nsresult 
00634 nsMsgIdentity::setFolderPref(const char *prefname, const char *value)
00635 {
00636   nsXPIDLCString oldpref;
00637   nsresult rv;
00638   nsCOMPtr<nsIRDFResource> res;
00639   nsCOMPtr<nsIMsgFolder> folder;
00640   PRUint32 folderflag;
00641   nsCOMPtr<nsIRDFService> rdf(do_GetService(kRDFServiceCID, &rv));
00642   
00643   if (nsCRT::strcmp(prefname, "fcc_folder") == 0)
00644   {
00645     // Clear the temporary return receipt filter so that the new filter
00646     // rule can be recreated (by ConfigureTemporaryFilters()).
00647     nsCOMPtr<nsIMsgAccountManager> accountManager = 
00648       do_GetService(NS_MSGACCOUNTMANAGER_CONTRACTID, &rv);
00649     NS_ENSURE_SUCCESS(rv,rv);
00650     
00651     nsCOMPtr<nsISupportsArray> servers; 
00652     rv = accountManager->GetServersForIdentity(this, getter_AddRefs(servers));
00653     NS_ENSURE_SUCCESS(rv,rv);
00654     PRUint32 cnt = 0;
00655     servers->Count(&cnt);
00656     if (cnt > 0)
00657     {
00658       nsCOMPtr<nsISupports> supports = getter_AddRefs(servers->ElementAt(0));
00659       nsCOMPtr<nsIMsgIncomingServer> server = do_QueryInterface(supports,&rv);
00660       if (NS_SUCCEEDED(rv))
00661         server->ClearTemporaryReturnReceiptsFilter(); // okay to fail; no need to check for return code
00662     }
00663     folderflag = MSG_FOLDER_FLAG_SENTMAIL;
00664   }
00665   else if (nsCRT::strcmp(prefname, "draft_folder") == 0)
00666     folderflag = MSG_FOLDER_FLAG_DRAFTS;
00667   else if (nsCRT::strcmp(prefname, "stationery_folder") == 0)
00668     folderflag = MSG_FOLDER_FLAG_TEMPLATES;
00669   else
00670     return NS_ERROR_FAILURE;
00671   
00672   // get the old folder, and clear the special folder flag on it
00673   rv = getFolderPref(prefname, getter_Copies(oldpref), PR_FALSE);
00674   if (NS_SUCCEEDED(rv) && !oldpref.IsEmpty())
00675   {
00676     rv = rdf->GetResource(oldpref, getter_AddRefs(res));
00677     if (NS_SUCCEEDED(rv) && res)
00678     {
00679       folder = do_QueryInterface(res, &rv);
00680       if (NS_SUCCEEDED(rv))
00681         rv = folder->ClearFlag(folderflag);
00682     }
00683   }
00684   
00685   // set the new folder, and set the special folder flags on it
00686   rv = setCharPref(prefname, value);
00687   if (NS_SUCCEEDED(rv) && value && *value)
00688   {
00689     rv = rdf->GetResource(nsDependentCString(value), getter_AddRefs(res));
00690     if (NS_SUCCEEDED(rv) && res)
00691     {
00692       folder = do_QueryInterface(res, &rv);
00693       if (NS_SUCCEEDED(rv))
00694         rv = folder->SetFlag(folderflag);
00695     }
00696   }
00697   return rv;
00698 }
00699 
00700 NS_IMETHODIMP nsMsgIdentity::SetUnicharAttribute(const char *aName, const PRUnichar *val)
00701 {
00702   return setUnicharPref(aName, val);
00703 }
00704 
00705 NS_IMETHODIMP nsMsgIdentity::GetUnicharAttribute(const char *aName, PRUnichar **val)
00706 {
00707   return getUnicharPref(aName, val);
00708 }
00709 
00710 NS_IMETHODIMP nsMsgIdentity::SetCharAttribute(const char *aName, const char *val)
00711 {
00712   return setCharPref(aName, val);
00713 }
00714 
00715 NS_IMETHODIMP nsMsgIdentity::GetCharAttribute(const char *aName, char **val)
00716 {
00717   return getCharPref(aName, val);
00718 }
00719 
00720 NS_IMETHODIMP nsMsgIdentity::SetBoolAttribute(const char *aName, PRBool val)
00721 {
00722   return setBoolPref(aName, val);
00723 }
00724 
00725 NS_IMETHODIMP nsMsgIdentity::GetBoolAttribute(const char *aName, PRBool *val)
00726 {
00727   return getBoolPref(aName, val);
00728 }
00729 
00730 NS_IMETHODIMP nsMsgIdentity::SetIntAttribute(const char *aName, PRInt32 val)
00731 {
00732   return setIntPref(aName, val);
00733 }
00734 
00735 NS_IMETHODIMP nsMsgIdentity::GetIntAttribute(const char *aName, PRInt32 *val)
00736 {
00737   return getIntPref(aName, val);
00738 }
00739 
00740 #define COPY_IDENTITY_FILE_VALUE(SRC_ID,MACRO_GETTER,MACRO_SETTER)    \
00741        {      \
00742               nsresult macro_rv;   \
00743               nsCOMPtr <nsILocalFile>macro_spec;   \
00744               macro_rv = SRC_ID->MACRO_GETTER(getter_AddRefs(macro_spec)); \
00745               if (NS_SUCCEEDED(macro_rv)) \
00746                 this->MACRO_SETTER(macro_spec);     \
00747        }
00748 
00749 #define COPY_IDENTITY_INT_VALUE(SRC_ID,MACRO_GETTER,MACRO_SETTER)     \
00750        {      \
00751                   nsresult macro_rv;      \
00752               PRInt32 macro_oldInt;       \
00753               macro_rv = SRC_ID->MACRO_GETTER(&macro_oldInt);  \
00754               if (NS_SUCCEEDED(macro_rv)) \
00755                 this->MACRO_SETTER(macro_oldInt);     \
00756        }
00757 
00758 #define COPY_IDENTITY_BOOL_VALUE(SRC_ID,MACRO_GETTER,MACRO_SETTER)    \
00759        {      \
00760                   nsresult macro_rv;      \
00761               PRBool macro_oldBool;       \
00762               macro_rv = SRC_ID->MACRO_GETTER(&macro_oldBool); \
00763               if (NS_SUCCEEDED(macro_rv)) \
00764                 this->MACRO_SETTER(macro_oldBool);     \
00765        }
00766 
00767 #define COPY_IDENTITY_STR_VALUE(SRC_ID,MACRO_GETTER,MACRO_SETTER)     \
00768        {      \
00769               nsXPIDLCString macro_oldStr;       \
00770                   nsresult macro_rv;      \
00771               macro_rv = SRC_ID->MACRO_GETTER(getter_Copies(macro_oldStr));  \
00772             if (NS_SUCCEEDED(macro_rv)) { \
00773                 if (!macro_oldStr) {      \
00774                      this->MACRO_SETTER("");     \
00775               }      \
00776                 else {      \
00777                      this->MACRO_SETTER(macro_oldStr);  \
00778               }      \
00779             } \
00780        }
00781 
00782 static const PRUnichar unicharEmptyString[] = { (PRUnichar)'\0' };
00783 
00784 #define COPY_IDENTITY_WSTR_VALUE(SRC_ID,MACRO_GETTER,MACRO_SETTER)    \
00785        {      \
00786               nsXPIDLString macro_oldStr; \
00787                   nsresult macro_rv;      \
00788               macro_rv = SRC_ID->MACRO_GETTER(getter_Copies(macro_oldStr)); \
00789               if (NS_SUCCEEDED(macro_rv)) { \
00790                 if (!macro_oldStr) {      \
00791                      this->MACRO_SETTER(unicharEmptyString);   \
00792               }      \
00793                 else {      \
00794                      this->MACRO_SETTER(macro_oldStr);  \
00795               }      \
00796             } \
00797        }
00798 
00799 NS_IMETHODIMP
00800 nsMsgIdentity::Copy(nsIMsgIdentity *identity)
00801 {
00802     COPY_IDENTITY_BOOL_VALUE(identity,GetComposeHtml,SetComposeHtml)
00803     COPY_IDENTITY_STR_VALUE(identity,GetEmail,SetEmail)
00804     COPY_IDENTITY_STR_VALUE(identity,GetReplyTo,SetReplyTo)
00805     COPY_IDENTITY_WSTR_VALUE(identity,GetFullName,SetFullName)
00806     COPY_IDENTITY_WSTR_VALUE(identity,GetOrganization,SetOrganization)
00807     COPY_IDENTITY_STR_VALUE(identity,GetDraftFolder,SetDraftFolder)
00808     COPY_IDENTITY_STR_VALUE(identity,GetFccFolder,SetFccFolder)
00809     COPY_IDENTITY_BOOL_VALUE(identity,GetFccReplyFollowsParent,
00810                              SetFccReplyFollowsParent)
00811     COPY_IDENTITY_STR_VALUE(identity,GetStationeryFolder,SetStationeryFolder)
00812     COPY_IDENTITY_BOOL_VALUE(identity,GetAttachSignature,SetAttachSignature)
00813     COPY_IDENTITY_FILE_VALUE(identity,GetSignature,SetSignature)
00814     COPY_IDENTITY_BOOL_VALUE(identity,GetAutoQuote,SetAutoQuote)
00815     COPY_IDENTITY_INT_VALUE(identity,GetReplyOnTop,SetReplyOnTop)
00816     COPY_IDENTITY_BOOL_VALUE(identity,GetSigBottom,SetSigBottom)
00817     COPY_IDENTITY_INT_VALUE(identity,GetSignatureDate,SetSignatureDate)
00818     COPY_IDENTITY_BOOL_VALUE(identity,GetAttachVCard,SetAttachVCard)
00819     COPY_IDENTITY_STR_VALUE(identity,GetEscapedVCard,SetEscapedVCard)
00820     COPY_IDENTITY_STR_VALUE(identity,GetSmtpServerKey,SetSmtpServerKey)
00821     return NS_OK;
00822 }
00823 
00824 NS_IMETHODIMP
00825 nsMsgIdentity::GetRequestReturnReceipt(PRBool *aVal)
00826 {
00827   NS_ENSURE_ARG_POINTER(aVal);
00828 
00829   PRBool useCustomPrefs = PR_FALSE;
00830   nsresult rv = GetBoolAttribute("use_custom_prefs", &useCustomPrefs);
00831   NS_ENSURE_SUCCESS(rv, rv);
00832   if (useCustomPrefs)
00833     return GetBoolAttribute("request_return_receipt_on", aVal);
00834 
00835   rv = getPrefService();
00836   NS_ENSURE_SUCCESS(rv, rv);
00837   return m_prefBranch->GetBoolPref("mail.receipt.request_return_receipt_on", aVal);
00838 }
00839 
00840 NS_IMETHODIMP
00841 nsMsgIdentity::GetReceiptHeaderType(PRInt32 *aType)
00842 {
00843   NS_ENSURE_ARG_POINTER(aType);
00844 
00845   PRBool useCustomPrefs = PR_FALSE;
00846   nsresult rv = GetBoolAttribute("use_custom_prefs", &useCustomPrefs);
00847   NS_ENSURE_SUCCESS(rv, rv);
00848   if (useCustomPrefs)
00849     return GetIntAttribute("request_receipt_header_type", aType);
00850 
00851   rv = getPrefService();
00852   NS_ENSURE_SUCCESS(rv, rv);
00853   return m_prefBranch->GetIntPref("mail.receipt.request_header_type", aType);
00854 }