NCViewer+Menu.swift 12 KB

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