NCViewer+Menu.swift 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  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. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  145. vcRename.metadata = metadata
  146. vcRename.disableChangeExt = true
  147. vcRename.imagePreview = imageIcon
  148. vcRename.indexPath = indexPath
  149. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  150. viewController.present(popup, animated: true)
  151. }
  152. }
  153. )
  154. )
  155. }
  156. //
  157. // COPY - MOVE
  158. //
  159. if !webView, metadata.isCopyableMovable {
  160. actions.append(.moveOrCopyAction(selectedMetadatas: [metadata], viewController: viewController, indexPath: []))
  161. }
  162. //
  163. // DOWNLOAD FULL RESOLUTION
  164. //
  165. if !webView, metadata.session.isEmpty, !self.utilityFileSystem.fileProviderStorageExists(metadata) {
  166. actions.append(
  167. NCMenuAction(
  168. title: NSLocalizedString("_try_download_full_resolution_", comment: ""),
  169. icon: utility.loadImage(named: "photo", colors: [NCBrandColor.shared.iconImageColor]),
  170. action: { _ in
  171. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  172. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  173. selector: "",
  174. sceneIdentifier: sceneIdentifier) else { return }
  175. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  176. }
  177. )
  178. )
  179. }
  180. //
  181. // PDF
  182. //
  183. if metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" {
  184. actions.append(
  185. NCMenuAction(
  186. title: NSLocalizedString("_search_", comment: ""),
  187. icon: utility.loadImage(named: "magnifyingglass", colors: [NCBrandColor.shared.iconImageColor]),
  188. action: { _ in
  189. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuSearchTextPDF)
  190. }
  191. )
  192. )
  193. actions.append(
  194. NCMenuAction(
  195. title: NSLocalizedString("_go_to_page_", comment: ""),
  196. icon: utility.loadImage(named: "book.pages", colors: [NCBrandColor.shared.iconImageColor]),
  197. action: { _ in
  198. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuGotToPageInPDF)
  199. }
  200. )
  201. )
  202. }
  203. //
  204. // MODIFY WITH QUICK LOOK
  205. //
  206. if !webView, metadata.isModifiableWithQuickLook {
  207. actions.append(
  208. NCMenuAction(
  209. title: NSLocalizedString("_modify_", comment: ""),
  210. icon: utility.loadImage(named: "pencil.tip.crop.circle", colors: [NCBrandColor.shared.iconImageColor]),
  211. action: { _ in
  212. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  213. NotificationCenter.default.post(
  214. name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile),
  215. object: nil,
  216. userInfo: ["ocId": metadata.ocId,
  217. "selector": NCGlobal.shared.selectorLoadFileQuickLook,
  218. "error": NKError(),
  219. "account": metadata.account])
  220. } else {
  221. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  222. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  223. selector: NCGlobal.shared.selectorLoadFileQuickLook,
  224. sceneIdentifier: sceneIdentifier) else { return }
  225. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  226. }
  227. }
  228. )
  229. )
  230. }
  231. //
  232. // DELETE
  233. //
  234. if !webView, metadata.isDeletable {
  235. actions.append(.deleteAction(selectedMetadatas: [metadata], indexPaths: [], metadataFolder: nil, viewController: viewController))
  236. }
  237. viewController.presentMenu(with: actions)
  238. }
  239. }