Debian Codegraph @

src/imports/Lomiri/AddressBook/Base/ContactExporter.qml

raw
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 QtContacts 5.0
19
20 import Lomiri.Components 1.3
21 import Lomiri.Content 1.3
22 import Lomiri.Components.Popups 1.3
23 import Qt.labs.platform 1.0
24
25 Item {
26 id: root
27
28 property var contactModel
29 property bool exportToDisk: true
30 property var activeTransfer: null
31 readonly property string exportPath: StandardPaths.writableLocation(StandardPaths.CacheLocation)
32
33 signal contactsFetched(var contacts)
34 signal done(string outputFile)
35
36 function start(contacts) {
37 if (!contactModel) {
38 console.error("No contact model defined")
39 return
40 }
41
42 // skip if a query is running
43 if (priv.currentQueryId != -1) {
44 console.error("Export already running")
45 return
46 }
47
48 if (!priv.busyDialog) {
49 priv.busyDialog = PopupUtils.open(Qt.resolvedUrl("BusyExportingDialog.qml"), root)
50 }
51
52 var ids = []
53 for (var i=0; i < contacts.length; i++) {
54 ids.push(contacts[i].contactId)
55 }
56 if (ids.length == 0) {
57 console.debug("The contact list is empty")
58 done("")
59 } else {
60 if (root.contactModel.manager === "memory") {
61 // memory backend emit contact fetched before return from "fetchContacts" we will use operation = "-2"
62 // to say that we are wainting for a operation from memory manager
63 priv.currentQueryId = -2
64 contactModel.fetchContacts(ids)
65 } else {
66 priv.currentQueryId = contactModel.fetchContacts(ids)
67 }
68 }
69 }
70
71 function dismissBusyDialog()
72 {
73 if (priv.busyDialog) {
74 PopupUtils.close(priv.busyDialog)
75 priv.busyDialog = null
76 }
77 }
78
79 Item {
80 id: priv
81
82 property var busyDialog: null
83 property int currentQueryId: -1
84 readonly property var detailsBlackList: [ ContactDetail.Favorite,
85 ContactDetail.Tag,
86 ContactDetail.ExtendedDetail,
87 ContactDetail.Guid ]
88
89 function filterContactDetails(contact)
90 {
91 var newContact = Qt.createQmlObject("import QtContacts 5.0; Contact { }", root)
92 var allDetails = contact.contactDetails
93 for(var i=0; i < allDetails.length; i++) {
94 var det = allDetails[i]
95 if (detailsBlackList.indexOf(det.type) == -1) {
96 newContact.addDetail(det)
97 }
98 }
99 return newContact
100 }
101
102 function generateOutputFileName(contacts)
103 {
104 if (contacts.length === 1) {
105 return exportPath + "/%1.vcf".arg(contacts[0].displayLabel.label.replace(/[\s\?\[\]\/\\=<>:;,\'"&\$#*()|~`!{}%+]+/gi, '_'))
106 } else {
107 var fileName = new Date().toLocaleString(Qt.locale(), "yyyyMMddHHmmss")
108 return exportPath + "/lomiri_contacts_" + fileName + ".vcf";
109 }
110 }
111
112 Connections {
113 target: root.contactModel
114
115 function onExportCompleted(error, url) {
116 // send contacts back to source app (pick mode)
117 if (error === ContactModel.ExportNoError) {
118 var obj = Qt.createQmlObject("import Lomiri.Content 1.3; ContentItem { url: '" + url + "' }", root)
119 if (root.activeTransfer) {
120 root.activeTransfer.items = [obj]
121 root.activeTransfer.state = ContentTransfer.Charged
122 } else {
123 console.error("No active transfer")
124 }
125 } else {
126 root.activeTransfer = ContentHub.ContentTransfer.Aborted
127 console.error("Fail to export contacts:" + error)
128 }
129 root.dismissBusyDialog()
130 root.done(url)
131 }
132
133 function onContactsFetched(requestId, fetchedContacts) {
134 // currentQueryId == -2 is used during a fetch using "memory" manager
135 if ((priv.currentQueryId == -2) || (requestId == priv.currentQueryId)) {
136 if (root.exportToDisk) {
137 var contacts = []
138 // remove unnecessary info from contacts
139 for(var i=0; i < fetchedContacts.length; i++) {
140 contacts.push(priv.filterContactDetails(fetchedContacts[i]))
141 }
142 // update outputFile with a friendly name
143 var outputFile = priv.generateOutputFileName(contacts)
144 root.contactModel.exportContacts(outputFile,
145 [],
146 contacts)
147 }
148 root.contactsFetched(fetchedContacts)
149 priv.currentQueryId = -1
150 }
151 }
152 }
153
154 Connections {
155 target: root.activeTransfer
156
157 function onStateChanged() {
158 if (root.activeTransfer.state === ContentTransfer.Aborted) {
159 root.activeTransfer = null
160 root.done("")
161 }
162 }
163 }
164 }
165 }