NCContextMenu.swift 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  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, 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 copy = UIAction(title: NSLocalizedString("_copy_file_", comment: ""), image: UIImage(systemName: "doc.on.doc")) { _ in
  41. NCFunctionCenter.shared.copyPasteboard(pasteboardOcIds: [metadata.ocId], hudView: viewController.view)
  42. }
  43. let detail = UIAction(title: NSLocalizedString("_details_", comment: ""), image: UIImage(systemName: "info")) { _ in
  44. NCFunctionCenter.shared.openShare(viewController: viewController, metadata: metadata, indexPage: .activity)
  45. }
  46. let save = UIAction(title: titleSave, image: UIImage(systemName: "square.and.arrow.down")) { _ in
  47. if metadataMOV != nil {
  48. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  49. } else {
  50. if CCUtility.fileProviderStorageExists(metadata) {
  51. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  52. } else {
  53. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  54. }
  55. }
  56. }
  57. let openIn = UIAction(title: NSLocalizedString("_open_in_", comment: ""), image: UIImage(systemName: "square.and.arrow.up") ) { _ in
  58. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  59. }
  60. let viewInFolder = UIAction(title: NSLocalizedString("_view_in_folder_", comment: ""), image: UIImage(systemName: "arrow.forward.square")) { _ in
  61. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil)
  62. }
  63. let modify = UIAction(title: NSLocalizedString("_modify_", comment: ""), image: UIImage(systemName: "pencil.tip.crop.circle")) { _ in
  64. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  65. }
  66. let favorite = UIAction(title: titleFavorite, image: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite)) { _ in
  67. NCNetworking.shared.favoriteMetadata(metadata) { error in
  68. if error != .success {
  69. NCContentPresenter.shared.showError(error: error)
  70. }
  71. }
  72. }
  73. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile, image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  74. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false) { error in
  75. if error != .success {
  76. NCContentPresenter.shared.showError(error: error)
  77. }
  78. }
  79. }
  80. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""), image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  81. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true) { _ in
  82. }
  83. }
  84. var delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmLocal, deleteConfirmFile])
  85. if viewController is NCMedia || metadata.directory {
  86. delete = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmFile])
  87. }
  88. // ------ MENU -----
  89. // DIR
  90. guard !metadata.directory else {
  91. var submenu = UIMenu()
  92. if !isDirectoryE2EE && metadata.e2eEncrypted {
  93. submenu = UIMenu(title: "", options: .displayInline, children: [favorite])
  94. } else {
  95. submenu = UIMenu(title: "", options: .displayInline, children: [favorite, delete])
  96. }
  97. guard appDelegate!.disableSharesView == false else { return submenu }
  98. return UIMenu(title: "", children: [detail, submenu])
  99. }
  100. // FILE
  101. var menu: [UIMenuElement] = []
  102. if metadata.lock {
  103. menu.append(openIn)
  104. menu.append(save)
  105. menu.append(copy)
  106. } else {
  107. menu.append(favorite)
  108. menu.append(openIn)
  109. menu.append(save)
  110. if viewController is NCMedia {
  111. menu.append(viewInFolder)
  112. }
  113. menu.append(copy)
  114. menu.append(modify)
  115. menu.append(delete)
  116. }
  117. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  118. /*
  119. var children: [UIMenuElement] = [openIn, copy]
  120. if !metadata.lock {
  121. // Workaround: PROPPATCH doesn't work (favorite)
  122. // https://github.com/nextcloud/files_lock/issues/68
  123. children.insert(favorite, at: 0)
  124. children.append(delete)
  125. } else if enableDeleteLocal {
  126. children.append(deleteConfirmLocal)
  127. }
  128. if (metadata.contentType != "image/svg+xml") && (metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.classFile == NKCommon.typeClassFile.video.rawValue) {
  129. children.insert(save, at: 2)
  130. }
  131. if enableViewInFolder {
  132. children.insert(viewInFolder, at: children.count - 1)
  133. }
  134. 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) {
  135. children.insert(modify, at: children.count - 1)
  136. }
  137. let submenu = UIMenu(title: "", options: .displayInline, children: children)
  138. guard appDelegate!.disableSharesView == false else { return submenu }
  139. return UIMenu(title: "", children: [detail, submenu])
  140. */
  141. }
  142. }