NCViewer+Menu.swift 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  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. var titleDelete = NSLocalizedString("_delete_", comment: "")
  40. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: nil) {
  41. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  42. } else if metadata.directory {
  43. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  44. } else {
  45. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  46. }
  47. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  48. //
  49. // FAVORITE
  50. //
  51. actions.append(
  52. NCMenuAction(
  53. title: titleFavorite,
  54. icon: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite),
  55. action: { _ in
  56. NCNetworking.shared.favoriteMetadata(metadata) { errorCode, errorDescription in
  57. if errorCode != 0 {
  58. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  59. }
  60. }
  61. }
  62. )
  63. )
  64. //
  65. // DETAIL
  66. //
  67. if !appDelegate.disableSharesView {
  68. actions.append(
  69. NCMenuAction(
  70. title: NSLocalizedString("_details_", comment: ""),
  71. icon: NCUtility.shared.loadImage(named: "info"),
  72. action: { _ in
  73. NCFunctionCenter.shared.openShare(viewController: viewController, metadata: metadata, indexPage: .activity)
  74. }
  75. )
  76. )
  77. }
  78. //
  79. // OFFLINE
  80. //
  81. if metadata.session == "" && !webView {
  82. actions.append(
  83. NCMenuAction(
  84. title: titleOffline,
  85. icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
  86. action: { _ in
  87. if (localFile == nil || !CCUtility.fileProviderStorageExists(metadata)) && metadata.session == "" {
  88. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorLoadOffline) { _ in }
  89. } else {
  90. NCManageDatabase.shared.setLocalFile(ocId: metadata.ocId, offline: !localFile!.offline)
  91. }
  92. }
  93. )
  94. )
  95. }
  96. //
  97. // OPEN IN
  98. //
  99. if metadata.session == "" && !webView {
  100. actions.append(
  101. NCMenuAction(
  102. title: NSLocalizedString("_open_in_", comment: ""),
  103. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  104. action: { _ in
  105. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  106. }
  107. )
  108. )
  109. }
  110. //
  111. // PRINT
  112. //
  113. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {
  114. actions.append(
  115. NCMenuAction(
  116. title: NSLocalizedString("_print_", comment: ""),
  117. icon: NCUtility.shared.loadImage(named: "printer"),
  118. action: { _ in
  119. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  120. }
  121. )
  122. )
  123. }
  124. //
  125. // CONVERSION VIDEO TO MPEG4 (MFFF Lib)
  126. //
  127. #if MFFFLIB
  128. if metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  129. actions.append(
  130. NCMenuAction(
  131. title: NSLocalizedString("_video_processing_", comment: ""),
  132. icon: NCUtility.shared.loadImage(named: "film"),
  133. action: { menuAction in
  134. if let ncplayer = (viewController as? NCViewerMediaPage)?.currentViewController.ncplayer {
  135. ncplayer.convertVideo()
  136. }
  137. }
  138. )
  139. )
  140. }
  141. #endif
  142. //
  143. // SAVE IMAGE / VIDEO
  144. //
  145. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  146. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  147. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  148. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  149. if metadataMOV != nil {
  150. title = NSLocalizedString("_livephoto_save_", comment: "")
  151. icon = NCUtility.shared.loadImage(named: "livephoto")
  152. }
  153. actions.append(
  154. NCMenuAction(
  155. title: title,
  156. icon: icon,
  157. action: { _ in
  158. if metadataMOV != nil {
  159. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  160. } else {
  161. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  162. }
  163. }
  164. )
  165. )
  166. }
  167. //
  168. // SAVE AS SCAN
  169. //
  170. if #available(iOS 13.0, *) {
  171. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml" {
  172. actions.append(
  173. NCMenuAction(
  174. title: NSLocalizedString("_save_as_scan_", comment: ""),
  175. icon: NCUtility.shared.loadImage(named: "viewfinder.circle"),
  176. action: { _ in
  177. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan)
  178. }
  179. )
  180. )
  181. }
  182. }
  183. //
  184. // RENAME
  185. //
  186. if !webView {
  187. actions.append(
  188. NCMenuAction(
  189. title: NSLocalizedString("_rename_", comment: ""),
  190. icon: NCUtility.shared.loadImage(named: "pencil"),
  191. action: { _ in
  192. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  193. vcRename.metadata = metadata
  194. vcRename.disableChangeExt = true
  195. vcRename.imagePreview = imageIcon
  196. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  197. viewController.present(popup, animated: true)
  198. }
  199. }
  200. )
  201. )
  202. }
  203. //
  204. // COPY - MOVE
  205. //
  206. if !webView {
  207. actions.append(
  208. NCMenuAction(
  209. title: NSLocalizedString("_move_or_copy_", comment: ""),
  210. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  211. action: { _ in
  212. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  213. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  214. let viewController = navigationController.topViewController as! NCSelect
  215. viewController.delegate = NCViewer.shared
  216. viewController.typeOfCommandView = .copyMove
  217. viewController.items = [metadata]
  218. self.appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  219. }
  220. )
  221. )
  222. }
  223. //
  224. // COPY
  225. //
  226. actions.append(
  227. NCMenuAction(
  228. title: NSLocalizedString("_copy_file_", comment: ""),
  229. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  230. action: { _ in
  231. NCFunctionCenter.shared.copyPasteboard(pasteboardOcIds: [metadata.ocId], hudView: viewController.view)
  232. }
  233. )
  234. )
  235. //
  236. // VIEW IN FOLDER
  237. //
  238. if !webView {
  239. actions.append(
  240. NCMenuAction(
  241. title: NSLocalizedString("_view_in_folder_", comment: ""),
  242. icon: NCUtility.shared.loadImage(named: "arrow.forward.square"),
  243. action: { menuAction in
  244. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  245. }
  246. )
  247. )
  248. }
  249. //
  250. // DOWNLOAD IMAGE MAX RESOLUTION
  251. //
  252. if metadata.session == "" {
  253. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && !CCUtility.fileProviderStorageExists(metadata) && metadata.session == "" {
  254. actions.append(
  255. NCMenuAction(
  256. title: NSLocalizedString("_download_image_max_", comment: ""),
  257. icon: NCUtility.shared.loadImage(named: "square.and.arrow.down"),
  258. action: { _ in
  259. NCNetworking.shared.download(metadata: metadata, selector: "") { _ in }
  260. }
  261. )
  262. )
  263. }
  264. }
  265. //
  266. // PDF
  267. //
  268. if metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" {
  269. actions.append(
  270. NCMenuAction(
  271. title: NSLocalizedString("_search_", comment: ""),
  272. icon: UIImage(named: "search")!.image(color: NCBrandColor.shared.gray, size: 50),
  273. action: { _ in
  274. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuSearchTextPDF)
  275. }
  276. )
  277. )
  278. var title = ""
  279. var icon = UIImage()
  280. if CCUtility.getPDFDisplayDirection() == .horizontal {
  281. title = NSLocalizedString("_pdf_vertical_", comment: "")
  282. icon = UIImage(named: "pdf-vertical")!.image(color: NCBrandColor.shared.gray, size: 50)
  283. } else {
  284. title = NSLocalizedString("_pdf_horizontal_", comment: "")
  285. icon = UIImage(named: "pdf-horizontal")!.image(color: NCBrandColor.shared.gray, size: 50)
  286. }
  287. actions.append(
  288. NCMenuAction(
  289. title: title,
  290. icon: icon,
  291. action: { _ in
  292. if CCUtility.getPDFDisplayDirection() == .horizontal {
  293. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuPDFDisplayDirection, userInfo: ["direction": PDFDisplayDirection.vertical])
  294. } else {
  295. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuPDFDisplayDirection, userInfo: ["direction": PDFDisplayDirection.horizontal])
  296. }
  297. }
  298. )
  299. )
  300. actions.append(
  301. NCMenuAction(
  302. title: NSLocalizedString("_go_to_page_", comment: ""),
  303. icon: NCUtility.shared.loadImage(named: "repeat"),
  304. action: { _ in
  305. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuGotToPageInPDF)
  306. }
  307. )
  308. )
  309. }
  310. //
  311. // MODIFY
  312. //
  313. if #available(iOS 13.0, *) {
  314. if !isFolderEncrypted && metadata.contentType != "image/gif" && (metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" || metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue) {
  315. actions.append(
  316. NCMenuAction(
  317. title: NSLocalizedString("_modify_", comment: ""),
  318. icon: NCUtility.shared.loadImage(named: "pencil.tip.crop.circle"),
  319. action: { _ in
  320. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  321. }
  322. )
  323. )
  324. }
  325. }
  326. //
  327. // DELETE
  328. //
  329. if !webView {
  330. actions.append(
  331. NCMenuAction(
  332. title: titleDelete,
  333. icon: NCUtility.shared.loadImage(named: "trash"),
  334. action: { _ in
  335. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  336. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  337. NCNetworking.shared.deleteMetadata(metadata, onlyLocalCache: false) { errorCode, errorDescription in
  338. if errorCode != 0 {
  339. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  340. }
  341. }
  342. })
  343. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  344. viewController.present(alertController, animated: true, completion: nil)
  345. }
  346. )
  347. )
  348. }
  349. viewController.presentMenu(with: actions)
  350. }
  351. }