Warning, /plasma-mobile/plasma-phonebook/src/contents/ui/AddContactPage.qml is written in an unsupported language. File is not indexed.

0001 /*
0002  *   SPDX-FileCopyrightText: 2019 Nicolas Fella <nicolas.fella@gmx.de>
0003  *
0004  *   SPDX-License-Identifier: LGPL-2.0-or-later
0005  */
0006 
0007 import QtQuick
0008 import QtQuick.Controls as Controls
0009 import QtQuick.Layouts
0010 import QtQuick.Templates as T
0011 
0012 import Qt.labs.platform
0013 import Qt5Compat.GraphicalEffects
0014 
0015 import org.kde.people as KPeople
0016 import org.kde.kirigami as Kirigami
0017 import org.kde.kirigamiaddons.formcard as FormCard
0018 
0019 import org.kde.phonebook 1.0
0020 
0021 FormCard.FormCardPage {
0022     id: root
0023 
0024     property QtObject person
0025     property var addressee: ContactController.emptyAddressee()
0026 
0027     property var pendingPhoneNumbers: addressee.phoneNumbers
0028     property var pendingEmails: addressee.emails
0029     property var pendingImpps: addressee.impps
0030     property var pendingPhoto: addressee.photo
0031 
0032     signal save()
0033 
0034     states: [
0035         State {
0036             name: "create"
0037             PropertyChanges { target: root; title: i18n("Adding contact") }
0038         },
0039         State {
0040             name: "update"
0041             PropertyChanges { target: root; title: i18n("Editing contact") }
0042         }
0043     ]
0044 
0045     enabled: !person || person.isEditable
0046 
0047     Controls.RoundButton {
0048         Layout.alignment: Qt.AlignHCenter
0049         Layout.topMargin: Kirigami.Units.gridUnit
0050 
0051         Kirigami.FormData.label: i18n("Photo")
0052 
0053         // Square button
0054         implicitWidth: Kirigami.Units.gridUnit * 5
0055         implicitHeight: implicitWidth
0056 
0057         contentItem: Item {
0058             id: icon
0059 
0060             Kirigami.Icon {
0061                 id:image
0062                 source: {
0063                     if (root.pendingPhoto.isEmpty) {
0064                         return "edit-image-face-add"
0065                     } else if (root.pendingPhoto.isIntern) {
0066                         return root.pendingPhoto.data
0067                     } else {
0068                         return root.pendingPhoto.url
0069                     }
0070                 }
0071                 anchors.fill: parent
0072                 layer.enabled: true
0073                 layer.effect: OpacityMask {
0074                     maskSource: mask
0075                 }
0076 
0077                 Connections {
0078                     target: root
0079                     function onSave() {
0080                         addressee.photo = root.pendingPhoto
0081                     }
0082                 }
0083             }
0084 
0085             Rectangle {
0086                 id: mask
0087                 anchors.fill: parent
0088                 visible: false
0089                 radius: parent.height/2
0090             }
0091         }
0092 
0093         onClicked: fileDialog.open()
0094 
0095         FileDialog {
0096             id: fileDialog
0097 
0098             onAccepted: {
0099                 root.pendingPhoto = ContactController.preparePhoto(currentFile)
0100             }
0101         }
0102     }
0103 
0104     Controls.Label{
0105         visible: root.pendingPhoto.isEmpty
0106         color: Kirigami.Theme.disabledTextColor
0107         text: i18n("Add profile picture")
0108         Layout.alignment: Qt.AlignHCenter
0109     }
0110 
0111     FormCard.FormCard {
0112         Layout.topMargin: Kirigami.Units.largeSpacing
0113         Layout.fillWidth: true
0114 
0115         FormCard.FormTextFieldDelegate{
0116             id: name
0117             label: i18n("Name")
0118 
0119             text: addressee.formattedName
0120             onAccepted: {
0121                 addressee.formattedName = text
0122             }
0123             Connections {
0124                 target: root
0125                 function onSave() {
0126                     name.accepted()
0127                 }
0128             }
0129         }
0130     }
0131 
0132     FormCard.FormCard {
0133         Layout.topMargin: Kirigami.Units.largeSpacing
0134 
0135         data: Connections {
0136             target: root;
0137             function onSave() {
0138                 if (toAddPhone.text !== "") {
0139                     var numbers = pendingPhoneNumbers
0140                     numbers.push(ContactController.createPhoneNumber(toAddPhone.text))
0141                     pendingPhoneNumbers = numbers
0142                 }
0143 
0144                 addressee.phoneNumbers = root.pendingPhoneNumbers
0145             }
0146         }
0147 
0148         Repeater{
0149             model: pendingPhoneNumbers
0150             delegate:FormCard.FormTextFieldDelegate{
0151                 id: phoneField
0152 
0153                 required property var modelData
0154                 required property int index
0155 
0156                 label: i18n("Phone")
0157                 text: modelData.number
0158                 inputMethodHints: Qt.ImhDialableCharactersOnly
0159                 onAccepted: {
0160                     root.pendingPhoneNumbers[index].number = text
0161                 }
0162                 onTextChanged: if(text == "") {
0163                     var newList = root.pendingPhoneNumbers.filter((value, index) => index != phoneField.index)
0164                     root.pendingPhoneNumbers = newList
0165                 }
0166 
0167                 Connections {
0168                     target: root
0169                     function onSave() {
0170                         phoneField.accepted()
0171                         addressee.phoneNumbers = root.pendingPhoneNumbers
0172                     }
0173                 }
0174             }
0175         }
0176         FormCard.FormTextFieldDelegate{
0177             id: toAddPhone
0178             label: i18n("Phone")
0179 
0180             placeholderText: i18n("+1 555 2368")
0181             inputMethodHints: Qt.ImhDialableCharactersOnly
0182             onAccepted: {
0183                 addressee.formattedName = text
0184             }
0185             Connections {
0186                 target: root;
0187                 function onSave() {
0188                     if (toAddPhone.text !== "") {
0189                         var numbers = pendingPhoneNumbers
0190                         numbers.push(ContactController.createPhoneNumber(toAddPhone.text))
0191                         pendingPhoneNumbers = numbers
0192                     }
0193 
0194                     addressee.phoneNumbers = root.pendingPhoneNumbers
0195                 }
0196             }
0197         }
0198         FormCard.FormDelegateSeparator { above: addPhone}
0199         FormCard.FormButtonDelegate {
0200             id: addPhone
0201             enabled: toAddPhone.text.length > 0
0202             text: i18n("Add phone number")
0203             leading: Kirigami.Icon{
0204                 source: "list-add"
0205                 implicitHeight: Kirigami.Units.gridUnit
0206             }
0207             onClicked: {
0208                 var numbers = pendingPhoneNumbers
0209                 numbers.push(ContactController.createPhoneNumber(toAddPhone.text))
0210                 pendingPhoneNumbers = numbers
0211                 toAddPhone.text = ""
0212             }
0213         }
0214     }
0215 
0216     FormCard.FormCard {
0217         Layout.topMargin: Kirigami.Units.largeSpacing
0218 
0219         data: Connections {
0220             target: root;
0221             function onSave() {
0222                 if (toAddEmail.text !== "") {
0223                     var emails = root.pendingEmails
0224                     emails.push(ContactController.createEmail(toAddEmail.text))
0225                     root.pendingEmails = emails
0226                 }
0227 
0228                 addressee.emails = root.pendingEmails
0229             }
0230         }
0231 
0232         Repeater {
0233             model: root.pendingEmails
0234             delegate:FormCard.FormTextFieldDelegate{
0235                 id: emailField
0236 
0237                 required property var modelData
0238                 required property int index
0239 
0240 
0241                 label: i18n("Email")
0242                 text: modelData.email
0243                 inputMethodHints: Qt.ImhEmailCharactersOnly
0244 
0245                 onAccepted: {
0246                     root.pendingEmails[index].email = text
0247                 }
0248                 onTextChanged: if(text == "") {
0249                     root.pendingEmails = root.pendingEmails.filter((value, index) => index != emailField.index)
0250                 }
0251 
0252                 Connections {
0253                     target: root
0254                     function onSave() {
0255                         textField.accepted()
0256                         addressee.emails = root.pendingEmails
0257                     }
0258                 }
0259             }
0260         }
0261         FormCard.FormTextFieldDelegate{
0262             id: toAddEmail
0263             label: i18n("Email")
0264 
0265             placeholderText: i18n("user@example.org")
0266             inputMethodHints: Qt.ImhEmailCharactersOnly
0267             onAccepted: {
0268                 addressee.formattedName = text
0269             }
0270 
0271         }
0272         FormCard.FormDelegateSeparator { above: addEmail}
0273         FormCard.FormButtonDelegate {
0274             id: addEmail
0275             enabled: toAddEmail.text.length > 0
0276             text: i18n("Add email address")
0277             leading: Kirigami.Icon{
0278                 source: "list-add"
0279                 implicitHeight: Kirigami.Units.gridUnit
0280             }
0281             onClicked: {
0282                 var emails = root.pendingEmails
0283                 emails.push(ContactController.createEmail(toAddEmail.text))
0284                 root.pendingEmails = emails
0285                 toAddEmail.text = ""
0286             }
0287         }
0288     }
0289 
0290     FormCard.FormCard {
0291         Layout.topMargin: Kirigami.Units.largeSpacing
0292 
0293         data: Connections {
0294             target: root;
0295             function onSave() {
0296                 if (toAddImpp.text !== "") {
0297                     var impps = root.pendingImpps
0298                     impps.push(ContactController.createImpp(toAddImpp.text))
0299                     root.pendingImpps = impps
0300                 }
0301 
0302                 addressee.impps = root.pendingImpps
0303             }
0304         }
0305 
0306         Repeater{
0307             model: root.pendingImpps
0308             delegate:FormCard.FormTextFieldDelegate{
0309                 id: imppField
0310 
0311                 required property var modelData
0312                 required property int index
0313 
0314                 label: i18n("Instant Messenger")
0315                 text: modelData.address
0316                 inputMethodHints: Qt.ImhEmailCharactersOnly
0317 
0318                 onAccepted: {
0319                     root.pendingImpps[index].address = text
0320                 }
0321                 onTextChanged: if(text == "") {
0322                     root.pendingImpps = root.pendingImpps.filter((value, index) => index != imppField.index)
0323                 }
0324                 Connections {
0325                     target: root
0326                     onSave: {
0327                         imppField.accepted()
0328                         addressee.impps = root.pendingImpps
0329                     }
0330                 }
0331             }
0332         }
0333         FormCard.FormTextFieldDelegate {
0334             id: toAddImpp
0335             label: i18n("Instant Messenger")
0336 
0337             placeholderText: i18n("protocol:person@example.com")
0338             inputMethodHints: Qt.ImhEmailCharactersOnly
0339 
0340             Connections {
0341                 target: root
0342                 function onSave() {
0343                     if (toAddPhone.text !== "") {
0344                         var numbers = pendingPhoneNumbers
0345                         numbers.push(ContactController.createPhoneNumber(toAddPhone.text))
0346                         pendingPhoneNumbers = numbers
0347                     }
0348 
0349                     addressee.phoneNumbers = root.pendingPhoneNumbers
0350                 }
0351             }
0352         }
0353         FormCard.FormDelegateSeparator { above: addImpp}
0354         FormCard.FormButtonDelegate {
0355             id: addImpp
0356             text: i18n("Add instant messenger address")
0357             enabled: toAddImpp.text.length > 0
0358             leading: Kirigami.Icon{
0359                 source: "list-add"
0360                 implicitHeight: Kirigami.Units.gridUnit
0361             }
0362             onClicked: {
0363                 var impps = root.pendingImpps
0364                 impps.push(ContactController.createImpp(toAddImpp.text))
0365                 pendingImpps = impps
0366                 toAddImpp.text = ""
0367             }
0368         }
0369     }
0370 
0371     FormCard.FormCard {
0372         Layout.topMargin: Kirigami.Units.largeSpacing
0373 
0374         FormCard.FormDateTimeDelegate {
0375             text: i18n("Birthday")
0376             value: addressee.birthday
0377             dateTimeDisplay: FormCard.FormDateTimeDelegate.DateTimeDisplay.Date
0378 
0379             Connections {
0380                 target: root
0381                 function onSave() {
0382                     addressee.birthday = birthday.value // TODO birthday is not writable
0383                 }
0384             }
0385         }
0386     }
0387 
0388     FormCard.FormCard {
0389         Layout.topMargin: Kirigami.Units.largeSpacing
0390 
0391         FormCard.AbstractFormDelegate {
0392             contentItem: ColumnLayout {
0393                 Controls.Label {
0394                     text: i18n("Note:")
0395                     Layout.fillWidth: true
0396                 }
0397 
0398                 Controls.TextArea {
0399                     id: note
0400                     text: addressee.note
0401                     Layout.fillWidth: true
0402 
0403                     Connections {
0404                         target: root
0405                         function onSave() {
0406                             addressee.note = note.text
0407                         }
0408                     }
0409                 }
0410             }
0411         }
0412     }
0413 
0414     footer: T.Control {
0415         id: footerToolBar
0416 
0417         implicitWidth: Math.max(implicitBackgroundWidth + leftInset + rightInset,
0418                                 implicitContentWidth + leftPadding + rightPadding)
0419         implicitHeight: Math.max(implicitBackgroundHeight + topInset + bottomInset,
0420                                 implicitContentHeight + topPadding + bottomPadding)
0421 
0422         leftPadding: Kirigami.Units.smallSpacing
0423         rightPadding: Kirigami.Units.smallSpacing
0424         bottomPadding: Kirigami.Units.smallSpacing
0425         topPadding: Kirigami.Units.smallSpacing + footerSeparator.implicitHeight
0426 
0427         contentItem: RowLayout {
0428             spacing: parent.spacing
0429 
0430             // footer buttons
0431             Controls.DialogButtonBox {
0432                 // we don't explicitly set padding, to let the style choose the padding
0433                 id: dialogButtonBox
0434                 standardButtons: Controls.DialogButtonBox.Close | Controls.DialogButtonBox.Save
0435 
0436                 Layout.fillWidth: true
0437                 Layout.alignment: dialogButtonBox.alignment
0438 
0439                 position: Controls.DialogButtonBox.Footer
0440 
0441                 onAccepted: {
0442                     root.save();
0443                     switch(root.state) {
0444                         case "create":
0445                             if (!KPeople.PersonPluginManager.addContact({ "vcard": ContactController.addresseeToVCard(addressee) }))
0446                                 console.warn("could not create contact")
0447                             break;
0448                         case "update":
0449                             if (!root.person.setContactCustomProperty("vcard", ContactController.addresseeToVCard(addressee)))
0450                                 console.warn("Could not save", addressee.url)
0451                             break;
0452                     }
0453                     root.closeDialog()
0454                 }
0455                 onRejected: root.closeDialog()
0456             }
0457         }
0458 
0459         background: Item {
0460             // separator above footer
0461             Kirigami.Separator {
0462                 id: footerSeparator
0463                 visible: root.contentItem.height < root.contentItem.flickableItem.contentHeight
0464                 width: parent.width
0465                 anchors.top: parent.top
0466             }
0467         }
0468     }
0469 }