NCCollectionViewCommon+Menu.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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. import NCCommunication
  27. extension NCCollectionViewCommon {
  28. func toggleMoreMenu(viewController: UIViewController, metadata: tableMetadata) {
  29. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(metadata.ocId) {
  30. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  31. mainMenuViewController.actions = self.initMenuMore(viewController: viewController, metadata: metadata)
  32. let menuPanelController = NCMenuPanelController()
  33. menuPanelController.parentPresenter = viewController
  34. menuPanelController.delegate = mainMenuViewController
  35. menuPanelController.set(contentViewController: mainMenuViewController)
  36. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  37. viewController.present(menuPanelController, animated: true, completion: nil)
  38. }
  39. }
  40. private func initMenuMore(viewController: UIViewController, metadata: tableMetadata) -> [NCMenuAction] {
  41. var actions = [NCMenuAction]()
  42. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  43. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl+"/"+metadata.fileName, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  44. let serverUrlHome = NCUtility.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  45. var isOffline = false
  46. if metadata.directory {
  47. if let directory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!)) {
  48. isOffline = directory.offline
  49. }
  50. } else {
  51. if let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  52. isOffline = localFile.offline
  53. }
  54. }
  55. var iconHeader: UIImage!
  56. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  57. iconHeader = icon
  58. } else {
  59. if metadata.directory {
  60. iconHeader = CCGraphics.changeThemingColorImage(UIImage(named: "folder"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon)
  61. } else {
  62. iconHeader = UIImage(named: metadata.iconName)
  63. }
  64. }
  65. actions.append(
  66. NCMenuAction(
  67. title: metadata.fileNameView,
  68. icon: iconHeader,
  69. action: nil
  70. )
  71. )
  72. // Favorite
  73. if (layoutKey == k_layout_view_favorite && serverUrl == "") || (layoutKey != k_layout_view_favorite) {
  74. actions.append(
  75. NCMenuAction(
  76. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  77. icon: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.yellowFavorite),
  78. action: { menuAction in
  79. NCNetworking.shared.favoriteMetadata(metadata, urlBase: appDelegate.urlBase) { (errorCode, errorDescription) in
  80. if errorCode != 0 {
  81. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  82. }
  83. }
  84. }
  85. )
  86. )
  87. }
  88. // Offline
  89. if !isFolderEncrypted && (layoutKey == k_layout_view_offline && serverUrl == "" || (layoutKey != k_layout_view_offline)) {
  90. actions.append(
  91. NCMenuAction(
  92. title: isOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  93. icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  94. action: { menuAction in
  95. if isOffline {
  96. if metadata.directory {
  97. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!, offline: false, account: self.appDelegate.account)
  98. } else {
  99. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: false)
  100. }
  101. } else {
  102. if metadata.directory {
  103. NCManageDatabase.sharedInstance.setDirectory(serverUrl: CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!, offline: true, account: self.appDelegate.account)
  104. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: selectorDownloadAllFile)
  105. } else {
  106. NCNetworking.shared.download(metadata: metadata, selector: selectorLoadOffline) { (_) in }
  107. if let metadataLivePhoto = NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) {
  108. NCNetworking.shared.download(metadata: metadataLivePhoto, selector: selectorLoadOffline) { (_) in }
  109. }
  110. }
  111. }
  112. self.reloadDataSource()
  113. }
  114. )
  115. )
  116. }
  117. // All
  118. if !metadata.e2eEncrypted {
  119. actions.append(
  120. NCMenuAction(
  121. title: NSLocalizedString("_details_", comment: ""),
  122. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  123. action: { menuAction in
  124. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  125. }
  126. )
  127. )
  128. }
  129. if !metadata.directory && !NCBrandOptions.sharedInstance.disable_openin_file {
  130. actions.append(
  131. NCMenuAction(
  132. title: NSLocalizedString("_open_in_", comment: ""),
  133. icon: CCGraphics.changeThemingColorImage(UIImage(named: "openFile"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  134. action: { menuAction in
  135. NCMainCommon.shared.downloadOpen(metadata: metadata, selector: selectorOpenIn)
  136. }
  137. )
  138. )
  139. }
  140. if !isFolderEncrypted {
  141. actions.append(
  142. NCMenuAction(
  143. title: NSLocalizedString("_rename_", comment: ""),
  144. icon: CCGraphics.changeThemingColorImage(UIImage(named: "rename"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  145. action: { menuAction in
  146. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  147. alertController.addTextField { (textField) in
  148. textField.text = metadata.fileNameView
  149. }
  150. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  151. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  152. if let fileNameNew = alertController.textFields?.first?.text {
  153. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: appDelegate.urlBase, viewController: self) { (errorCode, errorDescription) in
  154. if errorCode != 0 {
  155. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  156. }
  157. }
  158. }
  159. })
  160. alertController.addAction(cancelAction)
  161. alertController.addAction(okAction)
  162. self.present(alertController, animated: true, completion: nil)
  163. }
  164. )
  165. )
  166. }
  167. if !isFolderEncrypted && serverUrl != "" {
  168. actions.append(
  169. NCMenuAction(
  170. title: NSLocalizedString("_move_or_copy_", comment: ""),
  171. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  172. action: { menuAction in
  173. NCCollectionCommon.shared.openSelectView(viewController: viewController, array: [metadata])
  174. }
  175. )
  176. )
  177. }
  178. actions.append(
  179. NCMenuAction(
  180. title: NSLocalizedString("_delete_", comment: ""),
  181. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  182. action: { menuAction in
  183. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  184. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  185. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: false)
  186. })
  187. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (action:UIAlertAction) in
  188. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: true)
  189. })
  190. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  191. self.present(alertController, animated: true, completion:nil)
  192. }
  193. )
  194. )
  195. if !metadata.e2eEncrypted && metadata.directory && CCUtility.isEnd(toEndEnabled: appDelegate.account) && metadata.serverUrl == serverUrlHome {
  196. actions.append(
  197. NCMenuAction(
  198. title: NSLocalizedString("_e2e_set_folder_encrypted_", comment: ""),
  199. icon: CCGraphics.changeThemingColorImage(UIImage(named: "lock"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  200. action: { menuAction in
  201. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: false) { (account, errorCode, errorDescription) in
  202. if errorCode == 0 {
  203. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  204. NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  205. NCManageDatabase.sharedInstance.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: true, richWorkspace: nil, account: metadata.account)
  206. NCManageDatabase.sharedInstance.setMetadataEncrypted(ocId: metadata.ocId, encrypted: true)
  207. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_changeStatusFolderE2EE, userInfo: ["serverUrl":metadata.serverUrl])
  208. } else {
  209. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_mark_folder_", comment: ""), description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: .error, errorCode: errorCode)
  210. }
  211. }
  212. }
  213. )
  214. )
  215. }
  216. if metadata.e2eEncrypted && metadata.directory && CCUtility.isEnd(toEndEnabled: appDelegate.account) && metadata.serverUrl == serverUrlHome {
  217. actions.append(
  218. NCMenuAction(
  219. title: NSLocalizedString("_e2e_remove_folder_encrypted_", comment: ""),
  220. icon: CCGraphics.changeThemingColorImage(UIImage(named: "lock"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  221. action: { menuAction in
  222. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: true) { (account, errorCode, errorDescription) in
  223. if errorCode == 0 {
  224. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  225. NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  226. NCManageDatabase.sharedInstance.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: false, richWorkspace: nil, account: metadata.account)
  227. NCManageDatabase.sharedInstance.setMetadataEncrypted(ocId: metadata.ocId, encrypted: false)
  228. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_changeStatusFolderE2EE, userInfo: ["serverUrl":metadata.serverUrl])
  229. } else {
  230. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_delete_mark_folder_", comment: ""), description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: .error, errorCode: errorCode)
  231. }
  232. }
  233. }
  234. )
  235. )
  236. }
  237. return actions
  238. }
  239. func toggleMoreSelect(viewController: UIViewController, selectOcId: [String]) {
  240. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  241. mainMenuViewController.actions = self.initMenuSelect(viewController: viewController, selectOcId: selectOcId)
  242. let menuPanelController = NCMenuPanelController()
  243. menuPanelController.parentPresenter = viewController
  244. menuPanelController.delegate = mainMenuViewController
  245. menuPanelController.set(contentViewController: mainMenuViewController)
  246. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  247. viewController.present(menuPanelController, animated: true, completion: nil)
  248. }
  249. private func initMenuSelect(viewController: UIViewController, selectOcId: [String]) -> [NCMenuAction] {
  250. var actions = [NCMenuAction]()
  251. actions.append(
  252. NCMenuAction(
  253. title: NSLocalizedString("_select_all_", comment: ""),
  254. icon: CCGraphics.changeThemingColorImage(UIImage(named: "selectFull"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  255. action: { menuAction in
  256. self.collectionViewSelectAll()
  257. }
  258. )
  259. )
  260. actions.append(
  261. NCMenuAction(
  262. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  263. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  264. action: { menuAction in
  265. var meradatasSelect = [tableMetadata]()
  266. for ocId in selectOcId {
  267. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  268. meradatasSelect.append(metadata)
  269. }
  270. }
  271. if meradatasSelect.count > 0 {
  272. NCCollectionCommon.shared.openSelectView(viewController: viewController, array: meradatasSelect)
  273. }
  274. self.tapSelect(sender: self)
  275. }
  276. )
  277. )
  278. actions.append(
  279. NCMenuAction(
  280. title: NSLocalizedString("_delete_selected_files_", comment: ""),
  281. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  282. action: { menuAction in
  283. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  284. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  285. for ocId in selectOcId {
  286. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  287. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: false)
  288. }
  289. }
  290. self.tapSelect(sender: self)
  291. })
  292. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (action:UIAlertAction) in
  293. for ocId in selectOcId {
  294. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  295. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: true)
  296. }
  297. }
  298. self.tapSelect(sender: self)
  299. })
  300. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  301. self.present(alertController, animated: true, completion:nil)
  302. }
  303. )
  304. )
  305. actions.append(
  306. NCMenuAction(
  307. title: NSLocalizedString("_save_selected_files_", comment: ""),
  308. icon: CCGraphics.changeThemingColorImage(UIImage(named: "saveSelectedFiles"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  309. action: { menuAction in
  310. for ocId in selectOcId {
  311. if let metadata = NCManageDatabase.sharedInstance.getMetadataFromOcId(ocId) {
  312. if metadata.typeFile == k_metadataTypeFile_image || metadata.typeFile == k_metadataTypeFile_video {
  313. NCOperationQueue.shared.download(metadata: metadata, selector: selectorSaveAlbum, setFavorite: false)
  314. }
  315. }
  316. }
  317. self.tapSelect(sender: self)
  318. }
  319. )
  320. )
  321. return actions
  322. }
  323. }