Main Page   Class Hierarchy   Compound List   File List   Compound Members  

mime.cpp

00001 /*
00002  * Purpose: wxWindows mime implementation
00003  * Author:  Frank Buß
00004  * Created: 2002
00005  */
00006 
00007 #include "email.h"
00008 #include "Base64.h"
00009 
00010 
00011 #include "wx/arrimpl.cpp"
00012 WX_DEFINE_OBJARRAY(wxArrayMimePart);
00013 
00014 struct wxMimeTypeMapping
00015 {
00016     const wxChar* extension;
00017     const wxChar* mainType;
00018     const wxChar* subType;
00019 };
00020 
00021 // extension, main type, sub type
00022 // TODO: must be merged / integrated / substituted with wxFileType
00023 static const wxMimeTypeMapping g_mimeTypeMappings[] =
00024 {
00025     { _T("txt"), _T("text"), _T("plain") },
00026     { _T("htm"), _T("text"), _T("html") },
00027     { _T("html"), _T("text"), _T("html") },
00028     { _T("gif"), _T("image"), _T("gif") },
00029     { _T("png"), _T("image"), _T("png") },
00030     { _T("jpg"), _T("image"), _T("jpeg") },
00031     { _T("jpeg"), _T("image"), _T("jpeg") },
00032     { _T("pdf"), _T("application"), _T("pdf") },
00033     { _T("doc"), _T("application"), _T("msword") }
00034 };
00035 
00036 wxMimePart::wxMimePart(const wxFileName& fileName) :
00037     m_fileName(fileName)
00038 {
00039     // setup default encoding
00040     m_mainType = _T("application");
00041     m_subType = _T("octet-stream");
00042 
00043     // try to determine encoding by filename extension
00044     wxString ext = fileName.GetExt().Lower();
00045     for (int i = 0; i < sizeof(g_mimeTypeMappings) / sizeof(wxMimeTypeMapping); i++) {
00046         if (g_mimeTypeMappings[i].extension == ext) {
00047             m_mainType = g_mimeTypeMappings[i].mainType;
00048             m_subType = g_mimeTypeMappings[i].subType;
00049             break;
00050         }
00051     }
00052 }
00053 
00054 void wxMimePart::Encode(wxOutputStream& out)
00055 {
00056     // TODO: error handling for every return
00057     // TODO: encoding it on-the-fly without loading it all in memory
00058     wxFileInputStream in(m_fileName.GetFullPath());
00059     if (!in.Ok()) return;
00060 
00061     // determine file length
00062     // TODO: perhaps this can be added to wxFileName for size()
00063     in.SeekI(0, wxFromEnd);
00064     size_t len = (size_t) in.TellI();
00065     in.SeekI(0);
00066     // that's possible for windows (#include <sys/types.h> and #include <sys/stat.h>):
00067     // struct _stat lenStat;
00068     // if (_stat(filename, &lenStat) == -1) return error;
00069     // size_t len = (size_t) lenStat.st_size;
00070 
00071     if (len == 0) return;
00072 
00073     // read file
00074     wxUint8* pData = new wxUint8[len];
00075     if (!pData) {
00076         wxASSERT_MSG(pData != NULL, _T("out of memory"));
00077         return;
00078     }
00079     in.Read(pData, len);
00080 
00081     // write encoded mime part to stream
00082     wxString cr("\x00d\x00a");
00083     wxString result;
00084     result << "Content-Type: " << m_mainType << "/" << m_subType << "; name=\"" << m_fileName.GetFullName() + "\"" + cr
00085            << "Content-Transfer-Encoding: base64" << cr
00086            << "Content-Disposition: attachment; filename=\"" << m_fileName.GetName() << "\"" << cr
00087            << cr
00088            << wxBase64::Encode(pData, len) << cr;
00089     out.Write((const char*) result, result.Length());
00090 }
00091 

Generated on Mon May 6 01:14:20 2002 for Wino by doxygen1.2.13.1 written by Dimitri van Heesch, © 1997-2001