NCCapabilitiesViewController.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 NextcloudKit
  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. textView.backgroundColor = .secondarySystemBackground
  68. statusFileSharing.layer.cornerRadius = 12.5
  69. statusFileSharing.layer.borderWidth = 0.5
  70. statusFileSharing.layer.borderColor = UIColor.systemGray.cgColor
  71. statusFileSharing.layer.masksToBounds = true
  72. statusFileSharing.backgroundColor = .secondarySystemBackground
  73. statusExternalSite.layer.cornerRadius = 12.5
  74. statusExternalSite.layer.borderWidth = 0.5
  75. statusExternalSite.layer.borderColor = UIColor.systemGray.cgColor
  76. statusExternalSite.layer.masksToBounds = true
  77. statusExternalSite.backgroundColor = .secondarySystemBackground
  78. statusEndToEndEncryption.layer.cornerRadius = 12.5
  79. statusEndToEndEncryption.layer.borderWidth = 0.5
  80. statusEndToEndEncryption.layer.borderColor = UIColor.systemGray.cgColor
  81. statusEndToEndEncryption.layer.masksToBounds = true
  82. statusEndToEndEncryption.backgroundColor = .secondarySystemBackground
  83. statusActivity.layer.cornerRadius = 12.5
  84. statusActivity.layer.borderWidth = 0.5
  85. statusActivity.layer.borderColor = UIColor.systemGray.cgColor
  86. statusActivity.layer.masksToBounds = true
  87. statusActivity.backgroundColor = .secondarySystemBackground
  88. statusNotification.layer.cornerRadius = 12.5
  89. statusNotification.layer.borderWidth = 0.5
  90. statusNotification.layer.borderColor = UIColor.systemGray.cgColor
  91. statusNotification.layer.masksToBounds = true
  92. statusNotification.backgroundColor = .secondarySystemBackground
  93. statusDeletedFiles.layer.cornerRadius = 12.5
  94. statusDeletedFiles.layer.borderWidth = 0.5
  95. statusDeletedFiles.layer.borderColor = UIColor.systemGray.cgColor
  96. statusDeletedFiles.layer.masksToBounds = true
  97. statusDeletedFiles.backgroundColor = .secondarySystemBackground
  98. statusText.layer.cornerRadius = 12.5
  99. statusText.layer.borderWidth = 0.5
  100. statusText.layer.borderColor = UIColor.systemGray.cgColor
  101. statusText.layer.masksToBounds = true
  102. statusText.backgroundColor = .secondarySystemBackground
  103. statusCollabora.layer.cornerRadius = 12.5
  104. statusCollabora.layer.borderWidth = 0.5
  105. statusCollabora.layer.borderColor = UIColor.systemGray.cgColor
  106. statusCollabora.layer.masksToBounds = true
  107. statusCollabora.backgroundColor = .secondarySystemBackground
  108. statusOnlyOffice.layer.cornerRadius = 12.5
  109. statusOnlyOffice.layer.borderWidth = 0.5
  110. statusOnlyOffice.layer.borderColor = UIColor.systemGray.cgColor
  111. statusOnlyOffice.layer.masksToBounds = true
  112. statusOnlyOffice.backgroundColor = .secondarySystemBackground
  113. statusUserStatus.layer.cornerRadius = 12.5
  114. statusUserStatus.layer.borderWidth = 0.5
  115. statusUserStatus.layer.borderColor = UIColor.systemGray.cgColor
  116. statusUserStatus.layer.masksToBounds = true
  117. statusUserStatus.backgroundColor = .secondarySystemBackground
  118. statusComments.layer.cornerRadius = 12.5
  119. statusComments.layer.borderWidth = 0.5
  120. statusComments.layer.borderColor = UIColor.systemGray.cgColor
  121. statusComments.layer.masksToBounds = true
  122. statusComments.backgroundColor = .secondarySystemBackground
  123. statusLockFile.layer.cornerRadius = 12.5
  124. statusLockFile.layer.borderWidth = 0.5
  125. statusLockFile.layer.borderColor = UIColor.systemGray.cgColor
  126. statusLockFile.layer.masksToBounds = true
  127. statusLockFile.backgroundColor = .secondarySystemBackground
  128. imageFileSharing.image = UIImage(named: "share")!.image(color: UIColor.systemGray, size: 50)
  129. imageExternalSite.image = NCUtility.shared.loadImage(named: "network", color: UIColor.systemGray)
  130. imageEndToEndEncryption.image = NCUtility.shared.loadImage(named: "lock", color: UIColor.systemGray)
  131. imageActivity.image = UIImage(named: "bolt")!.image(color: UIColor.systemGray, size: 50)
  132. imageNotification.image = NCUtility.shared.loadImage(named: "bell", color: UIColor.systemGray)
  133. imageDeletedFiles.image = NCUtility.shared.loadImage(named: "trash", color: UIColor.systemGray)
  134. imageText.image = UIImage(named: "text")!.image(color: UIColor.systemGray, size: 50)
  135. imageCollabora.image = UIImage(named: "collabora")!.image(color: UIColor.systemGray, size: 50)
  136. imageOnlyOffice.image = UIImage(named: "onlyoffice")!.image(color: UIColor.systemGray, size: 50)
  137. imageUserStatus.image = UIImage(named: "userStatusAway")!.image(color: UIColor.systemGray, size: 50)
  138. imageComments.image = UIImage(named: "comments")!.image(color: UIColor.systemGray, size: 50)
  139. imageLockFile.image = UIImage(named: "lock")!.image(color: UIColor.systemGray, size: 50)
  140. guard let activeAccount = NCManageDatabase.shared.getActiveAccount() else { return }
  141. self.account = activeAccount.account
  142. if let text = NCManageDatabase.shared.getCapabilities(account: activeAccount.account) {
  143. capabilitiesText = text
  144. updateCapabilities()
  145. } else {
  146. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_no_capabilities_found_")
  147. NCContentPresenter.shared.showError(error: error, priority: .max)
  148. DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
  149. self.dismiss(animated: true, completion: nil)
  150. }
  151. }
  152. homeImage.image = UIImage(named: "home")!.image(color: UIColor.systemGray, size: 50)
  153. homeServer.text = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, userId: appDelegate.userId) + "/"
  154. }
  155. @objc func updateCapabilities() {
  156. NextcloudKit.shared.getCapabilities { account, data, error in
  157. if error == .success && data != nil {
  158. NCManageDatabase.shared.addCapabilitiesJSon(data!, account: account)
  159. // EDITORS
  160. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  161. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion18 {
  162. NextcloudKit.shared.NCTextObtainEditorDetails { account, editors, creators, data, error in
  163. if error == .success && account == self.appDelegate.account {
  164. NCManageDatabase.shared.addDirectEditing(account: account, editors: editors, creators: creators)
  165. self.readCapabilities()
  166. }
  167. if self.view.window != nil {
  168. // self.timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.updateCapabilities), userInfo: nil, repeats: false)
  169. }
  170. }
  171. } else {
  172. if self.view.window != nil {
  173. // self.timer = Timer.scheduledTimer(timeInterval: 5, target: self, selector: #selector(self.updateCapabilities), userInfo: nil, repeats: false)
  174. }
  175. }
  176. if let text = NCManageDatabase.shared.getCapabilities(account: account) {
  177. self.capabilitiesText = text
  178. }
  179. self.readCapabilities()
  180. }
  181. }
  182. readCapabilities()
  183. }
  184. @objc func share() {
  185. // timer?.invalidate()
  186. self.dismiss(animated: true) {
  187. let fileURL = NSURL.fileURL(withPath: NSTemporaryDirectory(), isDirectory: true).appendingPathComponent("capabilities.txt")
  188. do {
  189. try self.capabilitiesText.write(to: fileURL, atomically: true, encoding: .utf8)
  190. if let view = self.appDelegate.window?.rootViewController?.view {
  191. self.documentController = UIDocumentInteractionController(url: fileURL)
  192. self.documentController?.delegate = self
  193. self.documentController?.presentOptionsMenu(from: CGRect.zero, in: view, animated: true)
  194. }
  195. } catch { }
  196. }
  197. }
  198. @objc func close() {
  199. // timer?.invalidate()
  200. self.dismiss(animated: true, completion: nil)
  201. }
  202. func readCapabilities() {
  203. textView.text = capabilitiesText
  204. if NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFileSharingApiEnabled, exists: false) {
  205. statusFileSharing.text = "✓ " + NSLocalizedString("_available_", comment: "")
  206. } else {
  207. statusFileSharing.text = NSLocalizedString("_not_available_", comment: "")
  208. }
  209. if NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesExternalSitesExists, exists: true) {
  210. statusExternalSite.text = "✓ " + NSLocalizedString("_available_", comment: "")
  211. } else {
  212. statusExternalSite.text = NSLocalizedString("_not_available_", comment: "")
  213. }
  214. let isE2EEEnabled = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesE2EEEnabled, exists: false)
  215. // let versionE2EE = NCManageDatabase.shared.getCapabilitiesServerString(account: account, elements: NCElementsJSON.shared.capabilitiesE2EEApiVersion)
  216. if isE2EEEnabled {
  217. statusEndToEndEncryption.text = "✓ " + NSLocalizedString("_available_", comment: "")
  218. } else {
  219. statusEndToEndEncryption.text = NSLocalizedString("_not_available_", comment: "")
  220. }
  221. let activity = NCManageDatabase.shared.getCapabilitiesServerArray(account: account, elements: NCElementsJSON.shared.capabilitiesActivity)
  222. if activity != nil {
  223. statusActivity.text = "✓ " + NSLocalizedString("_available_", comment: "")
  224. } else {
  225. statusActivity.text = NSLocalizedString("_not_available_", comment: "")
  226. }
  227. let notification = NCManageDatabase.shared.getCapabilitiesServerArray(account: account, elements: NCElementsJSON.shared.capabilitiesNotification)
  228. if notification != nil {
  229. statusNotification.text = "✓ " + NSLocalizedString("_available_", comment: "")
  230. } else {
  231. statusNotification.text = NSLocalizedString("_not_available_", comment: "")
  232. }
  233. let deleteFiles = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFilesUndelete, exists: false)
  234. if deleteFiles {
  235. statusDeletedFiles.text = "✓ " + NSLocalizedString("_available_", comment: "")
  236. } else {
  237. statusDeletedFiles.text = NSLocalizedString("_not_available_", comment: "")
  238. }
  239. var textEditor = false
  240. var onlyofficeEditors = false
  241. if let editors = NCManageDatabase.shared.getDirectEditingEditors(account: account) {
  242. for editor in editors {
  243. if editor.editor == NCGlobal.shared.editorText {
  244. textEditor = true
  245. } else if editor.editor == NCGlobal.shared.editorOnlyoffice {
  246. onlyofficeEditors = true
  247. }
  248. }
  249. }
  250. if textEditor {
  251. statusText.text = "✓ " + NSLocalizedString("_available_", comment: "")
  252. } else {
  253. statusText.text = NSLocalizedString("_not_available_", comment: "")
  254. }
  255. let richdocumentsMimetypes = NCManageDatabase.shared.getCapabilitiesServerArray(account: account, elements: NCElementsJSON.shared.capabilitiesRichdocumentsMimetypes)
  256. if richdocumentsMimetypes != nil {
  257. statusCollabora.text = "✓ " + NSLocalizedString("_available_", comment: "")
  258. } else {
  259. statusCollabora.text = NSLocalizedString("_not_available_", comment: "")
  260. }
  261. if onlyofficeEditors {
  262. statusOnlyOffice.text = "✓ " + NSLocalizedString("_available_", comment: "")
  263. } else {
  264. statusOnlyOffice.text = NSLocalizedString("_not_available_", comment: "")
  265. }
  266. let userStatus = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesUserStatusEnabled, exists: false)
  267. if userStatus {
  268. statusUserStatus.text = "✓ " + NSLocalizedString("_available_", comment: "")
  269. } else {
  270. statusUserStatus.text = NSLocalizedString("_not_available_", comment: "")
  271. }
  272. let comments = NCManageDatabase.shared.getCapabilitiesServerBool(account: account, elements: NCElementsJSON.shared.capabilitiesFilesComments, exists: false)
  273. if comments {
  274. statusComments.text = "✓ " + NSLocalizedString("_available_", comment: "")
  275. } else {
  276. statusComments.text = NSLocalizedString("_not_available_", comment: "")
  277. }
  278. let hasLockCapability = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFilesLockVersion) >= 1
  279. if hasLockCapability {
  280. statusLockFile.text = "✓ " + NSLocalizedString("_available_", comment: "")
  281. } else {
  282. statusLockFile.text = NSLocalizedString("_not_available_", comment: "")
  283. }
  284. print("end.")
  285. }
  286. }