Welcome to mirror list, hosted at ThFree Co, Russian Federation.

DBusObject.h « objects « fdosecrets « src - github.com/keepassxreboot/keepassxc.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 4cdaf5ced1c8a6f4d070321bcb3da794cbc247f8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
/*
 *  Copyright (C) 2018 Aetf <aetf@unlimitedcodeworks.xyz>
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 or (at your option)
 *  version 3 of the License.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#ifndef KEEPASSXC_FDOSECRETS_DBUSOBJECT_H
#define KEEPASSXC_FDOSECRETS_DBUSOBJECT_H

#include "fdosecrets/objects/DBusReturn.h"
#include "fdosecrets/objects/DBusTypes.h"

#include <QDBusConnection>
#include <QDBusConnectionInterface>
#include <QDBusContext>
#include <QDBusObjectPath>
#include <QDebug>
#include <QList>
#include <QMetaProperty>
#include <QObject>
#include <QScopedPointer>

class QDBusAbstractAdaptor;

namespace FdoSecrets
{
    class Service;

    /**
     * @brief A common base class for all dbus-exposed objects
     */
    class DBusObject : public QObject, public QDBusContext
    {
        Q_OBJECT
    public:
        explicit DBusObject(DBusObject* parent = nullptr);

        const QDBusObjectPath& objectPath() const
        {
            return m_objectPath;
        }

        QDBusAbstractAdaptor& dbusAdaptor() const
        {
            return *m_dbusAdaptor;
        }

    protected:
        void registerWithPath(const QString& path, QDBusAbstractAdaptor* adaptor);

        void unregisterCurrentPath()
        {
            QDBusConnection::sessionBus().unregisterObject(m_objectPath.path());
            m_dbusAdaptor = nullptr;
            m_objectPath.setPath(QStringLiteral("/"));
        }

        QString callingPeer() const
        {
            Q_ASSERT(calledFromDBus());
            return message().service();
        }

        uint callingPeerPid() const
        {
            return connection().interface()->servicePid(callingPeer());
        }

        QString callingPeerName() const;

        DBusObject* p() const
        {
            return qobject_cast<DBusObject*>(parent());
        }

    private:
        /**
         * Derived class should not directly use sendErrorReply.
         * Instead, use raiseError
         */
        using QDBusContext::sendErrorReply;

        template <typename U> friend class DBusReturn;

    private:
        QDBusAbstractAdaptor* m_dbusAdaptor;
        QDBusObjectPath m_objectPath;
    };

    /**
     * Return the object path of the pointed DBusObject, or "/" if the pointer is null
     * @tparam T
     * @param object
     * @return
     */
    template <typename T> QDBusObjectPath objectPathSafe(T* object)
    {
        if (object) {
            return object->objectPath();
        }
        return QDBusObjectPath(QStringLiteral("/"));
    }

    /**
     * Convert a list of DBusObjects to object path
     * @tparam T
     * @param objects
     * @return
     */
    template <typename T> QList<QDBusObjectPath> objectsToPath(QList<T*> objects)
    {
        QList<QDBusObjectPath> res;
        res.reserve(objects.size());
        for (auto object : objects) {
            res.append(objectPathSafe(object));
        }
        return res;
    }

    /**
     * Convert an object path to a pointer of the object
     * @tparam T
     * @param path
     * @return the pointer of the object, or nullptr if path is "/"
     */
    template <typename T> T* pathToObject(const QDBusObjectPath& path)
    {
        if (path.path() == QStringLiteral("/")) {
            return nullptr;
        }
        return qobject_cast<T*>(QDBusConnection::sessionBus().objectRegisteredAt(path.path()));
    }

    /**
     * Convert a list of object paths to a list of objects.
     * "/" paths (i.e. nullptrs) will be skipped in the resulting list
     * @tparam T
     * @param paths
     * @return
     */
    template <typename T> QList<T*> pathsToObject(const QList<QDBusObjectPath>& paths)
    {
        QList<T*> res;
        res.reserve(paths.size());
        for (const auto& path : paths) {
            auto object = pathToObject<T>(path);
            if (object) {
                res.append(object);
            }
        }
        return res;
    }

    /**
     * Encode the string value to a DBus object path safe representation,
     * using a schema similar to URI encoding, but with percentage(%) replaced with
     * underscore(_). All characters except [A-Za-z0-9] are encoded. For non-ascii
     * characters, UTF-8 encoding is first applied and each of the resulting byte
     * value is encoded.
     * @param value
     * @return encoded string
     */
    QString encodePath(const QString& value);

} // namespace FdoSecrets

#endif // KEEPASSXC_FDOSECRETS_DBUSOBJECT_H