NCShare.swift 22 KB

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