NCMenuAction.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: false) })
  127. completion?()
  128. })
  129. }
  130. // NCMedia removes image from collection view if removed from cache
  131. if !(viewController is NCMedia) {
  132. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  133. selectedMetadatas.forEach({ NCOperationQueue.shared.delete(metadata: $0, onlyLocalCache: true) })
  134. completion?()
  135. })
  136. }
  137. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  138. viewController.present(alertController, animated: true, completion: nil)
  139. }
  140. )
  141. }
  142. /// Open "share view" (activity VC) to open files in another app
  143. static func openInAction(selectedMetadatas: [tableMetadata], viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  144. NCMenuAction(
  145. title: NSLocalizedString("_open_in_", comment: ""),
  146. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  147. order: order,
  148. action: { _ in
  149. NCActionCenter.shared.openActivityViewController(selectedMetadata: selectedMetadatas)
  150. completion?()
  151. }
  152. )
  153. }
  154. /// Save selected files to user's photo library
  155. static func saveMediaAction(selectedMediaMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  156. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  157. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  158. if selectedMediaMetadatas.allSatisfy({ NCManageDatabase.shared.getMetadataLivePhoto(metadata: $0) != nil }) {
  159. title = NSLocalizedString("_livephoto_save_", comment: "")
  160. icon = NCUtility.shared.loadImage(named: "livephoto")
  161. }
  162. return NCMenuAction(
  163. title: title,
  164. icon: icon,
  165. order: order,
  166. action: { _ in
  167. for metadata in selectedMediaMetadatas {
  168. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  169. NCActionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  170. } else {
  171. if CCUtility.fileProviderStorageExists(metadata) {
  172. NCActionCenter.shared.saveAlbum(metadata: metadata)
  173. } else {
  174. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  175. }
  176. }
  177. }
  178. completion?()
  179. }
  180. )
  181. }
  182. /// Set (or remove) a file as *available offline*. Downloads the file if not downloaded already
  183. static func setAvailableOfflineAction(selectedMetadatas: [tableMetadata], isAnyOffline: Bool, viewController: UIViewController, order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  184. NCMenuAction(
  185. title: isAnyOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  186. icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
  187. order: order,
  188. action: { _ in
  189. if !isAnyOffline, selectedMetadatas.count > 3 {
  190. let alert = UIAlertController(
  191. title: NSLocalizedString("_set_available_offline_", comment: ""),
  192. message: NSLocalizedString("_select_offline_warning_", comment: ""),
  193. preferredStyle: .alert)
  194. alert.addAction(UIAlertAction(title: NSLocalizedString("_continue_", comment: ""), style: .default, handler: { _ in
  195. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  196. completion?()
  197. }))
  198. alert.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel))
  199. viewController.present(alert, animated: true)
  200. } else {
  201. selectedMetadatas.forEach { NCActionCenter.shared.setMetadataAvalableOffline($0, isOffline: isAnyOffline) }
  202. completion?()
  203. }
  204. }
  205. )
  206. }
  207. /// Open view that lets the user move or copy the files within Nextcloud
  208. static func moveOrCopyAction(selectedMetadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  209. NCMenuAction(
  210. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  211. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  212. order: order,
  213. action: { _ in
  214. NCActionCenter.shared.openSelectView(items: selectedMetadatas)
  215. completion?()
  216. }
  217. )
  218. }
  219. /// Open AirPrint view to print a single file
  220. static func printAction(metadata: tableMetadata, order: Int = 0) -> NCMenuAction {
  221. NCMenuAction(
  222. title: NSLocalizedString("_print_", comment: ""),
  223. icon: NCUtility.shared.loadImage(named: "printer"),
  224. order: order,
  225. action: { _ in
  226. if CCUtility.fileProviderStorageExists(metadata) {
  227. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorPrint, "error": NKError(), "account": metadata.account])
  228. } else {
  229. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorPrint) { _, _ in }
  230. }
  231. }
  232. )
  233. }
  234. /// Lock or unlock a file using *files_lock*
  235. static func lockUnlockFiles(shouldLock: Bool, metadatas: [tableMetadata], order: Int = 0, completion: (() -> Void)? = nil) -> NCMenuAction {
  236. let titleKey: String
  237. if metadatas.count == 1 {
  238. titleKey = shouldLock ? "_lock_file_" : "_unlock_file_"
  239. } else {
  240. titleKey = shouldLock ? "_lock_selected_files_" : "_unlock_selected_files_"
  241. }
  242. let imageName = !shouldLock ? "lock_open" : "lock"
  243. return NCMenuAction(
  244. title: NSLocalizedString(titleKey, comment: ""),
  245. icon: NCUtility.shared.loadImage(named: imageName),
  246. order: order,
  247. action: { _ in
  248. for metadata in metadatas where metadata.lock != shouldLock {
  249. NCNetworking.shared.lockUnlockFile(metadata, shoulLock: shouldLock)
  250. }
  251. completion?()
  252. }
  253. )
  254. }
  255. }