| 1 | /* |
| 2 | * Copyright (C) 2013 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of lomiri-online-accounts-ui |
| 7 | * |
| 8 | * This program is free software: you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public License version 3, as published |
| 10 | * 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 General Public License for more details. |
| 16 | * |
| 17 | * You should have received a copy of the GNU General Public License along |
| 18 | * with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | */ |
| 20 | |
| 21 | #include "access-model.h" |
| 22 | #include <online-accounts-common/debug.h> |
| 23 | #include <online-accounts-common/globals.h> |
| 24 | #include "provider-request.h" |
| 25 | |
| 26 | #include <LomiriOnlineAccountsPlugin/account-manager.h> |
| 27 | #include <LomiriOnlineAccountsPlugin/application-manager.h> |
| 28 | #include <QQmlContext> |
| 29 | #include <QQmlEngine> |
| 30 | #include <QQuickItem> |
| 31 | #include <QQuickView> |
| 32 | #include <QStandardPaths> |
| 33 | |
| 34 | using namespace OnlineAccountsUi; |
| 35 | |
| 36 | static bool firstTime = true; |
| 37 | |
| 38 | namespace OnlineAccountsUi { |
| 39 | |
| 40 | class ProviderRequestPrivate: public QObject |
| 41 | { |
| 42 | Q_OBJECT |
| 43 | Q_DECLARE_PUBLIC(ProviderRequest) |
| 44 | Q_PROPERTY(QVariantMap application READ applicationInfo CONSTANT) |
| 45 | Q_PROPERTY(QVariantMap provider READ providerInfo CONSTANT) |
| 46 | |
| 47 | public: |
| 48 | ProviderRequestPrivate(ProviderRequest *request); |
| 49 | ~ProviderRequestPrivate(); |
| 50 | |
| 51 | void start(); |
| 52 | |
| 53 | QVariantMap applicationInfo() const { return m_applicationInfo; } |
| 54 | QVariantMap providerInfo() const { return m_providerInfo; } |
| 55 | |
| 56 | public Q_SLOTS: |
| 57 | void deny(); |
| 58 | void allow(int accountId); |
| 59 | |
| 60 | private Q_SLOTS: |
| 61 | void onWindowVisibleChanged(bool visible); |
| 62 | |
| 63 | private: |
| 64 | mutable ProviderRequest *q_ptr; |
| 65 | QQuickView *m_view; |
| 66 | QVariantMap m_applicationInfo; |
| 67 | QVariantMap m_providerInfo; |
| 68 | }; |
| 69 | |
| 70 | } // namespace |
| 71 | |
| 72 | ProviderRequestPrivate::ProviderRequestPrivate(ProviderRequest *request): |
| 73 | QObject(request), |
| 74 | q_ptr(request), |
| 75 | m_view(0) |
| 76 | { |
| 77 | if (firstTime) { |
| 78 | qmlRegisterType<AccessModel>("Lomiri.OnlineAccounts.Internal", |
| 79 | 1, 0, "AccessModel"); |
| 80 | firstTime = false; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | ProviderRequestPrivate::~ProviderRequestPrivate() |
| 85 | { |
| 86 | delete m_view; |
| 87 | } |
| 88 | |
| 89 | void ProviderRequestPrivate::start() |
| 90 | { |
| 91 | Q_Q(ProviderRequest); |
| 92 | |
| 93 | QString applicationId = |
| 94 | q->parameters().value(OAU_KEY_APPLICATION).toString(); |
| 95 | ApplicationManager *appManager = ApplicationManager::instance(); |
| 96 | m_applicationInfo = |
| 97 | appManager->applicationInfo(applicationId, |
| 98 | q->clientApparmorProfile()); |
| 99 | if (Q_UNLIKELY(m_applicationInfo.isEmpty())) { |
| 100 | q->fail(OAU_ERROR_INVALID_APPLICATION, |
| 101 | QStringLiteral("Invalid client application")); |
| 102 | return; |
| 103 | } |
| 104 | |
| 105 | QString providerId; |
| 106 | QString serviceId = q->parameters().value(OAU_KEY_SERVICE_ID).toString(); |
| 107 | if (!serviceId.isEmpty()) { |
| 108 | Accounts::Service service = |
| 109 | AccountManager::instance()->service(serviceId); |
| 110 | if (Q_UNLIKELY(!service.isValid())) { |
| 111 | q->fail(OAU_ERROR_INVALID_SERVICE, |
| 112 | QString("Service %1 not found").arg(serviceId)); |
| 113 | return; |
| 114 | } |
| 115 | providerId = service.provider(); |
| 116 | } else { |
| 117 | providerId = q->parameters().value(OAU_KEY_PROVIDER).toString(); |
| 118 | } |
| 119 | m_providerInfo = appManager->providerInfo(providerId); |
| 120 | |
| 121 | m_view = new QQuickView; |
| 122 | QObject::connect(m_view, SIGNAL(visibleChanged(bool)), |
| 123 | this, SLOT(onWindowVisibleChanged(bool))); |
| 124 | m_view->setResizeMode(QQuickView::SizeRootObjectToView); |
| 125 | QString mountPoint = q->mountPoint(); |
| 126 | QQmlEngine *engine = m_view->engine(); |
| 127 | engine->addImportPath(mountPoint + PLUGIN_PRIVATE_MODULE_DIR); |
| 128 | |
| 129 | /* If the plugin comes from a click package, also add |
| 130 | * <package-dir>/lib |
| 131 | * <package-dir>/lib/<DEB_HOST_MULTIARCH> |
| 132 | * to the QML import path. |
| 133 | */ |
| 134 | QString packageDir = m_providerInfo.value("package-dir").toString(); |
| 135 | if (!packageDir.isEmpty()) { |
| 136 | engine->addImportPath(packageDir + "/lib"); |
| 137 | #ifdef DEB_HOST_MULTIARCH |
| 138 | engine->addImportPath(packageDir + "/lib/" DEB_HOST_MULTIARCH); |
| 139 | #endif |
| 140 | } |
| 141 | |
| 142 | QQmlContext *context = m_view->rootContext(); |
| 143 | |
| 144 | context->setContextProperty("systemQmlPluginPath", |
| 145 | QUrl::fromLocalFile(mountPoint + OAU_PLUGIN_DIR)); |
| 146 | context->setContextProperty("localQmlPluginPath", |
| 147 | QUrl::fromLocalFile(QStandardPaths::writableLocation( |
| 148 | QStandardPaths::GenericDataLocation) + |
| 149 | "/accounts/qml-plugins/")); |
| 150 | context->setContextProperty("request", this); |
| 151 | context->setContextProperty("mainWindow", m_view); |
| 152 | |
| 153 | m_view->setSource(QUrl(QStringLiteral("qrc:/qml/ProviderRequest.qml"))); |
| 154 | /* It could be that allow() or deny() have been already called; don't show |
| 155 | * the window in that case. */ |
| 156 | if (q->isInProgress()) { |
| 157 | q->setWindow(m_view); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | void ProviderRequestPrivate::onWindowVisibleChanged(bool visible) |
| 162 | { |
| 163 | Q_Q(ProviderRequest); |
| 164 | |
| 165 | QObject::disconnect(m_view, SIGNAL(visibleChanged(bool)), |
| 166 | this, SLOT(onWindowVisibleChanged(bool))); |
| 167 | if (!visible) { |
| 168 | q->setResult(QVariantMap()); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | void ProviderRequestPrivate::deny() |
| 173 | { |
| 174 | Q_Q(ProviderRequest); |
| 175 | DEBUG(); |
| 176 | if (m_view->isVisible()) { |
| 177 | /* Just close the window; this will deliver the empty result to the |
| 178 | * client */ |
| 179 | m_view->close(); |
| 180 | } else { |
| 181 | q->setResult(QVariantMap()); |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | void ProviderRequestPrivate::allow(int accountId) |
| 186 | { |
| 187 | Q_Q(ProviderRequest); |
| 188 | DEBUG() << "Access allowed for account:" << accountId; |
| 189 | /* If the request came from an app, add a small delay so that we could |
| 190 | * serve an authentication request coming right after this one. */ |
| 191 | if (m_view->isVisible() && |
| 192 | m_applicationInfo.value("id").toString() != "lomiri-system-settings") { |
| 193 | q->setDelay(3000); |
| 194 | } |
| 195 | QVariantMap result; |
| 196 | result.insert(OAU_KEY_ACCOUNT_ID, quint32(accountId)); |
| 197 | q->setResult(result); |
| 198 | /* We keep the view opened */ |
| 199 | } |
| 200 | |
| 201 | ProviderRequest::ProviderRequest(const QString &interface, |
| 202 | int id, |
| 203 | const QString &clientProfile, |
| 204 | const QVariantMap ¶meters, |
| 205 | QObject *parent): |
| 206 | Request(interface, id, clientProfile, parameters, parent), |
| 207 | d_ptr(new ProviderRequestPrivate(this)) |
| 208 | { |
| 209 | } |
| 210 | |
| 211 | ProviderRequest::~ProviderRequest() |
| 212 | { |
| 213 | } |
| 214 | |
| 215 | void ProviderRequest::start() |
| 216 | { |
| 217 | Q_D(ProviderRequest); |
| 218 | Request::start(); |
| 219 | d->start(); |
| 220 | } |
| 221 | |
| 222 | #include "provider-request.moc" |