NCContextMenu.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  1. //
  2. // NCContextMenu.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 10/01/23.
  6. // Copyright © 2023 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. import NextcloudKit
  25. class NCContextMenu: NSObject {
  26. func viewMenu(ocId: String, viewController: UIViewController, enableDeleteLocal: Bool, enableViewInFolder: Bool, image: UIImage?) -> UIMenu {
  27. let appDelegate = UIApplication.shared.delegate as? AppDelegate
  28. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) else {
  29. return UIMenu()
  30. }
  31. let isDirectoryE2EE = NCUtility.shared.isDirectoryE2EE(metadata: metadata)
  32. var titleDeleteConfirmFile = NSLocalizedString("_delete_file_", comment: "")
  33. if metadata.directory { titleDeleteConfirmFile = NSLocalizedString("_delete_folder_", comment: "") }
  34. var titleSave: String = NSLocalizedString("_save_selected_files_", comment: "")
  35. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  36. if metadataMOV != nil {
  37. titleSave = NSLocalizedString("_livephoto_save_", comment: "")
  38. }
  39. let titleFavorite = metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: "")
  40. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  41. var isOffline = false
  42. if metadata.directory {
  43. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate!.account, serverUrl)) {
  44. isOffline = directory.offline
  45. }
  46. } else {
  47. if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  48. isOffline = localFile.offline
  49. }
  50. }
  51. let titleOffline = isOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: "")
  52. let titleLock = metadata.lock ? NSLocalizedString("_unlock_file_", comment: "") : NSLocalizedString("_lock_file_", comment: "")
  53. let iconLock = metadata.lock ? "lock.open" : "lock"
  54. let copy = UIAction(title: NSLocalizedString("_copy_file_", comment: ""), image: UIImage(systemName: "doc.on.doc")) { _ in
  55. NCFunctionCenter.shared.copyPasteboard(pasteboardOcIds: [metadata.ocId], hudView: viewController.view)
  56. }
  57. let copyPath = UIAction(title: NSLocalizedString("_copy_path_", comment: ""), image: UIImage(systemName: "doc.on.clipboard")) { _ in
  58. let board = UIPasteboard.general
  59. board.string = NCUtilityFileSystem.shared.getPath(path: metadata.path, user: metadata.user, fileName: metadata.fileName)
  60. let error = NKError(errorCode: NCGlobal.shared.errorInternalError, errorDescription: "_copied_path_")
  61. NCContentPresenter.shared.showInfo(error: error)
  62. }
  63. let detail = UIAction(title: NSLocalizedString("_details_", comment: ""), image: UIImage(systemName: "info")) { _ in
  64. NCFunctionCenter.shared.openShare(viewController: viewController, metadata: metadata, indexPage: .activity)
  65. }
  66. let offline = UIAction(title: titleOffline, image: UIImage(systemName: "tray.and.arrow.down")) { _ in
  67. NCFunctionCenter.shared.setMetadataAvalableOffline(metadata, isOffline: isOffline)
  68. if let viewController = viewController as? NCCollectionViewCommon {
  69. viewController.reloadDataSource()
  70. }
  71. }
  72. let lockUnlock = UIAction(title: titleLock, image: UIImage(systemName: iconLock)) { _ in
  73. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: !metadata.lock)
  74. }
  75. let save = UIAction(title: titleSave, image: UIImage(systemName: "square.and.arrow.down")) { _ in
  76. if metadataMOV != nil {
  77. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  78. } else {
  79. if CCUtility.fileProviderStorageExists(metadata) {
  80. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  81. } else {
  82. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  83. }
  84. }
  85. }
  86. let viewInFolder = UIAction(title: NSLocalizedString("_view_in_folder_", comment: ""), image: UIImage(systemName: "arrow.forward.square")) { _ in
  87. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil)
  88. }
  89. let openIn = UIAction(title: NSLocalizedString("_open_in_", comment: ""), image: UIImage(systemName: "square.and.arrow.up") ) { _ in
  90. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  91. }
  92. let print = UIAction(title: NSLocalizedString("_print_", comment: ""), image: UIImage(systemName: "printer") ) { _ in
  93. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  94. }
  95. let modify = UIAction(title: NSLocalizedString("_modify_", comment: ""), image: UIImage(systemName: "pencil.tip.crop.circle")) { _ in
  96. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  97. }
  98. let saveAsScan = UIAction(title: NSLocalizedString("_save_as_scan_", comment: ""), image: UIImage(systemName: "viewfinder.circle")) { _ in
  99. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan)
  100. }
  101. // let open = UIMenu(title: NSLocalizedString("_open_", comment: ""), image: UIImage(systemName: "square.and.arrow.up"), children: [openIn, openQuickLook])
  102. let moveCopy = UIAction(title: NSLocalizedString("_move_or_copy_", comment: ""), image: UIImage(systemName: "arrow.up.right.square")) { _ in
  103. NCFunctionCenter.shared.openSelectView(items: [metadata])
  104. }
  105. let rename = UIAction(title: NSLocalizedString("_rename_", comment: ""), image: UIImage(systemName: "pencil")) { _ in
  106. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  107. vcRename.metadata = metadata
  108. vcRename.imagePreview = image
  109. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  110. viewController.present(popup, animated: true)
  111. }
  112. }
  113. let favorite = UIAction(title: titleFavorite, image: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite)) { _ in
  114. NCNetworking.shared.favoriteMetadata(metadata) { error in
  115. if error != .success {
  116. NCContentPresenter.shared.showError(error: error)
  117. }
  118. }
  119. }
  120. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile, image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  121. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false) { error in
  122. if error != .success {
  123. NCContentPresenter.shared.showError(error: error)
  124. }
  125. }
  126. }
  127. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""), image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  128. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true) { _ in
  129. }
  130. }
  131. var delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmLocal, deleteConfirmFile])
  132. if !enableDeleteLocal {
  133. delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmFile])
  134. }
  135. if metadata.directory {
  136. delete = UIMenu(title: NSLocalizedString("_delete_folder_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmFile])
  137. }
  138. // ------ MENU -----
  139. // DIR
  140. guard !metadata.directory else {
  141. var submenu = UIMenu()
  142. if !isDirectoryE2EE && metadata.e2eEncrypted {
  143. submenu = UIMenu(title: "", options: .displayInline, children: [favorite, offline, rename, moveCopy, copyPath])
  144. } else {
  145. submenu = UIMenu(title: "", options: .displayInline, children: [favorite, offline, rename, moveCopy, copyPath, delete])
  146. }
  147. guard appDelegate!.disableSharesView == false else { return submenu }
  148. return UIMenu(title: "", children: [detail, submenu])
  149. }
  150. // FILE
  151. var children: [UIMenuElement] = [offline, openIn, moveCopy, copy, copyPath]
  152. if !metadata.lock {
  153. // Workaround: PROPPATCH doesn't work (favorite)
  154. // https://github.com/nextcloud/files_lock/issues/68
  155. children.insert(favorite, at: 0)
  156. children.append(delete)
  157. children.insert(rename, at: 3)
  158. } else if enableDeleteLocal {
  159. children.append(deleteConfirmLocal)
  160. }
  161. if NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate!.account, elements: NCElementsJSON.shared.capabilitiesFilesLockVersion) >= 1, metadata.canUnlock(as: appDelegate!.userId) {
  162. children.insert(lockUnlock, at: metadata.lock ? 0 : 1)
  163. }
  164. if (metadata.contentType != "image/svg+xml") && (metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.classFile == NKCommon.typeClassFile.video.rawValue) {
  165. children.insert(save, at: 2)
  166. }
  167. if (metadata.contentType != "image/svg+xml") && (metadata.classFile == NKCommon.typeClassFile.image.rawValue) {
  168. children.insert(saveAsScan, at: 2)
  169. }
  170. if (metadata.contentType != "image/svg+xml") && (metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf") {
  171. children.insert(print, at: 2)
  172. }
  173. if enableViewInFolder {
  174. children.insert(viewInFolder, at: children.count - 1)
  175. }
  176. if (!isDirectoryE2EE && metadata.contentType != "image/gif" && metadata.contentType != "image/svg+xml") && (metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" || metadata.classFile == NKCommon.typeClassFile.image.rawValue) {
  177. children.insert(modify, at: children.count - 1)
  178. }
  179. let submenu = UIMenu(title: "", options: .displayInline, children: children)
  180. guard appDelegate!.disableSharesView == false else { return submenu }
  181. return UIMenu(title: "", children: [detail, submenu])
  182. }
  183. }