IOS-1542 Reuse servers, credentials. IOS-1542
authorDirk Zimmermann <dz@pep.security>
Thu, 25 Apr 2019 16:25:40 +0200
branchIOS-1542
changeset 83770ed9a236fa32
parent 8376 f517a983ae7c
child 8378 73a67e61d662
IOS-1542 Reuse servers, credentials.
pEpForiOS/Models/VerifiableAccount/VerifiableAccount.swift
     1.1 --- a/pEpForiOS/Models/VerifiableAccount/VerifiableAccount.swift	Thu Apr 25 16:25:40 2019 +0200
     1.2 +++ b/pEpForiOS/Models/VerifiableAccount/VerifiableAccount.swift	Thu Apr 25 16:25:40 2019 +0200
     1.3 @@ -176,49 +176,65 @@
     1.4  
     1.5              let cdAccount = findOrCreateAccount(context: moc, identity: cdIdentity)
     1.6  
     1.7 -            // TODO: Reuse server!
     1.8 +            var imapServer: CdServer?
     1.9              if let theServer = cdAccount.imapCdServer {
    1.10 -                delete(server: theServer, fromAccount: cdAccount)
    1.11 +                update(server: theServer,
    1.12 +                       address: addressImap,
    1.13 +                       port: portIMAP,
    1.14 +                       serverType: .imap,
    1.15 +                       authMethod: authMethod,
    1.16 +                       trusted: trustedImapServer,
    1.17 +                       transport: transportIMAP)
    1.18 +                imapServer = theServer
    1.19              }
    1.20  
    1.21 -            // TODO: Reuse server!
    1.22 +            let theImapServer = imapServer ?? createServer(context: moc,
    1.23 +                                                           address: addressImap,
    1.24 +                                                           port: portIMAP,
    1.25 +                                                           serverType: .imap,
    1.26 +                                                           authMethod: authMethod,
    1.27 +                                                           trusted: trustedImapServer,
    1.28 +                                                           transport: transportIMAP)
    1.29 +
    1.30 +            var smtpServer: CdServer?
    1.31              if let theServer = cdAccount.smtpCdServer {
    1.32 -                delete(server: theServer, fromAccount: cdAccount)
    1.33 +                update(server: theServer,
    1.34 +                       address: addressSmtp,
    1.35 +                       port: portSMTP,
    1.36 +                       serverType: .smtp,
    1.37 +                       authMethod: authMethod,
    1.38 +                       trusted: false,
    1.39 +                       transport: transportSMTP)
    1.40 +                smtpServer = theServer
    1.41              }
    1.42  
    1.43 -            let imapServer = createServer(context: moc,
    1.44 -                                          address: addressImap,
    1.45 -                                          port: portIMAP,
    1.46 -                                          serverType: .imap,
    1.47 -                                          authMethod: authMethod,
    1.48 -                                          trusted: trustedImapServer,
    1.49 -                                          transport: transportIMAP)
    1.50 +            let theSmtpServer = smtpServer ?? createServer(context: moc,
    1.51 +                                                           address: addressSmtp,
    1.52 +                                                           port: portSMTP,
    1.53 +                                                           serverType: .smtp,
    1.54 +                                                           authMethod: authMethod,
    1.55 +                                                           trusted: false,
    1.56 +                                                           transport: transportSMTP)
    1.57  
    1.58 -            let smtpServer = createServer(context: moc,
    1.59 -                                          address: addressSmtp,
    1.60 -                                          port: portSMTP,
    1.61 -                                          serverType: .smtp,
    1.62 -                                          authMethod: authMethod,
    1.63 -                                          trusted: false,
    1.64 -                                          transport: transportSMTP)
    1.65 +            let credentialsImap = theImapServer.credentials ?? createCredentials(
    1.66 +                context: moc,
    1.67 +                loginName: loginName,
    1.68 +                address: address,
    1.69 +                password: password,
    1.70 +                accessToken: accessToken)
    1.71 +            credentialsImap.servers = NSSet(array: [theImapServer])
    1.72 +            theImapServer.credentials = credentialsImap
    1.73  
    1.74 -            let credentialsImap = createCredentials(context: moc,
    1.75 -                                                    loginName: loginName,
    1.76 -                                                    address: address,
    1.77 -                                                    password: password,
    1.78 -                                                    accessToken: accessToken)
    1.79 -            credentialsImap.servers = NSSet(array: [imapServer])
    1.80 -            imapServer.credentials = credentialsImap
    1.81 +            let credentialsSmtp = theSmtpServer.credentials ?? createCredentials(
    1.82 +                context: moc,
    1.83 +                loginName: loginName,
    1.84 +                address: address,
    1.85 +                password: password,
    1.86 +                accessToken: accessToken)
    1.87 +            credentialsSmtp.servers = NSSet(array: [theSmtpServer])
    1.88 +            theSmtpServer.credentials = credentialsSmtp
    1.89  
    1.90 -            let credentialsSmtp = createCredentials(context: moc,
    1.91 -                                                    loginName: loginName,
    1.92 -                                                    address: address,
    1.93 -                                                    password: password,
    1.94 -                                                    accessToken: accessToken)
    1.95 -            credentialsSmtp.servers = NSSet(array: [smtpServer])
    1.96 -            smtpServer.credentials = credentialsSmtp
    1.97 -
    1.98 -            cdAccount.servers = NSSet(array: [imapServer, smtpServer])
    1.99 +            cdAccount.servers = NSSet(array: [theImapServer, theSmtpServer])
   1.100  
   1.101              moc.saveAndLogErrors()
   1.102          }