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 00187 void setContent( const QByteArray &s ); 00188 00207 virtual void parse(); 00208 00218 bool isFrozen() const; 00219 00226 void setFrozen( bool frozen = true ); 00227 00247 virtual void assemble(); 00248 00252 // KDE5: make non-virtual. 00253 virtual void clear(); 00254 00266 void clearContents( bool del = true ); 00267 00273 QByteArray head() const; 00274 00285 void setHead( const QByteArray &head ); 00286 00294 KDE_DEPRECATED Headers::Generic *getNextHeader( QByteArray &head ); 00295 00303 // KDE5: Remove this. This method has nothing to do with *this object. 00304 KDE_DEPRECATED Headers::Generic *nextHeader( QByteArray &head ); 00305 00310 // KDE5: Make non-virtual. 00311 KDE_DEPRECATED virtual Headers::Base *getHeaderByType( const char *type ); 00312 00318 // KDE5: Make non-virtual. 00319 virtual Headers::Base *headerByType( const char *type ); 00320 00332 template <typename T> T *header( bool create = false ); 00333 00339 virtual QList<Headers::Base*> headersByType( const char *type ); 00340 00352 // KDE5: make non-virtual. 00353 virtual void setHeader( Headers::Base *h ); 00354 00360 void appendHeader( Headers::Base *h ); 00361 00367 void prependHeader( Headers::Base *h ); 00368 00375 // TODO probably provide removeHeader<T>() too. 00376 // KDE5: make non-virtual. 00377 virtual bool removeHeader( const char *type ); 00378 00383 // TODO probably provide hasHeader<T>() too. 00384 // TODO: KDE5: make const 00385 bool hasHeader( const char *type ); 00386 00392 Headers::ContentType *contentType( bool create = true ); 00393 00399 Headers::ContentTransferEncoding *contentTransferEncoding( bool create = true ); 00400 00406 Headers::ContentDisposition *contentDisposition( bool create = true ); 00407 00413 Headers::ContentDescription *contentDescription( bool create = true ); 00414 00421 Headers::ContentLocation *contentLocation( bool create = true ); 00422 00428 Headers::ContentID *contentID( bool create = true ); 00429 00435 int size(); 00436 00440 int storageSize() const; 00441 00445 int lineCount() const; 00446 00455 QByteArray body() const; 00456 00467 void setBody( const QByteArray &body ); 00468 00483 QByteArray encodedContent( bool useCrLf = false ); 00484 00491 QByteArray encodedBody(); 00492 00499 // TODO: KDE5: BIC: Rename this to decodedBody(), since only the body is returned. 00500 // In contrast, setContent() sets the head and the body! 00501 // Also, try to make this const. 00502 QByteArray decodedContent(); 00503 00517 // TODO: KDE5: BIC: Convert to enums. Also, what if trimText = true but removeTrailingNewlines 00518 // is false? 00519 QString decodedText( bool trimText = false, 00520 bool removeTrailingNewlines = false ); 00521 00531 void fromUnicodeString( const QString &s ); 00532 00536 Content *textContent(); 00537 00543 List attachments( bool incAlternatives = false ); 00544 00550 List contents() const; 00551 00568 // KDE5: Do not convert single-part->multipart automatically. 00569 void addContent( Content *content, bool prepend = false ); 00570 00587 // KDE5: Do not convert multipart->single-part automatically. 00588 void removeContent( Content *content, bool del = false ); 00589 00596 void changeEncoding( Headers::contentEncoding e ); 00597 00605 void toStream( QTextStream &ts, bool scrambleFromLines = false ); 00606 00607 // NOTE: The charset methods below are accessed by the headers which 00608 // have this Content as a parent. 00609 00616 QByteArray defaultCharset() const; 00617 00625 void setDefaultCharset( const QByteArray &cs ); 00626 00633 bool forceDefaultCharset() const; 00634 00644 virtual void setForceDefaultCharset( bool b ); 00645 00653 Content *content( const ContentIndex &index ) const; 00654 00660 ContentIndex indexForContent( Content *content ) const; 00661 00667 virtual bool isTopLevel() const; 00668 00675 void setParent( Content *parent ); 00676 00681 Content* parent() const; 00682 00687 Content* topLevel() const; 00688 00693 ContentIndex index() const; 00694 00701 //AK_REVIEW: move to MessageViewer/ObjectTreeParser 00702 bool bodyIsMessage() const; 00703 00722 //AK_REVIEW: move to MessageViewer/ObjectTreeParser 00723 boost::shared_ptr<Message> bodyAsMessage() const; 00724 00725 protected: 00731 virtual QByteArray assembleHeaders(); 00732 00737 KDE_DEPRECATED QByteArray rawHeader( const char *name ) const; 00738 00743 KDE_DEPRECATED QList<QByteArray> rawHeaders( const char *name ) const; 00744 00748 // KDE5: Not needed outside. Move to Private class. 00749 bool decodeText(); 00750 00755 template <class T> KDE_DEPRECATED T *headerInstance( T *ptr, bool create ); 00756 00761 // KDE5: Not needed outside. Move to Private class. 00762 Headers::Base::List h_eaders; 00763 00764 //@cond PRIVATE 00765 ContentPrivate *d_ptr; 00766 explicit Content( ContentPrivate *d ); 00767 //@endcond 00768 00769 private: 00770 Q_DECLARE_PRIVATE( Content ) 00771 Q_DISABLE_COPY( Content ) 00772 }; 00773 00774 // some compilers (for instance Compaq C++) need template inline functions 00775 // here rather than in the *.cpp file 00776 00777 template <class T> T *Content::headerInstance( T *ptr, bool create ) 00778 { 00779 return header<T>( create ); 00780 } 00781 00782 template <typename T> T *Content::header( bool create ) 00783 { 00784 Headers::Base *h = headerByType( T::staticType() ); 00785 if( h ) { 00786 // Make sure the header is actually of the right type. 00787 Q_ASSERT( dynamic_cast<T*>( h ) ); 00788 } else if( create ) { 00789 h = new T( this ); 00790 setHeader( h ); 00791 } 00792 return static_cast<T*>( h ); 00793 } 00794 00795 } // namespace KMime 00796 00797 #endif // __KMIME_CONTENT_H__