AppDelegate+Menu.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  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 FloatingPanel
  26. import NCCommunication
  27. extension AppDelegate {
  28. func toggleMenu(viewController: UIViewController) {
  29. let menuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu
  30. var actions: [NCMenuAction] = []
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. let directEditingCreators = NCManageDatabase.shared.getDirectEditingCreators(account: appDelegate.account)
  33. let isEncrypted = CCUtility.isFolderEncrypted(appDelegate.activeServerUrl, e2eEncrypted: false, account: appDelegate.account, urlBase: appDelegate.urlBase)
  34. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, appDelegate.activeServerUrl))
  35. let serverVersionMajor = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesVersionMajor)
  36. actions.append(
  37. NCMenuAction(
  38. title: NSLocalizedString("_upload_photos_videos_", comment: ""), icon: UIImage(named: "file_photo")!.image(color: NCBrandColor.shared.icon, size: 50), action: { menuAction in
  39. NCAskAuthorization.shared.askAuthorizationPhotoLibrary(viewController: viewController) { (hasPermission) in
  40. if hasPermission {
  41. if let viewController = appDelegate.window?.rootViewController {
  42. NCPhotosPickerViewController.init(viewController: viewController, maxSelectedAssets: 0, singleSelectedMode: false)
  43. }
  44. }
  45. }
  46. }
  47. )
  48. )
  49. actions.append(
  50. NCMenuAction(
  51. title: NSLocalizedString("_upload_file_", comment: ""), icon: UIImage(named: "file")!.image(color: NCBrandColor.shared.icon, size: 50), action: { menuAction in
  52. if let tabBarController = self.window?.rootViewController as? UITabBarController {
  53. self.documentPickerViewController = NCDocumentPickerViewController.init(tabBarController: tabBarController)
  54. }
  55. }
  56. )
  57. )
  58. if NCCommunication.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorText}) && !isEncrypted {
  59. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorText})!
  60. actions.append(
  61. NCMenuAction(title: NSLocalizedString("_create_nextcloudtext_document_", comment: ""), icon: UIImage(named: "file_txt")!.image(color: NCBrandColor.shared.icon, size: 50), action: { menuAction in
  62. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  63. return
  64. }
  65. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  66. let viewController = (navigationController as! UINavigationController).topViewController as! NCCreateFormUploadDocuments
  67. viewController.editorId = NCGlobal.shared.editorText
  68. viewController.creatorId = directEditingCreator.identifier
  69. viewController.typeTemplate = NCGlobal.shared.templateDocument
  70. viewController.serverUrl = appDelegate.activeServerUrl
  71. viewController.titleForm = NSLocalizedString("_create_nextcloudtext_document_", comment: "")
  72. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  73. })
  74. )
  75. }
  76. if #available(iOS 13.0, *) {
  77. actions.append(
  78. NCMenuAction(
  79. title: NSLocalizedString("_scans_document_", comment: ""), icon: NCCollectionCommon.shared.loadImage(named: "doc.text.viewfinder"), action: { menuAction in
  80. if let viewController = appDelegate.window?.rootViewController {
  81. NCCreateScanDocument.shared.openScannerDocument(viewController: viewController)
  82. }
  83. }
  84. )
  85. )
  86. }
  87. actions.append(
  88. NCMenuAction(
  89. title: NSLocalizedString("_create_voice_memo_", comment: ""), icon: UIImage(named: "microphone")!.image(color: NCBrandColor.shared.icon, size: 50), action: { menuAction in
  90. NCAskAuthorization.shared.askAuthorizationAudioRecord(viewController: viewController) { (hasPermission) in
  91. if hasPermission {
  92. let fileName = CCUtility.createFileNameDate(NSLocalizedString("_voice_memo_filename_", comment: ""), extension: "m4a")!
  93. let viewController = UIStoryboard(name: "NCAudioRecorderViewController", bundle: nil).instantiateInitialViewController() as! NCAudioRecorderViewController
  94. viewController.delegate = self
  95. viewController.createRecorder(fileName: fileName)
  96. viewController.modalTransitionStyle = .crossDissolve
  97. viewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
  98. appDelegate.window?.rootViewController?.present(viewController, animated: true, completion: nil)
  99. }
  100. }
  101. }
  102. )
  103. )
  104. actions.append(
  105. NCMenuAction(title: NSLocalizedString("_create_folder_", comment: ""),
  106. icon: UIImage(named: "folder")!.image(color: NCBrandColor.shared.brandElement, size: 50), action: { menuAction in
  107. if appDelegate.activeServerUrl == "" { return }
  108. let alertController = UIAlertController(title: NSLocalizedString("_create_folder_on_", comment: ""), message: nil, preferredStyle: .alert)
  109. alertController.addTextField { (textField) in
  110. textField.autocapitalizationType = UITextAutocapitalizationType.sentences
  111. }
  112. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  113. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  114. if let fileNameFolder = alertController.textFields?.first?.text {
  115. NCNetworking.shared.createFolder(fileName: fileNameFolder, serverUrl: appDelegate.activeServerUrl, account: appDelegate.account, urlBase: appDelegate.urlBase, overwrite: false) { (errorCode, errorDescription) in
  116. if errorCode != 0 {
  117. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  118. }
  119. }
  120. }
  121. })
  122. alertController.addAction(cancelAction)
  123. alertController.addAction(okAction)
  124. appDelegate.window?.rootViewController?.present(alertController, animated: true, completion: nil)
  125. }
  126. )
  127. )
  128. if serverVersionMajor >= NCGlobal.shared.nextcloudVersion18 && directory?.richWorkspace == nil && !isEncrypted && NCCommunication.shared.isNetworkReachable() {
  129. actions.append(
  130. NCMenuAction(
  131. title: NSLocalizedString("_add_folder_info_", comment: ""), icon: UIImage(named: "addFolderInfo")!.image(color: NCBrandColor.shared.icon, size: 50), action: { menuAction in
  132. let richWorkspaceCommon = NCRichWorkspaceCommon()
  133. if let viewController = self.activeViewController {
  134. if NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", appDelegate.account, appDelegate.activeServerUrl, NCGlobal.shared.fileNameRichWorkspace.lowercased())) == nil {
  135. richWorkspaceCommon.createViewerNextcloudText(serverUrl: appDelegate.activeServerUrl, viewController: viewController)
  136. } else {
  137. richWorkspaceCommon.openViewerNextcloudText(serverUrl: appDelegate.activeServerUrl, viewController: viewController)
  138. }
  139. }
  140. }
  141. )
  142. )
  143. }
  144. if NCCommunication.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeDocx}) && !isEncrypted {
  145. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeDocx})!
  146. actions.append(
  147. NCMenuAction(
  148. title: NSLocalizedString("_create_new_document_", comment: ""), icon: UIImage(named: "create_file_document")!, action: { menuAction in
  149. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  150. return
  151. }
  152. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  153. let viewController = (navigationController as! UINavigationController).topViewController as! NCCreateFormUploadDocuments
  154. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  155. viewController.creatorId = directEditingCreator.identifier
  156. viewController.typeTemplate = NCGlobal.shared.templateDocument
  157. viewController.serverUrl = appDelegate.activeServerUrl
  158. viewController.titleForm = NSLocalizedString("_create_new_document_", comment: "")
  159. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  160. }
  161. )
  162. )
  163. }
  164. if NCCommunication.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeXlsx}) && !isEncrypted {
  165. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeXlsx})!
  166. actions.append(
  167. NCMenuAction(
  168. title: NSLocalizedString("_create_new_spreadsheet_", comment: ""), icon: UIImage(named: "create_file_xls")!, action: { menuAction in
  169. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  170. return
  171. }
  172. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  173. let viewController = (navigationController as! UINavigationController).topViewController as! NCCreateFormUploadDocuments
  174. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  175. viewController.creatorId = directEditingCreator.identifier
  176. viewController.typeTemplate = NCGlobal.shared.templateSpreadsheet
  177. viewController.serverUrl = appDelegate.activeServerUrl
  178. viewController.titleForm = NSLocalizedString("_create_new_spreadsheet_", comment: "")
  179. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  180. }
  181. )
  182. )
  183. }
  184. if NCCommunication.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficePptx}) && !isEncrypted {
  185. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficePptx})!
  186. actions.append(
  187. NCMenuAction(
  188. title: NSLocalizedString("_create_new_presentation_", comment: ""), icon: UIImage(named: "create_file_ppt")!, action: { menuAction in
  189. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  190. return
  191. }
  192. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  193. let viewController = (navigationController as! UINavigationController).topViewController as! NCCreateFormUploadDocuments
  194. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  195. viewController.creatorId = directEditingCreator.identifier
  196. viewController.typeTemplate = NCGlobal.shared.templatePresentation
  197. viewController.serverUrl = appDelegate.activeServerUrl
  198. viewController.titleForm = NSLocalizedString("_create_new_presentation_", comment: "")
  199. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  200. }
  201. )
  202. )
  203. }
  204. if let richdocumentsMimetypes = NCManageDatabase.shared.getCapabilitiesServerArray(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesRichdocumentsMimetypes) {
  205. if richdocumentsMimetypes.count > 0 && NCCommunication.shared.isNetworkReachable() && !isEncrypted {
  206. actions.append(
  207. NCMenuAction(
  208. title: NSLocalizedString("_create_new_document_", comment: ""), icon: UIImage(named: "create_file_document")!, action: { menuAction in
  209. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  210. return
  211. }
  212. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  213. let viewController = (navigationController as! UINavigationController).topViewController as! NCCreateFormUploadDocuments
  214. viewController.editorId = NCGlobal.shared.editorCollabora
  215. viewController.typeTemplate = NCGlobal.shared.templateDocument
  216. viewController.serverUrl = appDelegate.activeServerUrl
  217. viewController.titleForm = NSLocalizedString("_create_nextcloudtext_document_", comment: "")
  218. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  219. }
  220. )
  221. )
  222. actions.append(
  223. NCMenuAction(
  224. title: NSLocalizedString("_create_new_spreadsheet_", comment: ""), icon: UIImage(named: "create_file_xls")!, action: { menuAction in
  225. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  226. return
  227. }
  228. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  229. let viewController = (navigationController as! UINavigationController).topViewController as! NCCreateFormUploadDocuments
  230. viewController.editorId = NCGlobal.shared.editorCollabora
  231. viewController.typeTemplate = NCGlobal.shared.templateSpreadsheet
  232. viewController.serverUrl = appDelegate.activeServerUrl
  233. viewController.titleForm = NSLocalizedString("_create_new_spreadsheet_", comment: "")
  234. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  235. }
  236. )
  237. )
  238. actions.append(
  239. NCMenuAction(
  240. title: NSLocalizedString("_create_new_presentation_", comment: ""), icon: UIImage(named: "create_file_ppt")!, action: { menuAction in
  241. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  242. return
  243. }
  244. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  245. let viewController = (navigationController as! UINavigationController).topViewController as! NCCreateFormUploadDocuments
  246. viewController.editorId = NCGlobal.shared.editorCollabora
  247. viewController.typeTemplate = NCGlobal.shared.templatePresentation
  248. viewController.serverUrl = appDelegate.activeServerUrl
  249. viewController.titleForm = NSLocalizedString("_create_new_presentation_", comment: "")
  250. appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  251. }
  252. )
  253. )
  254. }
  255. }
  256. menuViewController.actions = actions
  257. let menuPanelController = NCMenuPanelController()
  258. menuPanelController.parentPresenter = viewController
  259. menuPanelController.delegate = menuViewController
  260. menuPanelController.set(contentViewController: menuViewController)
  261. menuPanelController.track(scrollView: menuViewController.tableView)
  262. viewController.present(menuPanelController, animated: true, completion: nil)
  263. }
  264. }