~belohoub/owncloud-sync/UBsync

« back to all changes in this revision

Viewing changes to Owncloud-Sync/ui/EditTarget.qml

  • Committer: Jan Belohoubek
  • Date: 2021-09-22 14:15:52 UTC
  • Revision ID: belohoub@fel.zcu.cz-20210922141552-n9me6m1vv5lm9u9s
UI refactoring and database model

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import QtQuick 2.4
 
2
import Ubuntu.Components 1.3
 
3
 
 
4
import Ubuntu.OnlineAccounts 2.0
 
5
 
 
6
import QtQuick.LocalStorage 2.0
 
7
 
 
8
 
 
9
Page {
 
10
    id: targetPage
 
11
 
 
12
    property var db
 
13
 
 
14
    /* Pass paramters */
 
15
    property int targetID: 0
 
16
 
 
17
    /* Required when creating a new record */
 
18
    property int accountID: 0
 
19
 
 
20
    /* Other parameters */
 
21
    property string accountUser: ""
 
22
    property string accountPassword: ""
 
23
    property string accountRemoteAddress: ""
 
24
 
 
25
 
 
26
    function loadDB() {
 
27
 
 
28
        targetPage.db = LocalStorage.openDatabaseSync("UBsync", "1.0", "UBsync", 1000000);
 
29
 
 
30
        console.log("Loading SyncTargets ID " + targetPage.targetID)
 
31
 
 
32
        targetPage.db.transaction(
 
33
                    function(tx) {
 
34
                        // Create table if it doesn't already exist
 
35
                        tx.executeSql('CREATE TABLE IF NOT EXISTS SyncTargets(targetID INTEGER PRIMARY KEY AUTOINCREMENT, accountID INTEGER, localPath TEXT, remotePath TEXT, targetName TEXT, active BOOLEAN)');
 
36
 
 
37
                        // load selected target
 
38
                        var rs = tx.executeSql('SELECT * FROM SyncTargets WHERE targetID = (?)', targetPage.targetID);
 
39
 
 
40
                        for(var i = 0; i < rs.rows.length; i++) {
 
41
                            targetPage.accountID =  rs.rows.item(i).accountID
 
42
 
 
43
                            targetName.text = rs.rows.item(i).targetName
 
44
 
 
45
                            localPath.text = rs.rows.item(i).localPath
 
46
                            remotePath.text = rs.rows.item(i).remotePath
 
47
 
 
48
                            console.log("Target Enabled: " + rs.rows.item(i).active)
 
49
                            activeSwitch.checked = rs.rows.item(i).active
 
50
                        }
 
51
                    }
 
52
                    )
 
53
 
 
54
        targetPage.db.transaction(
 
55
                    function(tx) {
 
56
                        // load selected target
 
57
                        var rs = tx.executeSql('SELECT * FROM SyncAccounts WHERE accountID = (?)', targetPage.accountID);
 
58
 
 
59
                        for(var i = 0; i < rs.rows.length; i++) {
 
60
                           accountName.text = rs.rows.item(i).accountName
 
61
 
 
62
                           targetPage.accountUser = rs.rows.item(i).remoteUser
 
63
                           targetPage.accountRemoteAddress = rs.rows.item(i).remoteAddress
 
64
                        }
 
65
                    }
 
66
                    )
 
67
    }
 
68
 
 
69
    function updateDB() {
 
70
        targetPage.db.transaction(
 
71
                    function(tx) {
 
72
 
 
73
                        var rs = tx.executeSql('SELECT * FROM SyncTargets WHERE targetID = (?)', targetPage.targetID);
 
74
 
 
75
                        if (rs.rows.length === 0) {
 
76
                            console.log("Inserting SyncTargets ID " + targetPage.targetID)
 
77
                            tx.executeSql('INSERT INTO SyncTargets VALUES(NULL, (?), (?), (?), (?), 1)', [
 
78
                                              targetPage.accountID,
 
79
                                              localPath.text,
 
80
                                              remotePath.text,
 
81
                                              targetName.text]);
 
82
 
 
83
                            // load inserted ID
 
84
                            rs = tx.executeSql('SELECT * FROM SyncTargets');
 
85
                            targetPage.targetID = rs.rows.item(rs.rows.length - 1).targetID
 
86
 
 
87
                            targetIDText.text = "ID: " + targetPage.targetID
 
88
 
 
89
                        } else {
 
90
                            console.log("Updating SyncTargets ID " + targetPage.targetID)
 
91
                            tx.executeSql('UPDATE SyncTargets SET accountID=(?), localPath=(?), remotePath=(?), targetName=(?), active=(?) WHERE targetID = (?)', [
 
92
                                              targetPage.accountID,
 
93
                                              localPath.text,
 
94
                                              remotePath.text,
 
95
                                              targetName.text,
 
96
                                              activeSwitch.checked,
 
97
                                              targetPage.targetID]);
 
98
                        }
 
99
                    }
 
100
                    )
 
101
    }
 
102
 
 
103
    AccountModel {
 
104
        id: accounts
 
105
        applicationId: "ubsyncdev_UBsyncDEV"
 
106
    }
 
107
 
 
108
 
 
109
    Connections {
 
110
          id: accountConnection
 
111
          target: null
 
112
 
 
113
          onAuthenticationReply: {
 
114
              var reply = authenticationData
 
115
 
 
116
              if ("errorCode" in reply) {
 
117
                  console.warn("Authentication error: " + reply.errorText + " (" + reply.errorCode + ")")
 
118
                  // TODO: report an error to user ?
 
119
 
 
120
              } else {
 
121
                  targetPage.accountUser = reply.Username
 
122
                  targetPage.accountPassword = reply.Password
 
123
                  /* TODO: activate in debug mode? */
 
124
                  //console.log("Account details are: " + reply.Username + ":" + reply.Password )
 
125
              }
 
126
 
 
127
          }
 
128
    }
 
129
 
 
130
    Connections {
 
131
            target: targetPage
 
132
 
 
133
            /*onCompleted: {
 
134
                for (var j = 0; j < accounts.count; j++) {
 
135
                    if (accounts.get(j, "account").accountId === targetPage.accountID) {
 
136
                        console.log("Account auth to get password ... ")
 
137
                        accountConnection.target = accounts.get(j, "account")
 
138
                        accounts.get(j, "account").authenticate({})
 
139
                    }
 
140
                }
 
141
            }*/
 
142
 
 
143
            onActiveChanged: {
 
144
                /* re-render anytime page is shown */
 
145
                console.log("editTargetPage activated")
 
146
                targetPage.loadDB(targetPage.accountID)
 
147
                targetPage.updateDB(targetPage.accountID)
 
148
 
 
149
                console.log("Authenticat accountID: " + targetPage.accountID)
 
150
                console.log("  - account CNT: " + accounts.count)
 
151
 
 
152
                for (var j = 0; j < accounts.count; j++) {
 
153
                    console.log("  - accountID: " + accounts.get(j, "account").accountId)
 
154
                    if (accounts.get(j, "account").accountId === targetPage.accountID) {
 
155
                        console.log("Account auth to get password ... ")
 
156
                        accountConnection.target = accounts.get(j, "account")
 
157
                        accounts.get(j, "account").authenticate({})
 
158
                    }
 
159
                }
 
160
            }
 
161
        }
 
162
 
 
163
 
 
164
    header: PageHeader {
 
165
        id: header
 
166
        title: i18n.tr("Account Settings")
 
167
        flickable: flickable
 
168
 
 
169
        trailingActionBar{
 
170
            actions: [
 
171
 
 
172
            ]
 
173
        }
 
174
    }
 
175
 
 
176
    Flickable {
 
177
        id: flickable
 
178
        anchors.fill: parent
 
179
        contentHeight: accountEditColumn.height
 
180
 
 
181
        Column {
 
182
            id: accountEditColumn
 
183
 
 
184
            spacing: units.gu(1.5)
 
185
            anchors {
 
186
                top: parent.top; left: parent.left; right: parent.right; margins: units.gu(2)
 
187
            }
 
188
 
 
189
            Item {
 
190
                width: parent.width
 
191
                height: targetSymbol.height
 
192
                anchors.topMargin: units.gu(10)
 
193
 
 
194
            Rectangle {
 
195
                id: targetSymbol
 
196
                color: "silver" /*"#0000B5"*/
 
197
                width: units.gu(15)
 
198
                height: units.gu(15)
 
199
                border.width: 0
 
200
                radius: units.gu(2)
 
201
                anchors {
 
202
                   left: parent.left; top: parent.top
 
203
                }
 
204
            }
 
205
 
 
206
            Text {
 
207
                id: targetSymbolText
 
208
                text: "U"  /* Modified by Name change */
 
209
                color: "white"
 
210
                font.pixelSize: units.gu(10)
 
211
                anchors {
 
212
                   horizontalCenter: targetSymbol.horizontalCenter; verticalCenter: targetSymbol.verticalCenter
 
213
                }
 
214
            }
 
215
 
 
216
 
 
217
            TextEdit {
 
218
                id: targetName
 
219
                text: "New Target"
 
220
                anchors.leftMargin: units.gu(2)
 
221
                font.pixelSize: units.gu(3)
 
222
                anchors {
 
223
                   left: targetSymbol.right; top: targetSymbol.top
 
224
                }
 
225
                onTextChanged: {
 
226
                    targetSymbolText.text = "" + text.charAt(0).toUpperCase()
 
227
                    /* Invoke update DB */
 
228
                    targetPage.updateDB()
 
229
                }
 
230
            }
 
231
 
 
232
            Text {
 
233
                id: targetIDText
 
234
                text: "ID: " + targetPage.targetID
 
235
                anchors.leftMargin: units.gu(2)
 
236
                anchors.topMargin: units.gu(1)
 
237
                font.pixelSize: units.gu(3)
 
238
                anchors {
 
239
                   left: targetSymbol.right; top: targetName.bottom
 
240
                }
 
241
                onTextChanged: {
 
242
                    /* Invoke load DB */
 
243
                    targetPage.loadDB()
 
244
                }
 
245
            }
 
246
 
 
247
            }
 
248
 
 
249
            Item {
 
250
                width: parent.width
 
251
                height: accountSymbol.height
 
252
 
 
253
            Rectangle {
 
254
                id: accountSymbol
 
255
                color: "steelblue" /*"#0000B5"*/
 
256
                width: units.gu(6)
 
257
                height: units.gu(6)
 
258
                border.width: 0
 
259
                radius: units.gu(0.9)
 
260
                anchors {
 
261
                   left: parent.left; top: parent.top
 
262
                }
 
263
            }
 
264
 
 
265
            Text {
 
266
                id: accountSymbolText
 
267
                text: "U"  /* Modified by accountName change */
 
268
                color: "white"
 
269
                font.pixelSize: units.gu(4)
 
270
                anchors {
 
271
                   horizontalCenter: accountSymbol.horizontalCenter; verticalCenter: accountSymbol.verticalCenter
 
272
                }
 
273
            }
 
274
 
 
275
 
 
276
            TextEdit {
 
277
                id: accountName
 
278
                text: "Unknown Account"
 
279
                anchors.leftMargin: units.gu(2)
 
280
                font.pixelSize: units.gu(2)
 
281
                anchors {
 
282
                   left: accountSymbol.right; verticalCenter: accountSymbol.verticalCenter
 
283
                }
 
284
                onTextChanged: {
 
285
                    accountSymbolText.text = "" + text.charAt(0).toUpperCase()
 
286
                    /* Invoke update DB */
 
287
                    targetPage.updateDB()
 
288
                }
 
289
            }
 
290
 
 
291
            }
 
292
 
 
293
            Item {
 
294
                anchors.topMargin: units.gu(10)
 
295
                width: parent.width
 
296
                height: localIcon.height
 
297
 
 
298
 
 
299
                MouseArea {
 
300
                    width: localIcon.width
 
301
                    height: localIcon.height
 
302
 
 
303
                    onClicked: {
 
304
                        console.log("Change Local Folder")
 
305
                        apl.addPageToNextColumn(targetPage, Qt.resolvedUrl("LocalFileBrowser.qml"), {caller:localPath})
 
306
                    }
 
307
            Icon {
 
308
                id: localIcon
 
309
                name: "folder-symbolic"
 
310
                width: units.gu(6)
 
311
                height: width
 
312
                anchors {
 
313
                   left: parent.left; top: parent.top
 
314
                }
 
315
                }
 
316
 
 
317
            Text {
 
318
                id: localPath
 
319
                text: ""
 
320
                anchors.leftMargin: units.gu(3)
 
321
                anchors.verticalCenterOffset: 0
 
322
                font.pixelSize: units.gu(2)
 
323
                anchors {
 
324
                   left: localIcon.right; verticalCenter: localIcon.verticalCenter
 
325
                }
 
326
                onTextChanged: {
 
327
                    /* Invoke update DB */
 
328
                    targetPage.updateDB()
 
329
                }
 
330
            }
 
331
 
 
332
            }
 
333
 
 
334
            }
 
335
 
 
336
            Item {
 
337
                anchors.topMargin: units.gu(10)
 
338
                width: parent.width
 
339
                height: remoteIcon.height
 
340
 
 
341
 
 
342
                MouseArea {
 
343
                    width: remoteIcon.width
 
344
                    height: remoteIcon.height
 
345
 
 
346
                    onClicked: {
 
347
                        console.log("Change Remote Folder")
 
348
                         apl.addPageToNextColumn(targetPage, Qt.resolvedUrl("WebdavFileBrowser.qml"), {caller:remotePath, paramUsername: targetPage.accountUser, paramPassword: targetPage.accountPassword, paramServerUrl: targetPage.accountRemoteAddress})
 
349
                    }
 
350
            Icon {
 
351
                id: remoteIcon
 
352
                name: "network-server-symbolic"
 
353
                width: units.gu(6)
 
354
                height: width
 
355
                anchors {
 
356
                   left: parent.left; top: parent.top
 
357
                }
 
358
            }
 
359
 
 
360
            Text {
 
361
                id: remotePath
 
362
                text: ""
 
363
                anchors.leftMargin: units.gu(3)
 
364
                anchors.verticalCenterOffset: 0
 
365
                font.pixelSize: units.gu(2)
 
366
                anchors {
 
367
                   left: remoteIcon.right; verticalCenter: remoteIcon.verticalCenter
 
368
                }
 
369
                onTextChanged: {
 
370
                    /* Invoke update DB */
 
371
                    targetPage.updateDB()
 
372
                }
 
373
            }
 
374
 
 
375
            }
 
376
 
 
377
            }
 
378
 
 
379
            Item {
 
380
                anchors.topMargin: units.gu(10)
 
381
                width: parent.width
 
382
                height: mobileDataLabel.height
 
383
 
 
384
            Label{
 
385
                id: mobileDataLabel
 
386
                font.pixelSize: units.gu(2)
 
387
                anchors.topMargin: units.gu(2)
 
388
                text: i18n.tr("Enable/Disable this Target")
 
389
                anchors {
 
390
                    left: parent.left; top: parent.top
 
391
                }
 
392
            }
 
393
 
 
394
            Switch{
 
395
                id: activeSwitch
 
396
                checked: null
 
397
                anchors {
 
398
                   right: parent.right; verticalCenter: mobileDataLabel.verticalCenter
 
399
                }
 
400
                onCheckedChanged: {
 
401
                    /* Invoke update DB */
 
402
                    targetPage.updateDB()
 
403
                    /* Change activity indication color */
 
404
                    if (activeSwitch.checked == true) {
 
405
                        targetSymbol.color = "orange"
 
406
                    } else {
 
407
                        targetSymbol.color = "silver"
 
408
                    }
 
409
                }
 
410
            }
 
411
 
 
412
            }
 
413
 
 
414
        }
 
415
 
 
416
    }
 
417
 
 
418
 
 
419
}
 
420