kmail

kmfoldercachedimap.h

00001 /*
00002  *  kmfoldercachedimap.cpp
00003  *
00004  *  Copyright (c) 2002-2004 Bo Thorsen <bo@sonofthor.dk>
00005  *  Copyright (c) 2002-2003 Steffen Hansen <steffen@klaralvdalens-datakonsult.se>
00006  *
00007  *  This program is free software; you can redistribute it and/or modify
00008  *  it under the terms of the GNU General Public License as published by
00009  *  the Free Software Foundation; version 2 of the License
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU 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 kmfoldercachedimap_h
00033 #define kmfoldercachedimap_h
00034 
00035 #include <kdialogbase.h>
00036 #include <kstandarddirs.h>
00037 #include <tqvaluelist.h>
00038 #include <tqvaluevector.h>
00039 #include <tqptrlist.h>
00040 #include <tqdialog.h>
00041 
00042 #include "kmfoldermaildir.h"
00043 #include "kmfolderimap.h"
00044 #include "kmacctcachedimap.h"
00045 #include "kmfoldertype.h"
00046 #include "folderjob.h"
00047 #include "cachedimapjob.h"
00048 #include "quotajobs.h"
00049 
00050 #include <set>
00051 
00052 using KMail::FolderJob;
00053 using KMail::QuotaInfo;
00054 class KMCommand;
00055 
00056 class TQComboBox;
00057 class TQRadioButton;
00058 
00059 namespace KMail {
00060   class AttachmentStrategy;
00061   class ImapAccountBase;
00062   struct ACLListEntry;
00063 }
00064 using KMail::AttachmentStrategy;
00065 
00066 class DImapTroubleShootDialog : public KDialogBase
00067 {
00068   Q_OBJECT
00069 public:
00070   enum SelectedOperation {
00071     None = -1,
00072     ReindexCurrent = 0,
00073     ReindexRecursive = 1,
00074     ReindexAll = 2,
00075     RefreshCache
00076   };
00077 
00078   DImapTroubleShootDialog( TQWidget* parent=0, const char* name=0 );
00079 
00080   static int run();
00081 
00082 private slots:
00083   void slotDone();
00084   void slotChanged();
00085 private:
00086   TQRadioButton *mIndexButton, *mCacheButton;
00087   TQComboBox *mIndexScope;
00088   TQButtonGroup *mButtonGroup;
00089   int rc;
00090 };
00091 
00092 class KMFolderCachedImap : public KMFolderMaildir
00093 {
00094   Q_OBJECT
00095 
00096 public:
00097   static TQString cacheLocation() {
00098      return locateLocal("data", "kmail/dimap" );
00099   }
00100 
00104   KMFolderCachedImap(KMFolder* folder, const char* name=0);
00105   virtual ~KMFolderCachedImap();
00106 
00108   void reallyDoClose(const char* owner);
00109 
00111   void initializeFrom( KMFolderCachedImap* parent );
00112 
00113   virtual void readConfig();
00114   virtual void writeConfig();
00115 
00116   void writeConfigKeysWhichShouldNotGetOverwrittenByReadConfig();
00117 
00119   virtual KMFolderType folderType() const { return KMFolderTypeCachedImap; }
00120 
00122   virtual int create();
00123 
00125   virtual void remove();
00126 
00128   virtual void serverSync( bool recurse, bool quotaOnly = false );
00129 
00131   void resetSyncState( );
00132 
00136   void setAlarmsBlocked( bool blocked );
00138   bool alarmsBlocked() const;
00139 
00140   void checkUidValidity();
00141 
00142   enum imapState { imapNoInformation=0, imapInProgress=1, imapFinished=2 };
00143 
00144   virtual imapState getContentState() const { return mContentState; }
00145   virtual void setContentState(imapState state) { mContentState = state; }
00146 
00147   virtual imapState getSubfolderState() { return mSubfolderState; }
00148   virtual void setSubfolderState(imapState state);
00149 
00151   void setImapPath(const TQString &path);
00152   TQString imapPath() const { return mImapPath; }
00153 
00155   void setLastUid( ulong uid );
00156   ulong lastUid();
00157 
00159   KMMsgBase* findByUID( ulong uid );
00160 
00162   void setUidValidity(const TQString &validity) { mUidValidity = validity; }
00163   TQString uidValidity() const { return mUidValidity; }
00164 
00167   void clearUidMap() { uidMap.clear(); }
00168 
00170   void setAccount(KMAcctCachedImap *acct);
00171   KMAcctCachedImap* account() const;
00172 
00174   TQString uidCacheLocation() const;
00175 
00177   int readUidCache();
00178 
00180   int writeUidCache();
00181 
00183   int progress() const { return mProgress; }
00184 
00185   /* Reimplemented from KMFolder. Moving is not supported, so aParent must be 0 */
00186   virtual int rename(const TQString& aName, KMFolderDir *aParent=0);
00187 
00193   virtual KMMessage* take(int idx);
00194 
00200   void takeTemporarily( int idx );
00201 
00202   /* Reimplemented from KMFolderMaildir */
00203   virtual int addMsg(KMMessage* msg, int* index_return = 0);
00204   /* internal version that doesn't remove the X-UID header */
00205   virtual int addMsgInternal(KMMessage* msg, bool, int* index_return = 0);
00206   virtual int addMsgKeepUID(KMMessage* msg, int* index_return = 0) {
00207     return addMsgInternal(msg, false, index_return);
00208   }
00209 
00210   /* Reimplemented from KMFolderMaildir */
00211   virtual void removeMsg(int i, bool imapQuiet = false);
00212   virtual void removeMsg( const TQPtrList<KMMsgBase> & msgList, bool imapQuiet = false)
00213     { FolderStorage::removeMsg(msgList, imapQuiet); }
00214 
00216   bool isReadOnly() const { return KMFolderMaildir::isReadOnly() || mReadOnly; }
00217   bool canDeleteMessages() const;
00218 
00219 
00223   void sendFolderComplete(bool success)
00224   { emit folderComplete(this, success); }
00225 
00229   void setSilentUpload( bool silent ) { mSilentUpload = silent; }
00230   bool silentUpload() { return mSilentUpload; }
00231 
00232   virtual int createIndexFromContents() {
00233     const int result = KMFolderMaildir::createIndexFromContents();
00234     reloadUidMap();
00235     return result;
00236   }
00237 
00238   int createIndexFromContentsRecursive();
00239 
00240   //virtual void holdSyncs( bool hold ) { mHoldSyncs = hold; }
00241 
00247   virtual bool listDirectory();
00248 
00249   virtual void listNamespaces();
00250 
00252   KMFolder* trashFolder() const;
00253 
00259   int userRights() const { return mUserRights; }
00260   KMail::ACLJobs::ACLFetchState userRightsState() const { return mUserRightsState; }
00261 
00263   void setUserRights( unsigned int userRights, KMail::ACLJobs::ACLFetchState state  );
00264 
00274   const QuotaInfo quotaInfo() const { return mQuotaInfo; }
00275   void setQuotaInfo( const QuotaInfo & );
00276 
00278   typedef TQValueVector<KMail::ACLListEntry> ACLList;
00279   const ACLList& aclList() const { return mACLList; }
00280   KMail::ACLJobs::ACLFetchState aclListState() const { return mACLListState; };
00281 
00283   void setACLList( const ACLList& arr );
00284 
00285   // Reimplemented so the mStatusChangedLocally bool can be set
00286   virtual void setStatus( int id, KMMsgStatus status, bool toggle );
00287   virtual void setStatus( TQValueList<int>& ids, KMMsgStatus status, bool toggle );
00288 
00289   TQString annotationFolderType() const { return mAnnotationFolderType; }
00290 
00291   // For kmailicalifaceimpl only
00292   void updateAnnotationFolderType();
00293 
00302   enum IncidencesFor { IncForNobody, IncForAdmins, IncForReaders };
00303 
00304   IncidencesFor incidencesFor() const { return mIncidencesFor; }
00306   void setIncidencesFor( IncidencesFor incfor );
00307 
00309   bool sharedSeenFlags() const { return mSharedSeenFlags; }
00311   void setSharedSeenFlags( bool b );
00312 
00314   virtual bool isMoveable() const;
00315 
00320   TQStringList namespacesToList() { return mNamespacesToList; }
00321   void setNamespacesToList( TQStringList list ) { mNamespacesToList = list; }
00322 
00327   const TQString& imapPathForCreation() { return mImapPathCreation; }
00328   void setImapPathForCreation( const TQString& path ) { mImapPathCreation = path; }
00329 
00331   bool isCloseToQuota() const;
00332 
00334   int permanentFlags() const { return mPermanentFlags; }
00335 
00336 
00337   TQString folderAttributes() const { return mFolderAttributes; }
00338 
00339   virtual bool mailCheckInProgress() const;
00340 
00341 protected slots:
00342   void slotGetMessagesData(KIO::Job * job, const TQByteArray & data);
00343   void getMessagesResult(KMail::FolderJob *, bool lastSet);
00344   void slotGetLastMessagesResult(KMail::FolderJob *);
00345   void slotProgress(unsigned long done, unsigned long total);
00346   void slotPutProgress( unsigned long, unsigned long );
00347 
00348   //virtual void slotCheckValidityResult(KIO::Job * job);
00349   void slotSubFolderComplete(KMFolderCachedImap*, bool);
00350   void slotSubFolderCloseToQuotaChanged();
00351 
00352   // Connected to the imap account
00353   void slotConnectionResult( int errorCode, const TQString& errorMsg );
00354 
00355   void slotCheckUidValidityResult( KMail::FolderJob* job );
00356   void slotPermanentFlags( int flags );
00357   void slotTestAnnotationResult(KIO::Job *job);
00358   void slotGetAnnotationResult( KIO::Job* );
00359   void slotMultiUrlGetAnnotationResult( KIO::Job* );
00360   void slotSetAnnotationResult(KIO::Job *job);
00361   void slotReceivedUserRights( KMFolder* );
00362   void slotReceivedACL( KMFolder*, KIO::Job*, const KMail::ACLList& );
00363 
00364   void slotMultiSetACLResult(KIO::Job *);
00365   void slotACLChanged( const TQString&, int );
00366   void slotAnnotationResult(const TQString& entry, const TQString& value, bool found);
00367   void slotAnnotationChanged( const TQString& entry, const TQString& attribute, const TQString& value );
00368   void slotDeleteMessagesResult(KMail::FolderJob *);
00369   void slotImapStatusChanged(KMFolder* folder, const TQString&, bool);
00370   void slotStorageQuotaResult( const QuotaInfo& );
00371   void slotQuotaResult( KIO::Job* job );
00372 
00373 protected:
00374   /* returns true if there were messages to delete
00375      on the server */
00376   bool deleteMessages();
00377   void listMessages();
00378   void uploadNewMessages();
00379   void uploadFlags();
00380   void uploadSeenFlags();
00381   void createNewFolders();
00382 
00383   void listDirectory2();
00384   void createFoldersNewOnServerAndFinishListing( const TQValueVector<int> foldersNewOnServer );
00385 
00386 
00389   virtual TQValueList<unsigned long> findNewMessages();
00392   virtual TQValueList<KMFolderCachedImap*> findNewFolders();
00393 
00395   virtual bool canRemoveFolder() const;
00396 
00398   virtual FolderJob* doCreateJob( KMMessage *msg, FolderJob::JobType jt, KMFolder *folder,
00399                                   TQString partSpecifier, const AttachmentStrategy *as ) const;
00400   virtual FolderJob* doCreateJob( TQPtrList<KMMessage>& msgList, const TQString& sets,
00401                                   FolderJob::JobType jt, KMFolder *folder ) const;
00402 
00403   virtual void timerEvent( TQTimerEvent* );
00404 
00405   /* update progress status */
00406   void newState( int progress, const TQString& syncStatus );
00407 
00409   KMFolderCachedImap* findParent( const TQString& path, const TQString& name );
00410 
00411 
00412 
00413 public slots:
00417   void slotSimpleData(KIO::Job * job, const TQByteArray & data);
00418 
00422   void slotTroubleshoot();
00423 
00428   void slotListResult( const TQStringList&, const TQStringList&,
00429       const TQStringList&, const TQStringList&, const ImapAccountBase::jobData& );
00430 
00435   void slotCheckNamespace( const TQStringList&, const TQStringList&,
00436       const TQStringList&, const TQStringList&, const ImapAccountBase::jobData& );
00437 
00438 private slots:
00439   void serverSyncInternal();
00440   void slotIncreaseProgress();
00441   void slotUpdateLastUid();
00442   void slotFolderDeletionOnServerFinished();
00443   void slotRescueDone( KMCommand* command );
00444   void slotRenameFolderFinished();
00445 
00446 signals:
00447   void folderComplete(KMFolderCachedImap *folder, bool success);
00448   void listComplete( KMFolderCachedImap* );
00449 
00453   void closeToQuotaChanged();
00454 
00455 private:
00456   void setReadOnly( bool readOnly );
00457   TQString state2String( int state ) const;
00458   void rememberDeletion( int );
00463   KMCommand* rescueUnsyncedMessages();
00465   void rescueUnsyncedMessagesAndDeleteFolder( KMFolder *folder, bool root = true );
00466 
00471   void disconnectSubFolderSignals();
00472 
00478   void syncNextSubFolder( bool secondSync );
00479 
00483   void buildSubFolderList();
00484 
00486   enum {
00487     SYNC_STATE_INITIAL,
00488     SYNC_STATE_TEST_ANNOTATIONS,
00489     SYNC_STATE_PUT_MESSAGES,
00490     SYNC_STATE_UPLOAD_FLAGS,
00491     SYNC_STATE_CREATE_SUBFOLDERS,
00492     SYNC_STATE_LIST_NAMESPACES,
00493     SYNC_STATE_LIST_SUBFOLDERS,
00494     SYNC_STATE_LIST_SUBFOLDERS2,
00495     SYNC_STATE_DELETE_SUBFOLDERS,
00496     SYNC_STATE_LIST_MESSAGES,
00497     SYNC_STATE_DELETE_MESSAGES,
00498     SYNC_STATE_EXPUNGE_MESSAGES,
00499     SYNC_STATE_GET_MESSAGES,
00500     SYNC_STATE_HANDLE_INBOX,
00501     SYNC_STATE_GET_USERRIGHTS,
00502     SYNC_STATE_GET_ANNOTATIONS,
00503     SYNC_STATE_SET_ANNOTATIONS,
00504     SYNC_STATE_GET_ACLS,
00505     SYNC_STATE_SET_ACLS,
00506     SYNC_STATE_GET_QUOTA,
00507     SYNC_STATE_FIND_SUBFOLDERS,
00508     SYNC_STATE_SYNC_SUBFOLDERS,
00509     SYNC_STATE_CHECK_UIDVALIDITY,
00510     SYNC_STATE_RENAME_FOLDER,
00511     SYNC_STATE_CLOSE,
00512     SYNC_STATE_GET_SUBFOLDER_QUOTA
00513   } mSyncState;
00514 
00515   int mProgress;
00516   int mStatusFlagsJobs;
00517 
00518   TQString mUidValidity;
00519   TQString     mImapPath;
00520   imapState   mContentState, mSubfolderState;
00521   TQStringList mSubfolderNames, mSubfolderPaths,
00522               mSubfolderMimeTypes, mSubfolderAttributes;
00523   TQString     mFolderAttributes;
00524   TQString     mAnnotationFolderType;
00525   IncidencesFor mIncidencesFor;
00526   bool mSharedSeenFlags;
00527 
00528   bool        mHasInbox;
00529   bool        mIsSelected;
00530   bool        mCheckFlags;
00531   bool        mReadOnly;
00532   mutable TQGuardedPtr<KMAcctCachedImap> mAccount;
00533 
00534   TQIntDict<int> uidsOnServer;
00535   TQValueList<ulong> uidsForDeletionOnServer;
00536   TQValueList<KMail::CachedImapJob::MsgForDownload> mMsgsForDownload;
00537   TQValueList<ulong> mUidsForDownload;
00538   TQStringList       foldersForDeletionOnServer;
00539 
00540   TQValueList< TQGuardedPtr<KMFolderCachedImap> > mSubfoldersForSync;
00541   KMFolderCachedImap* mCurrentSubfolder;
00542 
00546   TQMap<ulong,int> uidMap;
00547   bool uidMapDirty;
00548   void reloadUidMap();
00549   int uidWriteTimer;
00550 
00560   ulong mLastUid;
00565   ulong mTentativeHighestUid;
00566 
00570   bool mFoundAnIMAPDigest;
00571 
00572   int mUserRights, mOldUserRights;
00573   KMail::ACLJobs::ACLFetchState mUserRightsState;
00574   ACLList mACLList;
00575   KMail::ACLJobs::ACLFetchState mACLListState;
00576 
00577   bool mSilentUpload;
00578   bool mFolderRemoved;
00579   //bool mHoldSyncs;
00580   bool mRecurse;
00581   bool mQuotaOnly;
00582 
00584   bool mAnnotationFolderTypeChanged;
00586   bool mIncidencesForChanged;
00588   bool mSharedSeenFlagsChanged;
00589 
00596   std::set<ulong> mUIDsOfLocallyChangedStatuses;
00597 
00602   bool mStatusChangedLocally;
00603 
00604   TQStringList mNamespacesToList;
00605   int mNamespacesToCheck;
00606   bool mPersonalNamespacesCheckDone;
00607   TQString mImapPathCreation;
00608 
00609   QuotaInfo mQuotaInfo;
00610 
00613   bool mSomeSubFolderCloseToQuotaChanged;
00614 
00615   TQMap<ulong,void*> mDeletedUIDsSinceLastSync;
00616   bool mAlarmsBlocked;
00617 
00618   TQValueList<KMFolder*> mToBeDeletedAfterRescue;
00619   int mRescueCommandCount;
00620 
00621   TQValueList< TQGuardedPtr<KMFolderCachedImap> > mNewlyCreatedSubfolders;
00622   int mPermanentFlags;
00623 };
00624 
00625 #endif /*kmfoldercachedimap_h*/