IOS-1480 Keep Account+Extentions. IOS-1480
authorDirk Zimmermann <dz@pep.security>
Mon, 18 Feb 2019 16:22:31 +0100
branchIOS-1480
changeset 7706ef61e55be41a
parent 7705 e88ff99dfa78
child 7722 59ee768e10f2
IOS-1480 Keep Account+Extentions.
Backed out changeset 4cff8dbca89a
pEpForiOS.xcodeproj/project.pbxproj
pEpForiOS/Models/Account+Extentions.swift
     1.1 --- a/pEpForiOS.xcodeproj/project.pbxproj	Mon Feb 18 16:22:31 2019 +0100
     1.2 +++ b/pEpForiOS.xcodeproj/project.pbxproj	Mon Feb 18 16:22:31 2019 +0100
     1.3 @@ -177,6 +177,7 @@
     1.4  		15B220521FC2DAE200CA52BA /* InfoPlist+pEpStrings.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15B220511FC2DAE200CA52BA /* InfoPlist+pEpStrings.swift */; };
     1.5  		15B483DB1F28E2FC000FB2CF /* SpecialUseMailboxesTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15B483DA1F28E2FC000FB2CF /* SpecialUseMailboxesTest.swift */; };
     1.6  		15B483E11F290B14000FB2CF /* LoginImapOperationTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15B483E01F290B14000FB2CF /* LoginImapOperationTest.swift */; };
     1.7 +		15BA536A20A08B100090F126 /* Account+Extentions.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15BA536920A08B100090F126 /* Account+Extentions.swift */; };
     1.8  		15BA536E20A095410090F126 /* UnifiedInboxTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15BA536D20A095410090F126 /* UnifiedInboxTest.swift */; };
     1.9  		15BBBC681FD04DFC00B9DCC8 /* ReportingErrorContainerTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15BBBC671FD04DFC00B9DCC8 /* ReportingErrorContainerTest.swift */; };
    1.10  		15BBBC6B1FD0527200B9DCC8 /* DisplayUserErrorTest.swift in Sources */ = {isa = PBXBuildFile; fileRef = 15BBBC6A1FD0527200B9DCC8 /* DisplayUserErrorTest.swift */; };
    1.11 @@ -709,6 +710,7 @@
    1.12  		15B220511FC2DAE200CA52BA /* InfoPlist+pEpStrings.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "InfoPlist+pEpStrings.swift"; sourceTree = "<group>"; };
    1.13  		15B483DA1F28E2FC000FB2CF /* SpecialUseMailboxesTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = SpecialUseMailboxesTest.swift; sourceTree = "<group>"; };
    1.14  		15B483E01F290B14000FB2CF /* LoginImapOperationTest.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = LoginImapOperationTest.swift; sourceTree = "<group>"; };
    1.15 +		15BA536920A08B100090F126 /* Account+Extentions.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "Account+Extentions.swift"; sourceTree = "<group>"; };
    1.16  		15BA536D20A095410090F126 /* UnifiedInboxTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = UnifiedInboxTest.swift; sourceTree = "<group>"; };
    1.17  		15BA537B20A1F5F50090F126 /* MoveToAccountViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = MoveToAccountViewController.swift; sourceTree = "<group>"; };
    1.18  		15BBBC671FD04DFC00B9DCC8 /* ReportingErrorContainerTest.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReportingErrorContainerTest.swift; sourceTree = "<group>"; };
    1.19 @@ -2273,6 +2275,7 @@
    1.20  			isa = PBXGroup;
    1.21  			children = (
    1.22  				15F0F7AB21359372002BE853 /* Filter */,
    1.23 +				15BA536920A08B100090F126 /* Account+Extentions.swift */,
    1.24  				15F835231F34BE1300FCE887 /* AccountUserInput.swift */,
    1.25  				43CE63CA1DE8830100FAC505 /* CdAccount+pEp.swift */,
    1.26  				431B04811DE583A000E40CD3 /* CdAttachment+Extension.swift */,
    1.27 @@ -3210,6 +3213,7 @@
    1.28  				43CE63CB1DE8830100FAC505 /* CdAccount+pEp.swift in Sources */,
    1.29  				438052871FE3E1B100ACF729 /* OAuth2AuthorizationFactoryProtocol.swift in Sources */,
    1.30  				4356FFE52135448600804089 /* ReplyAllPossibleChecker.swift in Sources */,
    1.31 +				15BA536A20A08B100090F126 /* Account+Extentions.swift in Sources */,
    1.32  				15255B031F825CD100A2CFC9 /* IdentityImageTool.swift in Sources */,
    1.33  				43AA82531E9B925C00ABD5A8 /* UIView+Util.swift in Sources */,
    1.34  				152A39D121905C3E00D9F8E4 /* AttachmentViewModel.swift in Sources */,
     2.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     2.2 +++ b/pEpForiOS/Models/Account+Extentions.swift	Mon Feb 18 16:22:31 2019 +0100
     2.3 @@ -0,0 +1,88 @@
     2.4 +//
     2.5 +//  Account+Extentions.swift
     2.6 +//  pEp
     2.7 +//
     2.8 +//  Created by Andreas Buff on 07.05.18.
     2.9 +//  Copyright © 2018 p≡p Security S.A. All rights reserved.
    2.10 +//
    2.11 +
    2.12 +import MessageModel
    2.13 +
    2.14 +extension Account {
    2.15 +    /// Returns the account that should be used as deafult when sending a message.
    2.16 +    ///
    2.17 +    /// - Returns: default account
    2.18 +    static public func defaultAccount() -> Account? {
    2.19 +        guard let addressDefaultAccount = AppSettings.defaultAccount else {
    2.20 +            return all().first
    2.21 +        }
    2.22 +        return Account.by(address: addressDefaultAccount)
    2.23 +    }
    2.24 +
    2.25 +    func emailConnectInfos() -> [EmailConnectInfo] {
    2.26 +        var result = [EmailConnectInfo]()
    2.27 +        guard let servers = servers else {
    2.28 +            return result
    2.29 +        }
    2.30 +
    2.31 +        for server in servers {
    2.32 +            guard
    2.33 +                server.serverType == Server.ServerType.imap ||
    2.34 +                    server.serverType == Server.ServerType.smtp
    2.35 +                else {
    2.36 +                    Logger.modelLogger.errorAndCrash("Unsupported server type")
    2.37 +                    continue
    2.38 +            }
    2.39 +            let credentials = server.credentials
    2.40 +            if let emailConnectInfo = Account.emailConnectInfo(account: self,
    2.41 +                                                               server: server,
    2.42 +                                                               credentials: credentials) {
    2.43 +                result.append(emailConnectInfo)
    2.44 +            }
    2.45 +        }
    2.46 +
    2.47 +        return result
    2.48 +    }
    2.49 +
    2.50 +    /**
    2.51 +     - Returns: The first found IMAP connect info. Used by some tests.
    2.52 +     */
    2.53 +    var imapConnectInfo: EmailConnectInfo? {
    2.54 +        return emailConnectInfos().filter { return $0.emailProtocol == .imap }.first
    2.55 +    }
    2.56 +
    2.57 +    /**
    2.58 +     - Returns: The first found SMTP connect info. Used by some tests.
    2.59 +     */
    2.60 +    var smtpConnectInfo: EmailConnectInfo? {
    2.61 +        return emailConnectInfos().filter { return $0.emailProtocol == .smtp }.first
    2.62 +    }
    2.63 +
    2.64 +    static func emailConnectInfo(account: Account, server: Server,
    2.65 +                          credentials: ServerCredentials) -> EmailConnectInfo? {
    2.66 +        guard
    2.67 +            let emailProtocol = EmailProtocol(serverType: server.serverType),
    2.68 +            let connectionTransport = server.transport
    2.69 +            else {
    2.70 +                Logger.modelLogger.errorAndCrash("Missing emailProtocol")
    2.71 +                return nil
    2.72 +        }
    2.73 +
    2.74 +        let trusedServer =
    2.75 +            server.trusted || AppSettings.isManuallyTrustedServer(address: account.user.address)
    2.76 +
    2.77 +        return EmailConnectInfo(account: account,
    2.78 +                                server: server,
    2.79 +                                credentials: credentials,
    2.80 +                                loginName: credentials.loginName,
    2.81 +                                loginPasswordKeyChainKey: credentials.key,
    2.82 +                                networkAddress: server.address,
    2.83 +                                networkPort: server.port,
    2.84 +                                networkAddressType: nil,
    2.85 +                                networkTransportType: nil,
    2.86 +                                emailProtocol: emailProtocol,
    2.87 +                                connectionTransport: ConnectionTransport(fromInt: Int(connectionTransport.rawValue)),
    2.88 +                                authMethod: AuthMethod(string: server.authMethod),
    2.89 +                                trusted: trusedServer)
    2.90 +    }
    2.91 +}