NCShareNetworking.swift 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, activeUrl: self.activeUrl, items: itemsOCSharedDto)
  43. } else {
  44. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  45. }
  46. self.delegate?.readShareCompleted()
  47. })
  48. }
  49. func share(password: String, permission: Int, hideDownload: Bool) {
  50. NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
  51. let fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
  52. OCNetworking.sharedManager()?.share(withAccount: metadata.account, fileName: fileName, password: password, permission: permission, hideDownload: hideDownload, completion: { (account, message, errorCode) in
  53. if errorCode == 0 {
  54. 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
  55. NCUtility.sharedInstance.stopActivityIndicator()
  56. if errorCode == 0 {
  57. let itemsOCSharedDto = items as! [OCSharedDto]
  58. NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, activeUrl: self.activeUrl, items: itemsOCSharedDto)
  59. } else {
  60. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  61. }
  62. self.delegate?.shareCompleted()
  63. })
  64. } else {
  65. NCUtility.sharedInstance.stopActivityIndicator()
  66. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  67. }
  68. })
  69. }
  70. func unShare(idRemoteShared: Int) {
  71. NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
  72. OCNetworking.sharedManager()?.unshareAccount(metadata.account, shareID: idRemoteShared, completion: { (account, message, errorCode) in
  73. NCUtility.sharedInstance.stopActivityIndicator()
  74. if errorCode == 0 {
  75. NCManageDatabase.sharedInstance.deleteTableShare(account: account!, idRemoteShared: idRemoteShared)
  76. self.delegate?.unShareCompleted()
  77. } else {
  78. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  79. }
  80. })
  81. }
  82. func updateShare(idRemoteShared: Int, password: String?, permission: Int, note: String?, expirationTime: String?, hideDownload: Bool) {
  83. NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
  84. OCNetworking.sharedManager()?.shareUpdateAccount(metadata.account, shareID: idRemoteShared, password: password, note:note, permission: permission, expirationTime: expirationTime, hideDownload: hideDownload, completion: { (account, message, errorCode) in
  85. NCUtility.sharedInstance.stopActivityIndicator()
  86. if errorCode == 0 {
  87. self.readShare()
  88. } else {
  89. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  90. self.delegate?.updateShareWithError(idRemoteShared: idRemoteShared)
  91. }
  92. })
  93. }
  94. func getUserAndGroup(searchString: String) {
  95. NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
  96. OCNetworking.sharedManager()?.getUserGroup(withAccount: metadata.account, search: searchString, completion: { (account, items, message, errorCode) in
  97. NCUtility.sharedInstance.stopActivityIndicator()
  98. if errorCode == 0 {
  99. let itemsOCShareUser = items as! [OCShareUser]
  100. self.delegate?.getUserAndGroup(items: itemsOCShareUser)
  101. } else {
  102. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  103. self.delegate?.getUserAndGroup(items: nil)
  104. }
  105. })
  106. }
  107. func shareUserAndGroup(name: String, shareeType: Int, metadata: tableMetadata) {
  108. NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
  109. let fileName = CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: activeUrl)!
  110. var permission: Int = 0
  111. if metadata.directory { permission = Int(k_max_folder_share_permission) } else { permission = Int(k_max_file_share_permission) }
  112. OCNetworking.sharedManager()?.shareUserGroup(withAccount: metadata.account, userOrGroup: name, fileName: fileName, permission: permission, shareeType: shareeType, completion: { (account, message, errorCode) in
  113. if errorCode == 0 {
  114. OCNetworking.sharedManager()?.readShare(withAccount: account, path: CCUtility.returnFileNamePath(fromFileName: metadata.fileName, serverUrl: metadata.serverUrl, activeUrl: self.activeUrl), completion: { (account, items, message, errorCode) in
  115. NCUtility.sharedInstance.stopActivityIndicator()
  116. if errorCode == 0 {
  117. let itemsOCSharedDto = items as! [OCSharedDto]
  118. NCManageDatabase.sharedInstance.addShare(account: self.metadata.account, activeUrl: self.activeUrl, items: itemsOCSharedDto)
  119. } else {
  120. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  121. }
  122. self.delegate?.shareCompleted()
  123. })
  124. } else {
  125. NCUtility.sharedInstance.stopActivityIndicator()
  126. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  127. }
  128. })
  129. }
  130. }
  131. protocol NCShareNetworkingDelegate {
  132. func readShareCompleted()
  133. func shareCompleted()
  134. func unShareCompleted()
  135. func updateShareWithError(idRemoteShared: Int)
  136. func getUserAndGroup(items: [OCShareUser]?)
  137. }