NCShareNetworking.swift 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. //
  2. // NCShareNetworking.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/07/2019.
  6. // Copyright © 2019 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. import Foundation
  23. import NCCommunication
  24. class NCShareNetworking: NSObject {
  25. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  26. var activeUrl: String
  27. var delegate: NCShareNetworkingDelegate?
  28. var view: UIView
  29. var metadata: tableMetadata
  30. init(metadata: tableMetadata, activeUrl: String, view: UIView, delegate: NCShareNetworkingDelegate?) {
  31. self.metadata = metadata
  32. self.activeUrl = activeUrl
  33. self.view = view
  34. self.delegate = delegate
  35. super.init()
  36. }
  37. func readShare() {
  38. NCUtility.sharedInstance.startActivityIndicator(view: view)
  39. NCCommunication.shared.readShares(path: CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)) { (account, shares, errorCode, errorDescription) in
  40. NCUtility.sharedInstance.stopActivityIndicator()
  41. if errorCode == 0 && shares != nil {
  42. self.appDelegate.shares = NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, activeUrl: self.activeUrl, shares: shares!)
  43. self.appDelegate.activeMain?.tableView?.reloadData()
  44. self.appDelegate.activeFavorites?.tableView?.reloadData()
  45. } else {
  46. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: 0)
  47. }
  48. self.delegate?.readShareCompleted()
  49. }
  50. }
  51. func share(password: String, permission: Int, hideDownload: Bool) {
  52. NCUtility.sharedInstance.startActivityIndicator(view: view)
  53. let fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
  54. NCCommunication.shared.createShare(path: fileName, shareType: 0, shareWith: "", password: password, permissions: permission) { (account, sharee, errorCode, errorDescription) in
  55. }
  56. /*
  57. OCNetworking.sharedManager()?.share(withAccount: metadata.account, fileName: fileName, password: password, permission: permission, hideDownload: hideDownload, completion: { (account, message, errorCode) in
  58. if errorCode == 0 {
  59. OCNetworking.sharedManager()?.readShare(withAccount: account, path: CCUtility.returnFileNamePath(fromFileName: self.metadata.fileName, serverUrl: self.metadata.serverUrl, activeUrl: self.activeUrl), completion: { (account, items, message, errorCode) in
  60. NCUtility.sharedInstance.stopActivityIndicator()
  61. if errorCode == 0 {
  62. let itemsOCSharedDto = items as! [OCSharedDto]
  63. self.appDelegate.shares = NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, activeUrl: self.activeUrl, items: itemsOCSharedDto)
  64. self.appDelegate.activeMain?.tableView?.reloadData()
  65. self.appDelegate.activeFavorites?.tableView?.reloadData()
  66. } else {
  67. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: 0)
  68. }
  69. self.delegate?.shareCompleted()
  70. })
  71. } else {
  72. NCUtility.sharedInstance.stopActivityIndicator()
  73. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: 0)
  74. }
  75. })
  76. */
  77. }
  78. func unShare(idShare: Int) {
  79. NCUtility.sharedInstance.startActivityIndicator(view: view)
  80. NCCommunication.shared.deleteShare(idShare: idShare) { (account, errorCode, errorDescription) in
  81. NCUtility.sharedInstance.stopActivityIndicator()
  82. if errorCode == 0 {
  83. NCManageDatabase.sharedInstance.deleteTableShare(account: account, idShare: idShare)
  84. self.delegate?.unShareCompleted()
  85. } else {
  86. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: 0)
  87. }
  88. }
  89. }
  90. func updateShare(idShare: Int, password: String?, permission: Int, note: String?, expirationTime: String?, hideDownload: Bool) {
  91. NCUtility.sharedInstance.startActivityIndicator(view: view)
  92. /*
  93. OCNetworking.sharedManager()?.shareUpdateAccount(metadata.account, shareID: idRemoteShared, password: password, note:note, permission: permission, expirationTime: expirationTime, hideDownload: hideDownload, completion: { (account, message, errorCode) in
  94. NCUtility.sharedInstance.stopActivityIndicator()
  95. if errorCode == 0 {
  96. self.readShare()
  97. } else {
  98. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: 0)
  99. self.delegate?.updateShareWithError(idShare: idShare)
  100. }
  101. })
  102. */
  103. }
  104. func getUserAndGroup(searchString: String) {
  105. NCUtility.sharedInstance.startActivityIndicator(view: view)
  106. NCCommunication.shared.searchSharees(search: searchString) { (account, sharees, errorCode, errorDescription) in
  107. NCUtility.sharedInstance.stopActivityIndicator()
  108. if errorCode == 0 {
  109. self.delegate?.getUserAndGroup(sharees: sharees)
  110. } else {
  111. NCContentPresenter.shared.messageNotification("_share_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: 0)
  112. self.delegate?.getUserAndGroup(sharees: nil)
  113. }
  114. }
  115. }
  116. func shareUserAndGroup(name: String, shareeType: Int, metadata: tableMetadata) {
  117. NCUtility.sharedInstance.startActivityIndicator(view: view)
  118. let fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
  119. var permission: Int = 0
  120. if metadata.directory { permission = Int(k_max_folder_share_permission) } else { permission = Int(k_max_file_share_permission) }
  121. /*
  122. OCNetworking.sharedManager()?.shareUserGroup(withAccount: metadata.account, userOrGroup: name, fileName: fileName, permission: permission, shareeType: shareeType, completion: { (account, message, errorCode) in
  123. if errorCode == 0 {
  124. OCNetworking.sharedManager()?.readShare(withAccount: account, path: CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: self.activeUrl), completion: { (account, items, message, errorCode) in
  125. NCUtility.sharedInstance.stopActivityIndicator()
  126. if errorCode == 0 {
  127. let itemsOCSharedDto = items as! [OCSharedDto]
  128. self.appDelegate.shares = NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, activeUrl: self.activeUrl, items: itemsOCSharedDto)
  129. self.appDelegate.activeMain?.tableView?.reloadData()
  130. self.appDelegate.activeFavorites?.tableView?.reloadData()
  131. } else {
  132. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: 0)
  133. }
  134. self.delegate?.shareCompleted()
  135. })
  136. } else {
  137. NCUtility.sharedInstance.stopActivityIndicator()
  138. NCContentPresenter.shared.messageNotification("_share_", description: message, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: 0)
  139. }
  140. })
  141. */
  142. }
  143. }
  144. protocol NCShareNetworkingDelegate {
  145. func readShareCompleted()
  146. func shareCompleted()
  147. func unShareCompleted()
  148. func updateShareWithError(idShare: Int)
  149. func getUserAndGroup(sharees: [NCCommunicationSharee]?)
  150. }