KMIME Library
kmime_content.h
Go to the documentation of this file.
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 Copyright (c) 2006 Volker Krause <vkrause@kde.org> 00008 Copyright (c) 2009 Constantin Berzan <exit3219@gmail.com> 00009 00010 This library is free software; you can redistribute it and/or 00011 modify it under the terms of the GNU Library General Public 00012 License as published by the Free Software Foundation; either 00013 version 2 of the License, or (at your option) any later version. 00014 00015 This library is distributed in the hope that it will be useful, 00016 but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 Library General Public License for more details. 00019 00020 You should have received a copy of the GNU Library General Public License 00021 along with this library; see the file COPYING.LIB. If not, write to 00022 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00023 Boston, MA 02110-1301, USA. 00024 */ 00048 #ifndef __KMIME_CONTENT_H__ 00049 #define __KMIME_CONTENT_H__ 00050 00051 #include "kmime_export.h" 00052 #include "kmime_contentindex.h" 00053 #include "kmime_util.h" 00054 #include "kmime_headers.h" 00055 00056 #include <QtCore/QTextStream> 00057 #include <QtCore/QByteArray> 00058 #include <QtCore/QList> 00059 00060 #include <boost/shared_ptr.hpp> 00061 00062 00063 namespace KMime { 00064 00065 class ContentPrivate; 00066 class Message; 00067 00106 /* 00107 KDE5: 00108 * Do not convert singlepart <-> multipart automatically. 00109 * A bunch of methods probably don't need to be virtual (since they're not needed 00110 in either Message or NewsArticle). 00111 */ 00112 class KMIME_EXPORT Content 00113 { 00114 public: 00115 00119 typedef QList<KMime::Content*> List; 00120 00124 Content(); 00125 00131 explicit Content( Content* parent ); // KDE5: Merge with the above. 00132 00139 Content( const QByteArray &head, const QByteArray &body ); 00140 00149 // KDE5: Merge with the above. 00150 Content( const QByteArray &head, const QByteArray &body, Content *parent ); 00151 00155 virtual ~Content(); 00156 00160 bool hasContent() const; 00161 00172 void setContent( const QList<QByteArray> &l ); 00173 00184 void setContent( const QByteArray &s ); 00185 00204 virtual void parse(); 00205 00215 bool isFrozen() const; 00216 00223 void setFrozen( bool frozen = true ); 00224 00244 virtual void assemble(); 00245 00249 // KDE5: make non-virtual. 00250 virtual void clear(); 00251 00263 void clearContents( bool del = true ); 00264 00270 QByteArray head() const; 00271 00282 void setHead( const QByteArray &head ); 00283 00291 KDE_DEPRECATED Headers::Generic *getNextHeader( QByteArray &head ); 00292 00300 // KDE5: Remove this. This method has nothing to do with *this object. 00301 KDE_DEPRECATED Headers::Generic *nextHeader( QByteArray &head ); 00302 00307 // KDE5: Make non-virtual. 00308 KDE_DEPRECATED virtual Headers::Base *getHeaderByType( const char *type ); 00309 00315 // KDE5: Make non-virtual. 00316 virtual Headers::Base *headerByType( const char *type ); 00317 00329 template <typename T> T *header( bool create = false ); 00330 00336 virtual QList<Headers::Base*> headersByType( const char *type ); 00337 00349 // KDE5: make non-virtual. 00350 virtual void setHeader( Headers::Base *h ); 00351 00357 void appendHeader( Headers::Base *h ); 00358 00364 void prependHeader( Headers::Base *h ); 00365 00372 // TODO probably provide removeHeader<T>() too. 00373 // KDE5: make non-virtual. 00374 virtual bool removeHeader( const char *type ); 00375 00380 // TODO probably provide hasHeader<T>() too. 00381 // TODO: KDE5: make const 00382 bool hasHeader( const char *type ); 00383 00389 Headers::ContentType *contentType( bool create = true ); 00390 00396 Headers::ContentTransferEncoding *contentTransferEncoding( bool create = true ); 00397 00403 Headers::ContentDisposition *contentDisposition( bool create = true ); 00404 00410 Headers::ContentDescription *contentDescription( bool create = true ); 00411 00418 Headers::ContentLocation *contentLocation( bool create = true ); 00419 00425 Headers::ContentID *contentID( bool create = true ); 00426 00432 int size(); 00433 00437 int storageSize() const; 00438 00442 int lineCount() const; 00443 00452 QByteArray body() const; 00453 00464 void setBody( const QByteArray &body ); 00465 00480 QByteArray encodedContent( bool useCrLf = false ); 00481 00488 // TODO: KDE5: BIC: Rename this to decodedBody(), since only the body is returned. 00489 // In contrast, setContent() sets the head and the body! 00490 // Also, try to make this const. 00491 QByteArray decodedContent(); 00492 00506 // TODO: KDE5: BIC: Convert to enums. Also, what if trimText = true but removeTrailingNewlines 00507 // is false? 00508 QString decodedText( bool trimText = false, 00509 bool removeTrailingNewlines = false ); 00510 00520 void fromUnicodeString( const QString &s ); 00521 00525 Content *textContent(); 00526 00532 List attachments( bool incAlternatives = false ); 00533 00539 List contents() const; 00540 00557 // KDE5: Do not convert single-part->multipart automatically. 00558 void addContent( Content *content, bool prepend = false ); 00559 00576 // KDE5: Do not convert multipart->single-part automatically. 00577 void removeContent( Content *content, bool del = false ); 00578 00585 void changeEncoding( Headers::contentEncoding e ); 00586 00594 void toStream( QTextStream &ts, bool scrambleFromLines = false ); 00595 00596 // NOTE: The charset methods below are accessed by the headers which 00597 // have this Content as a parent. 00598 00605 QByteArray defaultCharset() const; 00606 00614 void setDefaultCharset( const QByteArray &cs ); 00615 00622 bool forceDefaultCharset() const; 00623 00633 virtual void setForceDefaultCharset( bool b ); 00634 00642 Content *content( const ContentIndex &index ) const; 00643 00649 ContentIndex indexForContent( Content *content ) const; 00650 00656 virtual bool isTopLevel() const; 00657 00664 void setParent( Content *parent ); 00665 00670 Content* parent() const; 00671 00676 Content* topLevel() const; 00677 00682 ContentIndex index() const; 00683 00690 //AK_REVIEW: move to MessageViewer/ObjectTreeParser 00691 bool bodyIsMessage() const; 00692 00711 //AK_REVIEW: move to MessageViewer/ObjectTreeParser 00712 boost::shared_ptr<Message> bodyAsMessage() const; 00713 00714 protected: 00720 virtual QByteArray assembleHeaders(); 00721 00726 KDE_DEPRECATED QByteArray rawHeader( const char *name ) const; 00727 00732 KDE_DEPRECATED QList<QByteArray> rawHeaders( const char *name ) const; 00733 00737 // KDE5: Not needed outside. Move to Private class. 00738 bool decodeText(); 00739 00744 template <class T> KDE_DEPRECATED T *headerInstance( T *ptr, bool create ); 00745 00750 // KDE5: Not needed outside. Move to Private class. 00751 Headers::Base::List h_eaders; 00752 00753 //@cond PRIVATE 00754 ContentPrivate *d_ptr; 00755 explicit Content( ContentPrivate *d ); 00756 //@endcond 00757 00758 private: 00759 Q_DECLARE_PRIVATE( Content ) 00760 Q_DISABLE_COPY( Content ) 00761 }; 00762 00763 // some compilers (for instance Compaq C++) need template inline functions 00764 // here rather than in the *.cpp file 00765 00766 template <class T> T *Content::headerInstance( T *ptr, bool create ) 00767 { 00768 return header<T>( create ); 00769 } 00770 00771 template <typename T> T *Content::header( bool create ) 00772 { 00773 Headers::Base *h = headerByType( T::staticType() ); 00774 if( h ) { 00775 // Make sure the header is actually of the right type. 00776 Q_ASSERT( dynamic_cast<T*>( h ) ); 00777 } else if( create ) { 00778 h = new T( this ); 00779 setHeader( h ); 00780 } 00781 return static_cast<T*>( h ); 00782 } 00783 00784 } // namespace KMime 00785 00786 #endif // __KMIME_CONTENT_H__