CCMain+Menu.swift 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  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. if errorCode != 0 {
  124. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  125. }
  126. }
  127. }
  128. )
  129. )
  130. if (!isFolderEncrypted) {
  131. actions.append(
  132. NCMenuAction(
  133. title: NSLocalizedString("_details_", comment: ""),
  134. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  135. action: { menuAction in
  136. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  137. }
  138. )
  139. )
  140. }
  141. if (!metadata.e2eEncrypted) {
  142. actions.append(
  143. NCMenuAction(
  144. title: NSLocalizedString("_rename_", comment: ""),
  145. icon: CCGraphics.changeThemingColorImage(UIImage(named: "rename"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  146. action: { menuAction in
  147. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  148. alertController.addTextField { (textField) in
  149. textField.text = metadata.fileNameView
  150. textField.delegate = self as? UITextFieldDelegate
  151. textField.addTarget(self, action: #selector(self.minCharTextFieldDidChange(_:)
  152. ), for: UIControl.Event.editingChanged)
  153. }
  154. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  155. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  156. let fileNameNew = alertController.textFields![0].text
  157. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew!, urlBase: appDelegate.urlBase, viewController: self) { (errorCode, errorDescription) in
  158. if errorCode != 0 {
  159. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
  160. }
  161. }
  162. })
  163. okAction.isEnabled = false
  164. alertController.addAction(cancelAction)
  165. alertController.addAction(okAction)
  166. self.present(alertController, animated: true, completion: nil)
  167. }
  168. )
  169. )
  170. }
  171. if (!isFolderEncrypted) {
  172. actions.append(
  173. NCMenuAction(
  174. title: NSLocalizedString("_move_or_copy_", comment: ""),
  175. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  176. action: { menuAction in
  177. self.moveOpenWindow([indexPath])
  178. }
  179. )
  180. )
  181. }
  182. if (!isFolderEncrypted) {
  183. actions.append(
  184. NCMenuAction(
  185. title: isOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  186. icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  187. action: { menuAction in
  188. let serverUrl = CCUtility.stringAppendServerUrl(metadata.serverUrl, addFileName: metadata.fileName)!
  189. NCManageDatabase.sharedInstance.setDirectory(serverUrl: serverUrl, offline: !isOffline, account: appDelegate.account)
  190. if (!isOffline) {
  191. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: selectorDownloadAllFile)
  192. }
  193. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  194. }
  195. )
  196. )
  197. }
  198. if (!metadata.e2eEncrypted && CCUtility.isEnd(toEndEnabled: appDelegate.account)) {
  199. actions.append(
  200. NCMenuAction(
  201. title: NSLocalizedString("_e2e_set_folder_encrypted_", comment: ""),
  202. icon: CCGraphics.changeThemingColorImage(UIImage(named: "lock"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  203. action: { menuAction in
  204. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: false) { (account, errorCode, errorDescription) in
  205. if errorCode == 0 {
  206. let serverUrl = self.serverUrl + "/" + metadata.fileName
  207. NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  208. NCManageDatabase.sharedInstance.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: true, richWorkspace: nil, account: metadata.account)
  209. NCManageDatabase.sharedInstance.setMetadataEncrypted(ocId: metadata.ocId, encrypted: true)
  210. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  211. } else {
  212. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_mark_folder_", comment: ""), description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: .error, errorCode: errorCode)
  213. }
  214. }
  215. }
  216. )
  217. )
  218. }
  219. if (metadata.e2eEncrypted && !metadataFolder.e2eEncrypted && CCUtility.isEnd(toEndEnabled: appDelegate.account)) {
  220. actions.append(
  221. NCMenuAction(
  222. title: NSLocalizedString("_e2e_remove_folder_encrypted_", comment: ""),
  223. icon: CCGraphics.changeThemingColorImage(UIImage(named: "lock"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  224. action: { menuAction in
  225. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: true) { (account, errorCode, errorDescription) in
  226. if errorCode == 0 {
  227. let serverUrl = self.serverUrl + "/" + metadata.fileName
  228. NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, "\(self.serverUrl ?? "")/\(metadata.fileName)"))
  229. NCManageDatabase.sharedInstance.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: false, richWorkspace: nil, account: metadata.account)
  230. NCManageDatabase.sharedInstance.setMetadataEncrypted(ocId: metadata.ocId, encrypted: false)
  231. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  232. } else {
  233. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_delete_mark_folder_", comment: ""), description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: .error, errorCode: errorCode)
  234. }
  235. }
  236. }
  237. )
  238. )
  239. }
  240. } else {
  241. var iconHeader: UIImage!
  242. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  243. iconHeader = icon
  244. } else {
  245. iconHeader = UIImage(named: metadata.iconName)
  246. }
  247. let isEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  248. actions.append(
  249. NCMenuAction(
  250. title: metadata.fileNameView,
  251. icon: iconHeader,
  252. action: nil
  253. )
  254. )
  255. actions.append(
  256. NCMenuAction(
  257. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  258. icon: CCGraphics.changeThemingColorImage(UIImage(named: "favorite"), width: 50, height: 50, color: NCBrandColor.sharedInstance.yellowFavorite),
  259. action: { menuAction in
  260. NCNetworking.shared.favoriteMetadata(metadata, urlBase: appDelegate.urlBase) { (errorCode, errorDescription) in }
  261. }
  262. )
  263. )
  264. if (!isEncrypted) {
  265. actions.append(
  266. NCMenuAction(
  267. title: NSLocalizedString("_details_", comment: ""),
  268. icon: CCGraphics.changeThemingColorImage(UIImage(named: "details"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  269. action: { menuAction in
  270. NCMainCommon.shared.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  271. }
  272. )
  273. )
  274. }
  275. if(!NCBrandOptions.sharedInstance.disable_openin_file) {
  276. actions.append(
  277. NCMenuAction(title: NSLocalizedString("_open_in_", comment: ""),
  278. icon: CCGraphics.changeThemingColorImage(UIImage(named: "openFile"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  279. action: { menuAction in
  280. self.tableView.setEditing(false, animated: true)
  281. NCMainCommon.shared.downloadOpen(metadata: metadata, selector: selectorOpenIn)
  282. }
  283. )
  284. )
  285. }
  286. actions.append(
  287. NCMenuAction(
  288. title: NSLocalizedString("_rename_", comment: ""),
  289. icon: CCGraphics.changeThemingColorImage(UIImage(named: "rename"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  290. action: { menuAction in
  291. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  292. alertController.addTextField { (textField) in
  293. textField.text = metadata.fileNameView
  294. textField.delegate = self as? UITextFieldDelegate
  295. textField.addTarget(self, action: #selector(self.minCharTextFieldDidChange(_:)
  296. ), for: UIControl.Event.editingChanged)
  297. }
  298. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  299. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  300. let fileNameNew = alertController.textFields![0].text
  301. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew!, urlBase: appDelegate.urlBase, viewController: self) { (errorCode, errorDescription) in }
  302. })
  303. okAction.isEnabled = false
  304. alertController.addAction(cancelAction)
  305. alertController.addAction(okAction)
  306. self.present(alertController, animated: true, completion: nil)
  307. }
  308. )
  309. )
  310. if (!isEncrypted) {
  311. actions.append(
  312. NCMenuAction(
  313. title: NSLocalizedString("_move_or_copy_", comment: ""),
  314. icon: CCGraphics.changeThemingColorImage(UIImage(named: "move"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  315. action: { menuAction in
  316. self.moveOpenWindow([indexPath])
  317. }
  318. )
  319. )
  320. }
  321. if (!isEncrypted) {
  322. let localFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  323. var title: String!
  324. if (localFile == nil || localFile!.offline == false) {
  325. title = NSLocalizedString("_set_available_offline_", comment: "")
  326. } else {
  327. title = NSLocalizedString("_remove_available_offline_", comment: "")
  328. }
  329. actions.append(
  330. NCMenuAction(
  331. title: title,
  332. icon: CCGraphics.changeThemingColorImage(UIImage(named: "offline"), width: 50, height: 50, color: NCBrandColor.sharedInstance.icon),
  333. action: { menuAction in
  334. if (localFile == nil || !CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView)) {
  335. NCNetworking.shared.download(metadata: metadata, selector: selectorLoadOffline) { (_) in }
  336. if let metadataLivePhoto = NCManageDatabase.sharedInstance.isLivePhoto(metadata: metadata) {
  337. NCNetworking.shared.download(metadata: metadataLivePhoto, selector: selectorLoadOffline) { (_) in }
  338. }
  339. } else {
  340. NCManageDatabase.sharedInstance.setLocalFile(ocId: metadata.ocId, offline: !localFile!.offline)
  341. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataSource, userInfo: ["ocId":metadata.ocId, "serverUrl":metadata.serverUrl])
  342. }
  343. }
  344. )
  345. )
  346. }
  347. }
  348. actions.append(
  349. NCMenuAction(
  350. title: NSLocalizedString("_delete_", comment: ""),
  351. icon: CCGraphics.changeThemingColorImage(UIImage(named: "trash"), width: 50, height: 50, color: .red),
  352. action: { menuAction in
  353. self.actionDelete(indexPath)
  354. }
  355. )
  356. )
  357. return actions
  358. }
  359. }