AppDelegate+Menu.swift 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  32. let directEditingCreators = NCManageDatabase.shared.getDirectEditingCreators(account: appDelegate.account)
  33. let isDirectoryE2EE = NCUtilityFileSystem().isDirectoryE2EE(serverUrl: appDelegate.activeServerUrl, userBase: appDelegate)
  34. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, appDelegate.activeServerUrl))
  35. actions.append(
  36. NCMenuAction(
  37. title: NSLocalizedString("_upload_photos_videos_", comment: ""), icon: UIImage(named: "file_photo")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  38. NCAskAuthorization().askAuthorizationPhotoLibrary(viewController: viewController) { hasPermission in
  39. if hasPermission {NCPhotosPickerViewController(viewController: viewController, maxSelectedAssets: 0, singleSelectedMode: false)
  40. }
  41. }
  42. }
  43. )
  44. )
  45. actions.append(
  46. NCMenuAction(
  47. title: NSLocalizedString("_upload_file_", comment: ""), icon: UIImage(named: "file")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  48. if let tabBarController = self.window?.rootViewController as? UITabBarController {
  49. self.documentPickerViewController = NCDocumentPickerViewController(tabBarController: tabBarController, isViewerMedia: false, allowsMultipleSelection: true)
  50. }
  51. }
  52. )
  53. )
  54. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorText}) && !isDirectoryE2EE {
  55. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorText})!
  56. actions.append(
  57. NCMenuAction(title: NSLocalizedString("_create_nextcloudtext_document_", comment: ""), icon: UIImage(named: "file_txt")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  58. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  59. return
  60. }
  61. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  62. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  63. viewController.editorId = NCGlobal.shared.editorText
  64. viewController.creatorId = directEditingCreator.identifier
  65. viewController.typeTemplate = NCGlobal.shared.templateDocument
  66. viewController.serverUrl = appDelegate.activeServerUrl
  67. viewController.titleForm = NSLocalizedString("_create_nextcloudtext_document_", comment: "")
  68. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  69. }
  70. })
  71. )
  72. }
  73. actions.append(
  74. NCMenuAction(
  75. title: NSLocalizedString("_scans_document_", comment: ""), icon: NCUtility().loadImage(named: "doc.text.viewfinder"), action: { _ in
  76. if let viewController = appDelegate.window?.rootViewController {
  77. NCDocumentCamera.shared.openScannerDocument(viewController: viewController)
  78. }
  79. }
  80. )
  81. )
  82. actions.append(
  83. NCMenuAction(
  84. title: NSLocalizedString("_create_voice_memo_", comment: ""), icon: UIImage(named: "microphone")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  85. NCAskAuthorization().askAuthorizationAudioRecord(viewController: viewController) { hasPermission in
  86. if hasPermission {
  87. let fileName = NCUtilityFileSystem().createFileNameDate(NSLocalizedString("_voice_memo_filename_", comment: ""), ext: "m4a")
  88. if let viewController = UIStoryboard(name: "NCAudioRecorderViewController", bundle: nil).instantiateInitialViewController() as? NCAudioRecorderViewController {
  89. viewController.delegate = self
  90. viewController.createRecorder(fileName: fileName)
  91. viewController.modalTransitionStyle = .crossDissolve
  92. viewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
  93. appDelegate.window?.rootViewController?.present(viewController, animated: true, completion: nil)
  94. }
  95. }
  96. }
  97. }
  98. )
  99. )
  100. if NCKeychain().isEndToEndEnabled(account: appDelegate.account) {
  101. actions.append(.seperator(order: 0))
  102. }
  103. let titleCreateFolder = isDirectoryE2EE ? NSLocalizedString("_create_folder_e2ee_", comment: "") : NSLocalizedString("_create_folder_", comment: "")
  104. let imageCreateFolder = isDirectoryE2EE ? UIImage(named: "folderEncrypted")! : UIImage(named: "folder")!
  105. actions.append(
  106. NCMenuAction(title: titleCreateFolder,
  107. icon: imageCreateFolder.image(color: NCBrandColor.shared.brandElement, size: 50), action: { _ in
  108. guard !appDelegate.activeServerUrl.isEmpty else { return }
  109. let alertController = UIAlertController.createFolder(serverUrl: appDelegate.activeServerUrl, urlBase: appDelegate)
  110. appDelegate.window?.rootViewController?.present(alertController, animated: true, completion: nil)
  111. }
  112. )
  113. )
  114. // Folder encrypted
  115. if !isDirectoryE2EE && NCKeychain().isEndToEndEnabled(account: appDelegate.account) {
  116. actions.append(
  117. NCMenuAction(title: NSLocalizedString("_create_folder_e2ee_", comment: ""),
  118. icon: UIImage(named: "folderEncrypted")!.image(color: NCBrandColor.shared.brandElement, size: 50),
  119. action: { _ in
  120. guard !appDelegate.activeServerUrl.isEmpty else { return }
  121. let alertController = UIAlertController.createFolder(serverUrl: appDelegate.activeServerUrl, urlBase: appDelegate, markE2ee: true)
  122. appDelegate.window?.rootViewController?.present(alertController, animated: true, completion: nil)
  123. })
  124. )
  125. }
  126. if NCKeychain().isEndToEndEnabled(account: appDelegate.account) {
  127. actions.append(.seperator(order: 0))
  128. }
  129. if NCGlobal.shared.capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion18 && directory?.richWorkspace == nil && !isDirectoryE2EE && NextcloudKit.shared.isNetworkReachable() {
  130. actions.append(
  131. NCMenuAction(
  132. title: NSLocalizedString("_add_folder_info_", comment: ""), icon: UIImage(named: "addFolderInfo")!.image(color: UIColor.systemGray, size: 50), action: { _ in
  133. let richWorkspaceCommon = NCRichWorkspaceCommon()
  134. if let viewController = self.activeViewController {
  135. if NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", appDelegate.account, appDelegate.activeServerUrl, NCGlobal.shared.fileNameRichWorkspace.lowercased())) == nil {
  136. richWorkspaceCommon.createViewerNextcloudText(serverUrl: appDelegate.activeServerUrl, viewController: viewController)
  137. } else {
  138. richWorkspaceCommon.openViewerNextcloudText(serverUrl: appDelegate.activeServerUrl, viewController: viewController)
  139. }
  140. }
  141. }
  142. )
  143. )
  144. }
  145. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeDocx}) && !isDirectoryE2EE {
  146. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeDocx})!
  147. actions.append(
  148. NCMenuAction(
  149. title: NSLocalizedString("_create_new_document_", comment: ""), icon: UIImage(named: "create_file_document")!, action: { _ in
  150. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  151. return
  152. }
  153. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  154. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  155. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  156. viewController.creatorId = directEditingCreator.identifier
  157. viewController.typeTemplate = NCGlobal.shared.templateDocument
  158. viewController.serverUrl = appDelegate.activeServerUrl
  159. viewController.titleForm = NSLocalizedString("_create_new_document_", comment: "")
  160. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  161. }
  162. }
  163. )
  164. )
  165. }
  166. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeXlsx}) && !isDirectoryE2EE {
  167. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeXlsx})!
  168. actions.append(
  169. NCMenuAction(
  170. title: NSLocalizedString("_create_new_spreadsheet_", comment: ""), icon: UIImage(named: "create_file_xls")!, action: { _ in
  171. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  172. return
  173. }
  174. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  175. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  176. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  177. viewController.creatorId = directEditingCreator.identifier
  178. viewController.typeTemplate = NCGlobal.shared.templateSpreadsheet
  179. viewController.serverUrl = appDelegate.activeServerUrl
  180. viewController.titleForm = NSLocalizedString("_create_new_spreadsheet_", comment: "")
  181. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  182. }
  183. }
  184. )
  185. )
  186. }
  187. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficePptx}) && !isDirectoryE2EE {
  188. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficePptx})!
  189. actions.append(
  190. NCMenuAction(
  191. title: NSLocalizedString("_create_new_presentation_", comment: ""), icon: UIImage(named: "create_file_ppt")!, action: { _ in
  192. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  193. return
  194. }
  195. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  196. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  197. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  198. viewController.creatorId = directEditingCreator.identifier
  199. viewController.typeTemplate = NCGlobal.shared.templatePresentation
  200. viewController.serverUrl = appDelegate.activeServerUrl
  201. viewController.titleForm = NSLocalizedString("_create_new_presentation_", comment: "")
  202. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  203. }
  204. }
  205. )
  206. )
  207. }
  208. if NCGlobal.shared.capabilityRichdocumentsEnabled {
  209. if NextcloudKit.shared.isNetworkReachable() && !isDirectoryE2EE {
  210. actions.append(
  211. NCMenuAction(
  212. title: NSLocalizedString("_create_new_document_", comment: ""), icon: UIImage(named: "create_file_document")!, action: { _ in
  213. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  214. return
  215. }
  216. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  217. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  218. viewController.editorId = NCGlobal.shared.editorCollabora
  219. viewController.typeTemplate = NCGlobal.shared.templateDocument
  220. viewController.serverUrl = appDelegate.activeServerUrl
  221. viewController.titleForm = NSLocalizedString("_create_nextcloudtext_document_", comment: "")
  222. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  223. }
  224. }
  225. )
  226. )
  227. actions.append(
  228. NCMenuAction(
  229. title: NSLocalizedString("_create_new_spreadsheet_", comment: ""), icon: UIImage(named: "create_file_xls")!, action: { _ in
  230. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  231. return
  232. }
  233. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  234. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  235. viewController.editorId = NCGlobal.shared.editorCollabora
  236. viewController.typeTemplate = NCGlobal.shared.templateSpreadsheet
  237. viewController.serverUrl = appDelegate.activeServerUrl
  238. viewController.titleForm = NSLocalizedString("_create_new_spreadsheet_", comment: "")
  239. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  240. }
  241. }
  242. )
  243. )
  244. actions.append(
  245. NCMenuAction(
  246. title: NSLocalizedString("_create_new_presentation_", comment: ""), icon: UIImage(named: "create_file_ppt")!, action: { _ in
  247. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  248. return
  249. }
  250. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  251. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  252. viewController.editorId = NCGlobal.shared.editorCollabora
  253. viewController.typeTemplate = NCGlobal.shared.templatePresentation
  254. viewController.serverUrl = appDelegate.activeServerUrl
  255. viewController.titleForm = NSLocalizedString("_create_new_presentation_", comment: "")
  256. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  257. }
  258. }
  259. )
  260. )
  261. }
  262. }
  263. viewController.presentMenu(with: actions)
  264. }
  265. }