AppDelegate+Menu.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. //
  2. // AppDelegate+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by Philippe Weidmann on 24.01.20.
  6. // Copyright © 2020 Philippe Weidmann. All rights reserved.
  7. // Copyright © 2020 Marino Faggiana All rights reserved.
  8. //
  9. // Author Philippe Weidmann <philippe.weidmann@infomaniak.com>
  10. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import UIKit
  26. import FloatingPanel
  27. import NextcloudKit
  28. extension AppDelegate {
  29. func toggleMenu(viewController: UIViewController) {
  30. var actions: [NCMenuAction] = []
  31. // swiftlint:disable force_cast
  32. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  33. // swiftlint:enable force_cast
  34. let directEditingCreators = NCManageDatabase.shared.getDirectEditingCreators(account: appDelegate.account)
  35. let isDirectoryE2EE = NCUtility.shared.isDirectoryE2EE(serverUrl: appDelegate.activeServerUrl, userBase: appDelegate)
  36. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, appDelegate.activeServerUrl))
  37. actions.append(
  38. NCMenuAction(
  39. title: NSLocalizedString("_upload_photos_videos_", comment: ""), icon: UIImage(named: "file_photo")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  40. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: viewController) { hasPermission in
  41. if hasPermission {NCPhotosPickerViewController(viewController: viewController, maxSelectedAssets: 0, singleSelectedMode: false)
  42. }
  43. }
  44. }
  45. )
  46. )
  47. actions.append(
  48. NCMenuAction(
  49. title: NSLocalizedString("_upload_file_", comment: ""), icon: UIImage(named: "file")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  50. if let tabBarController = self.window?.rootViewController as? UITabBarController {
  51. self.documentPickerViewController = NCDocumentPickerViewController(tabBarController: tabBarController, isViewerMedia: false, allowsMultipleSelection: true)
  52. }
  53. }
  54. )
  55. )
  56. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorText}) && !isDirectoryE2EE {
  57. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorText})!
  58. actions.append(
  59. NCMenuAction(title: NSLocalizedString("_create_nextcloudtext_document_", comment: ""), icon: UIImage(named: "file_txt")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  60. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  61. return
  62. }
  63. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  64. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  65. viewController.editorId = NCGlobal.shared.editorText
  66. viewController.creatorId = directEditingCreator.identifier
  67. viewController.typeTemplate = NCGlobal.shared.templateDocument
  68. viewController.serverUrl = appDelegate.activeServerUrl
  69. viewController.titleForm = NSLocalizedString("_create_nextcloudtext_document_", comment: "")
  70. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  71. }
  72. })
  73. )
  74. }
  75. actions.append(
  76. NCMenuAction(
  77. title: NSLocalizedString("_scans_document_", comment: ""), icon: NCUtility.shared.loadImage(named: "doc.text.viewfinder"), action: { _ in
  78. if let viewController = appDelegate.window?.rootViewController {
  79. NCDocumentCamera.shared.openScannerDocument(viewController: viewController)
  80. }
  81. }
  82. )
  83. )
  84. actions.append(
  85. NCMenuAction(
  86. title: NSLocalizedString("_create_voice_memo_", comment: ""), icon: UIImage(named: "microphone")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  87. NCAskAuthorization.shared.askAuthorizationAudioRecord(viewController: viewController) { hasPermission in
  88. if hasPermission {
  89. let fileName = CCUtility.createFileNameDate(NSLocalizedString("_voice_memo_filename_", comment: ""), extension: "m4a")!
  90. if let viewController = UIStoryboard(name: "NCAudioRecorderViewController", bundle: nil).instantiateInitialViewController() as? NCAudioRecorderViewController {
  91. viewController.delegate = self
  92. viewController.createRecorder(fileName: fileName)
  93. viewController.modalTransitionStyle = .crossDissolve
  94. viewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
  95. appDelegate.window?.rootViewController?.present(viewController, animated: true, completion: nil)
  96. }
  97. }
  98. }
  99. }
  100. )
  101. )
  102. if CCUtility.isEnd(toEndEnabled: appDelegate.account) {
  103. actions.append(.seperator(order: 0))
  104. }
  105. let titleCreateFolder = isDirectoryE2EE ? NSLocalizedString("_create_folder_e2ee_", comment: "") : NSLocalizedString("_create_folder_", comment: "")
  106. let imageCreateFolder = isDirectoryE2EE ? UIImage(named: "folderEncrypted")! : UIImage(named: "folder")!
  107. actions.append(
  108. NCMenuAction(title: titleCreateFolder,
  109. icon: imageCreateFolder.image(color: NCBrandColor.shared.brandElement, size: 50), action: { _ in
  110. guard !appDelegate.activeServerUrl.isEmpty else { return }
  111. let alertController = UIAlertController.createFolder(serverUrl: appDelegate.activeServerUrl, urlBase: appDelegate)
  112. appDelegate.window?.rootViewController?.present(alertController, animated: true, completion: nil)
  113. }
  114. )
  115. )
  116. // Folder encrypted
  117. if !isDirectoryE2EE && CCUtility.isEnd(toEndEnabled: appDelegate.account) {
  118. actions.append(
  119. NCMenuAction(title: NSLocalizedString("_create_folder_e2ee_", comment: ""),
  120. icon: UIImage(named: "folderEncrypted")!.image(color: NCBrandColor.shared.brandElement, size: 50),
  121. action: { _ in
  122. guard !appDelegate.activeServerUrl.isEmpty else { return }
  123. let alertController = UIAlertController.createFolder(serverUrl: appDelegate.activeServerUrl, urlBase: appDelegate, markE2ee: true)
  124. appDelegate.window?.rootViewController?.present(alertController, animated: true, completion: nil)
  125. })
  126. )
  127. }
  128. if CCUtility.isEnd(toEndEnabled: appDelegate.account) {
  129. actions.append(.seperator(order: 0))
  130. }
  131. if NCGlobal.shared.capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion18 && directory?.richWorkspace == nil && !isDirectoryE2EE && NextcloudKit.shared.isNetworkReachable() {
  132. actions.append(
  133. NCMenuAction(
  134. title: NSLocalizedString("_add_folder_info_", comment: ""), icon: UIImage(named: "addFolderInfo")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  135. let richWorkspaceCommon = NCRichWorkspaceCommon()
  136. if let viewController = self.activeViewController {
  137. if NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", appDelegate.account, appDelegate.activeServerUrl, NCGlobal.shared.fileNameRichWorkspace.lowercased())) == nil {
  138. richWorkspaceCommon.createViewerNextcloudText(serverUrl: appDelegate.activeServerUrl, viewController: viewController)
  139. } else {
  140. richWorkspaceCommon.openViewerNextcloudText(serverUrl: appDelegate.activeServerUrl, viewController: viewController)
  141. }
  142. }
  143. }
  144. )
  145. )
  146. }
  147. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeDocx}) && !isDirectoryE2EE {
  148. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeDocx})!
  149. actions.append(
  150. NCMenuAction(
  151. title: NSLocalizedString("_create_new_document_", comment: ""), icon: UIImage(named: "create_file_document")!, action: { _ in
  152. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  153. return
  154. }
  155. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  156. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  157. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  158. viewController.creatorId = directEditingCreator.identifier
  159. viewController.typeTemplate = NCGlobal.shared.templateDocument
  160. viewController.serverUrl = appDelegate.activeServerUrl
  161. viewController.titleForm = NSLocalizedString("_create_new_document_", comment: "")
  162. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  163. }
  164. }
  165. )
  166. )
  167. }
  168. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeXlsx}) && !isDirectoryE2EE {
  169. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeXlsx})!
  170. actions.append(
  171. NCMenuAction(
  172. title: NSLocalizedString("_create_new_spreadsheet_", comment: ""), icon: UIImage(named: "create_file_xls")!, action: { _ in
  173. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  174. return
  175. }
  176. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  177. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  178. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  179. viewController.creatorId = directEditingCreator.identifier
  180. viewController.typeTemplate = NCGlobal.shared.templateSpreadsheet
  181. viewController.serverUrl = appDelegate.activeServerUrl
  182. viewController.titleForm = NSLocalizedString("_create_new_spreadsheet_", comment: "")
  183. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  184. }
  185. }
  186. )
  187. )
  188. }
  189. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficePptx}) && !isDirectoryE2EE {
  190. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficePptx})!
  191. actions.append(
  192. NCMenuAction(
  193. title: NSLocalizedString("_create_new_presentation_", comment: ""), icon: UIImage(named: "create_file_ppt")!, action: { _ in
  194. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  195. return
  196. }
  197. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  198. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  199. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  200. viewController.creatorId = directEditingCreator.identifier
  201. viewController.typeTemplate = NCGlobal.shared.templatePresentation
  202. viewController.serverUrl = appDelegate.activeServerUrl
  203. viewController.titleForm = NSLocalizedString("_create_new_presentation_", comment: "")
  204. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  205. }
  206. }
  207. )
  208. )
  209. }
  210. if !NCGlobal.shared.capabilityRichdocumentsMimetypes.isEmpty {
  211. if NextcloudKit.shared.isNetworkReachable() && !isDirectoryE2EE {
  212. actions.append(
  213. NCMenuAction(
  214. title: NSLocalizedString("_create_new_document_", comment: ""), icon: UIImage(named: "create_file_document")!, action: { _ in
  215. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  216. return
  217. }
  218. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  219. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  220. viewController.editorId = NCGlobal.shared.editorCollabora
  221. viewController.typeTemplate = NCGlobal.shared.templateDocument
  222. viewController.serverUrl = appDelegate.activeServerUrl
  223. viewController.titleForm = NSLocalizedString("_create_nextcloudtext_document_", comment: "")
  224. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  225. }
  226. }
  227. )
  228. )
  229. actions.append(
  230. NCMenuAction(
  231. title: NSLocalizedString("_create_new_spreadsheet_", comment: ""), icon: UIImage(named: "create_file_xls")!, action: { _ in
  232. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  233. return
  234. }
  235. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  236. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  237. viewController.editorId = NCGlobal.shared.editorCollabora
  238. viewController.typeTemplate = NCGlobal.shared.templateSpreadsheet
  239. viewController.serverUrl = appDelegate.activeServerUrl
  240. viewController.titleForm = NSLocalizedString("_create_new_spreadsheet_", comment: "")
  241. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  242. }
  243. }
  244. )
  245. )
  246. actions.append(
  247. NCMenuAction(
  248. title: NSLocalizedString("_create_new_presentation_", comment: ""), icon: UIImage(named: "create_file_ppt")!, action: { _ in
  249. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  250. return
  251. }
  252. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  253. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  254. viewController.editorId = NCGlobal.shared.editorCollabora
  255. viewController.typeTemplate = NCGlobal.shared.templatePresentation
  256. viewController.serverUrl = appDelegate.activeServerUrl
  257. viewController.titleForm = NSLocalizedString("_create_new_presentation_", comment: "")
  258. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  259. }
  260. }
  261. )
  262. )
  263. }
  264. }
  265. viewController.presentMenu(with: actions)
  266. }
  267. }