summaryrefslogtreecommitdiffstats
path: root/kmail/popaccount.h
blob: 718ac3165508cfdf847f3145c6c878804610e2c4 (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
// -*- c++ -*-
#ifndef KMAcctExpPop_h
#define KMAcctExpPop_h

#include "networkaccount.h"

#include <qvaluelist.h>
#include <qstringlist.h>
#include <qvaluevector.h>
#include <qtimer.h>
#include <qdict.h>

class KMPopHeaders;
class KMMessage;
class QDataStream;
namespace KIO {
  class MetaData;
  class Slave;
  class SimpleJob;
  class Job;
}

/** The namespace where all classes of KMail can be found in. */
namespace KMail {
/**
 * KMail account for pop mail account
 */
class PopAccount: public NetworkAccount {
  Q_OBJECT

public:
  virtual ~PopAccount();
  virtual void init(void);

  virtual KIO::MetaData slaveConfig() const;

  /** A weak assignment operator */
  virtual void pseudoAssign( const KMAccount * a );

  virtual QString protocol() const;
  virtual unsigned short int defaultPort() const;

  /**
   * Sending of several commands at once
   */
  bool usePipelining(void) const { return mUsePipelining; }
  virtual void setUsePipelining(bool);
 
  /**
   * Shall messages be left on the server upon retreival (TRUE)
   * or deleted (FALSE).
   */
  bool leaveOnServer(void) const { return mLeaveOnServer; }
  virtual void setLeaveOnServer(bool);

  /**
   * If value is positive, leave mail on the server for so many days.
   */
  int leaveOnServerDays(void) const { return mLeaveOnServerDays; }
  virtual void setLeaveOnServerDays(int);

  /**
   * If value is positive, leave so many messages on the server.
   */
  int leaveOnServerCount(void) const { return mLeaveOnServerCount; }
  virtual void setLeaveOnServerCount(int);

  /**
   * If value is positive, leave so many MBs on the server.
   */
  int leaveOnServerSize(void) const { return mLeaveOnServerSize; }
  virtual void setLeaveOnServerSize(int);

  /**
   * Shall messages be filter on the server (TRUE)
   * or not (FALSE).
   */
  bool filterOnServer(void) const { return mFilterOnServer; }
  virtual void setFilterOnServer(bool);

  /**
   * Size of messages which should be check on the
   * pop server before download
   */
  unsigned int filterOnServerCheckSize(void) const { return mFilterOnServerCheckSize; }
  virtual void setFilterOnServerCheckSize(unsigned int);

  /**
   * Inherited methods.
   */
  virtual QString type(void) const;
  virtual void readConfig(KConfig&);
  virtual void writeConfig(KConfig&);
  virtual void processNewMail(bool _interactive);

  virtual void killAllJobs( bool disconnectSlave=false ); // NOOP currently

protected:
  enum Stage { Idle, List, Uidl, Head, Retr, Dele, Quit };
  friend class ::AccountManager;
  PopAccount(AccountManager* owner, const QString& accountName, uint id);

  /**
   * Start a KIO Job to get a list of messages on the pop server
   */
  void startJob();

  /**
   * Connect up the standard signals/slots for the KIO Jobs
   */
  void connectJob();

  /**
   * Process any queued messages
   */
  void processRemainingQueuedMessages();

  /**
   * Save the list of seen uids for this user/server
   */
  void saveUidList();

  bool    mUsePipelining;
  bool    mLeaveOnServer;
  int     mLeaveOnServerDays;
  int     mLeaveOnServerCount;
  int     mLeaveOnServerSize;
  bool    gotMsgs;
  bool    mFilterOnServer;
  unsigned int mFilterOnServerCheckSize;

  KIO::SimpleJob *job;
  //Map of ID's vs. sizes of messages which should be downloaded
  QMap<QString, int> mMsgsPendingDownload;

  QPtrList<KMPopHeaders> headersOnServer;
  QPtrListIterator<KMPopHeaders> headerIt;
  bool headers;

  QMap<QString, bool> mHeaderDeleteUids;
  QMap<QString, bool> mHeaderDownUids;
  QMap<QString, bool> mHeaderLaterUids;

  QStringList idsOfMsgs; //used for ids and for count
  QValueList<int> lensOfMsgs;
  QMap<QString, QString> mUidForIdMap; // maps message ID (i.e. index on the server) to UID
  QDict<int> mUidsOfSeenMsgsDict; // set of UIDs of previously seen messages (for fast lookup)
  QDict<int> mUidsOfNextSeenMsgsDict; // set of UIDs of seen messages (for the next check)
  QValueVector<int> mTimeOfSeenMsgsVector; // list of times of previously seen messages
  QMap<QString, int> mTimeOfNextSeenMsgsMap; // map of uid to times of seen messages
  QDict<int> mSizeOfNextSeenMsgsDict;
  QStringList idsOfMsgsToDelete;
  QStringList idsOfForcedDeletes;
  int indexOfCurrentMsg;

  QValueList<KMMessage*> msgsAwaitingProcessing;
  QStringList msgIdsAwaitingProcessing;
  QStringList msgUidsAwaitingProcessing;

  QByteArray curMsgData;
  QDataStream *curMsgStrm;

  int curMsgLen;
  Stage stage;
  QTimer processMsgsTimer;
  int processingDelay;
  int numMsgs, numBytes, numBytesToRead, numBytesRead, numMsgBytesRead;
  bool interactive;
  bool mProcessing;
  bool mUidlFinished;
  int dataCounter;

protected slots:
  /**
   * Messages are downloaded in the background and then once every x seconds
   * a batch of messages are processed. Messages are processed in batches to
   * reduce flicker (multiple refreshes of the qlistview of messages headers
   * in a single second causes flicker) when using a fast pop server such as
   * one on a lan.
   *
   * Processing a message means applying KMAccount::processNewMsg to it and
   * adding its UID to the list of seen UIDs
   */
  void slotProcessPendingMsgs();

  /**
   * If there are more messages to be downloaded then start a new kio job
   * to get the message whose id is at the head of the queue
   */
  void slotGetNextMsg();

  /**
   * A messages has been retrieved successfully. The next data belongs to the
   * next message.
   */
  void slotMsgRetrieved(KIO::Job*, const QString &);

  /**
   * New data has arrived append it to the end of the current message
   */
  void slotData( KIO::Job*, const QByteArray &);

  /**
   * Finished downloading the current kio job, either due to an error
   * or because the job has been canceled or because the complete message
   * has been downloaded
   */
  void slotResult( KIO::Job* );

  /**
   * Cleans up after a user cancels the current job
   */
  void slotCancel();

  /**
   * Kills the job if still stage == List
   */
  void slotAbortRequested();

  /**
   * Called when a job is finished. Basically a finite state machine for
   * cycling through the Idle, List, Uidl, Retr, Quit stages
   */
  void slotJobFinished();

  /**
   * Slave error handling
   */
  void slotSlaveError(KIO::Slave *, int, const QString &);

  /**
   * If there are more headers to be downloaded then start a new kio job
   * to get the next header
   */
  void slotGetNextHdr();
};

} // namespace KMail



#endif /*KMAcctExpPop_h*/