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