Debian Codegraph @

src/fdosecrets/dbus/DBusObject.h

raw
1 /*
2 * Copyright (C) 2018 Aetf <[email protected]>
3 *
4 * This program is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 or (at your option)
7 * version 3 of the License.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18 #ifndef KEEPASSXC_FDOSECRETS_DBUSOBJECT_H
19 #define KEEPASSXC_FDOSECRETS_DBUSOBJECT_H
20
21 #include "DBusConstants.h"
22 #include "DBusMgr.h"
23 #include "DBusTypes.h"
24
25 #include <QDBusError>
26
27 #ifndef Q_MOC_RUN
28 // define the tag text as empty, so the compiler doesn't see it
29 #define DBUS_PROPERTY
30 #endif // #ifndef Q_MOC_RUN
31
32 namespace FdoSecrets
33 {
34 class Service;
35
36 /**
37 * @brief A common base class for all dbus-exposed objects.
38 */
39 class DBusObject : public QObject
40 {
41 Q_OBJECT
42 public:
43 ~DBusObject() override;
44
45 const QDBusObjectPath& objectPath() const
46 {
47 return m_objectPath;
48 }
49
50 const QSharedPointer<DBusMgr>& dbus() const
51 {
52 return m_dbus;
53 }
54
55 signals:
56 /**
57 * @brief Necessary because by the time QObject::destroyed is emitted,
58 * we already lost any info in DBusObject
59 */
60 void destroyed(DBusObject* self);
61
62 protected:
63 explicit DBusObject(DBusObject* parent);
64 explicit DBusObject(QSharedPointer<DBusMgr> dbus);
65
66 private:
67 friend class DBusMgr;
68 void setObjectPath(const QString& path);
69
70 QDBusObjectPath m_objectPath;
71 QSharedPointer<DBusMgr> m_dbus;
72 };
73
74 /**
75 * @brief A dbus error or not
76 */
77 class DBusResult : public QString
78 {
79 public:
80 DBusResult() = default;
81 explicit DBusResult(QString error)
82 : QString(std::move(error))
83 {
84 }
85
86 // Implicitly convert from QDBusError
87 DBusResult(QDBusError::ErrorType error) // NOLINT(google-explicit-constructor)
88 : QString(error == QDBusError::ErrorType::NoError ? "" : QDBusError::errorString(error))
89 {
90 }
91
92 bool ok() const
93 {
94 return isEmpty();
95 }
96 bool err() const
97 {
98 return !isEmpty();
99 }
100 void okOrDie() const
101 {
102 Q_ASSERT(ok());
103 }
104 };
105
106 /**
107 * Encode the string value to a DBus object path safe representation,
108 * using a schema similar to URI encoding, but with percentage(%) replaced with
109 * underscore(_). All characters except [A-Za-z0-9] are encoded. For non-ascii
110 * characters, UTF-8 encoding is first applied and each of the resulting byte
111 * value is encoded.
112 * @param value
113 * @return encoded string
114 */
115 QString encodePath(const QString& value);
116
117 } // namespace FdoSecrets
118
119 Q_DECLARE_METATYPE(FdoSecrets::DBusResult);
120
121 #endif // KEEPASSXC_FDOSECRETS_DBUSOBJECT_H