CCMain+Menu.swift 22 KB

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