NCRichdocument.swift 6.0 KB

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