NCCollectionViewCommon+Menu.swift 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406
  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 serverUrlHome = NCUtilityFileSystem.shared.getHomeServer(urlBase: appDelegate.urlBase, userId: appDelegate.userId)
  37. let isOffline: Bool
  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. } else { isOffline = false }
  43. let editors = NCUtility.shared.isDirectEditing(account: metadata.account, contentType: metadata.contentType)
  44. let isRichDocument = NCUtility.shared.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 = 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. order: 0,
  61. action: nil
  62. )
  63. )
  64. //
  65. // DETAILS
  66. //
  67. if !appDelegate.disableSharesView {
  68. actions.append(
  69. NCMenuAction(
  70. title: NSLocalizedString("_details_", comment: ""),
  71. icon: NCUtility.shared.loadImage(named: "info"),
  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 = NCUtility.shared.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: NCUtility.shared.loadImage(named: "questionmark.folder"),
  117. order: 21,
  118. action: { menuAction in
  119. NCActionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil)
  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 (ONLY ROOT)
  132. //
  133. if metadata.serverUrl == serverUrlHome, metadata.isDirectoySettableE2EE {
  134. actions.append(
  135. NCMenuAction(
  136. title: NSLocalizedString("_e2e_set_folder_encrypted_", comment: ""),
  137. icon: NCUtility.shared.loadImage(named: "lock"),
  138. order: 30,
  139. action: { _ in
  140. NextcloudKit.shared.markE2EEFolder(fileId: metadata.fileId, delete: false) { account, error in
  141. if error == .success {
  142. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl))
  143. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: true, richWorkspace: nil, account: metadata.account)
  144. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: true)
  145. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl": metadata.serverUrl])
  146. } else {
  147. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_mark_folder_", comment: ""), error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
  148. }
  149. }
  150. }
  151. )
  152. )
  153. }
  154. //
  155. // UNSET FOLDER E2EE
  156. //
  157. if metadata.isDirectoryUnsettableE2EE {
  158. actions.append(
  159. NCMenuAction(
  160. title: NSLocalizedString("_e2e_remove_folder_encrypted_", comment: ""),
  161. icon: NCUtility.shared.loadImage(named: "lock"),
  162. order: 30,
  163. action: { _ in
  164. NextcloudKit.shared.markE2EEFolder(fileId: metadata.fileId, delete: true) { account, error in
  165. if error == .success {
  166. NCManageDatabase.shared.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", self.appDelegate.account, serverUrl))
  167. NCManageDatabase.shared.setDirectory(serverUrl: serverUrl, serverUrlTo: nil, etag: nil, ocId: nil, fileId: nil, encrypted: false, richWorkspace: nil, account: metadata.account)
  168. NCManageDatabase.shared.setMetadataEncrypted(ocId: metadata.ocId, encrypted: false)
  169. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeStatusFolderE2EE, userInfo: ["serverUrl": metadata.serverUrl])
  170. } else {
  171. NCContentPresenter.shared.messageNotification(NSLocalizedString("_e2e_error_delete_mark_folder_", comment: ""), error: error, delay: NCGlobal.shared.dismissAfterSecond, type: .error)
  172. }
  173. }
  174. }
  175. )
  176. )
  177. }
  178. actions.append(.seperator(order: 40))
  179. //
  180. // FAVORITE
  181. // FIXME: PROPPATCH doesn't work
  182. // https://github.com/nextcloud/files_lock/issues/68
  183. if !metadata.lock {
  184. actions.append(
  185. NCMenuAction(
  186. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  187. icon: NCUtility.shared.loadImage(named: "star.fill", color: NCBrandColor.shared.yellowFavorite),
  188. order: 50,
  189. action: { _ in
  190. NCNetworking.shared.favoriteMetadata(metadata) { error in
  191. if error != .success {
  192. NCContentPresenter.shared.showError(error: error)
  193. }
  194. }
  195. }
  196. )
  197. )
  198. }
  199. //
  200. // OFFLINE
  201. //
  202. if metadata.isSettableOnOffline {
  203. actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: self, order: 60, completion: {
  204. self.reloadDataSource()
  205. }))
  206. }
  207. //
  208. // OPEN with external editor
  209. //
  210. if metadata.canOpenExternalEditor {
  211. var editor = ""
  212. var title = ""
  213. var icon: UIImage?
  214. if editors.contains(NCGlobal.shared.editorOnlyoffice) {
  215. editor = NCGlobal.shared.editorOnlyoffice
  216. title = NSLocalizedString("_open_in_onlyoffice_", comment: "")
  217. icon = NCUtility.shared.loadImage(named: "onlyoffice")
  218. } else if isRichDocument {
  219. editor = NCGlobal.shared.editorCollabora
  220. title = NSLocalizedString("_open_in_collabora_", comment: "")
  221. icon = NCUtility.shared.loadImage(named: "collabora")
  222. }
  223. if editor != "" {
  224. actions.append(
  225. NCMenuAction(
  226. title: title,
  227. icon: icon!,
  228. order: 70,
  229. action: { _ in
  230. NCViewer.shared.view(viewController: self, metadata: metadata, metadatas: [metadata], imageIcon: imageIcon, editor: editor, isRichDocument: isRichDocument)
  231. }
  232. )
  233. )
  234. }
  235. }
  236. //
  237. // OPEN IN
  238. //
  239. if metadata.canOpenIn {
  240. actions.append(.openInAction(selectedMetadatas: [metadata], viewController: self, order: 80))
  241. }
  242. //
  243. // PRINT
  244. //
  245. if metadata.isPrintable {
  246. actions.append(.printAction(metadata: metadata, order: 90))
  247. }
  248. //
  249. // SAVE CAMERA ROLL
  250. //
  251. if metadata.isSavebleInCameraRoll {
  252. actions.append(.saveMediaAction(selectedMediaMetadatas: [metadata], order: 100))
  253. }
  254. //
  255. // SAVE AS SCAN
  256. //
  257. if metadata.isSavebleAsImage {
  258. actions.append(
  259. NCMenuAction(
  260. title: NSLocalizedString("_save_as_scan_", comment: ""),
  261. icon: NCUtility.shared.loadImage(named: "viewfinder.circle"),
  262. order: 110,
  263. action: { _ in
  264. if CCUtility.fileProviderStorageExists(metadata) {
  265. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorSaveAsScan, "error": NKError(), "account": metadata.account])
  266. } else {
  267. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorSaveAsScan) { _, _ in }
  268. }
  269. }
  270. )
  271. )
  272. }
  273. //
  274. // RENAME
  275. //
  276. if metadata.isRenameable {
  277. actions.append(
  278. NCMenuAction(
  279. title: NSLocalizedString("_rename_", comment: ""),
  280. icon: NCUtility.shared.loadImage(named: "pencil"),
  281. order: 120,
  282. action: { _ in
  283. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  284. vcRename.metadata = metadata
  285. vcRename.imagePreview = imageIcon
  286. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  287. self.present(popup, animated: true)
  288. }
  289. }
  290. )
  291. )
  292. }
  293. //
  294. // COPY - MOVE
  295. //
  296. if metadata.isCopyableMovable {
  297. actions.append(.moveOrCopyAction(selectedMetadatas: [metadata], order: 130))
  298. }
  299. //
  300. // COPY IN PASTEBOARD
  301. //
  302. if metadata.isCopyableInPasteboard {
  303. actions.append(.copyAction(selectOcId: [metadata.ocId], hudView: self.view, order: 140))
  304. }
  305. //
  306. // MODIFY WITH QUICK LOOK
  307. //
  308. if metadata.isModifiableWithQuickLook {
  309. actions.append(
  310. NCMenuAction(
  311. title: NSLocalizedString("_modify_", comment: ""),
  312. icon: NCUtility.shared.loadImage(named: "pencil.tip.crop.circle"),
  313. order: 150,
  314. action: { menuAction in
  315. if CCUtility.fileProviderStorageExists(metadata) {
  316. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile, userInfo: ["ocId": metadata.ocId, "selector": NCGlobal.shared.selectorLoadFileQuickLook, "error": NKError(), "account": metadata.account])
  317. } else {
  318. NCNetworking.shared.download(metadata: metadata, selector: NCGlobal.shared.selectorLoadFileQuickLook) { _, _ in }
  319. }
  320. }
  321. )
  322. )
  323. }
  324. //
  325. // COLOR FOLDER
  326. //
  327. if self is NCFiles, metadata.directory {
  328. actions.append(
  329. NCMenuAction(
  330. title: NSLocalizedString("_change_color_", comment: ""),
  331. icon: NCUtility.shared.loadImage(named: "palette"),
  332. order: 160,
  333. action: { _ in
  334. if let picker = UIStoryboard(name: "NCColorPicker", bundle: nil).instantiateInitialViewController() as? NCColorPicker {
  335. picker.metadata = metadata
  336. let popup = NCPopupViewController(contentController: picker, popupWidth: 200, popupHeight: 320)
  337. popup.backgroundAlpha = 0
  338. self.present(popup, animated: true)
  339. }
  340. }
  341. )
  342. )
  343. }
  344. //
  345. // DELETE
  346. //
  347. if metadata.isDeletable {
  348. actions.append(.deleteAction(selectedMetadatas: [metadata], metadataFolder: metadataFolder, viewController: self, order: 170))
  349. }
  350. applicationHandle.addCollectionViewCommonMenu(metadata: metadata, imageIcon: imageIcon, actions: &actions)
  351. presentMenu(with: actions)
  352. }
  353. }
  354. extension TimeInterval {
  355. func format() -> String? {
  356. let formatter = DateComponentsFormatter()
  357. formatter.allowedUnits = [.day, .hour, .minute]
  358. formatter.unitsStyle = .full
  359. formatter.maximumUnitCount = 1
  360. return formatter.string(from: self)
  361. }
  362. }