NCCollectionViewCommon+Menu.swift 16 KB

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