~renatofilho/address-book-app/new-designer-contact-list

« back to all changes in this revision

Viewing changes to src/imports/ContactList/ContactListPage.qml

  • Committer: Renato Araujo Oliveira Filho
  • Date: 2014-06-17 00:41:33 UTC
  • mfrom: (169.9.9 staging)
  • Revision ID: renato.filho@canonical.com-20140617004133-m4lm6hbiiyd1zx0w
Staging merged.

Show diffs side-by-side

added added

removed removed

Lines of Context:
21
21
import Ubuntu.Components.ListItems 0.1 as ListItem
22
22
import Ubuntu.Contacts 0.1 as ContactsUI
23
23
import Ubuntu.Components.Popups 0.1 as Popups
 
24
import Ubuntu.Content 0.1 as ContentHub
24
25
import "../ContactEdit"
25
26
import "../Common"
26
27
 
29
30
    objectName: "contactListPage"
30
31
 
31
32
    property bool pickMode: false
 
33
    property alias contentHubTransfer: contactExporter.activeTransfer
32
34
    property bool pickMultipleContacts: false
33
35
    property var onlineAccountsMessageDialog: null
34
36
    property QtObject contactIndex: null
122
124
            bottom: keyboard.top
123
125
            right: parent.right
124
126
        }
 
127
        contactNameFilter: searchField.text
125
128
        detailToPick: ContactDetail.PhoneNumber
126
129
        multiSelectionEnabled: true
127
130
        multipleSelection: !pickMode ||
128
 
                           ((contactContentHub && contactContentHub.multipleItems) || mainPage.pickMultipleContacts)
129
 
        DetailFilter {
130
 
            id: nameFilter
131
 
 
132
 
            detail: ContactDetail.DisplayLabel
133
 
            field: DisplayLabel.Label
134
 
            value: searchField.text
135
 
            matchFlags: DetailFilter.MatchContains
136
 
        }
137
 
 
138
 
        filter: (searchField.text != "" ? nameFilter : null)
 
131
                           mainPage.pickMultipleContacts || (contactExporter.active && contactExporter.isMultiple)
139
132
 
140
133
        anchors.fill: parent
141
134
 
190
183
                for (var i=0; i < items.count; i++) {
191
184
                    contacts.push(items.get(i).model.contact)
192
185
                }
193
 
                exporter.contactModel = contactList.listModel
194
 
                exporter.contacts = contacts
195
 
                exporter.start()
 
186
                contactExporter.exportContacts(contacts)
196
187
            } else {
197
188
                var contacts = []
198
189
 
207
198
 
208
199
        onSelectionCanceled: {
209
200
            if (pickMode) {
210
 
                if (contactContentHub) {
211
 
                    contactContentHub.cancelTransfer()
 
201
                if (contentHubTransfer) {
 
202
                    contentHubTransfer.state = ContentTransfer.Aborted
212
203
                }
213
204
                pageStack.pop()
214
205
                application.returnVcard("")
336
327
            bottomMargin: units.gu(1.5)
337
328
            verticalCenter: parent.verticalCenter
338
329
        }
339
 
        onTextChanged: {
340
 
            contactList.currentIndex = -1
341
 
            contactSearchTimeout.restart()
342
 
        }
 
330
        onTextChanged: contactList.currentIndex = -1
343
331
        inputMethodHints: Qt.ImhNoPredictiveText
344
332
    }
345
333
 
442
430
        }
443
431
    }
444
432
 
445
 
    ContactExporter {
446
 
        id: exporter
447
 
        contactModel: contactList.listModel ? contactList.listModel : null
448
 
        outputFile: contactContentHub ? contactContentHub.createTemporaryFile() : "/tmp/vcard_address_book_app.vcf"
449
 
        onCompleted: {
450
 
            if (contactContentHub) {
451
 
                if (error == ContactModel.ExportNoError) {
452
 
                    contactContentHub.returnContacts(exporter.outputFile)
453
 
                } else {
454
 
                    contactContentHub.cancelTransfer()
455
 
                }
 
433
 
 
434
    QtObject {
 
435
        id: contactExporter
 
436
 
 
437
        property var activeTransfer: null
 
438
        readonly property bool active: activeTransfer && (activeTransfer.state === ContentHub.ContentTransfer.InProgress && activeTransfer.direction === ContentHub.ContentTransfer.Import)
 
439
        readonly property bool isMultiple: activeTransfer && (activeTransfer.selectionType === ContentHub.ContentTransfer.Multiple)
 
440
 
 
441
        function exportContacts(contacts)
 
442
        {
 
443
            if (activeTransfer) {
 
444
                var exportUrl = "file:///tmp/address_book_app_export.vcf"
 
445
                mainPage.contactModel.exportCompleted.connect(contactExporter.onExportCompleted)
 
446
                mainPage.contactModel.exportContacts(exportUrl, [], contacts)
 
447
            } else {
 
448
                console.error("Export requested with noo active transfer")
 
449
            }
 
450
        }
 
451
 
 
452
        function onExportCompleted(error, url)
 
453
        {
 
454
            mainPage.contactModel.exportCompleted.disconnect(contactExporter.onExportCompleted)
 
455
            if (error === ContactModel.ExportNoError) {
 
456
                var obj = Qt.createQmlObject("import Ubuntu.Content 0.1;  ContentItem { url: '" + url + "' }", contactExporter)
 
457
                activeTransfer.items = [obj]
 
458
                activeTransfer.state = ContentHub.ContentTransfer.Charged
 
459
            } else {
 
460
                console.error("Fail to export contacts:" + error)
456
461
            }
457
462
            pageStack.pop()
458
 
            application.returnVcard(exporter.outputFile)
459
 
        }
460
 
    }
461
 
 
462
 
    Timer {
463
 
        id: contactSearchTimeout
464
 
 
465
 
        running: false
466
 
        repeat: false
467
 
        interval: 300
468
 
        onTriggered: {
469
 
            if (searchField.text === "") {
470
 
                if (contactList.filter !== null) {
471
 
                    contactList.changeFilter(null)
472
 
                }
473
 
            } else {
474
 
                if (contactList.filter !== nameFilter) {
475
 
                    contactList.changeFilter(nameFilter)
476
 
                }
477
 
                nameFilter.value = searchField.text
478
 
            }
479
 
        }
480
 
    }
481
 
 
 
463
            application.returnVcard(url)
 
464
        }
 
465
    }
482
466
 
483
467
    Component.onCompleted: {
484
468
        if (pickMode) {
492
476
        if (TEST_DATA != "") {
493
477
            contactList.listModel.importContacts("file://" + TEST_DATA)
494
478
        }
 
479
 
 
480
        if (!pickMode) {
 
481
            mainPage.setBottomEdgePage(Qt.resolvedUrl("../ContactEdit/ContactEditor.qml"),
 
482
                                       {model: contactList.listModel,
 
483
                                        contact: mainPage.createEmptyContact(""),
 
484
                                        active: false,
 
485
                                        enabled: false})
 
486
        }
495
487
    }
496
488
}