| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015 Canonical Ltd. |
| 3 |
* |
| 4 |
* Contact: Alberto Mardegan <[email protected]> |
| 5 |
* |
| 6 |
* This program is free software: you can redistribute it and/or modify it |
| 7 |
* under the terms of the GNU General Public License version 3, as published |
| 8 |
* by the Free Software Foundation. |
| 9 |
* |
| 10 |
* This program is distributed in the hope that it will be useful, but |
| 11 |
* WITHOUT ANY WARRANTY; without even the implied warranties of |
| 12 |
* MERCHANTABILITY, SATISFACTORY QUALITY, or FITNESS FOR A PARTICULAR |
| 13 |
* PURPOSE. See the GNU General Public License for more details. |
| 14 |
* |
| 15 |
* You should have received a copy of the GNU General Public License along |
| 16 |
* with this program. If not, see <http://www.gnu.org/licenses/>. |
| 17 |
*/ |
| 18 |
|
| 19 |
import QtQuick 2.9 |
| 20 |
import Lomiri.Components |
| 21 |
import SSO.OnlineAccounts 0.1 |
| 22 |
import Lomiri.OnlineAccounts.Internal 1.0 |
| 23 |
|
| 24 |
MainView { |
| 25 |
id: root |
| 26 |
|
| 27 |
property var applicationInfo: request.application |
| 28 |
property var providerInfo: request.provider |
| 29 |
property int __createdAccountId: 0 |
| 30 |
|
| 31 |
width: units.gu(48) |
| 32 |
height: units.gu(60) |
| 33 |
automaticOrientation: true |
| 34 |
|
| 35 |
Component.onCompleted: { |
| 36 |
i18n.domain = "lomiri-online-accounts" |
| 37 |
if (accessModel.count === 0 && !accessModel.canCreateAccounts) { |
| 38 |
/* No accounts to authorize */ |
| 39 |
request.deny() |
| 40 |
return |
| 41 |
} |
| 42 |
if (!applicationInfo.id && accessModel.count == 1 && |
| 43 |
applicationInfo.profile == "unconfined") { |
| 44 |
/* Degenerate case: unconfined app making requests with no valid |
| 45 |
* app ID */ |
| 46 |
request.allow(accountsModel.get(0, "accountId")) |
| 47 |
return |
| 48 |
} |
| 49 |
loader.active = true |
| 50 |
pageStack.push(mainPage) |
| 51 |
} |
| 52 |
|
| 53 |
on__CreatedAccountIdChanged: grantAccessIfReady() |
| 54 |
|
| 55 |
PageStack { |
| 56 |
id: pageStack |
| 57 |
|
| 58 |
Page { |
| 59 |
id: mainPage |
| 60 |
|
| 61 |
header: PageHeader { |
| 62 |
id: pageHeader |
| 63 |
title: providerInfo.displayName |
| 64 |
property string iconSource: providerInfo.icon.indexOf("/") === 0 ? |
| 65 |
providerInfo.icon: "image://theme/" + providerInfo.icon |
| 66 |
contents: Row { |
| 67 |
anchors.fill: parent |
| 68 |
spacing: units.gu(1) |
| 69 |
LomiriShape { |
| 70 |
height: Math.min(units.gu(4), parent.height - units.gu(1)) |
| 71 |
width: height |
| 72 |
anchors.verticalCenter: parent.verticalCenter |
| 73 |
source: Image { source: pageHeader.iconSource} |
| 74 |
} |
| 75 |
Label { |
| 76 |
text: pageHeader.title |
| 77 |
anchors.verticalCenter: parent.verticalCenter |
| 78 |
} |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
Loader { |
| 83 |
id: loader |
| 84 |
anchors { |
| 85 |
top: pageHeader.bottom |
| 86 |
left: parent.left |
| 87 |
right: parent.right |
| 88 |
bottom: parent.bottom |
| 89 |
} |
| 90 |
active: false |
| 91 |
sourceComponent: ((accessModel.count == 0 && accessModel.canCreateAccounts) || |
| 92 |
applicationInfo.id === "lomiri-system-settings") ? |
| 93 |
accountCreationPage : authorizationPage |
| 94 |
onLoaded: { |
| 95 |
// use this trick to break the sourceComponent binding |
| 96 |
var tmp = sourceComponent |
| 97 |
sourceComponent = tmp |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
AccountServiceModel { |
| 104 |
id: accountsModel |
| 105 |
service: "global" |
| 106 |
provider: providerInfo.id |
| 107 |
includeDisabled: true |
| 108 |
onCountChanged: root.grantAccessIfReady() |
| 109 |
|
| 110 |
function indexOfAccount(accountId) { |
| 111 |
for (var i = 0; i < accountsModel.count; i++) { |
| 112 |
if (accountsModel.get(i, "accountId") == accountId) |
| 113 |
return i |
| 114 |
} |
| 115 |
return -1 |
| 116 |
} |
| 117 |
} |
| 118 |
|
| 119 |
AccessModel { |
| 120 |
id: accessModel |
| 121 |
accountModel: accountsModel |
| 122 |
applicationId: applicationInfo.id |
| 123 |
|
| 124 |
property bool canCreateAccounts: !providerInfo.isSingleAccount || accountsModel.count === 0 |
| 125 |
} |
| 126 |
|
| 127 |
Component { |
| 128 |
id: accountCreationPage |
| 129 |
AccountCreationPage { |
| 130 |
providerId: providerInfo.id |
| 131 |
onFinished: { |
| 132 |
if (accountId == 0) request.deny() |
| 133 |
/* if an account was created, just remember its ID. when the |
| 134 |
* accountsModel will notice it we'll proceed with the access |
| 135 |
* grant */ |
| 136 |
else root.__createdAccountId = accountId |
| 137 |
} |
| 138 |
} |
| 139 |
} |
| 140 |
|
| 141 |
Component { |
| 142 |
id: authorizationPage |
| 143 |
AuthorizationPage { |
| 144 |
model: accessModel |
| 145 |
application: applicationInfo |
| 146 |
provider: providerInfo |
| 147 |
canAddAnotherAccount: accessModel.canCreateAccounts |
| 148 |
onDenied: request.deny() |
| 149 |
onAllowed: root.grantAccess(accountId) |
| 150 |
onCreateAccount: pageStack.push(createAccountPageComponent) |
| 151 |
} |
| 152 |
} |
| 153 |
|
| 154 |
Component { |
| 155 |
id: createAccountPageComponent |
| 156 |
Page { |
| 157 |
header: PageHeader { |
| 158 |
id: pageHeader |
| 159 |
title: providerInfo.displayName |
| 160 |
} |
| 161 |
|
| 162 |
Loader { |
| 163 |
anchors { |
| 164 |
top: pageHeader.bottom |
| 165 |
left: parent.left |
| 166 |
right: parent.right |
| 167 |
bottom: parent.bottom |
| 168 |
} |
| 169 |
sourceComponent: accountCreationPage |
| 170 |
} |
| 171 |
} |
| 172 |
} |
| 173 |
|
| 174 |
Component { |
| 175 |
id: spinnerComponent |
| 176 |
Item { |
| 177 |
ActivityIndicator { |
| 178 |
anchors.centerIn: parent |
| 179 |
running: true |
| 180 |
} |
| 181 |
} |
| 182 |
} |
| 183 |
|
| 184 |
Component { |
| 185 |
id: accountServiceComponent |
| 186 |
AccountService { |
| 187 |
autoSync: false |
| 188 |
} |
| 189 |
} |
| 190 |
|
| 191 |
Account { |
| 192 |
id: account |
| 193 |
onSynced: accountEnablingDone() |
| 194 |
} |
| 195 |
|
| 196 |
AccountService { |
| 197 |
id: globalAccountService |
| 198 |
objectHandle: account.accountServiceHandle |
| 199 |
credentials: accountCredentials |
| 200 |
autoSync: false |
| 201 |
} |
| 202 |
|
| 203 |
AccountServiceModel { |
| 204 |
id: accountServiceModel |
| 205 |
includeDisabled: true |
| 206 |
account: account.objectHandle |
| 207 |
} |
| 208 |
|
| 209 |
Credentials { |
| 210 |
id: accountCredentials |
| 211 |
credentialsId: globalAccountService.objectHandle != null ? globalAccountService.authData.credentialsId : 0 |
| 212 |
onSynced: { |
| 213 |
console.log("Credentials ready (store secret = " + storeSecret + ")") |
| 214 |
if (acl.indexOf(applicationInfo.profile) >= 0) { |
| 215 |
console.log("Application is in ACL: " + acl) |
| 216 |
root.aclDone() |
| 217 |
return |
| 218 |
} |
| 219 |
acl.push(applicationInfo.profile) |
| 220 |
sync() |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
function grantAccessIfReady() { |
| 225 |
if (root.__createdAccountId != 0) { |
| 226 |
// If the request comes from system settings, stop here |
| 227 |
if (applicationInfo.id === "lomiri-system-settings") { |
| 228 |
request.allow(root.__createdAccountId) |
| 229 |
loader.sourceComponent = spinnerComponent |
| 230 |
return |
| 231 |
} |
| 232 |
|
| 233 |
if (accountsModel.indexOfAccount(root.__createdAccountId) >= 0) { |
| 234 |
root.grantAccess(root.__createdAccountId) |
| 235 |
root.__createdAccountId = 0 |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
function grantAccess(accountId) { |
| 241 |
console.log("granting access to account " + accountId) |
| 242 |
// find the index in the model for this account |
| 243 |
var i = accountsModel.indexOfAccount(accountId) |
| 244 |
if (i < 0) { |
| 245 |
// very unlikely; maybe the account has been deleted in the meantime |
| 246 |
console.log("Account not found:" + accountId) |
| 247 |
request.deny() |
| 248 |
return |
| 249 |
} |
| 250 |
|
| 251 |
// setting this will trigger the update of the ACL |
| 252 |
account.objectHandle = accountsModel.get(i, "accountHandle") |
| 253 |
} |
| 254 |
|
| 255 |
function aclDone() { |
| 256 |
console.log("acl done") |
| 257 |
/* now we can enable the application services in the account. */ |
| 258 |
for (var i = 0; i < accountServiceModel.count; i++) { |
| 259 |
var accountService = accountServiceComponent.createObject(null, { |
| 260 |
"objectHandle": accountServiceModel.get(i, "accountServiceHandle") |
| 261 |
}) |
| 262 |
console.log("Account service account id: " + accountService.accountId) |
| 263 |
var serviceId = accountService.service.id |
| 264 |
if (applicationInfo.services.indexOf(serviceId) >= 0 && |
| 265 |
!accountService.serviceEnabled) { |
| 266 |
console.log("Enabling service " + serviceId) |
| 267 |
accountService.updateServiceEnabled(true) |
| 268 |
} |
| 269 |
/* The accountService is just a convenience object: all the changes |
| 270 |
* are stored in the account object. So we can destroy this one. */ |
| 271 |
accountService.destroy() |
| 272 |
} |
| 273 |
|
| 274 |
// Store the changes |
| 275 |
account.sync() |
| 276 |
} |
| 277 |
|
| 278 |
function accountEnablingDone() { |
| 279 |
console.log("account enabling done") |
| 280 |
request.allow(account.accountId) |
| 281 |
loader.sourceComponent = spinnerComponent |
| 282 |
} |
| 283 |
} |