Back to index

lightning-sunbird  0.9+nobinonly
nsMsgCompFields.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either of the GNU General Public License Version 2 or later (the "GPL"),
00027  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsMsgCompFields.h"
00040 #include "nsCRT.h"
00041 #include "nsMsgI18N.h"
00042 #include "nsMsgComposeStringBundle.h"
00043 #include "nsMsgRecipientArray.h"
00044 #include "nsIMsgHeaderParser.h"
00045 #include "nsMsgCompUtils.h"
00046 #include "nsMsgUtils.h"
00047 #include "prmem.h"
00048 #include "nsIFileChannel.h"
00049 #include "nsReadableUtils.h"
00050 #include "nsIMsgMdnGenerator.h"
00051 
00052 /* the following macro actually implement addref, release and query interface for our component. */
00053 NS_IMPL_THREADSAFE_ISUPPORTS1(nsMsgCompFields, nsIMsgCompFields)
00054 
00055 nsMsgCompFields::nsMsgCompFields()
00056 {
00057   PRInt16 i;
00058   for (i = 0; i < MSG_MAX_HEADERS; i ++)
00059     m_headers[i] = nsnull;
00060 
00061   m_body.Truncate();
00062 
00063   NS_NewISupportsArray(getter_AddRefs(m_attachments));
00064 
00065   m_attachVCard = PR_FALSE;
00066   m_forcePlainText = PR_FALSE;
00067   m_useMultipartAlternative = PR_FALSE;
00068   m_uuEncodeAttachments = PR_FALSE;
00069   m_returnReceipt = PR_FALSE;
00070   m_receiptHeaderType = nsIMsgMdnGenerator::eDntType;
00071   m_bodyIsAsciiOnly = PR_FALSE;
00072   m_forceMsgEncoding = PR_FALSE;
00073   m_needToCheckCharset = PR_TRUE;
00074 
00075   // Get the default charset from pref, use this as a mail charset.
00076   nsXPIDLString charset;
00077   NS_GetLocalizedUnicharPreferenceWithDefault(nsnull, "mailnews.send_default_charset", 
00078                                               NS_LITERAL_STRING("ISO-8859-1"), charset);
00079 
00080   LossyCopyUTF16toASCII(charset, m_DefaultCharacterSet); // Charsets better be ASCII
00081   SetCharacterSet(m_DefaultCharacterSet.get());
00082 }
00083 
00084 nsMsgCompFields::~nsMsgCompFields()
00085 {
00086   PRInt16 i;
00087   for (i = 0; i < MSG_MAX_HEADERS; i ++)
00088     PR_FREEIF(m_headers[i]);
00089 }
00090 
00091 nsresult nsMsgCompFields::SetAsciiHeader(MsgHeaderID header, const char *value)
00092 {
00093   NS_ASSERTION(header >= 0 && header < MSG_MAX_HEADERS,
00094                "Invalid message header index!");
00095 
00096   int rv = NS_OK;
00097   char* old = m_headers[header]; /* Done with careful paranoia, in case the
00098                                     value given is the old value (or worse,
00099                                     a substring of the old value, as does
00100                                     happen here and there.)
00101                                   */
00102   if (value != old)
00103   {
00104     if (value)
00105     {
00106         m_headers[header] = nsCRT::strdup(value);
00107         if (!m_headers[header]) 
00108            rv = NS_ERROR_OUT_OF_MEMORY;
00109     }
00110     else 
00111       m_headers[header] = nsnull;
00112 
00113     PR_FREEIF(old);
00114   }
00115 
00116   return rv;
00117 }
00118 
00119 const char* nsMsgCompFields::GetAsciiHeader(MsgHeaderID header)
00120 {
00121   NS_ASSERTION(header >= 0 && header < MSG_MAX_HEADERS,
00122                "Invalid message header index!");
00123 
00124   return m_headers[header] ? m_headers[header] : "";
00125 }
00126 
00127 nsresult nsMsgCompFields::SetUnicodeHeader(MsgHeaderID header, const nsAString& value)
00128 {
00129   return SetAsciiHeader(header, NS_ConvertUTF16toUTF8(value).get());
00130 }
00131 
00132 nsresult nsMsgCompFields::GetUnicodeHeader(MsgHeaderID header, nsAString& aResult)
00133 {
00134   CopyUTF8toUTF16(GetAsciiHeader(header), aResult);
00135   return NS_OK;
00136 }
00137 
00138 NS_IMETHODIMP nsMsgCompFields::SetFrom(const nsAString &value)
00139 {
00140   return SetUnicodeHeader(MSG_FROM_HEADER_ID, value);
00141 }
00142 
00143 
00144 NS_IMETHODIMP nsMsgCompFields::GetFrom(nsAString &_retval)
00145 {
00146   return GetUnicodeHeader(MSG_FROM_HEADER_ID, _retval);
00147 }
00148 
00149 NS_IMETHODIMP nsMsgCompFields::SetReplyTo(const nsAString &value)
00150 {
00151   return SetUnicodeHeader(MSG_REPLY_TO_HEADER_ID, value);
00152 }
00153 
00154 NS_IMETHODIMP nsMsgCompFields::GetReplyTo(nsAString &_retval)
00155 {
00156   return GetUnicodeHeader(MSG_REPLY_TO_HEADER_ID, _retval);
00157 }
00158 
00159 NS_IMETHODIMP nsMsgCompFields::SetTo(const nsAString &value)
00160 {
00161   return SetUnicodeHeader(MSG_TO_HEADER_ID, value);
00162 }
00163 
00164 NS_IMETHODIMP nsMsgCompFields::GetTo(nsAString &_retval)
00165 {
00166   return GetUnicodeHeader(MSG_TO_HEADER_ID, _retval);
00167 }
00168 
00169 NS_IMETHODIMP nsMsgCompFields::SetCc(const nsAString &value)
00170 {
00171   return SetUnicodeHeader(MSG_CC_HEADER_ID, value);
00172 }
00173 
00174 NS_IMETHODIMP nsMsgCompFields::GetCc(nsAString &_retval)
00175 {
00176   return GetUnicodeHeader(MSG_CC_HEADER_ID, _retval);
00177 }
00178 
00179 NS_IMETHODIMP nsMsgCompFields::SetBcc(const nsAString &value)
00180 {
00181   return SetUnicodeHeader(MSG_BCC_HEADER_ID, value);
00182 }
00183 
00184 NS_IMETHODIMP nsMsgCompFields::GetBcc(nsAString &_retval)
00185 {
00186   return GetUnicodeHeader(MSG_BCC_HEADER_ID, _retval);
00187 }
00188 
00189 NS_IMETHODIMP nsMsgCompFields::SetFcc(const nsAString &value)
00190 {
00191   return SetUnicodeHeader(MSG_FCC_HEADER_ID, value);
00192 }
00193 
00194 NS_IMETHODIMP nsMsgCompFields::GetFcc(nsAString &_retval)
00195 {
00196   return GetUnicodeHeader(MSG_FCC_HEADER_ID, _retval);
00197 }
00198 
00199 NS_IMETHODIMP nsMsgCompFields::SetFcc2(const nsAString &value)
00200 {
00201   return SetUnicodeHeader(MSG_FCC2_HEADER_ID, value);
00202 }
00203 
00204 NS_IMETHODIMP nsMsgCompFields::GetFcc2(nsAString &_retval)
00205 {
00206   return GetUnicodeHeader(MSG_FCC2_HEADER_ID, _retval);
00207 }
00208 
00209 NS_IMETHODIMP nsMsgCompFields::SetNewsgroups(const nsAString &aValue)
00210 {
00211   return SetUnicodeHeader(MSG_NEWSGROUPS_HEADER_ID, aValue);
00212 }
00213 
00214 NS_IMETHODIMP nsMsgCompFields::GetNewsgroups(nsAString &aGroup)
00215 {
00216   return GetUnicodeHeader(MSG_NEWSGROUPS_HEADER_ID, aGroup);
00217 }
00218 
00219 NS_IMETHODIMP nsMsgCompFields::SetNewshost(const char *value)
00220 {
00221   return SetAsciiHeader(MSG_NEWSPOSTURL_HEADER_ID, value);
00222 }
00223 
00224 NS_IMETHODIMP nsMsgCompFields::GetNewshost(char **_retval)
00225 {
00226   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_NEWSPOSTURL_HEADER_ID));
00227   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00228 }
00229 
00230 NS_IMETHODIMP nsMsgCompFields::SetFollowupTo(const char *value)
00231 {
00232   return SetAsciiHeader(MSG_FOLLOWUP_TO_HEADER_ID, value);
00233 }
00234 
00235 NS_IMETHODIMP nsMsgCompFields::GetFollowupTo(char **_retval)
00236 {
00237   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_FOLLOWUP_TO_HEADER_ID));
00238   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00239 }
00240 
00241 NS_IMETHODIMP nsMsgCompFields::SetSubject(const nsAString &value)
00242 {
00243   return SetUnicodeHeader(MSG_SUBJECT_HEADER_ID, value);
00244 }
00245 
00246 NS_IMETHODIMP nsMsgCompFields::GetSubject(nsAString &_retval)
00247 {
00248   return GetUnicodeHeader(MSG_SUBJECT_HEADER_ID, _retval);
00249 }
00250 
00251 NS_IMETHODIMP nsMsgCompFields::SetAttachments(const char *value)
00252 {
00253   NS_ASSERTION(0, "nsMsgCompFields::SetAttachments is not supported anymore, please use nsMsgCompFields::AddAttachment");
00254   return SetAsciiHeader(MSG_ATTACHMENTS_HEADER_ID, value);
00255 }
00256 
00257 NS_IMETHODIMP nsMsgCompFields::SetTemporaryFiles(const char *value)
00258 {
00259   NS_ASSERTION(0, "nsMsgCompFields::SetTemporaryFiles is not supported anymore, please use nsMsgCompFields::AddAttachment");
00260   return SetAsciiHeader(MSG_TEMPORARY_FILES_HEADER_ID, value);
00261 }
00262 
00263 NS_IMETHODIMP nsMsgCompFields::GetAttachments(char **_retval)
00264 {
00265   NS_ASSERTION(0, "nsMsgCompFields::GetAttachments is not supported anymore, please use nsMsgCompFields::GetAttachmentsArray");
00266   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_ATTACHMENTS_HEADER_ID));
00267   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00268 }
00269 
00270 NS_IMETHODIMP nsMsgCompFields::GetTemporaryFiles(char **_retval)
00271 {
00272   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_TEMPORARY_FILES_HEADER_ID));
00273   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00274 }
00275 
00276 NS_IMETHODIMP nsMsgCompFields::SetOrganization(const nsAString &value)
00277 {
00278   return SetUnicodeHeader(MSG_ORGANIZATION_HEADER_ID, value);
00279 }
00280 
00281 NS_IMETHODIMP nsMsgCompFields::GetOrganization(nsAString &_retval)
00282 {
00283   return GetUnicodeHeader(MSG_ORGANIZATION_HEADER_ID, _retval);
00284 }
00285 
00286 NS_IMETHODIMP nsMsgCompFields::SetReferences(const char *value)
00287 {
00288   return SetAsciiHeader(MSG_REFERENCES_HEADER_ID, value);
00289 }
00290 
00291 NS_IMETHODIMP nsMsgCompFields::GetReferences(char **_retval)
00292 {
00293   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_REFERENCES_HEADER_ID));
00294   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00295 }
00296 
00297 NS_IMETHODIMP nsMsgCompFields::SetOtherRandomHeaders(const nsAString &value)
00298 {
00299   return SetUnicodeHeader(MSG_OTHERRANDOMHEADERS_HEADER_ID, value);
00300 }
00301 
00302 NS_IMETHODIMP nsMsgCompFields::GetOtherRandomHeaders(nsAString &_retval)
00303 {
00304   return GetUnicodeHeader(MSG_OTHERRANDOMHEADERS_HEADER_ID, _retval);
00305 }
00306 
00307 NS_IMETHODIMP nsMsgCompFields::SetNewspostUrl(const char *value)
00308 {
00309   return SetAsciiHeader(MSG_NEWSPOSTURL_HEADER_ID, value);
00310 }
00311 
00312 NS_IMETHODIMP nsMsgCompFields::GetNewspostUrl(char **_retval)
00313 {
00314   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_NEWSPOSTURL_HEADER_ID));
00315   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00316 }
00317 
00318 NS_IMETHODIMP nsMsgCompFields::SetPriority(const char *value)
00319 {
00320   return SetAsciiHeader(MSG_PRIORITY_HEADER_ID, value);
00321 }
00322 
00323 NS_IMETHODIMP nsMsgCompFields::GetPriority(char **_retval)
00324 {
00325   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_PRIORITY_HEADER_ID));
00326   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00327 }
00328 
00329 NS_IMETHODIMP nsMsgCompFields::SetCharacterSet(const char *value)
00330 {
00331   return SetAsciiHeader(MSG_CHARACTER_SET_HEADER_ID, value);
00332 }
00333 
00334 NS_IMETHODIMP nsMsgCompFields::GetCharacterSet(char **_retval)
00335 {
00336   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_CHARACTER_SET_HEADER_ID));
00337   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00338 }
00339 
00340 NS_IMETHODIMP nsMsgCompFields::SetMessageId(const char *value)
00341 {
00342   return SetAsciiHeader(MSG_MESSAGE_ID_HEADER_ID, value);
00343 }
00344 
00345 NS_IMETHODIMP nsMsgCompFields::GetMessageId(char **_retval)
00346 {
00347   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_MESSAGE_ID_HEADER_ID));
00348   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00349 }
00350 
00351 NS_IMETHODIMP nsMsgCompFields::SetTemplateName(const nsAString &value)
00352 {
00353   return SetUnicodeHeader(MSG_X_TEMPLATE_HEADER_ID, value);
00354 }
00355 
00356 NS_IMETHODIMP nsMsgCompFields::GetTemplateName(nsAString &_retval)
00357 {
00358   return GetUnicodeHeader(MSG_X_TEMPLATE_HEADER_ID, _retval);
00359 }
00360 
00361 NS_IMETHODIMP nsMsgCompFields::SetDraftId(const char *value)
00362 {
00363   return SetAsciiHeader(MSG_DRAFT_ID_HEADER_ID, value);
00364 }
00365 
00366 NS_IMETHODIMP nsMsgCompFields::GetDraftId(char **_retval)
00367 {
00368   *_retval = nsCRT::strdup(GetAsciiHeader(MSG_DRAFT_ID_HEADER_ID));
00369   return *_retval ? NS_OK : NS_ERROR_OUT_OF_MEMORY;
00370 }
00371 
00372 NS_IMETHODIMP nsMsgCompFields::SetReturnReceipt(PRBool value)
00373 {
00374   m_returnReceipt = value;
00375   return NS_OK;
00376 }
00377 
00378 NS_IMETHODIMP nsMsgCompFields::GetReturnReceipt(PRBool *_retval)
00379 {
00380   *_retval = m_returnReceipt;
00381   return NS_OK;
00382 }
00383 
00384 NS_IMETHODIMP nsMsgCompFields::SetReceiptHeaderType(PRInt32 value)
00385 {
00386     m_receiptHeaderType = value;
00387     return NS_OK;
00388 }
00389 
00390 NS_IMETHODIMP nsMsgCompFields::GetReceiptHeaderType(PRInt32 *_retval)
00391 {
00392     *_retval = m_receiptHeaderType;
00393     return NS_OK;
00394 }
00395 
00396 NS_IMETHODIMP nsMsgCompFields::SetAttachVCard(PRBool value)
00397 {
00398   m_attachVCard = value;
00399   return NS_OK;
00400 }
00401 
00402 NS_IMETHODIMP nsMsgCompFields::GetAttachVCard(PRBool *_retval)
00403 {
00404   *_retval = m_attachVCard;
00405   return NS_OK;
00406 }
00407 
00408 NS_IMETHODIMP nsMsgCompFields::SetForcePlainText(PRBool value)
00409 {
00410   m_forcePlainText = value;
00411   return NS_OK;
00412 }
00413 
00414 NS_IMETHODIMP nsMsgCompFields::GetForcePlainText(PRBool *_retval)
00415 {
00416   *_retval = m_forcePlainText;
00417   return NS_OK;
00418 }
00419 
00420 NS_IMETHODIMP nsMsgCompFields::SetForceMsgEncoding(PRBool value)
00421 {
00422   m_forceMsgEncoding = value;
00423   return NS_OK;
00424 }
00425 
00426 NS_IMETHODIMP nsMsgCompFields::GetForceMsgEncoding(PRBool *_retval)
00427 {
00428   NS_ENSURE_ARG_POINTER(_retval);
00429   *_retval = m_forceMsgEncoding;
00430   return NS_OK;
00431 }
00432 
00433 NS_IMETHODIMP nsMsgCompFields::SetUseMultipartAlternative(PRBool value)
00434 {
00435   m_useMultipartAlternative = value;
00436   return NS_OK;
00437 }
00438 
00439 NS_IMETHODIMP nsMsgCompFields::GetUseMultipartAlternative(PRBool *_retval)
00440 {
00441   *_retval = m_useMultipartAlternative;
00442   return NS_OK;
00443 }
00444 
00445 NS_IMETHODIMP nsMsgCompFields::SetUuEncodeAttachments(PRBool value)
00446 {
00447   m_uuEncodeAttachments = value;
00448   return NS_OK;
00449 }
00450 
00451 NS_IMETHODIMP nsMsgCompFields::GetUuEncodeAttachments(PRBool *_retval)
00452 {
00453   *_retval = m_uuEncodeAttachments;
00454   return NS_OK;
00455 }
00456 
00457 NS_IMETHODIMP nsMsgCompFields::SetBodyIsAsciiOnly(PRBool value)
00458 {
00459   m_bodyIsAsciiOnly = value;
00460   return NS_OK;
00461 }
00462 
00463 NS_IMETHODIMP nsMsgCompFields::GetBodyIsAsciiOnly(PRBool *_retval)
00464 {
00465   NS_ENSURE_ARG_POINTER(_retval);
00466 
00467   *_retval = m_bodyIsAsciiOnly;
00468   return NS_OK;
00469 }
00470 
00471 NS_IMETHODIMP nsMsgCompFields::SetBody(const nsAString &value)
00472 {
00473   CopyUTF16toUTF8(value, m_body);
00474   return NS_OK;
00475 }
00476 
00477 NS_IMETHODIMP nsMsgCompFields::GetBody(nsAString &_retval)
00478 {
00479   CopyUTF8toUTF16(m_body, _retval);
00480   return NS_OK;
00481 }
00482 
00483 nsresult nsMsgCompFields::SetBody(const char *value)
00484 {
00485   if (value)
00486     m_body = value;
00487   else
00488     m_body.Truncate();
00489   return NS_OK;
00490 }
00491 
00492 const char* nsMsgCompFields::GetBody()
00493 {
00494     return m_body.get();
00495 }
00496 
00497 /* readonly attribute nsISupportsArray attachmentsArray; */
00498 NS_IMETHODIMP nsMsgCompFields::GetAttachmentsArray(nsISupportsArray * *aAttachmentsArray)
00499 {
00500   NS_ENSURE_ARG_POINTER(aAttachmentsArray);
00501   *aAttachmentsArray = m_attachments;
00502   NS_IF_ADDREF(*aAttachmentsArray);
00503   return NS_OK;
00504 }
00505 
00506 /* void addAttachment (in nsIMsgAttachment attachment); */
00507 NS_IMETHODIMP nsMsgCompFields::AddAttachment(nsIMsgAttachment *attachment)
00508 {
00509   PRUint32 i;
00510   PRUint32 attachmentCount = 0;
00511   m_attachments->Count(&attachmentCount);
00512 
00513   //Don't add twice the same attachment.
00514   nsCOMPtr<nsIMsgAttachment> element;
00515   PRBool sameUrl;
00516   for (i = 0; i < attachmentCount; i ++)
00517   {
00518     m_attachments->QueryElementAt(i, NS_GET_IID(nsIMsgAttachment), getter_AddRefs(element));
00519     if (element)
00520     {
00521       element->EqualsUrl(attachment, &sameUrl);
00522       if (sameUrl)
00523         return NS_OK;
00524     }
00525   }
00526 
00527   return m_attachments->InsertElementAt(attachment, attachmentCount);
00528 }
00529 
00530 /* void removeAttachment (in nsIMsgAttachment attachment); */
00531 NS_IMETHODIMP nsMsgCompFields::RemoveAttachment(nsIMsgAttachment *attachment)
00532 {
00533   PRUint32 i;
00534   PRUint32 attachmentCount = 0;
00535   m_attachments->Count(&attachmentCount);
00536 
00537   nsCOMPtr<nsIMsgAttachment> element;
00538   PRBool sameUrl;
00539   for (i = 0; i < attachmentCount; i ++)
00540   {
00541     m_attachments->QueryElementAt(i, NS_GET_IID(nsIMsgAttachment), getter_AddRefs(element));
00542     if (element)
00543     {
00544       element->EqualsUrl(attachment, &sameUrl);
00545       if (sameUrl)
00546       {
00547         m_attachments->DeleteElementAt(i);
00548         break;
00549       }
00550     }
00551   }
00552 
00553   return NS_OK;
00554 }
00555 
00556 /* void removeAttachments (); */
00557 NS_IMETHODIMP nsMsgCompFields::RemoveAttachments()
00558 {
00559   PRUint32 i;
00560   PRUint32 attachmentCount = 0;
00561   m_attachments->Count(&attachmentCount);
00562 
00563   for (i = 0; i < attachmentCount; i ++)
00564     m_attachments->DeleteElementAt(0);
00565 
00566   return NS_OK;
00567 }
00568 
00569 
00570 // This method is called during the creation of a new window.
00571 NS_IMETHODIMP nsMsgCompFields::SplitRecipients(const PRUnichar *recipients, PRBool emailAddressOnly, nsIMsgRecipientArray **_retval)
00572 {
00573   NS_ASSERTION(recipients, "The recipient list is not supposed to be null -Fix the caller!");
00574 
00575   nsresult rv = NS_OK;
00576 
00577        if (! _retval)
00578               return NS_ERROR_NULL_POINTER;
00579        *_retval = nsnull;
00580               
00581        nsMsgRecipientArray* pAddrArray = new nsMsgRecipientArray;
00582        if (! pAddrArray)
00583               return NS_ERROR_OUT_OF_MEMORY;
00584        
00585        rv = pAddrArray->QueryInterface(NS_GET_IID(nsIMsgRecipientArray), (void **)_retval);
00586        if (NS_SUCCEEDED(rv))
00587        {
00588               nsCOMPtr<nsIMsgHeaderParser> parser = do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID);
00589               if (parser)
00590               {
00591                      nsCAutoString recipientsStr;
00592                      char * names;
00593                      char * addresses;
00594                      PRUint32 numAddresses;
00595 
00596                      CopyUTF16toUTF8(recipients, recipientsStr);
00597                      
00598                      rv= parser->ParseHeaderAddresses("UTF-8", recipientsStr.get(), &names, 
00599                                         &addresses, &numAddresses);
00600                      if (NS_SUCCEEDED(rv))
00601                      {
00602                             PRUint32 i=0;
00603                             char * pNames = names;
00604                             char * pAddresses = addresses;
00605                             PRBool aBool;
00606                             
00607         for (i = 0; i < numAddresses; i ++)
00608         {
00609           nsXPIDLCString fullAddress;
00610           nsAutoString recipient;
00611           if (!emailAddressOnly)
00612             rv = parser->MakeFullAddress("UTF-8", pNames,
00613                                          pAddresses, getter_Copies(fullAddress));
00614           if (NS_SUCCEEDED(rv) && !emailAddressOnly)
00615           {
00616             rv = ConvertToUnicode("UTF-8", fullAddress, recipient);
00617           }
00618           else
00619             rv = ConvertToUnicode("UTF-8", nsDependentCString(pAddresses), recipient);
00620           if (NS_FAILED(rv))
00621             break;
00622 
00623           rv = pAddrArray->AppendString(recipient.get(), &aBool);
00624           if (NS_FAILED(rv))
00625             break;
00626                                           
00627                                    pNames += PL_strlen(pNames) + 1;
00628                                    pAddresses += PL_strlen(pAddresses) + 1;
00629                             }
00630                      
00631                             PR_FREEIF(names);
00632                             PR_FREEIF(addresses);
00633                      }
00634               }
00635               else
00636                      rv = NS_ERROR_FAILURE;
00637        }
00638               
00639        return rv;
00640 }
00641 
00642 
00643 // This method is called during the sending of message from nsMsgCompose::CheckAndPopulateRecipients()
00644 nsresult nsMsgCompFields::SplitRecipientsEx(const PRUnichar *recipients, nsIMsgRecipientArray ** fullAddrsArray, nsIMsgRecipientArray ** emailsArray)
00645 {
00646   NS_ASSERTION(recipients, "The recipient list is not supposed to be null -Fix the caller!");
00647 
00648   nsresult rv = NS_OK;
00649 
00650   nsMsgRecipientArray* pAddrsArray = nsnull;
00651   if (fullAddrsArray)
00652   {
00653     *fullAddrsArray = nsnull;
00654     pAddrsArray = new nsMsgRecipientArray;
00655     if (! pAddrsArray)
00656       return NS_ERROR_OUT_OF_MEMORY;
00657     rv = pAddrsArray->QueryInterface(NS_GET_IID(nsIMsgRecipientArray), (void **)fullAddrsArray);
00658     if (NS_FAILED(rv))
00659       return rv;
00660   }
00661   
00662   nsMsgRecipientArray* pEmailsArray = nsnull;
00663   if (emailsArray)
00664   {
00665     *emailsArray = nsnull;
00666     pEmailsArray = new nsMsgRecipientArray;
00667     if (! pEmailsArray)
00668       return NS_ERROR_OUT_OF_MEMORY;
00669     rv = pEmailsArray->QueryInterface(NS_GET_IID(nsIMsgRecipientArray), (void **)emailsArray);
00670     if (NS_FAILED(rv))
00671       return rv;
00672   }
00673        
00674        if (pAddrsArray || pEmailsArray)
00675        {
00676               nsCOMPtr<nsIMsgHeaderParser> parser = do_GetService(NS_MAILNEWS_MIME_HEADER_PARSER_CONTRACTID);
00677               if (parser)
00678               {
00679                      nsCAutoString recipientsStr;
00680                      char * names;
00681                      char *addresses;
00682                      PRUint32 numAddresses;
00683 
00684                         CopyUTF16toUTF8(recipients, recipientsStr);
00685                      rv= parser->ParseHeaderAddresses("UTF-8", recipientsStr.get(), &names,
00686                                        &addresses, &numAddresses);
00687                      if (NS_SUCCEEDED(rv))
00688                      {
00689                             PRUint32 i=0;
00690                             char * pNames = names;
00691                             char * pAddresses = addresses;
00692                             nsAutoString recipient;
00693                             PRBool aBool;
00694                             
00695         for (i = 0; i < numAddresses; i ++)
00696         {
00697           nsXPIDLCString fullAddress;
00698           if (pAddrsArray)
00699           {
00700             rv = parser->MakeFullAddress("UTF-8", pNames, pAddresses, 
00701                                          getter_Copies(fullAddress));
00702             if (NS_SUCCEEDED(rv))
00703             {
00704               rv = ConvertToUnicode("UTF-8", fullAddress, recipient);
00705             }
00706             else
00707               rv = ConvertToUnicode("UTF-8", pAddresses, recipient);
00708             if (NS_FAILED(rv))
00709               return rv;
00710               
00711             rv = pAddrsArray->AppendString(recipient.get(), &aBool);
00712             if (NS_FAILED(rv))
00713               return rv;
00714           }
00715 
00716           if (pEmailsArray)
00717           {
00718             rv = ConvertToUnicode("UTF-8", pAddresses, recipient);
00719             if (NS_FAILED(rv))
00720               return rv;
00721             rv = pEmailsArray->AppendString(recipient.get(), &aBool);
00722             if (NS_FAILED(rv))
00723               return rv;
00724           }
00725 
00726           pNames += PL_strlen(pNames) + 1;
00727           pAddresses += PL_strlen(pAddresses) + 1;
00728         }
00729       
00730                             PR_FREEIF(names);
00731                             PR_FREEIF(addresses);
00732       }
00733     }
00734     else
00735       rv = NS_ERROR_FAILURE;
00736   }
00737     
00738   return rv;
00739 }
00740 
00741 NS_IMETHODIMP nsMsgCompFields::ConvertBodyToPlainText()
00742 {
00743   nsresult rv = NS_OK;
00744   
00745   if (!m_body.IsEmpty())
00746   {
00747     nsAutoString body;
00748     rv = GetBody(body);
00749     if (NS_SUCCEEDED(rv))
00750     {
00751       rv = ConvertBufToPlainText(body, UseFormatFlowed(GetCharacterSet()));
00752       if (NS_SUCCEEDED(rv))
00753         rv = SetBody(body);
00754     }
00755   }
00756   return rv;
00757 }
00758 
00759 NS_IMETHODIMP nsMsgCompFields::GetSecurityInfo(nsISupports ** aSecurityInfo)
00760 {
00761   NS_ENSURE_ARG_POINTER(aSecurityInfo);
00762   *aSecurityInfo = mSecureCompFields;
00763   NS_IF_ADDREF(*aSecurityInfo);
00764   return NS_OK;
00765 }
00766 
00767 NS_IMETHODIMP nsMsgCompFields::SetSecurityInfo(nsISupports * aSecurityInfo)
00768 {
00769   mSecureCompFields = aSecurityInfo;
00770   return NS_OK;
00771 }
00772 
00773 NS_IMETHODIMP nsMsgCompFields::GetDefaultCharacterSet(char * *aDefaultCharacterSet)
00774 {
00775   NS_ENSURE_ARG_POINTER(aDefaultCharacterSet);
00776   *aDefaultCharacterSet = nsCRT::strdup(m_DefaultCharacterSet.get());
00777   return *aDefaultCharacterSet ? NS_OK : NS_ERROR_OUT_OF_MEMORY; 
00778 }
00779 
00780 NS_IMETHODIMP nsMsgCompFields::CheckCharsetConversion(char **fallbackCharset, PRBool *_retval)
00781 {
00782   NS_ENSURE_ARG_POINTER(_retval);
00783 
00784   nsCAutoString headers;
00785   for (PRInt16 i = 0; i < MSG_MAX_HEADERS; i++)
00786     headers.Append(m_headers[i]);
00787 
00788   // charset conversion check
00789   *_retval = nsMsgI18Ncheck_data_in_charset_range(GetCharacterSet(), NS_ConvertUTF8toUCS2(headers.get()).get(),
00790                                                   fallbackCharset);
00791 
00792   return NS_OK;
00793 }
00794 
00795 NS_IMETHODIMP nsMsgCompFields::GetNeedToCheckCharset(PRBool *_retval)
00796 {
00797   NS_ENSURE_ARG_POINTER(_retval);
00798   *_retval = m_needToCheckCharset;
00799   return NS_OK;
00800 }
00801 
00802 NS_IMETHODIMP nsMsgCompFields::SetNeedToCheckCharset(PRBool aCheck)
00803 {
00804   m_needToCheckCharset = aCheck;
00805   return NS_OK;
00806 }