NCViewer+Menu.swift 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. let menuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu
  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. //
  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: { menuAction in
  55. NCNetworking.shared.favoriteMetadata(metadata, urlBase: self.appDelegate.urlBase) { (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: { menuAction in
  72. NCFunctionCenter.shared.openShare(ViewController: viewController, metadata: metadata, indexPage: 0)
  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: { menuAction 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: { menuAction in
  104. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  105. }
  106. )
  107. )
  108. }
  109. //
  110. // PRINT
  111. //
  112. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage || metadata.contentType == "application/pdf" {
  113. actions.append(
  114. NCMenuAction(
  115. title: NSLocalizedString("_print_", comment: ""),
  116. icon: NCUtility.shared.loadImage(named: "printer"),
  117. action: { menuAction in
  118. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  119. }
  120. )
  121. )
  122. }
  123. //
  124. // SAVE IMAGE / VIDEO
  125. //
  126. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCGlobal.shared.metadataTypeFileVideo {
  127. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  128. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  129. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  130. if metadataMOV != nil {
  131. title = NSLocalizedString("_livephoto_save_", comment: "")
  132. icon = NCUtility.shared.loadImage(named: "livephoto")
  133. }
  134. actions.append(
  135. NCMenuAction(
  136. title: title,
  137. icon: icon,
  138. action: { menuAction in
  139. if metadataMOV != nil {
  140. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  141. } else {
  142. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  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: { menuAction 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(
  173. NCMenuAction(
  174. title: NSLocalizedString("_move_or_copy_", comment: ""),
  175. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  176. action: { menuAction in
  177. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  178. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  179. let viewController = navigationController.topViewController as! NCSelect
  180. viewController.delegate = NCViewer.shared
  181. viewController.typeOfCommandView = .copyMove
  182. viewController.items = [metadata]
  183. self.appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  184. }
  185. )
  186. )
  187. }
  188. //
  189. // COPY
  190. //
  191. actions.append(
  192. NCMenuAction(
  193. title: NSLocalizedString("_copy_file_", comment: ""),
  194. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  195. action: { menuAction in
  196. self.appDelegate.pasteboardOcIds = [metadata.ocId];
  197. NCFunctionCenter.shared.copyPasteboard()
  198. }
  199. )
  200. )
  201. //
  202. // VIEW IN FOLDER
  203. //
  204. if !webView {
  205. if appDelegate.activeFileViewInFolder == nil {
  206. actions.append(
  207. NCMenuAction(
  208. title: NSLocalizedString("_view_in_folder_", comment: ""),
  209. icon: NCUtility.shared.loadImage(named: "arrow.forward.square"),
  210. action: { menuAction in
  211. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  212. }
  213. )
  214. )
  215. }
  216. }
  217. //
  218. // DOWNLOAD IMAGE MAX RESOLUTION
  219. //
  220. if metadata.session == "" {
  221. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage && !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.session == "" {
  222. actions.append(
  223. NCMenuAction(
  224. title: NSLocalizedString("_download_image_max_", comment: ""),
  225. icon: NCUtility.shared.loadImage(named: "square.and.arrow.down"),
  226. action: { menuAction in
  227. NCNetworking.shared.download(metadata: metadata, selector: "") { (_) in }
  228. }
  229. )
  230. )
  231. }
  232. }
  233. //
  234. // PDF
  235. //
  236. if (metadata.typeFile == NCGlobal.shared.metadataTypeFileDocument && metadata.contentType == "application/pdf" ) {
  237. actions.append(
  238. NCMenuAction(
  239. title: NSLocalizedString("_search_", comment: ""),
  240. icon: UIImage(named: "search")!.image(color: NCBrandColor.shared.gray, size: 50),
  241. action: { menuAction in
  242. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuSearchTextPDF)
  243. }
  244. )
  245. )
  246. }
  247. //
  248. // DELETE
  249. //
  250. if !webView {
  251. actions.append(
  252. NCMenuAction(
  253. title: titleDelete,
  254. icon: NCUtility.shared.loadImage(named: "trash"),
  255. action: { menuAction in
  256. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  257. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  258. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, onlyLocal: false) { (errorCode, errorDescription) in
  259. if errorCode != 0 {
  260. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  261. }
  262. }
  263. })
  264. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  265. viewController.present(alertController, animated: true, completion:nil)
  266. }
  267. )
  268. )
  269. }
  270. menuViewController.actions = actions
  271. let menuPanelController = NCMenuPanelController()
  272. menuPanelController.parentPresenter = viewController
  273. menuPanelController.delegate = menuViewController
  274. menuPanelController.set(contentViewController: menuViewController)
  275. menuPanelController.track(scrollView: menuViewController.tableView)
  276. viewController.present(menuPanelController, animated: true, completion: nil)
  277. }
  278. }