NCViewerQuickLook.swift 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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. var url: URL?
  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. @objc init(with url: URL, editingMode: Bool) {
  40. self.url = url
  41. self.editingMode = editingMode
  42. let previewItem = PreviewItem()
  43. previewItem.previewItemURL = url
  44. self.previewItems.append(previewItem)
  45. super.init(nibName: nil, bundle: nil)
  46. self.dataSource = self
  47. self.delegate = self
  48. self.currentPreviewItemIndex = 0
  49. self.navigationItem.leftBarButtonItem = UIBarButtonItem(barButtonSystemItem: .done, target: self, action: #selector(dismissPreviewController))
  50. }
  51. override func viewDidLoad() {
  52. super.viewDidLoad()
  53. }
  54. @objc func dismissPreviewController() {
  55. if editingMode {
  56. let alertController = UIAlertController(title: NSLocalizedString("_save_", comment: ""), message: "", preferredStyle: .alert)
  57. alertController.addAction(UIAlertAction(title: NSLocalizedString("_overwrite_original_", comment: ""), style: .default) { (action:UIAlertAction) in
  58. self.saveMode = .overwrite
  59. self.dismiss(animated: true)
  60. })
  61. alertController.addAction(UIAlertAction(title: NSLocalizedString("_save_as_copy_", comment: ""), style: .default) { (action:UIAlertAction) in
  62. self.saveMode = .copy
  63. self.dismiss(animated: true)
  64. })
  65. alertController.addAction(UIAlertAction(title: NSLocalizedString("_discard_changes_", comment: ""), style: .destructive) { (action:UIAlertAction) in
  66. self.saveMode = .discard
  67. self.dismiss(animated: true)
  68. })
  69. self.present(alertController, animated: true)
  70. } else {
  71. self.dismiss(animated: true)
  72. }
  73. }
  74. }
  75. extension NCViewerQuickLook: QLPreviewControllerDataSource, QLPreviewControllerDelegate {
  76. func numberOfPreviewItems(in controller: QLPreviewController) -> Int {
  77. previewItems.count
  78. }
  79. func previewController(_ controller: QLPreviewController, previewItemAt index: Int) -> QLPreviewItem {
  80. previewItems[index]
  81. }
  82. func previewController(_ controller: QLPreviewController, didUpdateContentsOf previewItem: QLPreviewItem) {
  83. }
  84. @available(iOS 13.0, *)
  85. func previewController(_ controller: QLPreviewController, editingModeFor previewItem: QLPreviewItem) -> QLPreviewItemEditingMode {
  86. if editingMode {
  87. return .createCopy
  88. } else {
  89. return .disabled
  90. }
  91. }
  92. func previewController(_ controller: QLPreviewController, didSaveEditedCopyOf previewItem: QLPreviewItem, at modifiedContentsURL: URL) {
  93. if saveMode == .copy {
  94. } else if saveMode == .overwrite {
  95. }
  96. }
  97. }
  98. extension URL {
  99. var hasHiddenExtension: Bool {
  100. get { (try? resourceValues(forKeys: [.hasHiddenExtensionKey]))?.hasHiddenExtension == true }
  101. set {
  102. var resourceValues = URLResourceValues()
  103. resourceValues.hasHiddenExtension = newValue
  104. try? setResourceValues(resourceValues)
  105. }
  106. }
  107. }
  108. import QuickLook
  109. class PreviewItem: NSObject, QLPreviewItem {
  110. var previewItemURL: URL?
  111. }