NCViewer+Menu.swift 17 KB

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