Debian Codegraph @

src/imports/Lomiri/Contacts/AddressBooksSelector.qml

raw ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
1 /*
2 * Copyright (C) 2024 UBports Foundation
3 *
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; version 3.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License
14 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 */
16 import QtQuick 2.4
17 import QtContacts 5.0
18
19 import Lomiri.Contacts 0.1
20 import Lomiri.Components 1.3
21
22 import Lomiri.OnlineAccounts 2.0
23
24 Item {
25
26 property var contactModel: null
27 property var currentCollectionId: null
28
29 signal addressBookChanged(string collectionId)
30
31 function update() {
32 writableSources.populate()
33 if (currentCollectionId != null) {
34 for( var i = 0; i < writableSources.count; i++ ) {
35 console.log(writableSources.get(i).collectionId, currentCollectionId)
36 if (writableSources.get(i).collectionId === currentCollectionId) {
37 sources.selectedIndex = i
38 break
39 }
40 }
41 }
42 }
43
44 onContactModelChanged: {
45 update()
46 }
47
48 AccountModel {
49 id: accountModel
50
51 applicationId: "lomiri-addressbook-app"
52 onCountChanged: {
53 console.debug("Model is Ready:" + ready)
54 console.debug("Model count:" + count)
55 if (count > 0) {
56 console.log('accountModel count:', count)
57 update()
58 }
59 }
60
61 function getAccount(accountId) {
62 return accountModel.accountList.find((account) => account.accountId === accountId);
63 }
64 }
65
66 ListModel {
67 id: writableSources
68
69 function populate() {
70 clear()
71
72 if (!contactModel) {
73 return
74 }
75
76 var collections = contactModel.collections;
77 for(var i = 0 ; i < collections.length ; ++i) {
78 var col = collections[i]
79 const isReadOnly = col.extendedMetaData("ReadOnly") === true
80 const isAggregate = col.collectionId === Contacts.aggregateCollectionId
81 const accountId = col.extendedMetaData("AccountId")
82 let accountName = i18n.dtr("lomiri-addressbook-app", "Personal")
83 const account = accountModel.getAccount(accountId)
84 if (account) {
85 accountName = account.displayName
86 }
87
88 //console.log(JSON.stringify(col))
89 if (!isReadOnly && !isAggregate) {
90 var item = {
91 'collectionId': col.collectionId,
92 'name': col.name,
93 'description': col.description,
94 'isLocal': col.extendedMetaData("AccountId") === 0,
95 'accountName': accountName
96 }
97 append(item)
98 }
99 }
100 }
101 }
102
103 OptionSelector {
104 id: sources
105 property bool _notify: true
106
107 model: writableSources
108
109 delegate: OptionSelectorDelegate {
110 text: name
111 subText: accountName
112
113 height: units.gu(4)
114 }
115
116 containerHeight: sources.model && sources.model.count > 4 ? itemHeight * 4 : sources.model ? itemHeight * sources.model.count : 0
117
118 onDelegateClicked: {
119 const ab = model.get(index)
120 console.log('changed address book to', ab.name)
121 addressBookChanged(ab.collectionId)
122 }
123 }
124 }