Changeset View
Changeset View
Standalone View
Standalone View
kcms/bolt/kcm_bolt.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (c) 2018 - 2019 Daniel Vrátil <dvratil@kde.org> | ||||
3 | * | ||||
4 | * This program is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU General Public License as | ||||
6 | * published by the Free Software Foundation; either version 2 of | ||||
7 | * the License or (at your option) version 3 or any later version | ||||
8 | * accepted by the membership of KDE e.V. (or its successor approved | ||||
9 | * by the membership of KDE e.V.), which shall act as a proxy | ||||
10 | * defined in Section 14 of version 3 of the license. | ||||
11 | * | ||||
12 | * This program is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||
15 | * GNU General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU General Public License | ||||
18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | */ | ||||
20 | | ||||
21 | #include "kcm_bolt.h" | ||||
22 | | ||||
23 | #include <KPluginFactory> | ||||
24 | #include <KAboutData> | ||||
25 | #include <KLocalizedString> | ||||
26 | | ||||
27 | #include "device.h" | ||||
28 | #include "devicemodel.h" | ||||
29 | #include "manager.h" | ||||
30 | #include "enum.h" | ||||
31 | | ||||
32 | #include <QDebug> | ||||
33 | | ||||
34 | K_PLUGIN_FACTORY_WITH_JSON(KCMBoltFactory, "kcm_bolt.json", registerPlugin<KCMBolt>();) | ||||
35 | | ||||
36 | class QMLHelper : public QObject | ||||
37 | { | ||||
38 | Q_OBJECT | ||||
39 | public: | ||||
40 | explicit QMLHelper(QObject *parent = nullptr) | ||||
41 | : QObject(parent) | ||||
42 | {} | ||||
43 | | ||||
44 | public Q_SLOTS: | ||||
45 | void authorizeDevice(Bolt::Device *device, Bolt::AuthFlags authFlags, | ||||
46 | QJSValue successCb = {}, QJSValue errorCb = {}) | ||||
47 | { | ||||
48 | device->authorize(authFlags, invoke(successCb), invoke<QString>(errorCb)); | ||||
49 | } | ||||
50 | | ||||
51 | void enrollDevice(Bolt::Manager *manager, const QString &uid, Bolt::Policy policy, | ||||
52 | Bolt::AuthFlags authFlags, QJSValue successCb = {}, QJSValue errorCb = {}) | ||||
53 | { | ||||
54 | manager->enrollDevice(uid, policy, authFlags, invoke(successCb), invoke<QString>(errorCb)); | ||||
55 | } | ||||
56 | | ||||
57 | void forgetDevice(Bolt::Manager *manager, const QString &uid, QJSValue successCb, QJSValue errorCb) | ||||
58 | { | ||||
59 | manager->forgetDevice(uid, invoke(successCb), invoke<QString>(errorCb)); | ||||
60 | } | ||||
61 | | ||||
62 | private: | ||||
63 | template<typename ... Args> | ||||
64 | std::function<void(Args ...)> invoke(QJSValue cb_) | ||||
65 | { | ||||
66 | return [cb = std::move(cb_)](Args && ... args) mutable { | ||||
67 | Q_ASSERT(cb.isCallable()); | ||||
68 | cb.call({std::forward<Args>(args) ...}); | ||||
69 | }; | ||||
70 | } | ||||
71 | }; | ||||
72 | | ||||
73 | KCMBolt::KCMBolt(QObject *parent, const QVariantList &args) | ||||
74 | : KQuickAddons::ConfigModule(parent, args) | ||||
75 | { | ||||
76 | qmlRegisterType<Bolt::DeviceModel>("org.kde.bolt", 0, 1, "DeviceModel"); | ||||
77 | qmlRegisterType<Bolt::Manager>("org.kde.bolt", 0, 1, "Manager"); | ||||
78 | qmlRegisterUncreatableType<Bolt::Device>("org.kde.bolt", 0, 1, "Device", | ||||
79 | QStringLiteral("Use DeviceModel")); | ||||
80 | qmlRegisterUncreatableMetaObject(Bolt::staticMetaObject, "org.kde.bolt", 0, 1, "Bolt", | ||||
81 | QStringLiteral("For enums and flags only")); | ||||
82 | qmlRegisterSingletonType<QMLHelper>("org.kde.bolt", 0, 1, "QMLHelper", | ||||
83 | [](auto, auto) -> QObject* { return new QMLHelper(); }); | ||||
84 | | ||||
85 | auto about = new KAboutData(QStringLiteral("kcm_bolt"), | ||||
86 | i18n("Thunderbolt Device Management"), | ||||
87 | QStringLiteral("0.1"), | ||||
88 | i18n("System Settings module for managing Thunderbolt devices."), | ||||
89 | KAboutLicense::GPL); | ||||
90 | about->addAuthor(i18n("Daniel Vrátil"), {}, QStringLiteral("dvratil@kde.org")); | ||||
91 | setAboutData(about); | ||||
92 | } | ||||
93 | | ||||
94 | #include "kcm_bolt.moc" |