NCCollectionViewCommon+Menu.swift 16 KB

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