NCCollectionViewCommon+Menu.swift 17 KB

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