Debian Codegraph @

examples/expintf-example/main.cpp

raw
1 // SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
2 //
3 // SPDX-License-Identifier: LGPL-3.0-or-later
4
5 #include "util/dexportedinterface.h"
6
7 #include <QCoreApplication>
8 #include <QDBusConnection>
9 #include <QJsonDocument>
10 #include <QJsonValue>
11
12 #include <cmath>
13
14 //#define ALTERNATE_USAGE
15
16 DCORE_USE_NAMESPACE
17
18 class CustomInterface : public DUtil::DExportedInterface
19 {
20 QVariant invoke(const QString &action, const QString &parameters) const
21 {
22 QJsonDocument d = QJsonDocument::fromJson(parameters.toUtf8());
23 if (action == "pow") {
24 return QVariant(pow(d["a"].toDouble(), d["b"].toDouble()));
25 }
26 return QVariant();
27 }
28 };
29
30 int main(int argc, char **argv)
31 {
32 QCoreApplication app(argc, argv);
33 QDBusConnection::sessionBus().registerService("com.deepin.ExpIntfTest");
34
35 #ifndef ALTERNATE_USAGE
36 DUtil::DExportedInterface *ei = new DUtil::DExportedInterface();
37 ei->registerAction("quit", "quit the application", [&app](QString)->QVariant {
38 app.quit();
39 return QVariant();
40 });
41
42 ei->registerAction("answer", "answer to the ultimate question of life, the universe, and everything",
43 [](QString)->QVariant {return QVariant(42);});
44
45 ei->registerAction("sum", "returns the sum of two integers", [](QString p)->QVariant {
46 QJsonDocument d = QJsonDocument::fromJson(p.toUtf8());
47 return QVariant(d["a"].toInt() + d["b"].toInt());
48 });
49 #else
50 CustomInterface *cei = new CustomInterface();
51 cei->registerAction("pow", "raise a number to a power");
52 #endif
53
54 return app.exec();
55 }