NCCollectionViewCommon+Menu.swift 17 KB

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