1 /**
2  * This file is part of TelepathyQt
3  *
4  * @copyright Copyright (C) 2010 Collabora Ltd. <http://www.collabora.co.uk/>
5  * @copyright Copyright (C) 2010 Nokia Corporation
6  * @license LGPL 2.1
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2.1 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
21  */
22 
23 #include <TelepathyQt/AccountFactory>
24 
25 #include "TelepathyQt/_gen/account-factory.moc.hpp"
26 
27 #include <TelepathyQt/Account>
28 
29 namespace Tp
30 {
31 
32 /**
33  * \class AccountFactory
34  * \ingroup utils
35  * \headerfile TelepathyQt/account-factory.h <TelepathyQt/AccountFactory>
36  *
37  * \brief The AccountFactory class is responsible for constructing Account
38  * objects according to application-defined settings.
39  *
40  * The class is used by AccountManager and other classes which construct Account
41  * proxy instances to enable sharing instances of application-defined Account
42  * subclasses with certain features always ready.
43  */
44 
45 /**
46  * Create a new AccountFactory object.
47  *
48  * Optionally, the \a features to make ready on all constructed proxies can be specified. The
49  * default is to make no features ready. It should be noted that unlike Account::becomeReady(),
50  * FeatureCore isn't assumed. If no features are specified, which is the default behavior, no
51  * Account::becomeReady() call is made at all and the proxy won't be Account::isReady().
52  *
53  * \param bus The QDBusConnection for proxies constructed using this factory to use.
54  * \param features The features to make ready on constructed Accounts.
55  * \return An AccountFactoryPtr object pointing to the newly created
56  *         AccountFactory object.
57  */
create(const QDBusConnection & bus,const Features & features)58 AccountFactoryPtr AccountFactory::create(const QDBusConnection &bus, const Features &features)
59 {
60     return AccountFactoryPtr(new AccountFactory(bus, features));
61 }
62 
63 /**
64  * Construct a new AccountFactory object.
65  *
66  * As in create(), it should be noted that unlike Account::becomeReady(), FeatureCore isn't assumed.
67  * If no \a features are specified, no Account::becomeReady() call is made at all and the proxy
68  * won't be Account::isReady().
69  *
70  * \param bus The QDBusConnection for proxies constructed using this factory to use.
71  * \param features The features to make ready on constructed Accounts.
72  */
AccountFactory(const QDBusConnection & bus,const Features & features)73 AccountFactory::AccountFactory(const QDBusConnection &bus, const Features &features)
74     : FixedFeatureFactory(bus)
75 {
76     addFeatures(features);
77 }
78 
79 /**
80  * Class destructor.
81  */
~AccountFactory()82 AccountFactory::~AccountFactory()
83 {
84 }
85 
86 /**
87  * Constructs an Account proxy and begins making it ready.
88  *
89  * If a valid proxy already exists in the factory cache for the given combination of \a busName and
90  * \a objectPath, it is returned instead. All newly created proxies are automatically cached until
91  * they're either DBusProxy::invalidated() or the last reference to them outside the factory has
92  * been dropped.
93  *
94  * The proxy can be accessed immediately after this function returns using PendingReady::proxy().
95  * The ready operation only finishes, however, when the features specified by features(), if any,
96  * are made ready as much as possible. If the service doesn't support a given feature, they won't
97  * obviously be ready even if the operation finished successfully, as is the case for
98  * Account::becomeReady().
99  *
100  * \param busName The bus/service name of the D-Bus account object the proxy is constructed for.
101  * (Usually #TP_QT_ACCOUNT_MANAGER_BUS_NAME).
102  * \param objectPath The object path of the account.
103  * \param connFactory The connection factory to use for the Account.
104  * \param chanFactory The channel factory to use for the Account.
105  * \param contactFactory The channel factory to use for the Account.
106  * \return A PendingReady operation with the proxy in PendingReady::proxy().
107  */
proxy(const QString & busName,const QString & objectPath,const ConnectionFactoryConstPtr & connFactory,const ChannelFactoryConstPtr & chanFactory,const ContactFactoryConstPtr & contactFactory) const108 PendingReady *AccountFactory::proxy(const QString &busName, const QString &objectPath,
109             const ConnectionFactoryConstPtr &connFactory,
110             const ChannelFactoryConstPtr &chanFactory,
111             const ContactFactoryConstPtr &contactFactory) const
112 {
113     DBusProxyPtr proxy = cachedProxy(busName, objectPath);
114     if (proxy.isNull()) {
115         proxy = construct(busName, objectPath, connFactory, chanFactory, contactFactory);
116     }
117 
118     return nowHaveProxy(proxy);
119 }
120 
121 /**
122  * Can be used by subclasses to override the Account subclass constructed by the factory.
123  *
124  * This is automatically called by proxy() to construct proxy instances if no valid cached proxy is
125  * found.
126  *
127  * The default implementation constructs Tp::Account objects.
128  *
129  * \param busName The bus/service name of the D-Bus account object the proxy is constructed for.
130  * (Usually #TP_QT_ACCOUNT_MANAGER_BUS_NAME).
131  * \param objectPath The object path of the account.
132  * \param connFactory The connection factory to use for the Account.
133  * \param chanFactory The channel factory to use for the Account.
134  * \param contactFactory The channel factory to use for the Account.
135  * \return A pointer to the constructed Account object.
136  */
construct(const QString & busName,const QString & objectPath,const ConnectionFactoryConstPtr & connFactory,const ChannelFactoryConstPtr & chanFactory,const ContactFactoryConstPtr & contactFactory) const137 AccountPtr AccountFactory::construct(const QString &busName, const QString &objectPath,
138         const ConnectionFactoryConstPtr &connFactory,
139         const ChannelFactoryConstPtr &chanFactory,
140         const ContactFactoryConstPtr &contactFactory) const
141 {
142     return Account::create(dbusConnection(), busName, objectPath, connFactory, chanFactory,
143             contactFactory);
144 }
145 
146 /**
147  * Identity transform, as is appropriate for Account objects.
148  *
149  * \param uniqueOrWellKnown The name to transform.
150  * \return \a uniqueOrWellKnown
151  */
finalBusNameFrom(const QString & uniqueOrWellKnown) const152 QString AccountFactory::finalBusNameFrom(const QString &uniqueOrWellKnown) const
153 {
154     return uniqueOrWellKnown;
155 }
156 
157 }
158