File indexing completed on 2024-11-24 03:41:06

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 2022 Slava Aseev <nullptrnine@basealt.ru>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-or-later
0006 */
0007 #ifndef _KWALLETDBUSCONTEXT_H_
0008 #define _KWALLETDBUSCONTEXT_H_
0009 
0010 #include "qdbuserror.h"
0011 #include <QDBusConnection>
0012 #include <QDBusContext>
0013 #include <QDBusMessage>
0014 
0015 /* FDO_DBUS_CONTEXT macro will be replaced by KWalletDBusContextDummy during
0016  * preprocessing if FDO_ENABLE_DUMMY_MESSAGE_CONNECTION was defined,
0017  * otherwise we get QDBusContext.
0018  *
0019  * This is used for mocking QDBusContext in autotests.
0020  *
0021  * QDBusContext's connection() and message() member functions can't be called
0022  * without a real connection context (this cause segfault).
0023  * So we need to use KWalletDBusContextDummy in case some DBus-related
0024  * member functions may call connection()/message().
0025  *
0026  * This header defines FDO_DBUS_CONTEXT macro that should be used instead of
0027  * QDBusContext in all DBus-related which we want to use in autotests.
0028  */
0029 
0030 #ifdef FDO_ENABLE_DUMMY_MESSAGE_CONNECTION
0031 
0032 class KWalletDBusContextDummy : public QDBusContext
0033 {
0034 public:
0035     const QDBusMessage &message()
0036     {
0037         static auto msg = QDBusMessage::createSignal(QStringLiteral("dummy"), QStringLiteral("dummy"), QStringLiteral("dummy"));
0038         return msg;
0039     }
0040     QDBusConnection connection() const
0041     {
0042         return QDBusConnection::sessionBus();
0043     }
0044 };
0045 
0046 #define FDO_DBUS_CONTEXT KWalletDBusContextDummy
0047 
0048 #else
0049 
0050 #define FDO_DBUS_CONTEXT QDBusContext
0051 
0052 #endif
0053 
0054 #endif // _KWALLETDBUSCONTEXT_H_