AppDelegate+Menu.swift 20 KB

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