NCShareNetworking.swift 9.2 KB

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