NCCollectionViewCommon+Menu.swift 24 KB

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