~phablet-team/telephony-service/qml-start-chat

« back to all changes in this revision

Viewing changes to accounts/common/NewAccountInterface.qml

merge parent

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import QtQuick 2.0
 
2
import Ubuntu.Components 1.3
 
3
import Ubuntu.OnlineAccounts 0.1
 
4
 
 
5
Item {
 
6
    id: root
 
7
 
 
8
    readonly property string keyPrefix: "telepathy/"
 
9
    readonly property var accountObjectHandle: account ? account.objectHandle : undefined
 
10
    readonly property alias isValid: paramsRepeater.fieldHasValues
 
11
 
 
12
    property string manager
 
13
    property string protocol
 
14
    property string icon
 
15
    property var params
 
16
    property var advancedParams
 
17
    property bool hasCrendentials: true
 
18
 
 
19
 
 
20
    signal finished
 
21
    height: fields.childrenRect.height +
 
22
            units.gu(10)
 
23
 
 
24
    function getAccountService() {
 
25
        var service = serviceModel.get(0, "accountServiceHandle")
 
26
        if (!service) {
 
27
            console.warn("No service handle from model")
 
28
            return null
 
29
        }
 
30
 
 
31
        return accountServiceComponent.createObject(null,
 
32
                                                    {"objectHandle": service})
 
33
    }
 
34
 
 
35
    // virual
 
36
    function extendedSettings(inputFields)
 
37
    {
 
38
        return {}
 
39
        //Helper class to be extended by derived class
 
40
    }
 
41
 
 
42
    // virtual
 
43
    function formatDisplayName(inputFields)
 
44
    {
 
45
        return inputFields['account']
 
46
        // Helper function that allow the derived class to format a different display name
 
47
    }
 
48
 
 
49
    function saveServiceSettings(serviceIM, creds) {
 
50
        var settingsIM = serviceIM.settings
 
51
        var inputFields = {}
 
52
 
 
53
        settingsIM[root.keyPrefix + 'manager'] = root.manager
 
54
        settingsIM[root.keyPrefix + 'protocol'] = root.protocol
 
55
        settingsIM[root.keyPrefix + 'Icon'] = root.icon
 
56
 
 
57
        // basic fields
 
58
        for (var i=0; i < paramsRepeater.count; i++) {
 
59
            var fieldData = root.params[i]
 
60
            var field = paramsRepeater.itemAt(i)
 
61
            var fieldParamName = root.keyPrefix + 'param-' + fieldData.name
 
62
 
 
63
            if (field.isEmpty) {
 
64
                delete settingsIM[fieldParamName]
 
65
            } else {
 
66
                inputFields[fieldData.name] = field.value
 
67
                if (fieldData.store) {
 
68
                    settingsIM[fieldParamName] = field.value
 
69
                }
 
70
            }
 
71
        }
 
72
 
 
73
        // advanced fields
 
74
        for (var i=0; i < advancedParamsRepeater.count; i++) {
 
75
            var xFieldData = root.advancedParams[i]
 
76
            var xField = advancedParamsRepeater.itemAt(i)
 
77
            var xFieldParamName = root.keyPrefix + 'param-' + xFieldData.name
 
78
 
 
79
            if (xField.isEmpty) {
 
80
                delete settingsIM[xFieldParamName]
 
81
            } else {
 
82
                inputFields[xFieldData.name] = xField.value
 
83
 
 
84
                if (xFieldData.store) {
 
85
                    settingsIM[xFieldParamName] = xField.value
 
86
                }
 
87
            }
 
88
        }
 
89
 
 
90
 
 
91
        var xSettings = extendedSettings(inputFields)
 
92
        for (var key in xSettings) {
 
93
            settingsIM[root.keyPrefix + key] = xSettings[key]
 
94
        }
 
95
 
 
96
        account.updateDisplayName(formatDisplayName(inputFields))
 
97
 
 
98
        serviceIM.updateSettings(settingsIM)
 
99
        //serviceIM.credentials = creds
 
100
        //serviceIM.updateServiceEnabled(true)
 
101
    }
 
102
 
 
103
    function continueAccountSave(creds)  {
 
104
        var imService = root.getAccountService()
 
105
        if (!imService) {
 
106
            console.warn("Fail to retrieve account service")
 
107
            return
 
108
        }
 
109
 
 
110
        root.saveServiceSettings(imService, creds)
 
111
        if (creds)
 
112
            globalAccountService.credentials = creds
 
113
        globalAccountService.updateServiceEnabled(true)
 
114
 
 
115
        account.synced.connect(root.finished)
 
116
        account.sync()
 
117
    }
 
118
 
 
119
    function credentialsStored() {
 
120
        if (creds.credentialsId === 0) {
 
121
            console.warn("Credentials not stored correct")
 
122
            return
 
123
        }
 
124
 
 
125
        var imService = root.getAccountService()
 
126
        if (!imService) {
 
127
            console.warn("Fail to retrieve account service")
 
128
            return
 
129
        }
 
130
 
 
131
        continueAccountSave(creds)
 
132
    }
 
133
 
 
134
    function parseCrendentials() {
 
135
        var credentials = {'userName': '', 'password': ''}
 
136
 
 
137
        for (var i=0; i < paramsRepeater.count; i++) {
 
138
            var fieldData = root.params[i]
 
139
            var field = paramsRepeater.itemAt(i)
 
140
 
 
141
            if (fieldData.name === 'account')
 
142
                credentials['userName'] = field.value
 
143
 
 
144
            if (fieldData.name === 'password')
 
145
                credentials['password'] = field.value
 
146
        }
 
147
 
 
148
        return credentials
 
149
    }
 
150
 
 
151
    function cancel() {
 
152
        account.removed.connect(root.finished)
 
153
        account.remove(Account.RemoveCredentials)
 
154
    }
 
155
 
 
156
    function confirm() {
 
157
        var info = root.parseCrendentials()
 
158
        // save account
 
159
        account.updateDisplayName(info.userName)
 
160
        if (root.hasCrendentials) {
 
161
            creds.userName = info.userName
 
162
            creds.secret = info.password
 
163
            creds.sync()
 
164
        } else {
 
165
            continueAccountSave(null)
 
166
        }
 
167
    }
 
168
 
 
169
    Column {
 
170
        id: fields
 
171
 
 
172
        anchors {
 
173
            top: parent.top
 
174
            topMargin: units.gu(5)
 
175
            left: parent.left
 
176
            right: parent.right
 
177
        }
 
178
        height: childrenRect.height
 
179
        spacing: units.gu(2)
 
180
 
 
181
        Icon {
 
182
            anchors.horizontalCenter: fields.horizontalCenter
 
183
            name: root.icon
 
184
        }
 
185
 
 
186
        Repeater {
 
187
            id: paramsRepeater
 
188
 
 
189
            property bool fieldHasValues: false
 
190
 
 
191
            function checkFieldsHasValues()
 
192
            {
 
193
                var hasEmptyField = false
 
194
                for (var i = 0; i < paramsRepeater.count; i++) {
 
195
 
 
196
                    var child = paramsRepeater.itemAt(i)
 
197
                    if (child && child.isEmpty) {
 
198
                        hasEmptyField = true
 
199
                        break
 
200
                    }
 
201
                }
 
202
                fieldHasValues = !hasEmptyField
 
203
            }
 
204
 
 
205
            width: parent.width
 
206
            model: root.params
 
207
            DynamicField {
 
208
                model: modelData
 
209
                anchors{
 
210
                    left: parent.left
 
211
                    right: parent.right
 
212
                    margins: units.gu(4)
 
213
                }
 
214
                onValueChanged: paramsRepeater.checkFieldsHasValues()
 
215
            }
 
216
        }
 
217
 
 
218
        Item {
 
219
            id: div
 
220
 
 
221
            anchors{
 
222
                left: parent.left
 
223
                right: parent.right
 
224
            }
 
225
            height: units.gu(3)
 
226
            visible: root.advancedParams.length > 0
 
227
        }
 
228
 
 
229
        Label {
 
230
            id: advancedParamsTitle
 
231
 
 
232
            anchors{
 
233
                left: parent.left
 
234
                right: parent.right
 
235
                margins: units.gu(4)
 
236
            }
 
237
            visible: root.advancedParams.length > 0
 
238
            text: i18n.tr("Advanced Options")
 
239
            textSize: Label.Medium
 
240
        }
 
241
 
 
242
        Repeater {
 
243
            id: advancedParamsRepeater
 
244
 
 
245
            width: parent.width
 
246
            model: root.advancedParams
 
247
            DynamicField {
 
248
                model: modelData
 
249
                anchors{
 
250
                    left: parent.left
 
251
                    right: parent.right
 
252
                    margins: units.gu(4)
 
253
                }
 
254
            }
 
255
        }
 
256
    }
 
257
 
 
258
    AccountService {
 
259
        id: globalAccountService
 
260
 
 
261
        objectHandle: account.accountServiceHandle
 
262
        autoSync: false
 
263
    }
 
264
 
 
265
    Credentials {
 
266
        id: creds
 
267
 
 
268
        caption: account.provider.id
 
269
        acl: "*" // untill later
 
270
        storeSecret: true
 
271
        onCredentialsIdChanged: {
 
272
            console.debug("Credetials id changed")
 
273
            root.credentialsStored()
 
274
        }
 
275
    }
 
276
 
 
277
    // necessary to store settings on the "IM" service
 
278
    AccountServiceModel {
 
279
        id: serviceModel
 
280
 
 
281
        includeDisabled: true
 
282
        account: root.accountObjectHandle
 
283
        serviceType: "IM"
 
284
    }
 
285
 
 
286
    Component {
 
287
        id: accountServiceComponent
 
288
 
 
289
        AccountService {
 
290
            autoSync: false
 
291
        }
 
292
    }
 
293
}