| 1 | /* |
| 2 | * This file is part of OnlineAccountsDaemon |
| 3 | * |
| 4 | * Copyright (C) 2015 Canonical Ltd. |
| 5 | * |
| 6 | * Contact: Alberto Mardegan <[email protected]> |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU Lesser General Public License version 3, as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, but |
| 13 | * WITHOUT ANY WARRANTY; without even the implied warranties of |
| 14 | * MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 15 | * PURPOSE. See the GNU Lesser General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU Lesser General Public License |
| 18 | * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "manager_adaptor.h" |
| 22 | |
| 23 | #include <QDBusMetaType> |
| 24 | #include <QDebug> |
| 25 | #include "client_registry.h" |
| 26 | |
| 27 | using namespace OnlineAccountsDaemon; |
| 28 | |
| 29 | QDBusArgument &operator<<(QDBusArgument &argument, const AccountInfo &info) |
| 30 | { |
| 31 | argument.beginStructure(); |
| 32 | argument << info.accountId << info.details; |
| 33 | argument.endStructure(); |
| 34 | return argument; |
| 35 | } |
| 36 | |
| 37 | const QDBusArgument &operator>>(const QDBusArgument &argument, |
| 38 | AccountInfo &info) |
| 39 | { |
| 40 | argument.beginStructure(); |
| 41 | argument >> info.accountId >> info.details; |
| 42 | argument.endStructure(); |
| 43 | return argument; |
| 44 | } |
| 45 | |
| 46 | CallContext::CallContext(QDBusContext *dbusContext): |
| 47 | m_connection(dbusContext->connection()), |
| 48 | m_message(dbusContext->message()) |
| 49 | { |
| 50 | CallContextCounter::instance()->addContext(*this); |
| 51 | } |
| 52 | |
| 53 | CallContext::CallContext(const CallContext &other): |
| 54 | m_connection(other.m_connection), |
| 55 | m_message(other.m_message) |
| 56 | { |
| 57 | CallContextCounter::instance()->addContext(*this); |
| 58 | } |
| 59 | |
| 60 | CallContext::~CallContext() |
| 61 | { |
| 62 | CallContextCounter::instance()->removeContext(*this); |
| 63 | } |
| 64 | |
| 65 | void CallContext::setDelayedReply(bool delayed) |
| 66 | { |
| 67 | m_message.setDelayedReply(delayed); |
| 68 | } |
| 69 | |
| 70 | void CallContext::sendReply(const QList<QVariant> &args) const |
| 71 | { |
| 72 | m_connection.send(m_message.createReply(args)); |
| 73 | } |
| 74 | |
| 75 | void CallContext::sendError(const QString &name, const QString &message) const |
| 76 | { |
| 77 | m_connection.send(m_message.createErrorReply(name, message)); |
| 78 | } |
| 79 | |
| 80 | QString CallContext::securityContext() const |
| 81 | { |
| 82 | ClientRegistry *clientRegistry = ClientRegistry::instance(); |
| 83 | QString client = clientRegistry->registerClient(m_connection, m_message); |
| 84 | return clientRegistry->clientSecurityContext(client); |
| 85 | } |
| 86 | |
| 87 | pid_t CallContext::clientPid() const |
| 88 | { |
| 89 | ClientRegistry *clientRegistry = ClientRegistry::instance(); |
| 90 | return clientRegistry->clientPid(m_message.service()); |
| 91 | } |
| 92 | |
| 93 | QString CallContext::clientName() const |
| 94 | { |
| 95 | return m_message.service(); |
| 96 | } |
| 97 | |
| 98 | static CallContextCounter *m_callContextCounterInstance = 0; |
| 99 | |
| 100 | CallContextCounter::CallContextCounter(): |
| 101 | QObject(), |
| 102 | m_contexts(0) |
| 103 | { |
| 104 | } |
| 105 | |
| 106 | CallContextCounter *CallContextCounter::instance() |
| 107 | { |
| 108 | if (!m_callContextCounterInstance) { |
| 109 | m_callContextCounterInstance = new CallContextCounter; |
| 110 | } |
| 111 | return m_callContextCounterInstance; |
| 112 | } |
| 113 | |
| 114 | void CallContextCounter::addContext(const CallContext &) |
| 115 | { |
| 116 | m_contexts++; |
| 117 | Q_EMIT activeContextsChanged(); |
| 118 | } |
| 119 | |
| 120 | void CallContextCounter::removeContext(const CallContext &) |
| 121 | { |
| 122 | m_contexts--; |
| 123 | Q_EMIT activeContextsChanged(); |
| 124 | } |
| 125 | |
| 126 | namespace OnlineAccountsDaemon { |
| 127 | |
| 128 | class ManagerAdaptorPrivate { |
| 129 | public: |
| 130 | ManagerAdaptorPrivate(); |
| 131 | |
| 132 | private: |
| 133 | friend class ManagerAdaptor; |
| 134 | }; |
| 135 | |
| 136 | } // namespace |
| 137 | |
| 138 | ManagerAdaptorPrivate::ManagerAdaptorPrivate() |
| 139 | { |
| 140 | } |
| 141 | |
| 142 | ManagerAdaptor::ManagerAdaptor(Manager *parent): |
| 143 | QDBusAbstractAdaptor(parent), |
| 144 | d_ptr(new ManagerAdaptorPrivate) |
| 145 | { |
| 146 | qRegisterMetaType<AccountInfo>("AccountInfo"); |
| 147 | qRegisterMetaType<QList<AccountInfo> >("QList<AccountInfo>"); |
| 148 | qDBusRegisterMetaType<AccountInfo>(); |
| 149 | qDBusRegisterMetaType<QList<AccountInfo>>(); |
| 150 | qDBusRegisterMetaType<QList<QVariantMap>>(); |
| 151 | |
| 152 | setAutoRelaySignals(false); |
| 153 | } |
| 154 | |
| 155 | ManagerAdaptor::~ManagerAdaptor() |
| 156 | { |
| 157 | delete d_ptr; |
| 158 | } |
| 159 | |
| 160 | void ManagerAdaptor::notifyAccountChange(const AccountInfo &info, |
| 161 | uint changeType) |
| 162 | { |
| 163 | AccountInfo copy(info); |
| 164 | copy.details[ONLINE_ACCOUNTS_INFO_KEY_CHANGE_TYPE] = changeType; |
| 165 | Q_EMIT AccountChanged(copy.serviceId(), copy); |
| 166 | } |
| 167 | |
| 168 | QVariantMap ManagerAdaptor::Authenticate(uint accountId, |
| 169 | const QString &serviceId, |
| 170 | bool interactive, bool invalidate, |
| 171 | const QVariantMap ¶meters) |
| 172 | { |
| 173 | parent()->authenticate(accountId, serviceId, |
| 174 | interactive, invalidate, parameters, |
| 175 | CallContext(dbusContext())); |
| 176 | return QVariantMap(); |
| 177 | } |
| 178 | |
| 179 | void ManagerAdaptor::GetAccounts(const QVariantMap &filters, |
| 180 | QList<AccountInfo> &accounts, |
| 181 | QList<QVariantMap> &services) |
| 182 | { |
| 183 | accounts = parent()->getAccounts(filters, |
| 184 | CallContext(dbusContext()), |
| 185 | services); |
| 186 | } |
| 187 | |
| 188 | AccountInfo ManagerAdaptor::RequestAccess(const QString &serviceId, |
| 189 | const QVariantMap ¶meters, |
| 190 | QVariantMap &credentials) |
| 191 | { |
| 192 | parent()->requestAccess(serviceId, parameters, |
| 193 | CallContext(dbusContext())); |
| 194 | credentials = QVariantMap(); |
| 195 | return AccountInfo(); |
| 196 | } |
| 197 | |
| 198 | #include "manager_adaptor.moc" |