NCViewer+Menu.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327
  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 FloatingPanel
  24. import NCCommunication
  25. extension NCViewer {
  26. func toggleMenu(viewController: UIViewController, metadata: tableMetadata, webView: Bool) {
  27. let menuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateInitialViewController() as! NCMenu
  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. //
  47. // FAVORITE
  48. //
  49. actions.append(
  50. NCMenuAction(
  51. title: titleFavorite,
  52. icon: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite),
  53. action: { menuAction in
  54. NCNetworking.shared.favoriteMetadata(metadata, urlBase: self.appDelegate.urlBase) { (errorCode, errorDescription) in
  55. if errorCode != 0 {
  56. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  57. }
  58. }
  59. }
  60. )
  61. )
  62. //
  63. // DETAIL
  64. //
  65. if !appDelegate.disableSharesView {
  66. actions.append(
  67. NCMenuAction(
  68. title: NSLocalizedString("_details_", comment: ""),
  69. icon: NCUtility.shared.loadImage(named: "info"),
  70. action: { menuAction in
  71. NCFunctionCenter.shared.openShare(ViewController: viewController, metadata: metadata, indexPage: 0)
  72. }
  73. )
  74. )
  75. }
  76. //
  77. // OFFLINE
  78. //
  79. if metadata.session == "" && !webView {
  80. actions.append(
  81. NCMenuAction(
  82. title: titleOffline,
  83. icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
  84. action: { menuAction in
  85. if ((localFile == nil || !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView)) && metadata.session == "") {
  86. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorLoadOffline) { (_) in }
  87. } else {
  88. NCManageDatabase.shared.setLocalFile(ocId: metadata.ocId, offline: !localFile!.offline)
  89. }
  90. }
  91. )
  92. )
  93. }
  94. //
  95. // OPEN IN
  96. //
  97. if metadata.session == "" && !webView {
  98. actions.append(
  99. NCMenuAction(
  100. title: NSLocalizedString("_open_in_", comment: ""),
  101. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  102. action: { menuAction in
  103. NCFunctionCenter.shared.downloadOpen(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  104. }
  105. )
  106. )
  107. }
  108. //
  109. // PRINT
  110. //
  111. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage {
  112. actions.append(
  113. NCMenuAction(
  114. title: NSLocalizedString("_print_", comment: ""),
  115. icon: NCUtility.shared.loadImage(named: "printer"),
  116. action: { menuAction in
  117. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  118. NCFunctionCenter.shared.printDocument(metadata: metadata)
  119. } else {
  120. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  121. }
  122. }
  123. )
  124. )
  125. }
  126. //
  127. // SAVE IMAGE / VIDEO
  128. //
  129. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCGlobal.shared.metadataTypeFileVideo {
  130. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  131. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  132. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  133. if metadataMOV != nil {
  134. title = NSLocalizedString("_livephoto_save_", comment: "")
  135. icon = NCUtility.shared.loadImage(named: "livephoto")
  136. }
  137. actions.append(
  138. NCMenuAction(
  139. title: title,
  140. icon: icon,
  141. action: { menuAction in
  142. if metadataMOV != nil {
  143. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  144. } else {
  145. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  146. }
  147. }
  148. )
  149. )
  150. }
  151. //
  152. // RENAME
  153. //
  154. if !webView {
  155. actions.append(
  156. NCMenuAction(
  157. title: NSLocalizedString("_rename_", comment: ""),
  158. icon: NCUtility.shared.loadImage(named: "pencil"),
  159. action: { menuAction in
  160. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  161. vcRename.metadata = metadata
  162. vcRename.disableChangeExt = true
  163. let popup = NCPopupViewController(contentController: vcRename, popupWidth: 300, popupHeight: 360)
  164. viewController.present(popup, animated: true)
  165. }
  166. }
  167. )
  168. )
  169. }
  170. //
  171. // COPY - MOVE
  172. //
  173. if !webView {
  174. actions.append(
  175. NCMenuAction(
  176. title: NSLocalizedString("_move_or_copy_", comment: ""),
  177. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  178. action: { menuAction in
  179. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  180. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  181. let viewController = navigationController.topViewController as! NCSelect
  182. viewController.delegate = NCViewer.shared
  183. viewController.hideButtonCreateFolder = false
  184. viewController.items = [metadata]
  185. viewController.selectFile = false
  186. viewController.includeDirectoryE2EEncryption = false
  187. viewController.includeImages = false
  188. viewController.type = ""
  189. viewController.titleButtonDone = NSLocalizedString("_move_", comment: "")
  190. viewController.titleButtonDone1 = NSLocalizedString("_copy_", comment: "")
  191. viewController.isButtonDone1Hide = false
  192. viewController.isOverwriteHide = false
  193. navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  194. self.appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  195. }
  196. )
  197. )
  198. }
  199. //
  200. // COPY
  201. //
  202. actions.append(
  203. NCMenuAction(
  204. title: NSLocalizedString("_copy_file_", comment: ""),
  205. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  206. action: { menuAction in
  207. self.appDelegate.pasteboardOcIds = [metadata.ocId];
  208. NCFunctionCenter.shared.copyPasteboard()
  209. }
  210. )
  211. )
  212. //
  213. // VIEW IN FOLDER
  214. //
  215. if !webView {
  216. if appDelegate.activeFileViewInFolder == nil {
  217. actions.append(
  218. NCMenuAction(
  219. title: NSLocalizedString("_view_in_folder_", comment: ""),
  220. icon: NCUtility.shared.loadImage(named: "folder-search-outline"),
  221. action: { menuAction in
  222. NCFunctionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  223. }
  224. )
  225. )
  226. }
  227. }
  228. //
  229. // DOWNLOAD IMAGE MAX RESOLUTION
  230. //
  231. if metadata.session == "" {
  232. if metadata.typeFile == NCGlobal.shared.metadataTypeFileImage && !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.session == "" {
  233. actions.append(
  234. NCMenuAction(
  235. title: NSLocalizedString("_download_image_max_", comment: ""),
  236. icon: NCUtility.shared.loadImage(named: "square.and.arrow.down"),
  237. action: { menuAction in
  238. NCNetworking.shared.download(metadata: metadata, selector: "") { (_) in }
  239. }
  240. )
  241. )
  242. }
  243. }
  244. //
  245. // PDF
  246. //
  247. if (metadata.typeFile == NCGlobal.shared.metadataTypeFileDocument && metadata.contentType == "application/pdf" ) {
  248. actions.append(
  249. NCMenuAction(
  250. title: NSLocalizedString("_search_", comment: ""),
  251. icon: UIImage(named: "search")!.image(color: NCBrandColor.shared.icon, size: 50),
  252. action: { menuAction in
  253. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuSearchTextPDF)
  254. }
  255. )
  256. )
  257. }
  258. //
  259. // DELETE
  260. //
  261. if !webView {
  262. actions.append(
  263. NCMenuAction(
  264. title: titleDelete,
  265. icon: NCUtility.shared.loadImage(named: "trash"),
  266. action: { menuAction in
  267. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  268. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  269. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, onlyLocal: false) { (errorCode, errorDescription) in
  270. if errorCode != 0 {
  271. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  272. }
  273. }
  274. })
  275. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  276. viewController.present(alertController, animated: true, completion:nil)
  277. }
  278. )
  279. )
  280. }
  281. menuViewController.actions = actions
  282. let menuPanelController = NCMenuPanelController()
  283. menuPanelController.parentPresenter = viewController
  284. menuPanelController.delegate = menuViewController
  285. menuPanelController.set(contentViewController: menuViewController)
  286. menuPanelController.track(scrollView: menuViewController.tableView)
  287. viewController.present(menuPanelController, animated: true, completion: nil)
  288. }
  289. }