NCShare.swift 22 KB

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