NCMenuAction.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. //
  2. // NCMenuAction.swift
  3. // Nextcloud
  4. //
  5. // Created by Henrik Storch on 17.02.22.
  6. // Copyright © 2022 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@nextcloud.com>
  9. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  10. //
  11. // This program is free software: you can redistribute it and/or modify
  12. // it under the terms of the GNU General Public License as published by
  13. // the Free Software Foundation, either version 3 of the License, or
  14. // (at your option) any later version.
  15. //
  16. // This program is distributed in the hope that it will be useful,
  17. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. // GNU General Public License for more details.
  20. //
  21. // You should have received a copy of the GNU General Public License
  22. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. import Foundation
  25. import UIKit
  26. import NextcloudKit
  27. class NCMenuAction {
  28. let title: String
  29. let details: String?
  30. let icon: UIImage
  31. let selectable: Bool
  32. var onTitle: String?
  33. var onIcon: UIImage?
  34. var selected: Bool = false
  35. var isOn: Bool = false
  36. var action: ((_ menuAction: NCMenuAction) -> Void)?
  37. var rowHeight: CGFloat { self.title == NCMenuAction.seperatorIdentifier ? NCMenuAction.seperatorHeight : self.details != nil ? 80 : 60 }
  38. var order: Int = 0
  39. init(title: String, details: String? = nil, icon: UIImage, order: Int = 0, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  40. self.title = title
  41. self.details = details
  42. self.icon = icon
  43. self.action = action
  44. self.selectable = false
  45. self.order = order
  46. }
  47. init(title: String, details: String? = nil, icon: UIImage, onTitle: String? = nil, onIcon: UIImage? = nil, selected: Bool, on: Bool, order: Int = 0, action: ((_ menuAction: NCMenuAction) -> Void)?) {
  48. self.title = title
  49. self.details = details
  50. self.icon = icon
  51. self.onTitle = onTitle ?? title
  52. self.onIcon = onIcon ?? icon
  53. self.action = action
  54. self.selected = selected
  55. self.isOn = on
  56. self.selectable = true
  57. self.order = order
  58. }
  59. }
  60. // MARK: - Actions
  61. extension NCMenuAction {
  62. static let seperatorIdentifier = "NCMenuAction.SEPARATOR"
  63. static let seperatorHeight: CGFloat = 0.5
  64. /// A static seperator, with no actions, text, or icons
  65. static func seperator(order: Int = 0) -> NCMenuAction {
  66. return NCMenuAction(title: seperatorIdentifier, icon: UIImage(), order: order, action: nil)
  67. }
  68. /// Select all items
  69. static func selectAllAction(action: @escaping () -> Void) -> NCMenuAction {
  70. NCMenuAction(
  71. title: NSLocalizedString("_select_all_", comment: ""),
  72. icon: NCUtility.shared.loadImage(named: "checkmark.circle.fill"),
  73. action: { _ in action() }
  74. )
  75. }
  76. /// Copy files to pasteboard
  77. static func copyAction(selectOcId: [String], hudView: UIView, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  78. NCMenuAction(
  79. title: NSLocalizedString("_copy_file_", comment: ""),
  80. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  81. order: order,
  82. action: { _ in
  83. NCActionCenter.shared.copyPasteboard(pasteboardOcIds: selectOcId, hudView: hudView)
  84. completion?()
  85. }
  86. )
  87. }
  88. /// Delete files either from cache or from Nextcloud
  89. static func deleteAction(selectedMetadatas: [tableMetadata], metadataFolder: tableMetadata? = nil, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  90. var titleDelete = NSLocalizedString("_delete_", comment: "")
  91. if selectedMetadatas.count > 1 {
  92. titleDelete = NSLocalizedString("_delete_selected_files_", comment: "")
  93. } else if let metadata = selectedMetadatas.first {
  94. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  95. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  96. } else if metadata.directory {
  97. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  98. } else {
  99. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  100. }
  101. if let metadataFolder = metadataFolder {
  102. let isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  103. let isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  104. if isShare || isMounted {
  105. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  106. }
  107. }
  108. } // else: no metadata selected
  109. let canDeleteServer = selectedMetadatas.allSatisfy { !$0.lock }
  110. var fileList = ""
  111. for (ix, metadata) in selectedMetadatas.enumerated() {
  112. guard ix < 3 else { fileList += "\n - ..."; break }
  113. fileList += "\n - " + metadata.fileNameView
  114. }
  115. return NCMenuAction(
  116. title: titleDelete,
  117. icon: NCUtility.shared.loadImage(named: "trash"),
  118. order: order,
  119. action: { _ in
  120. let alertController = UIAlertController(
  121. title: titleDelete,
  122. message: NSLocalizedString("_want_delete_", comment: "") + fileList,
  123. preferredStyle: .alert)
  124. if canDeleteServer {
  125. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  126. Task {
  127. var error = NKError()
  128. var ocId: [String] = []
  129. let account = selectedMetadatas.first?.account ?? ""
  130. for metadata in selectedMetadatas where error == .success {
  131. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false)
  132. if error == .success {
  133. ocId.append(metadata.ocId)
  134. }
  135. }
  136. if error != .success {
  137. NCContentPresenter.shared.showError(error: error)
  138. }
  139. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["account": account, "ocId": ocId, "error": error])
  140. }
  141. completion?()
  142. })
  143. }
  144. // NCMedia removes image from collection view if removed from cache
  145. if !(viewController is NCMedia) {
  146. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  147. Task {
  148. var error = NKError()
  149. var ocId: [String] = []
  150. let account = selectedMetadatas.first?.account ?? ""
  151. for metadata in selectedMetadatas where error == .success {
  152. error = await NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: true)
  153. if error == .success {
  154. ocId.append(metadata.ocId)
  155. }
  156. }
  157. if error != .success {
  158. NCContentPresenter.shared.showError(error: error)
  159. }
  160. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDeleteFile, userInfo: ["account": account, "ocId": ocId, "error": error])
  161. }
  162. completion?()
  163. })
  164. }
  165. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  166. viewController.present(alertController, animated: true, completion: nil)
  167. }
  168. )
  169. }
  170. /// Open "share view" (activity VC) to open files in another app
  171. static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  172. NCMenuAction(
  173. title: NSLocalizedString("_open_in_", comment: ""),
  174. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  175. order: order,
  176. action: { _ in
  177. NCActionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  178. completion?()
  179. }
  180. )
  181. }
  182. /// Save selected files to user's photo library
  183. static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  184. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  185. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  186. if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
  187. title = NSLocalizedString("_livephoto_save_", comment: "")
  188. icon = NCUtility.shared.loadImage(named: "livephoto")
  189. }
  190. return NCMenuAction(
  191. title: title,
  192. icon: icon,
  193. order: order,
  194. action: { _ in
  195. for metadata in selectedMediaMetadatas {
  196. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  197. NCActionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  198. } else {
  199. if CCUtility.fileProviderStorageExists(metadata) {
  200. NCActionCenter.shared.saveAlbum(metadata: metadata)
  201. } else {
  202. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  203. }
  204. }
  205. }
  206. completion?()
  207. }
  208. )
  209. }
  210. /// Set (or remove) a file as *available offline*. Downloads the file if not downloaded already
  211. static func setAvailableOfflineAction(selectedMetadatas: [tableMetadata], isAnyOffline: Bool, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  212. NCMenuAction(
  213. title: isAnyOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  214. icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
  215. order: order,
  216. action: { _ in
  217. if !isAnyOffline, selectedMetadatas.count > 3 {
  218. let alert = UIAlertController(
  219. title: NSLocalizedString("_set_available_offline_", comment: ""),
  220. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  221. preferredStyle: .alert)
  222. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  223. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  224. completion?()
  225. }))
  226. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  227. viewController.present(alert, animated: true)
  228. } else {
  229. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  230. completion?()
  231. }
  232. }
  233. )
  234. }
  235. /// Open view that lets the user move or copy the files within Nextcloud
  236. static func moveOrCopyAction(selectedMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  237. NCMenuAction(
  238. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  239. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  240. order: order,
  241. action: { _ in
  242. NCActionCenter.shared.openSelectView(items: selectedMetadatas)
  243. completion?()
  244. }
  245. )
  246. }
  247. /// Open AirPrint view to print a single file
  248. static func printAction(metadata: tableMetadata, order: Int = 0) -> NCMenuAction {
  249. NCMenuAction(
  250. title: NSLocalizedString("_print_", comment: ""),
  251. icon: NCUtility.shared.loadImage(named: "printer"),
  252. order: order,
  253. action: { _ in
  254. if CCUtility.fileProviderStorageExists(metadata) {
  255. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorPrint, "error": NKError(), "account": metadata.account])
  256. } else {
  257. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorPrint) { _, _ in }
  258. }
  259. }
  260. )
  261. }
  262. /// Lock or unlock a file using *files_lock*
  263. static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  264. let titleKey: String
  265. if metadatas.count == 1 {
  266. titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
  267. } else {
  268. titleKey = shouldLock ? "_lock_selected_files_" : "_unlock_selected_files_"
  269. }
  270. let imageName = !shouldLock ? "lock_open" : "lock"
  271. return NCMenuAction(
  272. title: NSLocalizedString(titleKey, comment: ""),
  273. icon: NCUtility.shared.loadImage(named: imageName),
  274. order: order,
  275. action: { _ in
  276. for metadata in metadatas where metadata.lock != shouldLock {
  277. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)
  278. }
  279. completion?()
  280. }
  281. )
  282. }
  283. }