NCCollectionViewCommon+Menu.swift 23 KB

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