Debian Codegraph @

src/imports/Lomiri/Contacts/ContactSimpleListView.qml

raw ◀ ▶
Source reconstructed at patch state 0 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 "galera"
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 /*!
231 private
232 Fetch contact and emit contact clicked signal
233 */
234 function _fetchContact(index, contact)
235 {
236 if (contact) {
237 contactFetch.fetchContact(contact.contactId)
238 }
239 }
240
241 function _updateSwipeState(item)
242 {
243 if (item.swipping) {
244 return
245 }
246
247 if (item.swipeState !== "Normal") {
248 if (contactListView._currentSwipedItem !== item) {
249 if (contactListView._currentSwipedItem) {
250 contactListView._currentSwipedItem.resetSwipe()
251 }
252 contactListView._currentSwipedItem = item
253 }
254 } else if (item.swipeState !== "Normal" && contactListView._currentSwipedItem === item) {
255 contactListView._currentSwipedItem = null
256 }
257 }
258
259 highlightFollowsCurrentItem: true
260 section {
261 property: showSections ? "contact.tag.tag" : ""
262 criteria: ViewSection.FirstCharacter
263 labelPositioning: ViewSection.InlineLabels
264 delegate: SectionDelegate {
265 anchors {
266 left: parent.left
267 right: parent.right
268 margins: units.gu(2)
269 }
270 text: section != "" ? section : "#"
271 }
272 }
273
274 onCountChanged: {
275 busyIndicator.ping()
276 dirtyModel.restart()
277 }
278
279 listDelegate: ContactDelegate {
280 id: contactDelegate
281
282 property var removalAnimation
283
284 function remove()
285 {
286 removalAnimation.start()
287 }
288
289 flicking: contactListView.flicking
290 width: parent.width
291 selected: (contactListView.multiSelectionEnabled && contactListView.isSelected(contactDelegate))
292 || (!contactListView.isInSelectionMode && contactListView.highlightSelected && (contactListView.currentIndex == index))
293 selectionMode: contactListView.isInSelectionMode
294 defaultAvatarUrl: contactListView.defaultAvatarImageUrl
295 isCurrentItem: ListView.isCurrentItem
296
297 // actions
298 leftSideAction: contactListView.leftSideAction
299 rightSideActions: contactListView.rightSideActions
300
301 // used by swipe to delete
302 removalAnimation: SequentialAnimation {
303 alwaysRunToEnd: true
304
305 PropertyAction {
306 target: contactDelegate
307 property: "ListView.delayRemove"
308 value: true
309 }
310 LomiriNumberAnimation {
311 target: contactDelegate
312 property: "height"
313 to: 1
314 }
315 PropertyAction {
316 target: contactDelegate
317 property: "ListView.delayRemove"
318 value: false
319 }
320 ScriptAction {
321 script: contactListView.listModel.removeContact(contact.contactId)
322 }
323 }
324
325 onClicked: {
326 if (contactListView.isInSelectionMode) {
327 if (!contactListView.selectItem(contactDelegate)) {
328 contactListView.deselectItem(contactDelegate)
329 }
330 } else {
331 contactListView.currentIndex = index
332 contactListView._fetchContact(index, contact)
333 }
334 }
335
336 onPressAndHold: {
337 if (contactListView.multiSelectionEnabled) {
338 contactListView.currentIndex = -1
339 contactListView.startSelection()
340 contactListView.selectItem(contactDelegate)
341 }
342 }
343
344 onSwippingChanged: contactListView._updateSwipeState(contactDelegate)
345 onSwipeStateChanged: contactListView._updateSwipeState(contactDelegate)
346 }
347
348 ContactFetch {
349 id: contactFetch
350
351 model: root.listModel
352 onContactFetched: contactListView.contactClicked(contact)
353 }
354 // This is a workaround to make sure the spinner will disappear if the model is empty
355 // FIXME: implement a model property to say if the model still busy or not
356 Item {
357 id: busyIndicator
358
359 property bool busy: timer.running || priv.currentOperation !== -1
360
361 function ping()
362 {
363 timer.restart()
364 }
365
366 visible: busy
367 anchors.fill: parent
368
369 Timer {
370 id: timer
371
372 interval: 3000
373 running: true
374 repeat: false
375 }
376 }
377
378 Timer {
379 id: dirtyModel
380
381 interval: 1000
382 running: false
383 repeat: false
384 onTriggered: Sections.initSectionData(contactListView)
385 }
386
387 QtObject {
388 id: priv
389
390 property int currentOperation: -1
391 property int pendingTargetIndex: 0
392 property variant pendingTargetMode: null
393 }
394 }