AppDelegate+Menu.swift 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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(mainTabBarController: NCMainTabBarController) {
  30. var actions: [NCMenuAction] = []
  31. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  32. let directEditingCreators = NCManageDatabase.shared.getDirectEditingCreators(account: appDelegate.account)
  33. let serverUrl = mainTabBarController.currentServerUrl()
  34. let isDirectoryE2EE = NCUtilityFileSystem().isDirectoryE2EE(serverUrl: serverUrl, userBase: appDelegate)
  35. let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  36. let utility = NCUtility()
  37. actions.append(
  38. NCMenuAction(
  39. title: NSLocalizedString("_upload_photos_videos_", comment: ""), icon: utility.loadImage(named: "photo", colors: [NCBrandColor.shared.iconImageColor]), action: { _ in
  40. NCAskAuthorization().askAuthorizationPhotoLibrary(viewController: mainTabBarController) { hasPermission in
  41. if hasPermission {NCPhotosPickerViewController(mainTabBarController: mainTabBarController, maxSelectedAssets: 0, singleSelectedMode: false)
  42. }
  43. }
  44. }
  45. )
  46. )
  47. actions.append(
  48. NCMenuAction(
  49. title: NSLocalizedString("_upload_file_", comment: ""), icon: utility.loadImage(named: "doc", colors: [NCBrandColor.shared.iconImageColor]), action: { _ in
  50. mainTabBarController.documentPickerViewController = NCDocumentPickerViewController(mainTabBarController: mainTabBarController, isViewerMedia: false, allowsMultipleSelection: true)
  51. }
  52. )
  53. )
  54. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorText}) && !isDirectoryE2EE {
  55. actions.append(
  56. NCMenuAction(title: NSLocalizedString("_create_nextcloudtext_document_", comment: ""), icon: utility.loadImage(named: "doc.text", colors: [NCBrandColor.shared.iconImageColor]), action: { _ in
  57. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorText})!
  58. Task {
  59. let fileName = await NCNetworking.shared.createFileName(fileNameBase: NSLocalizedString("_untitled_", comment: "") + ".md", account: appDelegate.account, serverUrl: serverUrl)
  60. let fileNamePath = NCUtilityFileSystem().getFileNamePath(String(describing: fileName), serverUrl: serverUrl, urlBase: appDelegate.urlBase, userId: appDelegate.userId)
  61. self.createTextDocument(mainTabBarController: mainTabBarController, fileNamePath: fileNamePath, fileName: String(describing: fileName), creatorId: directEditingCreator.identifier)
  62. }
  63. })
  64. )
  65. }
  66. actions.append(
  67. NCMenuAction(
  68. title: NSLocalizedString("_scans_document_", comment: ""), icon: utility.loadImage(named: "doc.text.viewfinder", colors: [NCBrandColor.shared.iconImageColor]), action: { _ in
  69. NCDocumentCamera.shared.openScannerDocument(viewController: mainTabBarController)
  70. }
  71. )
  72. )
  73. actions.append(
  74. NCMenuAction(
  75. title: NSLocalizedString("_create_voice_memo_", comment: ""), icon: utility.loadImage(named: "mic", colors: [NCBrandColor.shared.iconImageColor]), action: { _ in
  76. NCAskAuthorization().askAuthorizationAudioRecord(viewController: mainTabBarController) { hasPermission in
  77. if hasPermission {
  78. if let viewController = UIStoryboard(name: "NCAudioRecorderViewController", bundle: nil).instantiateInitialViewController() as? NCAudioRecorderViewController {
  79. viewController.serverUrl = serverUrl
  80. viewController.modalTransitionStyle = .crossDissolve
  81. viewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
  82. mainTabBarController.present(viewController, animated: true, completion: nil)
  83. }
  84. }
  85. }
  86. }
  87. )
  88. )
  89. if NCKeychain().isEndToEndEnabled(account: appDelegate.account) {
  90. actions.append(.seperator(order: 0))
  91. }
  92. let titleCreateFolder = isDirectoryE2EE ? NSLocalizedString("_create_folder_e2ee_", comment: "") : NSLocalizedString("_create_folder_", comment: "")
  93. let imageCreateFolder = isDirectoryE2EE ? NCImageCache.images.folderEncrypted : NCImageCache.images.folder
  94. actions.append(
  95. NCMenuAction(title: titleCreateFolder,
  96. icon: imageCreateFolder, action: { _ in
  97. let alertController = UIAlertController.createFolder(serverUrl: serverUrl, urlBase: appDelegate, sceneIdentifier: mainTabBarController.sceneIdentifier)
  98. mainTabBarController.present(alertController, animated: true, completion: nil)
  99. }
  100. )
  101. )
  102. // Folder encrypted
  103. if !isDirectoryE2EE && NCKeychain().isEndToEndEnabled(account: appDelegate.account) {
  104. actions.append(
  105. NCMenuAction(title: NSLocalizedString("_create_folder_e2ee_", comment: ""),
  106. icon: NCImageCache.images.folderEncrypted,
  107. action: { _ in
  108. let alertController = UIAlertController.createFolder(serverUrl: serverUrl, urlBase: appDelegate, markE2ee: true, sceneIdentifier: mainTabBarController.sceneIdentifier)
  109. mainTabBarController.present(alertController, animated: true, completion: nil)
  110. })
  111. )
  112. }
  113. if NCKeychain().isEndToEndEnabled(account: appDelegate.account) {
  114. actions.append(.seperator(order: 0))
  115. }
  116. if NCGlobal.shared.capabilityServerVersionMajor >= NCGlobal.shared.nextcloudVersion18 && directory?.richWorkspace == nil && !isDirectoryE2EE && NextcloudKit.shared.isNetworkReachable() {
  117. actions.append(
  118. NCMenuAction(
  119. title: NSLocalizedString("_add_folder_info_", comment: ""), icon: NCUtility().loadImage(named: "list.dash.header.rectangle", colors: [NCBrandColor.shared.iconImageColor]), action: { _ in
  120. let richWorkspaceCommon = NCRichWorkspaceCommon()
  121. if let viewController = mainTabBarController.currentViewController() {
  122. if NCManageDatabase.shared.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView LIKE[c] %@", appDelegate.account, serverUrl, NCGlobal.shared.fileNameRichWorkspace.lowercased())) == nil {
  123. richWorkspaceCommon.createViewerNextcloudText(serverUrl: serverUrl, viewController: viewController)
  124. } else {
  125. richWorkspaceCommon.openViewerNextcloudText(serverUrl: serverUrl, viewController: viewController)
  126. }
  127. }
  128. }
  129. )
  130. )
  131. }
  132. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeDocx}) && !isDirectoryE2EE {
  133. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeDocx})!
  134. actions.append(
  135. NCMenuAction(
  136. title: NSLocalizedString("_create_new_document_", comment: ""), icon: utility.loadImage(named: "doc.text", colors: [NCBrandColor.shared.documentIconColor]), action: { _ in
  137. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  138. return
  139. }
  140. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  141. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  142. viewController.mainTabBarController = mainTabBarController
  143. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  144. viewController.creatorId = directEditingCreator.identifier
  145. viewController.typeTemplate = NCGlobal.shared.templateDocument
  146. viewController.serverUrl = serverUrl
  147. viewController.titleForm = NSLocalizedString("_create_new_document_", comment: "")
  148. mainTabBarController.present(navigationController, animated: true, completion: nil)
  149. }
  150. }
  151. )
  152. )
  153. }
  154. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeXlsx}) && !isDirectoryE2EE {
  155. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficeXlsx})!
  156. actions.append(
  157. NCMenuAction(
  158. title: NSLocalizedString("_create_new_spreadsheet_", comment: ""), icon: utility.loadImage(named: "tablecells", colors: [NCBrandColor.shared.spreadsheetIconColor]), action: { _ in
  159. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  160. return
  161. }
  162. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  163. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  164. viewController.mainTabBarController = mainTabBarController
  165. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  166. viewController.creatorId = directEditingCreator.identifier
  167. viewController.typeTemplate = NCGlobal.shared.templateSpreadsheet
  168. viewController.serverUrl = serverUrl
  169. viewController.titleForm = NSLocalizedString("_create_new_spreadsheet_", comment: "")
  170. mainTabBarController.present(navigationController, animated: true, completion: nil)
  171. }
  172. }
  173. )
  174. )
  175. }
  176. if NextcloudKit.shared.isNetworkReachable() && directEditingCreators != nil && directEditingCreators!.contains(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficePptx}) && !isDirectoryE2EE {
  177. let directEditingCreator = directEditingCreators!.first(where: { $0.editor == NCGlobal.shared.editorOnlyoffice && $0.identifier == NCGlobal.shared.onlyofficePptx})!
  178. actions.append(
  179. NCMenuAction(
  180. title: NSLocalizedString("_create_new_presentation_", comment: ""), icon: utility.loadImage(named: "play.rectangle", colors: [NCBrandColor.shared.presentationIconColor]), action: { _ in
  181. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  182. return
  183. }
  184. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  185. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  186. viewController.mainTabBarController = mainTabBarController
  187. viewController.editorId = NCGlobal.shared.editorOnlyoffice
  188. viewController.creatorId = directEditingCreator.identifier
  189. viewController.typeTemplate = NCGlobal.shared.templatePresentation
  190. viewController.serverUrl = serverUrl
  191. viewController.titleForm = NSLocalizedString("_create_new_presentation_", comment: "")
  192. mainTabBarController.present(navigationController, animated: true, completion: nil)
  193. }
  194. }
  195. )
  196. )
  197. }
  198. if NCGlobal.shared.capabilityRichDocumentsEnabled {
  199. if NextcloudKit.shared.isNetworkReachable() && !isDirectoryE2EE {
  200. actions.append(
  201. NCMenuAction(
  202. title: NSLocalizedString("_create_new_document_", comment: ""), icon: utility.loadImage(named: "doc.richtext", colors: [NCBrandColor.shared.documentIconColor]), action: { _ in
  203. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  204. return
  205. }
  206. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  207. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  208. viewController.mainTabBarController = mainTabBarController
  209. viewController.editorId = NCGlobal.shared.editorCollabora
  210. viewController.typeTemplate = NCGlobal.shared.templateDocument
  211. viewController.serverUrl = serverUrl
  212. viewController.titleForm = NSLocalizedString("_create_nextcloudtext_document_", comment: "")
  213. mainTabBarController.present(navigationController, animated: true, completion: nil)
  214. }
  215. }
  216. )
  217. )
  218. actions.append(
  219. NCMenuAction(
  220. title: NSLocalizedString("_create_new_spreadsheet_", comment: ""), icon: utility.loadImage(named: "tablecells", colors: [NCBrandColor.shared.spreadsheetIconColor]), action: { _ in
  221. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  222. return
  223. }
  224. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  225. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  226. viewController.mainTabBarController = mainTabBarController
  227. viewController.editorId = NCGlobal.shared.editorCollabora
  228. viewController.typeTemplate = NCGlobal.shared.templateSpreadsheet
  229. viewController.serverUrl = serverUrl
  230. viewController.titleForm = NSLocalizedString("_create_new_spreadsheet_", comment: "")
  231. mainTabBarController.present(navigationController, animated: true, completion: nil)
  232. }
  233. }
  234. )
  235. )
  236. actions.append(
  237. NCMenuAction(
  238. title: NSLocalizedString("_create_new_presentation_", comment: ""), icon: utility.loadImage(named: "play.rectangle", colors: [NCBrandColor.shared.presentationIconColor]), action: { _ in
  239. guard let navigationController = UIStoryboard(name: "NCCreateFormUploadDocuments", bundle: nil).instantiateInitialViewController() else {
  240. return
  241. }
  242. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  243. if let viewController = (navigationController as? UINavigationController)?.topViewController as? NCCreateFormUploadDocuments {
  244. viewController.mainTabBarController = mainTabBarController
  245. viewController.editorId = NCGlobal.shared.editorCollabora
  246. viewController.typeTemplate = NCGlobal.shared.templatePresentation
  247. viewController.serverUrl = serverUrl
  248. viewController.titleForm = NSLocalizedString("_create_new_presentation_", comment: "")
  249. mainTabBarController.present(navigationController, animated: true, completion: nil)
  250. }
  251. }
  252. )
  253. )
  254. }
  255. }
  256. mainTabBarController.presentMenu(with: actions)
  257. }
  258. func createTextDocument(mainTabBarController: NCMainTabBarController, fileNamePath: String, fileName: String, creatorId: String) {
  259. guard let viewController = mainTabBarController.currentViewController() else { return }
  260. var UUID = NSUUID().uuidString
  261. UUID = "TEMP" + UUID.replacingOccurrences(of: "-", with: "")
  262. let serverUrl = mainTabBarController.currentServerUrl()
  263. let options = NKRequestOptions(customUserAgent: NCUtility().getCustomUserAgentNCText())
  264. NextcloudKit.shared.NCTextCreateFile(fileNamePath: fileNamePath, editorId: NCGlobal.shared.editorText, creatorId: creatorId, templateId: NCGlobal.shared.templateDocument, options: options) { account, url, _, error in
  265. guard error == .success, account == self.account, let url = url else {
  266. NCContentPresenter().showError(error: error)
  267. return
  268. }
  269. var results = NextcloudKit.shared.nkCommonInstance.getInternalType(fileName: fileName, mimeType: "", directory: false)
  270. // FIXME: iOS 12.0,* don't detect UTI text/markdown, text/x-markdown
  271. if results.mimeType.isEmpty {
  272. results.mimeType = "text/x-markdown"
  273. }
  274. let metadata = NCManageDatabase.shared.createMetadata(account: self.account, user: self.user, userId: self.userId, fileName: fileName, fileNameView: fileName, ocId: UUID, serverUrl: serverUrl, urlBase: self.urlBase, url: url, contentType: results.mimeType)
  275. NCViewer().view(viewController: viewController, metadata: metadata, metadatas: [metadata], imageIcon: nil)
  276. }
  277. }
  278. }