NCShare.swift 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. import ContactsUI
  31. class NCShare: UIViewController, NCShareNetworkingDelegate, NCSharePagingContent {
  32. @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var sharedWithYouByView: UIView!
  34. @IBOutlet weak var sharedWithYouByImage: UIImageView!
  35. @IBOutlet weak var sharedWithYouByLabel: UILabel!
  36. @IBOutlet weak var searchFieldTopConstraint: NSLayoutConstraint!
  37. @IBOutlet weak var searchField: UISearchBar!
  38. var textField: UIView? { searchField }
  39. @IBOutlet weak var tableView: UITableView!
  40. @IBOutlet weak var btnContact: UIButton!
  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. let shareCommon = NCShareCommon()
  46. let utilityFileSystem = NCUtilityFileSystem()
  47. let utility = NCUtility()
  48. let database = NCManageDatabase.shared
  49. var canReshare: Bool {
  50. return ((metadata.sharePermissionsCollaborationServices & NCPermissions().permissionShareShare) != 0)
  51. }
  52. var session: NCSession.Session {
  53. NCSession.shared.getSession(account: metadata.account)
  54. }
  55. var shares: (firstShareLink: tableShare?, share: [tableShare]?) = (nil, nil)
  56. private var dropDown = DropDown()
  57. var networking: NCShareNetworking?
  58. // MARK: - View Life Cycle
  59. override func viewDidLoad() {
  60. super.viewDidLoad()
  61. view.backgroundColor = .systemBackground
  62. viewContainerConstraint.constant = height
  63. searchFieldTopConstraint.constant = 0
  64. searchField.placeholder = NSLocalizedString("_shareLinksearch_placeholder_", comment: "")
  65. searchField.autocorrectionType = .no
  66. tableView.dataSource = self
  67. tableView.delegate = self
  68. tableView.allowsSelection = false
  69. tableView.backgroundColor = .systemBackground
  70. tableView.contentInset = UIEdgeInsets(top: 8, left: 0, bottom: 10, right: 0)
  71. tableView.register(UINib(nibName: "NCShareLinkCell", bundle: nil), forCellReuseIdentifier: "cellLink")
  72. tableView.register(UINib(nibName: "NCShareUserCell", bundle: nil), forCellReuseIdentifier: "cellUser")
  73. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: NSNotification.Name(rawValue: NCGlobal.shared.notificationCenterReloadDataNCShare), object: nil)
  74. if metadata.e2eEncrypted {
  75. let direcrory = self.database.getTableDirectory(account: metadata.account, serverUrl: metadata.serverUrl)
  76. let capabilities = NCCapabilities.shared.getCapabilities(account: metadata.account)
  77. if capabilities.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV12 ||
  78. (capabilities.capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV20 && direcrory?.e2eEncrypted ?? false) {
  79. searchFieldTopConstraint.constant = -50
  80. searchField.alpha = 0
  81. btnContact.alpha = 0
  82. }
  83. } else {
  84. checkSharedWithYou()
  85. }
  86. reloadData()
  87. networking = NCShareNetworking(metadata: metadata, view: self.view, delegate: self, session: session)
  88. if sharingEnabled {
  89. let isVisible = (self.navigationController?.topViewController as? NCSharePaging)?.page == .sharing
  90. networking?.readShare(showLoadingIndicator: isVisible)
  91. }
  92. searchField.searchTextField.font = .systemFont(ofSize: 14)
  93. searchField.delegate = self
  94. }
  95. func makeNewLinkShare() {
  96. guard
  97. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  98. let navigationController = self.navigationController else { return }
  99. self.checkEnforcedPassword(shareType: shareCommon.SHARE_TYPE_LINK) { password in
  100. advancePermission.networking = self.networking
  101. advancePermission.share = NCTableShareOptions.shareLink(metadata: self.metadata, password: password)
  102. advancePermission.metadata = self.metadata
  103. navigationController.pushViewController(advancePermission, animated: true)
  104. }
  105. }
  106. // Shared with you by ...
  107. func checkSharedWithYou() {
  108. guard !metadata.ownerId.isEmpty, metadata.ownerId != session.userId else { return }
  109. if !canReshare {
  110. searchField.isUserInteractionEnabled = false
  111. searchField.alpha = 0.5
  112. searchField.placeholder = NSLocalizedString("_share_reshare_disabled_", comment: "")
  113. }
  114. searchFieldTopConstraint.constant = 45
  115. sharedWithYouByView.isHidden = false
  116. sharedWithYouByLabel.text = NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata.ownerDisplayName
  117. sharedWithYouByImage.image = utility.loadUserImage(for: metadata.ownerId, displayName: metadata.ownerDisplayName, urlBase: session.urlBase)
  118. sharedWithYouByLabel.accessibilityHint = NSLocalizedString("_show_profile_", comment: "")
  119. let shareAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  120. sharedWithYouByImage.addGestureRecognizer(shareAction)
  121. let shareLabelAction = UITapGestureRecognizer(target: self, action: #selector(openShareProfile))
  122. sharedWithYouByLabel.addGestureRecognizer(shareLabelAction)
  123. let fileName = NCSession.shared.getFileName(urlBase: session.urlBase, user: metadata.ownerId)
  124. let results = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName)
  125. if results.image == nil {
  126. let etag = self.database.getTableAvatar(fileName: fileName)?.etag
  127. NextcloudKit.shared.downloadAvatar(
  128. user: metadata.ownerId,
  129. fileNameLocalPath: utilityFileSystem.directoryUserData + "/" + fileName,
  130. sizeImage: NCGlobal.shared.avatarSize,
  131. avatarSizeRounded: NCGlobal.shared.avatarSizeRounded,
  132. etag: etag,
  133. account: metadata.account) { _, imageAvatar, _, etag, _, error in
  134. if error == .success, let etag = etag, let imageAvatar = imageAvatar {
  135. self.database.addAvatar(fileName: fileName, etag: etag)
  136. self.sharedWithYouByImage.image = imageAvatar
  137. } else if error.errorCode == NCGlobal.shared.errorNotModified, let imageAvatar = self.database.setAvatarLoaded(fileName: fileName) {
  138. self.sharedWithYouByImage.image = imageAvatar
  139. }
  140. }
  141. }
  142. }
  143. // MARK: - Notification Center
  144. @objc func openShareProfile() {
  145. self.showProfileMenu(userId: metadata.ownerId, session: session)
  146. }
  147. // MARK: -
  148. @objc func reloadData() {
  149. shares = self.database.getTableShares(metadata: metadata)
  150. tableView.reloadData()
  151. }
  152. // MARK: - IBAction
  153. @IBAction func searchFieldDidEndOnExit(textField: UITextField) {
  154. // https://stackoverflow.com/questions/25471114/how-to-validate-an-e-mail-address-in-swift
  155. func isValidEmail(_ email: String) -> Bool {
  156. let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
  157. let emailPred = NSPredicate(format: "SELF MATCHES %@", emailRegEx)
  158. return emailPred.evaluate(with: email)
  159. }
  160. guard let searchString = textField.text, !searchString.isEmpty else { return }
  161. if searchString.contains("@"), !isValidEmail(searchString) { return }
  162. networking?.getSharees(searchString: searchString)
  163. }
  164. func checkEnforcedPassword(shareType: Int, completion: @escaping (String?) -> Void) {
  165. guard NCCapabilities.shared.getCapabilities(account: session.account).capabilityFileSharingPubPasswdEnforced,
  166. shareType == shareCommon.SHARE_TYPE_LINK || shareType == shareCommon.SHARE_TYPE_EMAIL
  167. else { return completion(nil) }
  168. self.present(UIAlertController.password(titleKey: "_enforce_password_protection_", completion: completion), animated: true)
  169. }
  170. @IBAction func selectContactClicked(_ sender: Any) {
  171. let cnPicker = CNContactPickerViewController()
  172. cnPicker.delegate = self
  173. cnPicker.displayedPropertyKeys = [CNContactEmailAddressesKey]
  174. cnPicker.predicateForEnablingContact = NSPredicate(format: "emailAddresses.@count > 0")
  175. cnPicker.predicateForSelectionOfProperty = NSPredicate(format: "emailAddresses.@count > 0")
  176. self.present(cnPicker, animated: true)
  177. }
  178. // MARK: - NCShareNetworkingDelegate
  179. func readShareCompleted() {
  180. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  181. }
  182. func shareCompleted() {
  183. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterReloadDataNCShare)
  184. }
  185. func unShareCompleted() {
  186. self.reloadData()
  187. }
  188. func updateShareWithError(idShare: Int) {
  189. self.reloadData()
  190. }
  191. func getSharees(sharees: [NKSharee]?) {
  192. guard let sharees else { return }
  193. dropDown = DropDown()
  194. let appearance = DropDown.appearance()
  195. // Setting up the blur effect
  196. let blurEffect = UIBlurEffect(style: .light) // You can choose .dark, .extraLight, or .light
  197. let blurEffectView = UIVisualEffectView(effect: blurEffect)
  198. blurEffectView.frame = CGRect(x: 0, y: 0, width: 500, height: 20)
  199. appearance.backgroundColor = .systemBackground
  200. appearance.cornerRadius = 10
  201. appearance.shadowColor = .black
  202. appearance.shadowOpacity = 0.2
  203. appearance.shadowRadius = 30
  204. appearance.animationduration = 0.25
  205. appearance.textColor = .darkGray
  206. for sharee in sharees {
  207. var label = sharee.label
  208. if sharee.shareType == shareCommon.SHARE_TYPE_CIRCLE {
  209. label += " (\(sharee.circleInfo), \(sharee.circleOwner))"
  210. }
  211. dropDown.dataSource.append(label)
  212. }
  213. dropDown.anchorView = searchField
  214. dropDown.bottomOffset = CGPoint(x: 10, y: searchField.bounds.height)
  215. dropDown.width = searchField.bounds.width - 20
  216. dropDown.direction = .bottom
  217. dropDown.cellNib = UINib(nibName: "NCSearchUserDropDownCell", bundle: nil)
  218. dropDown.customCellConfiguration = { (index: Index, _, cell: DropDownCell) -> Void in
  219. guard let cell = cell as? NCSearchUserDropDownCell else { return }
  220. let sharee = sharees[index]
  221. cell.setupCell(sharee: sharee, session: self.session)
  222. }
  223. dropDown.selectionAction = { index, _ in
  224. let sharee = sharees[index]
  225. guard
  226. let advancePermission = UIStoryboard(name: "NCShare", bundle: nil).instantiateViewController(withIdentifier: "NCShareAdvancePermission") as? NCShareAdvancePermission,
  227. let navigationController = self.navigationController else { return }
  228. self.checkEnforcedPassword(shareType: sharee.shareType) { password in
  229. let shareOptions = NCTableShareOptions(sharee: sharee, metadata: self.metadata, password: password)
  230. advancePermission.share = shareOptions
  231. advancePermission.networking = self.networking
  232. advancePermission.metadata = self.metadata
  233. navigationController.pushViewController(advancePermission, animated: true)
  234. }
  235. }
  236. dropDown.show()
  237. }
  238. }
  239. // MARK: - UITableViewDelegate
  240. extension NCShare: UITableViewDelegate {
  241. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  242. if indexPath.section == 0, indexPath.row == 0 {
  243. // internal cell has description
  244. return 40
  245. }
  246. return 60
  247. }
  248. }
  249. // MARK: - UITableViewDataSource
  250. extension NCShare: UITableViewDataSource {
  251. func numberOfSections(in tableView: UITableView) -> Int {
  252. return 2
  253. }
  254. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  255. var numRows = shares.share?.count ?? 0
  256. if section == 0 {
  257. if metadata.e2eEncrypted, NCCapabilities.shared.getCapabilities(account: metadata.account).capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV12 {
  258. numRows = 1
  259. } else {
  260. // don't allow link creation if reshare is disabled
  261. numRows = shares.firstShareLink != nil || canReshare ? 2 : 1
  262. }
  263. }
  264. return numRows
  265. }
  266. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  267. // Setup default share cells
  268. guard indexPath.section != 0 else {
  269. guard let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell
  270. else { return UITableViewCell() }
  271. cell.delegate = self
  272. if metadata.e2eEncrypted, NCCapabilities.shared.getCapabilities(account: metadata.account).capabilityE2EEApiVersion == NCGlobal.shared.e2eeVersionV12 {
  273. cell.tableShare = shares.firstShareLink
  274. } else {
  275. if indexPath.row == 1 {
  276. cell.isInternalLink = true
  277. } else if shares.firstShareLink?.isInvalidated != true {
  278. cell.tableShare = shares.firstShareLink
  279. }
  280. }
  281. cell.setupCellUI()
  282. return cell
  283. }
  284. guard let tableShare = shares.share?[indexPath.row] else { return UITableViewCell() }
  285. // LINK
  286. if tableShare.shareType == shareCommon.SHARE_TYPE_LINK {
  287. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell {
  288. cell.indexPath = indexPath
  289. cell.tableShare = tableShare
  290. cell.delegate = self
  291. cell.setupCellUI()
  292. return cell
  293. }
  294. } else {
  295. // USER / GROUP etc.
  296. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellUser", for: indexPath) as? NCShareUserCell {
  297. cell.indexPath = indexPath
  298. cell.tableShare = tableShare
  299. cell.delegate = self
  300. cell.setupCellUI(userId: session.userId)
  301. let fileName = NCSession.shared.getFileName(urlBase: session.urlBase, user: tableShare.shareWith)
  302. let results = NCManageDatabase.shared.getImageAvatarLoaded(fileName: fileName)
  303. if results.image == nil {
  304. cell.fileAvatarImageView?.image = utility.loadUserImage(for: tableShare.shareWith, displayName: tableShare.shareWithDisplayname, urlBase: metadata.urlBase)
  305. } else {
  306. cell.fileAvatarImageView?.image = results.image
  307. }
  308. if !(results.tableAvatar?.loaded ?? false),
  309. NCNetworking.shared.downloadAvatarQueue.operations.filter({ ($0 as? NCOperationDownloadAvatar)?.fileName == fileName }).isEmpty {
  310. NCNetworking.shared.downloadAvatarQueue.addOperation(NCOperationDownloadAvatar(user: tableShare.shareWith, fileName: fileName, account: metadata.account, view: tableView))
  311. }
  312. return cell
  313. }
  314. }
  315. return UITableViewCell()
  316. }
  317. }
  318. // MARK: CNContactPickerDelegate
  319. extension NCShare: CNContactPickerDelegate {
  320. func contactPicker(_ picker: CNContactPickerViewController, didSelect contact: CNContact) {
  321. if contact.emailAddresses.count > 1 {
  322. showEmailList(arrEmail: contact.emailAddresses.map({$0.value as String}))
  323. } else if let email = contact.emailAddresses.first?.value as? String {
  324. searchField?.text = email
  325. networking?.getSharees(searchString: email)
  326. }
  327. }
  328. func showEmailList(arrEmail: [String]) {
  329. var actions = [NCMenuAction]()
  330. for email in arrEmail {
  331. actions.append(
  332. NCMenuAction(
  333. title: email,
  334. icon: utility.loadImage(named: "email", colors: [NCBrandColor.shared.iconImageColor]),
  335. selected: false,
  336. on: false,
  337. action: { _ in
  338. self.searchField?.text = email
  339. self.networking?.getSharees(searchString: email)
  340. }
  341. )
  342. )
  343. }
  344. DispatchQueue.main.asyncAfter(deadline: .now() + 0.5) {
  345. self.presentMenu(with: actions)
  346. }
  347. }
  348. }
  349. extension NCShare: UISearchBarDelegate {
  350. func searchBar(_ searchBar: UISearchBar, textDidChange searchText: String) {
  351. NSObject.cancelPreviousPerformRequests(withTarget: self, selector: #selector(searchSharees), object: nil)
  352. if searchText.isEmpty {
  353. dropDown.hide()
  354. } else {
  355. perform(#selector(searchSharees), with: nil, afterDelay: 0.5)
  356. }
  357. }
  358. @objc private func searchSharees() {
  359. // https://stackoverflow.com/questions/25471114/how-to-validate-an-e-mail-address-in-swift
  360. func isValidEmail(_ email: String) -> Bool {
  361. let emailRegEx = "[A-Z0-9a-z._%+-]+@[A-Za-z0-9.-]+\\.[A-Za-z]{2,64}"
  362. let emailPred = NSPredicate(format: "SELF MATCHES %@", emailRegEx)
  363. return emailPred.evaluate(with: email)
  364. }
  365. guard let searchString = searchField.text, !searchString.isEmpty else { return }
  366. if searchString.contains("@"), !isValidEmail(searchString) { return }
  367. networking?.getSharees(searchString: searchString)
  368. }
  369. }