NCViewer+Menu.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. //
  2. // NCViewer.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/02/2020.
  6. // Copyright © 2020 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 UIKit
  24. import FloatingPanel
  25. import NextcloudKit
  26. extension NCViewer {
  27. func toggleMenu(viewController: UIViewController, metadata: tableMetadata, webView: Bool, imageIcon: UIImage?, indexPath: IndexPath = IndexPath()) {
  28. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId),
  29. let sceneIdentifier = (viewController.tabBarController as? NCMainTabBarController)?.sceneIdentifier else { return }
  30. var actions = [NCMenuAction]()
  31. let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  32. let isOffline = localFile?.offline == true
  33. //
  34. // DETAIL
  35. //
  36. if !NCGlobal.shared.disableSharesView {
  37. actions.append(
  38. NCMenuAction(
  39. title: NSLocalizedString("_details_", comment: ""),
  40. icon: utility.loadImage(named: "info.circle", colors: [NCBrandColor.shared.iconImageColor]),
  41. action: { _ in
  42. NCActionCenter.shared.openShare(viewController: viewController, metadata: metadata, page: .activity)
  43. }
  44. )
  45. )
  46. }
  47. //
  48. // VIEW IN FOLDER
  49. //
  50. if !webView {
  51. actions.append(
  52. NCMenuAction(
  53. title: NSLocalizedString("_view_in_folder_", comment: ""),
  54. icon: utility.loadImage(named: "questionmark.folder", colors: [NCBrandColor.shared.iconImageColor]),
  55. action: { _ in
  56. NCActionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil, sceneIdentifier: sceneIdentifier)
  57. }
  58. )
  59. )
  60. }
  61. //
  62. // FAVORITE
  63. // Workaround: PROPPATCH doesn't work
  64. // https://github.com/nextcloud/files_lock/issues/68
  65. if !metadata.lock {
  66. actions.append(
  67. NCMenuAction(
  68. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  69. icon: utility.loadImage(named: metadata.favorite ? "star.slash" : "star", colors: [NCBrandColor.shared.yellowFavorite]),
  70. action: { _ in
  71. NCNetworking.shared.favoriteMetadata(metadata) { error in
  72. if error != .success {
  73. NCContentPresenter().showError(error: error)
  74. }
  75. }
  76. }
  77. )
  78. )
  79. }
  80. //
  81. // OFFLINE
  82. //
  83. if !webView, metadata.canSetAsAvailableOffline {
  84. actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: viewController))
  85. }
  86. //
  87. // SHARE
  88. //
  89. if !webView, metadata.canShare {
  90. actions.append(.share(selectedMetadatas: [metadata], viewController: viewController))
  91. }
  92. //
  93. // SAVE LIVE PHOTO
  94. //
  95. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata),
  96. let hudView = viewController.tabBarController?.view {
  97. actions.append(
  98. NCMenuAction(
  99. title: NSLocalizedString("_livephoto_save_", comment: ""),
  100. icon: NCUtility().loadImage(named: "livephoto", colors: [NCBrandColor.shared.iconImageColor]),
  101. action: { _ in
  102. NCNetworking.shared.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV, hudView: hudView))
  103. }
  104. )
  105. )
  106. }
  107. //
  108. // SAVE AS SCAN
  109. //
  110. if !webView, metadata.isSavebleAsImage {
  111. actions.append(
  112. NCMenuAction(
  113. title: NSLocalizedString("_save_as_scan_", comment: ""),
  114. icon: utility.loadImage(named: "doc.viewfinder", colors: [NCBrandColor.shared.iconImageColor]),
  115. action: { _ in
  116. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  117. NotificationCenter.default.post(
  118. name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile),
  119. object: nil,
  120. userInfo: ["ocId": metadata.ocId,
  121. "selector": NCGlobal.shared.selectorSaveAsScan,
  122. "error": NKError(),
  123. "account": metadata.account])
  124. } else {
  125. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  126. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  127. selector: NCGlobal.shared.selectorSaveAsScan,
  128. sceneIdentifier: sceneIdentifier) else { return }
  129. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  130. }
  131. }
  132. )
  133. )
  134. }
  135. //
  136. // RENAME
  137. //
  138. if !webView, metadata.isRenameable {
  139. actions.append(
  140. NCMenuAction(
  141. title: NSLocalizedString("_rename_", comment: ""),
  142. icon: utility.loadImage(named: "text.cursor", colors: [NCBrandColor.shared.iconImageColor]),
  143. action: { _ in
  144. viewController.present(UIAlertController.renameFile(metadata: metadata, indexPath: indexPath), animated: true)
  145. }
  146. )
  147. )
  148. }
  149. //
  150. // COPY - MOVE
  151. //
  152. if !webView, metadata.isCopyableMovable {
  153. actions.append(.moveOrCopyAction(selectedMetadatas: [metadata], viewController: viewController, indexPath: []))
  154. }
  155. //
  156. // DOWNLOAD FULL RESOLUTION
  157. //
  158. if !webView, metadata.session.isEmpty, !self.utilityFileSystem.fileProviderStorageExists(metadata) {
  159. actions.append(
  160. NCMenuAction(
  161. title: NSLocalizedString("_try_download_full_resolution_", comment: ""),
  162. icon: utility.loadImage(named: "photo", colors: [NCBrandColor.shared.iconImageColor]),
  163. action: { _ in
  164. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  165. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  166. selector: "",
  167. sceneIdentifier: sceneIdentifier) else { return }
  168. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  169. }
  170. )
  171. )
  172. }
  173. //
  174. // PDF
  175. //
  176. if metadata.isPDF {
  177. actions.append(
  178. NCMenuAction(
  179. title: NSLocalizedString("_search_", comment: ""),
  180. icon: utility.loadImage(named: "magnifyingglass", colors: [NCBrandColor.shared.iconImageColor]),
  181. action: { _ in
  182. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuSearchTextPDF)
  183. }
  184. )
  185. )
  186. actions.append(
  187. NCMenuAction(
  188. title: NSLocalizedString("_go_to_page_", comment: ""),
  189. icon: utility.loadImage(named: "book.pages", colors: [NCBrandColor.shared.iconImageColor]),
  190. action: { _ in
  191. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuGotToPageInPDF)
  192. }
  193. )
  194. )
  195. }
  196. //
  197. // MODIFY WITH QUICK LOOK
  198. //
  199. if !webView, metadata.isModifiableWithQuickLook {
  200. actions.append(
  201. NCMenuAction(
  202. title: NSLocalizedString("_modify_", comment: ""),
  203. icon: utility.loadImage(named: "pencil.tip.crop.circle", colors: [NCBrandColor.shared.iconImageColor]),
  204. action: { _ in
  205. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  206. NotificationCenter.default.post(
  207. name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile),
  208. object: nil,
  209. userInfo: ["ocId": metadata.ocId,
  210. "selector": NCGlobal.shared.selectorLoadFileQuickLook,
  211. "error": NKError(),
  212. "account": metadata.account])
  213. } else {
  214. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  215. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  216. selector: NCGlobal.shared.selectorLoadFileQuickLook,
  217. sceneIdentifier: sceneIdentifier) else { return }
  218. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  219. }
  220. }
  221. )
  222. )
  223. }
  224. //
  225. // DELETE
  226. //
  227. if !webView, metadata.isDeletable {
  228. actions.append(.deleteAction(selectedMetadatas: [metadata], indexPaths: [], metadataFolder: nil, viewController: viewController))
  229. }
  230. viewController.presentMenu(with: actions)
  231. }
  232. }