Back to index

lightning-sunbird  0.9+nobinonly
nsOutlookCompose.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 nsOutlookCompose_h__
00040 #define nsOutlookCompose_h__
00041 
00042 #include "nscore.h"
00043 #include "nsString.h"
00044 #include "nsIFileSpec.h"
00045 #include "nsVoidArray.h"
00046 #include "nsIImportService.h"
00047 
00048 class nsIMsgSend;
00049 class nsIMsgCompFields;
00050 class nsIMsgIdentity;
00051 class nsIMsgSendListener;
00052 class nsIIOService;
00053 
00054 #include "nsIMsgSend.h"
00055 
00056 
00057 typedef struct {
00058        nsIFileSpec * pAttachment;
00059        char *               mimeType;
00060        char *               description;
00061 } OutlookAttachment;
00062 
00063 typedef struct {
00064        PRUint32             offset;
00065        PRUint32             size;
00066        nsIFileSpec * pFile;
00067 } ReadFileState;
00068 
00069 class SimpleBufferTonyRCopiedTwice {
00070 public:
00071        SimpleBufferTonyRCopiedTwice() {m_pBuffer = nsnull; m_size = 0; m_growBy = 4096; m_writeOffset = 0;
00072                                    m_bytesInBuf = 0; m_convertCRs = PR_FALSE;}
00073        ~SimpleBufferTonyRCopiedTwice() { if (m_pBuffer) delete [] m_pBuffer;}
00074        
00075        PRBool Allocate( PRInt32 sz) { 
00076               if (m_pBuffer) delete [] m_pBuffer; 
00077               m_pBuffer = new char[sz]; 
00078               if (m_pBuffer) { m_size = sz; return( PR_TRUE); }
00079               else { m_size = 0; return( PR_FALSE);}
00080        }
00081 
00082        PRBool Grow( PRInt32 newSize) { if (newSize > m_size) return( ReAllocate( newSize)); else return( PR_TRUE);}
00083        PRBool ReAllocate( PRInt32 newSize) {
00084               if (newSize <= m_size) return( PR_TRUE);
00085               char *pOldBuffer = m_pBuffer;
00086               PRInt32       oldSize = m_size;
00087               m_pBuffer = nsnull;
00088               while (m_size < newSize) m_size += m_growBy;
00089               if (Allocate( m_size)) {
00090                      if (pOldBuffer) { memcpy( m_pBuffer, pOldBuffer, oldSize); delete [] pOldBuffer;}
00091                      return( PR_TRUE);
00092               }
00093               else { m_pBuffer = pOldBuffer; m_size = oldSize; return( PR_FALSE);}
00094        }
00095        
00096        PRBool Write( PRInt32 offset, const char *pData, PRInt32 len, PRInt32 *pWritten) {
00097               *pWritten = len;
00098               if (!len) return( PR_TRUE);
00099               if (!Grow( offset + len)) return( PR_FALSE);
00100               if (m_convertCRs)
00101                      return( SpecialMemCpy( offset, pData, len, pWritten));
00102               memcpy( m_pBuffer + offset, pData, len);
00103               return( PR_TRUE);
00104        }
00105        
00106        PRBool Write( const char *pData, PRInt32 len) { 
00107               PRInt32 written;
00108               if (Write( m_writeOffset, pData, len, &written)) { m_writeOffset += written; return( PR_TRUE);}
00109               else return( PR_FALSE);
00110        }
00111 
00112        PRBool SpecialMemCpy( PRInt32 offset, const char *pData, PRInt32 len, PRInt32 *pWritten);
00113        
00114        PRBool m_convertCRs;
00115        char * m_pBuffer;
00116        PRInt32       m_bytesInBuf; // used when reading into this buffer
00117        PRInt32       m_size;                     // allocated size of buffer
00118        PRInt32       m_growBy;            // duh
00119        PRInt32       m_writeOffset;       // used when writing into and reading from the buffer
00120 };
00121 
00122 
00123 
00124 class nsOutlookCompose {
00125 public:
00126        nsOutlookCompose();
00127        ~nsOutlookCompose();
00128 
00129        nsresult      SendTheMessage( nsIFileSpec *pMsg, nsMsgDeliverMode mode, nsCString &useThisCType);
00130 
00131        void          SetBody( const char *pBody) { m_Body = pBody;}
00132        void          SetHeaders( const char *pHeaders) { m_Headers = pHeaders;}
00133        void          SetAttachments( nsVoidArray *pAttachments) { m_pAttachments = pAttachments;}
00134 
00135        nsresult      CopyComposedMessage( nsCString& fromLine, nsIFileSpec *pSrc, nsIFileSpec *pDst, SimpleBufferTonyRCopiedTwice& copy);
00136 
00137        static nsresult      FillMailBuffer( ReadFileState *pState, SimpleBufferTonyRCopiedTwice& read);
00138 
00139 private:
00140        nsresult      CreateComponents( void);
00141        nsresult      CreateIdentity( void);
00142        
00143        void          GetNthHeader( const char *pData, PRInt32 dataLen, PRInt32 n, nsCString& header, nsCString& val, PRBool unwrap);
00144        void          GetHeaderValue( const char *pData, PRInt32 dataLen, const char *pHeader, nsCString& val, PRBool unwrap = PR_TRUE);
00145        void          GetHeaderValue( const char *pData, PRInt32 dataLen, const char *pHeader, nsString& val) {
00146               val.Truncate();
00147               nsCString     hVal;
00148               GetHeaderValue( pData, dataLen, pHeader, hVal, PR_TRUE);
00149               CopyUTF8toUTF16(hVal, val);
00150        }
00151        void          ExtractCharset( nsString& str);
00152        void          ExtractType( nsString& str);
00153 
00154        nsMsgAttachedFile * GetLocalAttachments( void);
00155        void                        CleanUpAttach( nsMsgAttachedFile *a, PRInt32 count);
00156 
00157        nsresult      ReadHeaders( ReadFileState *pState, SimpleBufferTonyRCopiedTwice& copy, SimpleBufferTonyRCopiedTwice& header);
00158        PRInt32              FindNextEndLine( SimpleBufferTonyRCopiedTwice& data);
00159        PRInt32              IsEndHeaders( SimpleBufferTonyRCopiedTwice& data);
00160        PRInt32              IsSpecialHeader( const char *pHeader);
00161        nsresult      WriteHeaders( nsIFileSpec *pDst, SimpleBufferTonyRCopiedTwice& newHeaders);
00162        PRBool        IsReplaceHeader( const char *pHeader);
00163   void      ConvertSystemStringToUnicode( const char *pSysStr, nsString& uniStr);
00164 
00165 
00166 private:
00167        nsVoidArray *               m_pAttachments;
00168        nsIMsgSendListener * m_pListener;
00169        nsIMsgSend *                m_pMsgSend;
00170        nsIMsgSend *                m_pSendProxy;
00171        nsIMsgCompFields *          m_pMsgFields;
00172        nsIMsgIdentity *            m_pIdentity;
00173        nsIIOService *                     m_pIOService;
00174        nsCString       m_Headers;
00175        nsCString       m_Body;
00176        SimpleBufferTonyRCopiedTwice                     m_readHeaders;
00177   nsCOMPtr<nsIImportService>       m_pImportService;
00178 };
00179 
00180 
00181 #endif /* nsOutlookCompose_h__ */