NCViewerQuickLook.swift 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. @objc class NCViewerQuickLook: QLPreviewController {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. let previewController = QLPreviewController()
  28. var previewItems: [PreviewItem] = []
  29. var editingMode: Bool
  30. enum saveModeType{
  31. case overwrite
  32. case copy
  33. case discard
  34. }
  35. var saveMode: saveModeType = .discard
  36. required init?(coder: NSCoder) {
  37. fatalError("init(coder:) has not been implemented")
  38. }
  39. init(with url: URL, editingMode: Bool) {
  40. self.editingMode = editingMode
  41. super.init(nibName: nil, bundle: nil)
  42. self.dataSource = self
  43. self.delegate = self
  44. self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissPreviewController))
  45. URLSession.shared.dataTask(with: url) { data, response, error in
  46. guard let _ = data, error == nil else {
  47. self.presentAlertController(with: error?.localizedDescription ?? "Failed to look the file")
  48. return
  49. }
  50. var previewURL = url
  51. previewURL.hasHiddenExtension = true
  52. let previewItem = PreviewItem()
  53. previewItem.previewItemURL = previewURL
  54. self.previewItems.append(previewItem)
  55. self.currentPreviewItemIndex = 0
  56. }.resume()
  57. }
  58. override func viewDidLoad() {
  59. super.viewDidLoad()
  60. }
  61. func presentAlertController(with message: String) {
  62. // present your alert controller from the main thread
  63. DispatchQueue.main.async {
  64. UIApplication.shared.isNetworkActivityIndicatorVisible = false
  65. let alert = UIAlertController(title: "Alert", message: message, preferredStyle: .alert)
  66. alert.addAction(.init(title: "OK", style: .default))
  67. self.appDelegate.window?.rootViewController?.present(alert, animated: true)
  68. }
  69. }
  70. @objc func dismissPreviewController() {
  71. if editingMode {
  72. let alertController = UIAlertController(title: NSLocalizedString("_save_", comment: ""), message: "", preferredStyle: .alert)
  73. alertController.addAction(UIAlertAction(title: NSLocalizedString("_overwrite_original_", comment: ""), style: .default) { (action:UIAlertAction) in
  74. self.saveMode = .overwrite
  75. self.dismiss(animated: true)
  76. })
  77. alertController.addAction(UIAlertAction(title: NSLocalizedString("_save_as_copy_", comment: ""), style: .default) { (action:UIAlertAction) in
  78. self.saveMode = .copy
  79. self.dismiss(animated: true)
  80. })
  81. alertController.addAction(UIAlertAction(title: NSLocalizedString("_discard_changes_", comment: ""), style: .destructive) { (action:UIAlertAction) in
  82. self.saveMode = .discard
  83. self.dismiss(animated: true)
  84. })
  85. self.present(alertController, animated: true)
  86. } else {
  87. self.dismiss(animated: true)
  88. }
  89. }
  90. }
  91. extension NCViewerQuickLook: QLPreviewControllerDataSource, QLPreviewControllerDelegate {
  92. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  93. previewItems.count
  94. }
  95. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  96. previewItems[index]
  97. }
  98. func previewController(_ controller: QLPreviewController, didUpdateContentsOf previewItem: QLPreviewItem) {
  99. }
  100. @available(iOS 13.0, *)
  101. func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
  102. if editingMode {
  103. return .createCopy
  104. } else {
  105. return .disabled
  106. }
  107. }
  108. func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
  109. }
  110. }
  111. extension URL {
  112. var hasHiddenExtension: Bool {
  113. get { (try? resourceValues(forKeys: [.hasHiddenExtensionKey]))?.hasHiddenExtension == true }
  114. set {
  115. var resourceValues = URLResourceValues()
  116. resourceValues.hasHiddenExtension = newValue
  117. try? setResourceValues(resourceValues)
  118. }
  119. }
  120. }
  121. import QuickLook
  122. class PreviewItem: NSObject, QLPreviewItem {
  123. var previewItemURL: URL?
  124. }