2017-01-22 01:42:36 +08:00
//
// P G P K e y S e t t i n g T a b l e V i e w C o n t r o l l e r . s w i f t
// p a s s
//
// C r e a t e d b y M i n g s h e n S u n o n 2 1 / 1 / 2 0 1 7 .
// C o p y r i g h t © 2 0 1 7 B o b S u n . A l l r i g h t s r e s e r v e d .
//
import UIKit
2017-06-13 11:42:49 +08:00
import passKit
2017-01-22 01:42:36 +08:00
class PGPKeySettingTableViewController : UITableViewController {
2017-02-10 22:15:01 +08:00
@IBOutlet weak var pgpPublicKeyURLTextField : UITextField !
@IBOutlet weak var pgpPrivateKeyURLTextField : UITextField !
2017-02-17 14:58:27 +08:00
var pgpPassphrase : String ?
2017-03-16 22:39:03 -07:00
let passwordStore = PasswordStore . shared
2017-01-22 01:42:36 +08:00
override func viewDidLoad ( ) {
super . viewDidLoad ( )
2017-02-19 22:10:36 +08:00
tableView . rowHeight = UITableViewAutomaticDimension
2017-06-13 11:42:49 +08:00
pgpPublicKeyURLTextField . text = SharedDefaults [ . pgpPublicKeyURL ] ? . absoluteString
pgpPrivateKeyURLTextField . text = SharedDefaults [ . pgpPrivateKeyURL ] ? . absoluteString
2017-03-16 22:39:03 -07:00
pgpPassphrase = passwordStore . pgpKeyPassphrase
2017-01-22 01:42:36 +08:00
}
2017-02-06 13:25:27 +08:00
override func shouldPerformSegue ( withIdentifier identifier : String , sender : Any ? ) -> Bool {
if identifier = = " savePGPKeySegue " {
2017-05-11 22:55:55 -07:00
guard validatePGPKeyURL ( input : pgpPublicKeyURLTextField . text ) = = true ,
validatePGPKeyURL ( input : pgpPrivateKeyURLTextField . text ) = = true else {
2017-02-06 13:25:27 +08:00
return false
}
}
return true
}
2017-02-17 14:58:27 +08:00
2017-05-11 22:55:55 -07:00
private func validatePGPKeyURL ( input : String ? ) -> Bool {
guard let path = input , let url = URL ( string : path ) else {
Utils . alert ( title : " Cannot Save PGP Key " , message : " Please set PGP Key URL first. " , controller : self , completion : nil )
return false
}
guard let scheme = url . scheme , scheme = = " https " , scheme = = " https " else {
Utils . alert ( title : " Cannot Save PGP Key " , message : " HTTP connection is not supported. " , controller : self , completion : nil )
return false
}
return true
}
2017-02-17 14:58:27 +08:00
@IBAction func save ( _ sender : Any ) {
2017-06-07 21:11:01 +08:00
let savePassphraseAlert = UIAlertController ( title : " Passphrase " , message : " Do you want to save the passphrase for later decryption? " , preferredStyle : UIAlertControllerStyle . alert )
// n o
savePassphraseAlert . addAction ( UIAlertAction ( title : " No " , style : UIAlertActionStyle . default ) { _ in
self . pgpPassphrase = nil
2017-06-13 11:42:49 +08:00
SharedDefaults [ . isRememberPassphraseOn ] = false
2017-06-07 21:11:01 +08:00
self . performSegue ( withIdentifier : " savePGPKeySegue " , sender : self )
} )
// y e s
savePassphraseAlert . addAction ( UIAlertAction ( title : " Yes " , style : UIAlertActionStyle . destructive ) { _ in
// a s k f o r t h e p a s s p h r a s e
let alert = UIAlertController ( title : " Passphrase " , message : " Please fill in the passphrase of your PGP secret key. " , preferredStyle : UIAlertControllerStyle . alert )
alert . addAction ( UIAlertAction ( title : " OK " , style : UIAlertActionStyle . default , handler : { _ in
self . pgpPassphrase = alert . textFields ? . first ? . text
2017-06-13 11:42:49 +08:00
SharedDefaults [ . isRememberPassphraseOn ] = true
2017-06-07 21:11:01 +08:00
self . performSegue ( withIdentifier : " savePGPKeySegue " , sender : self )
} ) )
alert . addTextField ( configurationHandler : { ( textField : UITextField ! ) in
textField . text = self . pgpPassphrase
textField . isSecureTextEntry = true
} )
self . present ( alert , animated : true , completion : nil )
2017-02-17 14:58:27 +08:00
} )
2017-06-07 21:11:01 +08:00
self . present ( savePassphraseAlert , animated : true , completion : nil )
2017-02-17 14:58:27 +08:00
}
2017-01-22 01:42:36 +08:00
}