/* * Copyright (C) 2024 UBports Foundation * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; version 3. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ import QtQuick 2.4 import QtContacts 5.0 import Lomiri.Contacts 0.1 import Lomiri.Components 1.3 import Lomiri.OnlineAccounts 2.0 Item { property var contactModel: null property var currentCollectionId: null signal addressBookChanged(string collectionId) function update() { writableSources.populate() if (currentCollectionId != null) { for( var i = 0; i < writableSources.count; i++ ) { console.log(writableSources.get(i).collectionId, currentCollectionId) if (writableSources.get(i).collectionId === currentCollectionId) { sources.selectedIndex = i break } } } } onContactModelChanged: { update() } AccountModel { id: accountModel applicationId: "lomiri-addressbook-app" onCountChanged: { if (count > 0) { console.log('accountModel count:', count) update() } } function getAccount(accountId) { return accountModel.accountList.find((account) => account.accountId === accountId); } } ListModel { id: writableSources function populate() { clear() if (!contactModel) { return } var collections = ContactsJS.getCollections(contactModel) for(var i = 0 ; i < collections.length ; ++i) { var col = collections[i]; if (!col.readOnly && !col.isAggregate) { let accountName = i18n.dtr("lomiri-addressbook-app", "Personal") const account = accountModel.getAccount(col.accountId) if (account) { accountName = account.displayName } col.accountName = accountName append(col) } } } } OptionSelector { id: sources property bool _notify: true model: writableSources delegate: OptionSelectorDelegate { text: name subText: accountName height: units.gu(4) } containerHeight: sources.model && sources.model.count > 4 ? itemHeight * 4 : sources.model ? itemHeight * sources.model.count : 0 onDelegateClicked: { if (selectedIndex !== index) { const ab = model.get(index) console.log('changed address book to', ab.name) addressBookChanged(ab.collectionId) } } } }