NCShareNetworking.swift 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 account: String
  26. var activeUrl: String
  27. var delegate: NCShareNetworkingDelegate?
  28. var view: UIView?
  29. init(account: String, activeUrl: String, view: UIView?, delegate: NCShareNetworkingDelegate?) {
  30. self.account = account
  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: account, 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.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(metadata: tableMetadata, 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, 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.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(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(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: 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, permission: 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. OCNetworking.sharedManager()?.shareUserGroup(withAccount: account, userOrGroup: name, fileName: fileName, permission: permission, shareeType: shareeType, completion: { (account, message, errorCode) in
  111. if errorCode == 0 {
  112. OCNetworking.sharedManager()?.readShare(withAccount: account, completion: { (account, items, message, errorCode) in
  113. NCUtility.sharedInstance.stopActivityIndicator()
  114. if errorCode == 0 {
  115. let itemsOCSharedDto = items as! [OCSharedDto]
  116. NCManageDatabase.sharedInstance.addShare(account: self.account, activeUrl: self.activeUrl, items: itemsOCSharedDto)
  117. } else {
  118. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  119. }
  120. self.delegate?.shareCompleted()
  121. })
  122. } else {
  123. NCUtility.sharedInstance.stopActivityIndicator()
  124. self.appDelegate.messageNotification("_share_", description: message, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  125. }
  126. })
  127. }
  128. }
  129. protocol NCShareNetworkingDelegate {
  130. func readShareCompleted()
  131. func shareCompleted()
  132. func unShareCompleted()
  133. func updateShareWithError(idRemoteShared: Int)
  134. func getUserAndGroup(items: [OCShareUser]?)
  135. }