File: main.cpp

package info (click to toggle)
dtkcore 5.7.12-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,728 kB
  • sloc: cpp: 22,021; ansic: 183; python: 68; xml: 58; makefile: 27; sh: 15
file content (55 lines) | stat: -rw-r--r-- 1,628 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// SPDX-FileCopyrightText: 2017 - 2022 UnionTech Software Technology Co., Ltd.
//
// SPDX-License-Identifier: LGPL-3.0-or-later

#include "util/dexportedinterface.h"

#include <QCoreApplication>
#include <QDBusConnection>
#include <QJsonDocument>
#include <QJsonValue>

#include <cmath>

//#define ALTERNATE_USAGE

DCORE_USE_NAMESPACE

class CustomInterface : public DUtil::DExportedInterface
{
    QVariant invoke(const QString &action, const QString &parameters) const
    {
        QJsonDocument d = QJsonDocument::fromJson(parameters.toUtf8());
        if (action == "pow") {
            return QVariant(pow(d["a"].toDouble(), d["b"].toDouble()));
        }
        return QVariant();
    }
};

int main(int argc, char **argv)
{
    QCoreApplication app(argc, argv);
    QDBusConnection::sessionBus().registerService("com.deepin.ExpIntfTest");

#ifndef ALTERNATE_USAGE
    DUtil::DExportedInterface *ei = new DUtil::DExportedInterface();
    ei->registerAction("quit", "quit the application", [&app](QString)->QVariant {
        app.quit();
        return QVariant();
    });

    ei->registerAction("answer", "answer to the ultimate question of life, the universe, and everything",
                       [](QString)->QVariant {return QVariant(42);});

    ei->registerAction("sum", "returns the sum of two integers", [](QString p)->QVariant {
        QJsonDocument d = QJsonDocument::fromJson(p.toUtf8());
        return QVariant(d["a"].toInt() + d["b"].toInt());
    });
#else
    CustomInterface *cei = new CustomInterface();
    cei->registerAction("pow", "raise a number to a power");
#endif

    return app.exec();
}