Debian Codegraph @

src/imports/Lomiri/Contacts/SIMCardImportPage.qml

raw
1 /*
2 * Copyright (C) 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.Contacts 0.1
22 import Lomiri.Components.ListItems 1.3 as ListItem
23
24 import QOfono 0.2
25
26 Page {
27 id: root
28
29 readonly property string exportFile: Contacts.tempFile("lomiri_contacts_XXXXXX.vcf")
30 readonly property alias hasContacts: simCardContacts.hasContacts
31 property var targetModel: null
32 property var sims: []
33
34 signal importCompleted()
35
36 header: PageHeader {
37 id: pageHeader
38
39 title: i18n.dtr("lomiri-addressbook-app", "SIM contacts")
40 flickable: contactList.view
41 trailingActionBar {
42 actions: [
43 Action {
44 text: (contactList.selectedItems.count === contactList.count) ?
45 i18n.dtr("lomiri-addressbook-app", "Unselect All") :
46 i18n.dtr("lomiri-addressbook-app", "Select All")
47 iconName: "select"
48 onTriggered: {
49 if (contactList.selectedItems.count === contactList.count) {
50 contactList.clearSelection()
51 } else {
52 contactList.selectAll()
53 }
54 }
55 visible: (contactList.count > 0)
56 },
57 Action {
58 text: i18n.dtr("lomiri-addressbook-app", "Import")
59 objectName: "confirmImport"
60
61 iconName: "tick"
62 enabled: (contactList.selectedItems.count > 0)
63 onTriggered: {
64 root.state = "importing"
65 var contacts = []
66 var items = contactList.selectedItems
67
68 for (var i=0, iMax=items.count; i < iMax; i++) {
69 contacts.push(items.get(i).model.contact)
70 }
71
72 contactList.listModel.exportContacts(root.exportFile,
73 [],
74 contacts)
75 }
76 }
77 ]
78 }
79 }
80
81 function lockedSIMCount()
82 {
83 var count = 0
84 for(var i=0; i < sims.length; i++) {
85 if (sims[i].simMng.pinRequired !== OfonoSimManager.NoPin) {
86 count++
87 }
88 }
89 return count
90 }
91
92 Timer {
93 id: simUnlocking
94
95 interval: 2000
96 repeat: false
97 running: false
98 }
99
100 Column {
101 id: lockedSIMList
102 anchors {
103 left: parent.left
104 right: parent.right
105 }
106
107 Repeater {
108 id: lockedSIMRepeater
109 anchors {
110 left: parent.left
111 right: parent.right
112 }
113 model: sims.length
114 delegate: ListItem.Standard {
115 visible: sims[index].simMng.pinRequired !== OfonoSimManager.NoPin
116 onVisibleChanged: {
117 if (visible)
118 simUnlocking.stop()
119 else
120 simUnlocking.start()
121 }
122 text: i18n.dtr("lomiri-addressbook-app", "%1 is locked").arg(sims[index].title)
123 control: Button {
124 text: i18n.dtr("lomiri-addressbook-app", "Unlock")
125 onClicked: simCardContacts.unlockModem(sims[index].path)
126 }
127 }
128 }
129 }
130
131 ContactListView {
132 id: contactList
133 objectName: "contactListViewFromSimCard"
134
135 anchors {
136 left: parent.left
137 right: parent.right
138 top: lockedSIMList.bottom
139 bottom: parent.bottom
140 }
141 multiSelectionEnabled: true
142 multipleSelection: true
143 showSections: false
144 visible: !indicator.visible && !statusMessage.visible
145 showBusyIndicator: false
146
147 manager: "memory"
148 onSelectionCanceled: pageStack.removePages(root)
149 }
150
151 Label {
152 id: statusMessage
153
154 anchors.centerIn: parent
155 text: i18n.dtr("lomiri-addressbook-app", "No contacts found")
156 visible: ((contactList.count === 0) &&
157 (root.state === "") &&
158 !contactList.busy &&
159 (sims.length > root.lockedSIMCount()))
160 }
161
162 Column {
163 id: indicator
164
165 property alias title: activityLabel.text
166
167 anchors.centerIn: root
168 spacing: units.gu(2)
169 visible: false
170
171 ActivityIndicator {
172 id: activity
173
174 anchors.horizontalCenter: parent.horizontalCenter
175 running: indicator.visible
176 }
177 Label {
178 id: activityLabel
179
180 anchors.horizontalCenter: activity.horizontalCenter
181 }
182 }
183
184 SimCardContacts {
185 id: simCardContacts
186
187 property bool contactImported: false
188
189 Component.onCompleted: {
190 if (vcardFile != "" && !contactImported) {
191 contactImported = true
192 contactList.listModel.importContacts(vcardFile)
193 }
194 }
195 onVcardFileChanged: {
196 if ((vcardFile != "") && !contactImported) {
197 contactImported = true
198 contactList.listModel.importContacts(vcardFile)
199 }
200 }
201 onImportFail: {
202 console.error("Sim card import fail")
203 root.state = "error"
204 }
205 }
206
207 Connections {
208 target: contactList.listModel
209 function onImportCompleted(error, url, ids) {
210 contactList.startSelection()
211 root.state = ""
212 }
213
214 function onExportCompleted(error, url) {
215 if ((error === ContactModel.ExportNoError) && targetModel) {
216 root.state = "saving"
217 targetModel.importContacts(url)
218 } else {
219 console.error("Failt to export selected contacts")
220 root.state = "error"
221 }
222 }
223 }
224
225 Connections {
226 target: root.targetModel
227 function onImportCompleted(error, url, ids) {
228 if (error === ContactModel.ImportNoError) {
229 Contacts.removeFile(root.exportFile)
230 root.state = ""
231 if (pageStack.removePages)
232 pageStack.removePages(root)
233 else
234 pageStack.pop()
235 root.importCompleted()
236 } else {
237 console.error("Fail to import contacts on device")
238 root.state = "error"
239 }
240 }
241 }
242
243
244
245 states: [
246 State {
247 name: "loading"
248 when: (simCardContacts.busy || contactList.busy) &&
249 (sims.length > root.lockedSIMCount())
250 PropertyChanges {
251 target: indicator
252 title: i18n.dtr("lomiri-addressbook-app", "Loading...")
253 visible: true
254 }
255 },
256 State {
257 name: "unlocking"
258 when: !simCardContacts.busy && (contactList.count == 0) && (simUnlocking.running)
259 PropertyChanges {
260 target: indicator
261 title: i18n.dtr("lomiri-addressbook-app", "Unlocking...")
262 visible: true
263 }
264 },
265 State {
266 name: "importing"
267 PropertyChanges {
268 target: indicator
269 title: i18n.dtr("lomiri-addressbook-app", "Reading contacts from SIM...")
270 visible: true
271 }
272 },
273 State {
274 name: "saving"
275 PropertyChanges {
276 target: indicator
277 title: i18n.dtr("lomiri-addressbook-app", "Saving contacts on phone...")
278 visible: true
279 }
280 },
281 State {
282 name: "error"
283 PropertyChanges {
284 target: statusMessage
285 text: i18n.dtr("lomiri-addressbook-app", "Fail to read SIM card")
286 visible: true
287 }
288 }
289 ]
290
291 Component.onDestruction: {
292 Contacts.removeFile(root.exportFile)
293 }
294 }