| 1 | /* |
| 2 | * This file is part of libOnlineAccounts |
| 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_p.h" |
| 22 | |
| 23 | #include <QDBusMessage> |
| 24 | #include <QDBusPendingCallWatcher> |
| 25 | #include <QDebug> |
| 26 | #include "pending_call_p.h" |
| 27 | |
| 28 | using namespace OnlineAccounts; |
| 29 | |
| 30 | namespace OnlineAccounts { |
| 31 | |
| 32 | class RequestAccessReplyPrivate |
| 33 | { |
| 34 | public: |
| 35 | inline RequestAccessReplyPrivate(const PendingCall &call); |
| 36 | virtual ~RequestAccessReplyPrivate() {} |
| 37 | |
| 38 | Account *account(); |
| 39 | |
| 40 | private: |
| 41 | friend class RequestAccessReply; |
| 42 | PendingCall m_pendingCall; |
| 43 | AccountInfo m_accountInfo; |
| 44 | Error m_error; |
| 45 | }; |
| 46 | |
| 47 | } // namespace |
| 48 | |
| 49 | RequestAccessReplyPrivate::RequestAccessReplyPrivate(const PendingCall &call): |
| 50 | m_pendingCall(call) |
| 51 | { |
| 52 | const PendingCallPrivate *pCall = call.d.constData(); |
| 53 | |
| 54 | if (!m_pendingCall.isFinished()) { |
| 55 | m_pendingCall.waitForFinished(); |
| 56 | } |
| 57 | |
| 58 | if (Q_UNLIKELY(pCall->dbusCall().isError())) { |
| 59 | /* Treat all errors as permission denied. */ |
| 60 | qCWarning(DBG_ONLINE_ACCOUNTS) << "Error:" << |
| 61 | pCall->dbusCall().error().message(); |
| 62 | m_error = Error(Error::PermissionDenied, |
| 63 | pCall->dbusCall().error().message()); |
| 64 | return; |
| 65 | } |
| 66 | |
| 67 | PendingCallPrivate::InvokedMethod invokedMethod = pCall->invokedMethod(); |
| 68 | if (Q_UNLIKELY(invokedMethod != PendingCallPrivate::RequestAccess)) { |
| 69 | qFatal("Wrong invoked method %d", invokedMethod); |
| 70 | } |
| 71 | QDBusPendingReply<AccountInfo,QVariantMap> reply(pCall->dbusCall()); |
| 72 | m_accountInfo = reply.argumentAt<0>(); |
| 73 | } |
| 74 | |
| 75 | Account *RequestAccessReplyPrivate::account() |
| 76 | { |
| 77 | ManagerPrivate *managerPriv = m_pendingCall.d->manager()->d_ptr; |
| 78 | return managerPriv->ensureAccount(m_accountInfo); |
| 79 | } |
| 80 | |
| 81 | RequestAccessReply::RequestAccessReply(const PendingCall &call): |
| 82 | d_ptr(new RequestAccessReplyPrivate(call)) |
| 83 | { |
| 84 | } |
| 85 | |
| 86 | RequestAccessReply::~RequestAccessReply() |
| 87 | { |
| 88 | delete d_ptr; |
| 89 | } |
| 90 | |
| 91 | Error RequestAccessReply::error() const |
| 92 | { |
| 93 | Q_D(const RequestAccessReply); |
| 94 | return d->m_error; |
| 95 | } |
| 96 | |
| 97 | Account *RequestAccessReply::account() |
| 98 | { |
| 99 | Q_D(RequestAccessReply); |
| 100 | return d->account(); |
| 101 | } |