NCCollectionViewCommon+Menu.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. //
  2. // NCCollectionViewCommon+Menu.swift
  3. // Nextcloud
  4. //
  5. // Created by Philippe Weidmann on 24.01.20.
  6. // Copyright © 2020 Philippe Weidmann. All rights reserved.
  7. // Copyright © 2020 Marino Faggiana All rights reserved.
  8. //
  9. // Author Philippe Weidmann
  10. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import FloatingPanel
  26. extension NCCollectionViewCommon {
  27. func toggleMoreMenu(viewController: UIViewController, metadata: tableMetadata, selectOcId: [String]) {
  28. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(metadata.ocId) {
  29. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  30. mainMenuViewController.actions = self.initMenu(viewController: viewController, metadata: metadata, selectOcId: selectOcId)
  31. let menuPanelController = NCMenuPanelController()
  32. menuPanelController.parentPresenter = viewController
  33. menuPanelController.delegate = mainMenuViewController
  34. menuPanelController.set(contentViewController: mainMenuViewController)
  35. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  36. viewController.present(menuPanelController, animated: true, completion: nil)
  37. }
  38. }
  39. private func initMenu(viewController: UIViewController, metadata: tableMetadata, selectOcId: [String]) -> [NCMenuAction] {
  40. var actions = [NCMenuAction]()
  41. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  42. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl+"/"+metadata.fileName, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  43. var isOffline = false
  44. if metadata.directory {
  45. if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!)) {
  46. isOffline = directory.offline
  47. }
  48. } else {
  49. if let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  50. isOffline = localFile.offline
  51. }
  52. }
  53. var iconHeader: UIImage!
  54. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  55. iconHeader = icon
  56. } else {
  57. if metadata.directory {
  58. iconHeader = CCGraphics.changeThemingColorImage(UIImage(named: "folder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  59. } else {
  60. iconHeader = UIImage(named: metadata.iconName)
  61. }
  62. }
  63. actions.append(
  64. NCMenuAction(
  65. title: metadata.fileNameView,
  66. icon: iconHeader,
  67. action: nil
  68. )
  69. )
  70. // Favorite
  71. if (layoutKey == k_layout_view_favorite && serverUrl == "") || (layoutKey != k_layout_view_favorite) {
  72. actions.append(
  73. NCMenuAction(
  74. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  75. icon: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.yellowFavorite),
  76. action: { menuAction in
  77. NCNetworking.shared.favoriteMetadata(metadata, urlBase: appDelegate.urlBase) { (errorCode, errorDescription) in }
  78. }
  79. )
  80. )
  81. }
  82. // Offline
  83. if !isFolderEncrypted && (layoutKey == k_layout_view_offline && self.serverUrl == "" || (layoutKey != k_layout_view_offline)) {
  84. actions.append(
  85. NCMenuAction(
  86. title: isOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  87. icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  88. action: { menuAction in
  89. if isOffline {
  90. if metadata.directory {
  91. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!, offline: false, account: self.appDelegate.account)
  92. } else {
  93. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: false)
  94. }
  95. }
  96. self.reloadDataSource()
  97. }
  98. )
  99. )
  100. }
  101. // All
  102. actions.append(
  103. NCMenuAction(
  104. title: NSLocalizedString("_details_", comment: ""),
  105. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  106. action: { menuAction in
  107. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  108. }
  109. )
  110. )
  111. if !metadata.directory && !NCBrandOptions.sharedInstance.disable_openin_file {
  112. actions.append(
  113. NCMenuAction(
  114. title: NSLocalizedString("_open_in_", comment: ""),
  115. icon: CCGraphics.changeThemingColorImage(UIImage(named: "openFile"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  116. action: { menuAction in
  117. NCMainCommon.shared.downloadOpen(metadata: metadata, selector: selectorOpenIn)
  118. }
  119. )
  120. )
  121. }
  122. actions.append(
  123. NCMenuAction(
  124. title: NSLocalizedString("_rename_", comment: ""),
  125. icon: CCGraphics.changeThemingColorImage(UIImage(named: "rename"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  126. action: { menuAction in
  127. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  128. alertController.addTextField { (textField) in
  129. textField.text = metadata.fileNameView
  130. textField.delegate = self as? UITextFieldDelegate
  131. textField.addTarget(self, action: #selector(self.minCharTextFieldDidChange(sender:)), for: UIControl.Event.editingChanged)
  132. }
  133. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  134. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  135. let fileNameNew = alertController.textFields![0].text
  136. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew!, urlBase: appDelegate.urlBase, viewController: self) { (errorCode, errorDescription) in }
  137. })
  138. okAction.isEnabled = false
  139. alertController.addAction(cancelAction)
  140. alertController.addAction(okAction)
  141. self.present(alertController, animated: true, completion: nil)
  142. }
  143. )
  144. )
  145. if !isFolderEncrypted && serverUrl != "" {
  146. actions.append(
  147. NCMenuAction(
  148. title: NSLocalizedString("_move_or_copy_", comment: ""),
  149. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  150. action: { menuAction in
  151. NCCollectionCommon.shared.openSelectView(viewController: viewController, array: [metadata])
  152. }
  153. )
  154. )
  155. }
  156. actions.append(
  157. NCMenuAction(
  158. title: NSLocalizedString("_delete_", comment: ""),
  159. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  160. action: { menuAction in
  161. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  162. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  163. if selectOcId.count > 0 {
  164. for ocId in selectOcId {
  165. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  166. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: false)
  167. }
  168. }
  169. } else {
  170. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: false)
  171. }
  172. })
  173. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (action:UIAlertAction) in
  174. if selectOcId.count > 0 {
  175. for ocId in selectOcId {
  176. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  177. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: true)
  178. }
  179. }
  180. } else {
  181. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: true)
  182. }
  183. })
  184. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  185. self.present(alertController, animated: true, completion:nil)
  186. }
  187. )
  188. )
  189. return actions
  190. }
  191. }