kmail

partNode.h

00001 /* -*- c++ -*-
00002     partNode.h A node in a MIME tree.
00003 
00004     This file is part of KMail, the KDE mail client.
00005     Copyright (c) 2002,2004 Klar�lvdalens Datakonsult AB
00006 
00007     KMail is free software; you can redistribute it and/or modify it
00008     under the terms of the GNU General Public License, version 2, as
00009     published by the Free Software Foundation.
00010 
00011     KMail is distributed in the hope that it will be useful, but
00012     WITHOUT ANY WARRANTY; without even the implied warranty of
00013     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00014     General Public License for more details.
00015 
00016     You should have received a copy of the GNU General Public License
00017     along with this program; if not, write to the Free Software
00018     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00019 
00020     In addition, as a special exception, the copyright holders give
00021     permission to link the code of this program with any edition of
00022     the Qt library by Trolltech AS, Norway (or with modified versions
00023     of Qt that use the same license as Qt), and distribute linked
00024     combinations including the two.  You must obey the GNU General
00025     Public License in all respects for all of the code used other than
00026     Qt.  If you modify this file, you may extend this exception to
00027     your version of the file, but you are not obligated to do so.  If
00028     you do not wish to do so, delete this exception statement from
00029     your version.
00030 */
00031 
00032 #ifndef PARTNODE_H
00033 #define PARTNODE_H
00034 
00035 #include "kmmsgpart.h"
00036 #include "kmmsgbase.h"
00037 #include "kmmessage.h"
00038 
00039 #include "interfaces/bodypart.h"
00040 
00041 #include <mimelib/mimepp.h>
00042 #include <mimelib/body.h>
00043 #include <mimelib/utility.h>
00044 
00045 #include <kio/global.h>
00046 #include <kdebug.h>
00047 
00048 #include <map>
00049 
00050 class KMMimePartTreeItem;
00051 class KMMimePartTree;
00052 
00053 class KMReaderWin;
00054 
00055 /*
00056  ===========================================================================
00057 
00058 
00059        S T A R T    O F     T E M P O R A R Y     M I M E     C O D E
00060 
00061 
00062  ===========================================================================
00063   N O T E :   The partNode structure will most likely be replaced by KMime.
00064               It's purpose: Speed optimization for KDE 3.   (khz, 28.11.01)
00065  ===========================================================================
00066 */
00067 class partNode
00068 {
00069     partNode();
00070 
00071     int calcNodeIdOrFindNode( int& curId, const partNode* calcNode,
00072                               int findId, partNode** findNode );
00073 
00074     partNode( KMReaderWin * win, DwBodyPart* dwPart,
00075               int explicitType    = DwMime::kTypeUnknown,
00076               int explicitSubType = DwMime::kSubtypeUnknown,
00077           bool deleteDwBodyPart = false );
00078 
00079 public:
00080     static partNode * fromMessage( const KMMessage * msg, KMReaderWin * win=0 );
00081 
00082     partNode( bool deleteDwBodyPart,
00083               DwBodyPart* dwPart );
00084 
00085     ~partNode();
00086 
00087     void dump( int chars=0 ) const;
00088 
00089     void buildObjectTree( bool processSiblings=true );
00090 
00091     DwBodyPart* dwPart() const {
00092         return mDwPart;
00093     }
00094 
00095     void setDwPart( DwBodyPart* part ) {
00096         mDwPart = part;
00097         mMsgPartOk = false;
00098     }
00099 
00100     KMMessagePart& msgPart() const {
00101         if( !mMsgPartOk ) {
00102             KMMessage::bodyPart(mDwPart, &mMsgPart);
00103             mMsgPartOk = true;
00104         }
00105         return mMsgPart;
00106     }
00107 
00108     const QCString & encodedBody();
00109 
00110     void setType( int type ) {
00111         mType = type;
00112     }
00113 
00114     void setSubType( int subType ) {
00115         mSubType = subType;
00116     }
00117 
00118     int type() const {
00119         return mType;
00120     }
00121 
00122     QCString typeString() const;
00123 
00124     int subType() const {
00125         return mSubType;
00126     }
00127 
00128     QCString subTypeString() const;
00129 
00130     bool hasType( int type ) {
00131       return mType == type;
00132     }
00133 
00134     bool hasSubType( int subType ) {
00135       return mSubType == subType;
00136     }
00137 
00138     void setEncryptionState( KMMsgEncryptionState state ) {
00139         mEncryptionState = state;
00140     }
00141     KMMsgEncryptionState encryptionState() const {
00142         return mEncryptionState;
00143     }
00144 
00145     // look at the encryption states of all children and return result
00146     KMMsgEncryptionState overallEncryptionState() const ;
00147 
00148     // look at the signature states of all children and return result
00149     KMMsgSignatureState  overallSignatureState() const ;
00150 
00151     void setSignatureState( KMMsgSignatureState state ) {
00152         mSignatureState = state;
00153     }
00154     KMMsgSignatureState signatureState() const {
00155         return mSignatureState;
00156     }
00157 
00158     // path is a hierarchical path to this partNode. It is designed to
00159     // be stable under decryption, where new child nodes are
00160     // added. Treat it as an opaque string.
00161     QCString path() const;
00162 
00163     int nodeId() const;  // node ids start at 1 (this is the top level root node)
00164 
00165     partNode* findId( int id );  // returns the node which has the given id (or 0, resp.)
00166 
00167     partNode* findType( int type, int subType, bool deep=true, bool wide=true );
00168 
00169     partNode* findTypeNot( int type, int subType, bool deep=true,
00170                            bool wide=true );
00171 
00172     partNode* findNodeForDwPart( DwBodyPart* part );
00173 
00174     void fillMimePartTree( KMMimePartTreeItem* parentItem,
00175                            KMMimePartTree*     mimePartTree,
00176                            QString labelDescr    = QString::null,
00177                            QString labelCntType  = QString::null,
00178                            QString labelEncoding = QString::null,
00179                            KIO::filesize_t size=0,
00180                            bool revertOrder = false );
00181 
00182     void adjustDefaultType( partNode* node );
00183 
00184     void setNext( partNode* next ) {
00185         mNext = next;
00186         if( mNext ){
00187             mNext->mRoot = mRoot;
00188             adjustDefaultType( mNext );
00189         }
00190     }
00191 
00192     void setFirstChild( partNode* child ) {
00193         mChild = child;
00194         if( mChild ) {
00195             mChild->mRoot = this;
00196             adjustDefaultType( mChild );
00197         }
00198     }
00199 
00200     void setProcessed( bool processed, bool recurse ) {
00201         mWasProcessed = processed;
00202     if ( recurse ) {
00203       if( mChild )
00204             mChild->setProcessed( processed, true );
00205       if( mNext )
00206             mNext->setProcessed( processed, true );
00207     }
00208     }
00209 
00210     void setMimePartTreeItem( KMMimePartTreeItem* item ) {
00211         mMimePartTreeItem = item;
00212     }
00213 
00214     KMMimePartTreeItem* mimePartTreeItem() {
00215         return mMimePartTreeItem;
00216     }
00217 
00218     void setFromAddress( const QString& address ) {
00219         mFromAddress = address;
00220     }
00221 
00222     bool isAttachment() const;
00223     bool isHeuristicalAttachment() const;
00227     bool isFirstTextPart() const;
00228 
00229     bool hasContentDispositionInline() const;
00230 
00231     QString contentTypeParameter( const char * name ) const;
00232 
00233     const QString& trueFromAddress() const;
00234 
00235     const partNode * topLevelParent() const;
00236     partNode * parentNode() const { return mRoot; }
00237     partNode * nextSibling() const { return mNext; }
00238     partNode * firstChild() const { return mChild; }
00239     partNode * next( bool allowChildren=true ) const;
00240     int childCount() const;
00241     int totalChildCount() const;
00242     bool processed() const { return mWasProcessed; }
00243 
00244     KMail::Interface::BodyPartMemento * bodyPartMemento( const QCString & which ) const;
00245     void setBodyPartMemento( const QCString & which, KMail::Interface::BodyPartMemento * memento );
00246 
00247     // A flag to remember if the node was embedded. This is useful for attachment nodes, the reader
00248     // needs to know if they were displayed inline or not.
00249     bool isDisplayedEmbedded() const;
00250     void setDisplayedEmbedded( bool displayedEmbedded );
00251 
00252     // Get a href in the form attachment:<nodeId>?place=<place>, used by ObjectTreeParser and
00253     // UrlHandlerManager.
00254     QString asHREF( const QString &place ) const;
00255 
00256 private:
00257     KMReaderWin * reader() const {
00258         return mReader ? mReader : mRoot ? mRoot->reader() : 0 ;
00259     }
00260     KMail::Interface::BodyPartMemento * internalBodyPartMemento( const QCString & ) const;
00261     void internalSetBodyPartMemento( const QCString & which, KMail::Interface::BodyPartMemento * memento );
00262 
00263 private:
00264     partNode*     mRoot;
00265     partNode*     mNext;
00266     partNode*     mChild;
00267     bool          mWasProcessed; // to be used by parseObjectTree()
00268 private:
00269     DwBodyPart*   mDwPart;   // may be zero
00270     mutable KMMessagePart mMsgPart;  // is valid - even if mDwPart is zero
00271     QCString      mEncodedBody;
00272     QString       mFromAddress;
00273     int           mType;
00274     int           mSubType;
00275     KMMsgEncryptionState mEncryptionState;
00276     KMMsgSignatureState  mSignatureState;
00277     mutable bool  mMsgPartOk;
00278     bool          mEncodedOk;
00279     bool          mDeleteDwBodyPart;
00280     KMMimePartTreeItem* mMimePartTreeItem;
00281     std::map<QCString,KMail::Interface::BodyPartMemento*> mBodyPartMementoMap;
00282     KMReaderWin * mReader;
00283     bool mDisplayedEmbedded;
00284 };
00285 
00286 #endif