NCShare.swift 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359
  1. //
  2. // NCShare.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 17/07/2019.
  6. // Copyright © 2019 Marino Faggiana. All rights reserved.
  7. // Copyright © 2022 Henrik Storch. All rights reserved.
  8. //
  9. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  10. // Author Henrik Storch <henrik.storch@nextcloud.com>
  11. //
  12. // This program is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. import UIKit
  26. import Parchment
  27. import DropDown
  28. import NextcloudKit
  29. import MarqueeLabel
  30. class NCShare: UIViewController, NCShareNetworkingDelegate, NCSharePagingContent {
  31. @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
  32. @IBOutlet weak var sharedWithYouByView: UIView!
  33. @IBOutlet weak var sharedWithYouByImage: UIImageView!
  34. @IBOutlet weak var sharedWithYouByLabel: UILabel!
  35. @IBOutlet weak var sharedWithYouByNoteImage: UIImageView!
  36. @IBOutlet weak var sharedWithYouByNote: MarqueeLabel!
  37. @IBOutlet weak var searchFieldTopConstraint: NSLayoutConstraint!
  38. @IBOutlet weak var searchField: UITextField!
  39. var textField: UITextField? { searchField }
  40. @IBOutlet weak var tableView: UITableView!
  41. weak var appDelegate = UIApplication.shared.delegate as? AppDelegate
  42. public var metadata: tableMetadata?
  43. public var sharingEnabled = true
  44. public var height: CGFloat = 0
  45. var canReshare: Bool {
  46. guard let metadata = metadata else { return true }
  47. return ((metadata.sharePermissionsCollaborationServices & NCGlobal.shared.permissionShareShare) != 0)
  48. }
  49. var shares: (firstShareLink: tableShare?, share: [tableShare]?) = (nil, nil)
  50. private var dropDown = DropDown()
  51. var networking: NCShareNetworking?
  52. // MARK: - View Life Cycle
  53. override func viewDidLoad() {
  54. super.viewDidLoad()
  55. view.backgroundColor = .systemBackground
  56. viewContainerConstraint.constant = height
  57. searchFieldTopConstraint.constant = 10
  58. searchField.placeholder = NSLocalizedString("_shareLinksearch_placeholder_", comment: "")
  59. searchField.autocorrectionType = .no
  60. tableView.dataSource = self
  61. tableView.delegate = self
  62. tableView.allowsSelection = false
  63. tableView.backgroundColor = .systemBackground
  64. tableView.register(UINib(nibName: "NCShareLinkCell", bundle: nil), forCellReuseIdentifier: "cellLink")
  65. tableView.register(UINib(nibName: "NCShareUserCell", bundle: nil), forCellReuseIdentifier: "cellUser")
  66. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataNCShare), object: nil)
  67. guard let metadata = metadata else { return }
  68. if metadata.e2eEncrypted && NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV12 {
  69. searchFieldTopConstraint.constant = -50
  70. searchField.isHidden = true
  71. } else {
  72. checkSharedWithYou()
  73. }
  74. reloadData()
  75. networking = NCShareNetworking(metadata: metadata, view: self.view, delegate: self)
  76. if sharingEnabled {
  77. let isVisible = (self.navigationController?.topViewController as? NCSharePaging)?.page == .sharing
  78. networking?.readShare(showLoadingIndicator: isVisible)
  79. }
  80. }
  81. func makeNewLinkShare() {
  82. guard
  83. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  84. let navigationController = self.navigationController,
  85. let metadata = self.metadata else { return }
  86. self.checkEnforcedPassword(shareType: NCShareCommon.shared.SHARE_TYPE_LINK) { password in
  87. advancePermission.networking = self.networking
  88. advancePermission.share = NCTableShareOptions.shareLink(metadata: metadata, password: password)
  89. advancePermission.metadata = self.metadata
  90. navigationController.pushViewController(advancePermission, animated: true)
  91. }
  92. }
  93. // Shared with you by ...
  94. func checkSharedWithYou() {
  95. guard let appDelegate = self.appDelegate, let metadata = metadata, !metadata.ownerId.isEmpty, metadata.ownerId != appDelegate.userId else { return }
  96. if !canReshare {
  97. searchField.isEnabled = false
  98. searchField.placeholder = NSLocalizedString("_share_reshare_disabled_", comment: "")
  99. }
  100. searchFieldTopConstraint.constant = 65
  101. sharedWithYouByView.isHidden = false
  102. sharedWithYouByLabel.text = NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata.ownerDisplayName
  103. sharedWithYouByImage.image = NCUtility.shared.loadUserImage(
  104. for: metadata.ownerId,
  105. displayName: metadata.ownerDisplayName,
  106. userBaseUrl: appDelegate)
  107. sharedWithYouByLabel.accessibilityHint = NSLocalizedString("_show_profile_", comment: "")
  108. let shareAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  109. sharedWithYouByImage.addGestureRecognizer(shareAction)
  110. let shareLabelAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  111. sharedWithYouByLabel.addGestureRecognizer(shareLabelAction)
  112. if !metadata.note.isEmpty {
  113. searchFieldTopConstraint.constant = 95
  114. sharedWithYouByNoteImage.isHidden = false
  115. sharedWithYouByNoteImage.image = NCUtility.shared.loadImage(named: "note.text", color: .gray)
  116. sharedWithYouByNote.isHidden = false
  117. sharedWithYouByNote.text = metadata.note
  118. sharedWithYouByNote.textColor = .label
  119. sharedWithYouByNote.trailingBuffer = sharedWithYouByNote.frame.width
  120. } else {
  121. sharedWithYouByNoteImage.isHidden = true
  122. sharedWithYouByNote.isHidden = true
  123. }
  124. let fileName = appDelegate.userBaseUrl + "-" + metadata.ownerId + ".png"
  125. if NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) == nil {
  126. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  127. let etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
  128. NextcloudKit.shared.downloadAvatar(
  129. user: metadata.ownerId,
  130. fileNameLocalPath: fileNameLocalPath,
  131. sizeImage: NCGlobal.shared.avatarSize,
  132. avatarSizeRounded: NCGlobal.shared.avatarSizeRounded,
  133. etag: etag) { _, imageAvatar, _, etag, error in
  134. if error == .success, let etag = etag, let imageAvatar = imageAvatar {
  135. NCManageDatabase.shared.addAvatar(fileName: fileName, etag: etag)
  136. self.sharedWithYouByImage.image = imageAvatar
  137. } else if error.errorCode == NCGlobal.shared.errorNotModified, let imageAvatar = NCManageDatabase.shared.setAvatarLoaded(fileName: fileName) {
  138. self.sharedWithYouByImage.image = imageAvatar
  139. }
  140. }
  141. }
  142. }
  143. // MARK: - Notification Center
  144. @objc func openShareProfile() {
  145. guard let metadata = metadata else { return }
  146. self.showProfileMenu(userId: metadata.ownerId)
  147. }
  148. // MARK: -
  149. @objc func reloadData() {
  150. if let metadata = metadata {
  151. shares = NCManageDatabase.shared.getTableShares(metadata: metadata)
  152. }
  153. tableView.reloadData()
  154. }
  155. // MARK: - IBAction
  156. @IBAction func searchFieldDidEndOnExit(textField: UITextField) {
  157. guard let searchString = textField.text, !searchString.isEmpty else { return }
  158. if searchString.contains("@"), !NCUtility.shared.isValidEmail(searchString) { return }
  159. networking?.getSharees(searchString: searchString)
  160. }
  161. func checkEnforcedPassword(shareType: Int, completion: @escaping (String?) -> Void) {
  162. guard NCGlobal.shared.capabilityFileSharingPubPasswdEnforced,
  163. shareType == NCShareCommon.shared.SHARE_TYPE_LINK || shareType == NCShareCommon.shared.SHARE_TYPE_EMAIL
  164. else { return completion(nil) }
  165. self.present(UIAlertController.password(titleKey: "_enforce_password_protection_", completion: completion), animated: true)
  166. }
  167. // MARK: - NCShareNetworkingDelegate
  168. func readShareCompleted() {
  169. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  170. }
  171. func shareCompleted() {
  172. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  173. }
  174. func unShareCompleted() {
  175. self.reloadData()
  176. }
  177. func updateShareWithError(idShare: Int) {
  178. self.reloadData()
  179. }
  180. func getSharees(sharees: [NKSharee]?) {
  181. guard let sharees = sharees, let appDelegate = appDelegate else { return }
  182. dropDown = DropDown()
  183. let appearance = DropDown.appearance()
  184. appearance.backgroundColor = .systemBackground
  185. appearance.cornerRadius = 10
  186. appearance.shadowColor = UIColor(white: 0.5, alpha: 1)
  187. appearance.shadowOpacity = 0.9
  188. appearance.shadowRadius = 25
  189. appearance.animationduration = 0.25
  190. appearance.textColor = .darkGray
  191. appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
  192. for sharee in sharees {
  193. var label = sharee.label
  194. if sharee.shareType == NCShareCommon.shared.SHARE_TYPE_CIRCLE {
  195. label += " (\(sharee.circleInfo), \(sharee.circleOwner))"
  196. }
  197. dropDown.dataSource.append(label)
  198. }
  199. dropDown.anchorView = searchField
  200. dropDown.bottomOffset = CGPoint(x: 0, y: searchField.bounds.height)
  201. dropDown.width = searchField.bounds.width
  202. dropDown.direction = .bottom
  203. dropDown.cellNib = UINib(nibName: "NCSearchUserDropDownCell", bundle: nil)
  204. dropDown.customCellConfiguration = { (index: Index, _, cell: DropDownCell) -> Void in
  205. guard let cell = cell as? NCSearchUserDropDownCell else { return }
  206. let sharee = sharees[index]
  207. cell.setupCell(sharee: sharee, baseUrl: appDelegate)
  208. }
  209. dropDown.selectionAction = { index, _ in
  210. let sharee = sharees[index]
  211. guard
  212. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  213. let navigationController = self.navigationController,
  214. let metadata = self.metadata else { return }
  215. self.checkEnforcedPassword(shareType: sharee.shareType) { password in
  216. let shareOptions = NCTableShareOptions(sharee: sharee, metadata: metadata, password: password)
  217. advancePermission.share = shareOptions
  218. advancePermission.networking = self.networking
  219. advancePermission.metadata = metadata
  220. navigationController.pushViewController(advancePermission, animated: true)
  221. }
  222. }
  223. dropDown.show()
  224. }
  225. }
  226. // MARK: - UITableViewDelegate
  227. extension NCShare: UITableViewDelegate {
  228. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  229. if indexPath.section == 0, indexPath.row == 0 {
  230. // internal cell has description
  231. return 90
  232. }
  233. return 70
  234. }
  235. }
  236. // MARK: - UITableViewDataSource
  237. extension NCShare: UITableViewDataSource {
  238. func numberOfSections(in tableView: UITableView) -> Int {
  239. return 2
  240. }
  241. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  242. guard let metadata = self.metadata else { return 0}
  243. var numRows = shares.share?.count ?? 0
  244. if section == 0 {
  245. if metadata.e2eEncrypted && NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV12 {
  246. numRows = 1
  247. } else {
  248. // don't allow link creation if reshare is disabled
  249. numRows = shares.firstShareLink != nil || canReshare ? 2 : 1
  250. }
  251. }
  252. return numRows
  253. }
  254. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  255. // Setup default share cells
  256. guard indexPath.section != 0 else {
  257. guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell, let metadata = self.metadata
  258. else { return UITableViewCell() }
  259. cell.delegate = self
  260. if metadata.e2eEncrypted && NCGlobal.shared.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV12 {
  261. cell.tableShare = shares.firstShareLink
  262. } else {
  263. if indexPath.row == 0 {
  264. cell.isInternalLink = true
  265. } else if shares.firstShareLink?.isInvalidated != true {
  266. cell.tableShare = shares.firstShareLink
  267. }
  268. }
  269. cell.setupCellUI()
  270. return cell
  271. }
  272. guard let appDelegate = appDelegate, let tableShare = shares.share?[indexPath.row] else { return UITableViewCell() }
  273. // LINK
  274. if tableShare.shareType == NCShareCommon.shared.SHARE_TYPE_LINK {
  275. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell {
  276. cell.indexPath = indexPath
  277. cell.tableShare = tableShare
  278. cell.delegate = self
  279. cell.setupCellUI()
  280. return cell
  281. }
  282. } else {
  283. // USER / GROUP etc.
  284. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellUser", for: indexPath) as? NCShareUserCell {
  285. cell.indexPath = indexPath
  286. cell.tableShare = tableShare
  287. cell.delegate = self
  288. cell.setupCellUI(userId: appDelegate.userId)
  289. let fileName = appDelegate.userBaseUrl + "-" + tableShare.shareWith + ".png"
  290. NCOperationQueue.shared.downloadAvatar(user: tableShare.shareWith, dispalyName: tableShare.shareWithDisplayname, fileName: fileName, cell: cell, view: tableView, cellImageView: cell.fileAvatarImageView)
  291. return cell
  292. }
  293. }
  294. return UITableViewCell()
  295. }
  296. }