NCCollectionViewCommon+Menu.swift 18 KB

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