| 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 |
function changeFilter(newFilter) |
| 37 |
{ |
| 38 |
if (root.contacts.length > 0) { |
| 39 |
root._clearModel = true |
| 40 |
} |
| 41 |
root.externalFilter = newFilter |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
filter: { |
| 46 |
if (root._clearModel) { |
| 47 |
return invalidFilter |
| 48 |
} else if (contactsFilter.active) { |
| 49 |
return contactsFilter |
| 50 |
} else { |
| 51 |
return root._defaultFilter |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
_extraFilters: [ |
| 56 |
InvalidFilter { |
| 57 |
id: invalidFilter |
| 58 |
}, |
| 59 |
DetailFilter { |
| 60 |
id: favouritesFilter |
| 61 |
|
| 62 |
detail: ContactDetail.Favorite |
| 63 |
field: Favorite.Favorite |
| 64 |
value: true |
| 65 |
matchFlags: DetailFilter.MatchExactly |
| 66 |
}, |
| 67 |
CollectionFilter { |
| 68 |
id: collectionFilter |
| 69 |
ids: ["qtcontacts:org.nemomobile.contacts.sqlite::636f6c2d31"] // aggregate |
| 70 |
}, |
| 71 |
UnionFilter { |
| 72 |
id: contactTermFilter |
| 73 |
|
| 74 |
property string value: "" |
| 75 |
property var phoneNumberFilter: DetailFilter { |
| 76 |
detail: ContactDetail.PhoneNumber |
| 77 |
field: PhoneNumber.Number |
| 78 |
value: contactTermFilter.value |
| 79 |
matchFlags: (DetailFilter.MatchPhoneNumber | DetailFilter.MatchContains) |
| 80 |
} |
| 81 |
|
| 82 |
filters: [ |
| 83 |
DetailFilter { |
| 84 |
id: nameFilter |
| 85 |
detail: ContactDetail.DisplayLabel |
| 86 |
field: DisplayLabel.Label |
| 87 |
value: (root.manager === root._realManager ? Contacts.normalized(contactTermFilter.value) : contactTermFilter.value) |
| 88 |
matchFlags: DetailFilter.MatchContains |
| 89 |
} |
| 90 |
] |
| 91 |
|
| 92 |
onValueChanged: { |
| 93 |
var containsLetter = Contacts.containsLetters(value) |
| 94 |
if (containsLetter && (filters.length > 1)) { |
| 95 |
filters = [nameFilter] |
| 96 |
} else if (!containsLetter) { |
| 97 |
filters = [nameFilter, phoneNumberFilter] |
| 98 |
} |
| 99 |
} |
| 100 |
}, |
| 101 |
IntersectionFilter { |
| 102 |
id: contactsFilter |
| 103 |
|
| 104 |
// avoid runtime warning "depends on non-NOTIFYable properties" |
| 105 |
readonly property alias filtersProxy: contactsFilter.filters |
| 106 |
|
| 107 |
property bool active: { |
| 108 |
var filters_ = [] |
| 109 |
if (contactTermFilter.value.length > 0) { |
| 110 |
if (root._defaultFilter) { |
| 111 |
filters_.push(root._defaultFilter) |
| 112 |
} |
| 113 |
filters_.push(contactTermFilter) |
| 114 |
} else if (root.onlyFavorites) { |
| 115 |
filters_.push(favouritesFilter) |
| 116 |
} |
| 117 |
|
| 118 |
if (root.externalFilter) { |
| 119 |
filters_.push(root.externalFilter) |
| 120 |
} |
| 121 |
|
| 122 |
// check if the filter has changed |
| 123 |
var oldFilters = filtersProxy |
| 124 |
if (oldFilters.length !== filters_.length) { |
| 125 |
contactsFilter.filters = filters_ |
| 126 |
} else { |
| 127 |
for(var i=0; i < oldFilters.length; i++) { |
| 128 |
if (filters_.indexOf(oldFilters[i]) === -1) { |
| 129 |
contactsFilter.filters = filters_ |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
|
| 134 |
return (filters_.length > 0) |
| 135 |
} |
| 136 |
} |
| 137 |
] |
| 138 |
|
| 139 |
_timeout: Timer { |
| 140 |
id: contactSearchTimeout |
| 141 |
|
| 142 |
running: false |
| 143 |
repeat: false |
| 144 |
interval: 300 |
| 145 |
onTriggered: { |
| 146 |
if (root.view) { |
| 147 |
view.positionViewAtBeginning() |
| 148 |
} |
| 149 |
|
| 150 |
root.changeFilter(root.externalFilter) |
| 151 |
contactTermFilter.value = root.filterTerm.trim() |
| 152 |
|
| 153 |
// manually update if autoUpdate is disabled |
| 154 |
if (!root.autoUpdate) { |
| 155 |
root.update() |
| 156 |
} |
| 157 |
} |
| 158 |
} |
| 159 |
|
| 160 |
onFilterTermChanged: { |
| 161 |
var newFilterTerm = root.filterTerm.trim() |
| 162 |
if (contactTermFilter.value != newFilterTerm) |
| 163 |
contactSearchTimeout.restart() |
| 164 |
} |
| 165 |
|
| 166 |
onErrorChanged: { |
| 167 |
if (error) { |
| 168 |
console.error("Contact List error:" + error) |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
onContactsChanged: { |
| 173 |
if (root._clearModel) { |
| 174 |
root._clearModel = false |
| 175 |
// do a new update if autoUpdate is false |
| 176 |
if (!root.autoUpdate) { |
| 177 |
root.update() |
| 178 |
} |
| 179 |
|
| 180 |
} |
| 181 |
} |
| 182 |
|
| 183 |
/* TODO: once https://bugreports.qt.io/browse/QTBUG-87807 is fixed, we can |
| 184 |
* remove both these models and use the new relatedContacts() method. |
| 185 |
*/ |
| 186 |
property var _relationshipModel: Component { |
| 187 |
RelationshipModel { |
| 188 |
property var callback: null |
| 189 |
manager: root.manager |
| 190 |
relationshipType: "Aggregates" |
| 191 |
role: Relationship.First |
| 192 |
onRelationshipsChanged: inspectModel() |
| 193 |
function inspectModel() { |
| 194 |
var ids = [] |
| 195 |
for (var i = 0; i < relationships.length; i++) { |
| 196 |
ids.push(relationships[i].second) |
| 197 |
} |
| 198 |
callback(ids) |
| 199 |
} |
| 200 |
} |
| 201 |
} |
| 202 |
|
| 203 |
property var relationshipModel: null |
| 204 |
function enumerateConstituentIds(contact, callback) { |
| 205 |
var relationshipCb = function(contactIds) { |
| 206 |
callback(contactIds) |
| 207 |
relationshipModel.destroy() |
| 208 |
} |
| 209 |
relationshipModel = _relationshipModel.createObject(null, { |
| 210 |
"callback": relationshipCb, |
| 211 |
"participant": contact, |
| 212 |
}) |
| 213 |
} |
| 214 |
|
| 215 |
function enumerateConstituents(contact, callback) { |
| 216 |
var requestId = 0 |
| 217 |
var onConstituentIds = function(contactIds) { |
| 218 |
requestId = root.fetchContacts(contactIds) |
| 219 |
root.onContactsFetched.connect(function(reqId, contacts) { |
| 220 |
if (reqId != requestId) return |
| 221 |
callback(contacts) |
| 222 |
}) |
| 223 |
} |
| 224 |
enumerateConstituentIds(contact, onConstituentIds) |
| 225 |
} |
| 226 |
|
| 227 |
function removeAggregateContact(contact) { |
| 228 |
console.log("Removing aggregate contact " + contact) |
| 229 |
enumerateConstituentIds(contact, function(contactIds) { |
| 230 |
removeContacts(contactIds) |
| 231 |
}) |
| 232 |
} |
| 233 |
} |
| 234 |
|