NCViewerQuickLook.swift 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. //
  2. // NCViewerQuickLook.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 03/05/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 QuickLook
  25. import NCCommunication
  26. @objc class NCViewerQuickLook: QLPreviewController {
  27. let url: URL
  28. var previewItems: [PreviewItem] = []
  29. var isEditingEnabled: Bool
  30. var metadata: tableMetadata?
  31. // if the document has any changes (annotations)
  32. var hasChanges = false
  33. // used to display the save alert
  34. var parentVC: UIViewController?
  35. required init?(coder: NSCoder) {
  36. fatalError("init(coder:) has not been implemented")
  37. }
  38. @objc init(with url: URL, isEditingEnabled: Bool, metadata: tableMetadata?) {
  39. self.url = url
  40. self.isEditingEnabled = isEditingEnabled
  41. if let metadata = metadata {
  42. self.metadata = tableMetadata.init(value: metadata)
  43. }
  44. let previewItem = PreviewItem()
  45. previewItem.previewItemURL = url
  46. self.previewItems.append(previewItem)
  47. super.init(nibName: nil, bundle: nil)
  48. self.dataSource = self
  49. self.delegate = self
  50. self.currentPreviewItemIndex = 0
  51. }
  52. override func viewDidLoad() {
  53. super.viewDidLoad()
  54. guard isEditingEnabled else { return }
  55. if metadata?.classFile == NCCommunicationCommon.typeClassFile.image.rawValue {
  56. Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true, block: { t in
  57. if self.navigationItem.rightBarButtonItems?.count ?? 0 > 1 || !(self.navigationController?.isToolbarHidden ?? false) {
  58. if #available(iOS 14.0, *) {
  59. if self.navigationItem.rightBarButtonItems?.count ?? 0 > 1 {
  60. if let buttonItem = self.navigationItem.rightBarButtonItems?.last {
  61. _ = buttonItem.target?.perform(buttonItem.action, with: buttonItem)
  62. }
  63. } else {
  64. if let buttonItem = self.navigationItem.rightBarButtonItems?.first {
  65. _ = buttonItem.target?.perform(buttonItem.action, with: buttonItem)
  66. }
  67. }
  68. } else {
  69. if let buttonItem = self.navigationItem.rightBarButtonItems?.filter({$0.customView != nil}).first?.customView as? UIButton {
  70. buttonItem.sendActions(for: .touchUpInside)
  71. }
  72. }
  73. t.invalidate()
  74. }
  75. })
  76. }
  77. if metadata?.livePhoto == true {
  78. NCContentPresenter.shared.messageNotification(
  79. "", description: "_message_disable_overwrite_livephoto_",
  80. delay: NCGlobal.shared.dismissAfterSecond,
  81. type: NCContentPresenter.messageType.info,
  82. errorCode: NCGlobal.shared.errorCharactersForbidden)
  83. }
  84. }
  85. override func viewDidAppear(_ animated: Bool) {
  86. super.viewDidAppear(animated)
  87. self.parentVC = presentingViewController
  88. }
  89. override func viewDidDisappear(_ animated: Bool) {
  90. // called after `previewController(:didSaveEditedCopyOf:)`
  91. super.viewDidDisappear(animated)
  92. guard isEditingEnabled, hasChanges else { return }
  93. let alertController = UIAlertController(title: NSLocalizedString("_save_", comment: ""), message: "", preferredStyle: .alert)
  94. if metadata?.livePhoto == false {
  95. alertController.addAction(UIAlertAction(title: NSLocalizedString("_overwrite_original_", comment: ""), style: .default) { _ in
  96. self.saveModifiedFile(override: true)
  97. })
  98. }
  99. alertController.addAction(UIAlertAction(title: NSLocalizedString("_save_as_copy_", comment: ""), style: .default) { _ in
  100. self.saveModifiedFile(override: false)
  101. })
  102. alertController.addAction(UIAlertAction(title: NSLocalizedString("_discard_changes_", comment: ""), style: .destructive) { _ in })
  103. parentVC?.present(alertController, animated: true)
  104. }
  105. }
  106. extension NCViewerQuickLook: QLPreviewControllerDataSource, QLPreviewControllerDelegate {
  107. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  108. previewItems.count
  109. }
  110. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  111. previewItems[index]
  112. }
  113. @available(iOS 13.0, *)
  114. func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
  115. return isEditingEnabled ? .createCopy : .disabled
  116. }
  117. fileprivate func saveModifiedFile(override: Bool) {
  118. guard let metadata = self.metadata else { return }
  119. let ocId = NSUUID().uuidString
  120. let size = NCUtilityFileSystem.shared.getFileSize(filePath: url.path)
  121. if !override {
  122. let fileName = NCUtilityFileSystem.shared.createFileName(metadata.fileNameView, serverUrl: metadata.serverUrl, account: metadata.account)
  123. metadata.fileName = fileName
  124. metadata.fileNameView = fileName
  125. }
  126. guard let fileNamePath = CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: metadata.fileNameView),
  127. NCUtilityFileSystem.shared.copyFile(atPath: url.path, toPath: fileNamePath) else { return }
  128. let metadataForUpload = NCManageDatabase.shared.createMetadata(
  129. account: metadata.account,
  130. user: metadata.user,
  131. userId: metadata.userId,
  132. fileName: metadata.fileName,
  133. fileNameView: metadata.fileNameView,
  134. ocId: ocId,
  135. serverUrl: metadata.serverUrl,
  136. urlBase: metadata.urlBase,
  137. url: url.path,
  138. contentType: "",
  139. livePhoto: false)
  140. metadataForUpload.session = NCNetworking.shared.sessionIdentifierBackground
  141. metadataForUpload.sessionSelector = NCGlobal.shared.selectorUploadFile
  142. metadataForUpload.size = size
  143. metadataForUpload.status = NCGlobal.shared.metadataStatusWaitUpload
  144. (UIApplication.shared.delegate as? AppDelegate)?.networkingProcessUpload?.createProcessUploads(metadatas: [metadataForUpload])
  145. }
  146. func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
  147. // easier to handle that way than to use `.updateContents`
  148. // needs to be moved otherwise it will only be called once!
  149. guard NCUtilityFileSystem.shared.moveFile(atPath: modifiedContentsURL.path, toPath: url.path) else { return }
  150. hasChanges = true
  151. }
  152. }
  153. class PreviewItem: NSObject, QLPreviewItem {
  154. var previewItemURL: URL?
  155. }