NCViewer+Menu.swift 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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?) {
  28. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId) else { return }
  29. var actions = [NCMenuAction]()
  30. var titleFavorite = NSLocalizedString("_add_favorites_", comment: "")
  31. if metadata.favorite { titleFavorite = NSLocalizedString("_remove_favorites_", comment: "") }
  32. let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  33. let isDirectoryE2EE = NCUtility.shared.isDirectoryE2EE(metadata: metadata)
  34. let isOffline = localFile?.offline == true
  35. //
  36. // FAVORITE
  37. // Workaround: PROPPATCH doesn't work
  38. // https://github.com/nextcloud/files_lock/issues/68
  39. if !metadata.lock {
  40. actions.append(
  41. NCMenuAction(
  42. title: titleFavorite,
  43. icon: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite),
  44. action: { _ in
  45. NCNetworking.shared.favoriteMetadata(metadata) { error in
  46. if error != .success {
  47. NCContentPresenter.shared.showError(error: error)
  48. }
  49. }
  50. }
  51. )
  52. )
  53. }
  54. //
  55. // DETAIL
  56. //
  57. if !appDelegate.disableSharesView {
  58. actions.append(
  59. NCMenuAction(
  60. title: NSLocalizedString("_details_", comment: ""),
  61. icon: NCUtility.shared.loadImage(named: "info"),
  62. action: { _ in
  63. NCFunctionCenter.shared.openShare(viewController: viewController, metadata: metadata, indexPage: .activity)
  64. }
  65. )
  66. )
  67. }
  68. //
  69. // OFFLINE
  70. //
  71. if metadata.session == "" && !webView && !metadata.isViewOnly {
  72. actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: viewController))
  73. }
  74. //
  75. // OPEN IN
  76. //
  77. if metadata.session == "" && !webView && !metadata.isViewOnly {
  78. actions.append(.openInAction(selectedMetadatas: [metadata], viewController: viewController))
  79. }
  80. //
  81. // PRINT
  82. //
  83. if metadata.classFile == NKCommon.typeClassFile.image.rawValue || (!metadata.isViewOnly && (metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf")) {
  84. actions.append(
  85. NCMenuAction(
  86. title: NSLocalizedString("_print_", comment: ""),
  87. icon: NCUtility.shared.loadImage(named: "printer"),
  88. action: { _ in
  89. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  90. }
  91. )
  92. )
  93. }
  94. //
  95. // CONVERSION VIDEO TO MPEG4 (MFFF Lib)
  96. //
  97. #if MFFFLIB
  98. if metadata.classFile == NKCommon.typeClassFile.video.rawValue {
  99. actions.append(
  100. NCMenuAction(
  101. title: NSLocalizedString("_video_processing_", comment: ""),
  102. icon: NCUtility.shared.loadImage(named: "film"),
  103. action: { menuAction in
  104. if let ncplayer = (viewController as? NCViewerMediaPage)?.currentViewController.ncplayer {
  105. ncplayer.convertVideo(withAlert: false)
  106. }
  107. }
  108. )
  109. )
  110. }
  111. #endif
  112. //
  113. // SAVE IMAGE / VIDEO
  114. //
  115. if metadata.classFile == NKCommon.typeClassFile.image.rawValue || metadata.classFile == NKCommon.typeClassFile.video.rawValue {
  116. actions.append(.saveMediaAction(selectedMediaMetadatas: [metadata]))
  117. }
  118. //
  119. // SAVE AS SCAN
  120. //
  121. if metadata.classFile == NKCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml" {
  122. actions.append(
  123. NCMenuAction(
  124. title: NSLocalizedString("_save_as_scan_", comment: ""),
  125. icon: NCUtility.shared.loadImage(named: "viewfinder.circle"),
  126. action: { _ in
  127. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan)
  128. }
  129. )
  130. )
  131. }
  132. //
  133. // RENAME
  134. //
  135. if !webView, !metadata.lock, !metadata.isViewOnly {
  136. actions.append(
  137. NCMenuAction(
  138. title: NSLocalizedString("_rename_", comment: ""),
  139. icon: NCUtility.shared.loadImage(named: "pencil"),
  140. action: { _ in
  141. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  142. vcRename.metadata = metadata
  143. vcRename.disableChangeExt = true
  144. vcRename.imagePreview = imageIcon
  145. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  146. viewController.present(popup, animated: true)
  147. }
  148. }
  149. )
  150. )
  151. }
  152. //
  153. // COPY - MOVE
  154. //
  155. if !webView, !metadata.isViewOnly {
  156. actions.append(.moveOrCopyAction(selectedMetadatas: [metadata]))
  157. }
  158. //
  159. // COPY
  160. //
  161. if !metadata.isViewOnly {
  162. actions.append(.copyAction(selectOcId: [metadata.ocId], hudView: viewController.view))
  163. }
  164. //
  165. // VIEW IN FOLDER
  166. //
  167. if !webView {
  168. actions.append(
  169. NCMenuAction(
  170. title: NSLocalizedString("_view_in_folder_", comment: ""),
  171. icon: NCUtility.shared.loadImage(named: "arrow.forward.square"),
  172. action: { menuAction in
  173. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil)
  174. }
  175. )
  176. )
  177. }
  178. //
  179. // DOWNLOAD IMAGE MAX RESOLUTION
  180. //
  181. if metadata.session == "" {
  182. if metadata.classFile == NKCommon.typeClassFile.image.rawValue && !CCUtility.fileProviderStorageExists(metadata) && metadata.session == "" {
  183. actions.append(
  184. NCMenuAction(
  185. title: NSLocalizedString("_download_image_max_", comment: ""),
  186. icon: NCUtility.shared.loadImage(named: "square.and.arrow.down"),
  187. action: { _ in
  188. NCNetworking.shared.download(metadata: metadata, selector: "") { _, _ in }
  189. }
  190. )
  191. )
  192. }
  193. }
  194. //
  195. // PDF
  196. //
  197. if metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" {
  198. actions.append(
  199. NCMenuAction(
  200. title: NSLocalizedString("_search_", comment: ""),
  201. icon: UIImage(named: "search")!.image(color: UIColor.systemGray, size: 50),
  202. action: { _ in
  203. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuSearchTextPDF)
  204. }
  205. )
  206. )
  207. actions.append(
  208. NCMenuAction(
  209. title: NSLocalizedString("_go_to_page_", comment: ""),
  210. icon: NCUtility.shared.loadImage(named: "repeat"),
  211. action: { _ in
  212. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuGotToPageInPDF)
  213. }
  214. )
  215. )
  216. }
  217. //
  218. // MODIFY
  219. //
  220. if !isDirectoryE2EE && !metadata.isViewOnly && metadata.contentType != "image/gif" && (metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" || metadata.classFile == NKCommon.typeClassFile.image.rawValue) {
  221. actions.append(
  222. NCMenuAction(
  223. title: NSLocalizedString("_modify_", comment: ""),
  224. icon: NCUtility.shared.loadImage(named: "pencil.tip.crop.circle"),
  225. action: { _ in
  226. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  227. }
  228. )
  229. )
  230. }
  231. //
  232. // DELETE
  233. //
  234. if !webView {
  235. actions.append(.deleteAction(selectedMetadatas: [metadata], metadataFolder: nil, viewController: viewController))
  236. }
  237. viewController.presentMenu(with: actions)
  238. }
  239. }