| 1 | /* |
| 2 | * Copyright (C) 2013-2015 Canonical Ltd. |
| 3 | * |
| 4 | * Contact: Alberto Mardegan <[email protected]> |
| 5 | * |
| 6 | * This file is part of OnlineAccountsClient. |
| 7 | * |
| 8 | * OnlineAccountsClient is free software: you can redistribute it and/or |
| 9 | * modify it under the terms of the GNU Lesser General Public License as |
| 10 | * published by the Free Software Foundation, either version 3 of the |
| 11 | * License, or (at your option) any later version. |
| 12 | * |
| 13 | * OnlineAccountsClient 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 |
| 16 | * GNU Lesser General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU Lesser General Public License |
| 19 | * along with OnlineAccountsClient. If not, see |
| 20 | * <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | #include <online-accounts-common/globals.h> |
| 24 | #include "OnlineAccountsUi.h" |
| 25 | #include "setup.h" |
| 26 | |
| 27 | #include <QDBusConnection> |
| 28 | #include <QDBusPendingCallWatcher> |
| 29 | #include <QDBusPendingReply> |
| 30 | #include <QGuiApplication> |
| 31 | #include <QWindow> |
| 32 | #include <climits> |
| 33 | |
| 34 | using namespace OnlineAccountsClient; |
| 35 | using namespace com::lomiri; |
| 36 | |
| 37 | namespace OnlineAccountsClient { |
| 38 | |
| 39 | class SetupPrivate: public QObject |
| 40 | { |
| 41 | Q_OBJECT |
| 42 | Q_DECLARE_PUBLIC(Setup) |
| 43 | |
| 44 | public: |
| 45 | inline SetupPrivate(Setup *setup); |
| 46 | ~SetupPrivate() {}; |
| 47 | |
| 48 | void exec(); |
| 49 | QWindow *clientWindow() const; |
| 50 | |
| 51 | private Q_SLOTS: |
| 52 | void onRequestAccessReply(QDBusPendingCallWatcher *watcher); |
| 53 | |
| 54 | private: |
| 55 | OnlineAccountsUi m_onlineAccountsUi; |
| 56 | QString m_applicationId; |
| 57 | QString m_serviceId; |
| 58 | QString m_serviceTypeId; |
| 59 | QString m_providerId; |
| 60 | pid_t m_clientPid; |
| 61 | mutable Setup *q_ptr; |
| 62 | }; |
| 63 | |
| 64 | }; // namespace |
| 65 | |
| 66 | static QDBusConnection sessionBus() { |
| 67 | /* When this module is used within the online-accounts-service process, we |
| 68 | * need to use a separate D-Bus connection otherwise link-local calls won't |
| 69 | * work (QtDBus does not support delayed replies on local calls). */ |
| 70 | return QCoreApplication::applicationName() == "lomiri-online-accounts-service" ? |
| 71 | QDBusConnection::connectToBus(QDBusConnection::SessionBus, "oa-private") : |
| 72 | QDBusConnection::sessionBus(); |
| 73 | } |
| 74 | |
| 75 | SetupPrivate::SetupPrivate(Setup *setup): |
| 76 | QObject(setup), |
| 77 | m_onlineAccountsUi(OAU_SERVICE_NAME, |
| 78 | OAU_OBJECT_PATH, |
| 79 | sessionBus()), |
| 80 | m_clientPid(0), |
| 81 | q_ptr(setup) |
| 82 | { |
| 83 | m_onlineAccountsUi.setTimeout(INT_MAX); |
| 84 | } |
| 85 | |
| 86 | void SetupPrivate::exec() |
| 87 | { |
| 88 | QVariantMap options; |
| 89 | |
| 90 | options.insert(OAU_KEY_PID, |
| 91 | uint(m_clientPid != 0 ? m_clientPid : getpid())); |
| 92 | |
| 93 | QWindow *window = clientWindow(); |
| 94 | if (window) { |
| 95 | options.insert(OAU_KEY_WINDOW_ID, window->winId()); |
| 96 | /* TODO: remove this hack once Mir supports window reparenting. |
| 97 | * Since Mir always return the same window Id for all windows, we use |
| 98 | * the process ID instead; for the time being this is acceptable since |
| 99 | * Mir/lomiri don't support more than one window per process. |
| 100 | * See: https://bugs.launchpad.net/bugs/1153666 */ |
| 101 | if (QGuiApplication::platformName().startsWith("ubuntu")) { |
| 102 | options.insert(OAU_KEY_WINDOW_ID, uint(getpid())); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | if (!m_applicationId.isEmpty()) { |
| 107 | options.insert(OAU_KEY_APPLICATION, m_applicationId); |
| 108 | } |
| 109 | |
| 110 | if (!m_serviceId.isEmpty()) { |
| 111 | options.insert(OAU_KEY_SERVICE_ID, m_serviceId); |
| 112 | } |
| 113 | |
| 114 | if (!m_serviceTypeId.isEmpty()) { |
| 115 | options.insert(OAU_KEY_SERVICE_TYPE, m_serviceTypeId); |
| 116 | } |
| 117 | |
| 118 | if (!m_providerId.isEmpty()) { |
| 119 | options.insert(OAU_KEY_PROVIDER, m_providerId); |
| 120 | } |
| 121 | |
| 122 | QDBusPendingReply<QVariantMap> reply = |
| 123 | m_onlineAccountsUi.requestAccess(options); |
| 124 | QDBusPendingCallWatcher *watcher = new QDBusPendingCallWatcher(reply, |
| 125 | this); |
| 126 | QObject::connect(watcher, SIGNAL(finished(QDBusPendingCallWatcher*)), |
| 127 | this, |
| 128 | SLOT(onRequestAccessReply(QDBusPendingCallWatcher*))); |
| 129 | } |
| 130 | |
| 131 | QWindow *SetupPrivate::clientWindow() const |
| 132 | { |
| 133 | QWindow *window = QGuiApplication::focusWindow(); |
| 134 | if (window) return window; |
| 135 | |
| 136 | /* Otherwise, just return the first toplevel window; later on, if the need |
| 137 | * arises, we might add a property to let the client explicitly specify |
| 138 | * which window to use. */ |
| 139 | return QGuiApplication::topLevelWindows().value(0, 0); |
| 140 | } |
| 141 | |
| 142 | void SetupPrivate::onRequestAccessReply(QDBusPendingCallWatcher *watcher) |
| 143 | { |
| 144 | Q_Q(Setup); |
| 145 | |
| 146 | watcher->deleteLater(); |
| 147 | |
| 148 | QVariantMap response; |
| 149 | |
| 150 | QDBusPendingReply<QVariantMap> reply = *watcher; |
| 151 | if (reply.isError()) { |
| 152 | qWarning() << "RequestAccess failed:" << reply.error(); |
| 153 | response.insert("errorName", reply.error().name()); |
| 154 | } else { |
| 155 | // At the moment, we don't have any use for the reply. |
| 156 | response = reply.value(); |
| 157 | } |
| 158 | |
| 159 | Q_EMIT q->finished(response); |
| 160 | } |
| 161 | |
| 162 | /*! |
| 163 | * \qmltype Setup |
| 164 | * \inqmlmodule Lomiri.OnlineAccounts.Client 0.1 |
| 165 | * \ingroup Lomiri |
| 166 | * |
| 167 | * \brief Invoke the Online Accounts panel |
| 168 | * |
| 169 | * This object can be used by applications to request the creation of an |
| 170 | * account. By calling the \l exec() method, the Online Accounts panel will |
| 171 | * appear and guide the user through the creation of an account. Once done, the |
| 172 | * \l finished() signal will be emitted. The type of account to be created can |
| 173 | * be configured by this object's properties. |
| 174 | * |
| 175 | * Example: |
| 176 | * |
| 177 | * \qml |
| 178 | * import QtQuick 2.9 |
| 179 | * import Lomiri.Components 1.3 |
| 180 | * import Lomiri.OnlineAccounts.Client 0.1 |
| 181 | * |
| 182 | * Rectangle { |
| 183 | * width: 400 |
| 184 | * height: 300 |
| 185 | * |
| 186 | * Button { |
| 187 | * anchors.centerIn: parent |
| 188 | * text: "Create Facebook account" |
| 189 | * onClicked: setup.exec() |
| 190 | * } |
| 191 | * |
| 192 | * Setup { |
| 193 | * id: setup |
| 194 | * applicationId: "MyApp" |
| 195 | * providerId: "facebook" |
| 196 | * } |
| 197 | * } |
| 198 | * \endqml |
| 199 | */ |
| 200 | Setup::Setup(QObject *parent): |
| 201 | QObject(parent), |
| 202 | d_ptr(new SetupPrivate(this)) |
| 203 | { |
| 204 | } |
| 205 | |
| 206 | Setup::~Setup() |
| 207 | { |
| 208 | delete d_ptr; |
| 209 | } |
| 210 | |
| 211 | /*! |
| 212 | * \qmlproperty string Setup::applicationId |
| 213 | * Specifies which application is asking for access. The value of this string |
| 214 | * must be equal to the filename of the XML application file (installed in \c |
| 215 | * /usr/share/accounts/applications/ or \c |
| 216 | * ~/.local/share/accounts/applications/) minus the \c .application suffix. |
| 217 | */ |
| 218 | void Setup::setApplicationId(const QString &applicationId) |
| 219 | { |
| 220 | Q_D(Setup); |
| 221 | if (applicationId == d->m_applicationId) return; |
| 222 | d->m_applicationId = applicationId; |
| 223 | Q_EMIT applicationIdChanged(); |
| 224 | } |
| 225 | |
| 226 | QString Setup::applicationId() const |
| 227 | { |
| 228 | Q_D(const Setup); |
| 229 | return d->m_applicationId; |
| 230 | } |
| 231 | |
| 232 | /*! |
| 233 | * \qmlproperty string Setup::serviceId |
| 234 | * If set to a valid service ID, the user will be asked to create an Online |
| 235 | * Account which provides this service. |
| 236 | */ |
| 237 | void Setup::setServiceId(const QString &serviceId) |
| 238 | { |
| 239 | Q_D(Setup); |
| 240 | if (serviceId == d->m_serviceId) return; |
| 241 | d->m_serviceId = serviceId; |
| 242 | Q_EMIT serviceIdChanged(); |
| 243 | } |
| 244 | |
| 245 | QString Setup::serviceId() const |
| 246 | { |
| 247 | Q_D(const Setup); |
| 248 | return d->m_serviceId; |
| 249 | } |
| 250 | |
| 251 | /*! |
| 252 | * \qmlproperty string Setup::serviceTypeId |
| 253 | * If set to a valid service type, the user will be asked to create an Online |
| 254 | * Account which supports this service type. |
| 255 | */ |
| 256 | void Setup::setServiceTypeId(const QString &serviceTypeId) |
| 257 | { |
| 258 | Q_D(Setup); |
| 259 | if (serviceTypeId == d->m_serviceTypeId) return; |
| 260 | d->m_serviceTypeId = serviceTypeId; |
| 261 | Q_EMIT serviceTypeIdChanged(); |
| 262 | } |
| 263 | |
| 264 | QString Setup::serviceTypeId() const |
| 265 | { |
| 266 | Q_D(const Setup); |
| 267 | return d->m_serviceTypeId; |
| 268 | } |
| 269 | |
| 270 | /*! |
| 271 | * \qmlproperty string Setup::providerId |
| 272 | * If set to a valid provider, the user will be asked to create an Online |
| 273 | * Account provided by this entity. |
| 274 | */ |
| 275 | void Setup::setProviderId(const QString &providerId) |
| 276 | { |
| 277 | Q_D(Setup); |
| 278 | if (providerId == d->m_providerId) return; |
| 279 | d->m_providerId = providerId; |
| 280 | Q_EMIT providerIdChanged(); |
| 281 | } |
| 282 | |
| 283 | QString Setup::providerId() const |
| 284 | { |
| 285 | Q_D(const Setup); |
| 286 | return d->m_providerId; |
| 287 | } |
| 288 | |
| 289 | /* At the moment this method is meant to be used by unconfined services, when |
| 290 | * forwarding a request from their client. |
| 291 | */ |
| 292 | void Setup::setClientPid(pid_t clientPid) |
| 293 | { |
| 294 | Q_D(Setup); |
| 295 | d->m_clientPid = clientPid; |
| 296 | } |
| 297 | |
| 298 | /*! |
| 299 | * \qmlmethod void Setup::exec() |
| 300 | * Launches the Online Accounts panel. |
| 301 | */ |
| 302 | void Setup::exec() |
| 303 | { |
| 304 | Q_D(Setup); |
| 305 | d->exec(); |
| 306 | } |
| 307 | |
| 308 | /*! |
| 309 | * \qmlsignal Setup::finished() |
| 310 | * Emitted when the Online Accounts panel has been closed. |
| 311 | */ |
| 312 | |
| 313 | #include "setup.moc" |