NCContextMenu.swift 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. var titleDeleteConfirmFile = NSLocalizedString("_delete_file_", comment: "")
  32. if metadata.directory { titleDeleteConfirmFile = NSLocalizedString("_delete_folder_", comment: "") }
  33. var titleSave: String = NSLocalizedString("_save_selected_files_", comment: "")
  34. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  35. if metadataMOV != nil {
  36. titleSave = NSLocalizedString("_livephoto_save_", comment: "")
  37. }
  38. let detail = UIAction(title: NSLocalizedString("_details_", comment: ""), image: UIImage(systemName: "info")) { _ in
  39. NCFunctionCenter.shared.openShare(viewController: viewController, metadata: metadata, indexPage: .activity)
  40. }
  41. let favorite = UIAction(title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  42. image: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite)) { _ in
  43. NCNetworking.shared.favoriteMetadata(metadata) { error in
  44. if error != .success {
  45. NCContentPresenter.shared.showError(error: error)
  46. }
  47. }
  48. }
  49. let openIn = UIAction(title: NSLocalizedString("_open_in_", comment: ""), image: UIImage(systemName: "square.and.arrow.up") ) { _ in
  50. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  51. }
  52. let viewInFolder = UIAction(title: NSLocalizedString("_view_in_folder_", comment: ""), image: UIImage(systemName: "arrow.forward.square")) { _ in
  53. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil)
  54. }
  55. let save = UIAction(title: titleSave, image: UIImage(systemName: "square.and.arrow.down")) { _ in
  56. if metadataMOV != nil {
  57. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  58. } else {
  59. if CCUtility.fileProviderStorageExists(metadata) {
  60. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  61. } else {
  62. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  63. }
  64. }
  65. }
  66. let copy = UIAction(title: NSLocalizedString("_copy_file_", comment: ""), image: UIImage(systemName: "doc.on.doc")) { _ in
  67. NCFunctionCenter.shared.copyPasteboard(pasteboardOcIds: [metadata.ocId], hudView: viewController.view)
  68. }
  69. let modify = UIAction(title: NSLocalizedString("_modify_", comment: ""), image: UIImage(systemName: "pencil.tip.crop.circle")) { _ in
  70. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  71. }
  72. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile, image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  73. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false) { error in
  74. if error != .success {
  75. NCContentPresenter.shared.showError(error: error)
  76. }
  77. }
  78. }
  79. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""), image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  80. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true) { _ in
  81. }
  82. }
  83. let deleteSubMenu = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""), image: UIImage(systemName: "trash"), options: .destructive, children: [deleteConfirmLocal, deleteConfirmFile])
  84. // ------ MENU -----
  85. var menu: [UIMenuElement] = []
  86. if metadata.directory {
  87. if metadata.isDirectoryE2EE || metadata.e2eEncrypted {
  88. menu.append(favorite)
  89. } else {
  90. menu.append(favorite)
  91. menu.append(deleteConfirmFile)
  92. }
  93. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  94. } else {
  95. if metadata.lock {
  96. menu.append(favorite)
  97. if metadata.isViewOnly {
  98. //
  99. } else {
  100. menu.append(openIn)
  101. menu.append(save)
  102. menu.append(copy)
  103. }
  104. } else {
  105. menu.append(favorite)
  106. if metadata.isViewOnly {
  107. if viewController is NCMedia {
  108. menu.append(viewInFolder)
  109. }
  110. } else {
  111. menu.append(openIn)
  112. menu.append(save)
  113. if viewController is NCMedia {
  114. menu.append(viewInFolder)
  115. }
  116. menu.append(copy)
  117. menu.append(modify)
  118. }
  119. if viewController is NCMedia {
  120. menu.append(deleteConfirmFile)
  121. } else {
  122. menu.append(deleteSubMenu)
  123. }
  124. }
  125. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  126. }
  127. }
  128. }