NCContextMenu.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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 UIKit
  25. import Alamofire
  26. import NextcloudKit
  27. class NCContextMenu: NSObject {
  28. let utilityFileSystem = NCUtilityFileSystem()
  29. let utility = NCUtility()
  30. let database = NCManageDatabase.shared
  31. func viewMenu(ocId: String, viewController: UIViewController, image: UIImage?) -> UIMenu {
  32. guard let metadata = self.database.getMetadataFromOcId(ocId),
  33. let sceneIdentifier = (viewController.tabBarController as? NCMainTabBarController)?.sceneIdentifier else { return UIMenu() }
  34. var downloadRequest: DownloadRequest?
  35. var titleDeleteConfirmFile = NSLocalizedString("_delete_file_", comment: "")
  36. let metadataMOV = self.database.getMetadataLivePhoto(metadata: metadata)
  37. let hud = NCHud(viewController.view)
  38. if metadata.directory { titleDeleteConfirmFile = NSLocalizedString("_delete_folder_", comment: "") }
  39. // MENU ITEMS
  40. let detail = UIAction(title: NSLocalizedString("_details_", comment: ""),
  41. image: utility.loadImage(named: "info.circle")) { _ in
  42. NCActionCenter.shared.openShare(viewController: viewController, metadata: metadata, page: .activity)
  43. }
  44. let favorite = UIAction(title: metadata.favorite ?
  45. NSLocalizedString("_remove_favorites_", comment: "") :
  46. NSLocalizedString("_add_favorites_", comment: ""),
  47. image: utility.loadImage(named: metadata.favorite ? "star.slash" : "star", colors: [NCBrandColor.shared.yellowFavorite])) { _ in
  48. NCNetworking.shared.favoriteMetadata(metadata) { error in
  49. if error != .success {
  50. NCContentPresenter().showError(error: error)
  51. }
  52. }
  53. }
  54. let share = UIAction(title: NSLocalizedString("_share_", comment: ""),
  55. image: utility.loadImage(named: "square.and.arrow.up") ) { _ in
  56. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  57. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile,
  58. object: nil,
  59. userInfo: ["ocId": metadata.ocId,
  60. "ocIdTransfer": metadata.ocIdTransfer,
  61. "session": metadata.session,
  62. "selector": NCGlobal.shared.selectorOpenIn,
  63. "error": NKError(),
  64. "account": metadata.account],
  65. second: 0.5)
  66. } else {
  67. guard let metadata = self.database.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  68. session: NCNetworking.shared.sessionDownload,
  69. selector: NCGlobal.shared.selectorOpenIn,
  70. sceneIdentifier: sceneIdentifier) else { return }
  71. hud.initHudRing(text: NSLocalizedString("_downloading_", comment: ""), tapToCancelDetailText: true) {
  72. if let request = downloadRequest {
  73. request.cancel()
  74. }
  75. }
  76. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: false) {
  77. } requestHandler: { request in
  78. downloadRequest = request
  79. } progressHandler: { progress in
  80. hud.progress(progress.fractionCompleted)
  81. } completion: { afError, error in
  82. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  83. hud.success()
  84. } else {
  85. hud.error(text: error.description)
  86. }
  87. }
  88. }
  89. }
  90. let viewInFolder = UIAction(title: NSLocalizedString("_view_in_folder_", comment: ""),
  91. image: utility.loadImage(named: "questionmark.folder")) { _ in
  92. NCActionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil, sceneIdentifier: sceneIdentifier)
  93. }
  94. let livePhotoSave = UIAction(title: NSLocalizedString("_livephoto_save_", comment: ""), image: utility.loadImage(named: "livephoto")) { _ in
  95. if let metadataMOV = metadataMOV {
  96. NCNetworking.shared.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV, hudView: viewController.view))
  97. }
  98. }
  99. let modify = UIAction(title: NSLocalizedString("_modify_", comment: ""),
  100. image: utility.loadImage(named: "pencil.tip.crop.circle")) { _ in
  101. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  102. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile,
  103. object: nil,
  104. userInfo: ["ocId": metadata.ocId,
  105. "ocIdTransfer": metadata.ocIdTransfer,
  106. "session": metadata.session,
  107. "selector": NCGlobal.shared.selectorLoadFileQuickLook,
  108. "error": NKError(),
  109. "account": metadata.account],
  110. second: 0.5)
  111. } else {
  112. guard let metadata = self.database.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  113. session: NCNetworking.shared.sessionDownload,
  114. selector: NCGlobal.shared.selectorLoadFileQuickLook,
  115. sceneIdentifier: sceneIdentifier) else { return }
  116. hud.initHudRing(text: NSLocalizedString("_downloading_", comment: "")) {
  117. if let request = downloadRequest {
  118. request.cancel()
  119. }
  120. }
  121. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: false) {
  122. } requestHandler: { request in
  123. downloadRequest = request
  124. } progressHandler: { progress in
  125. hud.progress(progress.fractionCompleted)
  126. } completion: { afError, error in
  127. if error == .success || afError?.isExplicitlyCancelledError ?? false {
  128. hud.success()
  129. } else {
  130. hud.error(text: error.description)
  131. }
  132. }
  133. }
  134. }
  135. let deleteConfirmFile = UIAction(title: titleDeleteConfirmFile,
  136. image: utility.loadImage(named: "trash"), attributes: .destructive) { _ in
  137. var alertStyle = UIAlertController.Style.actionSheet
  138. if UIDevice.current.userInterfaceIdiom == .pad {
  139. alertStyle = .alert
  140. }
  141. let alertController = UIAlertController(title: nil, message: nil, preferredStyle: alertStyle)
  142. alertController.addAction(UIAlertAction(title: NSLocalizedString("_delete_file_", comment: ""), style: .destructive) { _ in
  143. NCNetworking.shared.deleteMetadatas([metadata], sceneIdentifier: sceneIdentifier)
  144. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataSource)
  145. })
  146. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel) { _ in })
  147. viewController.present(alertController, animated: true, completion: nil)
  148. }
  149. let deleteConfirmLocal = UIAction(title: NSLocalizedString("_remove_local_file_", comment: ""),
  150. image: utility.loadImage(named: "trash"), attributes: .destructive) { _ in
  151. Task {
  152. var ocId: [String] = []
  153. let error = await NCNetworking.shared.deleteCache(metadata, sceneIdentifier: sceneIdentifier)
  154. if error == .success {
  155. ocId.append(metadata.ocId)
  156. }
  157. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["ocId": ocId, "error": error])
  158. }
  159. }
  160. let deleteSubMenu = UIMenu(title: NSLocalizedString("_delete_file_", comment: ""),
  161. image: utility.loadImage(named: "trash"),
  162. options: .destructive,
  163. children: [deleteConfirmLocal, deleteConfirmFile])
  164. // ------ MENU -----
  165. var menu: [UIMenuElement] = []
  166. if NCNetworking.shared.isOnline {
  167. if metadata.directory {
  168. if metadata.isDirectoryE2EE || metadata.e2eEncrypted {
  169. menu.append(favorite)
  170. } else {
  171. menu.append(favorite)
  172. menu.append(deleteConfirmFile)
  173. }
  174. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  175. } else {
  176. if metadata.lock {
  177. menu.append(favorite)
  178. if metadata.isDocumentViewableOnly {
  179. //
  180. } else {
  181. menu.append(share)
  182. if self.database.getMetadataLivePhoto(metadata: metadata) != nil {
  183. menu.append(livePhotoSave)
  184. }
  185. }
  186. } else {
  187. menu.append(favorite)
  188. if metadata.isDocumentViewableOnly {
  189. if viewController is NCMedia {
  190. menu.append(viewInFolder)
  191. }
  192. } else {
  193. menu.append(share)
  194. if self.database.getMetadataLivePhoto(metadata: metadata) != nil {
  195. menu.append(livePhotoSave)
  196. }
  197. if viewController is NCMedia {
  198. menu.append(viewInFolder)
  199. }
  200. // MODIFY WITH QUICK LOOK
  201. if metadata.isModifiableWithQuickLook {
  202. menu.append(modify)
  203. }
  204. }
  205. if viewController is NCMedia {
  206. menu.append(deleteConfirmFile)
  207. } else {
  208. menu.append(deleteSubMenu)
  209. }
  210. }
  211. return UIMenu(title: "", children: [detail, UIMenu(title: "", options: .displayInline, children: menu)])
  212. }
  213. } else {
  214. return UIMenu()
  215. }
  216. }
  217. }