Debian Codegraph @

src/imports/Lomiri/Contacts/ContactListModel.qml

raw ◀ ▶
Source reconstructed at patch state 6 of 7 — not the installed (fully patched) source.
7 Debian patches touch this file
1 /*
2 * Copyright (C) 2012-2015 Canonical, Ltd.
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
17 import QtQuick 2.4
18 import QtContacts 5.0
19 import Lomiri.Contacts 0.1
20
21 ContactModel {
22 id: root
23
24 property var externalFilter: null
25 property string filterTerm: ""
26 property bool onlyFavorites: false
27 property var view: null
28
29 /* internal */
30 property bool _clearModel: false
31 property list<QtObject> _extraFilters
32 property QtObject _timeout
33 property string _realManager: ContactManager.defaultManager
34 property var _defaultFilter: root.manager == root._realManager ? collectionFilter : null
35
36 signal removalFinished()
37 signal removalError()
38
39 function changeFilter(newFilter)
40 {
41 if (root.contacts.length > 0) {
42 root._clearModel = true
43 }
44 root.externalFilter = newFilter
45 }
46
47
48 filter: {
49 if (root._clearModel) {
50 return invalidFilter
51 } else if (contactsFilter.active) {
52 return contactsFilter
53 } else {
54 return root._defaultFilter
55 }
56 }
57
58 _extraFilters: [
59 InvalidFilter {
60 id: invalidFilter
61 },
62 DetailFilter {
63 id: favouritesFilter
64
65 detail: ContactDetail.Favorite
66 field: Favorite.Favorite
67 value: true
68 matchFlags: DetailFilter.MatchExactly
69 },
70 CollectionFilter {
71 id: collectionFilter
72 ids: [Contacts.aggregateCollectionId] // aggregate
73 },
74 UnionFilter {
75 id: contactTermFilter
76
77 property string value: ""
78 property var phoneNumberFilter: DetailFilter {
79 detail: ContactDetail.PhoneNumber
80 field: PhoneNumber.Number
81 value: contactTermFilter.value
82 matchFlags: (DetailFilter.MatchPhoneNumber | DetailFilter.MatchContains)
83 }
84
85 filters: [
86 DetailFilter {
87 id: nameFilter
88 detail: ContactDetail.DisplayLabel
89 field: DisplayLabel.Label
90 value: contactTermFilter.value
91 matchFlags: DetailFilter.MatchContains
92 }
93 ]
94
95 onValueChanged: {
96 var containsLetter = Contacts.containsLetters(value)
97 if (containsLetter && (filters.length > 1)) {
98 filters = [nameFilter]
99 } else if (!containsLetter) {
100 filters = [nameFilter, phoneNumberFilter]
101 }
102 }
103 },
104 IntersectionFilter {
105 id: contactsFilter
106
107 // avoid runtime warning "depends on non-NOTIFYable properties"
108 readonly property alias filtersProxy: contactsFilter.filters
109
110 property bool active: {
111 var filters_ = []
112 if (contactTermFilter.value.length > 0) {
113 if (root._defaultFilter) {
114 filters_.push(root._defaultFilter)
115 }
116 filters_.push(contactTermFilter)
117 } else if (root.onlyFavorites) {
118 filters_.push(favouritesFilter)
119 }
120
121 if (root.externalFilter) {
122 filters_.push(root.externalFilter)
123 }
124
125 // check if the filter has changed
126 var oldFilters = filtersProxy
127 if (oldFilters.length !== filters_.length) {
128 contactsFilter.filters = filters_
129 } else {
130 for(var i=0; i < oldFilters.length; i++) {
131 if (filters_.indexOf(oldFilters[i]) === -1) {
132 contactsFilter.filters = filters_
133 }
134 }
135 }
136
137 return (filters_.length > 0)
138 }
139 }
140 ]
141
142 _timeout: Timer {
143 id: contactSearchTimeout
144
145 running: false
146 repeat: false
147 interval: 300
148 onTriggered: {
149 if (root.view) {
150 view.positionViewAtBeginning()
151 }
152
153 root.changeFilter(root.externalFilter)
154 contactTermFilter.value = root.filterTerm.trim()
155
156 // manually update if autoUpdate is disabled
157 if (!root.autoUpdate) {
158 root.update()
159 }
160 }
161 }
162
163 onFilterTermChanged: {
164 var newFilterTerm = root.filterTerm.trim()
165 if (contactTermFilter.value != newFilterTerm)
166 contactSearchTimeout.restart()
167 }
168
169 onErrorChanged: {
170 if (error) {
171 console.error("Contact List error:" + error)
172 }
173 }
174
175 onContactsChanged: {
176 if (root._clearModel) {
177 root._clearModel = false
178 // do a new update if autoUpdate is false
179 if (!root.autoUpdate) {
180 root.update()
181 }
182 }
183 }
184
185 /* TODO: once https://bugreports.qt.io/browse/QTBUG-87807 is fixed, we can
186 * remove both these models and use the new relatedContacts() method.
187 */
188 property var _relationshipModel: Component {
189 RelationshipModel {
190 id: rsModel
191 property var callback: null
192 manager: root.manager
193 relationshipType: "Aggregates"
194 role: Relationship.First
195 onRelationshipsChanged: {
196 var ids = []
197 for (var i = 0; i < relationships.length; i++) {
198 ids.push(relationships[i].second)
199 }
200 callback(ids)
201 rsModel.destroy()
202 }
203 }
204 }
205
206 function enumerateConstituentIds(contact, callback) {
207 var relationshipCb = function(contactIds) {
208 callback(contactIds)
209 }
210 // when contact come from IdFilters ( i.e from import ), the ids returned are not from the aggregate contacts
211 var role = root.externalFilter && root.externalFilter.type === Filter.IdFilter ? Relationship.Second : Relationship.First
212
213 relationshipModel = _relationshipModel.createObject(null, {
214 "callback": relationshipCb,
215 "participant": contact,
216 "role": role
217 })
218 }
219
220 function enumerateConstituents(contact, callback) {
221 var requestId = 0
222 var onConstituentIds = function(contactIds) {
223 requestId = root.fetchContacts(contactIds)
224 root.onContactsFetched.connect(function(reqId, contacts) {
225 if (reqId != requestId) return
226 callback(contacts)
227 })
228 }
229 enumerateConstituentIds(contact, onConstituentIds)
230 }
231
232 function removeAggregateContacts(contacts) {
233
234 const aggregateCtids = contacts.map(({contactId}) => contactId)
235 Contacts.removeAggregateContacts(aggregateCtids, function(success) {
236 if (success) {
237 root.removalFinished()
238 } else {
239 root.removalError()
240 }
241 })
242 }
243 }
244