Debian Codegraph @

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

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