NCRichdocument.swift 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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.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. viewDetail.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.viewDetail.navigationController?.popToRootViewController(animated: true)
  58. self.viewDetail.navigationController?.navigationBar.topItem?.title = ""
  59. }
  60. if message.body as! String == "insertGraphic" {
  61. let storyboard = UIStoryboard(name: "CCMove", bundle: nil)
  62. let movieNavigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  63. let moveViewController = movieNavigationController.topViewController as! CCMove
  64. moveViewController.delegate = self
  65. moveViewController.hideMoveutton = true
  66. moveViewController.hideCreateFolder = true
  67. moveViewController.tintColor = NCBrandColor.sharedInstance.brandText
  68. moveViewController.barTintColor = NCBrandColor.sharedInstance.brand
  69. moveViewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  70. moveViewController.networkingOperationQueue = appDelegate.netQueue
  71. moveViewController.includeImages = true
  72. moveViewController.includeDirectoryE2EEncryption = false
  73. moveViewController.selectFile = true
  74. movieNavigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  75. self.viewDetail.present(movieNavigationController, animated: true, completion: nil)
  76. }
  77. if message.body as! String == "share" {
  78. appDelegate.activeMain.openWindowShare(viewDetail.metadataDetail)
  79. }
  80. }
  81. }
  82. //MARK: -
  83. func select(_ metadata: tableMetadata!, serverUrl: String!) {
  84. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  85. ocNetworking?.createAssetRichdocuments(withFileName: metadata.fileName, serverUrl: serverUrl, success: { (url) in
  86. let functionJS = "OCA.RichDocuments.documentsMain.postAsset('\(metadata.fileNameView)', '\(url!)')"
  87. self.webView.evaluateJavaScript(functionJS, completionHandler: { (result, error) in })
  88. }, failure: { (message, errorCode) in
  89. self.appDelegate.messageNotification("_error_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  90. })
  91. }
  92. //MARK: -
  93. public func webView(_ webView: WKWebView, didReceive challenge: URLAuthenticationChallenge, completionHandler: @escaping (URLSession.AuthChallengeDisposition, URLCredential?) -> Void) {
  94. if let serverTrust = challenge.protectionSpace.serverTrust {
  95. completionHandler(Foundation.URLSession.AuthChallengeDisposition.useCredential, URLCredential(trust: serverTrust))
  96. } else {
  97. completionHandler(URLSession.AuthChallengeDisposition.useCredential, nil);
  98. }
  99. }
  100. public func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) {
  101. print("didStartProvisionalNavigation");
  102. }
  103. public func webView(_ webView: WKWebView, didReceiveServerRedirectForProvisionalNavigation navigation: WKNavigation!) {
  104. print("didReceiveServerRedirectForProvisionalNavigation");
  105. }
  106. public func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
  107. print("didFinish");
  108. }
  109. }