~phablet-team/address-book-app/rtm-14.09

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
/*
 * Copyright (C) 2012-2013 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.2
import QtContacts 5.0

import Ubuntu.Components 1.1
import Ubuntu.Content 1.1
import Ubuntu.Components.Popups 1.0

Item {
    id: root

    property var contactModel
    property bool exportToDisk: true
    property var activeTransfer: null

    signal contactsFetched(var contacts)
    signal done(string outputFile)

    function start(contacts) {
        if (!contactModel) {
            console.error("No contact model defined")
            return
        }

        // skip if a query is running
        if (priv.currentQueryId != -1) {
            console.error("Export already running")
            return
        }

        if (!priv.busyDialog) {
            priv.busyDialog = PopupUtils.open(busyDialogComponent, root)
        }

        var ids = []
        for (var i=0; i < contacts.length; i++) {
            ids.push(contacts[i].contactId)
        }
        if (ids.length == 0) {
            console.debug("The contact list is empty")
            done("")
        } else {
            priv.currentQueryId = contactModel.fetchContacts(ids)
        }
    }

    function dismissBusyDialog()
    {
        if (priv.busyDialog) {
            PopupUtils.close(priv.busyDialog)
            priv.busyDialog = null
        }
    }

    Item {
        id: priv

        property var busyDialog: null
        property int currentQueryId: -1
        readonly property var detailsBlackList: [ ContactDetail.Favorite, ContactDetail.Tag ]

        function filterContactDetails(contact)
        {
            var newContact = Qt.createQmlObject("import QtContacts 5.0;  Contact { }", root)
            var allDetails = contact.contactDetails
            for(var i=0; i < allDetails.length; i++) {
                var det = allDetails[i]
                if (detailsBlackList.indexOf(det.type) == -1) {
                    newContact.addDetail(det)
                }
            }
            return newContact
        }

        function generateOutputFileName(contacts)
        {
            if (contacts.length === 1) {
                return "file:///tmp/%1.vcf".arg(contacts[0].displayLabel.label.replace(/\s/g, ''))
            } else {
                return "file:///tmp/ubuntu_contacts.vcf";
            }
        }

        Connections {
            target: root.contactModel

            onExportCompleted: {
                // send contacts back to source app (pick mode)
                if (error === ContactModel.ExportNoError) {
                    var obj = Qt.createQmlObject("import Ubuntu.Content 1.1;  ContentItem { url: '" + url + "' }", root)
                    if (root.activeTransfer) {
                        root.activeTransfer.items = [obj]
                        root.activeTransfer.state = ContentTransfer.Charged
                    } else {
                        console.error("No active transfer")
                    }
                } else {
                    root.activeTransfer = ContentHub.ContentTransfer.Aborted
                    console.error("Fail to export contacts:" + error)
                }
                root.dismissBusyDialog()
                root.done(url)
            }

            onContactsFetched: {
                // currentQueryId == -2 is used during a fetch using "memory" manager
                if ((priv.currentQueryId == -2) || (requestId == priv.currentQueryId)) {
                    if (root.exportToDisk) {
                        var contacts = []
                        // remove unnecessary info from contacts
                        for(var i=0; i < fetchedContacts.length; i++) {
                            contacts.push(priv.filterContactDetails(fetchedContacts[i]))
                        }
                        // update outputFile with a friendly name
                        var outputFile = priv.generateOutputFileName(contacts)
                        root.contactModel.exportContacts(outputFile,
                                                         [],
                                                         contacts)
                    }
                    root.contactsFetched(fetchedContacts)
                    priv.currentQueryId = -1
                }
            }
        }

        Connections {
            target: root.activeTransfer

            onStateChanged: {
                if (root.activeTransfer.state === ContentTransfer.Aborted) {
                    root.activeTransfer = null
                    root.done("")
                }
            }
        }
    }

    Component {
        id: busyDialogComponent

        Dialog {
            title: i18n.tr("Exporting contacts...")

            ActivityIndicator {
                id: activity

                anchors.horizontalCenter: parent.horizontalCenter
                running: true
            }
        }
    }
}