NCContextMenu.swift 13 KB

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