Back to index

lightning-sunbird  0.9+nobinonly
nsEudoraCompose.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org Code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1998
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
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 #ifndef nsEudoraCompose_h__
00040 #define nsEudoraCompose_h__
00041 
00042 #include "nscore.h"
00043 #include "nsCOMPtr.h"
00044 #include "nsString.h"
00045 #include "nsIFileSpec.h"
00046 #include "nsVoidArray.h"
00047 #include "nsIImportService.h"
00048 #include "nsNativeCharsetUtils.h"
00049 
00050 
00051 class nsIMsgSend;
00052 class nsIMsgCompFields;
00053 class nsIMsgIdentity;
00054 class nsIMsgSendListener;
00055 class nsIIOService;
00056 
00057 #include "nsIMsgSend.h"
00058 
00059 
00060 typedef struct {
00061        nsIFileSpec * pAttachment;
00062        char *               mimeType;
00063        char *               description;
00064 } ImportAttachment;
00065 
00066 typedef struct {
00067        PRUint32             offset;
00068        PRUint32             size;
00069        nsIFileSpec * pFile;
00070 } ReadFileState;
00071 
00072 class SimpleBufferTonyRCopiedOnce {
00073 public:
00074        SimpleBufferTonyRCopiedOnce() {m_pBuffer = nsnull; m_size = 0; m_growBy = 4096; m_writeOffset = 0;
00075                                    m_bytesInBuf = 0; m_convertCRs = PR_FALSE;}
00076        ~SimpleBufferTonyRCopiedOnce() { if (m_pBuffer) delete [] m_pBuffer;}
00077        
00078        PRBool Allocate( PRInt32 sz) { 
00079               if (m_pBuffer) delete [] m_pBuffer; 
00080               m_pBuffer = new char[sz]; 
00081               if (m_pBuffer) { m_size = sz; return( PR_TRUE); }
00082               else { m_size = 0; return( PR_FALSE);}
00083        }
00084 
00085        PRBool Grow( PRInt32 newSize) { if (newSize > m_size) return( ReAllocate( newSize)); else return( PR_TRUE);}
00086        PRBool ReAllocate( PRInt32 newSize) {
00087               if (newSize <= m_size) return( PR_TRUE);
00088               char *pOldBuffer = m_pBuffer;
00089               PRInt32       oldSize = m_size;
00090               m_pBuffer = nsnull;
00091               while (m_size < newSize) m_size += m_growBy;
00092               if (Allocate( m_size)) {
00093                      if (pOldBuffer) { memcpy( m_pBuffer, pOldBuffer, oldSize); delete [] pOldBuffer;}
00094                      return( PR_TRUE);
00095               }
00096               else { m_pBuffer = pOldBuffer; m_size = oldSize; return( PR_FALSE);}
00097        }
00098        
00099        PRBool Write( PRInt32 offset, const char *pData, PRInt32 len, PRInt32 *pWritten) {
00100               *pWritten = len;
00101               if (!len) return( PR_TRUE);
00102               if (!Grow( offset + len)) return( PR_FALSE);
00103               if (m_convertCRs)
00104                      return( SpecialMemCpy( offset, pData, len, pWritten));
00105               memcpy( m_pBuffer + offset, pData, len);
00106               return( PR_TRUE);
00107        }
00108        
00109        PRBool Write( const char *pData, PRInt32 len) { 
00110               PRInt32 written;
00111               if (Write( m_writeOffset, pData, len, &written)) { m_writeOffset += written; return( PR_TRUE);}
00112               else return( PR_FALSE);
00113        }
00114 
00115        PRBool SpecialMemCpy( PRInt32 offset, const char *pData, PRInt32 len, PRInt32 *pWritten);
00116        
00117        PRBool m_convertCRs;
00118        char * m_pBuffer;
00119        PRInt32       m_bytesInBuf; // used when reading into this buffer
00120        PRInt32       m_size;                     // allocated size of buffer
00121        PRInt32       m_growBy;            // duh
00122        PRInt32       m_writeOffset;       // used when writing into and reading from the buffer
00123 };
00124 
00125 
00126 
00127 class nsEudoraCompose {
00128 public:
00129        nsEudoraCompose();
00130        ~nsEudoraCompose();
00131 
00132        nsresult      SendTheMessage( nsIFileSpec *pMsg);
00133 
00134   void        SetBody( const char *pBody, PRInt32 len, nsCString &bodyType) { m_pBody = pBody; m_bodyLen = len; m_bodyType = bodyType;}
00135        void          SetHeaders( const char *pHeaders, PRInt32 len) { m_pHeaders = pHeaders; m_headerLen = len;}
00136        void          SetAttachments( nsVoidArray *pAttachments) { m_pAttachments = pAttachments;}
00137   void        SetDefaultDate( nsCString date) { m_defaultDate = date;}
00138 
00139        nsresult      CopyComposedMessage( nsCString& fromLine, nsIFileSpec *pSrc, nsIFileSpec *pDst, SimpleBufferTonyRCopiedOnce& copy);
00140 
00141        static nsresult      FillMailBuffer( ReadFileState *pState, SimpleBufferTonyRCopiedOnce& read);
00142 
00143 private:
00144        nsresult      CreateComponents( void);
00145        nsresult      CreateIdentity( void);
00146        
00147        void          GetNthHeader( const char *pData, PRInt32 dataLen, PRInt32 n, nsCString& header, nsCString& val, PRBool unwrap);
00148        void          GetHeaderValue( const char *pData, PRInt32 dataLen, const char *pHeader, nsCString& val, PRBool unwrap = PR_TRUE);
00149        void          GetHeaderValue( const char *pData, PRInt32 dataLen, const char *pHeader, nsString& val) {
00150               val.Truncate();
00151               nsCString     hVal;
00152               GetHeaderValue( pData, dataLen, pHeader, hVal, PR_TRUE);
00153               NS_CopyNativeToUnicode( hVal, val);
00154        }
00155        void          ExtractCharset( nsString& str);
00156        void          ExtractType( nsString& str);
00157 
00158        nsMsgAttachedFile * GetLocalAttachments( void);
00159        void                        CleanUpAttach( nsMsgAttachedFile *a, PRInt32 count);
00160 
00161        nsresult      ReadHeaders( ReadFileState *pState, SimpleBufferTonyRCopiedOnce& copy, SimpleBufferTonyRCopiedOnce& header);
00162        PRInt32              FindNextEndLine( SimpleBufferTonyRCopiedOnce& data);
00163        PRInt32              IsEndHeaders( SimpleBufferTonyRCopiedOnce& data);
00164        PRInt32              IsSpecialHeader( const char *pHeader);
00165        nsresult      WriteHeaders( nsIFileSpec *pDst, SimpleBufferTonyRCopiedOnce& newHeaders);
00166        PRBool        IsReplaceHeader( const char *pHeader);
00167 
00168 private:
00169        nsVoidArray *               m_pAttachments;
00170        nsIMsgSendListener * m_pListener;
00171        nsIMsgSend *                m_pMsgSend;
00172        nsIMsgSend *                m_pSendProxy;
00173        nsIMsgCompFields *          m_pMsgFields;
00174        nsIMsgIdentity *            m_pIdentity;
00175        nsIIOService *                     m_pIOService;
00176        PRInt32                                   m_headerLen;
00177        const char *                m_pHeaders;
00178        PRInt32                                   m_bodyLen;
00179        const char *                m_pBody;
00180   nsCString                        m_bodyType;
00181        nsString                           m_defCharset;
00182        SimpleBufferTonyRCopiedOnce               m_readHeaders;
00183        nsCOMPtr<nsIImportService>  m_pImportService;
00184   nsCString       m_defaultDate;  // Use this if no Date: header in msgs
00185 };
00186 
00187 
00188 #endif /* nsEudoraCompose_h__ */