NCViewCertificateDetails.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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()
  26. }
  27. // optional func
  28. public extension NCViewCertificateDetailsDelegate {
  29. func viewCertificateDetailsDismiss() {}
  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 appDelegate = UIApplication.shared.delegate as! AppDelegate
  36. private let directoryCertificate = CCUtility.getDirectoryCerificates()!
  37. public var delegate: NCViewCertificateDetailsDelegate?
  38. public var host: String?
  39. // MARK: - View Life Cycle
  40. override func viewDidLoad() {
  41. super.viewDidLoad()
  42. var certificatePath = directoryCertificate + "/" + NCGlobal.shared.certificateTmpV2 + ".txt"
  43. let hostPushNotificationServerProxy = URL(string: NCBrandOptions.shared.pushNotificationServerProxy)?.host
  44. self.navigationItem.title = NSLocalizedString("_certificate_details_", comment: "")
  45. buttonCancel.title = NSLocalizedString("_close_", comment: "")
  46. if host == hostPushNotificationServerProxy {
  47. certificatePath = directoryCertificate + "/" + NCGlobal.shared.certificatePushNotificationServerProxy
  48. } else {
  49. if let host = host {
  50. certificatePath = directoryCertificate + "/" + host + ".der"
  51. } else {
  52. if let host = URL(string: appDelegate.urlBase)?.host {
  53. certificatePath = directoryCertificate + "/" + host + ".der"
  54. }
  55. }
  56. }
  57. if FileManager.default.fileExists(atPath: certificatePath) {
  58. do {
  59. let text = try String(contentsOfFile: certificatePath, encoding: .utf8)
  60. let font = UIFont.systemFont(ofSize: 13)
  61. let attributes = [NSAttributedString.Key.font: font] as [NSAttributedString.Key : Any]
  62. var contentRect = NSString(string: text).boundingRect(with: CGSize(width: CGFloat.greatestFiniteMagnitude, height: 0), options: NSStringDrawingOptions.usesLineFragmentOrigin, attributes: attributes, context: nil)
  63. contentRect = CGRect(x: contentRect.origin.x, y: contentRect.origin.y, width: ceil(contentRect.size.width), height: ceil(contentRect.size.height))
  64. var contentWidth = contentRect.size.width
  65. if contentWidth < view.frame.size.width {
  66. contentWidth = view.frame.size.width
  67. }
  68. var contentHeight = contentRect.size.height
  69. if contentHeight < view.frame.size.height {
  70. contentHeight = view.frame.size.width
  71. }
  72. textView.frame = CGRect(x: 0, y: 0, width: contentWidth, height: contentHeight)
  73. textView.font = font
  74. textView.text = text
  75. scrollView.contentSize = contentRect.size
  76. scrollView.contentInset = UIEdgeInsets(top: 0, left: 0, bottom: 50, right: 0)
  77. } catch {
  78. print("error")
  79. }
  80. } else {
  81. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  82. self.dismiss(animated: true, completion: nil)
  83. }
  84. }
  85. }
  86. override func viewWillDisappear(_ animated: Bool) {
  87. super.viewWillDisappear(animated)
  88. self.delegate?.viewCertificateDetailsDismiss()
  89. }
  90. // MARK: ACTION
  91. @IBAction func actionCancel(_ sender: UIBarButtonItem) {
  92. self.dismiss(animated: true, completion: nil)
  93. }
  94. }