NCViewer+Menu.swift 9.7 KB

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