NCViewer+Menu.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  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. @objc func toggleMoreMenu(viewController: UIViewController, metadata: tableMetadata, webView: Bool) {
  27. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  28. mainMenuViewController.actions = self.initMoreMenu(viewController: viewController, metadata: metadata, webView: webView)
  29. let menuPanelController = NCMenuPanelController()
  30. menuPanelController.parentPresenter = viewController
  31. menuPanelController.delegate = mainMenuViewController
  32. menuPanelController.set(contentViewController: mainMenuViewController)
  33. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  34. viewController.present(menuPanelController, animated: true, completion: nil)
  35. }
  36. private func initMoreMenu(viewController: UIViewController, metadata: tableMetadata, webView: Bool) -> [NCMenuAction] {
  37. var actions = [NCMenuAction]()
  38. var titleFavorite = NSLocalizedString("_add_favorites_", comment: "")
  39. if metadata.favorite { titleFavorite = NSLocalizedString("_remove_favorites_", comment: "") }
  40. let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  41. var titleOffline = ""
  42. if (localFile == nil || localFile!.offline == false) {
  43. titleOffline = NSLocalizedString("_set_available_offline_", comment: "")
  44. } else {
  45. titleOffline = NSLocalizedString("_remove_available_offline_", comment: "")
  46. }
  47. var titleDelete = NSLocalizedString("_delete_", comment: "")
  48. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: nil) {
  49. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  50. } else if metadata.directory {
  51. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  52. } else {
  53. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  54. }
  55. //
  56. // FAVORITE
  57. //
  58. actions.append(
  59. NCMenuAction(
  60. title: titleFavorite,
  61. icon: UIImage(named: "favorite")!.image(color: NCBrandColor.shared.yellowFavorite, size: 50),
  62. action: { menuAction in
  63. NCNetworking.shared.favoriteMetadata(metadata, urlBase: self.appDelegate.urlBase) { (errorCode, errorDescription) in
  64. if errorCode != 0 {
  65. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  66. }
  67. }
  68. }
  69. )
  70. )
  71. //
  72. // OFFLINE
  73. //
  74. if metadata.session == "" && !webView {
  75. actions.append(
  76. NCMenuAction(
  77. title: titleOffline,
  78. icon: UIImage(named: "offline")!.image(color: NCBrandColor.shared.icon, size: 50),
  79. action: { menuAction in
  80. if ((localFile == nil || !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView)) && metadata.session == "") {
  81. NCNetworking.shared.download(metadata: metadata, selector: NCBrandGlobal.shared.selectorLoadOffline) { (_) in }
  82. } else {
  83. NCManageDatabase.shared.setLocalFile(ocId: metadata.ocId, offline: !localFile!.offline)
  84. }
  85. }
  86. )
  87. )
  88. }
  89. //
  90. // DETAIL
  91. //
  92. if !appDelegate.disableSharesView {
  93. actions.append(
  94. NCMenuAction(
  95. title: NSLocalizedString("_details_", comment: ""),
  96. icon: UIImage(named: "details")!.image(color: NCBrandColor.shared.icon, size: 50),
  97. action: { menuAction in
  98. NCNetworkingNotificationCenter.shared.openShare(ViewController: viewController, metadata: metadata, indexPage: 0)
  99. }
  100. )
  101. )
  102. }
  103. //
  104. // RENAME
  105. //
  106. if !webView {
  107. actions.append(
  108. NCMenuAction(
  109. title: NSLocalizedString("_rename_", comment: ""),
  110. icon: UIImage(named: "rename")!.image(color: NCBrandColor.shared.icon, size: 50),
  111. action: { menuAction in
  112. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  113. alertController.addTextField { (textField) in textField.text = metadata.fileNameView }
  114. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  115. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  116. let fileNameNew = alertController.textFields![0].text
  117. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew!, urlBase: self.appDelegate.urlBase, viewController: viewController) { (errorCode, errorDescription) in
  118. if errorCode != 0 {
  119. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  120. }
  121. }
  122. })
  123. alertController.addAction(cancelAction)
  124. alertController.addAction(okAction)
  125. viewController.present(alertController, animated: true, completion: nil)
  126. }
  127. )
  128. )
  129. }
  130. //
  131. // COPY - MOVE
  132. //
  133. if !webView {
  134. actions.append(
  135. NCMenuAction(
  136. title: NSLocalizedString("_move_or_copy_", comment: ""),
  137. icon: UIImage(named: "move")!.image(color: NCBrandColor.shared.icon, size: 50),
  138. action: { menuAction in
  139. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  140. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  141. let viewController = navigationController.topViewController as! NCSelect
  142. viewController.delegate = NCViewer.shared
  143. viewController.hideButtonCreateFolder = false
  144. viewController.items = [metadata]
  145. viewController.selectFile = false
  146. viewController.includeDirectoryE2EEncryption = false
  147. viewController.includeImages = false
  148. viewController.type = ""
  149. viewController.titleButtonDone = NSLocalizedString("_move_", comment: "")
  150. viewController.titleButtonDone1 = NSLocalizedString("_copy_", comment: "")
  151. viewController.isButtonDone1Hide = false
  152. viewController.isOverwriteHide = false
  153. navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  154. self.appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  155. }
  156. )
  157. )
  158. }
  159. //
  160. // VIEW IN FOLDER
  161. //
  162. if !webView {
  163. if appDelegate.activeFileViewInFolder == nil {
  164. actions.append(
  165. NCMenuAction(
  166. title: NSLocalizedString("_view_in_folder_", comment: ""),
  167. icon: UIImage(named: "viewInFolder")!.image(color: NCBrandColor.shared.icon, size: 50),
  168. action: { menuAction in
  169. NCCollectionCommon.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  170. }
  171. )
  172. )
  173. }
  174. }
  175. //
  176. // OPEN IN
  177. //
  178. if metadata.session == "" && !webView {
  179. actions.append(
  180. NCMenuAction(
  181. title: NSLocalizedString("_open_in_", comment: ""),
  182. icon: UIImage(named: "openFile")!.image(color: NCBrandColor.shared.icon, size: 50),
  183. action: { menuAction in
  184. NCNetworkingNotificationCenter.shared.downloadOpen(metadata: metadata, selector: NCBrandGlobal.shared.selectorOpenIn)
  185. }
  186. )
  187. )
  188. }
  189. //
  190. // DOWNLOAD IMAGE MAX RESOLUTION
  191. //
  192. if metadata.session == "" {
  193. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage && !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) && metadata.session == "" {
  194. actions.append(
  195. NCMenuAction(
  196. title: NSLocalizedString("_download_image_max_", comment: ""),
  197. icon: UIImage(named: "downloadImageFullRes")!.image(color: NCBrandColor.shared.icon, size: 50),
  198. action: { menuAction in
  199. NCNetworking.shared.download(metadata: metadata, selector: "") { (_) in }
  200. }
  201. )
  202. )
  203. }
  204. }
  205. //
  206. // SAVE IMAGE / VIDEO
  207. //
  208. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo {
  209. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  210. var icon = UIImage(named: "saveSelectedFiles")!.image(color: NCBrandColor.shared.icon, size: 50)
  211. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  212. if metadataMOV != nil {
  213. title = NSLocalizedString("_livephoto_save_", comment: "")
  214. icon = UIImage(named: "livePhoto")!.image(color: NCBrandColor.shared.icon, size: 50)
  215. }
  216. actions.append(
  217. NCMenuAction(
  218. title: title,
  219. icon: icon,
  220. action: { menuAction in
  221. if metadataMOV != nil {
  222. NCCollectionCommon.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  223. } else {
  224. NCOperationQueue.shared.download(metadata: metadata, selector: NCBrandGlobal.shared.selectorSaveAlbum)
  225. }
  226. }
  227. )
  228. )
  229. }
  230. //
  231. // PDF
  232. //
  233. if (metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileDocument && metadata.contentType == "application/pdf" ) {
  234. actions.append(
  235. NCMenuAction(
  236. title: NSLocalizedString("_search_", comment: ""),
  237. icon: UIImage(named: "search")!.image(color: NCBrandColor.shared.icon, size: 50),
  238. action: { menuAction in
  239. NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterMenuSearchTextPDF)
  240. }
  241. )
  242. )
  243. }
  244. //
  245. // COPY
  246. //
  247. actions.append(
  248. NCMenuAction(
  249. title: NSLocalizedString("_copy_file_", comment: ""),
  250. icon: UIImage(named: "copy")!.image(color: NCBrandColor.shared.icon, size: 50),
  251. action: { menuAction in
  252. self.appDelegate.pasteboardOcIds = [metadata.ocId];
  253. NCCollectionCommon.shared.copyPasteboard()
  254. }
  255. )
  256. )
  257. //
  258. // DELETE
  259. //
  260. if !webView {
  261. actions.append(
  262. NCMenuAction(
  263. title: titleDelete,
  264. icon: UIImage(named: "trash")!.image(color: NCBrandColor.shared.icon, size: 50),
  265. action: { menuAction in
  266. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  267. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  268. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, onlyLocal: false) { (errorCode, errorDescription) in
  269. if errorCode != 0 {
  270. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  271. }
  272. }
  273. })
  274. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  275. viewController.present(alertController, animated: true, completion:nil)
  276. }
  277. )
  278. )
  279. }
  280. return actions
  281. }
  282. }