NCCapabilitiesViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  1. //
  2. // NCCapabilitiesViewController.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 28/07/2020.
  6. // Copyright © 2020 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. import NCCommunication
  25. class NCCapabilitiesViewController: UIViewController, UIDocumentInteractionControllerDelegate {
  26. @IBOutlet weak var textView: UITextView!
  27. @IBOutlet weak var imageFileSharing: UIImageView!
  28. @IBOutlet weak var statusFileSharing: UILabel!
  29. @IBOutlet weak var imageExternalSite: UIImageView!
  30. @IBOutlet weak var statusExternalSite: UILabel!
  31. @IBOutlet weak var imageEndToEndEncryption: UIImageView!
  32. @IBOutlet weak var statusEndToEndEncryption: UILabel!
  33. @IBOutlet weak var imageActivity: UIImageView!
  34. @IBOutlet weak var statusActivity: UILabel!
  35. @IBOutlet weak var imageNotification: UIImageView!
  36. @IBOutlet weak var statusNotification: UILabel!
  37. @IBOutlet weak var imageDeletedFiles: UIImageView!
  38. @IBOutlet weak var statusDeletedFiles: UILabel!
  39. @IBOutlet weak var imageUserStatus: UIImageView!
  40. @IBOutlet weak var statusUserStatus: UILabel!
  41. @IBOutlet weak var imageComments: UIImageView!
  42. @IBOutlet weak var statusComments: UILabel!
  43. @IBOutlet weak var imageText: UIImageView!
  44. @IBOutlet weak var statusText: UILabel!
  45. @IBOutlet weak var imageCollabora: UIImageView!
  46. @IBOutlet weak var statusCollabora: UILabel!
  47. @IBOutlet weak var imageOnlyOffice: UIImageView!
  48. @IBOutlet weak var statusOnlyOffice: UILabel!
  49. @IBOutlet weak var homeImage: UIImageView!
  50. @IBOutlet weak var homeServer: UILabel!
  51. @IBOutlet weak var imageLockFile: UIImageView!
  52. @IBOutlet weak var statusLockFile: UILabel!
  53. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  54. private var documentController: UIDocumentInteractionController?
  55. private var account: String = ""
  56. private var capabilitiesText = ""
  57. // private var timer: Timer?
  58. // MARK: - View Life Cycle
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. self.title = NSLocalizedString("_capabilities_", comment: "")
  62. let shareImage = UIImage(named: "shareFill")!.image(color: .gray, size: 25)
  63. self.navigationItem.rightBarButtonItem = UIBarButtonItem(image: shareImage, style: UIBarButtonItem.Style.plain, target: self, action: #selector(share))
  64. self.navigationItem.leftBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_done_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(close))
  65. textView.layer.cornerRadius = 15
  66. textView.layer.masksToBounds = true
  67. statusFileSharing.layer.cornerRadius = 12.5
  68. statusFileSharing.layer.borderWidth = 0.5
  69. statusFileSharing.layer.borderColor = UIColor.gray.cgColor
  70. statusFileSharing.layer.masksToBounds = true
  71. statusExternalSite.layer.cornerRadius = 12.5
  72. statusExternalSite.layer.borderWidth = 0.5
  73. statusExternalSite.layer.borderColor = UIColor.gray.cgColor
  74. statusExternalSite.layer.masksToBounds = true
  75. statusEndToEndEncryption.layer.cornerRadius = 12.5
  76. statusEndToEndEncryption.layer.borderWidth = 0.5
  77. statusEndToEndEncryption.layer.borderColor = UIColor.gray.cgColor
  78. statusEndToEndEncryption.layer.masksToBounds = true
  79. statusActivity.layer.cornerRadius = 12.5
  80. statusActivity.layer.borderWidth = 0.5
  81. statusActivity.layer.borderColor = UIColor.gray.cgColor
  82. statusActivity.layer.masksToBounds = true
  83. statusNotification.layer.cornerRadius = 12.5
  84. statusNotification.layer.borderWidth = 0.5
  85. statusNotification.layer.borderColor = UIColor.gray.cgColor
  86. statusNotification.layer.masksToBounds = true
  87. statusDeletedFiles.layer.cornerRadius = 12.5
  88. statusDeletedFiles.layer.borderWidth = 0.5
  89. statusDeletedFiles.layer.borderColor = UIColor.gray.cgColor
  90. statusDeletedFiles.layer.masksToBounds = true
  91. statusText.layer.cornerRadius = 12.5
  92. statusText.layer.borderWidth = 0.5
  93. statusText.layer.borderColor = UIColor.gray.cgColor
  94. statusText.layer.masksToBounds = true
  95. statusCollabora.layer.cornerRadius = 12.5
  96. statusCollabora.layer.borderWidth = 0.5
  97. statusCollabora.layer.borderColor = UIColor.gray.cgColor
  98. statusCollabora.layer.masksToBounds = true
  99. statusOnlyOffice.layer.cornerRadius = 12.5
  100. statusOnlyOffice.layer.borderWidth = 0.5
  101. statusOnlyOffice.layer.borderColor = UIColor.gray.cgColor
  102. statusOnlyOffice.layer.masksToBounds = true
  103. statusUserStatus.layer.cornerRadius = 12.5
  104. statusUserStatus.layer.borderWidth = 0.5
  105. statusUserStatus.layer.borderColor = UIColor.gray.cgColor
  106. statusUserStatus.layer.masksToBounds = true
  107. statusComments.layer.cornerRadius = 12.5
  108. statusComments.layer.borderWidth = 0.5
  109. statusComments.layer.borderColor = UIColor.gray.cgColor
  110. statusComments.layer.masksToBounds = true
  111. statusLockFile.layer.cornerRadius = 12.5
  112. statusLockFile.layer.borderWidth = 0.5
  113. statusLockFile.layer.borderColor = UIColor.gray.cgColor
  114. statusLockFile.layer.masksToBounds = true
  115. imageFileSharing.image = UIImage(named: "share")!.image(color: NCBrandColor.shared.gray, size: 50)
  116. imageExternalSite.image = NCUtility.shared.loadImage(named: "network", color: NCBrandColor.shared.gray)
  117. imageEndToEndEncryption.image = NCUtility.shared.loadImage(named: "lock", color: NCBrandColor.shared.gray)
  118. imageActivity.image = UIImage(named: "bolt")!.image(color: NCBrandColor.shared.gray, size: 50)
  119. imageNotification.image = NCUtility.shared.loadImage(named: "bell", color: NCBrandColor.shared.gray)
  120. imageDeletedFiles.image = NCUtility.shared.loadImage(named: "trash", color: NCBrandColor.shared.gray)
  121. imageText.image = UIImage(named: "text")!.image(color: NCBrandColor.shared.gray, size: 50)
  122. imageCollabora.image = UIImage(named: "collabora")!.image(color: NCBrandColor.shared.gray, size: 50)
  123. imageOnlyOffice.image = UIImage(named: "onlyoffice")!.image(color: NCBrandColor.shared.gray, size: 50)
  124. imageUserStatus.image = UIImage(named: "userStatusAway")!.image(color: NCBrandColor.shared.gray, size: 50)
  125. imageComments.image = UIImage(named: "comments")!.image(color: NCBrandColor.shared.gray, size: 50)
  126. imageLockFile.image = UIImage(named: "lock")!.image(color: NCBrandColor.shared.gray, size: 50)
  127. guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else { return }
  128. self.account = activeAccount.account
  129. if let text = NCManageDatabase.shared.getCapabilities(account: activeAccount.account) {
  130. capabilitiesText = text
  131. updateCapabilities()
  132. } else {
  133. NCContentPresenter.shared.messageNotification("_error_", description: "_no_capabilities_found_", delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.info, errorCode: NCGlobal.shared.errorInternalError, priority: .max)
  134. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  135. self.dismiss(animated: true, completion: nil)
  136. }
  137. }
  138. homeImage.image = UIImage(named: "home")!.image(color: NCBrandColor.shared.gray, size: 50)
  139. homeServer.text = NCUtilityFileSystem.shared.getHomeServer(account: appDelegate.account) + "/"
  140. }
  141. @objc func updateCapabilities() {
  142. NCCommunication.shared.getCapabilities { account, data, errorCode, _ in
  143. if errorCode == 0 && data != nil {
  144. NCManageDatabase.shared.addCapabilitiesJSon(data!, account: account)
  145. // EDITORS
  146. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  147. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion18 {
  148. NCCommunication.shared.NCTextObtainEditorDetails { account, editors, creators, errorCode, _ in
  149. if errorCode == 0 && account == self.appDelegate.account {
  150. NCManageDatabase.shared.addDirectEditing(account: account, editors: editors, creators: creators)
  151. self.readCapabilities()
  152. }
  153. if self.view.window != nil {
  154. // self.timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.updateCapabilities), userInfo: nil, repeats: false)
  155. }
  156. }
  157. } else {
  158. if self.view.window != nil {
  159. // self.timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.updateCapabilities), userInfo: nil, repeats: false)
  160. }
  161. }
  162. if let text = NCManageDatabase.shared.getCapabilities(account: account) {
  163. self.capabilitiesText = text
  164. }
  165. self.readCapabilities()
  166. }
  167. }
  168. readCapabilities()
  169. }
  170. @objc func share() {
  171. // timer?.invalidate()
  172. self.dismiss(animated: true) {
  173. let fileURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent("capabilities.txt")
  174. do {
  175. try self.capabilitiesText.write(to: fileURL, atomically: true, encoding: .utf8)
  176. if let view = self.appDelegate.window?.rootViewController?.view {
  177. self.documentController = UIDocumentInteractionController(url: fileURL)
  178. self.documentController?.delegate = self
  179. self.documentController?.presentOptionsMenu(from: CGRect.zero, in: view, animated: true)
  180. }
  181. } catch { }
  182. }
  183. }
  184. @objc func close() {
  185. // timer?.invalidate()
  186. self.dismiss(animated: true, completion: nil)
  187. }
  188. func readCapabilities() {
  189. textView.text = capabilitiesText
  190. if NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false) {
  191. statusFileSharing.text = "✓ " + NSLocalizedString("_available_", comment: "")
  192. } else {
  193. statusFileSharing.text = NSLocalizedString("_not_available_", comment: "")
  194. }
  195. if NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesExternalSitesExists, exists: true) {
  196. statusExternalSite.text = "✓ " + NSLocalizedString("_available_", comment: "")
  197. } else {
  198. statusExternalSite.text = NSLocalizedString("_not_available_", comment: "")
  199. }
  200. let isE2EEEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesE2EEEnabled, exists: false)
  201. // let versionE2EE = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesE2EEApiVersion)
  202. if isE2EEEnabled {
  203. statusEndToEndEncryption.text = "✓ " + NSLocalizedString("_available_", comment: "")
  204. } else {
  205. statusEndToEndEncryption.text = NSLocalizedString("_not_available_", comment: "")
  206. }
  207. let activity = NCManageDatabase.shared.getCapabilitiesServerArray(account: account, elements: NCElementsJSON.shared.capabilitiesActivity)
  208. if activity != nil {
  209. statusActivity.text = "✓ " + NSLocalizedString("_available_", comment: "")
  210. } else {
  211. statusActivity.text = NSLocalizedString("_not_available_", comment: "")
  212. }
  213. let notification = NCManageDatabase.shared.getCapabilitiesServerArray(account: account, elements: NCElementsJSON.shared.capabilitiesNotification)
  214. if notification != nil {
  215. statusNotification.text = "✓ " + NSLocalizedString("_available_", comment: "")
  216. } else {
  217. statusNotification.text = NSLocalizedString("_not_available_", comment: "")
  218. }
  219. let deleteFiles = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFilesUndelete, exists: false)
  220. if deleteFiles {
  221. statusDeletedFiles.text = "✓ " + NSLocalizedString("_available_", comment: "")
  222. } else {
  223. statusDeletedFiles.text = NSLocalizedString("_not_available_", comment: "")
  224. }
  225. var textEditor = false
  226. var onlyofficeEditors = false
  227. if let editors = NCManageDatabase.shared.getDirectEditingEditors(account: account) {
  228. for editor in editors {
  229. if editor.editor == NCGlobal.shared.editorText {
  230. textEditor = true
  231. } else if editor.editor == NCGlobal.shared.editorOnlyoffice {
  232. onlyofficeEditors = true
  233. }
  234. }
  235. }
  236. if textEditor {
  237. statusText.text = "✓ " + NSLocalizedString("_available_", comment: "")
  238. } else {
  239. statusText.text = NSLocalizedString("_not_available_", comment: "")
  240. }
  241. let richdocumentsMimetypes = NCManageDatabase.shared.getCapabilitiesServerArray(account: account, elements: NCElementsJSON.shared.capabilitiesRichdocumentsMimetypes)
  242. if richdocumentsMimetypes != nil {
  243. statusCollabora.text = "✓ " + NSLocalizedString("_available_", comment: "")
  244. } else {
  245. statusCollabora.text = NSLocalizedString("_not_available_", comment: "")
  246. }
  247. if onlyofficeEditors {
  248. statusOnlyOffice.text = "✓ " + NSLocalizedString("_available_", comment: "")
  249. } else {
  250. statusOnlyOffice.text = NSLocalizedString("_not_available_", comment: "")
  251. }
  252. let userStatus = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesUserStatusEnabled, exists: false)
  253. if userStatus {
  254. statusUserStatus.text = "✓ " + NSLocalizedString("_available_", comment: "")
  255. } else {
  256. statusUserStatus.text = NSLocalizedString("_not_available_", comment: "")
  257. }
  258. let comments = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFilesComments, exists: false)
  259. if comments {
  260. statusComments.text = "✓ " + NSLocalizedString("_available_", comment: "")
  261. } else {
  262. statusComments.text = NSLocalizedString("_not_available_", comment: "")
  263. }
  264. let hasLockCapability = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFilesLockVersion) >= 1
  265. if hasLockCapability {
  266. statusLockFile.text = "✓ " + NSLocalizedString("_available_", comment: "")
  267. } else {
  268. statusLockFile.text = NSLocalizedString("_not_available_", comment: "")
  269. }
  270. print("end.")
  271. }
  272. }