1 /*
2  * SPDX-FileCopyrightText: 2008 Igor Trindade Oliveira <igor_trindade@yahoo.com.br>
3  *
4  * SPDX-License-Identifier: LGPL-2.1-or-later
5  */
6 
7 #pragma once
8 
9 #include <servermanager.h>
10 
11 #include <QObject>
12 #include <QPair>
13 #include <QVector>
14 
15 #include <memory>
16 
17 class KProcess;
18 class KJob;
19 
20 class SetupTest : public QObject
21 {
22     Q_OBJECT
23     Q_CLASSINFO("D-Bus Interface", "org.kde.Akonadi.Testrunner")
24 
25 public:
26     SetupTest();
27     ~SetupTest() override;
28 
29     /**
30       Sets the instance identifier for the Akonadi session.
31       Call this before using any other Akonadi API!
32     */
33     void setupInstanceId();
34     bool startAkonadiDaemon();
35     void stopAkonadiDaemon();
36     QString basePath() const;
37 
38     /// Identifier used for the Akonadi session
39     QString instanceId() const;
40 
41     /// set an environment variable
42     void setEnvironmentVariable(const QByteArray &name, const QString &value);
43 
44     /// retrieve all modified environment variables, for writing the shell script
45     using EnvVar = QPair<QByteArray, QByteArray>;
46     QVector<EnvVar> environmentVariables() const;
47 
48 public Q_SLOTS:
49     Q_SCRIPTABLE void shutdown();
50     Q_SCRIPTABLE void shutdownHarder();
51     /** Synchronously restarts the server. */
52     Q_SCRIPTABLE void restartAkonadiServer();
53     Q_SCRIPTABLE void trackAkonadiProcess(bool track);
54 
55 Q_SIGNALS:
56     void setupDone();
57     void serverExited(int exitCode);
58 
59 private Q_SLOTS:
60     void serverStateChanged(Akonadi::ServerManager::State state);
61     void slotAkonadiDaemonProcessFinished(int exitCode);
62     void agentCreationResult(KJob *job);
63     void synchronizationResult(KJob *job);
64 
65 private:
66     void setupAgents();
67     void copyXdgDirectory(const QString &src, const QString &dst);
68     void copyDirectory(const QString &src, const QString &dst);
69     void createTempEnvironment();
70     void cleanTempEnvironment() const;
71     void setupFailed();
72     void writeAkonadiserverrc(const QString &path);
73     void checkSetupDone();
74 
75 private:
76     std::unique_ptr<KProcess> mAkonadiDaemonProcess;
77     bool mShuttingDown;
78     bool mAgentsCreated;
79     bool mTrackAkonadiProcess;
80     int mSetupJobCount;
81     int mExitCode;
82     QVector<EnvVar> mEnvVars;
83 };
84 
85