NCShare.swift 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  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. //
  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. //
  23. import Foundation
  24. import Parchment
  25. import DropDown
  26. import NCCommunication
  27. class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareLinkCellDelegate, NCShareUserCellDelegate, NCShareNetworkingDelegate {
  28. @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
  29. @IBOutlet weak var sharedWithYouByView: UIView!
  30. @IBOutlet weak var sharedWithYouByImage: UIImageView!
  31. @IBOutlet weak var sharedWithYouByLabel: UILabel!
  32. @IBOutlet weak var searchFieldTopConstraint: NSLayoutConstraint!
  33. @IBOutlet weak var searchField: UITextField!
  34. @IBOutlet weak var shareLinkImage: UIImageView!
  35. @IBOutlet weak var shareLinkLabel: UILabel!
  36. @IBOutlet weak var buttonCopy: UIButton!
  37. @IBOutlet weak var buttonMenu: UIButton!
  38. @IBOutlet weak var tableView: UITableView!
  39. private let appDelegate = UIApplication.shared.delegate as! AppDelegate
  40. var metadata: tableMetadata?
  41. public var height: CGFloat = 0
  42. private var shareLinkMenuView: NCShareLinkMenuView?
  43. private var shareUserMenuView: NCShareUserMenuView?
  44. private var shareMenuViewWindow: UIView?
  45. private var dropDown = DropDown()
  46. private var networking: NCShareNetworking?
  47. override func viewDidLoad() {
  48. super.viewDidLoad()
  49. viewContainerConstraint.constant = height
  50. searchFieldTopConstraint.constant = 10
  51. searchField.placeholder = NSLocalizedString("_shareLinksearch_placeholder_", comment: "")
  52. shareLinkLabel.text = NSLocalizedString("_share_link_", comment: "")
  53. shareLinkImage.image = NCShareCommon.sharedInstance.createLinkAvatar()
  54. buttonCopy.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareCopy"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  55. tableView.dataSource = self
  56. tableView.delegate = self
  57. tableView.allowsSelection = false
  58. tableView.register(UINib.init(nibName: "NCShareLinkCell", bundle: nil), forCellReuseIdentifier: "cellLink")
  59. tableView.register(UINib.init(nibName: "NCShareUserCell", bundle: nil), forCellReuseIdentifier: "cellUser")
  60. NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataNCShare), object: nil)
  61. // Shared with you by ...
  62. if metadata!.ownerId != self.appDelegate.activeUserID {
  63. searchFieldTopConstraint.constant = 65
  64. sharedWithYouByView.isHidden = false
  65. sharedWithYouByLabel.text = NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata!.ownerDisplayName
  66. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + metadata!.ownerId + ".png"
  67. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  68. if let image = UIImage(contentsOfFile: fileNameLocalPath) { sharedWithYouByImage.image = image }
  69. } else {
  70. NCCommunication.shared.downloadAvatar(userID: metadata!.ownerId, fileNameLocalPath: fileNameLocalPath, size: Int(k_avatar_size)) { (account, data, errorCode, errorMessage) in
  71. if errorCode == 0 && account == self.appDelegate.activeAccount && UIImage(data: data!) != nil {
  72. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  73. self.sharedWithYouByImage.image = image
  74. }
  75. } else {
  76. self.sharedWithYouByImage.image = UIImage(named: "avatar")
  77. }
  78. }
  79. }
  80. }
  81. reloadData()
  82. networking = NCShareNetworking.init(metadata: metadata!, activeUrl: appDelegate.activeUrl, view: self.view, delegate: self)
  83. networking?.readShare()
  84. // changeTheming
  85. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  86. changeTheming()
  87. }
  88. @objc func changeTheming() {
  89. appDelegate.changeTheming(self, tableView: tableView, collectionView: nil, form: true)
  90. shareLinkLabel.textColor = NCBrandColor.sharedInstance.textView
  91. }
  92. @objc func reloadData() {
  93. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  94. if shares.firstShareLink == nil {
  95. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareAdd"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  96. buttonCopy.isHidden = true
  97. } else {
  98. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width: 100, height: 100, color: UIColor.gray), for: .normal)
  99. buttonCopy.isHidden = false
  100. }
  101. tableView.reloadData()
  102. }
  103. // MARK: - IBAction
  104. @IBAction func searchFieldDidEndOnExit(textField: UITextField) {
  105. guard let searchString = textField.text else { return }
  106. networking?.getUserAndGroup(searchString: searchString)
  107. }
  108. @IBAction func touchUpInsideButtonCopy(_ sender: Any) {
  109. guard let metadata = self.metadata else { return }
  110. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata)
  111. tapCopy(with: shares.firstShareLink, sender: sender)
  112. }
  113. @IBAction func touchUpInsideButtonMenu(_ sender: Any) {
  114. guard let metadata = self.metadata else { return }
  115. let isFilesSharingPublicPasswordEnforced = NCManageDatabase.sharedInstance.getCapabilitiesServerBool(account: metadata.account, elements: NCElementsJSON.shared.capabilitiesFileSharingPubPasswdEnforced, exists: false)
  116. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata)
  117. if isFilesSharingPublicPasswordEnforced && shares.firstShareLink == nil {
  118. let alertController = UIAlertController(title: NSLocalizedString("_enforce_password_protection_", comment: ""), message: "", preferredStyle: .alert)
  119. alertController.addTextField { (textField) in
  120. textField.isSecureTextEntry = true
  121. textField.addTarget(self, action: #selector(self.minCharTextFieldDidChange(sender:)), for: UIControl.Event.editingChanged)
  122. }
  123. alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in })
  124. let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { (action:UIAlertAction) in
  125. let password = alertController.textFields?.first?.text
  126. self.networking?.shareLink(password: password ?? "")
  127. }
  128. okAction.isEnabled = false
  129. alertController.addAction(okAction)
  130. self.present(alertController, animated: true, completion:nil)
  131. } else if shares.firstShareLink == nil {
  132. networking?.shareLink(password: "")
  133. } else {
  134. tapMenu(with: shares.firstShareLink!, sender: sender)
  135. }
  136. }
  137. @objc func minCharTextFieldDidChange(sender: UITextField) {
  138. guard let alertController = self.presentedViewController as? UIAlertController else { return }
  139. guard let password = alertController.textFields?.first else { return }
  140. guard let ok = alertController.actions.last else { return }
  141. ok.isEnabled = password.text?.count ?? 0 >= 8
  142. }
  143. @objc func tapLinkMenuViewWindow(gesture: UITapGestureRecognizer) {
  144. shareLinkMenuView?.unLoad()
  145. shareLinkMenuView = nil
  146. shareUserMenuView?.unLoad()
  147. shareUserMenuView = nil
  148. }
  149. func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
  150. return gestureRecognizer.view == touch.view
  151. }
  152. func tapCopy(with tableShare: tableShare?, sender: Any) {
  153. NCShareCommon.sharedInstance.copyLink(tableShare: tableShare, viewController: self, sender: sender)
  154. }
  155. func switchCanEdit(with tableShare: tableShare?, switch: Bool, sender: UISwitch) {
  156. guard let tableShare = tableShare else { return }
  157. guard let metadata = self.metadata else { return }
  158. let canShare = CCUtility.isPermission(toCanShare: tableShare.permissions)
  159. var permission: Int = 0
  160. if sender.isOn {
  161. permission = CCUtility.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: canShare, andIsFolder: metadata.directory)
  162. } else {
  163. permission = CCUtility.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: canShare, andIsFolder: metadata.directory)
  164. }
  165. networking?.updateShare(idShare: tableShare.idShare, password: nil, permission: permission, note: nil, expirationDate: nil, hideDownload: tableShare.hideDownload)
  166. }
  167. func tapMenu(with tableShare: tableShare?, sender: Any) {
  168. guard let tableShare = tableShare else { return }
  169. if tableShare.shareType == 3 {
  170. let views = NCShareCommon.sharedInstance.openViewMenuShareLink(shareViewController: self, tableShare: tableShare, metadata: metadata!)
  171. shareLinkMenuView = views.shareLinkMenuView
  172. shareMenuViewWindow = views.viewWindow
  173. let tap = UITapGestureRecognizer(target: self, action: #selector(tapLinkMenuViewWindow))
  174. tap.delegate = self
  175. shareMenuViewWindow?.addGestureRecognizer(tap)
  176. } else {
  177. let views = NCShareCommon.sharedInstance.openViewMenuUser(shareViewController: self, tableShare: tableShare, metadata: metadata!)
  178. shareUserMenuView = views.shareUserMenuView
  179. shareMenuViewWindow = views.viewWindow
  180. let tap = UITapGestureRecognizer(target: self, action: #selector(tapLinkMenuViewWindow))
  181. tap.delegate = self
  182. shareMenuViewWindow?.addGestureRecognizer(tap)
  183. }
  184. }
  185. /// MARK: - NCShareNetworkingDelegate
  186. func readShareCompleted() {
  187. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataNCShare)
  188. }
  189. func shareCompleted() {
  190. NotificationCenter.default.postOnMainThread(name: k_notificationCenter_reloadDataNCShare)
  191. }
  192. func unShareCompleted() { }
  193. func updateShareWithError(idShare: Int) { }
  194. func getUserAndGroup(sharees: [NCCommunicationSharee]?) {
  195. guard let sharees = sharees else { return }
  196. dropDown = DropDown()
  197. let appearance = DropDown.appearance()
  198. appearance.backgroundColor = .white
  199. appearance.cornerRadius = 10
  200. appearance.shadowColor = UIColor(white: 0.5, alpha: 1)
  201. appearance.shadowOpacity = 0.9
  202. appearance.shadowRadius = 25
  203. appearance.animationduration = 0.25
  204. appearance.textColor = .darkGray
  205. appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
  206. for sharee in sharees {
  207. dropDown.dataSource.append(sharee.label)
  208. }
  209. dropDown.anchorView = searchField
  210. dropDown.bottomOffset = CGPoint(x: 0, y: searchField.bounds.height)
  211. dropDown.width = searchField.bounds.width
  212. dropDown.direction = .bottom
  213. dropDown.cellNib = UINib(nibName: "NCShareUserDropDownCell", bundle: nil)
  214. dropDown.customCellConfiguration = { (index: Index, item: String, cell: DropDownCell) -> Void in
  215. guard let cell = cell as? NCShareUserDropDownCell else { return }
  216. cell.imageItem.image = UIImage(named: "avatar")
  217. let sharee = sharees[index]
  218. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(self.appDelegate.activeUser, activeUrl: self.appDelegate.activeUrl) + "-" + sharee.label + ".png"
  219. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  220. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  221. } else {
  222. DispatchQueue.global().async {
  223. NCCommunication.shared.downloadAvatar(userID: sharee.label, fileNameLocalPath: fileNameLocalPath, size: Int(k_avatar_size)) { (account, data, errorCode, errorMessage) in
  224. if errorCode == 0 && account == self.appDelegate.activeAccount && UIImage(data: data!) != nil {
  225. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  226. cell.imageItem.image = image
  227. }
  228. } else {
  229. cell.imageItem.image = UIImage(named: "avatar")
  230. }
  231. }
  232. }
  233. }
  234. if sharee.shareType == 0 { cell.imageShareeType.image = UIImage(named: "shareTypeUser")} // shareTypeUser
  235. if sharee.shareType == 1 { cell.imageShareeType.image = UIImage(named: "shareTypeGroup")} // shareTypeGroup
  236. if sharee.shareType == 3 { cell.imageShareeType.image = UIImage(named: "shareTypeLink")} // shareTypeLink
  237. if sharee.shareType == 4 { cell.imageShareeType.image = UIImage(named: "shareTypeEmail")} // shareTypeEmail
  238. if sharee.shareType == 5 { cell.imageShareeType.image = UIImage(named: "shareTypeUser")} // shareTypeContact
  239. if sharee.shareType == 6 { cell.imageShareeType.image = UIImage(named: "shareTypeLink")} // shareTypeRemote
  240. }
  241. dropDown.selectionAction = { [weak self] (index, item) in
  242. let sharee = sharees[index]
  243. self!.networking?.share(name: sharee.label, shareType: sharee.shareType, metadata: self!.metadata!)
  244. }
  245. dropDown.show()
  246. }
  247. }
  248. // MARK: - UITableViewDelegate
  249. extension NCShare: UITableViewDelegate {
  250. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  251. return 60
  252. }
  253. }
  254. // MARK: - UITableViewDataSource
  255. extension NCShare: UITableViewDataSource {
  256. func numberOfSections(in tableView: UITableView) -> Int {
  257. return 1
  258. }
  259. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  260. var numOfRows = 0
  261. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  262. if shares.share != nil {
  263. numOfRows = shares.share!.count
  264. }
  265. return numOfRows
  266. }
  267. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  268. let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
  269. let tableShare = shares.share![indexPath.row]
  270. // LINK
  271. if tableShare.shareType == 3 {
  272. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell {
  273. cell.tableShare = tableShare
  274. cell.delegate = self
  275. cell.labelTitle.text = NSLocalizedString("_share_link_", comment: "")
  276. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  277. return cell
  278. }
  279. } else {
  280. // USER
  281. if let cell = tableView.dequeueReusableCell(withIdentifier: "cellUser", for: indexPath) as? NCShareUserCell {
  282. cell.tableShare = tableShare
  283. cell.delegate = self
  284. cell.labelTitle.text = tableShare.shareWithDisplayname
  285. cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
  286. cell.labelCanEdit.text = NSLocalizedString("_share_permission_edit_", comment: "")
  287. cell.labelCanEdit.textColor = NCBrandColor.sharedInstance.textView
  288. cell.isUserInteractionEnabled = true
  289. cell.switchCanEdit.isHidden = false
  290. cell.labelCanEdit.isHidden = false
  291. cell.buttonMenu.isHidden = false
  292. let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + tableShare.shareWith + ".png"
  293. if FileManager.default.fileExists(atPath: fileNameLocalPath) {
  294. if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
  295. } else {
  296. DispatchQueue.global().async {
  297. NCCommunication.shared.downloadAvatar(userID: tableShare.shareWith, fileNameLocalPath: fileNameLocalPath, size: Int(k_avatar_size)) { (account, data, errorCode, errorMessage) in
  298. if errorCode == 0 && account == self.appDelegate.activeAccount && UIImage(data: data!) != nil {
  299. if let image = UIImage(contentsOfFile: fileNameLocalPath) {
  300. cell.imageItem.image = image
  301. }
  302. } else {
  303. cell.imageItem.image = UIImage(named: "avatar")
  304. }
  305. }
  306. }
  307. }
  308. if CCUtility.isAnyPermission(toEdit: tableShare.permissions) {
  309. cell.switchCanEdit.setOn(true, animated: false)
  310. } else {
  311. cell.switchCanEdit.setOn(false, animated: false)
  312. }
  313. // If the initiator or the recipient is not the current user, show the list of sharees without any options to edit it.
  314. if tableShare.uidOwner != self.appDelegate.activeUserID && tableShare.uidFileOwner != self.appDelegate.activeUserID {
  315. cell.isUserInteractionEnabled = false
  316. cell.switchCanEdit.isHidden = true
  317. cell.labelCanEdit.isHidden = true
  318. cell.buttonMenu.isHidden = true
  319. }
  320. return cell
  321. }
  322. }
  323. return UITableViewCell()
  324. }
  325. }
  326. // MARK: - NCShareLinkCell
  327. class NCShareLinkCell: UITableViewCell {
  328. @IBOutlet weak var imageItem: UIImageView!
  329. @IBOutlet weak var labelTitle: UILabel!
  330. @IBOutlet weak var buttonCopy: UIButton!
  331. @IBOutlet weak var buttonMenu: UIButton!
  332. private let iconShare: CGFloat = 200
  333. var tableShare: tableShare?
  334. var delegate: NCShareLinkCellDelegate?
  335. override func awakeFromNib() {
  336. super.awakeFromNib()
  337. imageItem.image = NCShareCommon.sharedInstance.createLinkAvatar()
  338. buttonCopy.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareCopy"), width:100, height: 100, color: UIColor.gray), for: .normal)
  339. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width:100, height: 100, color: UIColor.gray), for: .normal)
  340. }
  341. @IBAction func touchUpInsideCopy(_ sender: Any) {
  342. delegate?.tapCopy(with: tableShare, sender: sender)
  343. }
  344. @IBAction func touchUpInsideMenu(_ sender: Any) {
  345. delegate?.tapMenu(with: tableShare, sender: sender)
  346. }
  347. }
  348. protocol NCShareLinkCellDelegate {
  349. func tapCopy(with tableShare: tableShare?, sender: Any)
  350. func tapMenu(with tableShare: tableShare?, sender: Any)
  351. }
  352. // MARK: - NCShareUserCell
  353. class NCShareUserCell: UITableViewCell {
  354. @IBOutlet weak var imageItem: UIImageView!
  355. @IBOutlet weak var labelTitle: UILabel!
  356. @IBOutlet weak var labelCanEdit: UILabel!
  357. @IBOutlet weak var switchCanEdit: UISwitch!
  358. @IBOutlet weak var buttonMenu: UIButton!
  359. var tableShare: tableShare?
  360. var delegate: NCShareUserCellDelegate?
  361. override func awakeFromNib() {
  362. super.awakeFromNib()
  363. switchCanEdit.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
  364. switchCanEdit.onTintColor = NCBrandColor.sharedInstance.brandElement
  365. buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width:100, height: 100, color: UIColor.gray), for: .normal)
  366. }
  367. @IBAction func switchCanEditChanged(sender: UISwitch) {
  368. delegate?.switchCanEdit(with: tableShare, switch: sender.isOn, sender: sender)
  369. }
  370. @IBAction func touchUpInsideMenu(_ sender: Any) {
  371. delegate?.tapMenu(with: tableShare, sender: sender)
  372. }
  373. }
  374. protocol NCShareUserCellDelegate {
  375. func switchCanEdit(with tableShare: tableShare?, switch: Bool, sender: UISwitch)
  376. func tapMenu(with tableShare: tableShare?, sender: Any)
  377. }
  378. // MARK: - NCShareUserDropDownCell
  379. class NCShareUserDropDownCell: DropDownCell {
  380. @IBOutlet weak var imageItem: UIImageView!
  381. @IBOutlet weak var imageShareeType: UIImageView!
  382. }