kmail

kmfoldersearch.h

00001 /*
00002     This file is part of KMail, the KDE mail client.
00003     Copyright (c) 2000 Don Sanders <sanders@kde.org>
00004 
00005     KMail is free software; you can redistribute it and/or modify it
00006     under the terms of the GNU General Public License, version 2, as
00007     published by the Free Software Foundation.
00008 
00009     KMail is distributed in the hope that it will be useful, but
00010     WITHOUT ANY WARRANTY; without even the implied warranty of
00011     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012     General Public License for more details.
00013 
00014     You should have received a copy of the GNU General Public License
00015     along with this program; if not, write to the Free Software
00016     Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
00017 */
00018 // Dynamic search folder
00019 
00020 #ifndef kmfoldersearch_h
00021 #define kmfoldersearch_h
00022 
00023 #include <tqguardedptr.h>
00024 #include <tqvaluelist.h>
00025 #include <tqvaluevector.h>
00026 #include <tqvaluestack.h>
00027 #include <tqmap.h>
00028 #include "kmfolder.h"
00029 #include "folderstorage.h"
00030 
00040 typedef TQValueList<TQ_UINT32> SerNumList;
00041 class KMSearchPattern;
00042 class KMFolderImap;
00043 class KMFolderSearchJob;
00044 class KMIndexSearchTarget;
00045 class TQTimer;
00046 
00047 namespace KMail {
00048    class AttachmentStrategy;
00049 }
00050 using KMail::AttachmentStrategy;
00051 
00052 class KMSearch: public TQObject
00053 {
00054   Q_OBJECT
00055   TQ_OBJECT
00056 
00057 public:
00058   KMSearch(TQObject * parent = 0, const char * name = 0);
00059   ~KMSearch();
00060 
00061   bool write(TQString location) const;
00062   bool read(TQString location);
00063   bool recursive() const { return mRecursive; }
00064   void setRecursive(bool recursive) { if (running()) stop(); mRecursive = recursive; }
00065   KMFolder* root() const { return mRoot; }
00066   void setRoot(KMFolder *folder) { if (running()) stop(); mRoot = folder; }
00067   bool inScope(KMFolder* folder) const;
00068   //Takes ownership of @searchPattern
00069   void setSearchPattern(KMSearchPattern *searchPattern);
00070   KMSearchPattern* searchPattern() const { return mSearchPattern; }
00071   void start();
00072   bool running() const { return mRunning; }
00073   void stop();
00074   int foundCount() const { return mFoundCount; }
00075   int searchCount() const { return mSearchCount; }
00076   TQString currentFolder() const { return mLastFolder; }
00077 
00078 public slots:
00079   void indexFinished();
00080 
00081 signals:
00082   void found(TQ_UINT32 serNum);
00083   void finished(bool success);
00084 
00085 protected slots:
00086   void slotProcessNextBatch();
00087   void slotSearchFolderResult( KMFolder*, TQValueList<TQ_UINT32>,
00088                                const KMSearchPattern*, bool );
00089 
00090 protected:
00091   friend class ::KMIndexSearchTarget;
00092   void setRunning(bool b) { mRunning = b; }
00093   void setFoundCount(int f) { mFoundCount = f; }
00094   void setCurrentFolder(const TQString &f) { mLastFolder = f; }
00095 
00096 private:
00097   int mRemainingFolders;
00098   bool mRecursive, mRunning, mIdle, mRunByIndex;
00099   TQGuardedPtr<KMFolder> mRoot;
00100   KMSearchPattern* mSearchPattern;
00101   TQValueList<TQGuardedPtr<KMFolder> > mFolders, mOpenedFolders;
00102   TQValueList<TQGuardedPtr<KMFolderImap> > mIncompleteFolders;
00103   SerNumList mSerNums;
00104   TQString mLastFolder;
00105   int mFoundCount;
00106   int mSearchCount;
00107   TQTimer *mProcessNextBatchTimer;
00108 };
00109 
00110 class KMFolderSearch: public FolderStorage
00111 {
00112   Q_OBJECT
00113   TQ_OBJECT
00114   friend class ::KMFolderSearchJob;
00115 public:
00116   KMFolderSearch(KMFolder* folder, const char* name=0);
00117   virtual ~KMFolderSearch();
00118 
00120   virtual KMFolderType folderType() const { return KMFolderTypeSearch; }
00121 
00122   // Sets and runs the search used by the folder
00123   void setSearch(KMSearch *search);
00124   // Returns the current search used by the folder
00125   const KMSearch* search() const;
00126   // Stops the current search
00127   void stopSearch() { if (mSearch) mSearch->stop(); }
00128 
00129   virtual KMMessage* getMsg(int idx);
00130   virtual void ignoreJobsForMessage( KMMessage* );
00131 
00132   virtual void tryReleasingFolder(KMFolder* folder);
00133 
00135   virtual bool isMoveable() const { return false; }
00136 
00137 protected slots:
00138   // Reads search definition for this folder and creates a KMSearch
00139   bool readSearch();
00140   // Runs the current search again
00141   void executeSearch();
00142   // Called when the search is finished
00143   void searchFinished(bool success);
00144   // Look at a new message and if it matches search() add it to the cache
00145   void examineAddedMessage(KMFolder *folder, TQ_UINT32 serNum);
00146   // Look at a removed message and remove it from the cache
00147   void examineRemovedMessage(KMFolder *folder, TQ_UINT32 serNum);
00148   // Look at a message whose status has changed
00149   void examineChangedMessage(KMFolder *folder, TQ_UINT32 serNum, int delta);
00150   // The serial numbers for a folder have been invalidated, deal with it
00151   void examineInvalidatedFolder(KMFolder *folder);
00152   // A folder has been deleted, deal with it
00153   void examineRemovedFolder(KMFolder *folder);
00154   // Propagate the msgHeaderChanged signal
00155   void propagateHeaderChanged(KMFolder *folder, int idx);
00156 
00157 public slots:
00158   // Appends the serial number to the cached list of messages that match
00159   // the search for this folder
00160   void addSerNum(TQ_UINT32 serNum);
00161   // Removes the serial number from the cached list of messages that match
00162   // the search for this folder
00163   void removeSerNum(TQ_UINT32 serNum);
00164 
00166   virtual int updateIndex();
00167 
00168   // Examine the message
00169   void slotSearchExamineMsgDone( KMFolder*, TQ_UINT32 serNum,
00170                                  const KMSearchPattern*, bool );
00171 
00172 public:
00173   //See base class for documentation
00174   virtual int addMsg(KMMessage* msg, int* index_return = 0);
00175   virtual int open(const char *owner);
00176   virtual int canAccess();
00177   virtual void sync();
00178   virtual void reallyDoClose(const char* owner);
00179   virtual int create();
00180   virtual int compact( bool );
00181   virtual bool isReadOnly() const;
00182   virtual const KMMsgBase* getMsgBase(int idx) const;
00183   virtual KMMsgBase* getMsgBase(int idx);
00184   virtual int find(const KMMsgBase* msg) const;
00185   virtual TQString indexLocation() const;
00186   virtual int writeIndex( bool createEmptyIndex = false );
00187   DwString getDwString(int idx);
00188   TQ_UINT32 serNum(int idx) { return mSerNums[idx]; }
00189 
00190 protected:
00191   virtual FolderJob* doCreateJob(KMMessage *msg, FolderJob::JobType jt,
00192                                  KMFolder *folder, TQString partSpecifier,
00193                                  const AttachmentStrategy *as ) const;
00194   virtual FolderJob* doCreateJob(TQPtrList<KMMessage>& msgList, const TQString& sets,
00195                                  FolderJob::JobType jt, KMFolder *folder) const;
00196   virtual KMMessage* readMsg(int idx);
00197   virtual bool readIndex();
00198   virtual int removeContents();
00199   virtual int expungeContents();
00200   virtual int count(bool cache = false) const;
00201   virtual KMMsgBase* takeIndexEntry(int idx);
00202   virtual KMMsgInfo* setIndexEntry(int idx, KMMessage *msg);
00203   virtual void clearIndex(bool autoDelete=true, bool syncDict = false);
00204   virtual void truncateIndex();
00205 
00206 private:
00207   TQValueVector<TQ_UINT32> mSerNums;
00208   TQValueList<TQGuardedPtr<KMFolder> > mFolders;
00209   TQValueStack<TQ_UINT32> mUnexaminedMessages;
00210   FILE *mIdsStream;
00211   KMSearch *mSearch;
00212   bool mInvalid, mUnlinked;
00213   bool mTempOpened;
00214   TQTimer *mExecuteSearchTimer;
00215   TQMap<const KMFolder*, unsigned int>mFoldersCurrentlyBeingSearched;
00216 };
00217 #endif /*kmfoldersearch_h*/
00218