00001
00002
00003
00004
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
00022
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
00040 m_mainType = _T("application");
00041 m_subType = _T("octet-stream");
00042
00043
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
00057
00058 wxFileInputStream in(m_fileName.GetFullPath());
00059 if (!in.Ok()) return;
00060
00061
00062
00063 in.SeekI(0, wxFromEnd);
00064 size_t len = (size_t) in.TellI();
00065 in.SeekI(0);
00066
00067
00068
00069
00070
00071 if (len == 0) return;
00072
00073
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
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