NCViewer+Menu.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  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 NCCommunication
  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 isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  34. let isOffline = localFile?.offline == true
  35. //
  36. // FAVORITE
  37. //
  38. actions.append(
  39. NCMenuAction(
  40. title: titleFavorite,
  41. icon: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite),
  42. action: { _ in
  43. NCNetworking.shared.favoriteMetadata(metadata) { errorCode, errorDescription in
  44. if errorCode != 0 {
  45. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  46. }
  47. }
  48. }
  49. )
  50. )
  51. //
  52. // DETAIL
  53. //
  54. if !appDelegate.disableSharesView {
  55. actions.append(
  56. NCMenuAction(
  57. title: NSLocalizedString("_details_", comment: ""),
  58. icon: NCUtility.shared.loadImage(named: "info"),
  59. action: { _ in
  60. NCFunctionCenter.shared.openShare(viewController: viewController, metadata: metadata, indexPage: .activity)
  61. }
  62. )
  63. )
  64. }
  65. //
  66. // OFFLINE
  67. //
  68. if metadata.session == "" && !webView {
  69. actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: viewController))
  70. }
  71. //
  72. // OPEN IN
  73. //
  74. if metadata.session == "" && !webView {
  75. actions.append(.openInAction(selectedMetadatas: [metadata], viewController: viewController))
  76. }
  77. //
  78. // PRINT
  79. //
  80. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {
  81. actions.append(
  82. NCMenuAction(
  83. title: NSLocalizedString("_print_", comment: ""),
  84. icon: NCUtility.shared.loadImage(named: "printer"),
  85. action: { _ in
  86. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  87. }
  88. )
  89. )
  90. }
  91. //
  92. // CONVERSION VIDEO TO MPEG4 (MFFF Lib)
  93. //
  94. #if MFFFLIB
  95. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  96. actions.append(
  97. NCMenuAction(
  98. title: NSLocalizedString("_video_processing_", comment: ""),
  99. icon: NCUtility.shared.loadImage(named: "film"),
  100. action: { menuAction in
  101. if let ncplayer = (viewController as? NCViewerMediaPage)?.currentViewController.ncplayer {
  102. ncplayer.convertVideo()
  103. }
  104. }
  105. )
  106. )
  107. }
  108. #endif
  109. //
  110. // SAVE IMAGE / VIDEO
  111. //
  112. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  113. actions.append(.saveMediaAction(selectedMediaMetadatas: [metadata]))
  114. }
  115. //
  116. // SAVE AS SCAN
  117. //
  118. if #available(iOS 13.0, *) {
  119. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml" {
  120. actions.append(
  121. NCMenuAction(
  122. title: NSLocalizedString("_save_as_scan_", comment: ""),
  123. icon: NCUtility.shared.loadImage(named: "viewfinder.circle"),
  124. action: { _ in
  125. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan)
  126. }
  127. )
  128. )
  129. }
  130. }
  131. //
  132. // RENAME
  133. //
  134. if !webView {
  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 {
  155. actions.append(.moveOrCopyAction(selectedMetadatas: [metadata]))
  156. }
  157. //
  158. // COPY
  159. //
  160. actions.append(.copyAction(selectOcId: [metadata.ocId], hudView: viewController.view))
  161. //
  162. // VIEW IN FOLDER
  163. //
  164. if !webView {
  165. actions.append(
  166. NCMenuAction(
  167. title: NSLocalizedString("_view_in_folder_", comment: ""),
  168. icon: NCUtility.shared.loadImage(named: "arrow.forward.square"),
  169. action: { menuAction in
  170. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  171. }
  172. )
  173. )
  174. }
  175. //
  176. // DOWNLOAD IMAGE MAX RESOLUTION
  177. //
  178. if metadata.session == "" {
  179. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && !CCUtility.fileProviderStorageExists(metadata) && metadata.session == "" {
  180. actions.append(
  181. NCMenuAction(
  182. title: NSLocalizedString("_download_image_max_", comment: ""),
  183. icon: NCUtility.shared.loadImage(named: "square.and.arrow.down"),
  184. action: { _ in
  185. NCNetworking.shared.download(metadata: metadata, selector: "") { _ in }
  186. }
  187. )
  188. )
  189. }
  190. }
  191. //
  192. // PDF
  193. //
  194. if metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" {
  195. actions.append(
  196. NCMenuAction(
  197. title: NSLocalizedString("_search_", comment: ""),
  198. icon: UIImage(named: "search")!.image(color: NCBrandColor.shared.gray, size: 50),
  199. action: { _ in
  200. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuSearchTextPDF)
  201. }
  202. )
  203. )
  204. var title = ""
  205. var icon = UIImage()
  206. if CCUtility.getPDFDisplayDirection() == .horizontal {
  207. title = NSLocalizedString("_pdf_vertical_", comment: "")
  208. icon = UIImage(named: "pdf-vertical")!.image(color: NCBrandColor.shared.gray, size: 50)
  209. } else {
  210. title = NSLocalizedString("_pdf_horizontal_", comment: "")
  211. icon = UIImage(named: "pdf-horizontal")!.image(color: NCBrandColor.shared.gray, size: 50)
  212. }
  213. actions.append(
  214. NCMenuAction(
  215. title: title,
  216. icon: icon,
  217. action: { _ in
  218. if CCUtility.getPDFDisplayDirection() == .horizontal {
  219. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuPDFDisplayDirection, userInfo: ["direction": PDFDisplayDirection.vertical])
  220. } else {
  221. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuPDFDisplayDirection, userInfo: ["direction": PDFDisplayDirection.horizontal])
  222. }
  223. }
  224. )
  225. )
  226. actions.append(
  227. NCMenuAction(
  228. title: NSLocalizedString("_go_to_page_", comment: ""),
  229. icon: NCUtility.shared.loadImage(named: "repeat"),
  230. action: { _ in
  231. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuGotToPageInPDF)
  232. }
  233. )
  234. )
  235. }
  236. //
  237. // MODIFY
  238. //
  239. if #available(iOS 13.0, *) {
  240. if !isFolderEncrypted && metadata.contentType != "image/gif" && (metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" || metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue) {
  241. actions.append(
  242. NCMenuAction(
  243. title: NSLocalizedString("_modify_", comment: ""),
  244. icon: NCUtility.shared.loadImage(named: "pencil.tip.crop.circle"),
  245. action: { _ in
  246. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  247. }
  248. )
  249. )
  250. }
  251. }
  252. //
  253. // DELETE
  254. //
  255. if !webView {
  256. actions.append(.deleteAction(selectedMetadatas: [metadata], metadataFolder: nil, viewController: viewController))
  257. }
  258. viewController.presentMenu(with: actions)
  259. }
  260. }