Debian Codegraph @

accounts/plugin/qml/NewAccount.qml

raw
1 import QtQuick 2.4
2 import Lomiri.Components 1.3
3 import SSO.OnlineAccounts 0.1
4
5 Item {
6 id: root
7
8 signal finished
9
10 height: contents.height
11
12 property var __account: account
13 property string __host: ""
14 property bool __busy: false
15 property string __hostError: i18n.dtr("lomiri-online-accounts-plugins-caldav", "Invalid host URL")
16
17 Column {
18 id: contents
19 anchors {
20 top: parent.top
21 left: parent.left
22 right: parent.right
23 margins: units.gu(2)
24 }
25 spacing: units.gu(2)
26
27 function formIsValid() {
28 return urlField.text.trim() !== "" && usernameField.text.trim() !== "" && passwordField.text.trim() !== ""
29 }
30
31 Label {
32 id: errorLabel
33 anchors { left: parent.left; right: parent.right }
34 font.bold: true
35 color: theme.palette.normal.negative
36 elide: Label.ElideRight
37 wrapMode: Text.Wrap
38 visible: !__busy && text != ""
39 }
40
41 Label {
42 anchors { left: parent.left; right: parent.right }
43 text: i18n.dtr("lomiri-online-accounts-plugins-caldav", "URL:")
44 }
45
46 TextField {
47 id: urlField
48 anchors { left: parent.left; right: parent.right }
49 placeholderText: i18n.dtr("lomiri-online-accounts-plugins-caldav", "http://myserver.com/caldav/")
50 focus: true
51 enabled: !__busy
52
53 inputMethodHints: Qt.ImhUrlCharactersOnly
54 }
55
56 Label {
57 anchors { left: parent.left; right: parent.right }
58 text: i18n.dtr("lomiri-online-accounts-plugins-caldav", "Username:")
59 }
60
61 TextField {
62 id: usernameField
63 anchors { left: parent.left; right: parent.right }
64 placeholderText: i18n.dtr("lomiri-online-accounts-plugins-caldav", "Your username")
65 enabled: !__busy
66 inputMethodHints: Qt.ImhNoAutoUppercase + Qt.ImhNoPredictiveText + Qt.ImhPreferLowercase
67
68 KeyNavigation.tab: passwordField
69 }
70
71 Label {
72 anchors { left: parent.left; right: parent.right }
73 text: i18n.dtr("lomiri-online-accounts-plugins-caldav", "Password:")
74 }
75
76 TextField {
77 id: passwordField
78 anchors { left: parent.left; right: parent.right }
79 placeholderText: i18n.dtr("lomiri-online-accounts-plugins-caldav", "Your password")
80 echoMode: TextInput.Password
81 enabled: !__busy
82
83 inputMethodHints: Qt.ImhSensitiveData
84 Keys.onReturnPressed: login()
85 }
86
87 Row {
88 id: buttons
89 anchors { left: parent.left; right: parent.right }
90 height: units.gu(5)
91 spacing: units.gu(1)
92 Button {
93 id: btnCancel
94 text: i18n.dtr("lomiri-online-accounts-plugins-caldav", "Cancel")
95 width: (parent.width / 2) - 0.5 * parent.spacing
96 onClicked: finished()
97 }
98 Button {
99 id: btnContinue
100 text: i18n.dtr("lomiri-online-accounts-plugins-caldav", "Continue")
101 color: theme.palette.normal.positive
102 width: (parent.width / 2) - 0.5 * parent.spacing
103 onClicked: login()
104 enabled: !__busy && contents.formIsValid()
105 }
106 }
107 }
108
109 ActivityIndicator {
110 anchors.centerIn: parent
111 running: __busy
112 }
113
114 Credentials {
115 id: creds
116 caption: account.provider.id
117 acl: [ "unconfined" ]
118 storeSecret: true
119 onCredentialsIdChanged: root.credentialsStored()
120 }
121
122 AccountService {
123 id: globalAccountSettings
124 objectHandle: account.accountServiceHandle
125 autoSync: false
126 }
127
128 function login() {
129 var host = cleanUrl(urlField.text)
130 var username = usernameField.text
131 var password = passwordField.text
132
133 errorLabel.text = ""
134 __busy = true
135
136 checkAccount(host, username, password, function cb(success) {
137 console.log("callback called: " + success)
138 if (success) {
139 saveData(host, username, password)
140 } else {
141 showError(i18n.tr("Could not connect to %1, invalid host or credentials").arg(host))
142 __busy = false
143 }
144 })
145 }
146
147 function saveData(host, username, password) {
148 __host = host
149 var strippedHost = host.replace(/^https?:\/\//, '')
150 account.updateDisplayName(username + '@' + strippedHost)
151 creds.userName = username
152 creds.secret = password
153 creds.sync()
154 }
155
156 function checkAccount(host, username, password, callback) {
157 console.log("Trying host " + host + " with " + username)
158 var request = new XMLHttpRequest();
159 request.onreadystatechange = function() {
160 if (request.readyState === XMLHttpRequest.DONE) {
161 console.log("response: ", request.status, request.responseText)
162 if (request.status >= 200 && request.status < 300) {
163 callback(true)
164 } else {
165 callback(false)
166 }
167 }
168 }
169 var url = host + "/" + username;
170 console.log('check url:', url)
171 request.open("PROPFIND", url, true, username, encodeURIComponent(password));
172 //request.setRequestHeader("Authorization", "Basic " + Qt.btoa(username + ":" + password));
173 request.setRequestHeader("Content-type",
174 "application/xml; charset='utf-8'");
175
176 var xml = '<?xml version="1.0" encoding="UTF-8" ?><propfind xmlns="DAV:">
177 <prop>
178 <current-user-principal/>
179 </prop>
180 </propfind>';
181 request.send(xml);
182 }
183
184 function showError(message) {
185 if (!errorLabel.text) errorLabel.text = message
186 }
187
188 function credentialsStored() {
189 console.log("Credentials stored, id: " + creds.credentialsId)
190 if (creds.credentialsId == 0) return
191
192 globalAccountSettings.updateServiceEnabled(true)
193 globalAccountSettings.credentials = creds
194 globalAccountSettings.updateSettings({
195 "host": __host,
196 "server_address": __host,
197 "webdav_path": '/'
198
199 })
200 account.synced.connect(finished)
201 account.sync()
202 __busy = false
203 }
204
205 // check host url for http
206 function cleanUrl(url) {
207 var host = url.trim()
208 return host
209 }
210
211 }