NCShare.swift 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  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. tableView.dataSource = self
  60. tableView.delegate = self
  61. tableView.allowsSelection = false
  62. tableView.backgroundColor = .systemBackground
  63. tableView.register(UINib(nibName: "NCShareLinkCell", bundle: nil), forCellReuseIdentifier: "cellLink")
  64. tableView.register(UINib(nibName: "NCShareUserCell", bundle: nil), forCellReuseIdentifier: "cellUser")
  65. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataNCShare), object: nil)
  66. guard let metadata = metadata else { return }
  67. if metadata.e2eEncrypted {
  68. searchFieldTopConstraint.constant = -50
  69. searchField.isHidden = true
  70. } else {
  71. checkSharedWithYou()
  72. }
  73. reloadData()
  74. networking = NCShareNetworking(metadata: metadata, view: self.view, delegate: self)
  75. if sharingEnabled {
  76. let isVisible = (self.navigationController?.topViewController as? NCSharePaging)?.page == .sharing
  77. networking?.readShare(showLoadingIndicator: isVisible)
  78. }
  79. }
  80. func makeNewLinkShare() {
  81. guard
  82. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  83. let navigationController = self.navigationController,
  84. let metadata = self.metadata else { return }
  85. self.checkEnforcedPassword(shareType: NCShareCommon.shared.SHARE_TYPE_LINK) { password in
  86. advancePermission.networking = self.networking
  87. advancePermission.share = NCTableShareOptions.shareLink(metadata: metadata, password: password)
  88. advancePermission.metadata = self.metadata
  89. navigationController.pushViewController(advancePermission, animated: true)
  90. }
  91. }
  92. // Shared with you by ...
  93. func checkSharedWithYou() {
  94. guard let appDelegate = self.appDelegate, let metadata = metadata, !metadata.ownerId.isEmpty, metadata.ownerId != appDelegate.userId else { return }
  95. if !canReshare {
  96. searchField.isEnabled = false
  97. searchField.placeholder = NSLocalizedString("_share_reshare_disabled_", comment: "")
  98. }
  99. searchFieldTopConstraint.constant = 65
  100. sharedWithYouByView.isHidden = false
  101. sharedWithYouByLabel.text = NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata.ownerDisplayName
  102. sharedWithYouByImage.image = NCUtility.shared.loadUserImage(
  103. for: metadata.ownerId,
  104. displayName: metadata.ownerDisplayName,
  105. userBaseUrl: appDelegate)
  106. sharedWithYouByLabel.accessibilityHint = NSLocalizedString("_show_profile_", comment: "")
  107. let shareAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  108. sharedWithYouByImage.addGestureRecognizer(shareAction)
  109. let shareLabelAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  110. sharedWithYouByLabel.addGestureRecognizer(shareLabelAction)
  111. if !metadata.note.isEmpty {
  112. searchFieldTopConstraint.constant = 95
  113. sharedWithYouByNoteImage.isHidden = false
  114. sharedWithYouByNoteImage.image = NCUtility.shared.loadImage(named: "note.text", color: .gray)
  115. sharedWithYouByNote.isHidden = false
  116. sharedWithYouByNote.text = metadata.note
  117. sharedWithYouByNote.textColor = .label
  118. sharedWithYouByNote.trailingBuffer = sharedWithYouByNote.frame.width
  119. } else {
  120. sharedWithYouByNoteImage.isHidden = true
  121. sharedWithYouByNote.isHidden = true
  122. }
  123. let fileName = appDelegate.userBaseUrl + "-" + metadata.ownerId + ".png"
  124. if NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName) == nil {
  125. let fileNameLocalPath = String(CCUtility.getDirectoryUserData()) + "/" + fileName
  126. let etag = NCManageDatabase.shared.getTableAvatar(fileName: fileName)?.etag
  127. NextcloudKit.shared.downloadAvatar(
  128. user: metadata.ownerId,
  129. fileNameLocalPath: fileNameLocalPath,
  130. sizeImage: NCGlobal.shared.avatarSize,
  131. avatarSizeRounded: NCGlobal.shared.avatarSizeRounded,
  132. etag: etag) { _, imageAvatar, _, etag, error in
  133. if error == .success, let etag = etag, let imageAvatar = imageAvatar {
  134. NCManageDatabase.shared.addAvatar(fileName: fileName, etag: etag)
  135. self.sharedWithYouByImage.image = imageAvatar
  136. } else if error.errorCode == NCGlobal.shared.errorNotModified, let imageAvatar = NCManageDatabase.shared.setAvatarLoaded(fileName: fileName) {
  137. self.sharedWithYouByImage.image = imageAvatar
  138. }
  139. }
  140. }
  141. }
  142. // MARK: - Notification Center
  143. @objc func openShareProfile() {
  144. guard let metadata = metadata else { return }
  145. self.showProfileMenu(userId: metadata.ownerId)
  146. }
  147. // MARK: -
  148. @objc func reloadData() {
  149. if let metadata = metadata {
  150. shares = NCManageDatabase.shared.getTableShares(metadata: metadata)
  151. }
  152. tableView.reloadData()
  153. }
  154. // MARK: - IBAction
  155. @IBAction func searchFieldDidEndOnExit(textField: UITextField) {
  156. guard let searchString = textField.text, !searchString.isEmpty else { return }
  157. if searchString.contains("@"), !NCUtility.shared.isValidEmail(searchString) { return }
  158. networking?.getSharees(searchString: searchString)
  159. }
  160. func checkEnforcedPassword(shareType: Int, completion: @escaping (String?) -> Void) {
  161. guard NCGlobal.shared.capabilityFileSharingPubPasswdEnforced,
  162. shareType == NCShareCommon.shared.SHARE_TYPE_LINK || shareType == NCShareCommon.shared.SHARE_TYPE_EMAIL
  163. else { return completion(nil) }
  164. self.present(UIAlertController.password(titleKey: "_enforce_password_protection_", completion: completion), animated: true)
  165. }
  166. // MARK: - NCShareNetworkingDelegate
  167. func readShareCompleted() {
  168. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  169. }
  170. func shareCompleted() {
  171. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  172. }
  173. func unShareCompleted() {
  174. self.reloadData()
  175. }
  176. func updateShareWithError(idShare: Int) {
  177. self.reloadData()
  178. }
  179. func getSharees(sharees: [NKSharee]?) {
  180. guard let sharees = sharees, let appDelegate = appDelegate else { return }
  181. dropDown = DropDown()
  182. let appearance = DropDown.appearance()
  183. appearance.backgroundColor = .systemBackground
  184. appearance.cornerRadius = 10
  185. appearance.shadowColor = UIColor(white: 0.5, alpha: 1)
  186. appearance.shadowOpacity = 0.9
  187. appearance.shadowRadius = 25
  188. appearance.animationduration = 0.25
  189. appearance.textColor = .darkGray
  190. appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
  191. for sharee in sharees {
  192. var label = sharee.label
  193. if sharee.shareType == NCShareCommon.shared.SHARE_TYPE_CIRCLE {
  194. label += " (\(sharee.circleInfo), \(sharee.circleOwner))"
  195. }
  196. dropDown.dataSource.append(label)
  197. }
  198. dropDown.anchorView = searchField
  199. dropDown.bottomOffset = CGPoint(x: 0, y: searchField.bounds.height)
  200. dropDown.width = searchField.bounds.width
  201. dropDown.direction = .bottom
  202. dropDown.cellNib = UINib(nibName: "NCSearchUserDropDownCell", bundle: nil)
  203. dropDown.customCellConfiguration = { (index: Index, _, cell: DropDownCell) -> Void in
  204. guard let cell = cell as? NCSearchUserDropDownCell else { return }
  205. let sharee = sharees[index]
  206. cell.setupCell(sharee: sharee, baseUrl: appDelegate)
  207. }
  208. dropDown.selectionAction = { index, _ in
  209. let sharee = sharees[index]
  210. guard
  211. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  212. let navigationController = self.navigationController,
  213. let metadata = self.metadata else { return }
  214. self.checkEnforcedPassword(shareType: sharee.shareType) { password in
  215. let shareOptions = NCTableShareOptions(sharee: sharee, metadata: metadata, password: password)
  216. advancePermission.share = shareOptions
  217. advancePermission.networking = self.networking
  218. advancePermission.metadata = metadata
  219. navigationController.pushViewController(advancePermission, animated: true)
  220. }
  221. }
  222. dropDown.show()
  223. }
  224. }
  225. // MARK: - UITableViewDelegate
  226. extension NCShare: UITableViewDelegate {
  227. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  228. if indexPath.section == 0, indexPath.row == 0 {
  229. // internal cell has description
  230. return 90
  231. }
  232. return 70
  233. }
  234. }
  235. // MARK: - UITableViewDataSource
  236. extension NCShare: UITableViewDataSource {
  237. func numberOfSections(in tableView: UITableView) -> Int {
  238. return 2
  239. }
  240. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  241. guard let metadata = self.metadata else { return 0}
  242. var numRows = shares.share?.count ?? 0
  243. if section == 0 {
  244. if metadata.e2eEncrypted {
  245. numRows = 1
  246. } else {
  247. // don't allow link creation if reshare is disabled
  248. numRows = shares.firstShareLink != nil || canReshare ? 2 : 1
  249. }
  250. }
  251. return numRows
  252. }
  253. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  254. // Setup default share cells
  255. guard indexPath.section != 0 else {
  256. guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell, let metadata = self.metadata
  257. else { return UITableViewCell() }
  258. cell.delegate = self
  259. if metadata.e2eEncrypted {
  260. cell.tableShare = shares.firstShareLink
  261. } else {
  262. if indexPath.row == 0 {
  263. cell.isInternalLink = true
  264. } else if shares.firstShareLink?.isInvalidated != true {
  265. cell.tableShare = shares.firstShareLink
  266. }
  267. }
  268. cell.setupCellUI()
  269. return cell
  270. }
  271. guard let appDelegate = appDelegate, let tableShare = shares.share?[indexPath.row] else { return UITableViewCell() }
  272. // LINK
  273. if tableShare.shareType == NCShareCommon.shared.SHARE_TYPE_LINK {
  274. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell {
  275. cell.tableShare = tableShare
  276. cell.delegate = self
  277. cell.setupCellUI()
  278. return cell
  279. }
  280. } else {
  281. // USER / GROUP etc.
  282. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellUser", for: indexPath) as? NCShareUserCell {
  283. cell.tableShare = tableShare
  284. cell.delegate = self
  285. cell.setupCellUI(userId: appDelegate.userId)
  286. let fileName = appDelegate.userBaseUrl + "-" + tableShare.shareWith + ".png"
  287. NCOperationQueue.shared.downloadAvatar(user: tableShare.shareWith, dispalyName: tableShare.shareWithDisplayname, fileName: fileName, cell: cell, view: tableView, cellImageView: cell.fileAvatarImageView)
  288. return cell
  289. }
  290. }
  291. return UITableViewCell()
  292. }
  293. }