Debian Codegraph @

src/imports/Lomiri/AddressBook/ContactEditor/ContactEditorPage.qml

raw ◀ ▶
Source reconstructed at patch state 0 of 3 — not the installed (fully patched) source.
3 Debian patches touch this file
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.Components.ListItems 1.3 as ListItems
22 import Lomiri.Components.Popups 1.3
23 import Lomiri.Contacts 0.1 as ContactsUI
24
25 import Lomiri.AddressBook.Base 0.1
26
27 Page {
28 id: contactEditor
29
30 property QtObject contact: null
31 property QtObject model: null
32 property QtObject activeItem: null
33
34 property string initialFocusSection: ""
35 property var newDetails: []
36 property list<QtObject> leadingActions
37 property alias headerActions: trailingBar.actions
38 property bool newFieldAction: false
39
40 readonly property bool isNewContact: contact && (!contact.contactId || contact.contactId === "qtcontacts:::")
41 readonly property bool isContactValid: !avatarEditor.busy && (!nameEditor.isEmpty() || !phonesEditor.isEmpty())
42 readonly property alias editorFlickable: scrollArea
43
44 signal contactSaved(var contact);
45 signal canceled()
46
47 function close() {
48 if (pageStack.removePages) {
49 pageStack.removePages(contactEditor)
50 } else {
51 pageStack.pop()
52 }
53 }
54
55
56 function cancel() {
57 for (var i = 0; i < contactEditor.newDetails.length; ++i) {
58 contactEditor.contact.removeDetail(contactEditor.newDetails[i])
59 }
60 contactEditor.newDetails = []
61
62 for(var i = 0; i < contents.children.length; ++i) {
63 var field = contents.children[i]
64 if (field.cancel) {
65 field.cancel()
66 }
67 }
68 if (pageStack && pageStack.removePages) {
69 pageStack.removePages(contactEditor)
70 } else if (pageStack) {
71 pageStack.pop()
72 }
73 contactEditor.canceled()
74 }
75
76 function save() {
77 var changed = false
78 for(var i = 0; i < contents.children.length; ++i) {
79 var field = contents.children[i]
80 if (field.save) {
81 if (field.save()) {
82 changed = true
83 }
84 }
85 }
86
87 // new contact and there is only two details (name, avatar)
88 // name and avatar, are not removable details, because of that the contact will have at least 2 details
89 if (isNewContact &&
90 (contact.contactDetails.length === 2)) {
91
92 // if name is empty this means that the contact is empty
93 var nameDetail = contact.detail(ContactDetail.Name)
94 if (nameDetail &&
95 (nameDetail.firstName && nameDetail.firstName != "") ||
96 (nameDetail.lastName && nameDetail.lastName != "")) {
97 // save contact
98 } else {
99 changed = false
100 }
101 }
102
103 if (changed) {
104 var newContact = (contact.model == null)
105 contactEditor.enabled = false
106 contact.onContactChanged.connect(contactEditor._onContactSaved)
107 contactEditor.model.onErrorChanged.connect(contactEditor._onContactSavedError)
108 contactEditor.model.saveContact(contact)
109 } else {
110 close()
111 }
112 }
113
114 function idleMakeMeVisible(item) {
115 if (!enabled || !item) {
116 return
117 }
118
119 activeItem = item
120 timerMakemakeMeVisible.restart()
121 }
122
123 function makeMeVisible(item) {
124 if (!item)
125 return
126
127
128 var position = item.mapToItem(editEditor, item.x, item.y);
129 // check if the item is already visible
130 var bottomY = scrollArea.contentY + scrollArea.height
131 var itemBottom = position.y + (item.height * 3) // extra margin
132 if (position.y >= scrollArea.contentY && itemBottom <= bottomY) {
133 Qt.inputMethod.show()
134 return;
135 }
136
137 // if it is not, try to scroll and make it visible
138 var targetY = itemBottom - scrollArea.height
139 if (targetY >= 0 && position.y) {
140 scrollArea.contentY = targetY;
141 } else if (position.y < scrollArea.contentY) {
142 // if it is hidden at the top, also show it
143 scrollArea.contentY = position.y;
144 }
145
146 scrollArea.returnToBounds()
147 Qt.inputMethod.show()
148 }
149
150 function ready()
151 {
152 enabled = true
153 switch (contactEditor.initialFocusSection)
154 {
155 case "phones":
156 contactEditor.focusToLastPhoneField()
157 break;
158 case "name":
159 default:
160 nameEditor.fieldDelegates[0].forceActiveFocus()
161 break;
162 }
163 }
164
165 function focusToLastPhoneField()
166 {
167 var lastPhoneField = phonesEditor.detailDelegates[phonesEditor.detailDelegates.length - 2].item
168 lastPhoneField.forceActiveFocus()
169 }
170
171 function focusToFirstEntry(field)
172 {
173 //only focus when new field requested
174 if (newFieldAction) {
175 var itemToFocus = field
176 if (field.repeater)
177 itemToFocus = field.repeater.itemAt(0)
178
179 if (itemToFocus) {
180 root.idleMakeMeVisible(itemToFocus)
181 itemToFocus.forceActiveFocus()
182 }
183 }
184 newFieldAction = false
185
186 }
187
188 function _onContactSaved()
189 {
190 contact.onContactChanged.disconnect(contactEditor._onContactSaved)
191 contactEditor.model.onErrorChanged.disconnect(contactEditor._onContactSavedError)
192 contactEditor.contactSaved(contact)
193 close()
194 }
195
196 function _onContactSavedError() {
197 if (contact) {
198 contact.onContactChanged.disconnect(contactEditor._onContactSaved)
199 }
200 contactEditor.model.onErrorChanged.disconnect(contactEditor._onContactSavedError)
201 PopupUtils.open(saveErrorDialogComp)
202 contactEditor.enabled = true
203 }
204
205 Timer {
206 id: timerMakemakeMeVisible
207
208 interval: 100
209 repeat: false
210 running: false
211 onTriggered: root.makeMeVisible(root.activeItem)
212 }
213
214 header: PageHeader {
215 id: pageHeader
216
217 title: isNewContact ? i18n.dtr("lomiri-addressbook-app", "New contact") : i18n.dtr("lomiri-addressbook-app", "Edit")
218 trailingActionBar {
219 id: trailingBar
220 }
221 leadingActionBar {
222 id: leadingBar
223 actions: contactEditor.leadingActions
224 }
225 }
226
227 enabled: active
228 flickable: null
229 Timer {
230 id: focusTimer
231
232 interval: 1000
233 running: false
234 repeat: false
235 onTriggered: contactEditor.ready()
236 }
237
238 ActivityIndicator {
239 z: 100
240 anchors.centerIn: parent
241 running: !contactEditor.enabled
242 }
243
244 Component {
245 id: saveErrorDialogComp
246 Dialog {
247 id: saveErrorDialog
248
249 title: i18n.dtr("lomiri-addressbook-app", "Contact Editor")
250 text: i18n.dtr("lomiri-addressbook-app", "Could not save the contact. Please check the logs.")
251
252 Button {
253 text: i18n.dtr("lomiri-addressbook-app", "Close")
254 onClicked: {
255 PopupUtils.close(saveErrorDialog)
256 contactEditor.close()
257 }
258 }
259
260 Component.onCompleted: Qt.inputMethod.hide()
261 }
262 }
263
264 Flickable {
265 id: scrollArea
266 objectName: "scrollArea"
267
268 // this is necessary to avoid the page to appear bellow the header
269 clip: true
270 flickableDirection: Flickable.VerticalFlick
271 anchors{
272 left: parent.left
273 top: parent.top
274 topMargin: pageHeader.height
275 right: parent.right
276 bottom: keyboardRectangle.top
277 }
278 contentHeight: contents.height + units.gu(2)
279 contentWidth: parent.width
280
281 Column {
282 id: contents
283
284 anchors {
285 top: parent.top
286 topMargin: units.gu(2)
287 left: parent.left
288 right: parent.right
289 }
290 height: childrenRect.height
291
292 Row {
293 id: editEditor
294 function save()
295 {
296 var avatarSave = avatarEditor.save()
297 var nameSave = nameEditor.save();
298
299 return (nameSave || avatarSave);
300 }
301
302 function isEmpty()
303 {
304 return (avatarEditor.isEmpty() && nameEditor.isEmpty())
305 }
306
307 anchors {
308 left: parent.left
309 leftMargin: units.gu(2)
310 right: parent.right
311 }
312 height: Math.max(avatarEditor.height, nameEditor.height) - units.gu(2)
313
314 ContactDetailAvatarEditor {
315 id: avatarEditor
316
317 contact: contactEditor.contact
318 height: implicitHeight
319 width: implicitWidth
320 anchors {
321 top: parent.top
322 }
323 }
324
325 ContactDetailNameEditor {
326 id: nameEditor
327
328 width: parent.width - avatarEditor.width
329 height: nameEditor.implicitHeight + units.gu(3)
330 contact: contactEditor.contact
331 }
332 }
333
334
335 ContactDetailPhoneNumbersEditor {
336 id: phonesEditor
337 objectName: "phones"
338
339 contact: contactEditor.contact
340 anchors {
341 left: parent.left
342 right: parent.right
343 }
344 height: implicitHeight
345 onNewFieldAdded: root.focusToFirstEntry(field)
346 }
347
348 ContactDetailEmailsEditor {
349 id: emailsEditor
350 objectName: "emails"
351
352 contact: contactEditor.contact
353 anchors {
354 left: parent.left
355 right: parent.right
356 }
357 height: implicitHeight
358 onNewFieldAdded: root.focusToFirstEntry(field)
359 }
360
361 ContactDetailUrlEditor {
362 id: urlEditor
363 objectName: "emails"
364
365 contact: contactEditor.contact
366 anchors {
367 left: parent.left
368 right: parent.right
369 }
370 height: implicitHeight
371 onNewFieldAdded: {
372 root.idleMakeMeVisible(field)
373 field.forceActiveFocus()
374 }
375 }
376
377 ContactDetailOnlineAccountsEditor {
378 id: accountsEditor
379 objectName: "ims"
380
381 contact: contactEditor.contact
382 anchors {
383 left: parent.left
384 right: parent.right
385 }
386 height: implicitHeight
387 onNewFieldAdded: root.focusToFirstEntry(field)
388 }
389
390 ContactDetailAddressesEditor {
391 id: addressesEditor
392 objectName: "addresses"
393
394 contact: contactEditor.contact
395 anchors {
396 left: parent.left
397 right: parent.right
398 }
399 height: implicitHeight
400 onNewFieldAdded: root.focusToFirstEntry(field)
401 }
402
403 ContactDetailBirthdayEditor {
404 id: birthdayEditor
405 objectName: "birthday"
406
407 contact: contactEditor.contact
408 anchors {
409 left: parent.left
410 right: parent.right
411 }
412 height: implicitHeight
413 }
414
415 ContactDetailNoteEditor {
416 id: noteEditor
417 objectName: "birthday"
418
419 contact: contactEditor.contact
420 anchors {
421 left: parent.left
422 right: parent.right
423 }
424 height: implicitHeight
425 onNewFieldAdded: {
426 root.idleMakeMeVisible(field)
427 field.forceActiveFocus()
428 }
429 }
430
431 ContactDetailOrganizationsEditor {
432 id: organizationsEditor
433 objectName: "professionalDetails"
434
435 contact: contactEditor.contact
436 anchors {
437 left: parent.left
438 right: parent.right
439 }
440 height: implicitHeight
441 onNewFieldAdded: root.focusToFirstEntry(field)
442 }
443
444 ContactDetailRingtoneEditor {
445 id: ringToneEditor
446 objectName: "ringTone"
447
448 contact: contactEditor.contact
449 anchors {
450 left: parent.left
451 right: parent.right
452 }
453 height: implicitHeight
454 overlayLoader: overlayEditorPage
455 }
456
457 ContactDetailSyncTargetEditor {
458 id: syncTargetEditor
459
460 active: contactEditor.active
461 contact: contactEditor.contact
462 anchors {
463 left: parent.left
464 right: parent.right
465 }
466 property var alertMessageDialog
467 onChanged: {
468 if (contactEditor.enabled &&
469 !contactEditor.isNewContact &&
470 syncTargetEditor.contactIsReadOnly(contactEditor.contact)) {
471 syncTargetEditor.alertMessageDialog = PopupUtils.open(Qt.resolvedUrl("AlertMessageDialog.qml"),
472 null,
473 {"contact": contactEditor.contact})
474 syncTargetEditor.alertMessageDialog.destruction.connect(contactEditor.close)
475 }
476 }
477 }
478
479 ListItems.ThinDivider {}
480
481 Item {
482 anchors {
483 left: parent.left
484 right: parent.right
485 }
486 height: units.gu(2)
487 }
488
489 ComboButtonAddField {
490 id: addNewFieldButton
491 objectName: "addNewFieldButton"
492
493 contact: contactEditor.contact
494 singleValueDetails: [organizationsEditor, birthdayEditor, noteEditor, urlEditor, ringToneEditor]
495 text: i18n.dtr("lomiri-addressbook-app", "Add field")
496 anchors {
497 left: parent.left
498 right: parent.right
499 margins: units.gu(2)
500 }
501 height: implicitHeight
502 activeFocusOnPress: false
503 onHeightChanged: {
504 if (expanded &&
505 (height === expandedHeight) &&
506 !scrollArea.atYEnd &&
507 !moveToBottom.running) {
508 moveToBottom.start()
509 }
510 }
511
512 LomiriNumberAnimation {
513 id: moveToBottom
514
515 target: scrollArea
516 property: "contentY"
517 alwaysRunToEnd: true
518 to: scrollArea.contentHeight - scrollArea.height
519 onStopped: {
520 scrollArea.returnToBounds()
521 addNewFieldButton.forceActiveFocus()
522 }
523 }
524
525 onSpecialFieldSelected: {
526 if (type === addNewFieldButton.specialFields.CONTACT_DETAIL_MIDDLE_NAME) {
527 nameEditor.showMiddleName = true
528 nameEditor.middleNameField.forceActiveFocus()
529 root.idleMakeMeVisible(nameEditor.middleNameField)
530 }
531 }
532
533 onFieldSelected: {
534 if ([ContactDetail.Birthday, ContactDetail.Note, ContactDetail.Url, ContactDetail.Ringtone].indexOf(detailType) > -1) {
535 // let those editors handle the Field Selected
536 return
537 }
538
539 if (qmlTypeName) {
540 var newDetail = Qt.createQmlObject("import QtContacts 5.0; " + qmlTypeName + "{}", contactEditor)
541 if (newDetail) {
542 newFieldAction = true
543 var newDetailsCopy = contactEditor.newDetails
544 newDetailsCopy.push(newDetail)
545 contactEditor.newDetails = newDetailsCopy
546 contactEditor.contact.addDetail(newDetail)
547 }
548 }
549 }
550 }
551
552
553 Item {
554 anchors {
555 left: parent.left
556 right: parent.right
557 }
558 height: units.gu(2)
559 }
560
561 Button {
562 id: deleteButton
563
564 text: i18n.dtr("lomiri-addressbook-app", "Delete")
565 visible: !contactEditor.isNewContact
566 color: theme.palette.normal.negative
567 anchors {
568 left: parent.left
569 right: parent.right
570 margins: units.gu(2)
571 }
572 action: Action {
573 enabled: contactEditor.active && deleteButton.visible
574 shortcut: "Ctrl+Delete"
575 onTriggered: {
576 var dialog = PopupUtils.open(Qt.resolvedUrl("RemoveContactsDialog.qml"), null,
577 {"contactEditor": contactEditor})
578 dialog.contacts = [contactEditor.contact]
579 }
580 }
581 }
582
583
584 Item {
585 anchors {
586 left: parent.left
587 right: parent.right
588 }
589 height: units.gu(2)
590 }
591 }
592 }
593
594 KeyboardRectangle {
595 id: keyboardRectangle
596
597 onHeightChanged: {
598 if (addNewFieldButton.expanded) {
599 scrollArea.contentY = scrollArea.contentHeight - scrollArea.height
600 scrollArea.returnToBounds()
601 } else if (activeItem) {
602 idleMakeMeVisible(activeItem)
603 }
604 }
605 }
606
607 Loader {
608 id: overlayEditorPage
609 anchors.fill: parent
610 z:2
611 active: false
612 }
613
614 onActiveChanged: {
615 if (!active || !enabled) {
616 return
617 }
618
619 focusTimer.restart()
620 }
621 }