NCCollectionViewCommon+Menu.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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 NextcloudKit
  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 isDirectoryE2EE = NCUtility.shared.isDirectoryE2EE(metadata: metadata)
  37. let serverUrlHome = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, userId: appDelegate.userId)
  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. let applicationHandle = NCApplicationHandle()
  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. order: 0,
  62. action: nil
  63. )
  64. )
  65. if metadata.lock {
  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. var lockTimeString: String?
  75. if let lockTime = metadata.lockTimeOut {
  76. if lockTime >= Date().addingTimeInterval(60),
  77. let timeInterval = (lockTime.timeIntervalSince1970 - Date().timeIntervalSince1970).format() {
  78. lockTimeString = String(format: NSLocalizedString("_time_remaining_", comment: ""), timeInterval)
  79. } else if lockTime > Date() {
  80. lockTimeString = NSLocalizedString("_less_a_minute_", comment: "")
  81. } // else: don't show negative time
  82. }
  83. if let lockTime = metadata.lockTime, lockTimeString == nil {
  84. lockTimeString = DateFormatter.localizedString(from: lockTime, dateStyle: .short, timeStyle: .short)
  85. }
  86. actions.append(
  87. NCMenuAction(
  88. title: String(format: NSLocalizedString("_locked_by_", comment: ""), lockOwnerName),
  89. details: lockTimeString,
  90. icon: lockIcon,
  91. order: 10,
  92. action: nil)
  93. )
  94. }
  95. //
  96. // LOCK / UNLOCK
  97. //
  98. let hasLockCapability = NCManageDatabase.shared.getCapabilitiesServerInt(account: appDelegate.account, elements: NCElementsJSON.shared.capabilitiesFilesLockVersion) >= 1
  99. if !metadata.directory, metadata.canUnlock(as: appDelegate.userId), hasLockCapability {
  100. actions.append(NCMenuAction.lockUnlockFiles(shouldLock: !metadata.lock, metadatas: [metadata], order: 15))
  101. }
  102. //
  103. // SET FOLDER E2EE
  104. //
  105. if !isDirectoryE2EE && metadata.directory && metadata.size == 0 && !metadata.e2eEncrypted && CCUtility.isEnd(toEndEnabled: appDelegate.account) {
  106. actions.append(
  107. NCMenuAction(
  108. title: NSLocalizedString("_e2e_set_folder_encrypted_", comment: ""),
  109. icon: NCUtility.shared.loadImage(named: "lock"),
  110. order: 15,
  111. action: { _ in
  112. NextcloudKit.shared.markE2EEFolder(fileId: metadata.fileId, delete: false) { account, error in
  113. if error == .success {
  114. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl))
  115. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: true, richWorkspace: nil, account: metadata.account)
  116. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: true)
  117. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl": metadata.serverUrl])
  118. } else {
  119. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_mark_folder_", comment: ""), error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
  120. }
  121. }
  122. }
  123. )
  124. )
  125. }
  126. //
  127. // UNSET FOLDER E2EE
  128. //
  129. if !isDirectoryE2EE && metadata.directory && metadata.size == 0 && metadata.e2eEncrypted && CCUtility.isEnd(toEndEnabled: appDelegate.account) {
  130. actions.append(
  131. NCMenuAction(
  132. title: NSLocalizedString("_e2e_remove_folder_encrypted_", comment: ""),
  133. icon: NCUtility.shared.loadImage(named: "lock"),
  134. order: 15,
  135. action: { _ in
  136. NextcloudKit.shared.markE2EEFolder(fileId: metadata.fileId, delete: true) { account, error in
  137. if error == .success {
  138. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl))
  139. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: false, richWorkspace: nil, account: metadata.account)
  140. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: false)
  141. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl": metadata.serverUrl])
  142. } else {
  143. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_delete_mark_folder_", comment: ""), error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
  144. }
  145. }
  146. }
  147. )
  148. )
  149. }
  150. actions.append(.seperator(order: 20))
  151. //
  152. // FAVORITE
  153. // FIXME: PROPPATCH doesn't work
  154. // https://github.com/nextcloud/files_lock/issues/68
  155. if !metadata.lock {
  156. actions.append(
  157. NCMenuAction(
  158. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  159. icon: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite),
  160. order: 30,
  161. action: { _ in
  162. NCNetworking.shared.favoriteMetadata(metadata) { error in
  163. if error != .success {
  164. NCContentPresenter.shared.showError(error: error)
  165. }
  166. }
  167. }
  168. )
  169. )
  170. }
  171. //
  172. // DETAIL
  173. //
  174. if !isDirectoryE2EE && !appDelegate.disableSharesView {
  175. actions.append(
  176. NCMenuAction(
  177. title: NSLocalizedString("_details_", comment: ""),
  178. icon: NCUtility.shared.loadImage(named: "info"),
  179. order: 40,
  180. action: { _ in
  181. NCFunctionCenter.shared.openShare(viewController: self, metadata: metadata, indexPage: .activity)
  182. }
  183. )
  184. )
  185. }
  186. //
  187. // OFFLINE
  188. //
  189. if !isDirectoryE2EE {
  190. actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: self, order: 60, completion: {
  191. self.reloadDataSource()
  192. }))
  193. }
  194. //
  195. // OPEN with external editor
  196. //
  197. if metadata.classFile == NKCommon.typeClassFile.document.rawValue && editors.contains(NCGlobal.shared.editorText) && ((editors.contains(NCGlobal.shared.editorOnlyoffice) || isRichDocument)) {
  198. var editor = ""
  199. var title = ""
  200. var icon: UIImage?
  201. if editors.contains(NCGlobal.shared.editorOnlyoffice) {
  202. editor = NCGlobal.shared.editorOnlyoffice
  203. title = NSLocalizedString("_open_in_onlyoffice_", comment: "")
  204. icon = NCUtility.shared.loadImage(named: "onlyoffice")
  205. } else if isRichDocument {
  206. editor = NCGlobal.shared.editorCollabora
  207. title = NSLocalizedString("_open_in_collabora_", comment: "")
  208. icon = NCUtility.shared.loadImage(named: "collabora")
  209. }
  210. if editor != "" {
  211. actions.append(
  212. NCMenuAction(
  213. title: title,
  214. icon: icon!,
  215. order: 70,
  216. action: { _ in
  217. NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon, editor: editor, isRichDocument: isRichDocument)
  218. }
  219. )
  220. )
  221. }
  222. }
  223. //
  224. // OPEN IN
  225. //
  226. if !metadata.directory && !NCBrandOptions.shared.disable_openin_file {
  227. actions.append(.openInAction(selectedMetadatas: [metadata], viewController: self, order: 80))
  228. }
  229. //
  230. // PRINT
  231. //
  232. if metadata.isPrintable {
  233. actions.append(.printAction(metadata: metadata, order: 90))
  234. }
  235. //
  236. // SAVE
  237. //
  238. if (metadata.classFile == NKCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml") || metadata.classFile == NKCommon.typeClassFile.video.rawValue {
  239. actions.append(.saveMediaAction(selectedMediaMetadatas: [metadata], order: 100))
  240. }
  241. //
  242. // SAVE AS SCAN
  243. //
  244. if metadata.classFile == NKCommon.typeClassFile.image.rawValue && metadata.contentType != "image/svg+xml" {
  245. actions.append(
  246. NCMenuAction(
  247. title: NSLocalizedString("_save_as_scan_", comment: ""),
  248. icon: NCUtility.shared.loadImage(named: "viewfinder.circle"),
  249. order: 110,
  250. action: { _ in
  251. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan)
  252. }
  253. )
  254. )
  255. }
  256. //
  257. // RENAME
  258. //
  259. if (!isDirectoryE2EE && metadata.e2eEncrypted) || metadata.lock {
  260. print("Not possible rename")
  261. } else {
  262. actions.append(
  263. NCMenuAction(
  264. title: NSLocalizedString("_rename_", comment: ""),
  265. icon: NCUtility.shared.loadImage(named: "pencil"),
  266. order: 120,
  267. action: { _ in
  268. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  269. vcRename.metadata = metadata
  270. vcRename.imagePreview = imageIcon
  271. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  272. self.present(popup, animated: true)
  273. }
  274. }
  275. )
  276. )
  277. }
  278. //
  279. // COPY - MOVE
  280. //
  281. if isDirectoryE2EE || metadata.e2eEncrypted {
  282. print("Not possible copy/move")
  283. } else {
  284. actions.append(.moveOrCopyAction(selectedMetadatas: [metadata], order: 130))
  285. }
  286. //
  287. // COPY
  288. //
  289. if !metadata.directory {
  290. actions.append(.copyAction(selectOcId: [metadata.ocId], hudView: self.view, order: 140))
  291. }
  292. //
  293. // MODIFY
  294. //
  295. if !isDirectoryE2EE && metadata.contentType != "image/gif" && metadata.contentType != "image/svg+xml" && (metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" || metadata.classFile == NKCommon.typeClassFile.image.rawValue) {
  296. actions.append(
  297. NCMenuAction(
  298. title: NSLocalizedString("_modify_", comment: ""),
  299. icon: NCUtility.shared.loadImage(named: "pencil.tip.crop.circle"),
  300. order: 150,
  301. action: { menuAction in
  302. NCFunctionCenter.shared.openDownload(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook)
  303. }
  304. )
  305. )
  306. }
  307. //
  308. // COLOR FOLDER
  309. //
  310. if self is NCFiles, metadata.directory {
  311. actions.append(
  312. NCMenuAction(
  313. title: NSLocalizedString("_change_color_", comment: ""),
  314. icon: NCUtility.shared.loadImage(named: "palette"),
  315. order: 160,
  316. action: { _ in
  317. if let picker = UIStoryboard(name: "NCColorPicker", bundle: nil).instantiateInitialViewController() as? NCColorPicker {
  318. picker.metadata = metadata
  319. let popup = NCPopupViewController(contentController: picker, popupWidth: 200, popupHeight: 320)
  320. popup.backgroundAlpha = 0
  321. self.present(popup, animated: true)
  322. }
  323. }
  324. )
  325. )
  326. }
  327. //
  328. // DELETE
  329. //
  330. if !isDirectoryE2EE && metadata.e2eEncrypted {
  331. print("Not possible delete")
  332. } else {
  333. actions.append(.deleteAction(selectedMetadatas: [metadata], metadataFolder: metadataFolder, viewController: self, order: 170))
  334. }
  335. applicationHandle.addCollectionViewCommonMenu(metadata: metadata, imageIcon: imageIcon, actions: actions)
  336. presentMenu(with: actions)
  337. }
  338. }
  339. extension TimeInterval {
  340. func format() -> String? {
  341. let formatter = DateComponentsFormatter()
  342. formatter.allowedUnits = [.day, .hour, .minute]
  343. formatter.unitsStyle = .full
  344. formatter.maximumUnitCount = 1
  345. return formatter.string(from: self)
  346. }
  347. }