1/*
2 Copyright (c) 2007 Volker Krause <vkrause@kde.org>
3
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
8
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17 02110-1301, USA.
18*/
19
20#ifndef AKONADI_SESSION_P_H
21#define AKONADI_SESSION_P_H
22
23#include "akonadiprivate_export.h"
24#include "session.h"
25#include "imapparser_p.h"
26#include "item.h"
27#include "servermanager.h"
28
29#include <QtNetwork/QLocalSocket>
30
31#include <QtCore/QQueue>
32#include <QtCore/QThreadStorage>
33#include <QFile>
34
35class QIODevice;
36
37namespace Akonadi {
38
39/**
40 * @internal
41 */
42class AKONADI_TESTS_EXPORT SessionPrivate
43{
44public:
45 explicit SessionPrivate(Session *parent);
46
47 virtual ~SessionPrivate()
48 {
49 delete parser;
50 }
51
52 virtual void init(const QByteArray &sessionId);
53
54 void startNext();
55 /// Disconnects a previously existing connection and tries to reconnect
56 void forceReconnect();
57 /// Attemps to establish a connections to the Akonadi server.
58 virtual void reconnect();
59 void serverStateChanged(ServerManager::State);
60 void socketDisconnected();
61 void socketError(QLocalSocket::LocalSocketError error);
62 void socketError(QAbstractSocket::SocketError error);
63 void dataReceived();
64 void doStartNext();
65 void startJob(Job *job);
66
67 /**
68 @internal For testing purposes only. See FakeSesson.
69 @param job the job to end
70 */
71 void endJob(Job *job);
72
73 void jobDone(KJob *job);
74 void jobWriteFinished(Akonadi::Job *job);
75 void jobDestroyed(QObject *job);
76
77 bool canPipelineNext();
78
79 /**
80 * Creates a new default session for this thread with
81 * the given @p sessionId. The session can be accessed
82 * later by defaultSession().
83 *
84 * You only need to call this method if you want that the
85 * default session has a special custom id, otherwise a random unique
86 * id is used automatically.
87 * @param sessionId the id of new default session
88 */
89 static void createDefaultSession(const QByteArray &sessionId);
90
91 /**
92 * Sets the default session.
93 * @internal Only for unit tests.
94 */
95 static void setDefaultSession(Session *session);
96
97 /**
98 Associates the given Job object with this session.
99 */
100 virtual void addJob(Job *job);
101
102 /**
103 Returns the next IMAP tag.
104 */
105 int nextTag();
106
107 /**
108 Sends the given raw data.
109 */
110 void writeData(const QByteArray &data);
111
112 /**
113 * Propagate item revision changes to following jobs.
114 */
115 void itemRevisionChanged(Akonadi::Item::Id itemId, int oldRevision, int newRevision);
116
117 static int minimumProtocolVersion()
118 {
119 return 44;
120 }
121
122 /**
123 * Default location for akonadiconnectionrc
124 */
125 static QString connectionFile();
126
127 Session *mParent;
128 QByteArray sessionId;
129 QIODevice *socket;
130 bool connected;
131 int theNextTag;
132 int protocolVersion;
133
134 // job management
135 QQueue<Job *> queue;
136 QQueue<Job *> pipeline;
137 Job *currentJob;
138 bool jobRunning;
139
140 // parser stuff
141 ImapParser *parser;
142
143 QFile *logFile;
144};
145
146}
147
148#endif
149