NCCollectionViewCommon+Menu.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  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, indexPath: IndexPath, imageIcon: UIImage?) {
  33. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId),
  34. let sceneIdentifier = (tabBarController as? NCMainTabBarController)?.sceneIdentifier else { return }
  35. var actions = [NCMenuAction]()
  36. let serverUrl = metadata.serverUrl + "/" + metadata.fileName
  37. var isOffline: Bool = false
  38. if metadata.directory, let directory = NCManageDatabase.shared.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.account, serverUrl)) {
  39. isOffline = directory.offline
  40. } else if let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId)) {
  41. isOffline = localFile.offline
  42. }
  43. let editors = utility.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
  44. let isRichDocument = utility.isRichDocument(metadata)
  45. let applicationHandle = NCApplicationHandle()
  46. var iconHeader: UIImage!
  47. if imageIcon != nil {
  48. iconHeader = imageIcon!
  49. } else {
  50. if metadata.directory {
  51. iconHeader = NCImageCache.images.folder
  52. } else {
  53. iconHeader = NCImageCache.images.file
  54. }
  55. }
  56. actions.append(
  57. NCMenuAction(
  58. title: metadata.fileNameView,
  59. boldTitle: true,
  60. icon: iconHeader,
  61. order: 0,
  62. action: nil
  63. )
  64. )
  65. actions.append(.seperator(order: 1))
  66. //
  67. // DETAILS
  68. //
  69. if !NCGlobal.shared.disableSharesView {
  70. actions.append(
  71. NCMenuAction(
  72. title: NSLocalizedString("_details_", comment: ""),
  73. icon: utility.loadImage(named: "info.circle", colors: [NCBrandColor.shared.iconImageColor]),
  74. order: 10,
  75. action: { _ in
  76. NCActionCenter.shared.openShare(viewController: self, metadata: metadata, page: .activity)
  77. }
  78. )
  79. )
  80. }
  81. if metadata.lock {
  82. var lockOwnerName = metadata.lockOwnerDisplayName.isEmpty ? metadata.lockOwner : metadata.lockOwnerDisplayName
  83. var lockIcon = utility.loadUserImage(for: metadata.lockOwner, displayName: lockOwnerName, userBaseUrl: metadata)
  84. if metadata.lockOwnerType != 0 {
  85. lockOwnerName += " app"
  86. if !metadata.lockOwnerEditor.isEmpty, let appIcon = UIImage(named: metadata.lockOwnerEditor) {
  87. lockIcon = appIcon
  88. }
  89. }
  90. var lockTimeString: String?
  91. if let lockTime = metadata.lockTimeOut {
  92. if lockTime >= Date().addingTimeInterval(60),
  93. let timeInterval = (lockTime.timeIntervalSince1970 - Date().timeIntervalSince1970).format() {
  94. lockTimeString = String(format: NSLocalizedString("_time_remaining_", comment: ""), timeInterval)
  95. } else if lockTime > Date() {
  96. lockTimeString = NSLocalizedString("_less_a_minute_", comment: "")
  97. } // else: don't show negative time
  98. }
  99. if let lockTime = metadata.lockTime, lockTimeString == nil {
  100. lockTimeString = DateFormatter.localizedString(from: lockTime, dateStyle: .short, timeStyle: .short)
  101. }
  102. actions.append(
  103. NCMenuAction(
  104. title: String(format: NSLocalizedString("_locked_by_", comment: ""), lockOwnerName),
  105. details: lockTimeString,
  106. icon: lockIcon,
  107. order: 20,
  108. action: nil)
  109. )
  110. }
  111. //
  112. // VIEW IN FOLDER
  113. //
  114. if layoutKey != NCGlobal.shared.layoutViewFiles {
  115. actions.append(
  116. NCMenuAction(
  117. title: NSLocalizedString("_view_in_folder_", comment: ""),
  118. icon: utility.loadImage(named: "questionmark.folder", colors: [NCBrandColor.shared.iconImageColor]),
  119. order: 21,
  120. action: { _ in
  121. NCActionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil, sceneIdentifier: sceneIdentifier)
  122. }
  123. )
  124. )
  125. }
  126. //
  127. // LOCK / UNLOCK
  128. //
  129. if !metadata.directory, metadata.canUnlock(as: appDelegate.userId), !NCGlobal.shared.capabilityFilesLockVersion.isEmpty {
  130. actions.append(NCMenuAction.lockUnlockFiles(shouldLock: !metadata.lock, metadatas: [metadata], order: 30))
  131. }
  132. //
  133. // SET FOLDER E2EE
  134. //
  135. if metadata.canSetDirectoryAsE2EE {
  136. actions.append(
  137. NCMenuAction(
  138. title: NSLocalizedString("_e2e_set_folder_encrypted_", comment: ""),
  139. icon: utility.loadImage(named: "lock", colors: [NCBrandColor.shared.iconImageColor]),
  140. order: 30,
  141. action: { _ in
  142. Task {
  143. let error = await NCNetworkingE2EEMarkFolder().markFolderE2ee(account: metadata.account, fileName: metadata.fileName, serverUrl: metadata.serverUrl, userId: metadata.userId)
  144. if error != .success {
  145. NCContentPresenter().showError(error: error)
  146. }
  147. }
  148. }
  149. )
  150. )
  151. }
  152. //
  153. // UNSET FOLDER E2EE
  154. //
  155. if metadata.canUnsetDirectoryAsE2EE {
  156. actions.append(
  157. NCMenuAction(
  158. title: NSLocalizedString("_e2e_remove_folder_encrypted_", comment: ""),
  159. icon: utility.loadImage(named: "lock", colors: [NCBrandColor.shared.iconImageColor]),
  160. order: 30,
  161. action: { _ in
  162. NextcloudKit.shared.markE2EEFolder(fileId: metadata.fileId, delete: true) { _, error in
  163. if error == .success {
  164. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl))
  165. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, encrypted: false, account: metadata.account)
  166. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: false)
  167. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl": metadata.serverUrl])
  168. } else {
  169. NCContentPresenter().messageNotification(NSLocalizedString("_e2e_error_", comment: ""), error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
  170. }
  171. }
  172. }
  173. )
  174. )
  175. }
  176. //
  177. // FAVORITE
  178. // FIXME: PROPPATCH doesn't work
  179. // https://github.com/nextcloud/files_lock/issues/68
  180. if !metadata.lock {
  181. actions.append(
  182. NCMenuAction(
  183. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  184. icon: utility.loadImage(named: metadata.favorite ? "star.slash" : "star", colors: [NCBrandColor.shared.yellowFavorite]),
  185. order: 50,
  186. action: { _ in
  187. NCNetworking.shared.favoriteMetadata(metadata) { error in
  188. if error != .success {
  189. NCContentPresenter().showError(error: error)
  190. }
  191. }
  192. }
  193. )
  194. )
  195. }
  196. //
  197. // OFFLINE
  198. //
  199. if metadata.canSetAsAvailableOffline {
  200. actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: self, order: 60, completion: {
  201. self.reloadDataSource()
  202. }))
  203. }
  204. //
  205. // OPEN with external editor
  206. //
  207. if metadata.canOpenExternalEditor {
  208. var editor = ""
  209. var title = ""
  210. var icon: UIImage?
  211. if editors.contains(NCGlobal.shared.editorOnlyoffice) {
  212. editor = NCGlobal.shared.editorOnlyoffice
  213. title = NSLocalizedString("_open_in_onlyoffice_", comment: "")
  214. icon = utility.loadImage(named: "onlyoffice", colors: [NCBrandColor.shared.iconImageColor])
  215. } else if isRichDocument {
  216. editor = NCGlobal.shared.editorCollabora
  217. title = NSLocalizedString("_open_in_collabora_", comment: "")
  218. icon = utility.loadImage(named: "collabora", colors: [NCBrandColor.shared.iconImageColor])
  219. }
  220. if !editor.isEmpty {
  221. actions.append(
  222. NCMenuAction(
  223. title: title,
  224. icon: icon!,
  225. order: 70,
  226. action: { _ in
  227. NCViewer().view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon, editor: editor, isRichDocument: isRichDocument)
  228. }
  229. )
  230. )
  231. }
  232. }
  233. //
  234. // SHARE
  235. //
  236. if metadata.canShare {
  237. actions.append(.share(selectedMetadatas: [metadata], viewController: self, order: 80))
  238. }
  239. //
  240. // SAVE LIVE PHOTO
  241. //
  242. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata),
  243. let hudView = self.tabBarController?.view {
  244. actions.append(
  245. NCMenuAction(
  246. title: NSLocalizedString("_livephoto_save_", comment: ""),
  247. icon: NCUtility().loadImage(named: "livephoto", colors: [NCBrandColor.shared.iconImageColor]),
  248. order: 100,
  249. action: { _ in
  250. NCNetworking.shared.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV, hudView: hudView))
  251. }
  252. )
  253. )
  254. }
  255. //
  256. // SAVE AS SCAN
  257. //
  258. if metadata.isSavebleAsImage {
  259. actions.append(
  260. NCMenuAction(
  261. title: NSLocalizedString("_save_as_scan_", comment: ""),
  262. icon: utility.loadImage(named: "doc.viewfinder", colors: [NCBrandColor.shared.iconImageColor]),
  263. order: 110,
  264. action: { _ in
  265. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  266. NotificationCenter.default.post(
  267. name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile),
  268. object: nil,
  269. userInfo: ["ocId": metadata.ocId,
  270. "selector": NCGlobal.shared.selectorSaveAsScan,
  271. "error": NKError(),
  272. "account": metadata.account])
  273. } else {
  274. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  275. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  276. selector: NCGlobal.shared.selectorSaveAsScan,
  277. sceneIdentifier: sceneIdentifier) else { return }
  278. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  279. }
  280. }
  281. )
  282. )
  283. }
  284. //
  285. // RENAME
  286. //
  287. if metadata.isRenameable {
  288. actions.append(
  289. NCMenuAction(
  290. title: NSLocalizedString("_rename_", comment: ""),
  291. icon: utility.loadImage(named: "text.cursor", colors: [NCBrandColor.shared.iconImageColor]),
  292. order: 120,
  293. action: { _ in
  294. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  295. vcRename.metadata = metadata
  296. vcRename.imagePreview = imageIcon
  297. vcRename.indexPath = indexPath
  298. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  299. self.present(popup, animated: true)
  300. }
  301. }
  302. )
  303. )
  304. }
  305. //
  306. // COPY - MOVE
  307. //
  308. if metadata.isCopyableMovable {
  309. actions.append(.moveOrCopyAction(selectedMetadatas: [metadata], viewController: self, indexPath: [indexPath], order: 130))
  310. }
  311. //
  312. // MODIFY WITH QUICK LOOK
  313. //
  314. if metadata.isModifiableWithQuickLook {
  315. actions.append(
  316. NCMenuAction(
  317. title: NSLocalizedString("_modify_", comment: ""),
  318. icon: utility.loadImage(named: "pencil.tip.crop.circle", colors: [NCBrandColor.shared.iconImageColor]),
  319. order: 150,
  320. action: { _ in
  321. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  322. NotificationCenter.default.post(
  323. name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile),
  324. object: nil,
  325. userInfo: ["ocId": metadata.ocId,
  326. "selector": NCGlobal.shared.selectorLoadFileQuickLook,
  327. "error": NKError(),
  328. "account": metadata.account])
  329. } else {
  330. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  331. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  332. selector: NCGlobal.shared.selectorLoadFileQuickLook,
  333. sceneIdentifier: sceneIdentifier) else { return }
  334. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  335. }
  336. }
  337. )
  338. )
  339. }
  340. //
  341. // COLOR FOLDER
  342. //
  343. if self is NCFiles, metadata.directory {
  344. actions.append(
  345. NCMenuAction(
  346. title: NSLocalizedString("_change_color_", comment: ""),
  347. icon: utility.loadImage(named: "paintpalette", colors: [NCBrandColor.shared.iconImageColor]),
  348. order: 160,
  349. action: { _ in
  350. if let picker = UIStoryboard(name: "NCColorPicker", bundle: nil).instantiateInitialViewController() as? NCColorPicker {
  351. picker.metadata = metadata
  352. let popup = NCPopupViewController(contentController: picker, popupWidth: 200, popupHeight: 320)
  353. popup.backgroundAlpha = 0
  354. self.present(popup, animated: true)
  355. }
  356. }
  357. )
  358. )
  359. }
  360. //
  361. // DELETE
  362. //
  363. if metadata.isDeletable {
  364. actions.append(.deleteAction(selectedMetadatas: [metadata], indexPaths: [indexPath], metadataFolder: metadataFolder, viewController: self, order: 170))
  365. }
  366. applicationHandle.addCollectionViewCommonMenu(metadata: metadata, imageIcon: imageIcon, actions: &actions)
  367. presentMenu(with: actions)
  368. }
  369. }
  370. extension TimeInterval {
  371. func format() -> String? {
  372. let formatter = DateComponentsFormatter()
  373. formatter.allowedUnits = [.day, .hour, .minute]
  374. formatter.unitsStyle = .full
  375. formatter.maximumUnitCount = 1
  376. return formatter.string(from: self)
  377. }
  378. }