~renatofilho/address-book-app/hide-favorite-if-empty

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
/*
 * Copyright (C) 2012-2015 Canonical, Ltd.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 3.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import QtQuick 2.4
import QtContacts 5.0

import Ubuntu.Components 1.3
import Ubuntu.Components.ListItems 1.3 as ListItem
import Ubuntu.Components.Popups 1.3 as Popups
import Ubuntu.Contacts 0.1 as ContactsUI
import Ubuntu.Content 1.1 as ContentHub

import Ubuntu.AddressBook.Base 0.1
import Ubuntu.AddressBook.ContactShare 0.1


Page {
    id: mainPage
    objectName: "contactListPage"

    property var viewPage: null
    property bool pickMode: false
    property alias contentHubTransfer: contactExporter.activeTransfer
    property bool pickMultipleContacts: false
    property QtObject contactIndex: null
    property alias contactManager: contactList.manager

    property var _busyDialog: null
    property bool _importingTestData: false
    property bool _creatingContact: false

    readonly property string currentViewContactId: viewPage && viewPage.contact ? viewPage.contact.contactId : ""
    readonly property bool isEmpty: (contactList.count === 0)
    readonly property bool allowToQuit: (application.callbackApplication.length > 0)
    readonly property var contactModel: contactList.listModel ? contactList.listModel : null
    readonly property bool searching: state === "searching"
    readonly property string headerTitle: pageHeader.title

    // this function is used to reset the contact list page to the default state if it was called
    // from the uri. For example when called to add a new contact
    function returnToNormalState()
    {
        application.callbackApplication = ""
    }

    function createContactWithPhoneNumber(phoneNumber)
    {
        var newContact = ContactsJS.createEmptyContact(phoneNumber, mainPage);
        if (bottomEdgeLoader.status == Loader.Ready) {
            bottomEdgeLoader.editContact(newContact)
        } else {
            contactList.currentIndex = -1
            var incubator = pageStack.addPageToNextColumn(mainPage,
                                                         Qt.resolvedUrl("ABContactEditorPage.qml"),
                                                        { model: mainPage.contactModel,
                                                          contact: newContact,
                                                          backIconName: 'back',
                                                          enabled: false
                                                          })
        }
    }

    function openViewPage(viewPageProperties) {
        var component = Qt.createComponent(Qt.resolvedUrl("ABContactViewPage.qml"))
        var incubator = pageStack.addPageToNextColumn(mainPage, component, viewPageProperties)
        if (incubator && (incubator.status === Component.Loading)) {
            incubator.onStatusChanged = function(status) {
                if (status === Component.Ready)
                    mainPage.viewPage =  incubator.object
            }
        } else if (incubator && incubator.status ===- Component.Ready) {
            mainPage.viewPage =  incubator.object
        } else {
            mainPage.viewPage =  null
        }
    }

    function showContact(contact)
    {
        var currentContact = contactList.listModel.contacts[contactList.currentIndex]
        if (currentContact && (mainPage.currentViewContactId === currentContact.contactId)) {
            // contact view already opened with this contact
            return
        }

        // go back to normal state if not searching
        if ((state !== "searching") &&
            (state !== "vcardImported")) {
            mainPage.state = "default";
        }
        openViewPage({model: contactList.listModel,
                      contact: contact});
    }

    function showContactWithId(contactId)
    {
        openViewPage({model: contactList.listModel,
                      contactId: contactId});
    }

    function importContact(urls)
    {
        mainPage._busyDialog = PopupUtils.open(busyDialogComponent, mainPage)

        var importing = false
        for(var i=0, iMax=urls.length; i < iMax; i++) {
            var url = urls[i]
            if (url && url != "") {
                importing = true
                contactList.listModel.importContacts(url)
            }
        }

        if (!importing) {
            PopupUtils.close(mainPage._busyDialog)
            mainPage._busyDialog = null
        }
    }

    function startPickMode(isSingleSelection, activeTransfer)
    {
        contentHubTransfer = activeTransfer
        pickMode = true
        pickMultipleContacts = !isSingleSelection
        contactList.startSelection()
    }

    function moveListToContact(contact)
    {
        if ((state !== "searching") &&
            (state !== "vcardImported")) {
            mainPage.state = "default"
        }

        contactIndex = contact
        // this means a new contact was created
        if (mainPage.allowToQuit) {
            application.goBackToSourceApp()
        }
    }


    function onNewContactSaved(contact) {
        _creatingContact = true
        moveListToContact(contact)
        if (pageStack.columns > 1) {
            showContact(contact);
        }
    }

    function filterContactsBySection() {
        switch (mainPage.head.sections.selectedIndex) {
        case 0:
            contactList.showAllContacts()
            break;
        case 1:
            contactList.showFavoritesContacts()
            break;
        default:
            break;
        }
    }

    // Delay contact fetch for some msecs (check 'fetchNewContactTimer')
    function delayFetchContact()
    {
        fetchNewContactTimer.restart()
    }

    function fetchContact()
    {
        if ((contactList.currentIndex >= 0) && (pageStack.columns > 1)) {
            var currentContact = contactList.listModel.contacts[contactList.currentIndex]
            if (!currentContact) {
                var component = Qt.createComponent(Qt.resolvedUrl("ABMultiColumnEmptyState.qml"))
                var searching = contactList.filterTerm !== ""
                pageStack.addPageToNextColumn(mainPage, component,
                                              { headerTitle: searching ? i18n.tr("No contact found") : i18n.tr("No contacts") })
                return
            }
            if (currentContact && (mainPage.currentViewContactId === currentContact.contactId))
                return

            contactList.view._fetchContact(contactList.currentIndex, currentContact)
        }
    }

    header: PageHeader {
        id: pageHeader

        property alias leadingActions: leadingBar.actions
        property alias trailingActions: trailingBar.actions
        property alias sectionsModel: sections.model

        title: i18n.tr("Contacts")
        //flickable: contactList.view
        trailingActionBar {
            id: trailingBar
        }
        leadingActionBar {
            id: leadingBar
        }
        extension: Sections {
            id: sections
            anchors {
                left: parent.left
                leftMargin: units.gu(2)
                bottom: parent.bottom
            }
            onSelectedIndexChanged: {
                switch (selectedIndex) {
                case 0:
                    contactList.showAllContacts()
                    break;
                case 1:
                    contactList.showFavoritesContacts()
                    break;
                default:
                    break;
                }
            }
        }
    }

    // This timer is to avoid fetch unecessary contact if the user select the contacts too fast
    // while navigating on contact list with keyboard
    Timer {
        id: fetchNewContactTimer

        interval: 300
        repeat: false
        onTriggered: mainPage.fetchContact()
    }

    title: i18n.tr("Contacts")
    flickable: null

    ContactsUI.ContactListView {
        id: contactList
        objectName: "contactListView"

        focus: true
        showImportOptions: !mainPage.pickMode &&
                           pageStack.bottomEdge &&
                           (pageStack.bottomEdge.status === BottomEdge.Hidden)
        anchors {
            top: parent.top
            topMargin: pageHeader.height
            left: parent.left
            bottom: keyboard.top
            right: parent.right
        }
        currentIndex: -1
        filterTerm: searchField.text
        multiSelectionEnabled: true
        multipleSelection: (mainPage.pickMode && mainPage.pickMultipleContacts) || !mainPage.pickMode
        showNewContact: (pageStack.columns > 1) && pageStack.bottomEdge && (pageStack.bottomEdge.status === BottomEdge.Committed)
        highlightSelected: pageStack.hasKeyboard && !mainPage._creatingContact
        onAddContactClicked: mainPage.createContactWithPhoneNumber(label)
        onContactClicked: mainPage.showContact(contact)
        onIsInSelectionModeChanged: mainPage.state = isInSelectionMode ? "selection"  : "default"
        onSelectionCanceled: {
            if (pickMode) {
                if (contentHubTransfer) {
                    contentHubTransfer.state = ContentHub.ContentTransfer.Aborted
                }
                pickMode = false
                contentHubTransfer = null
                application.returnVcard("")
            }
            mainPage.state = "default"
        }

        onError: pageStack.contactModelError(error)
        onActiveFocusChanged: {
            if (activeFocus && (contactList.currentIndex === -1)) {
                contactList.currentIndex = 0
            }
        }

        onCountChanged: {
            if (mainPage.active &&
                (pageStack.columns > 1) &&
                (contactList.currentIndex === -1) &&
                (pageStack.bottomEdge.status === BottomEdge.Hidden)) {
                contactList.currentIndex = 0
            }
            mainPage.delayFetchContact()
        }

        onCurrentIndexChanged: {
            if (!mainPage.contactIndex)
                mainPage.delayFetchContact()
        }

        Keys.onReturnPressed: {
            var currentContact = contactList.listModel.contacts[contactList.currentIndex]
            if (mainPage.currentViewContactId === currentContact.contactId)
                return

            contactList.view._fetchContact(contactList.currentIndex, currentContact)
        }

        //WORKAROUND: SDK does not allow us to disable focus for items due bug: #1514822
        //because of that we need this
        Keys.onRightPressed: {
            // only move focus away when in edit mode
            if (pageStack.bottomEdge.status === BottomEdge.Committed) {
                var next = pageStack._nextItemInFocusChain(view, true)
                if (next === searchField) {
                    pageStack._nextItemInFocusChain(next, true)
                }
            }
        }
        Keys.onTabPressed: {
            var next = pageStack._nextItemInFocusChain(view, true)
            if (next === searchField) {
                pageStack._nextItemInFocusChain(next, true)
            }
        }
    }

    TextField {
        id: searchField

        //WORKAROUND: SDK does not allow us to disable focus for items due bug: #1514822
        //because of that we need this
        readonly property bool _allowFocus: true

        anchors {
            top: parent ? parent.top : undefined
            left: parent ? parent.left : undefined
            right: parent ? parent.right : undefined
            margins: units.gu(1)
            rightMargin: units.gu(2)
        }

        visible: false
        inputMethodHints: Qt.ImhNoPredictiveText
        placeholderText: i18n.tr("Search...")
        onVisibleChanged: {
            if (visible) {
                if (activeFocus) {
                    Qt.inputMethod.show()
                } else {
                    searchField.forceActiveFocus()
                }
            }
        }

        Keys.onTabPressed: contactList.forceActiveFocus()
        Keys.onDownPressed: contactList.forceActiveFocus()
    }

    state: "default"
    states: [
        State {
            id: defaultState
            name: "default"

            property list<QtObject> leadingActions: [
                Action {
                    visible: mainPage.allowToQuit
                    iconName: "back"
                    text: i18n.tr("Quit")
                    onTriggered: {
                        application.goBackToSourceApp()
                        mainPage.returnToNormalState()
                    }
                }
            ]

            property list<QtObject> trailingActions:  [
                Action {
                    text: i18n.tr("Search")
                    iconName: "search"
                    visible: !mainPage.isEmpty
                    enabled: visible && (mainPage.state === "default")
                    shortcut: "Ctrl+F"
                    onTriggered: {
                        mainPage.state = "searching"
                        contactList.showAllContacts()
                        searchField.forceActiveFocus()
                    }
                },
                Action {
                    visible: (application.isOnline && (contactList.syncEnabled || application.serverSafeMode))
                    text: contactList.syncing ? i18n.tr("Syncing") : i18n.tr("Sync")
                    iconName: application.serverSafeMode ? "reset" : "reload"
                    enabled: !contactList.syncing && !application.updating
                    onTriggered: {
                        if (application.serverSafeMode) {
                            application.startUpdate()
                        } else {
                            contactList.sync()
                        }
                    }
                },
                Action {
                    text: i18n.tr("Settings")
                    iconName: "settings"
                    onTriggered:{
                        var incubator = pageStack.addPageToNextColumn(mainPage,
                                                                      Qt.resolvedUrl("./Settings/SettingsPage.qml"),
                                                                     {"contactListModel": contactList.listModel})
                        incubator.onStatusChanged = function(status) {
                            if (status === Component.Ready) {
                                incubator.object.onActiveChanged.connect(function(active) {
                                    if (!incubator.object.active) {
                                        mainPage.delayFetchContact()
                                        contactList.forceActiveFocus()
                                    }
                                })
                            }
                        }
                    }
                }
            ]

            PropertyChanges {
                target: pageHeader

                // TRANSLATORS: this refers to all contacts
                sectionsModel: contactMap.hasFavorites || (contactList.mostCalledCount > 0) ?
                                     [ i18n.tr("All"), i18n.tr("Favorites") ] : []
                leadingActions: defaultState.leadingActions
                trailingActions: defaultState.trailingActions
            }
            PropertyChanges {
                target: searchField
                text: ""
            }
            PropertyChanges {
                target: bottomEdgeLoader
                enabled: true
            }
        },
        State {
            id: searchingState
            name: "searching"

            property list<QtObject> leadingActions: [
                Action {
                    iconName: "back"
                    text: i18n.tr("Cancel")
                    enabled: (mainPage.state === "searching") &&
                             mainPage.active &&
                             (!pageStack.bottomEdge ||
                              (pageStack.bottomEdge && (pageStack.bottomEdge.status === BottomEdge.Hidden))) &&
                             ((pageStack.columns === 1) ||
                              (mainPage.viewPage && mainPage.viewPage.active))
                    shortcut:"Esc"
                    onTriggered: {
                        mainPage.head.sections.selectedIndex = 0
                        mainPage.state = "default"
                        contactList.forceActiveFocus()
                    }
                }
            ]

            PropertyChanges {
                target: pageHeader

                contents: searchField
                leadingActions: searchingState.leadingActions

            }

            PropertyChanges {
                target: bottomEdgeLoader
                enabled: false
            }

            PropertyChanges {
                target: searchField
                visible: true
                focus: true
                text: ""
            }
        },
        State {
            id: selectionState
            name: "selection"

            property list<QtObject> leadingActions: [
                Action {
                    objectName: "cancel"
                    name: "cancel"
                    text: i18n.tr("Cancel selection")
                    iconName: "back"
                    enabled: mainPage.state === "selection"
                    onTriggered: contactList.cancelSelection()
                    shortcut: "Esc"
                }
            ]

            property list<QtObject> trailingActions: [
                 Action {
                    text: (contactList.selectedItems.count === contactList.count) ? i18n.tr("Unselect All") : i18n.tr("Select All")
                    iconName: "select"
                    onTriggered: {
                        if (contactList.selectedItems.count === contactList.count) {
                            contactList.clearSelection()
                        } else {
                            contactList.selectAll()
                        }
                    }
                    visible: contactList.multipleSelection && !mainPage.isEmpty
                },
                Action {
                    objectName: "share"
                    text: i18n.tr("Share")
                    iconName: mainPage.pickMode ? "tick" : "share"
                    enabled: (contactList.selectedItems.count > 0)
                    visible: contactList.isInSelectionMode
                    onTriggered: {
                        var contacts = []
                        var items = contactList.selectedItems

                        for (var i=0, iMax=items.count; i < iMax; i++) {
                            contacts.push(items.get(i).model.contact)
                        }
                        contactExporter.start(contacts)
                        contactList.endSelection()
                    }
                },
                Action {
                    objectName: "delete"
                    text: i18n.tr("Delete")
                    iconName: "delete"
                    enabled: (contactList.selectedItems.count > 0)
                    visible: contactList.isInSelectionMode && !mainPage.pickMode
                    onTriggered: {
                        var contacts = []
                        var items = contactList.selectedItems

                        for (var i=0, iMax=items.count; i < iMax; i++) {
                            contacts.push(items.get(i).model.contact)
                        }

                        var dialog = PopupUtils.open(removeContactDialog, null)
                        dialog.contacts = contacts
                        contactList.endSelection()
                    }
                }
            ]

            PropertyChanges {
                target: pageHeader

                leadingActions: selectionState.leadingActions
                trailingActions: selectionState.trailingActions
            }

            PropertyChanges {
                target: bottomEdgeLoader
                enabled: false
            }
        },
        State {
            id: vcardImportedState
            name: "vcardImported"

            property list<QtObject> leadingActions: [
                Action {
                    objectName: "cancel"
                    name: "cancel"
                    iconName: "back"
                    text: i18n.tr("Back")
                    onTriggered: {
                        contactList.forceActiveFocus()
                        mainPage.state = "default"
                        importedIdsFilter.ids = []
                    }
                }
            ]

            PropertyChanges {
                target: pageHeader

                leadingActions: vcardImportedState.leadingActions
                title: i18n.tr("Imported contacts")
            }

            PropertyChanges {
                target: bottomEdgeLoader
                enabled: false
            }
        }
    ]

    //WORKAROUND: we need to call 'changeFilter' manually to make sure that the model will be cleared
    // before update it with the new model. This is faster than do a match of contacts
    transitions: [
         Transition {
            from: "vcardImported"
            ScriptAction {
                script: contactList.listModel.changeFilter(null)
            }
        },
        Transition {
            to: "vcardImported"
            ScriptAction {
                script: contactList.listModel.changeFilter(importedIdsFilter)
            }
        }
    ]

    onActiveChanged: {
        if (active && contactList.showAddNewButton) {
            contactList.positionViewAtBeginning()
        }

        if (active && (state === "searching")) {
            searchField.forceActiveFocus()
        } else if (active) {
            contactList.forceActiveFocus()
        }
    }

    IdFilter {
        id: importedIdsFilter
    }

    KeyboardRectangle {
        id: keyboard
        active: mainPage.active &&
                (pageStack.bottomEdge && (pageStack.bottomEdge.status === BottomEdge.Hidden))
    }

    ABEmptyState {
        id: emptyStateScreen

        anchors {
            verticalCenter: parent.verticalCenter
            left: parent.left
            right: parent.right
            leftMargin: units.gu(6)
            rightMargin: units.gu(6)
        }

        height: childrenRect.height
        visible: ((pageStack.columns === 1) &&
                  !contactList.busy &&
                  !contactList.favouritesIsSelected &&
                  mainPage.isEmpty &&
                  !(contactList.filterTerm && contactList.filterTerm !== ""))
        text: mainPage.pickMode ?
                  i18n.tr("You have no contacts.") :
                  i18n.tr("Create a new contact by swiping up from the bottom of the screen.")
    }

    ContactExporter {
        id: contactExporter

        contactModel: contactList.listModel
        exportToDisk: mainPage.pickMode
        onDone: {
            mainPage.pickMode = false
            mainPage.state = "default"
            application.returnVcard(outputFile)
        }

        onContactsFetched: {
            // Share contacts to an application chosen by the user
            if (!mainPage.pickMode) {
                contactExporter.dismissBusyDialog()
                pageStack.addPageToNextColumn(mainPage,
                                              contactShareComponent,
                                              {contactModel: contactExporter.contactModel,
                                               contacts: contacts })
            }
        }
    }

    Component {
        id: removeContactDialog

        RemoveContactsDialog {
            id: removeContactsDialogMessage

            onCanceled: {
                PopupUtils.close(removeContactsDialogMessage)
            }

            onAccepted: {
                removeContacts(contactList.listModel)
                PopupUtils.close(removeContactsDialogMessage)
            }
        }
    }

    Component {
        id: busyDialogComponent

        Popups.Dialog {
            id: busyDialog

            property alias allowToClose: closeButton.visible
            property alias showActivity: busyIndicator.visible

            title: i18n.tr("Importing...")

            ActivityIndicator {
                id: busyIndicator
                running: visible
                visible: true
            }
            Button {
                id: closeButton
                text: i18n.tr("Close")
                visible: false
                color: UbuntuColors.red
                onClicked: {
                    PopupUtils.close(mainPage._busyDialog)
                    mainPage._busyDialog = null
                }
            }
        }
    }

    Component {
        id: contactShareComponent

        ContactSharePage {
            objectName: "contactSharePage"
        }
    }

    Component.onCompleted: {
        application.elapsed()
        if ((typeof(TEST_DATA) !== "undefined") && (TEST_DATA != "")) {
            mainPage._importingTestData = true
            contactList.listModel.importContacts("file://" + TEST_DATA)
        }

        if (pageStack) {
            pageStack.contactListPage = mainPage
        }
    }

    Loader {
        id: bottomEdgeLoader

        enabled: false
        active: (pageStack.columns === 1) && bottomEdgeLoader.enabled
        asynchronous: true
        sourceComponent: ABNewContactBottomEdge {
            parent: mainPage
            modelToEdit: mainPage.contactModel
            hint.flickable: contactList.view
            pageStack: mainPage.pageStack
            enabled: mainPage.active
        }
    }

    Binding {
        target: pageStack
        property: 'bottomEdge'
        value: bottomEdgeLoader.item
        when: bottomEdgeLoader.status == Loader.Ready
    }

    ContactsUI.ContactMap {
        id: contactMap
    }

    Connections {
        target: mainPage.contactModel

        onContactsChanged: {
            if (contactIndex) {
                contactList.positionViewAtContact(mainPage.contactIndex)
                mainPage.contactIndex = null
                // at this point the operation has finished already
                mainPage._creatingContact = false
                fetchNewContactTimer.restart()
            }
        }
        onImportCompleted: {
            if (mainPage._importingTestData) {
                mainPage._importingTestData = false
                return
            }

            if (error !== ContactModel.ImportNoError) {
                console.error("Fail to import vcard:" + error)
                mainPage._busyDialog.title = i18n.tr("Fail to import contacts!")
                mainPage._busyDialog.allowToClose = true
                mainPage._busyDialog.showActivity = false
            } else {
                var importedIds = ids
                importedIds.concat(importedIdsFilter.ids)
                importedIdsFilter.ids = importedIds
                console.debug("Imported ids:" + importedIds)
                mainPage.state = "vcardImported"

                if (mainPage._busyDialog) {
                    PopupUtils.close(mainPage._busyDialog)
                    mainPage._busyDialog = null
                }
            }
        }
    }

    Connections {
        target: pageStack.bottomEdge
        onCommitCompleted: {
            if (mainPage.state !== "default") {
                mainPage.head.sections.selectedIndex = 0
                mainPage.state = "default"
            }
        }
        onCollapseCompleted: {
            if (!mainPage._creatingContact) {
                if (contactList.currentIndex === -1)
                    contactList.currentIndex = 0
                mainPage.delayFetchContact()
            }
            contactList.forceActiveFocus()
        }
    }
}