kmail

imapaccountbase.h

00001 /* -*- c++ -*-
00002  * imapaccountbase.h
00003  *
00004  * Copyright (c) 2000-2002 Michael Haeckel <haeckel@kde.org>
00005  * Copyright (c) 2002 Marc Mutz <mutz@kde.org>
00006  *
00007  * This file is based on work on pop3 and imap account implementations
00008  * by Don Sanders <sanders@kde.org> and Michael Haeckel <haeckel@kde.org>
00009  *
00010  *  This program is free software; you can redistribute it and/or modify
00011  *  it under the terms of the GNU General Public License as published by
00012  *  the Free Software Foundation; version 2 of the License
00013  *
00014  *  This program is distributed in the hope that it will be useful,
00015  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00017  *  GNU General Public License for more details.
00018  *
00019  *  You should have received a copy of the GNU General Public License
00020  *  along with this program; if not, write to the Free Software
00021  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00022  */
00023 
00024 #ifndef __KMAIL_IMAPACCOUNTBASE_H__
00025 #define __KMAIL_IMAPACCOUNTBASE_H__
00026 
00027 #include <set>
00028 
00029 #include "networkaccount.h"
00030 
00031 #include <tqtimer.h>
00032 #include <tqguardedptr.h>
00033 #include <kio/global.h>
00034 
00035 class AccountManager;
00036 class KMFolder;
00037 class KConfig/*Base*/;
00038 class KMMessagePart;
00039 class DwBodyPart;
00040 class DwMessage;
00041 class FolderStorage;
00042 template <typename T> class TQValueVector;
00043 
00044 namespace KIO {
00045   class Job;
00046 }
00047 
00048 namespace KPIM {
00049   class ProgressItem;
00050 }
00051 
00052 namespace KMail {
00053   struct ACLListEntry;
00054   struct QuotaInfo;
00055   typedef TQValueVector<KMail::ACLListEntry> ACLList;
00056 
00057   class AttachmentStrategy;
00058 
00059   class ImapAccountBase : public KMail::NetworkAccount {
00060     Q_OBJECT
00061   protected:
00062     ImapAccountBase( AccountManager * parent, const TQString & name, uint id );
00063   public:
00064     virtual ~ImapAccountBase();
00065 
00067     virtual void init();
00068 
00070     virtual void pseudoAssign( const KMAccount * a );
00071 
00074     bool autoExpunge() const { return mAutoExpunge; }
00075     virtual void setAutoExpunge( bool expunge );
00076 
00078     bool hiddenFolders() const { return mHiddenFolders; }
00079     virtual void setHiddenFolders( bool show );
00080 
00082     bool onlySubscribedFolders() const { return mOnlySubscribedFolders; }
00083     virtual void setOnlySubscribedFolders( bool show );
00084 
00086     bool onlyLocallySubscribedFolders() const { return mOnlyLocallySubscribedFolders; }
00087     virtual void setOnlyLocallySubscribedFolders( bool show );
00088 
00089 
00091     bool loadOnDemand() const { return mLoadOnDemand; }
00092     virtual void setLoadOnDemand( bool load );
00093 
00095     bool listOnlyOpenFolders() const { return mListOnlyOpenFolders; }
00096     virtual void setListOnlyOpenFolders( bool only );
00097 
00099     virtual KIO::MetaData slaveConfig() const;
00100 
00101     virtual void readConfig( KConfig& config );
00102     virtual void writeConfig( KConfig& config );
00103 
00107     enum ConnectionState { Error = 0, Connected, Connecting };
00108 
00109     // possible list types
00110     enum ListType {
00111       List,
00112       ListSubscribed,
00113       ListSubscribedNoCheck,
00114       ListFolderOnly,
00115       ListFolderOnlySubscribed
00116     };
00117 
00124     ConnectionState makeConnection();
00125 
00126     // namespace defines
00127     enum imapNamespace { PersonalNS=0, OtherUsersNS=1, SharedNS=2 };
00128 
00129     // map a namespace type to a list of namespaces
00130     typedef TQMap<imapNamespace, TQStringList> nsMap;
00131 
00132     // map a namespace to a delimiter
00133     typedef TQMap<TQString, TQString> namespaceDelim;
00134 
00135     // map a namespace type to a map with the namespace and the delimiter
00136     typedef TQMap<imapNamespace, namespaceDelim> nsDelimMap;
00137 
00141     struct jobData
00142     {
00143       // Needed by TQMap, don't use
00144       jobData() : url(TQString::null), parent(0), current(0), total(1), done(0), offset(0), progressItem(0),
00145                   onlySubscribed(false), quiet(false), cancellable(false) {}
00146       // Real constructor
00147       jobData( const TQString& _url, KMFolder *_parent = 0,
00148           int _total = 1, int _done = 0, bool _quiet = false,
00149           bool _cancelable = false )
00150         : url(_url), parent(_parent), current(0), total(_total), done(_done), offset(0),
00151           progressItem(0), quiet(_quiet), cancellable(_cancelable) {}
00152 
00153       TQString path;
00154       TQString url;
00155       TQString curNamespace;
00156       TQByteArray data;
00157       TQCString cdata;
00158       TQStringList items;
00159       KMFolder *parent, *current;
00160       TQPtrList<KMMessage> msgList;
00161       int total, done, offset;
00162       KPIM::ProgressItem *progressItem;
00163       bool onlySubscribed, quiet, cancellable;
00164     };
00165 
00166     typedef TQMap<KIO::Job *, jobData>::Iterator JobIterator;
00170     void insertJob( KIO::Job* job, const jobData& data ) {
00171       mapJobData.insert( job, data );
00172     }
00176     JobIterator findJob( KIO::Job* job ) { return mapJobData.find( job ); }
00177     JobIterator jobsEnd() { return mapJobData.end(); }
00182     void removeJob( JobIterator& it );
00183 
00184     void removeJob( KIO::Job* job );
00185 
00193     void changeSubscription(bool subscribe, const TQString& imapPath, bool quiet = false );
00194 
00199     bool locallySubscribedTo( const TQString& imapPath );
00200 
00205     void changeLocalSubscription( const TQString& imapPath, bool subscribe );
00206 
00207 
00213     void getUserRights( KMFolder* folder, const TQString& imapPath );
00214 
00220     void getACL( KMFolder* folder, const TQString& imapPath );
00221 
00227     void getStorageQuotaInfo( KMFolder* folder, const TQString& imapPath );
00228 
00233     void setImapStatus( KMFolder* folder, const TQString& path, const TQCString& flags );
00234 
00239     void setImapSeenStatus( KMFolder* folder, const TQString& path, bool seen );
00240 
00244     void slaveDied() { mSlave = 0; killAllJobs(); }
00245 
00249     void killAllJobs( bool disconnectSlave=false ) = 0;
00250 
00254     virtual void cancelMailCheck();
00255 
00259     enum FolderListType { Single, Recursive };
00260     void processNewMailInFolder( KMFolder* folder, FolderListType type = Single );
00261 
00265     bool checkingSingleFolder() { return mCheckingSingleFolder; }
00266 
00271     void postProcessNewMail( bool setStatusMsg = true );
00272 
00277     bool checkingMail( KMFolder *folder );
00278 
00279     bool checkingMail() { return NetworkAccount::checkingMail(); }
00280 
00284     void handleBodyStructure( TQDataStream & stream, KMMessage * msg,
00285                               const AttachmentStrategy *as );
00286 
00290     virtual void setFolder(KMFolder*, bool addAccount = false);
00291 
00296     bool hasACLSupport() const { return mACLSupport; }
00297 
00302     bool hasAnnotationSupport() const { return mAnnotationSupport; }
00303 
00307     void setHasNoAnnotationSupport() { mAnnotationSupport = false; }
00308 
00313     bool hasQuotaSupport() const { return mQuotaSupport; }
00314 
00318     void setHasNoQuotaSupport() { mQuotaSupport = false; }
00319 
00324     bool handleJobError( KIO::Job* job, const TQString& context, bool abortSync = false );
00325 
00329     virtual FolderStorage* rootFolder() const = 0;
00330 
00334     KPIM::ProgressItem* listDirProgressItem();
00335 
00340     virtual unsigned int folderCount() const;
00341 
00345     nsMap namespaces() const { return mNamespaces; }
00346 
00350     virtual void setNamespaces( nsMap map )
00351     { mNamespaces = map; }
00352 
00357     nsDelimMap namespacesWithDelimiter();
00358 
00362      TQString namespaceForFolder( FolderStorage* );
00363 
00367      TQString addPathToNamespace( const TQString& ns );
00368 
00372      TQString delimiterForNamespace( const TQString& prefix );
00373 
00377      TQString delimiterForFolder( FolderStorage* );
00378 
00382      namespaceDelim namespaceToDelimiter() const
00383      { return mNamespaceToDelimiter; }
00384 
00388      void setNamespaceToDelimiter( namespaceDelim map )
00389      { mNamespaceToDelimiter = map; }
00390 
00394      bool isNamespaceFolder( TQString& name );
00395 
00399      bool hasCapability( const TQString& capa ) {
00400       return mCapabilities.contains( capa ); }
00401 
00407      TQString createImapPath( FolderStorage* parent, const TQString& folderName );
00408 
00412      TQString createImapPath( const TQString& parent, const TQString& folderName );
00413 
00414 
00415   public slots:
00420     void getNamespaces();
00421 
00422   private slots:
00427     void slotSubscriptionResult(KIO::Job * job);
00428 
00429   protected slots:
00430     virtual void slotCheckQueuedFolders();
00431 
00433     void slotSchedulerSlaveConnected(KIO::Slave *aSlave);
00435     void slotSchedulerSlaveError(KIO::Slave *aSlave, int, const TQString &errorMsg);
00436 
00440     void slotSetStatusResult(KIO::Job * job);
00441 
00443     void slotGetUserRightsResult( KIO::Job* _job );
00444 
00446     void slotGetACLResult( KIO::Job* _job );
00447 
00449     void slotGetStorageQuotaInfoResult( KIO::Job* _job );
00450 
00454     void slotNoopTimeout();
00458     void slotIdleTimeout();
00459 
00463     void slotAbortRequested( KPIM::ProgressItem* );
00464 
00468     void slotSimpleResult(KIO::Job * job);
00469 
00471     void slotNamespaceResult( KIO::Job*, const TQString& str );
00472 
00476     void slotSaveNamespaces( const ImapAccountBase::nsDelimMap& map );
00477 
00481     void slotCapabilitiesResult( KIO::Job*, const TQString& result );
00482 
00483   protected:
00484 
00501     virtual bool handleError( int error, const TQString &errorMsg, KIO::Job* job, const TQString& context, bool abortSync = false );
00502 
00504     bool handlePutError( KIO::Job* job, jobData& jd, KMFolder* folder );
00505 
00506     virtual TQString protocol() const;
00507     virtual unsigned short int defaultPort() const;
00508 
00512     void constructParts( TQDataStream & stream, int count, KMMessagePart* parentKMPart,
00513        DwBodyPart * parent, const DwMessage * dwmsg );
00514 
00516     void migratePrefix();
00517 
00518     // used for writing the blacklist out to the config file
00519     TQStringList locallyBlacklistedFolders() const;
00520     void localBlacklistFromStringList( const TQStringList & );
00521     TQString prettifyQuotaError( const TQString& _error, KIO::Job * job );
00522 
00523   protected:
00524     TQPtrList<TQGuardedPtr<KMFolder> > mOpenFolders;
00525     TQStringList mSubfolderNames, mSubfolderPaths,
00526         mSubfolderMimeTypes, mSubfolderAttributes;
00527     TQMap<KIO::Job *, jobData> mapJobData;
00529     TQTimer mIdleTimer;
00531     TQTimer mNoopTimer;
00532     int mTotal, mCountUnread, mCountLastUnread;
00533     TQMap<TQString, int> mUnreadBeforeCheck;
00534     bool mAutoExpunge : 1;
00535     bool mHiddenFolders : 1;
00536     bool mOnlySubscribedFolders : 1;
00537     bool mOnlyLocallySubscribedFolders : 1;
00538     bool mLoadOnDemand : 1;
00539     bool mListOnlyOpenFolders : 1;
00540     bool mProgressEnabled : 1;
00541 
00542     bool mErrorDialogIsActive : 1;
00543     bool mPasswordDialogIsActive : 1;
00544     bool mACLSupport : 1;
00545     bool mAnnotationSupport : 1;
00546     bool mQuotaSupport : 1;
00547     bool mSlaveConnected : 1;
00548     bool mSlaveConnectionError : 1;
00549     bool mCheckingSingleFolder : 1;
00550 
00551     // folders that should be checked for new mails
00552     TQValueList<TQGuardedPtr<KMFolder> > mMailCheckFolders;
00553     // folders that should be checked after the current check is done
00554     TQValueList<TQGuardedPtr<KMFolder> > mFoldersQueuedForChecking;
00555     // holds messageparts from the bodystructure
00556     TQPtrList<KMMessagePart> mBodyPartList;
00557     // the current message for the bodystructure
00558     KMMessage* mCurrentMsg;
00559 
00560     TQGuardedPtr<KPIM::ProgressItem> mListDirProgressItem;
00561 
00562     // our namespaces in the form section=namespaceList
00563     nsMap mNamespaces;
00564 
00565     // namespace - delimiter map
00566     namespaceDelim mNamespaceToDelimiter;
00567 
00568     // old prefix for migration
00569     TQString mOldPrefix;
00570 
00571     // capabilities
00572     TQStringList mCapabilities;
00573 
00574     std::set<TQString> mLocalSubscriptionBlackList;
00575 
00576   signals:
00583     void connectionResult( int errorCode, const TQString& errorMsg );
00584 
00589     void subscriptionChanged(const TQString& imapPath, bool subscribed);
00590 
00595     void subscriptionChangeFailed( const TQString &errorMessage );
00596 
00602     void imapStatusChanged( KMFolder*, const TQString& imapPath, bool cont );
00603 
00610     void receivedUserRights( KMFolder* folder );
00611 
00619     void receivedACL( KMFolder* folder, KIO::Job* job, const KMail::ACLList& entries );
00620 
00629     void receivedStorageQuotaInfo( KMFolder* folder, KIO::Job* job, const KMail::QuotaInfo& entries );
00630 
00634     void namespacesFetched( const ImapAccountBase::nsDelimMap& );
00635 
00639     void namespacesFetched();
00640   };
00641 
00642 
00643 } // namespace KMail
00644 
00645 #endif // __KMAIL_IMAPACCOUNTBASE_H__