NCCollectionViewCommon+Menu.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343
  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. let isOffline: Bool
  39. if metadata.directory, let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl)) {
  40. isOffline = directory.offline
  41. } else if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  42. isOffline = localFile.offline
  43. } else { isOffline = false }
  44. let editors = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
  45. let isRichDocument = NCUtility.shared.isRichDocument(metadata)
  46. var iconHeader: UIImage!
  47. if imageIcon != nil {
  48. iconHeader = imageIcon!
  49. } else {
  50. if metadata.directory {
  51. iconHeader = NCBrandColor.cacheImages.folder
  52. } else {
  53. iconHeader = NCBrandColor.cacheImages.file
  54. }
  55. }
  56. actions.append(
  57. NCMenuAction(
  58. title: metadata.fileNameView,
  59. icon: iconHeader,
  60. action: nil
  61. )
  62. )
  63. if metadata.lock {
  64. let lockOwnerName = metadata.lockOwnerDisplayName.isEmpty ? metadata.lockOwner : metadata.lockOwnerDisplayName
  65. actions.append(
  66. NCMenuAction(
  67. title: String(format: NSLocalizedString("_file_locked_by_", comment: ""), lockOwnerName),
  68. icon: NCUtility.shared.loadUserImage(
  69. for: metadata.lockOwner,
  70. displayName: lockOwnerName,
  71. userBaseUrl: metadata),
  72. action: nil)
  73. )
  74. }
  75. actions.append(.seperator)
  76. //
  77. // FAVORITE
  78. //
  79. actions.append(
  80. NCMenuAction(
  81. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  82. icon: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite),
  83. action: { _ in
  84. NCNetworking.shared.favoriteMetadata(metadata) { errorCode, errorDescription in
  85. if errorCode != 0 {
  86. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  87. }
  88. }
  89. }
  90. )
  91. )
  92. if !metadata.directory {
  93. actions.append(.lockUnlockFiles(shouldLock: !metadata.lock, metadatas: [metadata]))
  94. }
  95. //
  96. // DETAIL
  97. //
  98. if !isFolderEncrypted && !appDelegate.disableSharesView {
  99. actions.append(
  100. NCMenuAction(
  101. title: NSLocalizedString("_details_", comment: ""),
  102. icon: NCUtility.shared.loadImage(named: "info"),
  103. action: { _ in
  104. NCFunctionCenter.shared.openShare(viewController: self, metadata: metadata, indexPage: .activity)
  105. }
  106. )
  107. )
  108. }
  109. //
  110. // OFFLINE
  111. //
  112. if !isFolderEncrypted {
  113. actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: self, completion: self.reloadDataSource))
  114. }
  115. //
  116. // OPEN with external editor
  117. //
  118. if metadata.classFile == NCCommunicationCommon.typeClassFile.document.rawValue && editors.contains(NCGlobal.shared.editorText) && ((editors.contains(NCGlobal.shared.editorOnlyoffice) || isRichDocument)) {
  119. var editor = ""
  120. var title = ""
  121. var icon: UIImage?
  122. if editors.contains(NCGlobal.shared.editorOnlyoffice) {
  123. editor = NCGlobal.shared.editorOnlyoffice
  124. title = NSLocalizedString("_open_in_onlyoffice_", comment: "")
  125. icon = NCUtility.shared.loadImage(named: "onlyoffice")
  126. } else if isRichDocument {
  127. editor = NCGlobal.shared.editorCollabora
  128. title = NSLocalizedString("_open_in_collabora_", comment: "")
  129. icon = NCUtility.shared.loadImage(named: "collabora")
  130. }
  131. if editor != "" {
  132. actions.append(
  133. NCMenuAction(
  134. title: title,
  135. icon: icon!,
  136. action: { _ in
  137. NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon, editor: editor, isRichDocument: isRichDocument)
  138. }
  139. )
  140. )
  141. }
  142. }
  143. //
  144. // OPEN IN
  145. //
  146. if !metadata.directory && !NCBrandOptions.shared.disable_openin_file {
  147. actions.append(.openInAction(selectedMetadatas: [metadata], viewController: self))
  148. }
  149. //
  150. // PRINT
  151. //
  152. if metadata.isPrintable {
  153. actions.append(.printAction(metadata: metadata))
  154. }
  155. //
  156. // SAVE
  157. //
  158. if (metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml") || metadata.classFile == NCCommunicationCommon.typeClassFile.video.rawValue {
  159. actions.append(.saveMediaAction(selectedMediaMetadatas: [metadata]))
  160. }
  161. //
  162. // SAVE AS SCAN
  163. //
  164. if #available(iOS 13.0, *) {
  165. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml" {
  166. actions.append(
  167. NCMenuAction(
  168. title: NSLocalizedString("_save_as_scan_", comment: ""),
  169. icon: NCUtility.shared.loadImage(named: "viewfinder.circle"),
  170. action: { _ in
  171. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan)
  172. }
  173. )
  174. )
  175. }
  176. }
  177. //
  178. // RENAME
  179. //
  180. if !(isFolderEncrypted && metadata.serverUrl == serverUrlHome) {
  181. actions.append(
  182. NCMenuAction(
  183. title: NSLocalizedString("_rename_", comment: ""),
  184. icon: NCUtility.shared.loadImage(named: "pencil"),
  185. action: { _ in
  186. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  187. vcRename.metadata = metadata
  188. vcRename.imagePreview = imageIcon
  189. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  190. self.present(popup, animated: true)
  191. }
  192. }
  193. )
  194. )
  195. }
  196. //
  197. // COPY - MOVE
  198. //
  199. if !isFolderEncrypted && serverUrl != "" {
  200. actions.append(.moveOrCopyAction(selectedMetadatas: [metadata]))
  201. }
  202. //
  203. // COPY
  204. //
  205. if !metadata.directory {
  206. actions.append(.copyAction(selectOcId: [metadata.ocId], hudView: self.view))
  207. }
  208. /*
  209. //
  210. // USE AS BACKGROUND
  211. //
  212. if #available(iOS 13.0, *) {
  213. if metadata.classFile == NCCommunicationCommon.typeClassFile.image.rawValue && self.layoutKey == NCGlobal.shared.layoutViewFiles && !NCBrandOptions.shared.disable_background_image {
  214. actions.append(
  215. NCMenuAction(
  216. title: NSLocalizedString("_use_as_background_", comment: ""),
  217. icon: NCUtility.shared.loadImage(named: "text.below.photo"),
  218. action: { menuAction in
  219. if CCUtility.fileProviderStorageExists(metadata.ocId, fileNameView: metadata.fileNameView) {
  220. NCFunctionCenter.shared.saveBackground(metadata: metadata)
  221. } else {
  222. NCOperationQueue.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveBackground)
  223. }
  224. }
  225. )
  226. )
  227. }
  228. }
  229. */
  230. //
  231. // MODIFY
  232. //
  233. if #available(iOS 13.0, *) {
  234. 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) {
  235. actions.append(
  236. NCMenuAction(
  237. title: NSLocalizedString("_modify_", comment: ""),
  238. icon: NCUtility.shared.loadImage(named: "pencil.tip.crop.circle"),
  239. action: { menuAction in
  240. if self is NCFileViewInFolder {
  241. self.dismiss(animated: true) {
  242. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  243. }
  244. } else {
  245. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  246. }
  247. }
  248. )
  249. )
  250. }
  251. }
  252. //
  253. // DELETE
  254. //
  255. actions.append(.deleteAction(selectedMetadatas: [metadata], metadataFolder: metadataFolder, viewController: self))
  256. //
  257. // SET FOLDER E2EE
  258. //
  259. if !metadata.e2eEncrypted && metadata.directory && CCUtility.isEnd(toEndEnabled: appDelegate.account) && metadata.serverUrl == serverUrlHome {
  260. actions.append(
  261. NCMenuAction(
  262. title: NSLocalizedString("_e2e_set_folder_encrypted_", comment: ""),
  263. icon: NCUtility.shared.loadImage(named: "lock"),
  264. action: { _ in
  265. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: false) { account, errorCode, errorDescription in
  266. if errorCode == 0 {
  267. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl))
  268. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: true, richWorkspace: nil, account: metadata.account)
  269. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: true)
  270. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl": metadata.serverUrl])
  271. } else {
  272. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_mark_folder_", comment: ""), description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: .error, errorCode: errorCode)
  273. }
  274. }
  275. }
  276. )
  277. )
  278. }
  279. //
  280. // UNSET FOLDER E2EE
  281. //
  282. if metadata.e2eEncrypted && metadata.directory && CCUtility.isEnd(toEndEnabled: appDelegate.account) && metadata.serverUrl == serverUrlHome {
  283. actions.append(
  284. NCMenuAction(
  285. title: NSLocalizedString("_e2e_remove_folder_encrypted_", comment: ""),
  286. icon: NCUtility.shared.loadImage(named: "lock"),
  287. action: { _ in
  288. NCCommunication.shared.markE2EEFolder(fileId: metadata.fileId, delete: true) { account, errorCode, errorDescription in
  289. if errorCode == 0 {
  290. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl))
  291. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: false, richWorkspace: nil, account: metadata.account)
  292. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: false)
  293. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl": metadata.serverUrl])
  294. } else {
  295. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_delete_mark_folder_", comment: ""), description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: .error, errorCode: errorCode)
  296. }
  297. }
  298. }
  299. )
  300. )
  301. }
  302. presentMenu(with: actions)
  303. }
  304. }