~om26er/address-book-app/abook_navigation_favorites

« back to all changes in this revision

Viewing changes to src/imports/ContactList.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
import QtQuick 2.0
 
7
import QtContacts 5.0
 
8
import Ubuntu.Components 0.1
 
9
import Ubuntu.Components.ListItems 0.1 as ListItem
 
10
 
 
11
import "ContactList.js" as Sections
 
12
 
 
13
Page {
 
14
    id: mainPage
 
15
 
 
16
    property var startTime
 
17
 
 
18
    Component.onCompleted: mainPage.startTime = new Date()
 
19
 
 
20
    ContactModel {
 
21
        id: contactsModel
 
22
 
 
23
        property string titleField : "First name"
 
24
        property string subTitleField: "Phone"
 
25
        property alias sortOrderField: sortOrder.field
 
26
 
 
27
        manager: "galera"
 
28
        sortOrders: [
 
29
            SortOrder {
 
30
                id: sortOrder
 
31
 
 
32
                detail: ContactDetail.Name
 
33
                field: Name.FirstName
 
34
                direction: Qt.AscendingOrder
 
35
            }
 
36
        ]
 
37
 
 
38
        fetchHint: FetchHint {
 
39
            detailTypesHint: [ContactDetail.Avatar,
 
40
                              ContactDetail.Name,
 
41
                              ContactDetail.PhoneNumber]
 
42
        }
 
43
 
 
44
        Component.onCompleted: {
 
45
            if (manager == "memory")
 
46
                contactsModel.importContacts(Qt.resolvedUrl("example.vcf"))
 
47
        }
 
48
 
 
49
        function getContactDetails(contact, fieldNames) {
 
50
            var fullValue = ""
 
51
 
 
52
            if (contact) {
 
53
                var fieldNameList = fieldNames.split(",")
 
54
                for (var fieldNameIndex in fieldNameList) {
 
55
                    var fieldName = fieldNameList[fieldNameIndex]
 
56
                    var value = "";
 
57
                    if (fieldName === "First name") {
 
58
                        value = contact.name.firstName
 
59
                    } else if (fieldName === "Middle name") {
 
60
                        value = contact.name.middleName
 
61
                    } else if (fieldName === "Last name") {
 
62
                        value = contact.name.lastName
 
63
                    } else if (fieldName === "Full name") {
 
64
                        value = contact.displayLabel.label
 
65
                    } else if (fieldName === "Nickname") {
 
66
                        value = contact.nickname.nickname
 
67
                    } else if (fieldName === "Phone") {
 
68
                        value = contact.phoneNumber.number
 
69
                    } else if (fieldName === "e-mail") {
 
70
                        value = contact.email.emailAddress
 
71
                    } else {
 
72
                        value = "null"
 
73
                    }
 
74
                    if (fullValue.length != 0)
 
75
                        fullValue += ", "
 
76
                    fullValue += value
 
77
                }
 
78
            }
 
79
            return fullValue
 
80
        }
 
81
    }
 
82
 
 
83
    ListView {
 
84
        id: alphabetView
 
85
 
 
86
        property string selectedLetter: contactListView.contentY > 0  ? contactListView.itemAt(0, contactListView.contentY).sectionName : "A"
 
87
 
 
88
        anchors {
 
89
            top: parent.top
 
90
            left: parent.left
 
91
            right: parent.right
 
92
        }
 
93
        focus: true
 
94
        height: units.gu(4)
 
95
        orientation: ListView.Horizontal
 
96
 
 
97
        model: ["A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z" ]
 
98
        delegate: Label {
 
99
            text: modelData
 
100
            font.bold: alphabetView.selectedLetter == text
 
101
            horizontalAlignment: Text.AlignHCenter
 
102
            fontSize: "large"
 
103
            width: units.gu(3)
 
104
 
 
105
            MouseArea {
 
106
                anchors.fill: parent
 
107
                onClicked: {
 
108
                    contactListView.scroolToSection(modelData)
 
109
                }
 
110
            }
 
111
 
 
112
        }
 
113
    }
 
114
 
 
115
    ListView {
 
116
        id: contactListView
 
117
 
 
118
        property string title
 
119
 
 
120
        clip: true
 
121
        snapMode: ListView.NoSnap
 
122
        section {
 
123
            property: "contact.name.firstName"
 
124
            criteria: ViewSection.FirstCharacter
 
125
            delegate: ListItem.Header {
 
126
                id: listHeader
 
127
                //text: section
 
128
                visible: false
 
129
                height: 0
 
130
            }
 
131
        }
 
132
 
 
133
        anchors {
 
134
            top: alphabetView.bottom
 
135
            left: parent.left
 
136
            right: parent.right
 
137
            bottom: parent.bottom
 
138
        }
 
139
        model: contactsModel
 
140
        header: ListItem.Header {
 
141
            text: contactListView.title
 
142
        }
 
143
 
 
144
        onCountChanged: {
 
145
            dirtyTimer.restart()
 
146
            if (mainPage.startTime) {
 
147
                var currentTime = new Date();
 
148
                var elapsed = currentTime.getTime() - mainPage.startTime.getTime()
 
149
                contactListView.title = "Elapsed time to load " + count + " contacts: " + (elapsed/1000) + " secs"
 
150
            }
 
151
        }
 
152
 
 
153
        delegate: ListItem.Subtitled {
 
154
            property variant contactObject: contact
 
155
            property string contactId: contact.contactId
 
156
            property string sectionName: ListView.section
 
157
 
 
158
            icon: contact && contact.avatar && (contact.avatar.imageUrl != "") ?  Qt.resolvedUrl(contact.avatar.imageUrl) : "artwork:/avatar.png"
 
159
            text: contactsModel.titleField ? contactsModel.getContactDetails(contact, contactsModel.titleField) : ""
 
160
            subText: contactsModel.subTitleField ? contactsModel.getContactDetails(contact, contactsModel.subTitleField) : ""
 
161
            selected: contactListView.currentIndex === index
 
162
 
 
163
            MouseArea {
 
164
                anchors.fill:  parent
 
165
                onClicked: {
 
166
                    contactListView.currentIndex = index
 
167
                }
 
168
                onDoubleClicked: {
 
169
                    editContactPriv.contactId = contactListView.currentItem.contactObject.contactId
 
170
                }
 
171
            }
 
172
        }
 
173
 
 
174
        UbuntuNumberAnimation { id: scroolToSectionAnimation; target: contactListView; property: "contentY"; }
 
175
        function scroolToSection(targetSection) {
 
176
            scroolToSectionAnimation.from = contactListView.contentY
 
177
            contactListView.positionViewAtIndex(Sections.getIndexFor(targetSection), ListView.Beginning)
 
178
            scroolToSectionAnimation.to = contactListView.contentY
 
179
            scroolToSectionAnimation.running = true
 
180
        }
 
181
 
 
182
        // function used to build the section cache by "ContactList.js"
 
183
        function sectionValueForContact(contact) {
 
184
            if (contact) {
 
185
                return contact.name && contact.name.firstName ? contact.name.firstName[0] : ""
 
186
            } else {
 
187
                return null
 
188
            }
 
189
        }
 
190
    }
 
191
 
 
192
    Timer {
 
193
        id: dirtyTimer
 
194
 
 
195
        interval: 2000
 
196
        running: false
 
197
        repeat: false
 
198
        onTriggered: {
 
199
            Sections.initSectionData(contactListView)
 
200
        }
 
201
    }
 
202
 
 
203
    ActivityIndicator {
 
204
        id: busyIndicator
 
205
 
 
206
        property bool pageIsBusy: false
 
207
 
 
208
        running: contactListView.count == 0 || pageIsBusy
 
209
        visible: running
 
210
        anchors.centerIn: contactListView
 
211
    }
 
212
 
 
213
    tools: ToolbarActions {
 
214
        Action {
 
215
            text: i18n.tr("Settings")
 
216
            iconSource: "artwork:/settings.png"
 
217
            onTriggered: pageStack.push(Qt.resolvedUrl("ContactSettings.qml"), {model: contactsModel})
 
218
        }
 
219
 
 
220
        Action {
 
221
            text: i18n.tr("Edit")
 
222
            iconSource: "artwork:/edit.png"
 
223
            onTriggered: {
 
224
                editContactPriv.contactId = contactListView.currentItem.contactObject.contactId
 
225
            }
 
226
        }
 
227
        Action {
 
228
            text: i18n.tr("New")
 
229
            iconSource: "artwork:/add.png"
 
230
            onTriggered: pageStack.push(Qt.resolvedUrl("ContactEditor.qml"), {model: contactsModel})
 
231
        }
 
232
        Action {
 
233
            text: i18n.tr("Delete")
 
234
            iconSource: "artwork:/delete.png"
 
235
            onTriggered: {
 
236
                contactsModel.removeContact(contactListView.currentItem.contactId);
 
237
            }
 
238
        }
 
239
    }
 
240
 
 
241
    Item {
 
242
        id: editContactPriv
 
243
 
 
244
        property string contactId
 
245
        property int currentQueryId: -1
 
246
 
 
247
        visible: false
 
248
        Connections {
 
249
            target: contactsModel
 
250
            onContactsFetched: {
 
251
                if (requestId == editContactPriv.currentQueryId) {
 
252
                    busyIndicator.pageIsBusy = false
 
253
                    pageStack.push(Qt.resolvedUrl("ContactEditor.qml"),
 
254
                                   {model: contactsModel, contact: fetchedContacts[0]})
 
255
                }
 
256
            }
 
257
        }
 
258
 
 
259
        onContactIdChanged: {
 
260
            if (!contactId || (currentQueryId != -1)) {
 
261
                return
 
262
            }
 
263
 
 
264
            busyIndicator.pageIsBusy = true
 
265
 
 
266
            currentQueryId = contactsModel.fetchContacts([contactId])
 
267
            if (currentQueryId == -1) {
 
268
                busyIndicator.pageIsBusy = false
 
269
            }
 
270
        }
 
271
 
 
272
 
 
273
    }
 
274
 
 
275
 
 
276
}