NCContextMenu.swift 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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 ITEMS
  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. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: .actionSheet)
  80. alertController.addAction(UIAlertAction(title: NSLocalizedString("_delete_file_", comment: ""), style: .destructive) { _ in
  81. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false) { error in
  82. if error != .success {
  83. NCContentPresenter.shared.showError(error: error)
  84. }
  85. }
  86. })
  87. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { _ in })
  88. viewController.present(alertController, animated: true, completion: nil)
  89. }
  90. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""),
  91. image: UIImage(systemName: "trash"), attributes: .destructive) { _ in
  92. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true) { _ in }
  93. }
  94. let deleteSubMenu = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""),
  95. image: UIImage(systemName: "trash"),
  96. options: .destructive,
  97. children: [deleteConfirmLocal, deleteConfirmFile])
  98. // ------ MENU -----
  99. var menu: [UIMenuElement] = []
  100. if metadata.directory {
  101. if metadata.isDirectoryE2EE || metadata.e2eEncrypted {
  102. menu.append(favorite)
  103. } else {
  104. menu.append(favorite)
  105. menu.append(deleteConfirmFile)
  106. }
  107. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  108. } else {
  109. if metadata.lock {
  110. menu.append(favorite)
  111. if metadata.isViewableOnly {
  112. //
  113. } else {
  114. menu.append(openIn)
  115. // SAVE CAMERA ROLL
  116. menu.append(save)
  117. }
  118. } else {
  119. menu.append(favorite)
  120. if metadata.isViewableOnly {
  121. if viewController is NCMedia {
  122. menu.append(viewInFolder)
  123. }
  124. } else {
  125. menu.append(openIn)
  126. // SAVE CAMERA ROLL
  127. menu.append(save)
  128. if viewController is NCMedia {
  129. menu.append(viewInFolder)
  130. }
  131. // MODIFY WITH QUICK LOOK
  132. if metadata.isModifiableWithQuickLook {
  133. menu.append(modify)
  134. }
  135. }
  136. if viewController is NCMedia {
  137. menu.append(deleteConfirmFile)
  138. } else {
  139. menu.append(deleteSubMenu)
  140. }
  141. }
  142. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  143. }
  144. }
  145. }