Skip to content

Instantly share code, notes, and snippets.

@tlkahn
Created September 24, 2017 03:53
Show Gist options
  • Save tlkahn/ab9149e133ca3522c97c813d85115dd8 to your computer and use it in GitHub Desktop.
Save tlkahn/ab9149e133ca3522c97c813d85115dd8 to your computer and use it in GitHub Desktop.
func makeConnectionType(str: String) throws -> ConnectionType {
switch str {
case "clear":
return .clear
case "startTLS":
return .startTLS
case "tls":
return .tls
default:
throw "unkown connection type string"
}
}
func getConnectionTypeRawValues(connectionType: ConnectionType) throws -> String {
switch connectionType {
case .clear:
return "clear"
case .startTLS:
return "startTLS"
case .tls:
return "tls"
}
}
func makePasswordType(str: String) throws -> PasswordType {
return .plain(str)
}
func getPasswordRawValues(passwordType: PasswordType) throws -> String {
switch passwordType {
case .plain(let str):
return str
default:
throw "password not plain"
}
}
class ConfigurationClass: NSObject, NSCoding {
var config: Configuration?
init(config: Configuration) {
self.config = config
super.init()
}
class func path() -> String {
let documentsPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
let path = documentsPath?.appending("/Configuration")
return path!
}
required init?(coder aDecoder: NSCoder) {
guard let hostname: String = aDecoder.decodeObject(forKey: "hostname") as? String else { config = nil; super.init(); return nil }
guard let port: UInt16 = aDecoder.decodeObject(forKey: "port") as? UInt16 else { config = nil; super.init(); return nil }
guard let login: String = aDecoder.decodeObject(forKey: "login") as? String else { config = nil; super.init(); return nil }
guard let password: String = aDecoder.decodeObject(forKey: "password") as? String else { config = nil; super.init(); return nil }
guard let connectionType: String = aDecoder.decodeObject(forKey: "connectionType") as? String else { config = nil; super.init(); return nil }
guard let checkCertificateEnabled: Bool = aDecoder.decodeObject(forKey: "checkCertificateEnabled") as? Bool else { config = nil; super.init(); return nil }
guard let batchSize: Int = aDecoder.decodeObject(forKey: "batchSize") as? Int else { config = nil; super.init(); return nil }
guard let spamFolderName: String = aDecoder.decodeObject(forKey: "spamFolderName") as? String else { config = nil; super.init(); return nil }
let ct = try! makeConnectionType(str: connectionType)
let pw = try! makePasswordType(str: password)
self.config = Configuration(hostname: hostname, port: port, login: login, password: pw, connectionType: ct, checkCertificateEnabled: checkCertificateEnabled, batchSize: batchSize, spamFolderName: spamFolderName)
super.init()
}
func encode(with aCoder: NSCoder) {
let ct = try! getConnectionTypeRawValues(connectionType: config!.connectionType)
let pw = try! getPasswordRawValues(passwordType: config!.password)
aCoder.encode(config!.hostname, forKey: "hostname")
aCoder.encode(config!.port, forKey: "port")
aCoder.encode(config!.login, forKey: "login")
// TODO: use .plain in all encoder for password now. This may change.
aCoder.encode(pw, forKey: "password")
aCoder.encode(ct, forKey: "connectionType")
aCoder.encode(config!.checkCertificateEnabled, forKey: "checkCertificateEnabled")
aCoder.encode(config!.batchSize, forKey: "batchSize")
aCoder.encode(config!.spamFolderName, forKey: "spamFolderName")
}
}
extension Configuration {
static func encode(config: Configuration) -> Data {
let configObj = ConfigurationClass(config: config)
NSKeyedArchiver.archiveRootObject(configObj, toFile: ConfigurationClass.path())
return NSKeyedArchiver.archivedData(withRootObject: configObj)
}
static func decode(data: Data) -> Configuration? {
let config = NSKeyedUnarchiver.init(forReadingWith: data).decodeObject(forKey: "root") as? ConfigurationClass //.unarchiveObject(withFile: ConfigurationClass.path()) as? ConfigurationClass
return config?.config
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment