/* * Copyright (C) 2012-2015 Canonical, Ltd. * * 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 ContactModel { id: root property var externalFilter: null property string filterTerm: "" property bool onlyFavorites: false property var view: null /* internal */ property bool _clearModel: false property list _extraFilters property QtObject _timeout property string _realManager: ContactManager.defaultManager property var _defaultFilter: root.manager == root._realManager ? collectionFilter : null signal removalFinished() signal removalError() function changeFilter(newFilter) { if (root.contacts.length > 0) { root._clearModel = true } root.externalFilter = newFilter } filter: { if (root._clearModel) { return invalidFilter } else if (contactsFilter.active) { return contactsFilter } else { return root._defaultFilter } } _extraFilters: [ InvalidFilter { id: invalidFilter }, DetailFilter { id: favouritesFilter detail: ContactDetail.Favorite field: Favorite.Favorite value: true matchFlags: DetailFilter.MatchExactly }, CollectionFilter { id: collectionFilter ids: [Contacts.aggregateCollectionId] // aggregate }, UnionFilter { id: contactTermFilter property string value: "" property var phoneNumberFilter: DetailFilter { detail: ContactDetail.PhoneNumber field: PhoneNumber.Number value: contactTermFilter.value matchFlags: (DetailFilter.MatchPhoneNumber | DetailFilter.MatchContains) } filters: [ DetailFilter { id: nameFilter detail: ContactDetail.DisplayLabel field: DisplayLabel.Label value: contactTermFilter.value matchFlags: DetailFilter.MatchContains } ] onValueChanged: { var containsLetter = Contacts.containsLetters(value) if (containsLetter && (filters.length > 1)) { filters = [nameFilter] } else if (!containsLetter) { filters = [nameFilter, phoneNumberFilter] } } }, IntersectionFilter { id: contactsFilter // avoid runtime warning "depends on non-NOTIFYable properties" readonly property alias filtersProxy: contactsFilter.filters property bool active: { var filters_ = [] if (contactTermFilter.value.length > 0) { if (root._defaultFilter) { filters_.push(root._defaultFilter) } filters_.push(contactTermFilter) } else if (root.onlyFavorites) { filters_.push(favouritesFilter) } if (root.externalFilter) { filters_.push(root.externalFilter) } // check if the filter has changed var oldFilters = filtersProxy if (oldFilters.length !== filters_.length) { contactsFilter.filters = filters_ } else { for(var i=0; i < oldFilters.length; i++) { if (filters_.indexOf(oldFilters[i]) === -1) { contactsFilter.filters = filters_ } } } return (filters_.length > 0) } } ] _timeout: Timer { id: contactSearchTimeout running: false repeat: false interval: 300 onTriggered: { if (root.view) { view.positionViewAtBeginning() } root.changeFilter(root.externalFilter) contactTermFilter.value = root.filterTerm.trim() // manually update if autoUpdate is disabled if (!root.autoUpdate) { root.update() } } } onFilterTermChanged: { var newFilterTerm = root.filterTerm.trim() if (contactTermFilter.value != newFilterTerm) contactSearchTimeout.restart() } onErrorChanged: { if (error) { console.error("Contact List error:" + error) } } onContactsChanged: { if (root._clearModel) { root._clearModel = false // do a new update if autoUpdate is false if (!root.autoUpdate) { root.update() } } } /* TODO: once https://bugreports.qt.io/browse/QTBUG-87807 is fixed, we can * remove both these models and use the new relatedContacts() method. */ property var _relationshipModel: Component { RelationshipModel { id: rsModel property var callback: null manager: root.manager relationshipType: "Aggregates" role: Relationship.First onRelationshipsChanged: { var ids = [] for (var i = 0; i < relationships.length; i++) { ids.push(relationships[i].second) } callback(ids) rsModel.destroy() } } } property var relationshipModel: null function enumerateConstituentIds(contact, callback) { var relationshipCb = function(contactIds) { callback(contactIds) } // when contact come from IdFilters ( i.e from import ), the ids returned are not from the aggregate contacts var role = root.externalFilter && root.externalFilter.type === Filter.IdFilter ? Relationship.Second : Relationship.First relationshipModel = _relationshipModel.createObject(null, { "callback": relationshipCb, "participant": contact, "role": role }) } function enumerateConstituents(contact, callback) { var requestId = 0 var onConstituentIds = function(contactIds) { requestId = root.fetchContacts(contactIds) root.onContactsFetched.connect(function(reqId, contacts) { if (reqId != requestId) return callback(contacts) }) } enumerateConstituentIds(contact, onConstituentIds) } function removeAggregateContacts(contacts) { const aggregateCtids = contacts.map(({contactId}) => contactId) Contacts.removeAggregateContacts(aggregateCtids, function(success) { if (success) { root.removalFinished() } else { root.removalError() } }) } }