Debian Codegraph @

src/imports/Lomiri/Contacts/ContactSimpleListView.qml

raw ◀ ▶
Source reconstructed at patch state 1 of 3 — not the installed (fully patched) source.
3 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.Components 1.3
20 import Lomiri.Components.ListItems 1.3 as ListItem
21 import Lomiri.Contacts 0.1
22
23 import "ContactList.js" as Sections
24
25 /*!
26 \qmltype ContactSimpleListView
27 \inqmlmodule Lomiri.Contacts 0.1
28 \ingroup ubuntu
29 \brief The ContactSimpleListView provides a simple contact list view
30
31 The ContactSimpleListView provide a easy way to show the contact list view
32 with all default visuals defined by Ubuntu system.
33
34 Example:
35 \qml
36 import Lomiri.Contacts 0.1
37
38 ContactSimpleListView {
39 anchors.fill: parent
40 onContactClicked: console.debug("Contact ID:" + contactId)
41 }
42 \endqml
43 */
44
45 MultipleSelectionListView {
46 id: contactListView
47
48 /*!
49 \qmlproperty bool showAvatar
50
51 This property holds if the contact avatar will appear on the list or not.
52 By default this is set to true.
53 */
54 property bool showAvatar: true
55
56 /*!
57 \qmlproperty list<SortOrder> sortOrders
58
59 This property holds a list of sort orders used by the contacts model.
60 \sa SortOrder
61 */
62 property list<SortOrder> sortOrders : [
63 SortOrder {
64 detail: ContactDetail.Tag
65 field: Tag.Tag
66 direction: Qt.AscendingOrder
67 blankPolicy: SortOrder.BlanksLast
68 caseSensitivity: Qt.CaseInsensitive
69 },
70 // empty tags will be sorted by display Label
71 SortOrder {
72 detail: ContactDetail.DisplayLabel
73 field: DisplayLabel.Label
74 direction: Qt.AscendingOrder
75 blankPolicy: SortOrder.BlanksLast
76 caseSensitivity: Qt.CaseInsensitive
77 }
78 ]
79 /*!
80 \qmlproperty FetchHint fetchHint
81
82 This property holds the fetch hint instance used by the contact model.
83
84 \sa FetchHint
85 */
86 property var fetchHint : FetchHint {
87 detailTypesHint: {
88 var hints = [ ContactDetail.Tag, // sections
89 ContactDetail.DisplayLabel // label
90 ]
91
92 if (contactListView.showAvatar) {
93 hints.push(ContactDetail.Avatar)
94 }
95 return hints
96 }
97 }
98 /*!
99 \qmlproperty bool multiSelectionEnabled
100
101 This property holds if the multi selection mode is enabled or not
102 By default this is set to false
103 */
104 property bool multiSelectionEnabled: false
105 /*!
106 \qmlproperty string defaultAvatarImage
107
108 This property holds the default image url to be used when the current contact does
109 not contains a photo
110 */
111 property string defaultAvatarImageUrl: "image://theme/contact"
112 /*!
113 \qmlproperty bool loading
114
115 This property holds when the model still loading new contacts
116 */
117 readonly property bool loading: busyIndicator.busy
118 /*!
119 \qmlproperty bool showSections
120
121 This property holds if the listview will show or not the section headers
122 By default this is set to true
123 */
124 property bool showSections: true
125
126 /*!
127 \qmlproperty string manager
128
129 This property holds the manager uri of the contact backend engine.
130 By default this is set to "org.nemomobile.contacts.sqlite"
131 */
132 property string manager: ContactManager.manager()
133
134 /*!
135 \qmlproperty Action leftSideAction
136
137 This property holds the available actions when swipe the contact item from left to right
138 */
139 property Action leftSideAction
140
141 /*!
142 \qmlproperty list<Action> rightSideActions
143
144 This property holds the available actions when swipe the contact item from right to left
145 */
146 property list<Action> rightSideActions
147
148 /*!
149 \qmlproperty highlightSelected
150
151 This property holds if the current contact should be highlighted or not
152 */
153 property bool highlightSelected: false
154
155 /* internal */
156 property var _currentSwipedItem: null
157
158 /*!
159 This handler is called when any error occurs in the contact model
160 */
161 signal error(string message)
162 /*!
163 This handler is called when any contact in the list receives a click
164 */
165 signal contactClicked(QtObject contact)
166 /*!
167 This handler is called when the contact delegate disapear (height === 0) caused by the function call makeDisappear
168 */
169 signal contactDisappeared(QtObject contact)
170 /*!
171 Retrieve the contact index inside of the list based on contact id or contact name if the id is empty
172 */
173 function getIndex(contact)
174 {
175 var contacts = listModel.contacts
176 var contactId = null
177 var firstName
178 var middleName
179 var lastName
180
181 if (contact.contactId !== "qtcontacts:::") {
182 contactId = contact.contactId
183 } else {
184 firstName = contact.name.firstName
185 middleName = contact.name.middleName
186 lastName = contact.name.lastName
187 }
188
189 for (var i = 0, count = contacts.length; i < count; i++) {
190 var c = contacts[i]
191 if (contactId && (c.contactId === contactId)) {
192 return i
193 } else if ((c.name.firstName === firstName) &&
194 (c.name.middleName === middleName) &&
195 (c.name.lastName === lastName)) {
196 return i
197 }
198 }
199
200 return -1
201 }
202
203 /*!
204 Scroll the list to requested contact if the contact exists in the list
205 */
206 function positionViewAtContact(contact)
207 {
208 currentIndex = getIndex(contact)
209 positionViewAtIndex(currentIndex, ListView.Center)
210 }
211
212
213 /*!
214 Scroll the list to requested contact if the contact exists in the list
215 */
216 function positionViewAtContactId(contactId)
217 {
218 var contacts = listModel.contacts
219
220 for (var i = 0, count = contacts.length; i < count; i++) {
221 var c = contacts[i]
222 if (c.contactId === contactId) {
223 currentIndex = i
224 positionViewAtIndex(i, ListView.Center)
225 return
226 }
227 }
228 }
229
230 function getSectionText(index) {
231 return Sections.sectionValueForContact(listModel.contacts[index])
232 }
233
234 /*!
235 private
236 Fetch contact and emit contact clicked signal
237 */
238 function _fetchContact(index, contact)
239 {
240 if (contact) {
241 contactFetch.fetchContact(contact.contactId)
242 }
243 }
244
245 function _updateSwipeState(item)
246 {
247 if (item.swipping) {
248 return
249 }
250
251 if (item.swipeState !== "Normal") {
252 if (contactListView._currentSwipedItem !== item) {
253 if (contactListView._currentSwipedItem) {
254 contactListView._currentSwipedItem.resetSwipe()
255 }
256 contactListView._currentSwipedItem = item
257 }
258 } else if (item.swipeState !== "Normal" && contactListView._currentSwipedItem === item) {
259 contactListView._currentSwipedItem = null
260 }
261 }
262
263 highlightFollowsCurrentItem: true
264 section {
265 property: showSections ? "contact.displayLabel.label" : ""
266 criteria: ViewSection.FirstCharacter
267 labelPositioning: ViewSection.InlineLabels
268 delegate: SectionDelegate {
269 anchors {
270 left: parent.left
271 right: parent.right
272 margins: units.gu(2)
273 }
274 text: section != "" ? section.toUpperCase() : "#"
275 }
276 }
277
278 onCountChanged: {
279 busyIndicator.ping()
280 dirtyModel.restart()
281 }
282
283 listDelegate: ContactDelegate {
284 id: contactDelegate
285
286 property var removalAnimation
287
288 function remove()
289 {
290 removalAnimation.start()
291 }
292
293 flicking: contactListView.flicking
294 width: parent.width
295 selected: (contactListView.multiSelectionEnabled && contactListView.isSelected(contactDelegate))
296 || (!contactListView.isInSelectionMode && contactListView.highlightSelected && (contactListView.currentIndex == index))
297 selectionMode: contactListView.isInSelectionMode
298 defaultAvatarUrl: contactListView.defaultAvatarImageUrl
299 isCurrentItem: ListView.isCurrentItem
300
301 // actions
302 leftSideAction: contactListView.leftSideAction
303 rightSideActions: contactListView.rightSideActions
304
305 // used by swipe to delete
306 removalAnimation: SequentialAnimation {
307 alwaysRunToEnd: true
308
309 PropertyAction {
310 target: contactDelegate
311 property: "ListView.delayRemove"
312 value: true
313 }
314 LomiriNumberAnimation {
315 target: contactDelegate
316 property: "height"
317 to: 1
318 }
319 PropertyAction {
320 target: contactDelegate
321 property: "ListView.delayRemove"
322 value: false
323 }
324 ScriptAction {
325 script: contactListView.listModel.removeContact(contact.contactId)
326 }
327 }
328
329 onClicked: {
330 if (contactListView.isInSelectionMode) {
331 if (!contactListView.selectItem(contactDelegate)) {
332 contactListView.deselectItem(contactDelegate)
333 }
334 } else {
335 contactListView.currentIndex = index
336 contactListView._fetchContact(index, contact)
337 }
338 }
339
340 onPressAndHold: {
341 if (contactListView.multiSelectionEnabled) {
342 contactListView.currentIndex = -1
343 contactListView.startSelection()
344 contactListView.selectItem(contactDelegate)
345 }
346 }
347
348 onSwippingChanged: contactListView._updateSwipeState(contactDelegate)
349 onSwipeStateChanged: contactListView._updateSwipeState(contactDelegate)
350 }
351
352 ContactFetch {
353 id: contactFetch
354
355 model: root.listModel
356 onContactFetched: contactListView.contactClicked(contact)
357 }
358 // This is a workaround to make sure the spinner will disappear if the model is empty
359 // FIXME: implement a model property to say if the model still busy or not
360 Item {
361 id: busyIndicator
362
363 property bool busy: timer.running || priv.currentOperation !== -1
364
365 function ping()
366 {
367 timer.restart()
368 }
369
370 visible: busy
371 anchors.fill: parent
372
373 Timer {
374 id: timer
375
376 interval: 3000
377 running: true
378 repeat: false
379 }
380 }
381
382 Timer {
383 id: dirtyModel
384
385 interval: 1000
386 running: false
387 repeat: false
388 onTriggered: Sections.initSectionData(contactListView)
389 }
390
391 QtObject {
392 id: priv
393
394 property int currentOperation: -1
395 property int pendingTargetIndex: 0
396 property variant pendingTargetMode: null
397 }
398 }