~renatofilho/address-book-app/address-book-app-wily

« back to all changes in this revision

Viewing changes to src/imports/Ubuntu/AddressBook/ContactEditor/ContactDetailSyncTargetEditor.qml

  • Committer: CI Train Bot
  • Author(s): Renato Araujo Oliveira Filho
  • Date: 2015-12-11 16:50:23 UTC
  • mfrom: (492.2.10 choose-default-address-book)
  • Revision ID: ci-train-bot@canonical.com-20151211165023-r96z3r4qh9ghopee
Implemented the ability to choose the default address-book from settings page. Fixes: #1511471
Approved by: Gustavo Pichorim Boiko

Show diffs side-by-side

added added

removed removed

Lines of Context:
27
27
    id: root
28
28
 
29
29
    property alias active: sourceModel.autoUpdate
 
30
    property bool isNewContact: contact && contact.contactId === "qtcontacts:::"
 
31
    property real myHeight: label.height + units.gu(6) + (sources.currentlyExpanded ? sources.containerHeight :
 
32
                                                                                      sources.itemHeight)
 
33
 
30
34
    signal changed()
31
35
 
32
36
    function save() {
49
53
        if (sources.model.count <= 0)
50
54
            return -1
51
55
 
52
 
        var selectedContact = sources.model.get(sources.selectedIndex).contact
53
 
        if (selectedContact) {
54
 
            return selectedContact.guid.guid
 
56
        var selectedSourceId = sources.model.get(sources.selectedIndex).sourceId
 
57
        if (selectedSourceId) {
 
58
            return selectedSourceId
55
59
        } else {
56
60
            return -1
57
61
        }
69
73
        return true
70
74
    }
71
75
 
72
 
    function targetIsReadOnly(target) {
73
 
        if (!target)
74
 
            return true
75
 
 
76
 
        var details = target.details(ContactDetail.ExtendedDetail)
77
 
        for(var d in details) {
78
 
            if ((details[d].name === "READ-ONLY") && (details[d].data === true)) {
79
 
                return true
80
 
            }
81
 
        }
82
 
 
83
 
        return false
84
 
    }
85
 
 
86
 
    property bool isNewContact: contact && contact.contactId === "qtcontacts:::"
87
 
    property real myHeight: sources.currentlyExpanded ? sources.containerHeight + units.gu(6) + label.height : sources.itemHeight + units.gu(6) + label.height
88
 
 
89
76
    detail: root.contact ? contact.detail(ContactDetail.SyncTarget) : null
90
77
    implicitHeight: root.isNewContact &&  sources.model && (sources.model.count > 1) ? myHeight : 0
91
78
 
111
98
    ListModel {
112
99
        id: writableSources
113
100
 
 
101
        function getSourceMetaData(contact) {
 
102
            var metaData = {'read-only' : false,
 
103
                            'account-provider': '',
 
104
                            'account-id': 0,
 
105
                            'is-primary': false}
 
106
 
 
107
            var details = contact.details(ContactDetail.ExtendedDetail)
 
108
            for(var d in details) {
 
109
                if (details[d].name === "READ-ONLY") {
 
110
                    metaData['read-only'] = details[d].data
 
111
                } else if (details[d].name === "PROVIDER") {
 
112
                    metaData['account-provider'] = details[d].data
 
113
                } else if (details[d].name === "APPLICATION-ID") {
 
114
                    metaData['account-id'] = details[d].data
 
115
                } else if (details[d].name === "IS-PRIMARY") {
 
116
                    metaData['is-primary'] = details[d].data
 
117
                }
 
118
            }
 
119
            return metaData
 
120
        }
 
121
 
114
122
        function reload() {
115
123
            clear()
116
124
 
120
128
                return
121
129
            }
122
130
 
 
131
            var data = []
123
132
            for(var i in contacts) {
124
 
                if (!targetIsReadOnly(contacts[i])) {
125
 
                    append({'contact': contacts[i]})
126
 
                }
127
 
            }
 
133
                var sourceMetaData = getSourceMetaData(contacts[i])
 
134
                if (!sourceMetaData['readOnly']) {
 
135
                    data.push({'sourceId': contacts[i].guid.guid,
 
136
                               'sourceName': contacts[i].displayLabel.label,
 
137
                               'accountId': sourceMetaData['account-id'],
 
138
                               'accountProvider': sourceMetaData['account-provider'],
 
139
                               'readOnly': sourceMetaData['read-only'],
 
140
                               'isPrimary': sourceMetaData['is-primary']
 
141
                                })
 
142
                }
 
143
            }
 
144
 
 
145
            data.sort(function(a, b) {
 
146
                var valA = a.accountId
 
147
                var valB = b.accountId
 
148
                if (a.accountId == b.accountId) {
 
149
                    valA = a.sourceName
 
150
                    valB = b.sourceName
 
151
                }
 
152
 
 
153
                if (valA == valB) {
 
154
                    return 0
 
155
                } else if (valA < valB) {
 
156
                    return -1
 
157
                } else {
 
158
                    return 1
 
159
                }
 
160
            })
 
161
 
 
162
            var primaryIndex = 0
 
163
            for (var i in data) {
 
164
                if (data[i].isPrimary) {
 
165
                    primaryIndex = i
 
166
                }
 
167
                append(data[i])
 
168
            }
 
169
 
 
170
            // select primary account
 
171
            sources.selectedIndex = primaryIndex
128
172
        }
129
173
    }
130
174
 
164
208
 
165
209
        delegate: OptionSelectorDelegate {
166
210
            text: {
167
 
                if ((contact.guid.guid != "system-address-book") &&
 
211
                if ((sourceId != "system-address-book") &&
168
212
                    (iconSource == "image://theme/address-book-app-symbolic")) {
169
 
                    return i18n.dtr("address-book-app", "Personal - %1").arg(contact.displayLabel.label)
 
213
                    //TRANSLATORS: %1 is the display name of the source (address-book)
 
214
                    return i18n.dtr("address-book-app", "Personal - %1").arg(sourceName)
170
215
                } else {
171
 
                    return contact.displayLabel.label
 
216
                    return sourceName
172
217
                }
173
218
            }
174
219
            constrainImage: true
175
 
            iconSource: {
176
 
                var details = contact.details(ContactDetail.ExtendedDetail)
177
 
                for(var i in details) {
178
 
                    if (details[i].name === "PROVIDER") {
179
 
                        if (details[i].data === "") {
180
 
                            return "image://theme/address-book-app-symbolic"
181
 
                        } else {
182
 
                            return "image://theme/online-accounts-%1".arg(details[i].data)
183
 
                        }
184
 
                    }
185
 
                }
186
 
                return "image://theme/address-book-app-symbolic"
187
 
            }
 
220
            iconSource: accountProvider == "" ?
 
221
                            "image://theme/address-book-app-symbolic" :
 
222
                            "image://theme/online-accounts-%1".arg(accountProvider)
188
223
            height: units.gu(4)
189
224
        }
190
225
 
196
231
            sourceModel.update()
197
232
        }
198
233
    }
 
234
 
 
235
    // In case of sources changed we need to update the model
 
236
    Connections {
 
237
        target: application
 
238
        onSourcesChanged: sourceModel.update()
 
239
    }
199
240
}
200
241