Back to index

lightning-sunbird  0.9+nobinonly
nsMsgAttachment.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  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "nsMsgAttachment.h"
00039 #include "nsILocalFile.h"
00040 #include "nsReadableUtils.h"
00041 #include "nsNetUtil.h"
00042 
00043 NS_IMPL_ISUPPORTS1(nsMsgAttachment, nsIMsgAttachment)
00044 
00045 nsMsgAttachment::nsMsgAttachment()
00046 {
00047   mTemporary = PR_FALSE;
00048 }
00049 
00050 nsMsgAttachment::~nsMsgAttachment()
00051 {
00052   if (mTemporary)
00053     (void)DeleteAttachment();
00054 }
00055 
00056 /* attribute wstring name; */
00057 NS_IMETHODIMP nsMsgAttachment::GetName(nsAString & aName)
00058 {
00059   aName = mName;
00060   return NS_OK;
00061 }
00062 
00063 NS_IMETHODIMP nsMsgAttachment::SetName(const nsAString & aName)
00064 {
00065   mName = aName;
00066   return NS_OK;
00067 }
00068 
00069 /* attribute string url; */
00070 NS_IMETHODIMP nsMsgAttachment::GetUrl(char * *aUrl)
00071 {
00072   NS_ENSURE_ARG_POINTER(aUrl);
00073 
00074   *aUrl = ToNewCString(mUrl);
00075   return (*aUrl ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
00076 }
00077 NS_IMETHODIMP nsMsgAttachment::SetUrl(const char * aUrl)
00078 {
00079   mUrl = aUrl;
00080   return NS_OK;
00081 }
00082 
00083 /* attribute string urlCharset; */
00084 NS_IMETHODIMP nsMsgAttachment::GetUrlCharset(nsACString & aUrlCharset)
00085 {
00086   aUrlCharset = mUrlCharset;
00087   return NS_OK;
00088 }
00089 NS_IMETHODIMP nsMsgAttachment::SetUrlCharset(const nsACString & aUrlCharset)
00090 {
00091   mUrlCharset = aUrlCharset;
00092   return NS_OK;
00093 }
00094 
00095 /* attribute boolean temporary; */
00096 NS_IMETHODIMP nsMsgAttachment::GetTemporary(PRBool *aTemporary)
00097 {
00098   NS_ENSURE_ARG_POINTER(aTemporary);
00099 
00100   *aTemporary = mTemporary;
00101   return NS_OK;
00102 }
00103 NS_IMETHODIMP nsMsgAttachment::SetTemporary(PRBool aTemporary)
00104 {
00105   mTemporary = aTemporary;
00106   return NS_OK;
00107 }
00108 
00109 /* attribute string contentLocation; */
00110 NS_IMETHODIMP nsMsgAttachment::GetContentLocation(char * *aContentLocation)
00111 {
00112   NS_ENSURE_ARG_POINTER(aContentLocation);
00113 
00114   *aContentLocation = ToNewCString(mContentLocation);
00115   return (*aContentLocation ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
00116 }
00117 NS_IMETHODIMP nsMsgAttachment::SetContentLocation(const char * aContentLocation)
00118 {
00119   mContentLocation = aContentLocation;
00120   return NS_OK;
00121 }
00122 
00123 /* attribute string contentType; */
00124 NS_IMETHODIMP nsMsgAttachment::GetContentType(char * *aContentType)
00125 {
00126   NS_ENSURE_ARG_POINTER(aContentType);
00127 
00128   *aContentType = ToNewCString(mContentType);
00129   return (*aContentType ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
00130 }
00131 NS_IMETHODIMP nsMsgAttachment::SetContentType(const char * aContentType)
00132 {
00133   mContentType = aContentType;
00134   /* a full content type could also contains parameters but we need to
00135      keep only the content type alone. Therefore we need to cleanup it.
00136   */
00137   PRInt32 offset = mContentType.FindChar(';');
00138   if (offset >= 0)
00139     mContentType.Truncate(offset);
00140 
00141   return NS_OK;
00142 }
00143 
00144 /* attribute string contentTypeParam; */
00145 NS_IMETHODIMP nsMsgAttachment::GetContentTypeParam(char * *aContentTypeParam)
00146 {
00147   NS_ENSURE_ARG_POINTER(aContentTypeParam);
00148 
00149   *aContentTypeParam = ToNewCString(mContentTypeParam);
00150   return (*aContentTypeParam ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
00151 }
00152 NS_IMETHODIMP nsMsgAttachment::SetContentTypeParam(const char * aContentTypeParam)
00153 {
00154   if (aContentTypeParam)
00155     while (*aContentTypeParam == ';' || *aContentTypeParam == ' ')
00156       aContentTypeParam ++;
00157   mContentTypeParam = aContentTypeParam;
00158 
00159   return NS_OK;
00160 }
00161 
00162 /* attribute string charset; */
00163 NS_IMETHODIMP nsMsgAttachment::GetCharset(char * *aCharset)
00164 {
00165   NS_ENSURE_ARG_POINTER(aCharset);
00166 
00167   *aCharset = ToNewCString(mCharset);
00168   return (*aCharset ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
00169 }
00170 NS_IMETHODIMP nsMsgAttachment::SetCharset(const char * aCharset)
00171 {
00172   mCharset = aCharset;
00173   return NS_OK;
00174 }
00175 
00176 /* attribute string macType; */
00177 NS_IMETHODIMP nsMsgAttachment::GetMacType(char * *aMacType)
00178 {
00179   NS_ENSURE_ARG_POINTER(aMacType);
00180 
00181   *aMacType = ToNewCString(mMacType);
00182   return (*aMacType ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
00183 }
00184 NS_IMETHODIMP nsMsgAttachment::SetMacType(const char * aMacType)
00185 {
00186   mMacType = aMacType;
00187   return NS_OK;
00188 }
00189 
00190 /* attribute string macCreator; */
00191 NS_IMETHODIMP nsMsgAttachment::GetMacCreator(char * *aMacCreator)
00192 {
00193   NS_ENSURE_ARG_POINTER(aMacCreator);
00194 
00195   *aMacCreator = ToNewCString(mMacCreator);
00196   return (*aMacCreator ? NS_OK : NS_ERROR_OUT_OF_MEMORY);
00197 }
00198 NS_IMETHODIMP nsMsgAttachment::SetMacCreator(const char * aMacCreator)
00199 {
00200   mMacCreator = aMacCreator;
00201   return NS_OK;
00202 }
00203 
00204 /* boolean equalsUrl (in nsIMsgAttachment attachment); */
00205 NS_IMETHODIMP nsMsgAttachment::EqualsUrl(nsIMsgAttachment *attachment, PRBool *_retval)
00206 {
00207   NS_ENSURE_ARG_POINTER(attachment);
00208   NS_ENSURE_ARG_POINTER(_retval);
00209 
00210   nsXPIDLCString url;
00211   attachment->GetUrl(getter_Copies(url));
00212 
00213   *_retval = mUrl.Equals(url);
00214   return NS_OK;
00215 }
00216 
00217 
00218 nsresult nsMsgAttachment::DeleteAttachment()
00219 {
00220   nsresult rv;
00221   PRBool isAFile = PR_FALSE;
00222 
00223   nsCOMPtr<nsIFile> urlFile;
00224   rv = NS_GetFileFromURLSpec(mUrl, getter_AddRefs(urlFile));
00225   NS_ASSERTION(NS_SUCCEEDED(rv), "Can't nsIFile from URL string");
00226   if (NS_SUCCEEDED(rv))
00227   {
00228     PRBool bExists = PR_FALSE;
00229     rv = urlFile->Exists(&bExists);
00230     NS_ASSERTION(NS_SUCCEEDED(rv), "Exists() call failed!");
00231     if (NS_SUCCEEDED(rv) && bExists)
00232     {
00233       rv = urlFile->IsFile(&isAFile);
00234       NS_ASSERTION(NS_SUCCEEDED(rv), "IsFile() call failed!");
00235     }
00236   }
00237 
00238   // remove it if it's a valid file
00239   if (isAFile)
00240          rv = urlFile->Remove(PR_FALSE); 
00241 
00242   return rv;
00243 }