Debian Codegraph @

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

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