Debian Codegraph @

src/imports/Lomiri/Contacts/AddressBooksSelector.qml

raw ◀ ▶
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 if (count > 0) {
54 console.log('accountModel count:', count)
55 update()
56 }
57 }
58
59 function getAccount(accountId) {
60 return accountModel.accountList.find((account) => account.accountId === accountId);
61 }
62 }
63
64 ListModel {
65 id: writableSources
66
67 function populate() {
68 clear()
69
70 if (!contactModel) {
71 return
72 }
73
•74 var collections = ContactsJS.getCollections(contactModel)
75 for(var i = 0 ; i < collections.length ; ++i) {
•76 var col = collections[i];
•77 if (!col.readOnly && !col.isAggregate) {
78 let accountName = i18n.dtr("lomiri-addressbook-app", "Personal")
79 const account = accountModel.getAccount(col.accountId)
80 if (account) {
81 accountName = account.displayName
82 }
•83 col.accountName = accountName
84
85 append(col)
86 }
87 }
88 }
89 }
90
91 OptionSelector {
92 id: sources
93 property bool _notify: true
94
95 model: writableSources
96
97 delegate: OptionSelectorDelegate {
98 text: name
99 subText: accountName
100
101 height: units.gu(4)
102 }
103
104 containerHeight: sources.model && sources.model.count > 4 ? itemHeight * 4 : sources.model ? itemHeight * sources.model.count : 0
105
106 onDelegateClicked: {
•107 if (selectedIndex !== index) {
108 const ab = model.get(index)
109 console.log('changed address book to', ab.name)
110 addressBookChanged(ab.collectionId)
111 }
112 }
113 }
114 }