kmail

folderstorage.h

Go to the documentation of this file.
00001 /*
00002     Virtual base class for mail storage.
00003 
00004     This file is part of KMail.
00005 
00006     Copyright (c) 2004 Bo Thorsen <bo@sonofthor.dk>
00007 
00008     This library is free software; you can redistribute it and/or
00009     modify it under the terms of the GNU Library General Public
00010     License as published by the Free Software Foundation; either
00011     version 2 of the License, or (at your option) any later version.
00012 
00013     This library is distributed in the hope that it will be useful,
00014     but WITHOUT ANY WARRANTY; without even the implied warranty of
00015     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00016     Library General Public License for more details.
00017 
00018     You should have received a copy of the GNU Library General Public License
00019     along with this library; see the file COPYING.LIB.  If not, write to
00020     the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00021     Boston, MA 02110-1301, USA.
00022 
00023     In addition, as a special exception, the copyright holders give
00024     permission to link the code of this program with any edition of
00025     the Qt library by Trolltech AS, Norway (or with modified versions
00026     of Qt that use the same license as Qt), and distribute linked
00027     combinations including the two.  You must obey the GNU General
00028     Public License in all respects for all of the code used other than
00029     Qt.  If you modify this file, you may extend this exception to
00030     your version of the file, but you are not obligated to do so.  If
00031     you do not wish to do so, delete this exception statement from
00032     your version.
00033 */
00034 
00035 #ifndef FOLDERSTORAGE_H
00036 #define FOLDERSTORAGE_H
00037 
00038 // for large file support
00039 #include <config.h>
00040 
00041 #include "kmfoldernode.h"
00042 #include "kmfoldertype.h"
00043 #include "kmmsginfo.h"
00044 #include "kmglobal.h"
00045 #include "folderjob.h"
00046 using KMail::FolderJob;
00047 
00048 #include "mimelib/string.h"
00049 
00050 #include <sys/types.h>
00051 #include <stdio.h>
00052 
00053 class KMMessage;
00054 class KMAccount;
00055 class KMFolderDir;
00056 class KMMsgDict; // for the rDict manipulations
00057 class KMMsgDictREntry;
00058 class QTimer;
00059 class KMSearchPattern;
00060 
00061 namespace KMail {
00062    class AttachmentStrategy;
00063 }
00064 using KMail::AttachmentStrategy;
00065 
00066 typedef QValueList<Q_UINT32> SerNumList;
00067 
00079 class FolderStorage : public QObject
00080 {
00081   Q_OBJECT
00082 
00083 public:
00084 
00085 
00089   FolderStorage( KMFolder* folder, const char* name=0 );
00090   virtual ~FolderStorage();
00091 
00092   KMFolder* folder() const { return mFolder; }
00093 
00095   virtual KMFolderType folderType() const { return KMFolderTypeUnknown; }
00096 
00098   virtual QString fileName() const;
00100   QString location() const;
00101 
00103   virtual QString indexLocation() const = 0;
00104 
00106   virtual bool noContent() const { return mNoContent; }
00107 
00109   virtual void setNoContent(bool aNoContent)
00110     { mNoContent = aNoContent; }
00111 
00113   virtual bool noChildren() const { return mNoChildren; }
00114 
00116   virtual void setNoChildren( bool aNoChildren );
00117 
00118   enum ChildrenState {
00119     HasChildren,
00120     HasNoChildren,
00121     ChildrenUnknown
00122   };
00125   virtual ChildrenState hasChildren() const { return mHasChildren; }
00126 
00128   virtual void setHasChildren( ChildrenState state )
00129     { mHasChildren = state; }
00130 
00132   virtual void updateChildrenState();
00133 
00135   virtual KMMessage* getMsg(int idx);
00136 
00138   virtual KMMsgInfo* unGetMsg(int idx);
00139 
00141   virtual bool isMessage(int idx);
00142 
00147   virtual KMMessage* readTemporaryMsg(int idx);
00148 
00150   virtual DwString getDwString(int idx) = 0;
00151 
00155   virtual void ignoreJobsForMessage( KMMessage* );
00156 
00161   virtual FolderJob* createJob( KMMessage *msg, FolderJob::JobType jt = FolderJob::tGetMessage,
00162                                 KMFolder *folder = 0, QString partSpecifier = QString::null,
00163                                 const AttachmentStrategy *as = 0 ) const;
00164   virtual FolderJob* createJob( QPtrList<KMMessage>& msgList, const QString& sets,
00165                                 FolderJob::JobType jt = FolderJob::tGetMessage,
00166                                 KMFolder *folder = 0 ) const;
00167 
00172   virtual const KMMsgBase* getMsgBase(int idx) const = 0;
00173   virtual KMMsgBase* getMsgBase(int idx) = 0;
00174 
00176   virtual const KMMsgBase* operator[](int idx) const { return getMsgBase(idx); }
00177 
00179   virtual KMMsgBase* operator[](int idx) { return getMsgBase(idx); }
00180 
00183   virtual KMMessage* take(int idx);
00184   virtual void take(QPtrList<KMMessage> msgList);
00185 
00192   virtual int addMsg(KMMessage* msg, int* index_return = 0) = 0;
00193 
00197   virtual int addMsgKeepUID(KMMessage* msg, int* index_return = 0) {
00198     return addMsg(msg, index_return);
00199   }
00200 
00205   virtual int addMsg( QPtrList<KMMessage>&, QValueList<int>& index_return );
00206 
00209   void emitMsgAddedSignals(int idx);
00210 
00213   virtual bool canAddMsgNow(KMMessage* aMsg, int* aIndex_ret);
00214 
00216   virtual void removeMsg(int i, bool imapQuiet = false);
00217   virtual void removeMsg(const QPtrList<KMMsgBase>& msgList, bool imapQuiet = false);
00218   virtual void removeMsg(const QPtrList<KMMessage>& msgList, bool imapQuiet = false);
00219 
00222   virtual int expungeOldMsg(int days);
00223 
00228   virtual int moveMsg(KMMessage* msg, int* index_return = 0);
00229   virtual int moveMsg(QPtrList<KMMessage>, int* index_return = 0);
00230 
00232   virtual int find(const KMMsgBase* msg) const = 0;
00233   int find( const KMMessage * msg ) const;
00234 
00236   virtual int count(bool cache = false) const;
00237 
00239   virtual int countUnread();
00240 
00242   Q_INT64 folderSize() const;
00243 
00246   virtual bool isCloseToQuota() const;
00247 
00250   virtual void msgStatusChanged( const KMMsgStatus oldStatus,
00251                                  const KMMsgStatus newStatus,
00252                  int idx);
00253 
00259   virtual int open(const char* owner) = 0;
00260 
00263   virtual int canAccess() = 0;
00264 
00268   void close(const char* owner, bool force=false);
00269   virtual void reallyDoClose(const char* owner) = 0;
00270 
00273   virtual void tryReleasingFolder(KMFolder*) {}
00274 
00276   virtual void sync() = 0;
00277 
00279   bool isOpened() const { return (mOpenCount>0); }
00280 
00282   virtual void markNewAsUnread();
00283 
00285   virtual void markUnreadAsRead();
00286 
00290   virtual int create() = 0;
00291 
00296   virtual void remove();
00297 
00301   virtual int expunge();
00302 
00307   virtual int compact( bool silent ) = 0;
00308 
00311   virtual int rename(const QString& newName, KMFolderDir *aParent = 0);
00312 
00314   bool autoCreateIndex() const { return mAutoCreateIndex; }
00315 
00318   virtual void setAutoCreateIndex(bool);
00319 
00323   bool dirty() const { return mDirty; }
00324 
00326   void setDirty(bool f);
00327 
00329   bool needsCompacting() const { return needsCompact; }
00330   virtual void setNeedsCompacting(bool f) { needsCompact = f; }
00331 
00340   virtual void quiet(bool beQuiet);
00341 
00343   virtual bool isReadOnly() const = 0;
00344 
00346   QString label() const;
00347 
00349   virtual void correctUnreadMsgsCount();
00350 
00353   virtual int writeIndex( bool createEmptyIndex = false ) = 0;
00354 
00357   void registerWithMessageDict();
00358 
00361   void deregisterFromMessageDict();
00362 
00364   virtual void setStatus(int idx, KMMsgStatus status, bool toggle=false);
00365 
00367   virtual void setStatus(QValueList<int>& ids, KMMsgStatus status, bool toggle=false);
00368 
00369   void removeJobs();
00370 
00372   static QString dotEscape(const QString&);
00373 
00375   virtual void readConfig();
00376 
00378   virtual void writeConfig();
00379 
00384   virtual KMFolder* trashFolder() const { return 0; }
00385 
00390   void addJob( FolderJob* ) const;
00391 
00393   bool compactable() const { return mCompactable; }
00394 
00396   // If quiet is true, the KMailIcalIface is not informed of the changed. That's usefull
00397   // for folder that are being copied around, should retain their type, but not cause
00398   // conflicts on copy because events are identical in two folders.
00399   virtual void setContentsType( KMail::FolderContentsType type, bool quiet = false );
00401   KMail::FolderContentsType contentsType() const { return mContentsType; }
00402 
00407   virtual void search( const KMSearchPattern* );
00408 
00413   virtual void search( const KMSearchPattern*, Q_UINT32 serNum );
00414 
00416   virtual bool isMoveable() const;
00417 
00418   virtual KMAccount* account() const;
00419 
00420 signals:
00423   void changed();
00424 
00427   void cleared();
00428 
00431   void expunged( KMFolder* );
00432 
00434   void closed( KMFolder* );
00435   
00437   void invalidated( KMFolder * );
00438 
00440   void nameChanged();
00441 
00445   void locationChanged( const QString &, const QString & );
00446 
00449   void contentsTypeChanged( KMail::FolderContentsType type );
00450 
00452   void readOnlyChanged(KMFolder*);
00453 
00455   void msgRemoved(KMFolder*, Q_UINT32 sernum);
00456 
00458   void msgRemoved( int idx, QString msgIdMD5 );
00459   void msgRemoved( KMFolder* );
00460 
00462   void msgAdded(int idx);
00463   void msgAdded(KMFolder*, Q_UINT32 sernum);
00464 
00466   void msgChanged(KMFolder*, Q_UINT32 sernum, int delta);
00467 
00469   void msgHeaderChanged(KMFolder*, int);
00470 
00472   void statusMsg(const QString&);
00473 
00475   void numUnreadMsgsChanged( KMFolder* );
00476 
00478   void removed(KMFolder*, bool);
00479 
00485   void searchResult( KMFolder*, QValueList<Q_UINT32>,
00486                      const KMSearchPattern*, bool complete );
00487 
00492   void searchDone( KMFolder*, Q_UINT32, const KMSearchPattern*, bool );
00493 
00495   void folderSizeChanged();
00496 
00497 
00498 public slots:
00500   virtual int updateIndex() = 0;
00501 
00504   virtual void reallyAddMsg(KMMessage* aMsg);
00505 
00508   virtual void reallyAddCopyOfMsg(KMMessage* aMsg);
00509 
00511   void slotEmitChangedTimer();
00512 
00513 protected slots:
00514   virtual void removeJob( QObject* );
00515 
00517   void slotProcessNextSearchBatch();
00518 
00519 protected:
00520 
00526   virtual FolderJob* doCreateJob( KMMessage *msg, FolderJob::JobType jt, KMFolder *folder,
00527                                   QString partSpecifier, const AttachmentStrategy *as ) const = 0;
00528   virtual FolderJob* doCreateJob( QPtrList<KMMessage>& msgList, const QString& sets,
00529                                   FolderJob::JobType jt, KMFolder *folder ) const = 0;
00530 
00534   void headerOfMsgChanged(const KMMsgBase*, int idx);
00535 
00538   virtual KMMessage* readMsg(int idx) = 0;
00539 
00540   //--------- Message Dict manipulation
00541 friend class KMMsgDict;
00545   virtual void fillMessageDict() {}
00546 
00550   void readFolderIdsFile();
00551 
00553   int writeFolderIdsFile() const;
00554 
00556   int touchFolderIdsFile();
00557 
00559   int appendToFolderIdsFile( int idx = -1 );
00560 
00564   void setRDict(KMMsgDictREntry *rentry) const;
00565 
00567   KMMsgDictREntry *rDict() const { return mRDict; }
00568 
00569 
00572   void replaceMsgSerNum( unsigned long sernum, KMMsgBase* msg, int idx );
00573 
00577   void invalidateFolder();
00578 
00582   virtual int removeContents() = 0;
00583 
00587   virtual int expungeContents() = 0;
00588 
00590   virtual bool readIndex() = 0;
00591   virtual KMMsgBase* takeIndexEntry( int idx ) = 0;
00592   virtual KMMsgInfo* setIndexEntry( int idx, KMMessage *msg ) = 0;
00593   virtual void clearIndex(bool autoDelete=true, bool syncDict = false) = 0;
00594   virtual void truncateIndex() = 0;
00595 
00596   virtual Q_INT64 doFolderSize() const { return 0; };
00597 
00598   int mOpenCount;
00599   int mQuiet;
00600   bool mChanged :1;
00602   bool mAutoCreateIndex :1;
00605   bool mExportsSernums :1;
00607   bool mDirty :1;
00609   bool mFilesLocked :1;
00610 
00612   int mUnreadMsgs, mGuessedUnreadMsgs;
00613   int mTotalMsgs;
00614   Q_INT64 mSize;
00615   bool mWriteConfigEnabled :1;
00617   bool needsCompact :1;
00619   bool mCompactable :1;
00620   bool mNoContent :1;
00621   bool mNoChildren :1;
00622   bool mConvertToUtf8 :1;
00623 
00625   mutable KMMsgDictREntry *mRDict;
00627   mutable QPtrList<FolderJob> mJobList;
00628 
00629   QTimer *mDirtyTimer;
00630   enum { mDirtyTimerInterval = 600000 }; // 10 minutes
00631 
00632   ChildrenState mHasChildren;
00633 
00635   KMail::FolderContentsType mContentsType;
00636 
00637   KMFolder* mFolder;
00638 
00639   QTimer * mEmitChangedTimer;
00640 
00641   int mCurrentSearchedMsg;
00642   const KMSearchPattern* mSearchPattern;
00643 };
00644 
00645 #endif // FOLDERSTORAGE_H