Commit 7040e191 authored by Alexander Lohnau's avatar Alexander Lohnau 💬

Run clang-format

If you want git blame to ignore formatting revisions run:
git config blame.ignoreRevsFile .git-blame-ignore-revs
parent 8d6036ab
......@@ -35,8 +35,7 @@ FakeDevice::FakeDevice(const QString &uid, QObject *parent)
new FakeDeviceAdaptor(this);
auto bus = QDBusConnection::sessionBus();
if (!bus.registerObject(dbusPath().path(), this)) {
throw FakeDeviceException(QStringLiteral("Failed to register device %1 to DBus: %2")
.arg(mUid, bus.lastError().message()));
throw FakeDeviceException(QStringLiteral("Failed to register device %1 to DBus: %2").arg(mUid, bus.lastError().message()));
}
}
......@@ -61,8 +60,7 @@ FakeDevice::FakeDevice(const QJsonObject &json, QObject *parent)
new FakeDeviceAdaptor(this);
auto bus = QDBusConnection::sessionBus();
if (!bus.registerObject(dbusPath().path(), this)) {
throw FakeDeviceException(QStringLiteral("Failed to register device %1 to DBus: %2")
.arg(mUid, bus.lastError().message()));
throw FakeDeviceException(QStringLiteral("Failed to register device %1 to DBus: %2").arg(mUid, bus.lastError().message()));
}
}
......@@ -73,8 +71,7 @@ FakeDevice::~FakeDevice()
QDBusObjectPath FakeDevice::dbusPath() const
{
return QDBusObjectPath(QStringLiteral("/org/freedesktop/bolt/devices/%1")
.arg(QString(mUid).replace(QLatin1Char('-'), QLatin1Char('_'))));
return QDBusObjectPath(QStringLiteral("/org/freedesktop/bolt/devices/%1").arg(QString(mUid).replace(QLatin1Char('-'), QLatin1Char('_'))));
}
QString FakeDevice::uid() const
......
......@@ -21,15 +21,17 @@
#ifndef FAKEDEVICE_H
#define FAKEDEVICE_H
#include <QObject>
#include <QDBusObjectPath>
#include <QJsonObject>
#include <QObject>
class FakeDeviceException : public std::runtime_error
{
public:
FakeDeviceException(const QString &what)
: std::runtime_error(what.toStdString()) {}
: std::runtime_error(what.toStdString())
{
}
};
class FakeDevice : public QObject
......@@ -116,4 +118,3 @@ private:
};
#endif
......@@ -19,8 +19,8 @@
*/
#include "fakemanager.h"
#include "fakemanageradaptor.h"
#include "fakedevice.h"
#include "fakemanageradaptor.h"
#include <QDBusConnection>
#include <QDBusError>
......@@ -32,7 +32,8 @@
using namespace std::chrono_literals;
namespace {
namespace
{
static const QString kManagerDBusPath = QStringLiteral("/org/freedesktop/bolt");
}
......@@ -45,8 +46,7 @@ FakeManager::FakeManager(const QJsonObject &json, QObject *parent)
{
new FakeManagerAdaptor(this);
if (!QDBusConnection::sessionBus().registerObject(kManagerDBusPath, this)) {
throw FakeManagerException(QStringLiteral("Failed to register FakeManager to DBus: %1")
.arg(QDBusConnection::sessionBus().lastError().message()));
throw FakeManagerException(QStringLiteral("Failed to register FakeManager to DBus: %1").arg(QDBusConnection::sessionBus().lastError().message()));
}
const auto jsonDevices = json[QStringLiteral("Devices")].toArray();
......@@ -61,8 +61,7 @@ FakeManager::FakeManager(QObject *parent)
{
new FakeManagerAdaptor(this);
if (!QDBusConnection::sessionBus().registerObject(kManagerDBusPath, this)) {
throw FakeManagerException(QStringLiteral("Failed to register FakeManager to DBus: %1")
.arg(QDBusConnection::sessionBus().lastError().message()));
throw FakeManagerException(QStringLiteral("Failed to register FakeManager to DBus: %1").arg(QDBusConnection::sessionBus().lastError().message()));
}
}
......@@ -89,12 +88,13 @@ void FakeManager::removeDevice(const QString &uid)
Q_EMIT DeviceRemoved(device->dbusPath());
}
QList<FakeDevice*> FakeManager::devices() const
QList<FakeDevice *> FakeManager::devices() const
{
QList<FakeDevice*> rv;
QList<FakeDevice *> rv;
rv.reserve(mDevices.size());
std::transform(mDevices.cbegin(), mDevices.cend(),
std::back_inserter(rv), [](const auto &v) { return v.second.get(); });
std::transform(mDevices.cbegin(), mDevices.cend(), std::back_inserter(rv), [](const auto &v) {
return v.second.get();
});
return rv;
}
......@@ -108,7 +108,7 @@ bool FakeManager::isProbing() const
return mProbing;
}
QString FakeManager::defaultPolicy() const
QString FakeManager::defaultPolicy() const
{
return mDefaultPolicy;
}
......@@ -150,9 +150,7 @@ QDBusObjectPath FakeManager::DeviceByUid(const QString &uid) const
}
}
QDBusObjectPath FakeManager::EnrollDevice(const QString &uid,
const QString &policy,
const QString &flags)
QDBusObjectPath FakeManager::EnrollDevice(const QString &uid, const QString &policy, const QString &flags)
{
std::this_thread::sleep_for(1s); // simulate this operation taking time
......@@ -185,4 +183,3 @@ void FakeManager::ForgetDevice(const QString &uid)
device->setStored(false);
device->setStatus(QLatin1String("connected"));
}
......@@ -21,21 +21,22 @@
#ifndef FAKEMANAGER_H_
#define FAKEMANAGER_H_
#include <QObject>
#include <QDBusObjectPath>
#include <QJsonObject>
#include <QList>
#include <QObject>
#include <QString>
#include <QJsonObject>
#include <memory>
#include <map>
#include <memory>
class FakeManagerException : public std::runtime_error
{
public:
FakeManagerException(const QString &what)
: std::runtime_error(what.toStdString())
{}
{
}
};
class FakeDevice;
......@@ -85,5 +86,4 @@ private:
std::map<QString /*uid*/, std::unique_ptr<FakeDevice>> mDevices;
};
#endif
......@@ -21,18 +21,18 @@
#include "fakeserver.h"
#include "fakemanager.h"
#include <QJsonDocument>
#include <QJsonObject>
#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusError>
#include <QDebug>
#include <QTest>
#include <QEventLoop>
#include <QJsonDocument>
#include <QJsonObject>
#include <QTest>
#include <QTimer>
namespace {
namespace
{
static const QString OrgKdeFakebolt = QStringLiteral("org.kde.fakebolt");
}
......@@ -42,16 +42,13 @@ FakeServer::FakeServer(const QString &filename)
QFile jsonFile(filename);
if (!jsonFile.open(QIODevice::ReadOnly)) {
qCritical("Failed to open file %s: %s", qUtf8Printable(filename), qUtf8Printable(jsonFile.errorString()));
throw FakeServerException(
QStringLiteral("Failed to open file %1: %2").arg(filename, jsonFile.errorString()));
throw FakeServerException(QStringLiteral("Failed to open file %1: %2").arg(filename, jsonFile.errorString()));
}
const auto doc = QJsonDocument::fromJson(jsonFile.readAll());
if (!QDBusConnection::sessionBus().registerService(OrgKdeFakebolt)) {
throw FakeServerException(
QStringLiteral("Failed to register org.kde.fakebolt service: %1")
.arg(QDBusConnection::sessionBus().lastError().message()));
throw FakeServerException(QStringLiteral("Failed to register org.kde.fakebolt service: %1").arg(QDBusConnection::sessionBus().lastError().message()));
}
try {
......@@ -64,9 +61,7 @@ FakeServer::FakeServer(const QString &filename)
FakeServer::FakeServer()
{
if (!QDBusConnection::sessionBus().registerService(OrgKdeFakebolt)) {
throw FakeServerException(
QStringLiteral("Failed to register org.kde.fakebolt service: %1")
.arg(QDBusConnection::sessionBus().lastError().message()));
throw FakeServerException(QStringLiteral("Failed to register org.kde.fakebolt service: %1").arg(QDBusConnection::sessionBus().lastError().message()));
}
try {
......
......@@ -29,9 +29,13 @@ class FakeServerException : public std::runtime_error
{
public:
FakeServerException(const char *what)
: std::runtime_error(what) {}
: std::runtime_error(what)
{
}
FakeServerException(const QString &what)
: std::runtime_error(what.toStdString()) {}
: std::runtime_error(what.toStdString())
{
}
};
class FakeManager;
......@@ -45,6 +49,7 @@ public:
static void enableFakeEnv();
FakeManager *manager() const;
private:
std::unique_ptr<FakeManager> mManager;
};
......
......@@ -18,12 +18,12 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QCommandLineOption>
#include <QCommandLineParser>
#include <QCoreApplication>
#include <QDBusConnection>
#include <QCommandLineParser>
#include <QCommandLineOption>
#include <QJsonDocument>
#include <QFile>
#include <QJsonDocument>
#include <QTimer>
#include <iostream>
......
......@@ -19,12 +19,12 @@
*/
#include <QObject>
#include <QTest>
#include <QSignalSpy>
#include <QTest>
#include "fakeserver.h"
#include "fakemanager.h"
#include "fakedevice.h"
#include "fakemanager.h"
#include "fakeserver.h"
#include "device.h"
......@@ -48,7 +48,6 @@ protected:
}
};
class KDEDTest : public QObject
{
Q_OBJECT
......@@ -68,7 +67,7 @@ private Q_SLOTS:
fakeServer = std::make_unique<FakeServer>();
} catch (const FakeServerException &e) {
qWarning("Fake server exception: %s", e.what());
QFAIL("Caught server exception");
QFAIL("Caught server exception");
}
TestableKDEDBolt kded(nullptr, {});
......
......@@ -19,12 +19,12 @@
*/
#include <QObject>
#include <QTest>
#include <QSignalSpy>
#include <QTest>
#include "fakeserver.h"
#include "fakemanager.h"
#include "fakedevice.h"
#include "fakemanager.h"
#include "fakeserver.h"
#include "device.h"
#include "manager.h"
......@@ -56,8 +56,7 @@ private Q_SLOTS:
auto fakeManager = fakeServer->manager();
FakeDevice *fakeDevice = nullptr;
try {
fakeDevice = fakeManager->addDevice(
std::make_unique<FakeDevice>(QStringLiteral("Device1")));
fakeDevice = fakeManager->addDevice(std::make_unique<FakeDevice>(QStringLiteral("Device1")));
} catch (const FakeDeviceException &e) {
qWarning("Fake device exception: %s", e.what());
QFAIL("Caught device exception");
......
......@@ -18,17 +18,17 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <QDebug>
#include <QObject>
#include <QTest>
#include <QSignalSpy>
#include <QDebug>
#include <QTest>
#include "fakeserver.h"
#include "fakemanager.h"
#include "fakedevice.h"
#include "fakemanager.h"
#include "fakeserver.h"
#include "manager.h"
#include "device.h"
#include "manager.h"
#include <memory>
......@@ -66,8 +66,7 @@ private Q_SLOTS:
FakeDevice *fakeDevice = nullptr;
try {
fakeDevice = fakeManager->addDevice(
std::make_unique<FakeDevice>(QStringLiteral("device1")));
fakeDevice = fakeManager->addDevice(std::make_unique<FakeDevice>(QStringLiteral("device1")));
} catch (const FakeDeviceException &e) {
qWarning("Fake device exception: %s", e.what());
QFAIL("Caught device exception");
......
......@@ -20,14 +20,14 @@
#include "kcm_bolt.h"
#include <KPluginFactory>
#include <KAboutData>
#include <KLocalizedString>
#include <KPluginFactory>
#include "device.h"
#include "devicemodel.h"
#include "manager.h"
#include "enum.h"
#include "manager.h"
#include <QDebug>
......@@ -39,17 +39,17 @@ class QMLHelper : public QObject
public:
explicit QMLHelper(QObject *parent = nullptr)
: QObject(parent)
{}
{
}
public Q_SLOTS:
void authorizeDevice(Bolt::Device *device, Bolt::AuthFlags authFlags,
QJSValue successCb = {}, QJSValue errorCb = {})
void authorizeDevice(Bolt::Device *device, Bolt::AuthFlags authFlags, QJSValue successCb = {}, QJSValue errorCb = {})
{
device->authorize(authFlags, invoke(successCb), invoke<QString>(errorCb));
}
void enrollDevice(Bolt::Manager *manager, const QString &uid, Bolt::Policy policy,
Bolt::AuthFlags authFlags, QJSValue successCb = {}, QJSValue errorCb = {})
void
enrollDevice(Bolt::Manager *manager, const QString &uid, Bolt::Policy policy, Bolt::AuthFlags authFlags, QJSValue successCb = {}, QJSValue errorCb = {})
{
manager->enrollDevice(uid, policy, authFlags, invoke(successCb), invoke<QString>(errorCb));
}
......@@ -60,12 +60,11 @@ public Q_SLOTS:
}
private:
template<typename ... Args>
std::function<void(Args ...)> invoke(QJSValue cb_)
template<typename... Args> std::function<void(Args...)> invoke(QJSValue cb_)
{
return [cb = std::move(cb_)](Args && ... args) mutable {
return [cb = std::move(cb_)](Args &&... args) mutable {
Q_ASSERT(cb.isCallable());
cb.call({std::forward<Args>(args) ...});
cb.call({std::forward<Args>(args)...});
};
}
};
......@@ -75,19 +74,17 @@ KCMBolt::KCMBolt(QObject *parent, const QVariantList &args)
{
qmlRegisterType<Bolt::DeviceModel>("org.kde.bolt", 0, 1, "DeviceModel");
qmlRegisterType<Bolt::Manager>("org.kde.bolt", 0, 1, "Manager");
qmlRegisterUncreatableType<Bolt::Device>("org.kde.bolt", 0, 1, "Device",
QStringLiteral("Use DeviceModel"));
qmlRegisterUncreatableMetaObject(Bolt::staticMetaObject, "org.kde.bolt", 0, 1, "Bolt",
QStringLiteral("For enums and flags only"));
qmlRegisterSingletonType<QMLHelper>("org.kde.bolt", 0, 1, "QMLHelper",
[](auto, auto) -> QObject* { return new QMLHelper(); });
qmlRegisterUncreatableType<Bolt::Device>("org.kde.bolt", 0, 1, "Device", QStringLiteral("Use DeviceModel"));
qmlRegisterUncreatableMetaObject(Bolt::staticMetaObject, "org.kde.bolt", 0, 1, "Bolt", QStringLiteral("For enums and flags only"));
qmlRegisterSingletonType<QMLHelper>("org.kde.bolt", 0, 1, "QMLHelper", [](auto, auto) -> QObject * {
return new QMLHelper();
});
auto about = std::make_unique<KAboutData>(
QStringLiteral("kcm_bolt"),
i18n("Thunderbolt Device Management"),
QStringLiteral("0.1"),
i18n("System Settings module for managing Thunderbolt devices."),
KAboutLicense::GPL);
auto about = std::make_unique<KAboutData>(QStringLiteral("kcm_bolt"),
i18n("Thunderbolt Device Management"),
QStringLiteral("0.1"),
i18n("System Settings module for managing Thunderbolt devices."),
KAboutLicense::GPL);
about->addAuthor(i18n("Daniel Vrátil"), {}, QStringLiteral("dvratil@kde.org"));
setAboutData(about.release());
}
......
......@@ -30,7 +30,6 @@ class KCMBolt : public KQuickAddons::ConfigModule
public:
KCMBolt(QObject *parent, const QVariantList &args);
~KCMBolt() override = default;
};
#endif
......@@ -25,8 +25,8 @@
#include <QPointer>
#include <KNotification>
#include <KLocalizedString>
#include <KNotification>
#include <chrono>
......@@ -44,32 +44,30 @@ KDEDBolt::KDEDBolt(QObject *parent, const QVariantList &)
mPendingDeviceTimer.setInterval(500ms);
connect(&mPendingDeviceTimer, &QTimer::timeout, this, &KDEDBolt::notify);
connect(&mManager, &Bolt::Manager::deviceAdded,
this, [this](const QSharedPointer<Bolt::Device> &device) {
// Already authorized, nothing else to do here
if (device->status() == Bolt::Status::Authorized) {
return;
}
connect(&mManager, &Bolt::Manager::deviceAdded, this, [this](const QSharedPointer<Bolt::Device> &device) {
// Already authorized, nothing else to do here
if (device->status() == Bolt::Status::Authorized) {
return;
}
mPendingDevices.append(device);
mPendingDeviceTimer.start();
mPendingDevices.append(device);
mPendingDeviceTimer.start();
});
connect(&mManager, &Bolt::Manager::deviceRemoved,
this, [this](const QSharedPointer<Bolt::Device> &device) {
// Check if maybe the device is in pending or currently active
// notification, remove it if so.
mPendingDevices.removeOne(device);
Q_ASSERT(!mPendingDevices.removeOne(device));
for (auto it = mNotifiedDevices.begin(), end = mNotifiedDevices.end(); it != end; ++it) {
if (it->contains(device)) {
auto devices = *it;
devices.removeOne(device);
mPendingDevices += devices;
mPendingDeviceTimer.start();
}
it.key()->close();
}
connect(&mManager, &Bolt::Manager::deviceRemoved, this, [this](const QSharedPointer<Bolt::Device> &device) {
// Check if maybe the device is in pending or currently active
// notification, remove it if so.
mPendingDevices.removeOne(device);
Q_ASSERT(!mPendingDevices.removeOne(device));
for (auto it = mNotifiedDevices.begin(), end = mNotifiedDevices.end(); it != end; ++it) {
if (it->contains(device)) {
auto devices = *it;
devices.removeOne(device);
mPendingDevices += devices;
mPendingDeviceTimer.start();
}
it.key()->close();
}
});
}
......@@ -79,34 +77,28 @@ KDEDBolt::~KDEDBolt()
void KDEDBolt::notify()
{
auto ntf = KNotification::event(
QStringLiteral("unauthorizedDeviceConnected"),
i18n("New Thunderbolt Device Detected"),
mPendingDevices.size() == 1
? i18n("Unauthorized Thunderbolt device <b>%1</b> was detected. Do you want to authorize it?", mPendingDevices.front()->name())
: i18np("%1 unauthorized Thunderbolt device was detected. Do you want to authorize it?",
"%1 unauthorized Thunderbolt devices were detected. Do you want to authorize them?",
mPendingDevices.size()),
/*icon*/ QPixmap{}, /* widget */ nullptr,
KNotification::Persistent,
QStringLiteral("kded_bolt"));
ntf->setActions({
i18n("Authorize Now"),
i18n("Authorize Permanently")
});
auto ntf = KNotification::event(QStringLiteral("unauthorizedDeviceConnected"),
i18n("New Thunderbolt Device Detected"),
mPendingDevices.size() == 1 ? i18n("Unauthorized Thunderbolt device <b>%1</b> was detected. Do you want to authorize it?",
mPendingDevices.front()->name())
: i18np("%1 unauthorized Thunderbolt device was detected. Do you want to authorize it?",
"%1 unauthorized Thunderbolt devices were detected. Do you want to authorize them?",
mPendingDevices.size()),
/*icon*/ QPixmap{},
/* widget */ nullptr,
KNotification::Persistent,
QStringLiteral("kded_bolt"));
ntf->setActions({i18n("Authorize Now"), i18n("Authorize Permanently")});
mNotifiedDevices.insert(ntf, mPendingDevices);
connect(ntf, &KNotification::action1Activated,
this, [this, devices = mPendingDevices]() {
authorizeDevices(sortDevices(devices), Authorize);
});
connect(ntf, &KNotification::action2Activated,
this, [this, devices = mPendingDevices]() {
authorizeDevices(sortDevices(devices), Enroll);
});
connect(ntf, &KNotification::closed,
this, [this, ntf]() {
mNotifiedDevices.remove(ntf);
});
connect(ntf, &KNotification::action1Activated, this, [this, devices = mPendingDevices]() {
authorizeDevices(sortDevices(devices), Authorize);
});
connect(ntf, &KNotification::action2Activated, this, [this, devices = mPendingDevices]() {
authorizeDevices(sortDevices(devices), Enroll);
});
connect(ntf, &KNotification::closed, this, [this, ntf]() {
mNotifiedDevices.remove(ntf);
});
mPendingDevices.clear();
}
......@@ -119,8 +111,12 @@ KDEDBolt::BoltDeviceList KDEDBolt::sortDevices(const BoltDeviceList &devices)
// Sort the devices so that parents go before their children. Probably
// fairly inefficient but there's rarely more than a couple of items.
for (const auto &device : devices) {
auto child = std::find_if(sorted.begin(), sorted.end(), [device](const auto &d) { return d->parent() == device->uid(); });
auto parent = std::find_if(sorted.begin(), child, [device](const auto &d) { return device->parent() == d->uid(); });
auto child = std::find_if(sorted.begin(), sorted.end(), [device](const auto &d) {
return d->parent() == device->uid();
});
auto parent = std::find_if(sorted.begin(), child, [device](const auto &d) {
return device->parent() == d->uid();
});
if (parent != sorted.end()) {
++parent;
}
......@@ -142,13 +138,13 @@ void KDEDBolt::authorizeDevices(BoltDeviceList devices, AuthMode mode)
authorizeDevices(std::move(devices), mode);
};
const auto errCb = [device](const QString &error) {
KNotification::event(
QStringLiteral("deviceAuthError"),
i18n("Thunderbolt Device Authorization Error"),
i18n("Failed to authorize Thunderbolt device <b>%1</b>: %2", device->name().toHtmlEscaped(), error),
/* icon */ QPixmap{}, /* parent */ nullptr,
KNotification::CloseOnTimeout,
QStringLiteral("kded_bolt"));
KNotification::event(QStringLiteral("deviceAuthError"),
i18n("Thunderbolt Device Authorization Error"),
i18n("Failed to authorize Thunderbolt device <b>%1</b>: %2", device->name().toHtmlEscaped(), error),
/* icon */ QPixmap{},
/* parent */ nullptr,
KNotification::CloseOnTimeout,
QStringLiteral("kded_bolt"));
};
if (mode == Enroll) {
mManager.enrollDevice(device->uid(), Bolt::Policy::Auto, Bolt::Auth::Boot | Bolt::Auth::NoKey, okCb, errCb);
......@@ -156,4 +152,3 @@ void KDEDBolt::authorizeDevices(BoltDeviceList devices, AuthMode mode)
device->authorize(Bolt::Auth::Boot | Bolt::Auth::NoKey, okCb, errCb);
}
}