NCViewerRichdocument.swift 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. //
  2. // NCViewerRichdocument.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/09/18.
  6. // Copyright © 2018 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 Foundation
  24. class NCViewerRichdocument: NSObject, WKNavigationDelegate, WKScriptMessageHandler, NCSelectDelegate {
  25. @objc static let sharedInstance: NCViewerRichdocument = {
  26. let instance = NCViewerRichdocument()
  27. return instance
  28. }()
  29. var detail: CCDetail!
  30. var webView: WKWebView!
  31. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  32. @objc func viewRichDocumentAt(_ link: String, detail: CCDetail) {
  33. self.detail = detail
  34. let contentController = WKUserContentController()
  35. contentController.add(self, name: "RichDocumentsMobileInterface")
  36. let configuration = WKWebViewConfiguration()
  37. configuration.userContentController = contentController
  38. webView = WKWebView(frame: detail.view.bounds, configuration: configuration)
  39. webView.autoresizingMask = [.flexibleWidth, .flexibleHeight]
  40. // webView.scrollView.showsHorizontalScrollIndicator = true
  41. // webView.scrollView.showsVerticalScrollIndicator = true
  42. webView.navigationDelegate = self
  43. var request = URLRequest(url: URL(string: link)!)
  44. request.addValue("true", forHTTPHeaderField: "OCS-APIRequest")
  45. let language = NSLocale.preferredLanguages[0] as String
  46. request.addValue(language, forHTTPHeaderField: "Accept-Language")
  47. let userAgent : String = CCUtility.getUserAgent()
  48. webView.customUserAgent = userAgent
  49. webView.load(request)
  50. detail.view.addSubview(webView)
  51. }
  52. //MARK: -
  53. public func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
  54. if (message.name == "RichDocumentsMobileInterface") {
  55. if message.body as! String == "close" {
  56. self.webView.removeFromSuperview()
  57. self.detail.navigationController?.popToRootViewController(animated: true)
  58. self.detail.navigationController?.navigationBar.topItem?.title = ""
  59. }
  60. if message.body as! String == "insertGraphic" {
  61. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  62. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  63. let viewController = navigationController.topViewController as! NCSelect
  64. viewController.delegate = self
  65. viewController.hideButtonCreateFolder = true
  66. viewController.selectFile = true
  67. viewController.includeDirectoryE2EEncryption = false
  68. viewController.includeImages = true
  69. viewController.type = ""
  70. viewController.layoutViewSelect = k_layout_view_richdocument
  71. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  72. self.detail.present(navigationController, animated: true, completion: nil)
  73. }
  74. if message.body as! String == "share" {
  75. appDelegate.activeMain.openWindowShare(self.detail.metadataDetail)
  76. }
  77. }
  78. }
  79. //MARK: -
  80. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String) {
  81. if serverUrl != nil && metadata != nil {
  82. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  83. ocNetworking?.createAssetRichdocuments(withFileName: metadata!.fileName, serverUrl: serverUrl, success: { (url) in
  84. let functionJS = "OCA.RichDocuments.documentsMain.postAsset('\(metadata!.fileNameView)', '\(url!)')"
  85. self.webView.evaluateJavaScript(functionJS, completionHandler: { (result, error) in })
  86. }, failure: { (message, errorCode) in
  87. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  88. })
  89. }
  90. }
  91. func select(_ metadata: tableMetadata!, serverUrl: String!) {
  92. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  93. ocNetworking?.createAssetRichdocuments(withFileName: metadata.fileName, serverUrl: serverUrl, success: { (url) in
  94. let functionJS = "OCA.RichDocuments.documentsMain.postAsset('\(metadata.fileNameView)', '\(url!)')"
  95. self.webView.evaluateJavaScript(functionJS, completionHandler: { (result, error) in })
  96. }, failure: { (message, errorCode) in
  97. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  98. })
  99. }
  100. //MARK: -
  101. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  102. if let serverTrust = challenge.protectionSpace.serverTrust {
  103. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  104. } else {
  105. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  106. }
  107. }
  108. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  109. print("didStartProvisionalNavigation");
  110. }
  111. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  112. print("didReceiveServerRedirectForProvisionalNavigation");
  113. }
  114. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  115. print("didFinish");
  116. }
  117. }