NCViewer+Menu.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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) {
  27. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  28. mainMenuViewController.actions = self.initMoreMenu(viewController: viewController, metadata: metadata)
  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) -> [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.sharedInstance.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.sharedInstance.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: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.yellowFavorite),
  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: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  66. }
  67. }
  68. }
  69. )
  70. )
  71. //
  72. // DETAIL
  73. //
  74. actions.append(
  75. NCMenuAction(
  76. title: NSLocalizedString("_details_", comment: ""),
  77. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  78. action: { menuAction in
  79. NCNetworkingNotificationCenter.shared.openShare(ViewController: viewController, metadata: metadata, indexPage: 0)
  80. }
  81. )
  82. )
  83. //
  84. // OPEN IN
  85. //
  86. if metadata.session == "" {
  87. actions.append(
  88. NCMenuAction(title: NSLocalizedString("_open_in_", comment: ""),
  89. icon: CCGraphics.changeThemingColorImage(UIImage(named: "openFile"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  90. action: { menuAction in
  91. NCNetworkingNotificationCenter.shared.downloadOpen(metadata: metadata, selector: selectorOpenIn)
  92. }
  93. )
  94. )
  95. }
  96. //
  97. // RENAME
  98. //
  99. actions.append(
  100. NCMenuAction(
  101. title: NSLocalizedString("_rename_", comment: ""),
  102. icon: CCGraphics.changeThemingColorImage(UIImage(named: "rename"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  103. action: { menuAction in
  104. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  105. alertController.addTextField { (textField) in textField.text = metadata.fileNameView }
  106. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  107. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  108. let fileNameNew = alertController.textFields![0].text
  109. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew!, urlBase: self.appDelegate.urlBase, viewController: viewController) { (errorCode, errorDescription) in
  110. if errorCode != 0 {
  111. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  112. }
  113. }
  114. })
  115. alertController.addAction(cancelAction)
  116. alertController.addAction(okAction)
  117. viewController.present(alertController, animated: true, completion: nil)
  118. }
  119. )
  120. )
  121. //
  122. // COPY - MOVE
  123. //
  124. actions.append(
  125. NCMenuAction(
  126. title: NSLocalizedString("_move_or_copy_", comment: ""),
  127. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  128. action: { menuAction in
  129. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  130. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  131. let viewController = navigationController.topViewController as! NCSelect
  132. viewController.delegate = NCViewer.shared
  133. viewController.hideButtonCreateFolder = false
  134. viewController.selectFile = false
  135. viewController.includeDirectoryE2EEncryption = false
  136. viewController.includeImages = false
  137. viewController.type = ""
  138. viewController.titleButtonDone = NSLocalizedString("_move_", comment: "")
  139. viewController.titleButtonDone1 = NSLocalizedString("_copy_", comment: "")
  140. viewController.isButtonDone1Hide = false
  141. viewController.isOverwriteHide = false
  142. navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  143. self.appDelegate.window?.rootViewController?.present(navigationController, animated: true, completion: nil)
  144. }
  145. )
  146. )
  147. //
  148. // OFFLINE
  149. //
  150. if metadata.session == "" {
  151. actions.append(
  152. NCMenuAction(
  153. title: titleOffline,
  154. icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  155. action: { menuAction in
  156. if ((localFile == nil || !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView)) && metadata.session == "") {
  157. NCNetworking.shared.download(metadata: metadata, selector: selectorLoadOffline) { (_) in }
  158. } else {
  159. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: !localFile!.offline)
  160. }
  161. }
  162. )
  163. )
  164. }
  165. //
  166. // VIEW IN FOLDER
  167. //
  168. if appDelegate.activeFileViewInFolder == nil {
  169. actions.append(
  170. NCMenuAction(
  171. title: NSLocalizedString("_view_in_folder_", comment: ""),
  172. icon: CCGraphics.changeThemingColorImage(UIImage(named: "viewInFolder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  173. action: { menuAction in
  174. NCCollectionCommon.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  175. }
  176. )
  177. )
  178. }
  179. //
  180. // DELETE
  181. //
  182. actions.append(
  183. NCMenuAction(title: titleDelete,
  184. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  185. action: { menuAction in
  186. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  187. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  188. NCNetworking.shared.deleteMetadata(metadata, account: self.appDelegate.account, urlBase: self.appDelegate.urlBase, onlyLocal: false) { (errorCode, errorDescription) in
  189. if errorCode != 0 {
  190. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  191. }
  192. }
  193. })
  194. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  195. viewController.present(alertController, animated: true, completion:nil)
  196. }
  197. )
  198. )
  199. //
  200. // PDF
  201. //
  202. if (metadata.typeFile == k_metadataTypeFile_document && metadata.contentType == "application/pdf" ) {
  203. actions.append(
  204. NCMenuAction(title: NSLocalizedString("_search_", comment: ""),
  205. icon: CCGraphics.changeThemingColorImage(UIImage(named: "search"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  206. action: { menuAction in
  207. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_menuSearchTextPDF)
  208. }
  209. )
  210. )
  211. }
  212. //
  213. // IMAGE - VIDEO - AUDIO
  214. //
  215. /*
  216. if metadata.session == "" {
  217. if (metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio) && !CCUtility.fileProviderStorageExists(appDelegate.activeDetail.metadata?.ocId, fileNameView: appDelegate.activeDetail.metadata?.fileNameView) && metadata.session == "" && metadata.typeFile == k_metadataTypeFile_image {
  218. actions.append(
  219. NCMenuAction(title: NSLocalizedString("_download_image_max_", comment: ""),
  220. icon: CCGraphics.changeThemingColorImage(UIImage(named: "downloadImageFullRes"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  221. action: { menuAction in
  222. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_menuDownloadImage, userInfo: ["metadata": metadata])
  223. }
  224. )
  225. )
  226. }
  227. }
  228. */
  229. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video || metadata.typeFile == k_metadataTypeFile_audio {
  230. if let metadataLive = NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) {
  231. if CCUtility.fileProviderStorageSize(metadata.ocId, fileNameView: metadata.fileNameView) > 0 && CCUtility.fileProviderStorageSize(metadataLive.ocId, fileNameView: metadataLive.fileNameView) > 0 {
  232. actions.append(
  233. NCMenuAction(title: NSLocalizedString("_livephoto_save_", comment: ""),
  234. icon: CCGraphics.changeThemingColorImage(UIImage(named: "livePhoto"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  235. action: { menuAction in
  236. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_menuSaveLivePhoto, userInfo: ["metadata": metadata, "metadataMov": metadataLive])
  237. }
  238. )
  239. )
  240. }
  241. }
  242. }
  243. //
  244. // CLOSE
  245. //
  246. /*
  247. actions.append(
  248. NCMenuAction(title: NSLocalizedString("_close_", comment: ""),
  249. icon: CCGraphics.changeThemingColorImage(UIImage(named: "exit"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  250. action: { menuAction in
  251. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_menuDetailClose)
  252. }
  253. )
  254. )
  255. */
  256. return actions
  257. }
  258. }