File indexing completed on 2024-06-23 05:06:55

0001 /*
0002     SPDX-FileCopyrightText: 2007 Volker Krause <vkrause@kde.org>
0003 
0004     SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #pragma once
0008 
0009 #include "akonadicore_export.h"
0010 #include "commandbuffer_p.h"
0011 #include "item.h"
0012 #include "servermanager.h"
0013 #include "session.h"
0014 
0015 #include <QFile>
0016 #include <QMetaObject>
0017 #include <QQueue>
0018 #include <QThreadStorage>
0019 
0020 namespace Akonadi
0021 {
0022 class SessionThread;
0023 class Connection;
0024 
0025 namespace Protocol
0026 {
0027 class Command;
0028 }
0029 
0030 /**
0031  * @internal
0032  */
0033 class AKONADICORE_EXPORT SessionPrivate
0034 {
0035 public:
0036     explicit SessionPrivate(Session *parent);
0037 
0038     virtual ~SessionPrivate();
0039 
0040     virtual void init(const QByteArray &sessionId);
0041 
0042     SessionThread *sessionThread() const
0043     {
0044         return mSessionThread;
0045     }
0046 
0047     void enqueueCommand(qint64 tag, const Protocol::CommandPtr &cmd);
0048 
0049     void startNext();
0050     /// Disconnects a previously existing connection and tries to reconnect
0051     void forceReconnect();
0052     /// Attempts to establish a connections to the Akonadi server.
0053     virtual void reconnect();
0054     void serverStateChanged(ServerManager::State);
0055     void socketDisconnected();
0056     void socketError(const QString &error);
0057     void dataReceived();
0058     virtual bool handleCommands();
0059     void doStartNext();
0060     void startJob(Job *job);
0061 
0062     /**
0063       @internal For testing purposes only. See FakeSesson.
0064       @param job the job to end
0065     */
0066     void endJob(Job *job);
0067 
0068     void jobDone(KJob *job);
0069     void jobWriteFinished(Akonadi::Job *job);
0070     void jobDestroyed(QObject *job);
0071 
0072     bool canPipelineNext();
0073 
0074     /**
0075      * Creates a new default session for this thread with
0076      * the given @p sessionId. The session can be accessed
0077      * later by defaultSession().
0078      *
0079      * You only need to call this method if you want that the
0080      * default session has a special custom id, otherwise a random unique
0081      * id is used automatically.
0082      * @param sessionId the id of new default session
0083      */
0084     static void createDefaultSession(const QByteArray &sessionId);
0085 
0086     /**
0087      * Sets the default session.
0088      * @internal Only for unit tests.
0089      */
0090     static void setDefaultSession(Session *session);
0091 
0092     /**
0093       Associates the given Job object with this session.
0094     */
0095     virtual void addJob(Job *job);
0096 
0097     /**
0098       Returns the next IMAP tag.
0099     */
0100     qint64 nextTag();
0101 
0102     /**
0103       Sends the given command to server
0104     */
0105     void sendCommand(qint64 tag, const Protocol::CommandPtr &command);
0106 
0107     /**
0108      * Propagate item revision changes to following jobs.
0109      */
0110     void itemRevisionChanged(Akonadi::Item::Id itemId, int oldRevision, int newRevision);
0111 
0112     void clear(bool forceReconnect);
0113 
0114     void publishOtherJobs(Job *thanThisJob);
0115 
0116     Session *mParent = nullptr;
0117     SessionThread *mSessionThread = nullptr;
0118     Connection *connection = nullptr;
0119     QMetaObject::Connection connThreadCleanUp;
0120     QByteArray sessionId;
0121     bool connected;
0122     qint64 theNextTag;
0123     int protocolVersion;
0124 
0125     CommandBuffer mCommandBuffer;
0126 
0127     // job management
0128     QQueue<Job *> queue;
0129     QQueue<Job *> pipeline;
0130     Job *currentJob = nullptr;
0131     bool jobRunning;
0132 
0133     QFile *logFile = nullptr;
0134 };
0135 
0136 }