Debian Codegraph @

src/imports/Lomiri/Contacts/ContactListView.qml

raw ◀ ▶
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.Profiles 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: syncManager.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: syncManager.synchronizing
234 /*!
235 \qmlproperty bool syncEnabled
236
237 This property holds if there is online account to sync or not
238 */
•239 readonly property bool syncEnabled: syncManager.profiles.length > 0
240
241 property alias headerItem: view.headerItem
242 /*!
243 \qmlproperty bool busy
244
245 This property holds if the list is busy or not
246 */
247 property alias busy: indicator.isBusy
248 /*!
249 \qmlproperty bool showBusyIndicator
250
251 This property holds if the busy indicator should became visible
252 */
253 property bool showBusyIndicator: true
254 /*!
255 \qmlproperty real verticalVelocity
256
257 This property holds the vertical velocity of the list
258 */
259 readonly property real verticalVelocity: view.verticalVelocity
260
261 property alias highlightSelected: view.highlightSelected
262
263 property var _busyDialog: null
264
265 //WORKAROUND: SDK does not allow us to disable focus for items due bug: #1514822
266 //because of that we need this
267 property bool _allowFocus: true
268
269
270 /*!
271 This property holds the application id used to request access to online accounts
272 */
273 property string onlineAccountApplicationId: "lomiri-addressbook-app"
274
275 /*!
276 This handler is called when the selection mode is finished without be canceled
277 */
278 signal selectionDone(var items)
279 /*!
280 This handler is called when the selection mode is canceled
281 */
282 signal selectionCanceled()
283 /*!
284 This handler is called when any error occurs in the contact model
285 */
286 signal error(string message)
287 /*!
288 This handler is called when a unknown contact is clicked, the label contains the phone number
289 */
290 signal addContactClicked(string label)
291 /*!
292 This handler is called when the contact delegate disapear (height === 0) caused by the function call makeDisappear
293 */
294 signal contactDisappeared(QtObject contact)
295 /*!
296 This handler is called when the button add new contact is clicked
297 */
298 signal addNewContactClicked()
299 /*!
300 This handler is called when any contact in the list receives a click
301 */
302 signal contactClicked(QtObject contact)
303 /*!
304 This handler is called when online account operation finish
305 */
306 signal onlineAccountFinished()
307
308 function startSelection()
309 {
310 view.startSelection()
311 }
312 function isSelected(item)
313 {
314 return view.isSelected(item)
315 }
316 function selectItem(item)
317 {
318 return view.selectItem(item)
319 }
320 function deselectItem(item)
321 {
322 return view.deselectItem(item)
323 }
324 function endSelection()
325 {
326 view.endSelection()
327 }
328 function cancelSelection()
329 {
330 view.cancelSelection()
331 }
332 function clearSelection()
333 {
334 view.clearSelection()
335 }
336 function selectAll()
337 {
338 view.selectAll()
339 }
340 function returnToBounds()
341 {
342 view.returnToBounds()
343 }
344 function positionViewAtContact(contact)
345 {
346 view.positionViewAtContact(contact)
347 }
348 function positionViewAtContactId(contactId)
349 {
350 view.positionViewAtContactId(contactId)
351 }
352 function positionViewAtBeginning()
353 {
354 moveToBegining.restart()
355 }
356 function changeFilter(newFilter)
357 {
358 contactsModel.changeFilter(newFilter)
359 }
360 function reset()
361 {
362 if (view.favouritesIsSelected) {
363 showAllContacts()
364 } else {
365 positionViewAtBeginning()
366 }
367 }
368 function showFavoritesContacts()
369 {
370 //WORKAROUND: clear the model before start populate it with the new contacts
371 //otherwise the model will wait for all contacts before show any new contact
372 if (!view.favouritesIsSelected) {
373 root.changeFilter(root.filter)
374 view.favouritesIsSelected = true
375 }
376 }
377 function showAllContacts()
378 {
379 if (view.favouritesIsSelected) {
380 root.changeFilter(root.filter)
381 view.favouritesIsSelected = false
382 }
383 }
384
385 /*!
386 Causes the list to update
387 \l autoUpdate
388 */
389 function update()
390 {
391 contactsModel.update()
392 }
393
394 /*!
395 Start an online account sync opration
396 */
397 function sync()
398 {
•399 syncManager.synchronize()
400 }
401
402 /*!
403 start an online account creation
404 */
405 function createOnlineAccount(provider)
406 {
407 onlineAccountHelper.start(provider)
408 }
409
410 focus: true
411
412 ContactSimpleListView {
413 id: view
414
415 property bool showFavourites: true
416 property alias favouritesIsSelected: contactsModel.onlyFavorites
417 property bool contactsLoaded: false
418
419 // if the favorite header became invisible we should move back to all contacts.
420 onShowFavouritesChanged: {
421 if (!showFavourites && view.favouritesIsSelected) {
422 root.changeFilter(root.filter)
423 view.favouritesIsSelected = false
424 }
425 }
426
427 onFlickStarted: {
428 if (autoHideKeyboard)
429 forceActiveFocus()
430 }
431 anchors.fill: parent
432
433
434 // WORKAROUND: The SDK header causes the contactY to move to a wrong postion
435 // calling the positionViewAtBeginning after the list created fix that
436 Timer {
437 id: moveToBegining
438
439 interval: 100
440 running: false
441 repeat: false
442 onTriggered: view.positionViewAtBeginning()
443 }
444
445 header: Column {
446 anchors {
447 left: parent.left
448 right: parent.right
449 }
450
451 // top margin
452 Item {
453 anchors {
454 left: parent.left
455 right: parent.right
456 }
457 height: units.gu(0.5)
458 }
459
460 Binding {
461 target: view
462 property: 'currentIndex'
463 value: -1
464 when: root.showNewContact
465 }
466
467 // AddNewButton
468 ContactListButtonDelegate {
469 anchors {
470 left: parent.left
471 right: parent.right
472 margins: units.gu(1)
473 }
474 objectName: "addNewButton"
475
476 iconSource: "image://theme/add"
477 // TRANSLATORS: this refers to a new contact
478 labelText: i18n.dtr("lomiri-addressbook-app", "+ Create New")
479 onClicked: root.addNewContactClicked()
480 visible: root.showAddNewButton
481 }
482
483 ContactDelegate {
484 property var contact: Contact {
485 Name {
486 firstName: i18n.tr("New contact")
487 }
488 Avatar {
489 imageUrl: "image://theme/contact"
490 }
491 }
492 selected: pageStack.hasOwnProperty("hasKeyboard") && pageStack.hasKeyboard
493 visible: root.showNewContact
494 height: root.showNewContact ? defaultHeight : 0
495 onHeightChanged: {
496 if (visible)
497 view.positionViewAtBeginning()
498 }
499 Behavior on height {LomiriNumberAnimation {}}
500 }
501
502 Column {
503 id: importFromButtons
504 objectName: "importFromButtons"
505
506 readonly property bool isSearching: (root.filterTerm && root.filterTerm !== "")
507
508 anchors {
509 left: parent.left
510 right: parent.right
511 margins: units.gu(1)
512 }
513 height: visible ? childrenRect.height : 0
514
515 visible: root.showImportOptions &&
516 !indicator.visible &&
517 (root.count === 0) &&
518 !view.favouritesIsSelected &&
519 !isSearching
520
521 // avoid show the button while the list still loading contacts
522 Behavior on visible {
523 SequentialAnimation {
524 PauseAnimation {
525 duration: !importFromButtons.visible ? 500 : 0
526 }
527 PropertyAction {
528 target: importFromButtons
529 property: "visible"
530 }
531 }
532 }
533
534 Repeater {
535 model: onlineAccountHelper.status === Loader.Ready ? onlineAccountHelper.item.providerModel : []
536 ContactListButtonDelegate {
537 objectName: "%1.importFromOnlineAccountButton.%2".arg(root.objectName).arg(model.providerId)
538
539 expandIcon: true
540 iconSource: model.iconName.indexOf("/") === 0 ?
541 model.iconName : "image://theme/" + model.iconName
542 labelText: i18n.dtr("lomiri-addressbook-app", "Import contacts from %1").arg(model.displayName)
543 onClicked: root.createOnlineAccount(model.providerId)
544 }
545 }
546
547 // Import from sim card
548 ContactListButtonDelegate {
549 id: importFromSimCard
550 objectName: "%1.importFromSimCardButton".arg(root.objectName)
551
552 expandIcon: true
553 iconSource: "image://theme/import"
554 labelText: i18n.dtr("lomiri-addressbook-app", "Import contacts from SIM card")
555 // Does not show the button if the list is not in a pageStack
556 visible: (typeof(pageStack) !== "undefined") &&
557 ((simList.sims.length > 0) && (simList.present.length > 0))
558 onClicked: {
559 if (pageStack.addPageToNextColumn)
560 pageStack.addPageToNextColumn(pageStack.primaryPage, Qt.resolvedUrl("SIMCardImportPage.qml"),
561 {"objectName": "simCardImportPage",
562 "targetModel": view.listModel,
563 "sims": simList.sims})
564 else
565 pageStack.push(Qt.resolvedUrl("SIMCardImportPage.qml"),
566 {"objectName": "simCardImportPage",
567 "targetModel": view.listModel,
568 "sims": simList.sims})
569 }
570 }
571
572 // Import from vcf file
573 ContactListButtonDelegate {
574 id: importFromVCF
575 objectName: "%1.importFromVcfCardButton".arg(root.objectName)
576
577 expandIcon: true
578 iconSource: "image://theme/import"
579 labelText: i18n.dtr("lomiri-addressbook-app", "Import contacts from vcard file")
580 // Does not show the button if the list is not in a pageStack
581 visible: typeof(pageStack) !== "undefined"
582 onClicked: {
583 if (pageStack.addPageToNextColumn)
584 pageStack.addPageToNextColumn(pageStack.primaryPage, Qt.resolvedUrl("ContactImportPage.qml"),
585 {"objectName": "contactImportPage" })
586 else
587 pageStack.push(Qt.resolvedUrl("ContactImportPage.qml"),
588 {"objectName": "contactImportPage" })
589 }
590 }
•591
592 Rectangle {
593 height: units.gu(12)
594 width: parent.width
595 color: theme.palette.normal.background
596 visible: !root.syncing && application.isOnline && root.syncEnabled
597
598 Button {
599 id: closeButton
600 anchors.centerIn: parent
601 text: i18n.dtr("lomiri-addressbook-app", "Start sync")
602
603 onClicked: {
604 root.sync()
605 }
606 }
607 }
608 }
609 }
610 onError: root.error(message)
611 onContactClicked: root.contactClicked(contact)
612 onSelectionDone: root.selectionDone(items)
613 onSelectionCanceled: root.selectionCanceled()
614 onContactDisappeared: root.contactDisappeared(contact)
615 onCountChanged: {
616 if (view.count > 0) {
617 view.contactsLoaded = true
618 }
619 }
620
621 clip: true
622 listModel: ContactListModel {
623 id: contactsModel
624
625 manager: root.manager
626 sortOrders: root.sortOrders
627 fetchHint: root.fetchHint
628 }
629 }
630
631 Column {
632 id: indicator
633
634 readonly property bool isBusy: ((view.loading && !view.contactsLoaded) ||
635 (root.syncing && (view.count === 0)) ||
636 ((onlineAccountHelper.status == Loader.Ready) &&
637 (onlineAccountHelper.item.running)))
638
639 anchors.centerIn: view
640 spacing: units.gu(2)
641 visible: root.showBusyIndicator && isBusy
642
643 ActivityIndicator {
644 id: activity
645
646 anchors.horizontalCenter: parent.horizontalCenter
647 running: indicator.visible
648 }
649 Label {
650 anchors.horizontalCenter: activity.horizontalCenter
651 text: root.syncing ? i18n.dtr("lomiri-addressbook-app", "Synchronizing...") : i18n.dtr("lomiri-addressbook-app", "Loading...")
652 }
653 }
654
655 FastScroll {
656 id: fastScroll
657
658 listView: view
659 // only enable FastScroll if the we have more than 2 pages of content and sections is enabled
660 enabled: !contactSwiped && showSections &&
661 (view.contentHeight > (view.height * 2)) &&
662 (view.height >= minimumHeight) &&
663 (((view.contentY - view.originY) - view.headerItem.height) >= 0)// hearder already invisble
664
665 anchors {
666 right: parent.right
667 verticalCenter: parent.verticalCenter
668 }
669 }
670
•671 SyncManager {
672 id: syncManager
673 filterHidden: true
674 filterBy.key: "category"
675 filterBy.value: "contacts"
676 }
677
678 SIMList {
679 id: simList
680 }
681
682 Loader {
683 id: onlineAccountHelper
684 objectName: "onlineAccountHelper"
685
686 property string createAccount: ""
687 property bool exitOnFinish: false
688 readonly property bool isSearching: (root.filterTerm && root.filterTerm !== "")
689 // if running on test mode does not load online account modules
690 readonly property string sourceFile: root.runningOnTestMode ?
691 Qt.resolvedUrl("OnlineAccountsDummy.qml") :
692 Qt.resolvedUrl("OnlineAccountsHelper.qml")
693
694 function start(provider)
695 {
696 if (root.onlineAccountApplicationId !== "lomiri-addressbook-app") {
697 // invoke address book app
698 Qt.openUrlExternally("addressbook:///createAccount?providerId=%1&callback=%2.desktop".arg(provider).arg(root.onlineAccountApplicationId))
699 } else {
700 if (item)
701 item.setupExec(provider)
702 else
703 createAccount = provider
704 }
705 }
706
707 anchors.fill: parent
708 asynchronous: true
709 source: root.showImportOptions &&
710 root.showImportFromAccountOption &&
711 (root.count === 0) &&
712 !view.favouritesIsSelected &&
713 !isSearching ? sourceFile : ""
714 onStatusChanged: {
715 if (createAccount && (status === Loader.Ready)) {
716 item.setupExec(createAccount)
717 createAccount = ""
718 }
719 }
720
721 Connections {
722 target: onlineAccountHelper.item ? onlineAccountHelper.item : null
723 function onFinished() { root.onlineAccountFinished() }
724 }
725
726 Binding {
727 target: onlineAccountHelper.item
728 property: "applicationId"
729 value: root.onlineAccountApplicationId
730 when: onlineAccountHelper.status == Loader.Ready
731 restoreMode: Binding.RestoreBinding
732 }
733 }
734
735 Keys.onUpPressed: {
736 //WORKAROUND: SDK does not allow us to disable focus for items due bug: #1514822
737 //because of that we need this
738 if (view.currentIndex == 0) {
739 pageStack._nextItemInFocusChain(view, false)
740 } else {
741 view.currentIndex -= 1
742 }
743 }
744 Keys.onDownPressed: {
745 //WORKAROUND: SDK does not allow us to disable focus for items due bug: #1514822
746 //because of that we need this
747 if (view.currentIndex == (view.count - 1)) {
748 //DO nothing
749 } else {
750 view.currentIndex += 1
751 }
752 }
753 }