~ken-vandine/address-book-app/add_profile

« back to all changes in this revision

Viewing changes to src/imports/ContactEditor.qml

  • Committer: Renato Araujo Oliveira Filho
  • Date: 2013-06-27 20:05:52 UTC
  • Revision ID: renato.filho@canonical.com-20130627200552-b115j5fcqfnpy7ez
First commit of Address Book Application

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
**
 
3
** Copyright (C) 2013 Canonical Ltd
 
4
**
 
5
****************************************************************************/
 
6
 
 
7
import QtQuick 2.0
 
8
import QtContacts 5.0
 
9
import Ubuntu.Components 0.1
 
10
import Ubuntu.Components.ListItems 0.1 as ListItem
 
11
 
 
12
Page {
 
13
    id: contactEditor
 
14
 
 
15
    property variant contact: null
 
16
    property variant model: null
 
17
 
 
18
    Column {
 
19
        anchors.fill: parent
 
20
 
 
21
        TextArea {
 
22
            id: contactFirstName
 
23
 
 
24
            width: parent.width
 
25
            height: units.gu(4)
 
26
            maximumLineCount: 0
 
27
            placeholderText: "First Name"
 
28
            Component.onCompleted: text = contact ? contact.name.firstName : ""
 
29
        }
 
30
        ListItem.ThinDivider {}
 
31
 
 
32
       TextArea {
 
33
            id: contactMiddleName
 
34
 
 
35
            width: parent.width
 
36
            height: units.gu(4)
 
37
            maximumLineCount: 0
 
38
            placeholderText: "Middle Name"
 
39
            Component.onCompleted: text = contact ? contact.name.middleName : ""
 
40
        }
 
41
        ListItem.ThinDivider {}
 
42
 
 
43
        TextArea {
 
44
            id: contactLastName
 
45
 
 
46
            width: parent.width
 
47
            height: units.gu(4)
 
48
            maximumLineCount: 0
 
49
            placeholderText: "Last Name"
 
50
            Component.onCompleted: text = contact ? contact.name.lastName : ""
 
51
        }
 
52
        ListItem.ThinDivider {}
 
53
 
 
54
        TextArea {
 
55
            id: contactPhone
 
56
 
 
57
            width: parent.width
 
58
            height: units.gu(4)
 
59
            maximumLineCount: 0
 
60
            placeholderText: "Phone Number"
 
61
            Component.onCompleted: text = contact ? contact.phoneNumber.number : ""
 
62
        }
 
63
        ListItem.ThinDivider {}
 
64
 
 
65
        TextArea {
 
66
            id: contactEmail
 
67
 
 
68
            width: parent.width
 
69
            height: units.gu(4)
 
70
            maximumLineCount: 0
 
71
            placeholderText: "e-mail"
 
72
            Component.onCompleted: text = contact ? contact.email.emailAddress : ""
 
73
        }
 
74
        ListItem.ThinDivider {}
 
75
    }
 
76
 
 
77
    function setContactDetails(contact) {
 
78
        contact.name.firstName = contactFirstName.text
 
79
        contact.name.middleName = contactMiddleName.text
 
80
        contact.name.lastName = contactLastName.text
 
81
        contact.email.emailAddress = contactEmail.text
 
82
        contact.phoneNumber.number = contactPhone.text
 
83
    }
 
84
 
 
85
    function updateContact() {
 
86
        if (!contact) { // create new contact
 
87
            var newContact = Qt.createQmlObject("import QtContacts 5.0; Contact{ }", contactEditor)
 
88
            setContactDetails(newContact)
 
89
            newContact.save()
 
90
            contactEditor.model.saveContact(newContact)
 
91
 
 
92
        } else if ((contactFirstName.text !== contactEditor.contact.name.firstName) ||
 
93
                   (contactMiddleName.text !== contactEditor.contact.name.middleName) ||
 
94
                   (contactLastName.text !== contactEditor.contact.name.lastName) ||
 
95
                   (contactEmail.text !== contactEditor.contact.email.emailAddress) ||
 
96
                   (contactPhone.text !== contactEditor.contact.phoneNumber.number)) {
 
97
            // update existing contact
 
98
            setContactDetails(contactEditor.contact)
 
99
            contact.save()
 
100
        }
 
101
    }
 
102
 
 
103
    tools: ToolbarActions {
 
104
        Action {
 
105
            text: i18n.tr("Save")
 
106
            iconSource: "artwork:/edit.png"
 
107
            onTriggered: {
 
108
                updateContact()
 
109
                pageStack.pop()
 
110
            }
 
111
        }
 
112
    }
 
113
}