summaryrefslogtreecommitdiff
path: root/TelepathyQt4/optional-interface-factory.cpp
blob: 50eb68da9c629a05b6496a2bdddeff208613f02d (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
/*
 * This file is part of TelepathyQt4
 *
 * Copyright (C) 2008-2009 Collabora Ltd. <http://www.collabora.co.uk/>
 * Copyright (C) 2008-2009 Nokia Corporation
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <TelepathyQt4/OptionalInterfaceFactory>

#include <TelepathyQt4/AbstractInterface>

#include "TelepathyQt4/debug-internal.h"

#include <QMap>
#include <QString>

namespace Tp
{

/**
 * \enum OptionalInterfaceFactory::InterfaceSupportedChecking
 *
 * Specifies if the interface being supported by the remote object should be
 * checked by optionalInterface() and the convenience functions for it.
 *
 * \sa optionalInterface()
 */

/**
 * \var OptionalInterfaceFactory::InterfaceSupportedChecking OptionalInterfaceFactory::CheckInterfaceSupported
 *
 * Don't return an interface instance unless it can be guaranteed that the
 * remote object actually implements the interface.
 */

/**
 * \var OptionalInterfaceFactory::InterfaceSupportedChecking OptionalInterfaceFactory::BypassInterfaceCheck
 *
 * Return an interface instance even if it can't be verified that the remote
 * object supports the interface.
 */

struct TELEPATHY_QT4_NO_EXPORT OptionalInterfaceCache::Private
{
    QObject *proxy;
    QMap<QString, AbstractInterface*> interfaces;

    Private(QObject *proxy);
};

OptionalInterfaceCache::Private::Private(QObject *proxy)
    : proxy(proxy)
{
}

/**
 * Class constructor.
 */
OptionalInterfaceCache::OptionalInterfaceCache(QObject *proxy)
    : mPriv(new Private(proxy))
{
}

/**
 * Class destructor.
 *
 * Frees all interface instances constructed by this factory.
 */
OptionalInterfaceCache::~OptionalInterfaceCache()
{
    delete mPriv;
}

QObject *OptionalInterfaceCache::proxy() const
{
    return mPriv->proxy;
}

AbstractInterface *OptionalInterfaceCache::getCached(const QString &name) const
{
    if (mPriv->interfaces.contains(name)) {
        return mPriv->interfaces.value(name);
    } else {
        return 0;
    }
}

void OptionalInterfaceCache::cache(AbstractInterface *interface) const
{
    QString name = interface->interface();
    Q_ASSERT(!mPriv->interfaces.contains(name));

    mPriv->interfaces[name] = interface;
}

/**
 * \class OptionalInterfaceFactory
 * \ingroup clientsideproxies
 * \headerfile TelepathyQt4/optional-interface-factory.h <TelepathyQt4/OptionalInterfaceFactory>
 *
 * \brief Implementation helper class for high-level proxy classes willing to
 * offer access to shared instances of interface proxies for optional
 * interfaces.
 *
 * This class is included in the public API for the benefit of high-level
 * proxies in extensions.
 *
 * To use this helper in a subclass of DBusProxy (say, ExampleObject),
 * ExampleObject should inherit privately from
 * OptionalInterfaceFactory<ExampleObject>, and call
 * OptionalInterfaceFactory(this) in its constructor's initialization list.
 *
 * \tparam DBusProxySubclass A subclass of DBusProxy
 */

/**
 * \fn OptionalInterfaceFactory::OptionalInterfaceFactory(DBusProxySubclass *this_)
 *
 * Class constructor.
 *
 * \param this_ The class to which this OptionalInterfaceFactory is
 *              attached
 */

/**
 * \fn OptionalInterfaceFactory::~OptionalInterfaceFactory()
 *
 * Class destructor.
 *
 * Frees all interface instances constructed by this factory.
 */

 /**
  * \fn OptionalInterfaceFactory::interfaces() const;
  *
  * Return a list of interfaces supported by this object.
  *
  * \return List of supported interfaces.
  */

/**
 * \fn template <typename Interface> inline Interface *OptionalInterfaceFactory::interface() const
 *
 * Return a pointer to a valid instance of a interface class, associated
 * with the same remote object as the given main interface instance. The
 * given main interface must be of the class the optional interface is
 * generated for (for eg. ChannelInterfaceGroupInterface this means
 * ChannelInterface) or a subclass.
 *
 * First invocation of this method for a particular optional interface
 * class will construct the instance; subsequent calls will return a
 * pointer to the same instance.
 *
 * The returned instance is freed when the factory is destroyed; using
 * it after destroying the factory will likely produce a crash. As the
 * instance is shared, it should not be freed directly.
 *
 * \tparam Interface Class of the interface instance to get.
 * \return A pointer to an optional interface instance.
 */

} // Tp