NCViewer+Menu.swift 11 KB

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