kmime_content.h
00001 /* 00002 kmime_content.h 00003 00004 KMime, the KDE internet mail/usenet news message library. 00005 Copyright (c) 2001 the KMime authors. 00006 See file AUTHORS for details 00007 00008 This program is free software; you can redistribute it and/or modify 00009 it under the terms of the GNU General Public License as published by 00010 the Free Software Foundation; either version 2 of the License, or 00011 (at your option) any later version. 00012 You should have received a copy of the GNU General Public License 00013 along with this program; if not, write to the Free Software Foundation, 00014 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, US 00015 */ 00016 #ifndef __KMIME_CONTENT_H__ 00017 #define __KMIME_CONTENT_H__ 00018 00019 //forward declarations 00020 #if 0 00021 class KMime::Headers::Base; 00022 class KMime::Headers::Generic; 00023 class KMime::Headers::ContentType; 00024 class KMime::Headers::CTEncoding; 00025 class KMime::Headers::CDisposition; 00026 class KMime::Headers::List; 00027 #endif 00028 00029 #include "kmime_util.h" 00030 #include "kmime_headers.h" 00031 00032 #include <tqtextstream.h> 00033 00034 namespace KMime { 00035 00036 00042 class Base { 00043 00044 public: 00045 00046 //enums 00047 enum articleType { ATmimeContent, 00048 ATremote, 00049 ATlocal }; 00050 00051 }; 00052 00053 00059 class KDE_EXPORT Content : public Base { 00060 00061 public: 00062 typedef TQPtrList<KMime::Content> List; 00063 00064 Content(); 00065 Content(const TQCString &h, const TQCString &b); 00066 virtual ~Content(); 00067 00068 //type 00069 virtual articleType type() { return ATmimeContent; } 00070 00071 //content handling 00072 bool hasContent() { return ( !h_ead.isEmpty() && (!b_ody.isEmpty() || (c_ontents && !c_ontents->isEmpty())) ); } 00073 void setContent(TQStrList *l); 00074 void setContent(const TQCString &s); 00075 virtual void parse(); 00076 virtual void assemble(); 00077 virtual void clear(); 00078 00079 //header access 00080 TQCString head() { return h_ead; } 00081 // extracts and removes the next header from head. The caller has to delete the returned header; 00082 Headers::Generic* getNextHeader(TQCString &head); 00083 virtual Headers::Base* getHeaderByType(const char *type); 00084 virtual void setHeader(Headers::Base *h); 00085 virtual bool removeHeader(const char *type); 00086 bool hasHeader(const char *type) { return (getHeaderByType(type)!=0); } 00087 Headers::ContentType* contentType(bool create=true) { Headers::ContentType *p=0; return getHeaderInstance(p, create); } 00088 Headers::CTEncoding* contentTransferEncoding(bool create=true) { Headers::CTEncoding *p=0; return getHeaderInstance(p, create); } 00089 Headers::CDisposition* contentDisposition(bool create=true) { Headers::CDisposition *p=0; return getHeaderInstance(p, create); } 00090 Headers::CDescription* contentDescription(bool create=true) { Headers::CDescription *p=0; return getHeaderInstance(p, create); } 00091 00092 //content access 00093 int size(); 00094 int storageSize(); 00095 int lineCount(); 00096 TQCString body() { return b_ody; } 00097 void setBody( const TQCString & str ) { b_ody = str; } 00098 TQCString encodedContent(bool useCrLf=false); 00099 TQByteArray decodedContent(); 00100 void decodedText(TQString &s, bool trimText=false, 00101 bool removeTrailingNewlines=false); 00102 void decodedText(TQStringList &s, bool trimText=false, 00103 bool removeTrailingNewlines=false); 00104 void fromUnicodeString(const TQString &s); 00105 00106 Content* textContent(); 00107 void attachments(List *dst, bool incAlternatives=false); 00108 void addContent(Content *c, bool prepend=false); 00109 void removeContent(Content *c, bool del=false); 00110 void changeEncoding(Headers::contentEncoding e); 00111 00112 //saves the encoded content to the given textstream 00113 // scrambleFromLines: replace "\nFrom " with "\n>From ", this is 00114 // needed to avoid problem with mbox-files 00115 void toStream(TQTextStream &ts, bool scrambleFromLines=false); 00116 00117 // this charset is used for all headers and the body 00118 // if the charset is not declared explictly 00119 TQCString defaultCharset() { return TQCString(d_efaultCS); } 00120 void setDefaultCharset(const TQCString &cs); 00121 00122 // use the default charset even if a different charset is 00123 // declared in the article 00124 bool forceDefaultCS() { return f_orceDefaultCS; } 00125 00126 // enables/disables the force mode, housekeeping. 00127 // works correctly only when the article is completely empty or 00128 // completely loaded 00129 virtual void setForceDefaultCS(bool b); 00130 00131 00132 protected: 00133 TQCString rawHeader(const char *name); 00134 bool decodeText(); 00135 template <class T> T* getHeaderInstance(T *ptr, bool create); 00136 00137 TQCString h_ead, 00138 b_ody; 00139 List *c_ontents; 00140 Headers::Base::List *h_eaders; 00141 const char *d_efaultCS; 00142 bool f_orceDefaultCS; 00143 00144 }; 00145 00146 // some compilers (for instance Compaq C++) need template inline functions 00147 // here rather than in the *.cpp file 00148 00149 template <class T> T* Content::getHeaderInstance(T *ptr, bool create) 00150 { 00151 T dummy; //needed to access virtual member T::type() 00152 00153 ptr=static_cast <T*> (getHeaderByType(dummy.type())); 00154 if(!ptr && create) { //no such header found, but we need one => create it 00155 ptr=new T(this); 00156 if(!(h_eaders)) { 00157 h_eaders=new Headers::Base::List(); 00158 h_eaders->setAutoDelete(true); 00159 } 00160 h_eaders->append(ptr); 00161 } 00162 00163 return ptr; 00164 } 00165 00166 00167 00168 } // namespace KMime 00169 00170 #endif // __KMIME_CONTENT_H__