123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489 |
- //
- // NCShare.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 17/07/2019.
- // Copyright © 2019 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import Foundation
- import Parchment
- import DropDown
- import NCCommunication
- class NCShare: UIViewController, UIGestureRecognizerDelegate, NCShareLinkCellDelegate, NCShareUserCellDelegate, NCShareNetworkingDelegate {
-
- @IBOutlet weak var viewContainerConstraint: NSLayoutConstraint!
- @IBOutlet weak var sharedWithYouByView: UIView!
- @IBOutlet weak var sharedWithYouByImage: UIImageView!
- @IBOutlet weak var sharedWithYouByLabel: UILabel!
- @IBOutlet weak var searchFieldTopConstraint: NSLayoutConstraint!
- @IBOutlet weak var searchField: UITextField!
- @IBOutlet weak var shareLinkImage: UIImageView!
- @IBOutlet weak var shareLinkLabel: UILabel!
- @IBOutlet weak var buttonCopy: UIButton!
- @IBOutlet weak var buttonMenu: UIButton!
- @IBOutlet weak var tableView: UITableView!
-
- private let appDelegate = UIApplication.shared.delegate as! AppDelegate
- var metadata: tableMetadata?
- public var height: CGFloat = 0
- private var shareLinkMenuView: NCShareLinkMenuView?
- private var shareUserMenuView: NCShareUserMenuView?
- private var shareMenuViewWindow: UIView?
- private var dropDown = DropDown()
- private var networking: NCShareNetworking?
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- viewContainerConstraint.constant = height
- searchFieldTopConstraint.constant = 10
-
- searchField.placeholder = NSLocalizedString("_shareLinksearch_placeholder_", comment: "")
-
- shareLinkLabel.text = NSLocalizedString("_share_link_", comment: "")
- shareLinkImage.image = NCShareCommon.sharedInstance.createLinkAvatar()
- buttonCopy.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareCopy"), width: 100, height: 100, color: UIColor.gray), for: .normal)
- tableView.dataSource = self
- tableView.delegate = self
- tableView.allowsSelection = false
- tableView.register(UINib.init(nibName: "NCShareLinkCell", bundle: nil), forCellReuseIdentifier: "cellLink")
- tableView.register(UINib.init(nibName: "NCShareUserCell", bundle: nil), forCellReuseIdentifier: "cellUser")
-
- NotificationCenter.default.addObserver(self, selector: #selector(reloadData), name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataNCShare), object: nil)
-
- // Shared with you by ...
- if metadata!.ownerId != self.appDelegate.activeUserID {
-
- searchFieldTopConstraint.constant = 65
- sharedWithYouByView.isHidden = false
- sharedWithYouByLabel.text = NSLocalizedString("_shared_with_you_by_", comment: "") + " " + metadata!.ownerDisplayName
-
- let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + metadata!.ownerId + ".png"
- if FileManager.default.fileExists(atPath: fileNameLocalPath) {
- if let image = UIImage(contentsOfFile: fileNameLocalPath) { sharedWithYouByImage.image = image }
- } else {
- NCCommunication.sharedInstance.downloadAvatar(serverUrl: appDelegate.activeUrl, userID: metadata!.ownerId, fileNameLocalPath: fileNameLocalPath, size: Int(k_avatar_size), customUserAgent: nil, addCustomHeaders: nil, account: appDelegate.activeAccount) { (account, data, errorCode, errorMessage) in
- if errorCode == 0 && account == self.appDelegate.activeAccount && UIImage(data: data!) != nil {
- if let image = UIImage(contentsOfFile: fileNameLocalPath) {
- self.sharedWithYouByImage.image = image
- }
- } else {
- self.sharedWithYouByImage.image = UIImage(named: "avatar")
- }
- }
- }
- }
-
- reloadData()
-
- networking = NCShareNetworking.init(metadata: metadata!, activeUrl: appDelegate.activeUrl, view: self.view, delegate: self)
- networking?.readShare()
-
- // changeTheming
- NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
- changeTheming()
- }
-
- @objc func changeTheming() {
- appDelegate.changeTheming(self, tableView: tableView, collectionView: nil, form: true)
-
- shareLinkLabel.textColor = NCBrandColor.sharedInstance.textView
- }
-
- @objc func reloadData() {
- let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
- if shares.firstShareLink == nil {
- buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareAdd"), width: 100, height: 100, color: UIColor.gray), for: .normal)
- buttonCopy.isHidden = true
- } else {
- buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width: 100, height: 100, color: UIColor.gray), for: .normal)
- buttonCopy.isHidden = false
- }
- tableView.reloadData()
- }
-
- // MARK: - IBAction
- @IBAction func searchFieldDidEndOnExit(textField: UITextField) {
-
- guard let searchString = textField.text else { return }
- networking?.getUserAndGroup(searchString: searchString)
- }
-
- @IBAction func touchUpInsideButtonCopy(_ sender: Any) {
-
- guard let metadata = self.metadata else { return }
- let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata)
- tapCopy(with: shares.firstShareLink, sender: sender)
- }
-
- @IBAction func touchUpInsideButtonMenu(_ sender: Any) {
- guard let metadata = self.metadata else { return }
- guard let capabilities = NCManageDatabase.sharedInstance.getCapabilites(account: metadata.account) else { return }
- let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata)
- if capabilities.isFilesSharingPublicPasswordEnforced && shares.firstShareLink == nil {
- let alertController = UIAlertController(title: NSLocalizedString("_enforce_password_protection_", comment: ""), message: "", preferredStyle: .alert)
- alertController.addTextField { (textField) in
- textField.isSecureTextEntry = true
- textField.addTarget(self, action: #selector(self.minCharTextFieldDidChange(sender:)), for: UIControl.Event.editingChanged)
- }
- alertController.addAction(UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in })
- let okAction = UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { (action:UIAlertAction) in
- let password = alertController.textFields?.first?.text
- self.networking?.share(password: password ?? "", permission: 1, hideDownload: false)
- }
- okAction.isEnabled = false
- alertController.addAction(okAction)
-
- self.present(alertController, animated: true, completion:nil)
- } else if shares.firstShareLink == nil {
- networking?.share(password: "", permission: 1, hideDownload: false)
- } else {
- tapMenu(with: shares.firstShareLink!, sender: sender)
- }
- }
-
- @objc func minCharTextFieldDidChange(sender: UITextField) {
- guard let alertController = self.presentedViewController as? UIAlertController else { return }
- guard let password = alertController.textFields?.first else { return }
- guard let ok = alertController.actions.last else { return }
- ok.isEnabled = password.text?.count ?? 0 >= 8
- }
-
- @objc func tapLinkMenuViewWindow(gesture: UITapGestureRecognizer) {
- shareLinkMenuView?.unLoad()
- shareLinkMenuView = nil
- shareUserMenuView?.unLoad()
- shareUserMenuView = nil
- }
-
- func gestureRecognizer(_ gestureRecognizer: UIGestureRecognizer, shouldReceive touch: UITouch) -> Bool {
- return gestureRecognizer.view == touch.view
- }
-
- func tapCopy(with tableShare: tableShare?, sender: Any) {
- NCShareCommon.sharedInstance.copyLink(tableShare: tableShare, viewController: self, sender: sender)
- }
-
- func switchCanEdit(with tableShare: tableShare?, switch: Bool, sender: UISwitch) {
-
- guard let tableShare = tableShare else { return }
- guard let metadata = self.metadata else { return }
- let canShare = UtilsFramework.isPermission(toCanShare: tableShare.permissions)
- var permission: Int = 0
-
- if sender.isOn {
- permission = UtilsFramework.getPermissionsValue(byCanEdit: true, andCanCreate: true, andCanChange: true, andCanDelete: true, andCanShare: canShare, andIsFolder: metadata.directory)
- } else {
- permission = UtilsFramework.getPermissionsValue(byCanEdit: false, andCanCreate: false, andCanChange: false, andCanDelete: false, andCanShare: canShare, andIsFolder: metadata.directory)
- }
-
- networking?.updateShare(idRemoteShared: tableShare.idRemoteShared, password: nil, permission: permission, note: nil, expirationTime: nil, hideDownload: tableShare.hideDownload)
- }
-
- func tapMenu(with tableShare: tableShare?, sender: Any) {
-
- guard let tableShare = tableShare else { return }
- if tableShare.shareType == Int(shareTypeLink.rawValue) {
- let views = NCShareCommon.sharedInstance.openViewMenuShareLink(shareViewController: self, tableShare: tableShare, metadata: metadata!)
- shareLinkMenuView = views.shareLinkMenuView
- shareMenuViewWindow = views.viewWindow
-
- let tap = UITapGestureRecognizer(target: self, action: #selector(tapLinkMenuViewWindow))
- tap.delegate = self
- shareMenuViewWindow?.addGestureRecognizer(tap)
- } else {
- let views = NCShareCommon.sharedInstance.openViewMenuUser(shareViewController: self, tableShare: tableShare, metadata: metadata!)
- shareUserMenuView = views.shareUserMenuView
- shareMenuViewWindow = views.viewWindow
-
- let tap = UITapGestureRecognizer(target: self, action: #selector(tapLinkMenuViewWindow))
- tap.delegate = self
- shareMenuViewWindow?.addGestureRecognizer(tap)
- }
- }
-
- /// MARK: - NCShareNetworkingDelegate
-
- func readShareCompleted() {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataNCShare), object: nil, userInfo: nil)
- }
-
- func shareCompleted() {
- NotificationCenter.default.post(name: NSNotification.Name(rawValue: k_notificationCenter_reloadDataNCShare), object: nil, userInfo: nil)
- }
-
- func unShareCompleted() { }
-
- func updateShareWithError(idRemoteShared: Int) { }
-
- func getUserAndGroup(items: [OCShareUser]?) {
-
- guard let items = items else { return }
- dropDown = DropDown()
- let appearance = DropDown.appearance()
-
- appearance.backgroundColor = .white
- appearance.cornerRadius = 10
- appearance.shadowColor = UIColor(white: 0.5, alpha: 1)
- appearance.shadowOpacity = 0.9
- appearance.shadowRadius = 25
- appearance.animationduration = 0.25
- appearance.textColor = .darkGray
- appearance.setupMaskedCorners([.layerMaxXMaxYCorner, .layerMinXMaxYCorner])
-
- for item in items {
- if item.displayName != nil && item.displayName != "" {
- dropDown.dataSource.append(item.displayName)
- } else {
- dropDown.dataSource.append(item.name)
- }
- }
-
- dropDown.anchorView = searchField
- dropDown.bottomOffset = CGPoint(x: 0, y: searchField.bounds.height)
- dropDown.width = searchField.bounds.width
- dropDown.direction = .bottom
-
- dropDown.cellNib = UINib(nibName: "NCShareUserDropDownCell", bundle: nil)
- dropDown.customCellConfiguration = { (index: Index, item: String, cell: DropDownCell) -> Void in
- guard let cell = cell as? NCShareUserDropDownCell else { return }
- cell.imageItem.image = UIImage(named: "avatar")
- let item = items[index]
- let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(self.appDelegate.activeUser, activeUrl: self.appDelegate.activeUrl) + "-" + item.name + ".png"
- if FileManager.default.fileExists(atPath: fileNameLocalPath) {
- if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
- } else {
- DispatchQueue.global().async {
- NCCommunication.sharedInstance.downloadAvatar(serverUrl: self.appDelegate.activeUrl, userID: item.name, fileNameLocalPath: fileNameLocalPath, size: Int(k_avatar_size), customUserAgent: nil, addCustomHeaders: nil, account: self.appDelegate.activeAccount) { (account, data, errorCode, errorMessage) in
- if errorCode == 0 && account == self.appDelegate.activeAccount && UIImage(data: data!) != nil {
- if let image = UIImage(contentsOfFile: fileNameLocalPath) {
- cell.imageItem.image = image
- }
- } else {
- cell.imageItem.image = UIImage(named: "avatar")
- }
- }
- }
- }
- if item.shareeType == 0 { cell.imageShareeType.image = UIImage(named: "shareTypeUser")} // shareTypeUser
- if item.shareeType == 1 { cell.imageShareeType.image = UIImage(named: "shareTypeGroup")} // shareTypeGroup
- if item.shareeType == 3 { cell.imageShareeType.image = UIImage(named: "shareTypeLink")} // shareTypeLink
- if item.shareeType == 4 { cell.imageShareeType.image = UIImage(named: "shareTypeEmail")} // shareTypeEmail
- if item.shareeType == 5 { cell.imageShareeType.image = UIImage(named: "shareTypeUser")} // shareTypeContact
- if item.shareeType == 6 { cell.imageShareeType.image = UIImage(named: "shareTypeLink")} // shareTypeRemote
- }
-
- dropDown.selectionAction = { [weak self] (index, item) in
- let item = items[index]
- self!.networking?.shareUserAndGroup(name: item.name, shareeType: item.shareeType, metadata: self!.metadata!)
- }
-
- dropDown.show()
- }
- }
- // MARK: - UITableViewDelegate
- extension NCShare: UITableViewDelegate {
-
- func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
- return 60
- }
- }
- // MARK: - UITableViewDataSource
- extension NCShare: UITableViewDataSource {
-
- func numberOfSections(in tableView: UITableView) -> Int {
- return 1
- }
-
- func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
-
- var numOfRows = 0
- let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
-
- if shares.share != nil {
- numOfRows = shares.share!.count
- }
-
- return numOfRows
- }
-
- func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
-
- let shares = NCManageDatabase.sharedInstance.getTableShares(metadata: metadata!)
- let tableShare = shares.share![indexPath.row]
-
- // LINK
- if tableShare.shareType == Int(shareTypeLink.rawValue) {
- if let cell = tableView.dequeueReusableCell(withIdentifier: "cellLink", for: indexPath) as? NCShareLinkCell {
- cell.tableShare = tableShare
- cell.delegate = self
- cell.labelTitle.text = NSLocalizedString("_share_link_", comment: "")
- cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
- return cell
- }
- } else {
- // USER
- if let cell = tableView.dequeueReusableCell(withIdentifier: "cellUser", for: indexPath) as? NCShareUserCell {
-
- cell.tableShare = tableShare
- cell.delegate = self
- cell.labelTitle.text = tableShare.shareWithDisplayName
- cell.labelTitle.textColor = NCBrandColor.sharedInstance.textView
- cell.labelCanEdit.text = NSLocalizedString("_share_permission_edit_", comment: "")
- cell.labelCanEdit.textColor = NCBrandColor.sharedInstance.textView
- cell.isUserInteractionEnabled = true
- cell.switchCanEdit.isHidden = false
- cell.labelCanEdit.isHidden = false
- cell.buttonMenu.isHidden = false
-
- let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(appDelegate.activeUser, activeUrl: appDelegate.activeUrl) + "-" + tableShare.shareWith + ".png"
- if FileManager.default.fileExists(atPath: fileNameLocalPath) {
- if let image = UIImage(contentsOfFile: fileNameLocalPath) { cell.imageItem.image = image }
- } else {
- DispatchQueue.global().async {
- NCCommunication.sharedInstance.downloadAvatar(serverUrl: self.appDelegate.activeUrl, userID: tableShare.shareWith, fileNameLocalPath: fileNameLocalPath, size: Int(k_avatar_size), customUserAgent: nil, addCustomHeaders: nil, account: self.appDelegate.activeAccount) { (account, data, errorCode, errorMessage) in
- if errorCode == 0 && account == self.appDelegate.activeAccount && UIImage(data: data!) != nil {
- if let image = UIImage(contentsOfFile: fileNameLocalPath) {
- cell.imageItem.image = image
- }
- } else {
- cell.imageItem.image = UIImage(named: "avatar")
- }
- }
- }
- }
-
- if UtilsFramework.isAnyPermission(toEdit: tableShare.permissions) {
- cell.switchCanEdit.setOn(true, animated: false)
- } else {
- cell.switchCanEdit.setOn(false, animated: false)
- }
-
- // If the initiator or the recipient is not the current user, show the list of sharees without any options to edit it.
- if tableShare.uidOwner != self.appDelegate.activeUserID && tableShare.uidFileOwner != self.appDelegate.activeUserID {
- cell.isUserInteractionEnabled = false
- cell.switchCanEdit.isHidden = true
- cell.labelCanEdit.isHidden = true
- cell.buttonMenu.isHidden = true
- }
-
- return cell
- }
- }
-
- return UITableViewCell()
- }
- }
- // MARK: - NCShareLinkCell
- class NCShareLinkCell: UITableViewCell {
-
- @IBOutlet weak var imageItem: UIImageView!
- @IBOutlet weak var labelTitle: UILabel!
- @IBOutlet weak var buttonCopy: UIButton!
- @IBOutlet weak var buttonMenu: UIButton!
-
- private let iconShare: CGFloat = 200
-
- var tableShare: tableShare?
- var delegate: NCShareLinkCellDelegate?
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- imageItem.image = NCShareCommon.sharedInstance.createLinkAvatar()
- buttonCopy.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareCopy"), width:100, height: 100, color: UIColor.gray), for: .normal)
- buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width:100, height: 100, color: UIColor.gray), for: .normal)
- }
-
- @IBAction func touchUpInsideCopy(_ sender: Any) {
- delegate?.tapCopy(with: tableShare, sender: sender)
- }
-
- @IBAction func touchUpInsideMenu(_ sender: Any) {
- delegate?.tapMenu(with: tableShare, sender: sender)
- }
- }
- protocol NCShareLinkCellDelegate {
- func tapCopy(with tableShare: tableShare?, sender: Any)
- func tapMenu(with tableShare: tableShare?, sender: Any)
- }
- // MARK: - NCShareUserCell
- class NCShareUserCell: UITableViewCell {
-
- @IBOutlet weak var imageItem: UIImageView!
- @IBOutlet weak var labelTitle: UILabel!
- @IBOutlet weak var labelCanEdit: UILabel!
- @IBOutlet weak var switchCanEdit: UISwitch!
- @IBOutlet weak var buttonMenu: UIButton!
-
- var tableShare: tableShare?
- var delegate: NCShareUserCellDelegate?
-
- override func awakeFromNib() {
- super.awakeFromNib()
-
- switchCanEdit.transform = CGAffineTransform(scaleX: 0.75, y: 0.75)
- switchCanEdit.onTintColor = NCBrandColor.sharedInstance.brand
- buttonMenu.setImage(CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMenu"), width:100, height: 100, color: UIColor.gray), for: .normal)
- }
-
- @IBAction func switchCanEditChanged(sender: UISwitch) {
- delegate?.switchCanEdit(with: tableShare, switch: sender.isOn, sender: sender)
- }
-
- @IBAction func touchUpInsideMenu(_ sender: Any) {
- delegate?.tapMenu(with: tableShare, sender: sender)
- }
- }
- protocol NCShareUserCellDelegate {
- func switchCanEdit(with tableShare: tableShare?, switch: Bool, sender: UISwitch)
- func tapMenu(with tableShare: tableShare?, sender: Any)
- }
- // MARK: - NCShareUserDropDownCell
- class NCShareUserDropDownCell: DropDownCell {
-
- @IBOutlet weak var imageItem: UIImageView!
- @IBOutlet weak var imageShareeType: UIImageView!
- }
|