NCCollectionViewCommon+Menu.swift 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. // Copyright © 2022 Henrik Storch. All rights reserved.
  9. //
  10. // Author Philippe Weidmann
  11. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  12. // Author Henrik Storch <henrik.storch@nextcloud.com>
  13. //
  14. // This program is free software: you can redistribute it and/or modify
  15. // it under the terms of the GNU General Public License as published by
  16. // the Free Software Foundation, either version 3 of the License, or
  17. // (at your option) any later version.
  18. //
  19. // This program is distributed in the hope that it will be useful,
  20. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. // GNU General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU General Public License
  25. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. import UIKit
  28. import FloatingPanel
  29. import NCCommunication
  30. import Queuer
  31. extension NCCollectionViewCommon {
  32. func toggleMenu(metadata: tableMetadata, imageIcon: UIImage?) {
  33. var actions = [NCMenuAction]()
  34. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId) else { return }
  35. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  36. let isFolderEncrypted = CCUtility.isFolderEncrypted(metadata.serverUrl, e2eEncrypted: metadata.e2eEncrypted, account: metadata.account, urlBase: metadata.urlBase)
  37. let serverUrlHome = NCUtilityFileSystem.shared.getHomeServer(account: appDelegate.account)
  38. var isOffline = false
  39. var titleDelete = NSLocalizedString("_delete_", comment: "")
  40. if NCManageDatabase.shared.isMetadataShareOrMounted(metadata: metadata, metadataFolder: metadataFolder) {
  41. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  42. } else if metadata.directory {
  43. titleDelete = NSLocalizedString("_delete_folder_", comment: "")
  44. } else {
  45. titleDelete = NSLocalizedString("_delete_file_", comment: "")
  46. }
  47. if let metadataFolder = metadataFolder {
  48. let isShare = metadata.permissions.contains(NCGlobal.shared.permissionShared) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionShared)
  49. let isMounted = metadata.permissions.contains(NCGlobal.shared.permissionMounted) && !metadataFolder.permissions.contains(NCGlobal.shared.permissionMounted)
  50. if isShare || isMounted {
  51. titleDelete = NSLocalizedString("_leave_share_", comment: "")
  52. }
  53. }
  54. if metadata.directory {
  55. if let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl)) {
  56. isOffline = directory.offline
  57. }
  58. } else {
  59. if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  60. isOffline = localFile.offline
  61. }
  62. }
  63. let editors = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
  64. let isRichDocument = NCUtility.shared.isRichDocument(metadata)
  65. var iconHeader: UIImage!
  66. if imageIcon != nil {
  67. iconHeader = imageIcon!
  68. } else {
  69. if metadata.directory {
  70. iconHeader = NCBrandColor.cacheImages.folder
  71. } else {
  72. iconHeader = NCBrandColor.cacheImages.file
  73. }
  74. }
  75. actions.append(
  76. NCMenuAction(
  77. title: metadata.fileNameView,
  78. icon: iconHeader,
  79. action: nil
  80. )
  81. )
  82. //
  83. // FAVORITE
  84. //
  85. actions.append(
  86. NCMenuAction(
  87. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  88. icon: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite),
  89. action: { _ in
  90. NCNetworking.shared.favoriteMetadata(metadata) { errorCode, errorDescription in
  91. if errorCode != 0 {
  92. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  93. }
  94. }
  95. }
  96. )
  97. )
  98. //
  99. // DETAIL
  100. //
  101. if !isFolderEncrypted && !appDelegate.disableSharesView {
  102. actions.append(
  103. NCMenuAction(
  104. title: NSLocalizedString("_details_", comment: ""),
  105. icon: NCUtility.shared.loadImage(named: "info"),
  106. action: { _ in
  107. NCFunctionCenter.shared.openShare(viewController: self, metadata: metadata, indexPage: .activity)
  108. }
  109. )
  110. )
  111. }
  112. //
  113. // OFFLINE
  114. //
  115. if !isFolderEncrypted {
  116. actions.append(
  117. NCMenuAction(
  118. title: isOffline ? NSLocalizedString("_remove_available_offline_", comment: "") : NSLocalizedString("_set_available_offline_", comment: ""),
  119. icon: NCUtility.shared.loadImage(named: "tray.and.arrow.down"),
  120. action: { _ in
  121. if isOffline {
  122. if metadata.directory {
  123. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, offline: false, account: self.appDelegate.account)
  124. } else {
  125. NCManageDatabase.shared.setLocalFile(ocId: metadata.ocId, offline: false)
  126. }
  127. } else {
  128. if metadata.directory {
  129. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, offline: true, account: self.appDelegate.account)
  130. NCOperationQueue.shared.synchronizationMetadata(metadata, selector: NCGlobal.shared.selectorDownloadAllFile)
  131. } else {
  132. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorLoadOffline) { _ in }
  133. if let metadataLivePhoto = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  134. NCNetworking.shared.download(metadata: metadataLivePhoto, selector: NCGlobal.shared.selectorLoadOffline) { _ in }
  135. }
  136. }
  137. }
  138. self.reloadDataSource()
  139. }
  140. )
  141. )
  142. }
  143. //
  144. // OPEN with external editor
  145. //
  146. if metadata.classFile == NCCommunicationCommon.typeClassFile.document.rawValue && editors.contains(NCGlobal.shared.editorText) && ((editors.contains(NCGlobal.shared.editorOnlyoffice) || isRichDocument)) {
  147. var editor = ""
  148. var title = ""
  149. var icon: UIImage?
  150. if editors.contains(NCGlobal.shared.editorOnlyoffice) {
  151. editor = NCGlobal.shared.editorOnlyoffice
  152. title = NSLocalizedString("_open_in_onlyoffice_", comment: "")
  153. icon = NCUtility.shared.loadImage(named: "onlyoffice")
  154. } else if isRichDocument {
  155. editor = NCGlobal.shared.editorCollabora
  156. title = NSLocalizedString("_open_in_collabora_", comment: "")
  157. icon = NCUtility.shared.loadImage(named: "collabora")
  158. }
  159. if editor != "" {
  160. actions.append(
  161. NCMenuAction(
  162. title: title,
  163. icon: icon!,
  164. action: { _ in
  165. NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon, editor: editor, isRichDocument: isRichDocument)
  166. }
  167. )
  168. )
  169. }
  170. }
  171. //
  172. // OPEN IN
  173. //
  174. if !metadata.directory && !NCBrandOptions.shared.disable_openin_file {
  175. actions.append(
  176. NCMenuAction(
  177. title: NSLocalizedString("_open_in_", comment: ""),
  178. icon: NCUtility.shared.loadImage(named: "square.and.arrow.up"),
  179. action: { menuAction in
  180. if self is NCFileViewInFolder {
  181. self.dismiss(animated: true) {
  182. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  183. }
  184. } else {
  185. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorOpenIn)
  186. }
  187. }
  188. )
  189. )
  190. }
  191. //
  192. // PRINT
  193. //
  194. if (metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml") || metadata.contentType == "application/pdf" || metadata.contentType == "com.adobe.pdf" {
  195. actions.append(
  196. NCMenuAction(
  197. title: NSLocalizedString("_print_", comment: ""),
  198. icon: NCUtility.shared.loadImage(named: "printer"),
  199. action: { _ in
  200. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorPrint)
  201. }
  202. )
  203. )
  204. }
  205. //
  206. // SAVE
  207. //
  208. if (metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml") || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  209. var title: String = NSLocalizedString("_save_selected_files_", comment: "")
  210. var icon = NCUtility.shared.loadImage(named: "square.and.arrow.down")
  211. let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata)
  212. if metadataMOV != nil {
  213. title = NSLocalizedString("_livephoto_save_", comment: "")
  214. icon = NCUtility.shared.loadImage(named: "livephoto")
  215. }
  216. actions.append(
  217. NCMenuAction(
  218. title: title,
  219. icon: icon,
  220. action: { _ in
  221. if metadataMOV != nil {
  222. NCFunctionCenter.shared.saveLivePhoto(metadata: metadata, metadataMOV: metadataMOV!)
  223. } else {
  224. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  225. NCFunctionCenter.shared.saveAlbum(metadata: metadata)
  226. } else {
  227. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAlbum)
  228. }
  229. }
  230. }
  231. )
  232. )
  233. }
  234. //
  235. // SAVE AS SCAN
  236. //
  237. if #available(iOS 13.0, *) {
  238. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml" {
  239. actions.append(
  240. NCMenuAction(
  241. title: NSLocalizedString("_save_as_scan_", comment: ""),
  242. icon: NCUtility.shared.loadImage(named: "viewfinder.circle"),
  243. action: { _ in
  244. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan)
  245. }
  246. )
  247. )
  248. }
  249. }
  250. //
  251. // RENAME
  252. //
  253. if !(isFolderEncrypted && metadata.serverUrl == serverUrlHome) {
  254. actions.append(
  255. NCMenuAction(
  256. title: NSLocalizedString("_rename_", comment: ""),
  257. icon: NCUtility.shared.loadImage(named: "pencil"),
  258. action: { _ in
  259. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  260. vcRename.metadata = metadata
  261. vcRename.imagePreview = imageIcon
  262. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  263. self.present(popup, animated: true)
  264. }
  265. }
  266. )
  267. )
  268. }
  269. //
  270. // COPY - MOVE
  271. //
  272. if !isFolderEncrypted && serverUrl != "" {
  273. actions.append(
  274. NCMenuAction(
  275. title: NSLocalizedString("_move_or_copy_", comment: ""),
  276. icon: NCUtility.shared.loadImage(named: "arrow.up.right.square"),
  277. action: { _ in
  278. NCFunctionCenter.shared.openSelectView(items: [metadata], viewController: self)
  279. }
  280. )
  281. )
  282. }
  283. //
  284. // COPY
  285. //
  286. if !metadata.directory {
  287. actions.append(
  288. NCMenuAction(
  289. title: NSLocalizedString("_copy_file_", comment: ""),
  290. icon: NCUtility.shared.loadImage(named: "doc.on.doc"),
  291. action: { _ in
  292. self.appDelegate.pasteboardOcIds = [metadata.ocId]
  293. NCFunctionCenter.shared.copyPasteboard()
  294. }
  295. )
  296. )
  297. }
  298. /*
  299. //
  300. // USE AS BACKGROUND
  301. //
  302. if #available(iOS 13.0, *) {
  303. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && self.layoutKey == NCGlobal.shared.layoutViewFiles && !NCBrandOptions.shared.disable_background_image {
  304. actions.append(
  305. NCMenuAction(
  306. title: NSLocalizedString("_use_as_background_", comment: ""),
  307. icon: NCUtility.shared.loadImage(named: "text.below.photo"),
  308. action: { menuAction in
  309. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  310. NCFunctionCenter.shared.saveBackground(metadata: metadata)
  311. } else {
  312. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveBackground)
  313. }
  314. }
  315. )
  316. )
  317. }
  318. }
  319. */
  320. //
  321. // MODIFY
  322. //
  323. if #available(iOS 13.0, *) {
  324. if !isFolderEncrypted && metadata.contentType != "image/gif" && metadata.contentType != "image/svg+xml" && (metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" || metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue) {
  325. actions.append(
  326. NCMenuAction(
  327. title: NSLocalizedString("_modify_", comment: ""),
  328. icon: NCUtility.shared.loadImage(named: "pencil.tip.crop.circle"),
  329. action: { menuAction in
  330. if self is NCFileViewInFolder {
  331. self.dismiss(animated: true) {
  332. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  333. }
  334. } else {
  335. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  336. }
  337. }
  338. )
  339. )
  340. }
  341. }
  342. //
  343. // DELETE
  344. //
  345. actions.append(
  346. NCMenuAction(
  347. title: titleDelete,
  348. icon: NCUtility.shared.loadImage(named: "trash"),
  349. action: { _ in
  350. let alertController = UIAlertController(title: "", message: metadata.fileNameView + "\n\n" + NSLocalizedString("_want_delete_", comment: ""), preferredStyle: .alert)
  351. alertController.addAction(UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (_: UIAlertAction) in
  352. NCOperationQueue.shared.delete(metadata: metadata, onlyLocalCache: false)
  353. })
  354. alertController.addAction(UIAlertAction(title: NSLocalizedString("_remove_local_file_", comment: ""), style: .default) { (_: UIAlertAction) in
  355. NCOperationQueue.shared.delete(metadata: metadata, onlyLocalCache: true)
  356. })
  357. alertController.addAction(UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (_: UIAlertAction) in })
  358. self.present(alertController, animated: true, completion: nil)
  359. }
  360. )
  361. )
  362. //
  363. // SET FOLDER E2EE
  364. //
  365. if !metadata.e2eEncrypted && metadata.directory && CCUtility.isEnd(toEndEnabled: appDelegate.account) && metadata.serverUrl == serverUrlHome {
  366. actions.append(
  367. NCMenuAction(
  368. title: NSLocalizedString("_e2e_set_folder_encrypted_", comment: ""),
  369. icon: NCUtility.shared.loadImage(named: "lock"),
  370. action: { _ in
  371. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: false) { account, errorCode, errorDescription in
  372. if errorCode == 0 {
  373. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl))
  374. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: true, richWorkspace: nil, account: metadata.account)
  375. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: true)
  376. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl": metadata.serverUrl])
  377. } else {
  378. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_mark_folder_", comment: ""), description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: .error, errorCode: errorCode)
  379. }
  380. }
  381. }
  382. )
  383. )
  384. }
  385. //
  386. // UNSET FOLDER E2EE
  387. //
  388. if metadata.e2eEncrypted && metadata.directory && CCUtility.isEnd(toEndEnabled: appDelegate.account) && metadata.serverUrl == serverUrlHome {
  389. actions.append(
  390. NCMenuAction(
  391. title: NSLocalizedString("_e2e_remove_folder_encrypted_", comment: ""),
  392. icon: NCUtility.shared.loadImage(named: "lock"),
  393. action: { _ in
  394. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: true) { account, errorCode, errorDescription in
  395. if errorCode == 0 {
  396. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl))
  397. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: false, richWorkspace: nil, account: metadata.account)
  398. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: false)
  399. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl": metadata.serverUrl])
  400. } else {
  401. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_delete_mark_folder_", comment: ""), description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: .error, errorCode: errorCode)
  402. }
  403. }
  404. }
  405. )
  406. )
  407. }
  408. presentMenu(with: actions)
  409. }
  410. }