NCViewer+Menu.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. //
  2. // NCViewer.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 07/02/2020.
  6. // Copyright © 2020 Marino Faggiana All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import UIKit
  24. import FloatingPanel
  25. import NextcloudKit
  26. extension NCViewer {
  27. func toggleMenu(viewController: UIViewController, metadata: tableMetadata, webView: Bool, imageIcon: UIImage?, indexPath: IndexPath = IndexPath()) {
  28. guard let metadata = NCManageDatabase.shared.getMetadataFromOcId(metadata.ocId) else { return }
  29. var actions = [NCMenuAction]()
  30. let localFile = NCManageDatabase.shared.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  31. let isOffline = localFile?.offline == true
  32. //
  33. // DETAIL
  34. //
  35. if !appDelegate.disableSharesView {
  36. actions.append(
  37. NCMenuAction(
  38. title: NSLocalizedString("_details_", comment: ""),
  39. icon: utility.loadImage(named: "info.circle"),
  40. action: { _ in
  41. NCActionCenter.shared.openShare(viewController: viewController, metadata: metadata, page: .activity)
  42. }
  43. )
  44. )
  45. }
  46. //
  47. // VIEW IN FOLDER
  48. //
  49. if !webView {
  50. actions.append(
  51. NCMenuAction(
  52. title: NSLocalizedString("_view_in_folder_", comment: ""),
  53. icon: utility.loadImage(named: "questionmark.folder"),
  54. action: { _ in
  55. NCActionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil)
  56. }
  57. )
  58. )
  59. }
  60. //
  61. // FAVORITE
  62. // Workaround: PROPPATCH doesn't work
  63. // https://github.com/nextcloud/files_lock/issues/68
  64. if !metadata.lock {
  65. actions.append(
  66. NCMenuAction(
  67. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  68. icon: utility.loadImage(named: metadata.favorite ? "star.slash.fill" : "star.fill", color: NCBrandColor.shared.yellowFavorite),
  69. action: { _ in
  70. NCNetworking.shared.favoriteMetadata(metadata) { error in
  71. if error != .success {
  72. NCContentPresenter().showError(error: error)
  73. }
  74. }
  75. }
  76. )
  77. )
  78. }
  79. //
  80. // OFFLINE
  81. //
  82. if !webView, metadata.canSetAsAvailableOffline {
  83. actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: viewController))
  84. }
  85. //
  86. // SHARE
  87. //
  88. if !webView, metadata.canOpenIn {
  89. actions.append(.share(selectedMetadatas: [metadata], viewController: viewController))
  90. }
  91. //
  92. // SAVE LIVE PHOTO
  93. //
  94. if let metadataMOV = NCManageDatabase.shared.getMetadataLivePhoto(metadata: metadata) {
  95. actions.append(
  96. NCMenuAction(
  97. title: NSLocalizedString("_livephoto_save_", comment: ""),
  98. icon: NCUtility().loadImage(named: "livephoto"),
  99. action: { _ in
  100. NCNetworking.shared.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV))
  101. }
  102. )
  103. )
  104. }
  105. //
  106. // SAVE AS SCAN
  107. //
  108. if !webView, metadata.isSavebleAsImage {
  109. actions.append(
  110. NCMenuAction(
  111. title: NSLocalizedString("_save_as_scan_", comment: ""),
  112. icon: utility.loadImage(named: "doc.viewfinder"),
  113. action: { _ in
  114. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  115. NotificationCenter.default.post(
  116. name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile),
  117. object: nil,
  118. userInfo: ["ocId": metadata.ocId,
  119. "selector": NCGlobal.shared.selectorSaveAsScan,
  120. "error": NKError(),
  121. "account": metadata.account])
  122. } else {
  123. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  124. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  125. selector: NCGlobal.shared.selectorSaveAsScan) else { return }
  126. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  127. }
  128. }
  129. )
  130. )
  131. }
  132. //
  133. // RENAME
  134. //
  135. if !webView, metadata.isRenameable {
  136. actions.append(
  137. NCMenuAction(
  138. title: NSLocalizedString("_rename_", comment: ""),
  139. icon: utility.loadImage(named: "text.cursor"),
  140. action: { _ in
  141. if let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile {
  142. vcRename.metadata = metadata
  143. vcRename.disableChangeExt = true
  144. vcRename.imagePreview = imageIcon
  145. vcRename.indexPath = indexPath
  146. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  147. viewController.present(popup, animated: true)
  148. }
  149. }
  150. )
  151. )
  152. }
  153. //
  154. // COPY - MOVE
  155. //
  156. if !webView, metadata.isCopyableMovable {
  157. actions.append(.moveOrCopyAction(selectedMetadatas: [metadata], indexPath: []))
  158. }
  159. //
  160. // DOWNLOAD FULL RESOLUTION
  161. //
  162. if !webView, metadata.session.isEmpty, !self.utilityFileSystem.fileProviderStorageExists(metadata) {
  163. actions.append(
  164. NCMenuAction(
  165. title: NSLocalizedString("_try_download_full_resolution_", comment: ""),
  166. icon: utility.loadImage(named: "photo"),
  167. action: { _ in
  168. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  169. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  170. selector: "") else { return }
  171. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  172. }
  173. )
  174. )
  175. }
  176. //
  177. // PDF
  178. //
  179. if metadata.contentType == "com.adobe.pdf" || metadata.contentType == "application/pdf" {
  180. actions.append(
  181. NCMenuAction(
  182. title: NSLocalizedString("_search_", comment: ""),
  183. icon: UIImage(named: "search")!.image(color: UIColor.systemGray, size: 50),
  184. action: { _ in
  185. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuSearchTextPDF)
  186. }
  187. )
  188. )
  189. actions.append(
  190. NCMenuAction(
  191. title: NSLocalizedString("_go_to_page_", comment: ""),
  192. icon: utility.loadImage(named: "repeat"),
  193. action: { _ in
  194. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuGotToPageInPDF)
  195. }
  196. )
  197. )
  198. }
  199. //
  200. // MODIFY WITH QUICK LOOK
  201. //
  202. if !webView, metadata.isModifiableWithQuickLook {
  203. actions.append(
  204. NCMenuAction(
  205. title: NSLocalizedString("_modify_", comment: ""),
  206. icon: utility.loadImage(named: "pencil.tip.crop.circle"),
  207. action: { _ in
  208. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  209. NotificationCenter.default.post(
  210. name: Notification.Name(rawValue: NCGlobal.shared.notificationCenterDownloadedFile),
  211. object: nil,
  212. userInfo: ["ocId": metadata.ocId,
  213. "selector": NCGlobal.shared.selectorLoadFileQuickLook,
  214. "error": NKError(),
  215. "account": metadata.account])
  216. } else {
  217. guard let metadata = NCManageDatabase.shared.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  218. session: NextcloudKit.shared.nkCommonInstance.sessionIdentifierDownload,
  219. selector: NCGlobal.shared.selectorLoadFileQuickLook) else { return }
  220. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  221. }
  222. }
  223. )
  224. )
  225. }
  226. //
  227. // DELETE
  228. //
  229. if !webView, metadata.isDeletable {
  230. actions.append(.deleteAction(selectedMetadatas: [metadata], indexPath: [], metadataFolder: nil, viewController: viewController))
  231. }
  232. viewController.presentMenu(with: actions)
  233. }
  234. }