| 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 |
|
| 34 |
|
| 35 |
function changeFilter(newFilter) |
| 36 |
{ |
| 37 |
if (root.contacts.length > 0) { |
| 38 |
root._clearModel = true |
| 39 |
} |
| 40 |
root.externalFilter = newFilter |
| 41 |
} |
| 42 |
|
| 43 |
|
| 44 |
filter: { |
| 45 |
if (root._clearModel) { |
| 46 |
return invalidFilter |
| 47 |
} else if (contactsFilter.active) { |
| 48 |
return contactsFilter |
| 49 |
} else { |
| 50 |
return null |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
_extraFilters: [ |
| 55 |
InvalidFilter { |
| 56 |
id: invalidFilter |
| 57 |
}, |
| 58 |
DetailFilter { |
| 59 |
id: favouritesFilter |
| 60 |
|
| 61 |
detail: ContactDetail.Favorite |
| 62 |
field: Favorite.Favorite |
| 63 |
value: true |
| 64 |
matchFlags: DetailFilter.MatchExactly |
| 65 |
}, |
| 66 |
UnionFilter { |
| 67 |
id: contactTermFilter |
| 68 |
|
| 69 |
property string value: "" |
| 70 |
property var phoneNumberFilter: DetailFilter { |
| 71 |
detail: ContactDetail.PhoneNumber |
| 72 |
field: PhoneNumber.Number |
| 73 |
value: contactTermFilter.value |
| 74 |
matchFlags: (DetailFilter.MatchPhoneNumber | DetailFilter.MatchContains) |
| 75 |
} |
| 76 |
|
| 77 |
filters: [ |
| 78 |
DetailFilter { |
| 79 |
id: nameFilter |
| 80 |
detail: (root.manager === "galera" ? ContactDetail.ExtendedDetail : ContactDetail.DisplayLabel) |
| 81 |
field: (root.manager === "galera" ? ExtendedDetail.Data : DisplayLabel.Label) |
| 82 |
value: (root.manager === "galera" ? Contacts.normalized(contactTermFilter.value) : contactTermFilter.value) |
| 83 |
matchFlags: DetailFilter.MatchContains |
| 84 |
} |
| 85 |
] |
| 86 |
|
| 87 |
onValueChanged: { |
| 88 |
var containsLetter = Contacts.containsLetters(value) |
| 89 |
if (containsLetter && (filters.length > 1)) { |
| 90 |
filters = [nameFilter] |
| 91 |
} else if (!containsLetter) { |
| 92 |
filters = [nameFilter, phoneNumberFilter] |
| 93 |
} |
| 94 |
} |
| 95 |
}, |
| 96 |
IntersectionFilter { |
| 97 |
id: contactsFilter |
| 98 |
|
| 99 |
// avoid runtime warning "depends on non-NOTIFYable properties" |
| 100 |
readonly property alias filtersProxy: contactsFilter.filters |
| 101 |
|
| 102 |
property bool active: { |
| 103 |
var filters_ = [] |
| 104 |
if (contactTermFilter.value.length > 0) { |
| 105 |
filters_.push(contactTermFilter) |
| 106 |
} else if (root.onlyFavorites) { |
| 107 |
filters_.push(favouritesFilter) |
| 108 |
} |
| 109 |
|
| 110 |
if (root.externalFilter) { |
| 111 |
filters_.push(root.externalFilter) |
| 112 |
} |
| 113 |
|
| 114 |
// check if the filter has changed |
| 115 |
var oldFilters = filtersProxy |
| 116 |
if (oldFilters.length !== filters_.length) { |
| 117 |
contactsFilter.filters = filters_ |
| 118 |
} else { |
| 119 |
for(var i=0; i < oldFilters.length; i++) { |
| 120 |
if (filters_.indexOf(oldFilters[i]) === -1) { |
| 121 |
contactsFilter.filters = filters_ |
| 122 |
} |
| 123 |
} |
| 124 |
} |
| 125 |
|
| 126 |
return (filters_.length > 0) |
| 127 |
} |
| 128 |
} |
| 129 |
] |
| 130 |
|
| 131 |
_timeout: Timer { |
| 132 |
id: contactSearchTimeout |
| 133 |
|
| 134 |
running: false |
| 135 |
repeat: false |
| 136 |
interval: 300 |
| 137 |
onTriggered: { |
| 138 |
if (root.view) { |
| 139 |
view.positionViewAtBeginning() |
| 140 |
} |
| 141 |
|
| 142 |
root.changeFilter(root.externalFilter) |
| 143 |
contactTermFilter.value = root.filterTerm.trim() |
| 144 |
|
| 145 |
// manually update if autoUpdate is disabled |
| 146 |
if (!root.autoUpdate) { |
| 147 |
root.update() |
| 148 |
} |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
onFilterTermChanged: { |
| 153 |
var newFilterTerm = root.filterTerm.trim() |
| 154 |
if (contactTermFilter.value != newFilterTerm) |
| 155 |
contactSearchTimeout.restart() |
| 156 |
} |
| 157 |
|
| 158 |
onErrorChanged: { |
| 159 |
if (error) { |
| 160 |
console.error("Contact List error:" + error) |
| 161 |
} |
| 162 |
} |
| 163 |
|
| 164 |
onContactsChanged: { |
| 165 |
if (root._clearModel) { |
| 166 |
root._clearModel = false |
| 167 |
// do a new update if autoUpdate is false |
| 168 |
if (!root.autoUpdate) { |
| 169 |
root.update() |
| 170 |
} |
| 171 |
|
| 172 |
} |
| 173 |
} |
| 174 |
} |
| 175 |
|