kmail

imapaccountbase.h
1 /* -*- c++ -*-
2  * imapaccountbase.h
3  *
4  * Copyright (c) 2000-2002 Michael Haeckel <haeckel@kde.org>
5  * Copyright (c) 2002 Marc Mutz <mutz@kde.org>
6  *
7  * This file is based on work on pop3 and imap account implementations
8  * by Don Sanders <sanders@kde.org> and Michael Haeckel <haeckel@kde.org>
9  *
10  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; version 2 of the License
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22  */
23 
24 #ifndef __KMAIL_IMAPACCOUNTBASE_H__
25 #define __KMAIL_IMAPACCOUNTBASE_H__
26 
27 #include <set>
28 
29 #include "networkaccount.h"
30 
31 #include <tqtimer.h>
32 #include <tqguardedptr.h>
33 #include <kio/global.h>
34 
35 class AccountManager;
36 class KMFolder;
37 class KConfig/*Base*/;
38 class KMMessagePart;
39 class DwBodyPart;
40 class DwMessage;
41 class FolderStorage;
42 template <typename T> class TQValueVector;
43 
44 namespace KIO {
45  class Job;
46 }
47 
48 namespace KPIM {
49  class ProgressItem;
50 }
51 
52 namespace KMail {
53  struct ACLListEntry;
54  struct QuotaInfo;
55  typedef TQValueVector<KMail::ACLListEntry> ACLList;
56 
57  class AttachmentStrategy;
58 
59  class ImapAccountBase : public KMail::NetworkAccount {
60  Q_OBJECT
61  TQ_OBJECT
62  protected:
63  ImapAccountBase( AccountManager * parent, const TQString & name, uint id );
64  public:
65  virtual ~ImapAccountBase();
66 
68  virtual void init();
69 
71  virtual void pseudoAssign( const KMAccount * a );
72 
75  bool autoExpunge() const { return mAutoExpunge; }
76  virtual void setAutoExpunge( bool expunge );
77 
79  bool hiddenFolders() const { return mHiddenFolders; }
80  virtual void setHiddenFolders( bool show );
81 
83  bool onlySubscribedFolders() const { return mOnlySubscribedFolders; }
84  virtual void setOnlySubscribedFolders( bool show );
85 
87  bool onlyLocallySubscribedFolders() const { return mOnlyLocallySubscribedFolders; }
88  virtual void setOnlyLocallySubscribedFolders( bool show );
89 
90 
92  bool loadOnDemand() const { return mLoadOnDemand; }
93  virtual void setLoadOnDemand( bool load );
94 
96  bool listOnlyOpenFolders() const { return mListOnlyOpenFolders; }
97  virtual void setListOnlyOpenFolders( bool only );
98 
100  virtual KIO::MetaData slaveConfig() const;
101 
102  virtual void readConfig( KConfig& config );
103  virtual void writeConfig( KConfig& config );
104 
108  enum ConnectionState { Error = 0, Connected, Connecting };
109 
110  // possible list types
111  enum ListType {
112  List,
113  ListSubscribed,
114  ListSubscribedNoCheck,
115  ListFolderOnly,
116  ListFolderOnlySubscribed
117  };
118 
125  ConnectionState makeConnection();
126 
127  // namespace defines
128  enum imapNamespace { PersonalNS=0, OtherUsersNS=1, SharedNS=2 };
129 
130  // map a namespace type to a list of namespaces
131  typedef TQMap<imapNamespace, TQStringList> nsMap;
132 
133  // map a namespace to a delimiter
134  typedef TQMap<TQString, TQString> namespaceDelim;
135 
136  // map a namespace type to a map with the namespace and the delimiter
137  typedef TQMap<imapNamespace, namespaceDelim> nsDelimMap;
138 
142  struct jobData
143  {
144  // Needed by TQMap, don't use
145  jobData() : url(TQString()), parent(0), current(0), total(1), done(0), offset(0), progressItem(0),
146  onlySubscribed(false), quiet(false), cancellable(false) {}
147  // Real constructor
148  jobData( const TQString& _url, KMFolder *_parent = 0,
149  int _total = 1, int _done = 0, bool _quiet = false,
150  bool _cancelable = false )
151  : url(_url), parent(_parent), current(0), total(_total), done(_done), offset(0),
152  progressItem(0), quiet(_quiet), cancellable(_cancelable) {}
153 
154  TQString path;
155  TQString url;
156  TQString curNamespace;
157  TQByteArray data;
158  TQCString cdata;
159  TQStringList items;
160  KMFolder *parent, *current;
161  TQPtrList<KMMessage> msgList;
162  int total, done, offset;
163  KPIM::ProgressItem *progressItem;
164  bool onlySubscribed, quiet, cancellable;
165  };
166 
167  typedef TQMap<KIO::Job *, jobData>::Iterator JobIterator;
171  void insertJob( KIO::Job* job, const jobData& data ) {
172  mapJobData.insert( job, data );
173  }
177  JobIterator findJob( KIO::Job* job ) { return mapJobData.find( job ); }
178  JobIterator jobsEnd() { return mapJobData.end(); }
183  void removeJob( JobIterator& it );
184 
185  void removeJob( KIO::Job* job );
186 
194  void changeSubscription(bool subscribe, const TQString& imapPath, bool quiet = false );
195 
200  bool locallySubscribedTo( const TQString& imapPath );
201 
206  void changeLocalSubscription( const TQString& imapPath, bool subscribe );
207 
208 
214  void getUserRights( KMFolder* folder, const TQString& imapPath );
215 
221  void getACL( KMFolder* folder, const TQString& imapPath );
222 
228  void getStorageQuotaInfo( KMFolder* folder, const TQString& imapPath );
229 
234  void setImapStatus( KMFolder* folder, const TQString& path, const TQCString& flags );
235 
240  void setImapSeenStatus( KMFolder* folder, const TQString& path, bool seen );
241 
245  void slaveDied() { mSlave = 0; killAllJobs(); }
246 
250  void killAllJobs( bool disconnectSlave=false ) = 0;
251 
255  virtual void cancelMailCheck();
256 
260  enum FolderListType { Single, Recursive };
261  void processNewMailInFolder( KMFolder* folder, FolderListType type = Single );
262 
266  bool checkingSingleFolder() { return mCheckingSingleFolder; }
267 
272  void postProcessNewMail( bool setStatusMsg = true );
273 
278  bool checkingMail( KMFolder *folder );
279 
280  bool checkingMail() { return NetworkAccount::checkingMail(); }
281 
285  void handleBodyStructure( TQDataStream & stream, KMMessage * msg,
286  const AttachmentStrategy *as );
287 
291  virtual void setFolder(KMFolder*, bool addAccount = false);
292 
297  bool hasACLSupport() const { return mACLSupport; }
298 
303  bool hasAnnotationSupport() const { return mAnnotationSupport; }
304 
308  void setHasNoAnnotationSupport() { mAnnotationSupport = false; }
309 
314  bool hasQuotaSupport() const { return mQuotaSupport; }
315 
319  void setHasNoQuotaSupport() { mQuotaSupport = false; }
320 
325  bool handleJobError( KIO::Job* job, const TQString& context, bool abortSync = false );
326 
330  virtual FolderStorage* rootFolder() const = 0;
331 
335  KPIM::ProgressItem* listDirProgressItem();
336 
341  virtual unsigned int folderCount() const;
342 
346  nsMap namespaces() const { return mNamespaces; }
347 
351  virtual void setNamespaces( nsMap map )
352  { mNamespaces = map; }
353 
358  nsDelimMap namespacesWithDelimiter();
359 
363  TQString namespaceForFolder( FolderStorage* );
364 
368  TQString addPathToNamespace( const TQString& ns );
369 
373  TQString delimiterForNamespace( const TQString& prefix );
374 
378  TQString delimiterForFolder( FolderStorage* );
379 
383  namespaceDelim namespaceToDelimiter() const
384  { return mNamespaceToDelimiter; }
385 
389  void setNamespaceToDelimiter( namespaceDelim map )
390  { mNamespaceToDelimiter = map; }
391 
395  bool isNamespaceFolder( TQString& name );
396 
400  bool hasCapability( const TQString& capa ) {
401  return mCapabilities.contains( capa ); }
402 
408  TQString createImapPath( FolderStorage* parent, const TQString& folderName );
409 
413  TQString createImapPath( const TQString& parent, const TQString& folderName );
414 
415 
416  public slots:
421  void getNamespaces();
422 
423  private slots:
428  void slotSubscriptionResult(KIO::Job * job);
429 
430  protected slots:
431  virtual void slotCheckQueuedFolders();
432 
434  void slotSchedulerSlaveConnected(KIO::Slave *aSlave);
436  void slotSchedulerSlaveError(KIO::Slave *aSlave, int, const TQString &errorMsg);
437 
441  void slotSetStatusResult(KIO::Job * job);
442 
444  void slotGetUserRightsResult( KIO::Job* _job );
445 
447  void slotGetACLResult( KIO::Job* _job );
448 
450  void slotGetStorageQuotaInfoResult( KIO::Job* _job );
451 
455  void slotNoopTimeout();
459  void slotIdleTimeout();
460 
464  void slotAbortRequested( KPIM::ProgressItem* );
465 
469  void slotSimpleResult(KIO::Job * job);
470 
472  void slotNamespaceResult( KIO::Job*, const TQString& str );
473 
477  void slotSaveNamespaces( const ImapAccountBase::nsDelimMap& map );
478 
482  void slotCapabilitiesResult( KIO::Job*, const TQString& result );
483 
484  protected:
485 
502  virtual bool handleError( int error, const TQString &errorMsg, KIO::Job* job, const TQString& context, bool abortSync = false );
503 
505  bool handlePutError( KIO::Job* job, jobData& jd, KMFolder* folder );
506 
507  virtual TQString protocol() const;
508  virtual unsigned short int defaultPort() const;
509 
513  void constructParts( TQDataStream & stream, int count, KMMessagePart* parentKMPart,
514  DwBodyPart * parent, const DwMessage * dwmsg );
515 
517  void migratePrefix();
518 
519  // used for writing the blacklist out to the config file
520  TQStringList locallyBlacklistedFolders() const;
521  void localBlacklistFromStringList( const TQStringList & );
522  TQString prettifyQuotaError( const TQString& _error, KIO::Job * job );
523 
524  protected:
525  TQPtrList<TQGuardedPtr<KMFolder> > mOpenFolders;
526  TQStringList mSubfolderNames, mSubfolderPaths,
527  mSubfolderMimeTypes, mSubfolderAttributes;
528  TQMap<KIO::Job *, jobData> mapJobData;
530  TQTimer mIdleTimer;
532  TQTimer mNoopTimer;
533  int mTotal, mCountUnread, mCountLastUnread;
534  TQMap<TQString, int> mUnreadBeforeCheck;
535  bool mAutoExpunge : 1;
536  bool mHiddenFolders : 1;
537  bool mOnlySubscribedFolders : 1;
538  bool mOnlyLocallySubscribedFolders : 1;
539  bool mLoadOnDemand : 1;
540  bool mListOnlyOpenFolders : 1;
541  bool mProgressEnabled : 1;
542 
543  bool mErrorDialogIsActive : 1;
544  bool mPasswordDialogIsActive : 1;
545  bool mACLSupport : 1;
546  bool mAnnotationSupport : 1;
547  bool mQuotaSupport : 1;
548  bool mSlaveConnected : 1;
549  bool mSlaveConnectionError : 1;
550  bool mCheckingSingleFolder : 1;
551 
552  // folders that should be checked for new mails
553  TQValueList<TQGuardedPtr<KMFolder> > mMailCheckFolders;
554  // folders that should be checked after the current check is done
555  TQValueList<TQGuardedPtr<KMFolder> > mFoldersQueuedForChecking;
556  // holds messageparts from the bodystructure
557  TQPtrList<KMMessagePart> mBodyPartList;
558  // the current message for the bodystructure
559  KMMessage* mCurrentMsg;
560 
561  TQGuardedPtr<KPIM::ProgressItem> mListDirProgressItem;
562 
563  // our namespaces in the form section=namespaceList
564  nsMap mNamespaces;
565 
566  // namespace - delimiter map
567  namespaceDelim mNamespaceToDelimiter;
568 
569  // old prefix for migration
570  TQString mOldPrefix;
571 
572  // capabilities
573  TQStringList mCapabilities;
574 
575  std::set<TQString> mLocalSubscriptionBlackList;
576 
577  signals:
584  void connectionResult( int errorCode, const TQString& errorMsg );
585 
590  void subscriptionChanged(const TQString& imapPath, bool subscribed);
591 
596  void subscriptionChangeFailed( const TQString &errorMessage );
597 
603  void imapStatusChanged( KMFolder*, const TQString& imapPath, bool cont );
604 
611  void receivedUserRights( KMFolder* folder );
612 
620  void receivedACL( KMFolder* folder, KIO::Job* job, const KMail::ACLList& entries );
621 
630  void receivedStorageQuotaInfo( KMFolder* folder, KIO::Job* job, const KMail::QuotaInfo& entries );
631 
635  void namespacesFetched( const ImapAccountBase::nsDelimMap& );
636 
640  void namespacesFetched();
641  };
642 
643 
644 } // namespace KMail
645 
646 #endif // __KMAIL_IMAPACCOUNTBASE_H__