NCViewer+Menu.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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(controller: NCMainTabBarController?, metadata: tableMetadata, webView: Bool, imageIcon: UIImage?, indexPath: IndexPath = IndexPath()) {
  28. guard let metadata = self.database.getMetadataFromOcId(metadata.ocId),
  29. let controller else { return }
  30. var actions = [NCMenuAction]()
  31. let localFile = self.database.getTableLocalFile(predicate: NSPredicate(format: "ocId == %@", metadata.ocId))
  32. let isOffline = localFile?.offline == true
  33. //
  34. // DETAIL
  35. //
  36. if !NCCapabilities.shared.disableSharesView(account: metadata.account) {
  37. actions.append(
  38. NCMenuAction(
  39. title: NSLocalizedString("_details_", comment: ""),
  40. icon: utility.loadImage(named: "info.circle", colors: [NCBrandColor.shared.iconImageColor]),
  41. action: { _ in
  42. NCActionCenter.shared.openShare(viewController: controller, metadata: metadata, page: .activity)
  43. }
  44. )
  45. )
  46. }
  47. //
  48. // VIEW IN FOLDER
  49. //
  50. if !webView {
  51. actions.append(
  52. NCMenuAction(
  53. title: NSLocalizedString("_view_in_folder_", comment: ""),
  54. icon: utility.loadImage(named: "questionmark.folder", colors: [NCBrandColor.shared.iconImageColor]),
  55. action: { _ in
  56. NCActionCenter.shared.openFileViewInFolder(serverUrl: metadata.serverUrl, fileNameBlink: metadata.fileName, fileNameOpen: nil, sceneIdentifier: controller.sceneIdentifier)
  57. }
  58. )
  59. )
  60. }
  61. //
  62. // FAVORITE
  63. // Workaround: PROPPATCH doesn't work
  64. // https://github.com/nextcloud/files_lock/issues/68
  65. if !metadata.lock {
  66. actions.append(
  67. NCMenuAction(
  68. title: metadata.favorite ? NSLocalizedString("_remove_favorites_", comment: "") : NSLocalizedString("_add_favorites_", comment: ""),
  69. icon: utility.loadImage(named: metadata.favorite ? "star.slash" : "star", colors: [NCBrandColor.shared.yellowFavorite]),
  70. action: { _ in
  71. NCNetworking.shared.favoriteMetadata(metadata) { error in
  72. if error != .success {
  73. NCContentPresenter().showError(error: error)
  74. }
  75. }
  76. }
  77. )
  78. )
  79. }
  80. //
  81. // OFFLINE
  82. //
  83. if !webView, metadata.canSetAsAvailableOffline {
  84. actions.append(.setAvailableOfflineAction(selectedMetadatas: [metadata], isAnyOffline: isOffline, viewController: controller))
  85. }
  86. //
  87. // SHARE
  88. //
  89. if !webView, metadata.canShare {
  90. actions.append(.share(selectedMetadatas: [metadata], controller: controller))
  91. }
  92. //
  93. // SAVE LIVE PHOTO
  94. //
  95. if let metadataMOV = self.database.getMetadataLivePhoto(metadata: metadata),
  96. let hudView = controller.view {
  97. actions.append(
  98. NCMenuAction(
  99. title: NSLocalizedString("_livephoto_save_", comment: ""),
  100. icon: NCUtility().loadImage(named: "livephoto", colors: [NCBrandColor.shared.iconImageColor]),
  101. action: { _ in
  102. NCNetworking.shared.saveLivePhotoQueue.addOperation(NCOperationSaveLivePhoto(metadata: metadata, metadataMOV: metadataMOV, hudView: hudView))
  103. }
  104. )
  105. )
  106. }
  107. //
  108. // SAVE AS SCAN
  109. //
  110. if !webView, metadata.isSavebleAsImage {
  111. actions.append(
  112. NCMenuAction(
  113. title: NSLocalizedString("_save_as_scan_", comment: ""),
  114. icon: utility.loadImage(named: "doc.viewfinder", colors: [NCBrandColor.shared.iconImageColor]),
  115. action: { _ in
  116. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  117. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile,
  118. object: nil,
  119. userInfo: ["ocId": metadata.ocId,
  120. "ocIdTransfer": metadata.ocIdTransfer,
  121. "session": metadata.session,
  122. "selector": NCGlobal.shared.selectorSaveAsScan,
  123. "error": NKError(),
  124. "account": metadata.account],
  125. second: 0.5)
  126. } else {
  127. guard let metadata = self.database.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  128. session: NCNetworking.shared.sessionDownload,
  129. selector: NCGlobal.shared.selectorSaveAsScan,
  130. sceneIdentifier: controller.sceneIdentifier) else { return }
  131. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  132. }
  133. }
  134. )
  135. )
  136. }
  137. //
  138. // DOWNLOAD FULL RESOLUTION
  139. //
  140. if !webView, metadata.session.isEmpty, !self.utilityFileSystem.fileProviderStorageExists(metadata) {
  141. actions.append(
  142. NCMenuAction(
  143. title: NSLocalizedString("_try_download_full_resolution_", comment: ""),
  144. icon: utility.loadImage(named: "photo", colors: [NCBrandColor.shared.iconImageColor]),
  145. action: { _ in
  146. guard let metadata = self.database.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  147. session: NCNetworking.shared.sessionDownload,
  148. selector: "",
  149. sceneIdentifier: controller.sceneIdentifier) else { return }
  150. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  151. }
  152. )
  153. )
  154. }
  155. //
  156. // PDF
  157. //
  158. if metadata.isPDF {
  159. actions.append(
  160. NCMenuAction(
  161. title: NSLocalizedString("_search_", comment: ""),
  162. icon: utility.loadImage(named: "magnifyingglass", colors: [NCBrandColor.shared.iconImageColor]),
  163. action: { _ in
  164. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuSearchTextPDF)
  165. }
  166. )
  167. )
  168. actions.append(
  169. NCMenuAction(
  170. title: NSLocalizedString("_go_to_page_", comment: ""),
  171. icon: utility.loadImage(named: "number.circle", colors: [NCBrandColor.shared.iconImageColor]),
  172. action: { _ in
  173. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterMenuGotToPageInPDF)
  174. }
  175. )
  176. )
  177. }
  178. //
  179. // MODIFY WITH QUICK LOOK
  180. //
  181. if !webView, metadata.isModifiableWithQuickLook {
  182. actions.append(
  183. NCMenuAction(
  184. title: NSLocalizedString("_modify_", comment: ""),
  185. icon: utility.loadImage(named: "pencil.tip.crop.circle", colors: [NCBrandColor.shared.iconImageColor]),
  186. action: { _ in
  187. if self.utilityFileSystem.fileProviderStorageExists(metadata) {
  188. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterDownloadedFile,
  189. object: nil,
  190. userInfo: ["ocId": metadata.ocId,
  191. "ocIdTransfer": metadata.ocIdTransfer,
  192. "session": metadata.session,
  193. "selector": NCGlobal.shared.selectorLoadFileQuickLook,
  194. "error": NKError(),
  195. "account": metadata.account],
  196. second: 0.5)
  197. } else {
  198. guard let metadata = self.database.setMetadatasSessionInWaitDownload(metadatas: [metadata],
  199. session: NCNetworking.shared.sessionDownload,
  200. selector: NCGlobal.shared.selectorLoadFileQuickLook,
  201. sceneIdentifier: controller.sceneIdentifier) else { return }
  202. NCNetworking.shared.download(metadata: metadata, withNotificationProgressTask: true)
  203. }
  204. }
  205. )
  206. )
  207. }
  208. //
  209. // DELETE
  210. //
  211. if !webView, metadata.isDeletable {
  212. actions.append(.deleteAction(selectedMetadatas: [metadata], metadataFolder: nil, controller: controller))
  213. }
  214. controller.presentMenu(with: actions)
  215. }
  216. }