summaryrefslogtreecommitdiffstats
path: root/kmail/kmfolderimap.h
blob: da026728cb5fe2dbff6b497e2b08931df04d04f0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
/*
 * kmfolderimap.h
 *
 * Copyright (c) 2001 Kurt Granroth <granroth@kde.org>
 * Copyright (c) 2000-2002 Michael Haeckel <haeckel@kde.org>
 *
 * This file is based on kmacctimap.h by Michael Haeckel which is
 * based on popaccount.h by Don Sanders
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; version 2 of the License
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef kmfolderimap_h
#define kmfolderimap_h

#include "kmacctimap.h"
#include "kmfoldermbox.h"
#include "kmmsgbase.h"

#include "kio/job.h"
#include "kio/global.h"

#include <kstandarddirs.h>

#include <tqintdict.h>
#include <tqdict.h>
template< typename T> class QPtrList;
template< typename T> class QValueList;

class KMFolderTreeItem;
class KMFolderImap;
class KMSearchPattern;
class KMMessage;
namespace KMail {
  class FolderJob;
  class ImapJob;
  class AttachmentStrategy;
  class ImapAccountBase;
}
namespace KPIM {
  class ProgressItem;
}
using KMail::FolderJob;
using KMail::ImapJob;
using KMail::AttachmentStrategy;
using KMail::ImapAccountBase;
using KPIM::ProgressItem;

class KMMsgMetaData
{
public:
  KMMsgMetaData(KMMsgStatus aStatus)
    :mStatus(aStatus), mSerNum(0) {}
  KMMsgMetaData(KMMsgStatus aStatus, Q_UINT32 aSerNum)
    :mStatus(aStatus), mSerNum(aSerNum) {}
  ~KMMsgMetaData() {};
  const KMMsgStatus status() const { return mStatus; }
  const Q_UINT32 serNum() const { return mSerNum; }
private:
  KMMsgStatus mStatus;
  Q_UINT32 mSerNum;
};



class KMFolderImap : public KMFolderMbox
{
  Q_OBJECT
  friend class ::KMail::ImapJob;
public:

  static TQString cacheLocation() {
     return locateLocal("data", "kmail/imap" );
  }

  enum imapState {
    imapNoInformation = 0,
    imapListingInProgress = 1,
    imapDownloadInProgress = 2,
    imapFinished = 3
  };

  virtual imapState getContentState() { return mContentState; }
  virtual void setContentState(imapState state) { mContentState = state; }

  virtual imapState getSubfolderState() { return mSubfolderState; }
  virtual void setSubfolderState(imapState state);

  /** Usually a parent is given. But in some cases there is no
    fitting parent object available. Then the name of the folder
    is used as the absolute path to the folder file. */
  KMFolderImap(KMFolder* folder, const char* name=0);
  virtual ~KMFolderImap();

  /** Returns the type of this folder */
  virtual KMFolderType folderType() const { return KMFolderTypeImap; }

  virtual KMMessage* getMsg(int idx);
  /** The path to the imap folder on the server */
  void setImapPath( const TQString &path );
  TQString imapPath() const { return mImapPath; }

  /** The highest UID in the folder */
  ulong lastUid();

  /** The uidvalidity of the last update */
  void setUidValidity(const TQString &validity) { mUidValidity = validity; }
  TQString uidValidity() { return mUidValidity; }

  /** The imap account associated with this folder */
  void setAccount(KMAcctImap *acct);
  KMAcctImap* account() const;

  /** Remove (first occurrence of) given message from the folder. */
  virtual void removeMsg(int i, bool quiet = FALSE);
  virtual void removeMsg(const TQPtrList<KMMessage>& msgList, bool quiet = FALSE);

  virtual int rename( const TQString& newName, KMFolderDir *aParent = 0 );

  /** Remove the IMAP folder on the server and if successful also locally */
  virtual void remove();

  /** Automatically expunge deleted messages when leaving the folder */
  bool autoExpunge();

  /** Write the config file */
  virtual void writeConfig();

  /** Read the config file */
  virtual void readConfig();

  /**
   * List a directory and add the contents to kmfoldermgr
   * It uses a ListJob to get the folders
   * returns false if the connection failed
   */
  virtual bool listDirectory();

  /**
   * Retrieve all mails in a folder
   */
  void getFolder(bool force = FALSE);

  /**
   * same as above but also checks for new mails
   */
  void getAndCheckFolder(bool force = FALSE);

  /**
   * Get the whole message
   */
  void getMessage(KMFolder * folder, KMMessage * msg);

  /**
   * Create a new subfolder
   * You may specify the root imap path or this folder will be used
   * If you set askUser to false and the server can only handle folders
   * that contain messages _or_ folders the new folder is set to "contains messages"
   * by default
   */
  void createFolder(const TQString &name,
      const TQString& imapPath = TQString::null, bool askUser = true);

  /**
   * Delete a message
   */
  void deleteMessage(KMMessage * msg);
  void deleteMessage(const TQPtrList<KMMessage>& msgList);

  /**
   * Change the status of the message indicated by @p index
   * Overloaded function for the following one
   */
  virtual void setStatus(int idx, KMMsgStatus status, bool toggle);

  /**
   * Change the status of several messages indicated by @p ids
   */
  virtual void setStatus(TQValueList<int>& _ids, KMMsgStatus status, bool toggle);

  /** generates sets of uids */
  static TQStringList makeSets( TQValueList<ulong>&, bool sort = true);
  static TQStringList makeSets(const TQStringList&, bool sort = true);

  /** splits the message list according to sets. Modifies the @msgList. */
  static TQPtrList<KMMessage> splitMessageList(const TQString& set,
                                              TQPtrList<KMMessage>& msgList);

  /** gets the uids of the given ids */
  void getUids(TQValueList<int>& ids, TQValueList<ulong>& uids);

  /** same as above but accepts a Message-List */
  void getUids(const TQPtrList<KMMessage>& msgList, TQValueList<ulong>& uids);

  /**
   * Expunge deleted messages from the folder
   */
  void expungeFolder(KMFolderImap * aFolder, bool quiet);

  virtual int compact( bool ) { expungeFolder(this, false); return 0; };

  /**
   * Emit the folderComplete signal
   */
  void sendFolderComplete(bool success)
  { emit folderComplete(this, success); }

  /**
   * Refresh the number of unseen mails
   * Returns false in an error condition
   */
  bool processNewMail(bool interactive);

  /**
   * Tell the folder, this it is selected and shall also display new mails,
   * not only their number, when checking for mail.
   */
  void setSelected(bool selected) { mIsSelected = selected; }
  bool isSelected() { return mIsSelected; }

  /**
   * Encode the given string in a filename save 7 bit string
   */
  static TQString encodeFileName(const TQString &);
  static TQString decodeFileName(const TQString &);
  static TQTextCodec * utf7Codec();

  /**
   * Convert message status to a list of IMAP flags
   */
  static TQString statusToFlags(KMMsgStatus status, int supportedFalgs);

  /**
   * Return the filename of the folder (reimplemented from KFolder)
   */
  virtual TQString fileName() const {
    return encodeFileName( KMFolderMbox::fileName() ); }

  /**
   * Get the serial number for the given UID (if available)
   */
   const ulong serNumForUID( ulong uid );

  /**
   * Save the metadata for the UID
   * If the UID is not supplied the one from the message is taken
   */
  void saveMsgMetaData( KMMessage* msg, ulong uid = 0 );

  /**
   * Splits a uid-set into single uids
   */
  static TQValueList<ulong> splitSets(const TQString);

  virtual void ignoreJobsForMessage( KMMessage* );

  /**
   * If this folder should be included in new-mail-check
   */
  bool includeInMailCheck() { return mCheckMail; }
  void setIncludeInMailCheck( bool check );

  /** Inherited */
  virtual int create();

  /** imap folders cannot expire */
  virtual bool isAutoExpire() const { return false; }

  /** Closes and cancels all pending jobs. */
  virtual void reallyDoClose(const char* owner);

  void setCheckingValidity( bool val ) { mCheckingValidity = val; }

  /** Return the trash folder. */
  KMFolder* trashFolder() const;

  /**
   * Mark the folder as already removed from the server
   * If set to true the folder will only be deleted locally
   * This will recursively be applied to all children
   */
  void setAlreadyRemoved(bool removed);

  /// Is the folder readonly?
  bool isReadOnly() const { return KMFolderMbox::isReadOnly() || mReadOnly; }

  /**
   * The user's rights on this folder - see bitfield in ACLJobs namespace.
   * @return 0 when not known yet
   */
  unsigned int userRights() const { return mUserRights; }

  /** Set the user's rights on this folder - called by getUserRights */
  void setUserRights( unsigned int userRights );

  /**
    * Search for messages
    * The actual search is done in slotSearch and the end
    * is signaled with searchDone()
    */
  virtual void search( const KMSearchPattern* );
  virtual void search( const KMSearchPattern*, Q_UINT32 serNum );

  /** Returns true if this folder can be moved */
  virtual bool isMoveable() const;

  /** Initialize this storage from another one. Used when creating a child folder */
  void initializeFrom( KMFolderImap* parent, TQString path, TQString mimeType );

  /** Returns the IMAP flags that can be stored on the server. */
  int permanentFlags() const { return mPermanentFlags; }

signals:
  void folderComplete(KMFolderImap *folder, bool success);

  /**
   * Emitted, when the account is deleted
   */
  void deleted(KMFolderImap*);

  /**
   * Emitted at the end of the directory listing
   */
  void directoryListingFinished(KMFolderImap*);

  /**
   * Emitted when a folder creation has finished.
   * @param name The name of the folder that should have been created.
   * @param success True if the folder was created, false otherwise.
   */
  void folderCreationResult( const TQString &name, bool success );

public slots:
  /** Add a message to a folder after is has been added on an IMAP server */
  virtual void addMsgQuiet(KMMessage *);
  virtual void addMsgQuiet(TQPtrList<KMMessage>);

  /** Add the given message to the folder. Usually the message
    is added at the end of the folder. Returns zero on success and
    an errno error code on failure. The index of the new message
    is stored in index_return if given.
    Please note that the message is added as is to the folder and the folder
    takes ownership of the message (deleting it in the destructor).*/
  virtual int addMsg(KMMessage* msg, int* index_return = 0);
  virtual int addMsg(TQPtrList<KMMessage>&, TQValueList<int>& index_return);

  /** Copy the messages to this folder */
  void copyMsg(TQPtrList<KMMessage>& msgList/*, KMFolder* parent*/);


  /** Detach message from this folder. Usable to call addMsg() afterwards.
    Loads the message if it is not loaded up to now. */
  virtual KMMessage* take(int idx);
  virtual void take(TQPtrList<KMMessage>);

  /**
   * Add the data a KIO::Job retrieves to the buffer
   */
  void slotSimpleData(KIO::Job * job, const TQByteArray & data);

  /**
   * Convert IMAP flags to a message status
   * @param newMsg specifies whether unseen messages are new or unread
   */
  static void flagsToStatus(KMMsgBase *msg, int flags, bool newMsg = TRUE, int supportedFalgs = 31 );

  /**
   * Convert IMAP seen flag to a message status.
   * @param newMsg specifies whether unseen messages are new or unread
   */
  static void seenFlagToStatus( KMMsgBase *msg, int flags, bool newMsg = true );

  /**
   * Connected to the result signal of the copy/move job
   */
  void slotCopyMsgResult( KMail::FolderJob* job );

  /**
   * Called from the SearchJob when the folder is done or messages where found
   */
  void slotSearchDone( TQValueList<Q_UINT32> serNums,
                       const KMSearchPattern* pattern,
                       bool complete );

  /**
   * Called from the SearchJob when the message was searched
   */
  void slotSearchDone( Q_UINT32 serNum, const KMSearchPattern* pattern, bool matches );

  /**
   * Connected to ListJob::receivedFolders
   * creates/removes folders
   */
  void slotListResult( const TQStringList&, const TQStringList&,
      const TQStringList&, const TQStringList&, const ImapAccountBase::jobData& );

  /**
   * Connected to slotListNamespaces
   * creates/removes namespace folders
   */
  void slotCheckNamespace( const TQStringList&, const TQStringList&,
      const TQStringList&, const TQStringList&, const ImapAccountBase::jobData& );

protected:
  virtual FolderJob* doCreateJob( KMMessage *msg, FolderJob::JobType jt,
                                  KMFolder *folder, TQString partSpecifier,
                                  const AttachmentStrategy *as ) const;
  virtual FolderJob* doCreateJob( TQPtrList<KMMessage>& msgList, const TQString& sets,
                                  FolderJob::JobType jt, KMFolder *folder ) const;

  void getMessagesResult(KIO::Job * job, bool lastSet);

  /** Called by KMFolder::expunge() to delete the actual contents.
    At the time of the call the folder has already been closed, and
    the various index files deleted.  Returns 0 on success. */
  virtual int expungeContents();

  void setChildrenState( TQString attributes );

  /** Create or find the INBOX and initialize it */
  void initInbox();

  /** See if there is a better parent then this folder */
  KMFolderImap* findParent( const TQString& path, const TQString& name );

  /** See if all folders are still present on server, otherwise delete them */
  void checkFolders( const TQStringList& folderNames, const TQString& ns );

  void finishMailCheck( const char *func, imapState state );

protected slots:

  /**
   * Retrieve the whole folder or only the changes
   */
  void checkValidity();
  void slotCheckValidityResult(KIO::Job * job);

  /**
   * Get the folder now (internal)
   */
  void reallyGetFolder(const TQString &startUid = TQString::null);

  /**
   * For listing the contents of a folder
   */
  void slotListFolderResult(KIO::Job * job);
  void slotListFolderEntries(KIO::Job * job, const KIO::UDSEntryList & uds);

  /**
   * For retrieving a message digest
   */
  void slotGetMessagesResult(KIO::Job * job);
  void slotGetLastMessagesResult(KIO::Job * job);
  void slotGetMessagesData(KIO::Job * job, const TQByteArray & data);

  /**
   * For creating a new subfolder
   */
  void slotCreateFolderResult(KIO::Job * job);

  /**
   * Remove the folder also locally, if removing on the server succeeded
   */
  void slotRemoveFolderResult(KIO::Job *job);

  /**
   * Update the number of unseen messages
   */
  void slotStatResult(KIO::Job *job);

  /**
   * notify the progress item that the mail check for this folder is
   * done.
   */
  void slotCompleteMailCheckProgress();

  /**
   * Is called when the slave is connected and triggers a newmail check
   */
  void slotProcessNewMail( int errorCode, const TQString& errorMsg );

  /**
   * Is connected when there are folders to be created on startup and the
   * account is still connecting. Once the account emits the connected
   * signal this slot is called and the folders created.
   */
  void slotCreatePendingFolders( int errorCode, const TQString& errorMsg );

  /**
   * Starts a namespace listing
   */
  void slotListNamespaces();

protected:
  TQString     mImapPath;
  ulong       mLastUid;
  imapState   mContentState, mSubfolderState;
  bool        mIsSelected;
  bool        mCheckFlags;
  bool        mReadOnly;
  bool        mCheckMail;
  mutable TQGuardedPtr<KMAcctImap> mAccount;
  // the current uidvalidity
  TQString mUidValidity;
  unsigned int mUserRights;

private:
  // if we're checking validity currently
  bool        mCheckingValidity;
  // uid - metadata cache
  TQIntDict<KMMsgMetaData> mUidMetaDataMap;
  // msgidMD5 - status map
  TQDict<KMMsgMetaData> mMetaDataMap;
  // if the folder should be deleted without server roundtrip
  bool        mAlreadyRemoved;
  // the progress for mailchecks
  TQGuardedPtr<ProgressItem> mMailCheckProgressItem;
  // the progress for listings
  ProgressItem *mListDirProgressItem;
  // the progress for addMsg
  ProgressItem *mAddMessageProgressItem;
  // to-be-added folders
  TQStringList mFoldersPendingCreation;

  // push all flags to the server instead of just the changed once
  // when doing a flag change the next time
  // this is needed for migrating local flags from the time where we didn't
  // have the ability to store them on the server
  bool mUploadAllFlags;

  // PERMANENTFLAGS part of SELECT response, needed to determine if custom flags can be
  // stored on the server
  int mPermanentFlags;
};

#endif // kmfolderimap_h