NCContextMenu.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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 detail = UIAction(title: NSLocalizedString("_details_", comment: ""), image: UIImage(systemName: "info")) { _ in
  58. NCFunctionCenter.shared.openShare(viewController: viewController, metadata: metadata, indexPage: .activity)
  59. }
  60. let offline = UIAction(title: titleOffline, image: UIImage(systemName: "tray.and.arrow.down")) { _ in
  61. NCFunctionCenter.shared.setMetadataAvalableOffline(metadata, isOffline: isOffline)
  62. if let viewController = viewController as? NCCollectionViewCommon {
  63. viewController.reloadDataSource()
  64. }
  65. }
  66. let lockUnlock = UIAction(title: titleLock, image: UIImage(systemName: iconLock)) { _ in
  67. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: !metadata.lock)
  68. }
  69. let save = UIAction(title: titleSave, image: UIImage(systemName: "square.and.arrow.down")) { _ in
  70. if metadataMOV != nil {
  71. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  72. } else {
  73. if CCUtility.fileProviderStorageExists(metadata) {
  74. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  75. } else {
  76. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  77. }
  78. }
  79. }
  80. let viewInFolder = UIAction(title: NSLocalizedString("_view_in_folder_", comment: ""), image: UIImage(systemName: "arrow.forward.square")) { _ in
  81. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil)
  82. }
  83. let openIn = UIAction(title: NSLocalizedString("_open_in_", comment: ""), image: UIImage(systemName: "square.and.arrow.up") ) { _ in
  84. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  85. }
  86. let print = UIAction(title: NSLocalizedString("_print_", comment: ""), image: UIImage(systemName: "printer") ) { _ in
  87. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  88. }
  89. let modify = UIAction(title: NSLocalizedString("_modify_", comment: ""), image: UIImage(systemName: "pencil.tip.crop.circle")) { _ in
  90. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  91. }
  92. let saveAsScan = UIAction(title: NSLocalizedString("_save_as_scan_", comment: ""), image: UIImage(systemName: "viewfinder.circle")) { _ in
  93. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan)
  94. }
  95. // let open = UIMenu(title: NSLocalizedString("_open_", comment: ""), image: UIImage(systemName: "square.and.arrow.up"), children: [openIn, openQuickLook])
  96. let moveCopy = UIAction(title: NSLocalizedString("_move_or_copy_", comment: ""), image: UIImage(systemName: "arrow.up.right.square")) { _ in
  97. NCFunctionCenter.shared.openSelectView(items: [metadata])
  98. }
  99. let rename = UIAction(title: NSLocalizedString("_rename_", comment: ""), image: UIImage(systemName: "pencil")) { _ in
  100. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  101. vcRename.metadata = metadata
  102. vcRename.imagePreview = image
  103. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  104. viewController.present(popup, animated: true)
  105. }
  106. }
  107. let favorite = UIAction(title: titleFavorite, image: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite)) { _ in
  108. NCNetworking.shared.favoriteMetadata(metadata) { error in
  109. if error != .success {
  110. NCContentPresenter.shared.showError(error: error)
  111. }
  112. }
  113. }
  114. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile, image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  115. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false) { error in
  116. if error != .success {
  117. NCContentPresenter.shared.showError(error: error)
  118. }
  119. }
  120. }
  121. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""), image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  122. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true) { _ in
  123. }
  124. }
  125. var delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmLocal, deleteConfirmFile])
  126. if !enableDeleteLocal {
  127. delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmFile])
  128. }
  129. if metadata.directory {
  130. delete = UIMenu(title: NSLocalizedString("_delete_folder_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmFile])
  131. }
  132. // ------ MENU -----
  133. // DIR
  134. guard !metadata.directory else {
  135. var submenu = UIMenu()
  136. if !isDirectoryE2EE && metadata.e2eEncrypted {
  137. submenu = UIMenu(title: "", options: .displayInline, children: [favorite, offline, rename, moveCopy])
  138. } else {
  139. submenu = UIMenu(title: "", options: .displayInline, children: [favorite, offline, rename, moveCopy, delete])
  140. }
  141. guard appDelegate!.disableSharesView == false else { return submenu }
  142. return UIMenu(title: "", children: [detail, submenu])
  143. }
  144. // FILE
  145. var children: [UIMenuElement] = [offline, openIn, moveCopy, copy]
  146. if !metadata.lock {
  147. // Workaround: PROPPATCH doesn't work (favorite)
  148. // https://github.com/nextcloud/files_lock/issues/68
  149. children.insert(favorite, at: 0)
  150. children.append(delete)
  151. children.insert(rename, at: 3)
  152. } else if enableDeleteLocal {
  153. children.append(deleteConfirmLocal)
  154. }
  155. if NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate!.account, elements: NCElementsJSON.shared.capabilitiesFilesLockVersion) >= 1, metadata.canUnlock(as: appDelegate!.userId) {
  156. children.insert(lockUnlock, at: metadata.lock ? 0 : 1)
  157. }
  158. if (metadata.contentType != "image/svg+xml") && (metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.classFile == NKCommon.typeClassFile.video.rawValue) {
  159. children.insert(save, at: 2)
  160. }
  161. if (metadata.contentType != "image/svg+xml") && (metadata.classFile == NKCommon.typeClassFile.image.rawValue) {
  162. children.insert(saveAsScan, at: 2)
  163. }
  164. if (metadata.contentType != "image/svg+xml") && (metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf") {
  165. children.insert(print, at: 2)
  166. }
  167. if enableViewInFolder {
  168. children.insert(viewInFolder, at: children.count - 1)
  169. }
  170. 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) {
  171. children.insert(modify, at: children.count - 1)
  172. }
  173. let submenu = UIMenu(title: "", options: .displayInline, children: children)
  174. guard appDelegate!.disableSharesView == false else { return submenu }
  175. return UIMenu(title: "", children: [detail, submenu])
  176. }
  177. }