NCContextMenu.swift 7.1 KB

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