Debian Codegraph @

Dekko/stores/Accounts/AccountSetup.qml

raw
1 /* Copyright (C) 2016 - 2017 Dan Chapman <[email protected]>
2
3 This file is part of Dekko email client for Ubuntu devices
4
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License as
7 published by the Free Software Foundation; either version 2 of
8 the License or (at your option) version 3
9
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18 pragma Singleton
19 import QtQuick 2.4
20 import QuickFlux 1.0
21 import Dekko.Mail.Accounts 1.0
22 import Dekko.AutoConfig 1.0
23 import Dekko.Mail 1.0
24 import Dekko.Mail.Settings 1.0
25 import Dekko.Mail.API 1.0
26 import Dekko.Utils 1.0 as QtCoreAPI
27
28 /*!
29 *
30 * AccountSetup API
31 *
32 * AccountSetup maintains the state and processes for setting up an account
33 *
34 * @ingroup qml_stores
35 */
36 AppListener {
37 id: accountSetup
38
39 // We emit these signals for the DSM.SignalTransition's
40 signal createAccount()
41 signal goBack()
42 signal goNext()
43 signal userDetailsSet()
44 signal quit()
45 // new account accessor for the setupwizard
46 property NewAccount account: newAccount.createObject(accountSetup)
47
48 /** type:bool Is the NewAccount using a preset configuration */
49 readonly property alias isPreset: d.isPresetAccount
50 /** type:bool Is this a new IMAP account */
51 readonly property alias isImap: d.isImapAccount
52 /** type:bool Is this a new POP3 account */
53 readonly property alias isPop3: d.isPopAccount
54 /** type:bool Does the NewAccount require validation */
55 readonly property alias needsValidation: d.shouldValidate
56 /** type:string The description of the account. i.e IMAP/SMTP/Yahoo! etc */
57 readonly property alias accountDescripion: d.accountDescripion
58 /** type:bool A provider configuration was found during the AutoDiscover process */
59 readonly property alias hasProvider: d.hasProvider
60
61 property bool wizardRunning: false
62
63 // New account object
64 // This is a subclass of Account that allows editing of
65 // an invalid account before comitting to the QMailStore
66 Component {
67 id: newAccount
68 NewAccount {
69 }
70 }
71
72 AutoDiscover {
73 id: autoDiscover
74 onSuccess: {
75 Log.logStatus("AccountSetup::AutoDiscover", "Lookup succeeded!")
76 d.emailProvider = _serverConf
77 // // Check there is a server config for the selected account type
78 WizardActions.checkProviderForAccountType()
79 }
80 onFailed: {
81 Log.logWarning("AccountSetup::AutoDiscover", "Lookup failed!")
82 WizardActions.noServerDetailsFound()
83 }
84 }
85 // TODO: logging
86 AccountValidator {
87 id: validator
88 onSuccess: {
89 Log.logInfo("AccountSetup::validationSuccess", "Account validation succeeded")
90 Log.logInfo("AccountSetup::validationSuccess", "Setting default settings policies")
91 PolicyManager.setDefaultPolicies(account.id)
92 WizardActions.setDefaultIdentity()
93 WizardActions.wizardStepForward()
94 }
95 onValidationFailed: {
96 Log.logInfo("AccountSetup::validationFailed", "Account validation failed. Going back to manual input")
97 if (d.hasProvider) {
98 d.providerFailedValidation = true
99 }
100 accountSetup.goBack()
101 }
102 onFailedActionStatus: {
103 // Client knows how to handle this and pass it on to ErrorManager
104 // for the popups etc.
105 Client.handleFailure(account.id, errorCode, "Account validation action failed")
106 }
107 }
108
109 Connections {
110 target: Client
111 onAccountSynced: {
112 if (id == account.id) {
113 WizardActions.accountSynced()
114 }
115 }
116 onSyncAccountFailed: {
117 if (id == account.id) {
118 WizardActions.accountSyncFailed()
119 }
120 }
121 }
122
123 Filter {
124 type: WizardKeys.syncNewAccount
125 onDispatched: {
126 Log.logInfo("AccountSetup::syncNewAccount", "Starting initial sync for new account")
127 Client.synchronizeAccount(account.id)
128 }
129 }
130
131 Filter {
132 type: WizardKeys.endSetup
133 onDispatched: accountSetup.quit()
134 }
135
136 Filter {
137 type: WizardKeys.removeNewAccount
138 onDispatched: {
139 Log.logInfo("AccountSetup::removeNewAccount", "Attempting account removal")
140 // We don't need to confirm removal here
141 if (account.isValid) {
142 AccountActions.deleteAccount(account.id, false)
143 }
144 WizardActions.wizardResetAccount()
145 }
146 }
147
148 Filter {
149 type: WizardKeys.accountSynced
150 onDispatched: {
151 Log.logInfo("AccountSetup::accountSynced", "Initial sync complete")
152 // TODO: what do we need to do here?
153 WizardActions.wizardStepForward()
154 }
155 }
156
157 Filter {
158 type: WizardKeys.accountSyncFailed
159 onDispatched: {
160 Log.logInfo("AccountSetup::accountSyncFailed", "Initial sync failed :-(")
161 // TODO: what do we need to do here?
162 WizardActions.wizardStepForward()
163 }
164 }
165
166 Filter {
167 type: WizardKeys.setNewAccountType
168 onDispatched: {
169 d.accountDescripion = message.description
170 var type = message.type
171 switch (message.type) {
172 case AccountConfig.IMAP:
173 d.isImapAccount = true;
174 break;
175 case AccountConfig.POP3:
176 d.isPopAccount = true;
177 break;
178 case AccountConfig.SMTP:
179 type = AccountConfig.IMAP
180 break;
181 }
182 account.setSourceType(type)
183 }
184 }
185 Filter {
186 type: WizardKeys.setUserDetails
187 onDispatched: {
188 var details = message.details
189 // Name is username for imap/pop
190 account.name = details.nickname
191 account.incoming.name = details.email
192 account.incoming.email = details.email
193 account.incoming.password = details.password
194 account.outgoing.name = details.name
195 account.outgoing.email = details.email
196 account.outgoing.username = details.email
197 account.outgoing.password = details.password
198 Log.logInfo("AccountSetup::setUserDetails: ", "name: %1, email: %2".arg(details.name).arg(details.email))
199 accountSetup.userDetailsSet()
200 }
201 }
202
203 Filter {
204 type: WizardKeys.validateUser
205 onDispatched: {
206 var user = message.user
207 var valid = true
208 var invalidField = new Array();
209 if (user.name.isEmpty()) {
210 valid = false
211 invalidField.push("name")
212 }
213 if (user.nickname.isEmpty()){
214 valid = false
215 invalidField.push("nickname")
216 }
217
218 if (!EmailValidator.validate(user.email)) {
219 if (valid && user.email) {
220 PopupActions.showError(PopupKeys.popupMainWindow, qsTr("Invalid email address"))
221 }
222 valid = false
223 invalidField.push("email")
224 }
225 if (!valid) {
226 //we want to signal this first as the user may want an empty password field
227 WizardActions.userDetailsInvalid(invalidField)
228 } else if (!d.allowEmptyPassword && user.password.isEmpty()) {
229 // if the password is empty and the user hasn't confirmed it then
230 // We have to prompt for them to allow it.
231 WizardActions.noPasswordSet()
232 } else {
233 WizardActions.userDetailsValid()
234 }
235 }
236 }
237
238 Filter {
239 type: WizardKeys.validateServer
240 onDispatched: {
241 var config = message.config
242 var valid = true
243 var invalidFields = new Array()
244 if (config.hostname.isEmpty()) {
245 valid = false
246 invalidFields.push("hostname")
247 }
248 if (config.port.isEmpty()) {
249 valid = false
250 invalidFields.push("port")
251 }
252 if (config.encryption < AccountConfig.None || config.encryption > AccountConfig.STARTTLS) {
253 valid = false
254 invalidFields.push("encryption")
255 }
256 if (config.username.isEmpty()) {
257 valid = false
258 invalidFields.push("username")
259 }
260 if (config.servicetype !== "pop3") {
261 if (config.authentication < AccountConfig.NONE || config.authentication > AccountConfig.CRAM_MD5) {
262 valid = false
263 invalidFields.push("authentication")
264 }
265 }
266 if (!valid) {
267 //we want to signal this first as the user may want an empty password field
268 WizardActions.invalidServerConfig(config.servicetype, invalidFields)
269 } else if (!d.allowEmptyPassword && config.password.isEmpty()) {
270 // if the password is empty and the user hasn't confirmed it then
271 // We have to prompt for them to allow it.
272 WizardActions.noServerPasswordSet()
273 } else {
274 WizardActions.serverConfigValid(config.servicetype)
275 }
276 }
277 }
278
279 Filter {
280 type: WizardKeys.setNoPasswordAllowed
281 onDispatched: {
282 d.allowEmptyPassword = true
283 }
284 }
285
286 Filter {
287 type: WizardKeys.applyServerConfig
288 onDispatched: {
289 var config = message.config
290 if (config.servicetype === "smtp") {
291 account.outgoing.username = config.username
292 account.outgoing.password = config.password
293 account.outgoing.server = config.hostname
294 account.outgoing.port = config.port
295 account.outgoing.encryption = config.encryption
296 account.outgoing.saslMechanism = config.authentication
297 account.outgoing.acceptUntrustedCertificates = config.allowUntrusted
298 } else {
299 account.incoming.name = config.username
300 account.incoming.email = config.username
301 account.incoming.password = config.password
302 account.incoming.server = config.hostname
303 account.incoming.port = config.port
304 account.incoming.encryption = config.encryption
305 account.incoming.saslMechanism = config.authentication
306 account.incoming.acceptUntrustedCertificates = config.allowUntrusted
307 }
308 account.save()
309 }
310 }
311
312 Filter {
313 type: WizardKeys.wizardStepBack
314 onDispatched: {
315 // We don't actually want to do anything here
316 // as we need to be sure the wizardStepBack listener on the StackView
317 // pops the page before emitting goBack, otherwise
318 // we end up with a push instead of pop
319 }
320 }
321 Filter {
322 type: WizardKeys.wizardStepForward
323 onDispatched: {
324 accountSetup.goNext()
325 }
326 }
327
328 Filter {
329 type: WizardKeys.wizardSetAccountPreset
330 onDispatched: {
331 var cfg = message.config
332 if (cfg.type !== Provider.PRESET) {
333 Log.logError("AccountSetup::wizardSetPreset", "Passed config not of type Provider.PRESET. Aborting...")
334 return
335 }
336 Log.logInfo("AccountSetup::wizardSetPreset", "ImapHost: %1, Port: %2".arg(cfg.imapHost).arg(cfg.imapPort))
337 account.incoming.server = cfg.imapHost
338 account.incoming.port = cfg.imapPort
339 account.incoming.encryption = d.getPresetEncryptionMethod(cfg.imapStartTLS, cfg.imapUseSSL)
340 account.incoming.saslMechanism = AccountConfig.LOGIN
341 account.incoming.acceptUntrustedCertificates = false
342 account.outgoing.server = cfg.smtpHost
343 account.outgoing.port = cfg.smtpPort
344 account.outgoing.encryption = d.getPresetEncryptionMethod(cfg.smtpStartTLS, cfg.smtpUseSSL)
345 account.outgoing.saslMechanism = AccountConfig.LOGIN
346 account.outgoing.acceptUntrustedCertificates = false
347 d.isPresetAccount = true
348 }
349 }
350
351 Filter {
352 type: WizardKeys.lookForServerDetails
353 onDispatched: {
354 if (EmailValidator.validate(account.incoming.email)) {
355 Log.logInfo("AccountSetup::lookForServerDetails", "Starting autodiscover......")
356 autoDiscover.lookUp(account.incoming.email)
357 } else {
358 // We can't work here without an email address
359 // so as to not bother anyone just dispatch that we couldn't find anything
360 WizardActions.noServerDetailsFound()
361 Log.logWarning("AccountSetup::lookForServerDetails", "Invalid email address. Signalled no details were found!")
362 }
363 }
364 }
365
366 Filter {
367 type: WizardKeys.noServerDetailsFound
368 onDispatched: WizardActions.requestManualInput()
369 }
370
371 Filter {
372 type: WizardKeys.checkProviderForAccountType
373 onDispatched: {
374 // TODO: ask about using imap instead for a pop3 account
375 if (d.isPopAccount && d.emailProvider.hasImapConfiguration() && d.emailProvider.hasPopConfiguration()) {
376 Log.logInfo("AccountSetup::checkProviderForAccountType", "Asking if user would prefer to use imap")
377 WizardActions.askAboutImapInstead()
378 return;
379 }
380 if (d.isPopAccount && d.emailProvider.hasPopConfiguration()) {
381 Log.logInfo("AccountSetup::checkProviderForAccountType", "Pop configuration found. Needs validating")
382 d.shouldValidate = true
383 }
384 if (d.isImapAccount && d.emailProvider.hasImapConfiguration()) {
385 Log.logInfo("AccountSetup::checkProviderForAccountType", "Imap configuration found. Needs validating")
386 d.shouldValidate = true
387 }
388 WizardActions.wizardStepForward()
389 }
390 }
391
392 Filter {
393 type: WizardKeys.runAccountValidation
394 onDispatched: {
395 account.save()
396 if (d.isImapAccount) {
397
398 var imap = account.incoming
399 imap.idleEnabled = true
400 imap.appendPushFolder("INBOX")
401 imap.checkInterval = 5
402 account.save()
403 console.log("IDLE enabled", imap.idleEnabled)
404 console.log("Push Folders: ", imap.pushFolders)
405 }
406
407 // Delay this starting to allow time for the
408 // the messageserver tobe notified of the new account
409 delayValidate.start()
410 }
411 }
412
413 Timer {
414 id: delayValidate
415 interval: 500 // probably overkill :-)
416 repeat: false
417 onTriggered: validator.validateAccount(account)
418 }
419
420 Filter {
421 type: WizardKeys.validateConfigToUse
422 onDispatched: {
423 Log.logStatus("AccountSetup::validateConfigToUse", "Determining which config to use")
424 // TODO: iterate over configs to find the one that works
425 // FOr now were just using the 1st config and request manual input if that fails
426 if (d.isPopAccount && d.emailProvider.hasPopConfiguration()) {
427 var pop = d.emailProvider.getFirstPopConfig()
428 account.incoming.server = pop.hostname
429 account.incoming.port = pop.port
430 account.incoming.encryption = d.getEncryptionMethodFromConfig(pop.socket)
431 account.incoming.saslMechanism = d.getSaslFromConfig(pop.mechanism)
432 }
433 if (d.isImapAccount && d.emailProvider.hasImapConfiguration()) {
434 var imap = d.emailProvider.getFirstImapConfig()
435 account.incoming.server = imap.hostname
436 account.incoming.port = imap.port
437 account.incoming.encryption = d.getEncryptionMethodFromConfig(imap.socket)
438 account.incoming.saslMechanism = d.getSaslFromConfig(imap.mechanism)
439 }
440 if (!d.emailProvider.hasSmtpConfiguration()) {
441 WizardActions.requestManualInput()
442 } else {
443 var smtp = d.emailProvider.getFirstSmtpConfig()
444 account.outgoing.server = smtp.hostname
445 account.outgoing.port = smtp.port
446 account.outgoing.encryption = d.getEncryptionMethodFromConfig(smtp.socket)
447 account.outgoing.saslMechanism = d.getSaslFromConfig(smtp.mechanism)
448 }
449 WizardActions.validateIfAccountHasEnoughCredentials()
450 }
451 }
452
453 Filter {
454 type: WizardKeys.validateCredentials
455 onDispatched: {
456 if (d.isPresetAccount) {
457 Log.logInfo("AccountSetup::validateCredentials", "Is preset account validating now.")
458 WizardActions.runAccountValidation()
459 } else if (d.hasProvider && !d.providerFailedValidation) {
460 Log.logInfo("AccountSetup::validateCredentials", "Has provider, determining valid config to use")
461 WizardActions.validateConfigToUse()
462 } else {
463 Log.logInfo("AccountSetup::validateCredentials", "Ok... checking if we have enough info to validate")
464 WizardActions.validateIfAccountHasEnoughCredentials()
465 }
466 }
467 }
468
469 Filter {
470 type: WizardKeys.validateIfAccountHasEnoughCredentials
471 onDispatched: {
472 if (d.accountIsSavable()) {
473 WizardActions.runAccountValidation()
474 } else {
475 WizardActions.requestManualInput()
476 }
477 }
478 }
479
480 Filter {
481 type: WizardKeys.requestManualInput
482 onDispatched: {
483 d.shouldValidate = false
484 WizardActions.wizardStepForward()
485 }
486 }
487
488 Filter {
489 type: WizardKeys.useImapInstead
490 onDispatched: {
491 Log.logInfo("AccountSetup::useImapInstead", "Switching to imap setup")
492 d.isPopAccount = false
493 d.isImapAccount = true
494 // TODO: Reset the NewAccount to create an ImapAccountConfiguration
495 WizardActions.wizardStepForward()
496 }
497 }
498
499 Filter {
500 type: WizardKeys.stickWithPop
501 onDispatched: {
502 if (d.isPopAccount && d.emailProvider.hasPopConfiguration()) {
503 Log.logInfo("AccountSetup::checkProviderForAccountType", "Pop configuration found. Needs validating")
504 d.shouldValidate = true
505 }
506 WizardActions.wizardStepForward()
507 }
508 }
509
510 Filter {
511 type: WizardKeys.wizardResetAccount
512 onDispatched: {
513 // TODO: reset account
514 Log.logError("AccountSetup::resetAccount", "FIXME: implement account reset")
515 d.isPresetAccount = false
516 d.isImapAccount = false
517 d.isPopAccount = false
518 d.accountDescripion = ""
519 d.allowEmptyPassword = false
520 d.emailProvider = undefined
521 d.shouldValidate = false
522 d.providerFailedValidation = false
523 account = newAccount.createObject(accountSetup)
524 }
525 }
526
527 Filter {
528 type: WizardKeys.setDefaultIdentity
529 onDispatched: {
530 Log.logStatus("AccountSetup::setDefaultIdentity", "Setting default identity")
531 var identity = {}
532 identity["parentId"] = account.id
533 identity["name"] = account.outgoing.name
534 identity["email"] = account.outgoing.email
535 identity["replyTo"] = ""
536 identity["signature"] = qsTr("Sent with Dekko")
537 AccountActions.addIdentity(identity)
538 }
539 }
540
541 // We don't expose any utility functions or properties, as developers
542 // should dispatch actions via the actions api's.
543 QtObject {
544 id: d
545 property bool isImapAccount: false
546 property bool isPopAccount: false
547 property bool isPresetAccount: false
548 property string accountDescripion: ""
549 property bool allowEmptyPassword: false
550 property var emailProvider: undefined
551 property bool shouldValidate: false
552 property bool hasProvider: emailProvider !== undefined
553 property bool providerFailedValidation: false
554
555 function getPresetEncryptionMethod(useStartTls, useSsl) {
556 if (useSsl && useStartTls) {
557 return AccountConfig.SSL;
558 } else if (useStartTls) {
559 return AccountConfig.STARTTLS;
560 } else {
561 return AccountConfig.NONE;
562 }
563 }
564
565 function getEncryptionMethodFromConfig(socket) {
566 switch(socket) {
567 case ServerConfig.SSL:
568 return AccountConfig.SSL
569 case ServerConfig.STARTTLS:
570 return AccountConfig.STARTTLS
571 case ServerConfig.PLAIN:
572 return AccountConfig.NONE
573 }
574 }
575
576 function getSaslFromConfig(sasl) {
577 switch(sasl) {
578 case ServerConfig.CRAM_MD5:
579 return AccountConfig.CRAM_MD5
580 case ServerConfig.LOGIN:
581 return AccountConfig.LOGIN
582 case ServerConfig.NTLM:
583 case ServerConfig.GSSAPI:
584 case ServerConfig.CLIENT_IP:
585 return AccountConfig.PLAIN
586 case ServerConfig.INVALID:
587 return AccountConfig.NONE
588 }
589 }
590
591 function serverUsable(server) {
592 if ((server.server !== "") &&
593 (server.port !== 0) &&
594 (server.name !== "") &&
595 (server.email !== "")) {
596 return true;
597 } else {
598 return false;
599 }
600
601 }
602
603 function accountIsSavable() {
604 if (!serverUsable(account.incoming)) {
605 return false
606 }
607 return serverUsable(account.outgoing)
608 }
609 }
610 }