NCShareNetworking.swift 9.2 KB

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