NCCollectionViewCommon+Menu.swift 21 KB

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