NCViewCertificateDetails.swift 3.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. //
  2. // NCViewCertificateDetails.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 01/06/21.
  6. // Copyright © 2021 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. public protocol NCViewCertificateDetailsDelegate {
  25. func viewCertificateDetailsDismiss(host: String)
  26. }
  27. // optional func
  28. public extension NCViewCertificateDetailsDelegate {
  29. func viewCertificateDetailsDismiss(host: String) {}
  30. }
  31. class NCViewCertificateDetails: UIViewController {
  32. @IBOutlet weak var buttonCancel: UIBarButtonItem!
  33. @IBOutlet weak var scrollView: UIScrollView!
  34. @IBOutlet weak var textView: UITextView!
  35. private let directoryCertificate = CCUtility.getDirectoryCerificates()!
  36. public var delegate: NCViewCertificateDetailsDelegate?
  37. @objc public var host: String = ""
  38. // MARK: - View Life Cycle
  39. override func viewDidLoad() {
  40. super.viewDidLoad()
  41. self.navigationItem.title = NSLocalizedString("_certificate_details_", comment: "")
  42. buttonCancel.title = NSLocalizedString("_close_", comment: "")
  43. let certNamePathTXT = directoryCertificate + "/" + host + ".txt"
  44. if FileManager.default.fileExists(atPath: certNamePathTXT) {
  45. do {
  46. let text = try String(contentsOfFile: certNamePathTXT, encoding: .utf8)
  47. let font = UIFont.systemFont(ofSize: 13)
  48. let attributes = [NSAttributedString.Key.font: font] as [NSAttributedString.Key: Any]
  49. var contentRect = NSString(string: text).boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil)
  50. contentRect = CGRect(x: contentRect.origin.x, y: contentRect.origin.y, width: ceil(contentRect.size.width), height: ceil(contentRect.size.height))
  51. var contentWidth = contentRect.size.width
  52. if contentWidth < view.frame.size.width {
  53. contentWidth = view.frame.size.width
  54. }
  55. var contentHeight = contentRect.size.height
  56. if contentHeight < view.frame.size.height {
  57. contentHeight = view.frame.size.width
  58. }
  59. textView.frame = CGRect(x: 0, y: 0, width: contentWidth, height: contentHeight)
  60. textView.font = font
  61. textView.text = text
  62. scrollView.contentSize = contentRect.size
  63. scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
  64. } catch {
  65. print("error")
  66. }
  67. } else {
  68. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  69. self.dismiss(animated: true, completion: nil)
  70. }
  71. }
  72. }
  73. override func viewWillDisappear(_ animated: Bool) {
  74. super.viewWillDisappear(animated)
  75. self.delegate?.viewCertificateDetailsDismiss(host: host)
  76. }
  77. // MARK: ACTION
  78. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  79. self.dismiss(animated: true, completion: nil)
  80. }
  81. }