NCCollectionViewCommon+Menu.swift 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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.shared.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 = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, account: appDelegate.account)
  46. var isOffline = false
  47. var titleDelete = NSLocalizedString("_delete_", comment: "")
  48. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  49. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  50. } else if metadata.directory {
  51. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  52. } else {
  53. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  54. }
  55. if let metadataFolder = metadataFolder {
  56. let isShare = metadata.permissions.contains(NCBrandGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCBrandGlobal.shared.permissionShared)
  57. let isMounted = metadata.permissions.contains(NCBrandGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCBrandGlobal.shared.permissionMounted)
  58. if isShare || isMounted {
  59. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  60. }
  61. }
  62. if metadata.directory {
  63. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl)) {
  64. isOffline = directory.offline
  65. }
  66. } else {
  67. if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  68. isOffline = localFile.offline
  69. }
  70. }
  71. var iconHeader: UIImage!
  72. if let icon = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  73. iconHeader = icon
  74. } else {
  75. if metadata.directory {
  76. if metadata.e2eEncrypted {
  77. iconHeader = NCCollectionCommon.images.cellFolderEncryptedImage
  78. } else {
  79. iconHeader = NCCollectionCommon.images.cellFolderImage
  80. }
  81. } else {
  82. iconHeader = UIImage(named: metadata.iconName)
  83. }
  84. }
  85. actions.append(
  86. NCMenuAction(
  87. title: metadata.fileNameView,
  88. icon: iconHeader,
  89. action: nil
  90. )
  91. )
  92. //
  93. // FAVORITE
  94. //
  95. actions.append(
  96. NCMenuAction(
  97. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  98. icon: UIImage(named: "favorite")!.image(color: NCBrandColor.shared.yellowFavorite, size: 50),
  99. action: { menuAction in
  100. NCNetworking.shared.favoriteMetadata(metadata, urlBase: appDelegate.urlBase) { (errorCode, errorDescription) in
  101. if errorCode != 0 {
  102. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  103. }
  104. }
  105. }
  106. )
  107. )
  108. //
  109. // DETAIL
  110. //
  111. if !isFolderEncrypted && !appDelegate.disableSharesView {
  112. actions.append(
  113. NCMenuAction(
  114. title: NSLocalizedString("_details_", comment: ""),
  115. icon: UIImage(named: "details")!.image(color: NCBrandColor.shared.icon, size: 50),
  116. action: { menuAction in
  117. NCNetworkingNotificationCenter.shared.openShare(ViewController: self, metadata: metadata, indexPage: 0)
  118. }
  119. )
  120. )
  121. }
  122. //
  123. // OPEN IN
  124. //
  125. if !metadata.directory && !NCBrandOptions.shared.disable_openin_file {
  126. actions.append(
  127. NCMenuAction(
  128. title: NSLocalizedString("_open_in_", comment: ""),
  129. icon: UIImage(named: "openFile")!.image(color: NCBrandColor.shared.icon, size: 50),
  130. action: { menuAction in
  131. NCNetworkingNotificationCenter.shared.downloadOpen(metadata: metadata, selector: NCBrandGlobal.shared.selectorOpenIn)
  132. }
  133. )
  134. )
  135. }
  136. //
  137. // SAVE
  138. //
  139. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo {
  140. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  141. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  142. if metadataMOV != nil {
  143. title = NSLocalizedString("_livephoto_save_", comment: "")
  144. }
  145. actions.append(
  146. NCMenuAction(
  147. title: title,
  148. icon: UIImage(named: "saveSelectedFiles")!.image(color: NCBrandColor.shared.icon, size: 50),
  149. action: { menuAction in
  150. if metadataMOV != nil {
  151. NCCollectionCommon.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  152. } else {
  153. NCOperationQueue.shared.download(metadata: metadata, selector: NCBrandGlobal.shared.selectorSaveAlbum, setFavorite: false)
  154. }
  155. }
  156. )
  157. )
  158. }
  159. //
  160. // RENAME
  161. //
  162. if !(isFolderEncrypted && metadata.serverUrl == serverUrlHome) {
  163. actions.append(
  164. NCMenuAction(
  165. title: NSLocalizedString("_rename_", comment: ""),
  166. icon: UIImage(named: "rename")!.image(color: NCBrandColor.shared.icon, size: 50),
  167. action: { menuAction in
  168. let alertController = UIAlertController(title: NSLocalizedString("_rename_", comment: ""), message: nil, preferredStyle: .alert)
  169. alertController.addTextField { (textField) in
  170. textField.text = metadata.fileNameView
  171. }
  172. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .cancel, handler: nil)
  173. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default, handler: { action in
  174. if let fileNameNew = alertController.textFields?.first?.text {
  175. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: appDelegate.urlBase, viewController: self) { (errorCode, errorDescription) in
  176. if errorCode != 0 {
  177. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  178. }
  179. }
  180. }
  181. })
  182. alertController.addAction(cancelAction)
  183. alertController.addAction(okAction)
  184. self.present(alertController, animated: true, completion: nil)
  185. }
  186. )
  187. )
  188. }
  189. //
  190. // COPY - MOVE
  191. //
  192. if !isFolderEncrypted && serverUrl != "" {
  193. actions.append(
  194. NCMenuAction(
  195. title: NSLocalizedString("_move_or_copy_", comment: ""),
  196. icon: UIImage(named: "move")!.image(color: NCBrandColor.shared.icon, size: 50),
  197. action: { menuAction in
  198. NCCollectionCommon.shared.openSelectView(items: [metadata], viewController: self)
  199. }
  200. )
  201. )
  202. }
  203. //
  204. // OFFLINE
  205. //
  206. if !isFolderEncrypted {
  207. actions.append(
  208. NCMenuAction(
  209. title: isOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  210. icon: UIImage(named: "offline")!.image(color: NCBrandColor.shared.icon, size: 50),
  211. action: { menuAction in
  212. if isOffline {
  213. if metadata.directory {
  214. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, offline: false, account: self.appDelegate.account)
  215. } else {
  216. NCManageDatabase.shared.setLocalFile(ocId: metadata.ocId, offline: false)
  217. }
  218. } else {
  219. if metadata.directory {
  220. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, offline: true, account: self.appDelegate.account)
  221. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: NCBrandGlobal.shared.selectorDownloadAllFile)
  222. } else {
  223. NCNetworking.shared.download(metadata: metadata, selector: NCBrandGlobal.shared.selectorLoadOffline) { (_) in }
  224. if let metadataLivePhoto = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  225. NCNetworking.shared.download(metadata: metadataLivePhoto, selector: NCBrandGlobal.shared.selectorLoadOffline) { (_) in }
  226. }
  227. }
  228. }
  229. self.reloadDataSource()
  230. }
  231. )
  232. )
  233. }
  234. //
  235. // VIEW IN FOLDER
  236. //
  237. if layoutKey == NCBrandGlobal.shared.layoutViewRecent && appDelegate.activeFileViewInFolder == nil {
  238. actions.append(
  239. NCMenuAction(
  240. title: NSLocalizedString("_view_in_folder_", comment: ""),
  241. icon: UIImage(named: "viewInFolder")!.image(color: NCBrandColor.shared.icon, size: 50),
  242. action: { menuAction in
  243. NCCollectionCommon.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileName: metadata.fileName)
  244. }
  245. )
  246. )
  247. }
  248. //
  249. // DELETE
  250. //
  251. actions.append(
  252. NCMenuAction(
  253. title: titleDelete,
  254. icon: UIImage(named: "trash")!.image(color: NCBrandColor.shared.icon, size: 50),
  255. action: { menuAction in
  256. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  257. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  258. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: false)
  259. })
  260. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (action:UIAlertAction) in
  261. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: true)
  262. })
  263. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  264. self.present(alertController, animated: true, completion:nil)
  265. }
  266. )
  267. )
  268. //
  269. // SET FOLDER E2EE
  270. //
  271. if !metadata.e2eEncrypted && metadata.directory && CCUtility.isEnd(toEndEnabled: appDelegate.account) && metadata.serverUrl == serverUrlHome {
  272. actions.append(
  273. NCMenuAction(
  274. title: NSLocalizedString("_e2e_set_folder_encrypted_", comment: ""),
  275. icon: UIImage(named: "lock")!.image(color: NCBrandColor.shared.icon, size: 50),
  276. action: { menuAction in
  277. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: false) { (account, errorCode, errorDescription) in
  278. if errorCode == 0 {
  279. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  280. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: true, richWorkspace: nil, account: metadata.account)
  281. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: true)
  282. NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl":metadata.serverUrl])
  283. } else {
  284. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_mark_folder_", comment: ""), description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: .error, errorCode: errorCode)
  285. }
  286. }
  287. }
  288. )
  289. )
  290. }
  291. //
  292. // UNSET FOLDER E2EE
  293. //
  294. if metadata.e2eEncrypted && metadata.directory && CCUtility.isEnd(toEndEnabled: appDelegate.account) && metadata.serverUrl == serverUrlHome {
  295. actions.append(
  296. NCMenuAction(
  297. title: NSLocalizedString("_e2e_remove_folder_encrypted_", comment: ""),
  298. icon: UIImage(named: "lock")!.image(color: NCBrandColor.shared.icon, size: 50),
  299. action: { menuAction in
  300. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: true) { (account, errorCode, errorDescription) in
  301. if errorCode == 0 {
  302. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl))
  303. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: false, richWorkspace: nil, account: metadata.account)
  304. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: false)
  305. NotificationCenter.default.postOnMainThread(name: NCBrandGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl":metadata.serverUrl])
  306. } else {
  307. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_delete_mark_folder_", comment: ""), description: errorDescription, delay: NCBrandGlobal.shared.dismissAfterSecond, type: .error, errorCode: errorCode)
  308. }
  309. }
  310. }
  311. )
  312. )
  313. }
  314. return actions
  315. }
  316. func toggleMoreSelect(viewController: UIViewController, selectOcId: [String]) {
  317. let mainMenuViewController = UIStoryboard.init(name: "NCMenu", bundle: nil).instantiateViewController(withIdentifier: "NCMainMenuTableViewController") as! NCMainMenuTableViewController
  318. mainMenuViewController.actions = self.initMenuSelect(viewController: viewController, selectOcId: selectOcId)
  319. let menuPanelController = NCMenuPanelController()
  320. menuPanelController.parentPresenter = viewController
  321. menuPanelController.delegate = mainMenuViewController
  322. menuPanelController.set(contentViewController: mainMenuViewController)
  323. menuPanelController.track(scrollView: mainMenuViewController.tableView)
  324. viewController.present(menuPanelController, animated: true, completion: nil)
  325. }
  326. private func initMenuSelect(viewController: UIViewController, selectOcId: [String]) -> [NCMenuAction] {
  327. var actions = [NCMenuAction]()
  328. //
  329. // SELECT ALL
  330. //
  331. actions.append(
  332. NCMenuAction(
  333. title: NSLocalizedString("_select_all_", comment: ""),
  334. icon: UIImage(named: "selectFull")!.image(color: NCBrandColor.shared.icon, size: 50),
  335. action: { menuAction in
  336. self.collectionViewSelectAll()
  337. }
  338. )
  339. )
  340. //
  341. // COPY
  342. //
  343. if #available(iOS 13.0, *) {
  344. actions.append(
  345. NCMenuAction(
  346. title: NSLocalizedString("_copy_file_", comment: ""),
  347. icon: UIImage(systemName: "doc.on.doc")!.image(color: NCBrandColor.shared.icon, size: 50),
  348. action: { menuAction in
  349. var copyOcIds = [String]()
  350. for ocId in selectOcId {
  351. copyOcIds.append(ocId)
  352. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  353. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  354. copyOcIds.append(metadataMOV.ocId)
  355. }
  356. }
  357. }
  358. NCCollectionCommon.shared.copyFile(ocIds: copyOcIds)
  359. self.tapSelect(sender: self)
  360. }
  361. )
  362. )
  363. }
  364. //
  365. // COPY - MOVE
  366. //
  367. actions.append(
  368. NCMenuAction(
  369. title: NSLocalizedString("_move_or_copy_selected_files_", comment: ""),
  370. icon: UIImage(named: "move")!.image(color: NCBrandColor.shared.icon, size: 50),
  371. action: { menuAction in
  372. var meradatasSelect = [tableMetadata]()
  373. for ocId in selectOcId {
  374. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  375. meradatasSelect.append(metadata)
  376. }
  377. }
  378. if meradatasSelect.count > 0 {
  379. NCCollectionCommon.shared.openSelectView(items: meradatasSelect, viewController: self)
  380. }
  381. self.tapSelect(sender: self)
  382. }
  383. )
  384. )
  385. //
  386. // SAVE TO PHOTO GALLERY
  387. //
  388. actions.append(
  389. NCMenuAction(
  390. title: NSLocalizedString("_save_selected_files_", comment: ""),
  391. icon: UIImage(named: "saveSelectedFiles")!.image(color: NCBrandColor.shared.icon, size: 50),
  392. action: { menuAction in
  393. for ocId in selectOcId {
  394. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  395. if metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileImage || metadata.typeFile == NCBrandGlobal.shared.metadataTypeFileVideo {
  396. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  397. NCCollectionCommon.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV)
  398. } else {
  399. NCOperationQueue.shared.download(metadata: metadata, selector: NCBrandGlobal.shared.selectorSaveAlbum, setFavorite: false)
  400. }
  401. }
  402. }
  403. }
  404. self.tapSelect(sender: self)
  405. }
  406. )
  407. )
  408. //
  409. // DELETE
  410. //
  411. actions.append(
  412. NCMenuAction(
  413. title: NSLocalizedString("_delete_selected_files_", comment: ""),
  414. icon: UIImage(named: "trash")!.image(color: NCBrandColor.shared.icon, size: 50),
  415. action: { menuAction in
  416. let alertController = UIAlertController(title: "", message: NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  417. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  418. for ocId in selectOcId {
  419. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  420. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: false)
  421. }
  422. }
  423. self.tapSelect(sender: self)
  424. })
  425. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (action:UIAlertAction) in
  426. for ocId in selectOcId {
  427. if let metadata = NCManageDatabase.shared.getMetadataFromOcId(ocId) {
  428. NCOperationQueue.shared.delete(metadata: metadata, onlyLocal: true)
  429. }
  430. }
  431. self.tapSelect(sender: self)
  432. })
  433. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in })
  434. self.present(alertController, animated: true, completion:nil)
  435. }
  436. )
  437. )
  438. return actions
  439. }
  440. }