File: AccountsPage.qml

package info (click to toggle)
lomiri-cloudsync-app 0.8.1%2Bdfsg-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,560 kB
  • sloc: cpp: 1,053; python: 67; javascript: 25; makefile: 21; sh: 9
file content (445 lines) | stat: -rw-r--r-- 16,633 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import QtQuick 2.4
import Lomiri.Components 1.3
import "../components"
import Lomiri.OnlineAccounts 2.0

import QtQuick.LocalStorage 2.0


Page {
    id: accountsPage

    property var db

    property string requestAccount: ""
    property bool accountsLoaded: false // accounts may be not ready ...
    
    property bool upMoveDetected: false // detect Up move to update the ListView
    
    /* load current data from DB */
    function loadDB() {

        if (accounts.ready === false) {
            return
        } else {
            accountsLoaded = true
        }

        accountListModel.clear();

        accountsPage.db = LocalStorage.openDatabaseSync("UBsync", "1.0", "UBsync", 1000000);

        console.log("AccountsPage :: Loading accountsPage data");

        accountsPage.db.transaction(
                    function(tx) {
                        // load selected account
                        var rs = tx.executeSql('SELECT * FROM SyncAccounts');

                        for(var i = 0; i < rs.rows.length; i++) {
                            console.log("AccountsPage :: Loading accountsPage: " + rs.rows.item(i).accountName + "; CNT: " + accounts.count + "; ID: " + rs.rows.item(i).accountID)
                            var j = 0
                            for (j = 0; j < accounts.count; j++) {
                                if (accounts.get(j, "account").accountId === rs.rows.item(i).accountID) {
                                    console.log("AccountsPage :: Loading accountsPage: " + rs.rows.item(i).accountName + "; " + accounts.count + "; " + accounts.get(j, "account").accountId)
                                    // Add enabled accounts to the list
                                    accountListModel.append({"accountID": rs.rows.item(i).accountID, "accountName": rs.rows.item(i).accountName, "color": owncloud.settings.color_accountEnabled, "isEditable": true})
                                    break
                                }
                            }

                            if (j === accounts.count) {
                                // account is NOT enabled!
                                accountListModel.append({"accountID": rs.rows.item(i).accountID, "accountName": rs.rows.item(i).accountName, "color" : owncloud.settings.color_accountDisabled, "isEditable": true})
                                console.log("AccountsPage :: Color is " + owncloud.settings.color_accountDisabled + " - account NOT enabled")
                            }
                        }

                        // Add enabled but unconfigured accounts to the end of the list
                        for (j = 0; j < accounts.count; j++) {
                            for(i = 0; i < rs.rows.length; i++) {
                                if (accounts.get(j, "account").accountId === rs.rows.item(i).accountID) {
                                    break
                                }
                            }
                            if (i === rs.rows.length) {
                                // Account was not found in database
                                accountListModel.append({"accountID": accounts.get(j, "account").accountId, "accountName": accounts.get(j, "account").displayName, "color" : owncloud.settings.color_accountEnabledNotConfigured, "isEditable": false})
                                console.log("AccountsPage :: Color is " + owncloud.settings.color_accountEnabledNotConfigured + " - account NOT configured")
                            }
                        }
                    }
                )

        accountList.forceLayout();
    }

    /* remove account */
    function removeAccount(accountID) {

        accountListModel.clear();

        accountsPage.db = LocalStorage.openDatabaseSync("UBsync", "1.0", "UBsync", 1000000);

        console.log("AccountsPage :: Removing account " + accountID)

        accountsPage.db.transaction(
                    function(tx) {
                        // remove selected account from DB
                        var rs = tx.executeSql('DELETE FROM SyncAccounts WHERE accountID = (?)', [accountID]);
                    }
                )

        // TODO - disable account in online accounts ?

        accountsPage.loadDB();
        accountList.forceLayout();
    }

    Connections {
            target: accountsPage

            onActiveChanged: {
                /* re-render anytime page is shown */
                console.log("AccountsPage :: accountsPage activated")
                accountsPage.loadDB()
            }
        }

    ListModel {
        id: accountListModel

        ListElement {
            accountID: 0
            accountName: "Unknown"
            color: "silver"
            isEditable: false  // account has a database entry
        }

        Component.onCompleted: {
            console.log("AccountsPage :: accountsPage created")

            accountsPage.loadDB()
        }
    }


    header: PageHeader {
        id: header
        title: i18n.tr("Online Accounts")

        trailingActionBar{
            actions: [
                Action {
                    iconName: "add"
                    onTriggered: {
                        console.log("AccountsPage :: Add New Online account.")
                        apl.addPageToNextColumn(accountsPage, Qt.resolvedUrl("RequestAccountPage.qml"))
                    }
                }
            ]
        }
    }

    Timer {
        id: continuousCheck
        interval: 50
        running: true
        repeat: true
        onTriggered: {
            // hide back navigation in double-column mode
            if (apl.columns === 1) {
                header.navigationActions[0].visible = true
            } else {
                header.navigationActions[0].visible = false
            }
        }
    }

    Timer {
        id: newAccountTimer
        interval: 250
        running: true
        repeat: true
        onTriggered: {
            // if accounts were not ready update again as soon as possible ...
            if (accountsLoaded === false) {
                accountsPage.loadDB()
                if (accountsLoaded === false) {
                    // if still not ready, wait ...
                    return
                }
            }

            if (accounts.count > accountListModel.count) {
                accountsPage.loadDB()
            }

            /* handle account requests */
            if (requestAccount.localeCompare("nextcloud") === 0) {
                console.log("AccountsPage :: NextCloud activation request!")
                accounts.requestAccess(accounts.applicationId + "_nextcloud", {})
            } else if (requestAccount.localeCompare("owncloud") === 0) {
                console.log("AccountsPage :: OwnCloud activation request!")
                accounts.requestAccess(accounts.applicationId + "_owncloud", {})
            }
            requestAccount = ""
        }
    }

    Connections {
          id: accountConnection
          target: accounts

          onAccessReply: {
              console.log("AccountsPage :: onAccessReply()")

              //console.log(JSON.stringify(reply.account))
              //console.log(JSON.stringify(authenticationData))

              if ("errorCode" in reply) {
                  console.warn("Authentication error: " + reply.errorText + " (" + reply.errorCode + ")")
                  // TODO: report an error to user ?

              } else {
                  var account = reply.account;

                  /* TODO: activate in debug mode? */
                  //console.log("AccountsPage :: Account details are: " + " (" + account.accountId + ")" + " " + account.settings.host + "; " + authenticationData.Username + ":" + authenticationData.Password )
                  apl.addPageToNextColumn(accountsPage, Qt.resolvedUrl("EditAccount.qml"), {accountID: account.accountId, defaultAccountName: account.displayName, remoteAddress: account.settings.host, remoteUser: authenticationData.Username, isEditable: true })

                  // update
                  accountsPage.loadDB()
              }

          }
    }

    Connections {
          id: accountConnectionAccount
          target: null

          onAuthenticationReply: {
              console.log("AccountsPage :: onAuthenticationReply()")
              var reply = authenticationData

              if ("errorCode" in reply) {
                  console.warn("Authentication error: " + reply.errorText + " (" + reply.errorCode + ")")
                  // TODO: report an error to user ?
              } else {
                  /* TODO: activate in debug mode? */
                  //console.log("AccountsPage :: Account details are: " + " (" + account.accountId + ")" + " " + account.settings.host + "; " + authenticationData.Username + ":" + authenticationData.Password )
                  apl.addPageToNextColumn(accountsPage, Qt.resolvedUrl("EditAccount.qml"), {accountID: target.accountId, defaultAccountName: target.displayName, remoteAddress: target.settings.host, remoteUser: reply.Username, isEditable: true })

                  // update
                  accountsPage.loadDB()
              }
          }
    }

    AccountModel {
        id: accounts
        applicationId: "lomiri-cloudsync-app"
    }

    Item {
        //Shown only if there are no items in accounts
        anchors{centerIn: parent}
        visible: !accountListModel.count
        width: parent.width - units.gu(4)

        Label{
            wrapMode: Text.Wrap
            text: i18n.tr("No accounts, press")
            anchors{left: parent.left; right: parent.right; bottom: addIcon.top; bottomMargin: units.gu(2)}
            horizontalAlignment: Text.AlignHCenter
        }

        Icon {
            id: addIcon
            name: "add"
            width: units.gu(4)
            height: width
            anchors{centerIn: parent}
        }

        Label{
            wrapMode: Text.Wrap
            text: i18n.tr("on the panel to add a new account")
            anchors{left: parent.left; right: parent.right; top: addIcon.bottom; topMargin: units.gu(2)}
            horizontalAlignment: Text.AlignHCenter
        }
    }
    
    Item {
        //TODO Shown only if there is a top move in progress
        id: accountsListUpdateHelper
        anchors{left:parent.left; right:parent.right; top:header.bottom; bottom:parent.bottom; bottomMargin: units.gu(5); topMargin: units.gu(5)}
        width: parent.width
        visible: false
        
        Label{
            wrapMode: Text.Wrap
            text: i18n.tr("Release to update the list of accounts ...")
            anchors{left: parent.left; right: parent.right; bottom: accountList.top; bottomMargin: units.gu(5); topMargin: units.gu(5)}
            horizontalAlignment: Text.AlignHCenter
        }
    }

    ListView {
        id: accountList
        model: accountListModel
        anchors{left:parent.left; right:parent.right; top:header.bottom; bottom:parent.bottom; bottomMargin:units.gu(2)}
        clip: true
        visible: accountListModel.count

        onContentYChanged: {
            // Prevent triggering pulldown item when rebound from top boundary.
            if (contentY < ((-1) * units.gu(20))) {
                console.log("AccountsPage :: Show reload helper text.")
                accountsListUpdateHelper.visible = true
                upMoveDetected = true 
            } else {
                accountsListUpdateHelper.visible = false
            }
        }
        
        onMovementStarted: {
            /* update page when moving ...*/
        }
        
        onMovementEnded: {
            /* update page when moving ...*/
            if ((accountList.atYBeginning === true) && (upMoveDetected === true)) {
                console.log("AccountsPage :: Page reload forced due to top positioning.")
                accountsPage.loadDB()
                upMoveDetected = false 
            }
        }

        delegate: ListItem {
            height: accountColumn.height
            anchors{left:parent.left; right:parent.right}

            onClicked: {
                if (model.isEditable === true) {
                    apl.addPageToNextColumn(accountsPage, Qt.resolvedUrl("EditAccount.qml"), {accountID: model.accountID, isEditable: model.isEditable})
                } else {
                    for (var j = 0; j < accounts.count; j++) {
                        if (accounts.get(j, "account").accountId === model.accountID) {
                            accountConnectionAccount.target = accounts.get(j, "account")
                            accounts.get(j, "account").authenticate({})
                        }
                    }
                }
            }

            Item {
                id: accountColumn
                height: units.gu(12)

                anchors.leftMargin: units.gu(2)

                //spacing: units.gu(1)
                anchors {
                    top: parent.top; left: parent.left; right: parent.right; margins:units.gu(2)
                }

                Rectangle {
                    id: accountIcon
                    color: model.color
                    width: units.gu(9)
                    height: width
                    border.width: 0
                    radius: 10
                    anchors {
                       left: parent.left; top: parent.top
                    }
                }

                Label {
                    id: accountIconText
                    text: model.accountName.charAt(0).toUpperCase()
                    color: "white"
                    font.pixelSize: units.gu(6)
                    anchors {
                       horizontalCenter: accountIcon.horizontalCenter; verticalCenter: accountIcon.verticalCenter
                    }
                }

                Label {
                    id: accountName
                    wrapMode: Text.Wrap
                    width: parent.width - accountIcon.width - units.gu(4)
                    text: model.accountName
                    height: units.gu(6)
                    font.pixelSize: units.gu(3)
                    anchors.leftMargin: units.gu(2)
                    anchors {
                       left: accountIcon.right; top: parent.top
                    }
                }

                /*Label {
                    id: accountID
                    text: model.accountID
                    font.pixelSize: units.gu(2)
                    anchors.leftMargin: units.gu(2)
                    anchors {
                       left: accountIcon.right; top: accountName.bottom
                    }
                }*/

                /* TODO display number of sync targets ? */

            }


            leadingActions: ListItemActions {
                actions: [
                    Action {
                        iconName: "delete"
                        enabled: model.isEditable
                        text: ""
                        onTriggered: {
                            accountsPage.removeAccount(model.accountID)
                        }
                    }
                ]
            }

            trailingActions: ListItemActions {
                actions: [
                    Action {
                        iconName: "edit"
                        text: ""
                        onTriggered: {
                            if (model.isEditable === true) {
                                apl.addPageToNextColumn(accountsPage, Qt.resolvedUrl("EditAccount.qml"), {accountID: model.accountID, isEditable: model.isEditable})
                            } else {
                                for (var j = 0; j < accounts.count; j++) {
                                    if (accounts.get(j, "account").accountId === model.accountID) {
                                        accountConnectionAccount.target = accounts.get(j, "account")
                                        accounts.get(j, "account").authenticate({})
                                    }
                                }
                            }
                        }
                    },

                    Action {
                        iconName: "note-new"
                        text: ""
                        enabled: model.isEditable
                        onTriggered: {
                            apl.addPageToNextColumn(accountsPage, Qt.resolvedUrl("EditTarget.qml"), {targetID: 0, accountID: model.accountID})
                        }
                    }
                ]
            }
        }
    }



}