Debian Codegraph @

src/imports/Lomiri/Contacts/ContactListView.qml

raw ◀ ▶
Source reconstructed at patch state 1 of 4 — not the installed (fully patched) source.
4 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 QtQml 2.15
19 import QtContacts 5.0
20
21 import Lomiri.Components 1.3
22 import Lomiri.Components.ListItems 1.3 as ListItem
23 import Lomiri.Components.Popups 1.3
24 import Lomiri.Contacts 0.1 as Contacts
25 import Buteo 0.1
26
27 /*!
28 \qmltype ContactListView
29 \inqmlmodule Lomiri.Contacts 0.1
30 \ingroup ubuntu
31 \brief The ContactListView provides the contact list view integrated with the Favorite List View
32
33 The ContactListView is based on ContactSimpleListView and provide a easy way to show the contact
34 list view with all default visuals defined by Ubuntu system.
35
36 Example:
37 \qml
38 import Lomiri.Contacts 0.1
39
40 ContactListView {
41 anchors.fill: parent
42 onContactClicked: console.debug("Contact ID:" + contact.contactId)
43 }
44 \endqml
45 */
46 FocusScope {
47 id: root
48
49 readonly property alias view: view
50 readonly property alias count: view.count
51
52 property var header: []
53
54 /*!
55 \qmlproperty string contactStringFilter
56
57 This property holds a string that will be used to filter contacts on the list
58 By default this is set to empty
59 */
60 property alias filterTerm: contactsModel.filterTerm
61 /*!
62 \qmlproperty Filter filter
63
64 This property holds the filter instance used by the contact model.
65
66 \sa Filter
67 */
68 property alias filter: contactsModel.externalFilter
69 /*!
70 \qmlproperty bool showAvatar
71
72 This property holds if the contact avatar will appear on the list or not.
73 By default this is set to true.
74 */
75 property alias showAvatar: view.showAvatar
76 /*!
77 \qmlproperty list<SortOrder> sortOrders
78
79 This property holds a list of sort orders used by the contacts model.
80 \sa SortOrder
81 */
82 property alias sortOrders: view.sortOrders
83 /*!
84 \qmlproperty FetchHint fetchHint
85
86 This property holds the fetch hint instance used by the contact model.
87
88 \sa FetchHint
89 */
90 property alias fetchHint: view.fetchHint
91 /*!
92 \qmlproperty bool multiSelectionEnabled
93
94 This property holds if the multi selection mode is enabled or not
95 By default this is set to false
96 */
97 property alias multiSelectionEnabled: view.multiSelectionEnabled
98 /*!
99 \qmlproperty string defaultAvatarImage
100
101 This property holds the default image url to be used when the current contact does
102 not contains a photo
103 */
104 property alias defaultAvatarImageUrl: view.defaultAvatarImageUrl
105 /*!
106 \qmlproperty bool loading
107
108 This property holds when the model still loading new contacts
109 */
110 readonly property alias loading: view.loading
111 /*!
112 \qmlproperty int currentIndex
113
114 This property holds the current active item index
115 */
116 property alias currentIndex: view.currentIndex
117 /*!
118 \qmlproperty bool showSections
119
120 This property holds if the listview will show or not the section headers
121 By default this is set to true
122 */
123 property alias showSections: view.showSections
124
125 /*!
126 \qmlproperty bool contactSwiped
127
128 This property holds if a contact swipe left or right is currently made
129 */
130 property bool contactSwiped: view._currentSwipedItem !== null
131
132 /*!
133 \qmlproperty string manager
134
135 This property holds the manager uri of the contact backend engine.
136 By default this is set to "org.nemomobile.contacts.sqlite"
137 */
138 property alias manager: view.manager
139 /*!
140 \qmlproperty bool fastScrolling
141
142 This property holds if the listview is in fast scroll mode or not
143 */
144 property alias fastScrolling: fastScroll.fastScrolling
145 /*!
146 \qmlproperty Action leftSideAction
147
148 This property holds the available actions when swipe the contact item from left to right
149 */
150 property alias leftSideAction: view.leftSideAction
151 /*!
152 \qmlproperty list<Action> rightSideActions
153
154 This property holds the available actions when swipe the contact item from right to left
155 */
156 property alias rightSideActions: view.rightSideActions
157 /*!
158 \qmlproperty model selectedItems
159
160 This property holds the list of selected items
161 */
162 readonly property alias selectedItems: view.selectedItems
163 /*!
164 \qmlproperty bool multipleSelection
165
166 This property holds if the selection will accept multiple items or single items
167 */
168 property alias multipleSelection: view.multipleSelection
169 /*!
170 \qmlproperty model listModel
171
172 This property holds the model providing data for the list.
173 */
174 property alias listModel: view.listModel
175 /*!
176 \qmlproperty Component listDelegate
177
178 The delegate provides a template defining each item instantiated by the view.
179 */
180 property alias listDelegate: view.listDelegate
181 /*!
182 \qmlproperty bool autoUpdate
183
184 This property indicates whether or not the contact model should be updated automatically, default value is true.
185 */
186 property alias autoUpdate: contactsModel.autoUpdate
187
188 /*!
189 \qmlproperty bool autoHideKeyboard
190
191 This property indicates if the OSK should disapear when the list starts to flick.
192 */
193 property bool autoHideKeyboard: true
194
195 /*!
196 \qmlproperty bool isInSelectionMode
197
198 This property holds a list with the index of selected items
199 */
200 readonly property alias isInSelectionMode: view.isInSelectionMode
201 /*!
202 \qmlproperty bool showImportOptions
203
204 This property holds if the import options should be visible on the list
205 */
206 property bool showImportOptions: false
207 /*!
208 \qmlproperty bool showImportFromAccountOption
209
210 This property holds if the import from account option should be visible on the list
211 */
212 property bool showImportFromAccountOption: buteoSync.serviceAvailable
213
214 property bool runningOnTestMode: false
215
216 /*!
217 \qmlproperty bool showAddNewButton
218
219 This property holds if the add new button should be visible or not
220 */
221 property bool showAddNewButton: false
222 /*!
223 \qmlproperty bool showNewContact
224
225 This property holds if a draft new contact should be visible or not
226 */
227 property bool showNewContact: false
228 /*!
229 \qmlproperty bool syncing
230
231 This property holds if the list is running a sync with online accounts or not
232 */
233 readonly property bool syncing: buteoSync.syncing || Contacts.Contacts.updateIsRunning
234 /*!
235 \qmlproperty bool syncEnabled
236
237 This property holds if there is online account to sync or not
238 */
239 // we are using 'buteoSync.visibleSyncProfiles because' it is a property
240 // and will re-check if the property changes.
241 // Using only '(buteoSync.syncProfilesByCategory("contacts").length > 0)'
242 // the value will be checked only on app startup
243 readonly property bool syncEnabled: (buteoSync.profilesCount > 0) &&
244 (buteoSync.syncProfilesByCategory("contacts").length > 0)
245
246 property alias headerItem: view.headerItem
247 /*!
248 \qmlproperty bool busy
249
250 This property holds if the list is busy or not
251 */
252 property alias busy: indicator.isBusy
253 /*!
254 \qmlproperty bool showBusyIndicator
255
256 This property holds if the busy indicator should became visible
257 */
258 property bool showBusyIndicator: true
259 /*!
260 \qmlproperty real verticalVelocity
261
262 This property holds the vertical velocity of the list
263 */
264 readonly property real verticalVelocity: view.verticalVelocity
265
266 property alias highlightSelected: view.highlightSelected
267
268 property var _busyDialog: null
269
270 //WORKAROUND: SDK does not allow us to disable focus for items due bug: #1514822
271 //because of that we need this
272 property bool _allowFocus: true
273
274
275 /*!
276 This property holds the application id used to request access to online accounts
277 */
278 property string onlineAccountApplicationId: "lomiri-addressbook-app"
279
280 /*!
281 This handler is called when the selection mode is finished without be canceled
282 */
283 signal selectionDone(var items)
284 /*!
285 This handler is called when the selection mode is canceled
286 */
287 signal selectionCanceled()
288 /*!
289 This handler is called when any error occurs in the contact model
290 */
291 signal error(string message)
292 /*!
293 This handler is called when a unknown contact is clicked, the label contains the phone number
294 */
295 signal addContactClicked(string label)
296 /*!
297 This handler is called when the contact delegate disapear (height === 0) caused by the function call makeDisappear
298 */
299 signal contactDisappeared(QtObject contact)
300 /*!
301 This handler is called when the button add new contact is clicked
302 */
303 signal addNewContactClicked()
304 /*!
305 This handler is called when any contact in the list receives a click
306 */
307 signal contactClicked(QtObject contact)
308 /*!
309 This handler is called when online account operation finish
310 */
311 signal onlineAccountFinished()
312
313 function startSelection()
314 {
315 view.startSelection()
316 }
317 function isSelected(item)
318 {
319 return view.isSelected(item)
320 }
321 function selectItem(item)
322 {
323 return view.selectItem(item)
324 }
325 function deselectItem(item)
326 {
327 return view.deselectItem(item)
328 }
329 function endSelection()
330 {
331 view.endSelection()
332 }
333 function cancelSelection()
334 {
335 view.cancelSelection()
336 }
337 function clearSelection()
338 {
339 view.clearSelection()
340 }
341 function selectAll()
342 {
343 view.selectAll()
344 }
345 function returnToBounds()
346 {
347 view.returnToBounds()
348 }
349 function positionViewAtContact(contact)
350 {
351 view.positionViewAtContact(contact)
352 }
353 function positionViewAtContactId(contactId)
354 {
355 view.positionViewAtContactId(contactId)
356 }
357 function positionViewAtBeginning()
358 {
359 moveToBegining.restart()
360 }
361 function changeFilter(newFilter)
362 {
363 contactsModel.changeFilter(newFilter)
364 }
365 function reset()
366 {
367 if (view.favouritesIsSelected) {
368 showAllContacts()
369 } else {
370 positionViewAtBeginning()
371 }
372 }
373 function showFavoritesContacts()
374 {
375 //WORKAROUND: clear the model before start populate it with the new contacts
376 //otherwise the model will wait for all contacts before show any new contact
377 if (!view.favouritesIsSelected) {
378 root.changeFilter(root.filter)
379 view.favouritesIsSelected = true
380 }
381 }
382 function showAllContacts()
383 {
384 if (view.favouritesIsSelected) {
385 root.changeFilter(root.filter)
386 view.favouritesIsSelected = false
387 }
388 }
389
390 /*!
391 Causes the list to update
392 \l autoUpdate
393 */
394 function update()
395 {
396 contactsModel.update()
397 }
398
399 /*!
400 Start an online account sync opration
401 */
402 function sync()
403 {
404 buteoSync.startSyncByCategory("contacts")
405 }
406
407 /*!
408 start an online account creation
409 */
410 function createOnlineAccount(provider)
411 {
412 onlineAccountHelper.start(provider)
413 }
414
415 focus: true
416
417 ContactSimpleListView {
418 id: view
419
420 property bool showFavourites: true
421 property alias favouritesIsSelected: contactsModel.onlyFavorites
422 property bool contactsLoaded: false
423
424 // if the favorite header became invisible we should move back to all contacts.
425 onShowFavouritesChanged: {
426 if (!showFavourites && view.favouritesIsSelected) {
427 root.changeFilter(root.filter)
428 view.favouritesIsSelected = false
429 }
430 }
431
432 onFlickStarted: {
433 if (autoHideKeyboard)
434 forceActiveFocus()
435 }
436 anchors.fill: parent
437
438
439 // WORKAROUND: The SDK header causes the contactY to move to a wrong postion
440 // calling the positionViewAtBeginning after the list created fix that
441 Timer {
442 id: moveToBegining
443
444 interval: 100
445 running: false
446 repeat: false
447 onTriggered: view.positionViewAtBeginning()
448 }
449
450 header: Column {
451 anchors {
452 left: parent.left
453 right: parent.right
454 }
455
456 // top margin
457 Item {
458 anchors {
459 left: parent.left
460 right: parent.right
461 }
462 height: units.gu(0.5)
463 }
464
465 Binding {
466 target: view
467 property: 'currentIndex'
468 value: -1
469 when: root.showNewContact
470 }
471
472 // AddNewButton
473 ContactListButtonDelegate {
474 anchors {
475 left: parent.left
476 right: parent.right
477 margins: units.gu(1)
478 }
479 objectName: "addNewButton"
480
481 iconSource: "image://theme/add"
482 // TRANSLATORS: this refers to a new contact
483 labelText: i18n.dtr("lomiri-addressbook-app", "+ Create New")
484 onClicked: root.addNewContactClicked()
485 visible: root.showAddNewButton
486 }
487
488 ContactDelegate {
489 property var contact: Contact {
490 Name {
491 firstName: i18n.tr("New contact")
492 }
493 Avatar {
494 imageUrl: "image://theme/contact"
495 }
496 }
497 selected: pageStack.hasOwnProperty("hasKeyboard") && pageStack.hasKeyboard
498 visible: root.showNewContact
499 height: root.showNewContact ? defaultHeight : 0
500 onHeightChanged: {
501 if (visible)
502 view.positionViewAtBeginning()
503 }
504 Behavior on height {LomiriNumberAnimation {}}
505 }
506
507 Column {
508 id: importFromButtons
509 objectName: "importFromButtons"
510
511 readonly property bool isSearching: (root.filterTerm && root.filterTerm !== "")
512
513 anchors {
514 left: parent.left
515 right: parent.right
516 margins: units.gu(1)
517 }
518 height: visible ? childrenRect.height : 0
519
520 visible: root.showImportOptions &&
521 !indicator.visible &&
522 (root.count === 0) &&
523 !view.favouritesIsSelected &&
524 !isSearching
525
526 // avoid show the button while the list still loading contacts
527 Behavior on visible {
528 SequentialAnimation {
529 PauseAnimation {
530 duration: !importFromButtons.visible ? 500 : 0
531 }
532 PropertyAction {
533 target: importFromButtons
534 property: "visible"
535 }
536 }
537 }
538
539 Repeater {
540 model: onlineAccountHelper.status === Loader.Ready ? onlineAccountHelper.item.providerModel : []
541 ContactListButtonDelegate {
542 objectName: "%1.importFromOnlineAccountButton.%2".arg(root.objectName).arg(model.providerId)
543
544 expandIcon: true
545 iconSource: model.iconName.indexOf("/") === 0 ?
546 model.iconName : "image://theme/" + model.iconName
547 labelText: i18n.dtr("lomiri-addressbook-app", "Import contacts from %1").arg(model.displayName)
548 onClicked: root.createOnlineAccount(model.providerId)
549 }
550 }
551
552 // Import from sim card
553 ContactListButtonDelegate {
554 id: importFromSimCard
555 objectName: "%1.importFromSimCardButton".arg(root.objectName)
556
557 expandIcon: true
558 iconSource: "image://theme/import"
559 labelText: i18n.dtr("lomiri-addressbook-app", "Import contacts from SIM card")
560 // Does not show the button if the list is not in a pageStack
561 visible: (typeof(pageStack) !== "undefined") &&
562 ((simList.sims.length > 0) && (simList.present.length > 0))
563 onClicked: {
564 if (pageStack.addPageToNextColumn)
565 pageStack.addPageToNextColumn(pageStack.primaryPage, Qt.resolvedUrl("SIMCardImportPage.qml"),
566 {"objectName": "simCardImportPage",
567 "targetModel": view.listModel,
568 "sims": simList.sims})
569 else
570 pageStack.push(Qt.resolvedUrl("SIMCardImportPage.qml"),
571 {"objectName": "simCardImportPage",
572 "targetModel": view.listModel,
573 "sims": simList.sims})
574 }
575 }
576
577 // Import from vcf file
578 ContactListButtonDelegate {
579 id: importFromVCF
580 objectName: "%1.importFromVcfCardButton".arg(root.objectName)
581
582 expandIcon: true
583 iconSource: "image://theme/import"
584 labelText: i18n.dtr("lomiri-addressbook-app", "Import contacts from vcard file")
585 // Does not show the button if the list is not in a pageStack
586 visible: typeof(pageStack) !== "undefined"
587 onClicked: {
588 if (pageStack.addPageToNextColumn)
589 pageStack.addPageToNextColumn(pageStack.primaryPage, Qt.resolvedUrl("ContactImportPage.qml"),
590 {"objectName": "contactImportPage" })
591 else
592 pageStack.push(Qt.resolvedUrl("ContactImportPage.qml"),
593 {"objectName": "contactImportPage" })
594 }
595 }
596 }
597 }
598 onError: root.error(message)
599 onContactClicked: root.contactClicked(contact)
600 onSelectionDone: root.selectionDone(items)
601 onSelectionCanceled: root.selectionCanceled()
602 onContactDisappeared: root.contactDisappeared(contact)
603 onCountChanged: {
604 if (view.count > 0) {
605 view.contactsLoaded = true
606 }
607 }
608
609 clip: true
610 listModel: ContactListModel {
611 id: contactsModel
612
613 manager: root.manager
614 sortOrders: root.sortOrders
615 fetchHint: root.fetchHint
616 }
617 }
618
619 Column {
620 id: indicator
621
622 readonly property bool isBusy: ((view.loading && !view.contactsLoaded) ||
623 (root.syncing && (view.count === 0)) ||
624 ((onlineAccountHelper.status == Loader.Ready) &&
625 (onlineAccountHelper.item.running)))
626
627 anchors.centerIn: view
628 spacing: units.gu(2)
629 visible: root.showBusyIndicator && isBusy
630
631 ActivityIndicator {
632 id: activity
633
634 anchors.horizontalCenter: parent.horizontalCenter
635 running: indicator.visible
636 }
637 Label {
638 anchors.horizontalCenter: activity.horizontalCenter
639 text: root.syncing ? i18n.dtr("lomiri-addressbook-app", "Synchronizing...") : i18n.dtr("lomiri-addressbook-app", "Loading...")
640 }
641 }
642
643 FastScroll {
644 id: fastScroll
645
646 listView: view
647 // only enable FastScroll if the we have more than 2 pages of content and sections is enabled
648 enabled: !contactSwiped && showSections &&
649 (view.contentHeight > (view.height * 2)) &&
650 (view.height >= minimumHeight) &&
651 (((view.contentY - view.originY) - view.headerItem.height) >= 0)// hearder already invisble
652
653 anchors {
654 right: parent.right
655 verticalCenter: parent.verticalCenter
656 }
657 }
658
659 ButeoSync {
660 id: buteoSync
661 }
662
663 SIMList {
664 id: simList
665 }
666
667 Loader {
668 id: onlineAccountHelper
669 objectName: "onlineAccountHelper"
670
671 property string createAccount: ""
672 property bool exitOnFinish: false
673 readonly property bool isSearching: (root.filterTerm && root.filterTerm !== "")
674 // if running on test mode does not load online account modules
675 readonly property string sourceFile: root.runningOnTestMode ?
676 Qt.resolvedUrl("OnlineAccountsDummy.qml") :
677 Qt.resolvedUrl("OnlineAccountsHelper.qml")
678
679 function start(provider)
680 {
681 if (root.onlineAccountApplicationId !== "lomiri-addressbook-app") {
682 // invoke address book app
683 Qt.openUrlExternally("addressbook:///createAccount?providerId=%1&callback=%2.desktop".arg(provider).arg(root.onlineAccountApplicationId))
684 } else {
685 if (item)
686 item.setupExec(provider)
687 else
688 createAccount = provider
689 }
690 }
691
692 anchors.fill: parent
693 asynchronous: true
694 source: root.showImportOptions &&
695 root.showImportFromAccountOption &&
696 (root.count === 0) &&
697 !view.favouritesIsSelected &&
698 !isSearching ? sourceFile : ""
699 onStatusChanged: {
700 if (createAccount && (status === Loader.Ready)) {
701 item.setupExec(createAccount)
702 createAccount = ""
703 }
704 }
705
706 Connections {
707 target: onlineAccountHelper.item ? onlineAccountHelper.item : null
708 function onFinished() { root.onlineAccountFinished() }
709 }
710
711 Binding {
712 target: onlineAccountHelper.item
713 property: "applicationId"
714 value: root.onlineAccountApplicationId
715 when: onlineAccountHelper.status == Loader.Ready
716 restoreMode: Binding.RestoreBinding
717 }
718 }
719
720 Keys.onUpPressed: {
721 //WORKAROUND: SDK does not allow us to disable focus for items due bug: #1514822
722 //because of that we need this
723 if (view.currentIndex == 0) {
724 pageStack._nextItemInFocusChain(view, false)
725 } else {
726 view.currentIndex -= 1
727 }
728 }
729 Keys.onDownPressed: {
730 //WORKAROUND: SDK does not allow us to disable focus for items due bug: #1514822
731 //because of that we need this
732 if (view.currentIndex == (view.count - 1)) {
733 //DO nothing
734 } else {
735 view.currentIndex += 1
736 }
737 }
738 }