Changeset View
Changeset View
Standalone View
Standalone View
autotests/bolt/fakeserver/fakeserver.cpp
- This file was added.
1 | #include "fakeserver.h" | ||||
---|---|---|---|---|---|
2 | #include "fakemanager.h" | ||||
3 | | ||||
4 | #include <QJsonDocument> | ||||
5 | #include <QJsonObject> | ||||
6 | #include <QDBusConnection> | ||||
7 | #include <QDBusConnectionInterface> | ||||
8 | #include <QDBusError> | ||||
9 | #include <QDebug> | ||||
10 | #include <QTest> | ||||
11 | #include <QEventLoop> | ||||
12 | #include <QTimer> | ||||
13 | | ||||
14 | namespace { | ||||
15 | | ||||
16 | static const QString OrgKdeFakebolt = QStringLiteral("org.kde.fakebolt"); | ||||
17 | | ||||
18 | } | ||||
19 | | ||||
20 | FakeServer::FakeServer(const QString &filename) | ||||
21 | { | ||||
22 | QFile jsonFile(filename); | ||||
23 | if (!jsonFile.open(QIODevice::ReadOnly)) { | ||||
24 | qCritical("Failed to open file %s: %s", qUtf8Printable(filename), qUtf8Printable(jsonFile.errorString())); | ||||
25 | throw FakeServerException( | ||||
26 | QStringLiteral("Failed to open file %1: %2").arg(filename, jsonFile.errorString())); | ||||
27 | } | ||||
28 | | ||||
29 | const auto doc = QJsonDocument::fromJson(jsonFile.readAll()); | ||||
30 | | ||||
31 | if (!QDBusConnection::sessionBus().registerService(OrgKdeFakebolt)) { | ||||
32 | throw FakeServerException( | ||||
33 | QStringLiteral("Failed to register org.kde.fakebolt service: %1") | ||||
34 | .arg(QDBusConnection::sessionBus().lastError().message())); | ||||
35 | } | ||||
36 | | ||||
37 | try { | ||||
38 | mManager.reset(new FakeManager(doc.object())); | ||||
39 | } catch (const FakeManagerException &e) { | ||||
40 | throw FakeServerException(e.what()); | ||||
41 | } | ||||
42 | } | ||||
43 | | ||||
44 | FakeServer::FakeServer() | ||||
45 | { | ||||
46 | if (!QDBusConnection::sessionBus().registerService(OrgKdeFakebolt)) { | ||||
47 | throw FakeServerException( | ||||
48 | QStringLiteral("Failed to register org.kde.fakebolt service: %1") | ||||
49 | .arg(QDBusConnection::sessionBus().lastError().message())); | ||||
50 | } | ||||
51 | | ||||
52 | try { | ||||
53 | mManager.reset(new FakeManager()); | ||||
54 | } catch (FakeManagerException &e) { | ||||
55 | throw FakeServerException(e.what()); | ||||
56 | } | ||||
57 | } | ||||
58 | | ||||
59 | FakeServer::~FakeServer() | ||||
60 | { | ||||
61 | } | ||||
62 | | ||||
63 | void FakeServer::enableFakeEnv() | ||||
64 | { | ||||
65 | qputenv("KBOLT_FAKE", "1"); | ||||
66 | } | ||||
67 | | ||||
68 | FakeManager *FakeServer::manager() const | ||||
69 | { | ||||
70 | return mManager.get(); | ||||
71 | } |