123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513 |
- //
- // NCCreateFormUploadDocuments.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 14/11/18.
- // Copyright © 2018 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 NCCommunication
- // MARK: -
- @objc class NCCreateFormUploadDocuments: XLFormViewController, NCSelectDelegate, UICollectionViewDataSource, UICollectionViewDelegate, UICollectionViewDelegateFlowLayout, NCCreateFormUploadConflictDelegate {
-
- var editorId = ""
- var creatorId = ""
- var typeTemplate = ""
- var templateIdentifier = ""
- var serverUrl = ""
- var fileNameFolder = ""
- var fileName = ""
- var fileNameExtension = ""
- var titleForm = ""
- var listOfTemplate = [NCCommunicationEditorTemplates]()
- var selectTemplate: NCCommunicationEditorTemplates?
-
- @IBOutlet weak var indicator: UIActivityIndicatorView!
- @IBOutlet weak var collectionView: UICollectionView!
- @IBOutlet weak var collectionViewHeigth: NSLayoutConstraint!
-
- // Layout
- let numItems = 2
- let sectionInsets: CGFloat = 10
- let highLabelName: CGFloat = 20
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- // MARK: - View Life Cycle
-
- override func viewDidLoad() {
- super.viewDidLoad()
-
- if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
- fileNameFolder = "/"
- } else {
- fileNameFolder = (serverUrl as NSString).lastPathComponent
- }
-
- self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
-
- let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
- let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
-
- self.navigationItem.leftBarButtonItem = cancelButton
- self.navigationItem.rightBarButtonItem = saveButton
- self.navigationItem.rightBarButtonItem?.isEnabled = false
-
- // title
- self.title = titleForm
-
- // Theming view
- NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
- changeTheming()
-
- // load the templates available
- getTemplate()
- }
-
- @objc func changeTheming() {
- appDelegate.changeTheming(self, tableView: tableView, collectionView: collectionView, form: false)
- initializeForm()
- }
-
- // MARK: - Tableview (XLForm)
- func initializeForm() {
-
- let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
- form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
-
- var section : XLFormSectionDescriptor
- var row : XLFormRowDescriptor
-
- // Section: Destination Folder
-
- section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: "").uppercased())
- form.addFormSection(section)
-
- row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: fileNameFolder)
- row.action.formSelector = #selector(changeDestinationFolder(_:))
- row.value = fileNameFolder
- row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
- row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement) as UIImage
-
- row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
- row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
- row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
- section.addFormRow(row)
-
- // Section: File Name
-
- section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: "").uppercased())
- form.addFormSection(section)
-
- row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
- row.value = fileName
- row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
-
- row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
- row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
- row.cellConfig["textField.textColor"] = NCBrandColor.sharedInstance.textView
-
- row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
- row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
- row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
-
- section.addFormRow(row)
-
- self.form = form
- }
- override func tableView(_ tableView: UITableView, willDisplayHeaderView view: UIView, forSection section: Int) {
- let header: UITableViewHeaderFooterView = view as! UITableViewHeaderFooterView
- header.textLabel?.font = UIFont.systemFont(ofSize: 13.0)
- header.textLabel?.textColor = .gray
- header.tintColor = NCBrandColor.sharedInstance.backgroundForm
- }
- // MARK: - CollectionView
-
- func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
- return listOfTemplate.count
- }
-
- func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
- let itemWidth: CGFloat = (collectionView.frame.width - (sectionInsets * 4) - CGFloat(numItems)) / CGFloat(numItems)
- let itemHeight: CGFloat = itemWidth + highLabelName
-
- collectionViewHeigth.constant = itemHeight + sectionInsets
-
- return CGSize(width: itemWidth, height: itemHeight)
- }
-
- func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
-
- let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "cell", for: indexPath)
-
- let template = listOfTemplate[indexPath.row]
-
- // image
- let imagePreview = cell.viewWithTag(100) as! UIImageView
- if template.preview != "" {
- let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + template.name + ".png"
- if FileManager.default.fileExists(atPath: fileNameLocalPath) {
- let imageURL = URL(fileURLWithPath: fileNameLocalPath)
- if let image = UIImage(contentsOfFile: imageURL.path) {
- imagePreview.image = image
- }
- } else {
- getImageFromTemplate(name: template.name, preview: template.preview, indexPath: indexPath)
- }
- }
-
- // name
- let name = cell.viewWithTag(200) as! UILabel
- name.text = template.name
- name.textColor = NCBrandColor.sharedInstance.backgroundView
-
- // select
- let imageSelect = cell.viewWithTag(300) as! UIImageView
- if selectTemplate != nil && selectTemplate?.name == template.name {
- cell.backgroundColor = NCBrandColor.sharedInstance.textView
- imageSelect.image = UIImage(named: "plus100")
- imageSelect.isHidden = false
- } else {
- cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView
- imageSelect.isHidden = true
- }
-
- return cell
- }
-
- func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
-
- let template = listOfTemplate[indexPath.row]
-
- selectTemplate = template
- fileNameExtension = template.ext
-
- collectionView.reloadData()
- }
-
- // MARK: - Action
-
- func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, buttonType: String, overwrite: Bool) {
-
- guard let serverUrl = serverUrl else {
- return
- }
-
- self.serverUrl = serverUrl
- if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
- fileNameFolder = "/"
- } else {
- fileNameFolder = (serverUrl as NSString).lastPathComponent
- }
-
- let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
- buttonDestinationFolder.title = fileNameFolder
-
- self.tableView.reloadData()
- }
-
- @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
-
- self.deselectFormRow(sender)
-
- let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
- let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
- let viewController = navigationController.topViewController as! NCSelect
-
- viewController.delegate = self
- viewController.hideButtonCreateFolder = false
- viewController.includeDirectoryE2EEncryption = false
- viewController.includeImages = false
- viewController.layoutViewSelect = k_layout_view_move
- viewController.selectFile = false
- viewController.titleButtonDone = NSLocalizedString("_select_", comment: "")
- viewController.type = ""
- navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
- self.present(navigationController, animated: true, completion: nil)
- }
-
- @objc func save() {
-
- guard let selectTemplate = self.selectTemplate else {
- return
- }
- templateIdentifier = selectTemplate.identifier
- let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
- guard var fileNameForm = rowFileName.value else {
- return
- }
-
- if fileNameForm as! String == "" {
- return
- } else {
-
- fileNameForm = (fileNameForm as! NSString).deletingPathExtension + "." + fileNameExtension
-
- if NCUtility.sharedInstance.getMetadataConflict(account: appDelegate.activeAccount, serverUrl: serverUrl, fileName: String(describing: fileNameForm)) != nil {
-
- let metadataForUpload = NCManageDatabase.sharedInstance.createMetadata(account: appDelegate.activeAccount, fileName: String(describing: fileNameForm), ocId: "", serverUrl: serverUrl, url: "", contentType: "")
-
- guard let conflictViewController = UIStoryboard(name: "NCCreateFormUploadConflict", bundle: nil).instantiateInitialViewController() as? NCCreateFormUploadConflict else { return }
- conflictViewController.textLabelDetailNewFile = NSLocalizedString("_now_", comment: "")
- conflictViewController.alwaysNewFileNameNumber = true
- conflictViewController.serverUrl = serverUrl
- conflictViewController.metadatasUploadInConflict = [metadataForUpload]
- conflictViewController.delegate = self
-
- self.present(conflictViewController, animated: true, completion: nil)
-
- } else {
-
- let fileNamePath = CCUtility.returnFileNamePath(fromFileName: String(describing: fileNameForm), serverUrl: serverUrl, activeUrl: appDelegate.activeUrl)!
- createDocument(fileNamePath: fileNamePath, fileName: String(describing: fileNameForm))
- }
- }
- }
-
- func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
-
- if metadatas == nil || metadatas?.count == 0 {
-
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
- self.cancel()
- }
-
- } else {
-
- let fileName = metadatas![0].fileName
- let fileNamePath = CCUtility.returnFileNamePath(fromFileName: fileName, serverUrl: serverUrl, activeUrl: appDelegate.activeUrl)!
-
- createDocument(fileNamePath: fileNamePath, fileName: fileName)
- }
- }
-
- func createDocument(fileNamePath: String, fileName: String) {
-
- if self.editorId == k_editor_text || self.editorId == k_editor_onlyoffice {
-
- var customUserAgent: String?
-
- if self.editorId == k_editor_onlyoffice {
- customUserAgent = NCUtility.sharedInstance.getCustomUserAgentOnlyOffice()
- }
-
- NCCommunication.shared.NCTextCreateFile(fileNamePath: fileNamePath, editorId: editorId, creatorId: creatorId, templateId: templateIdentifier, customUserAgent: customUserAgent) { (account, url, errorCode, errorMessage) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount {
-
- if url != nil && url!.count > 0 {
-
- var contentType = "text/markdown"
- if let directEditingCreators = NCManageDatabase.sharedInstance.getDirectEditingCreators(account: self.appDelegate.activeAccount) {
- for directEditingCreator in directEditingCreators {
- if directEditingCreator.ext == self.fileNameExtension {
- contentType = directEditingCreator.mimetype
- }
- }
- }
-
- self.dismiss(animated: true, completion: {
- let metadata = NCManageDatabase.sharedInstance.createMetadata(account: self.appDelegate.activeAccount, fileName: (fileName as NSString).deletingPathExtension + "." + self.fileNameExtension, ocId: CCUtility.createRandomString(12), serverUrl: self.serverUrl, url: url ?? "", contentType: contentType)
- self.appDelegate.activeMain.readFileReloadFolder()
- self.appDelegate.activeMain.shouldPerformSegue(metadata, selector: "")
- })
- }
-
- } else if errorCode != 0 {
- NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type:NCContentPresenter.messageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- }
-
- }
-
- if self.editorId == k_editor_collabora {
-
- NCCommunication.shared.createRichdocuments(path: fileNamePath, templateId: templateIdentifier) { (account, url, errorCode, errorDescription) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount && url != nil {
-
- self.dismiss(animated: true, completion: {
-
- let metadata = NCManageDatabase.sharedInstance.createMetadata(account: self.appDelegate.activeAccount, fileName: (fileName as NSString).deletingPathExtension + "." + self.fileNameExtension, ocId: CCUtility.createRandomString(12), serverUrl: self.serverUrl, url: url!, contentType: "")
-
- self.appDelegate.activeMain.shouldPerformSegue(metadata, selector: "")
- })
-
-
- } else if errorCode != 0 {
- NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- }
- }
- }
-
- @objc func cancel() {
-
- self.dismiss(animated: true, completion: nil)
- }
-
- // MARK: NC API
-
- func getTemplate() {
-
- indicator.color = NCBrandColor.sharedInstance.brand
- indicator.startAnimating()
-
- if self.editorId == k_editor_text || self.editorId == k_editor_onlyoffice {
-
- fileNameExtension = "md"
- var customUserAgent: String?
-
- if self.editorId == k_editor_onlyoffice {
- customUserAgent = NCUtility.sharedInstance.getCustomUserAgentOnlyOffice()
- }
-
- NCCommunication.shared.NCTextGetListOfTemplates(customUserAgent: customUserAgent) { (account, templates, errorCode, errorMessage) in
-
- self.indicator.stopAnimating()
-
- if errorCode == 0 && account == self.appDelegate.activeAccount {
-
- for template in templates {
-
- let temp = NCCommunicationEditorTemplates()
-
- temp.identifier = template.identifier
- temp.ext = template.ext
- temp.name = template.name
- temp.preview = template.preview
-
- self.listOfTemplate.append(temp)
-
- // default: template empty
- if temp.preview == "" {
- self.selectTemplate = temp
- self.fileNameExtension = template.ext
- self.navigationItem.rightBarButtonItem?.isEnabled = true
- }
- }
-
- // NOT ALIGNED getTemplatesRichdocuments
- if templates.count == 0 {
- let temp = NCCommunicationEditorTemplates()
-
- temp.identifier = ""
- if self.editorId == k_editor_text {
- temp.ext = "md"
- } else if self.editorId == k_editor_onlyoffice && self.typeTemplate == k_template_document {
- temp.ext = "docx"
- } else if self.editorId == k_editor_onlyoffice && self.typeTemplate == k_template_spreadsheet {
- temp.ext = "xlsx"
- } else if self.editorId == k_editor_onlyoffice && self.typeTemplate == k_template_presentation {
- temp.ext = "pptx"
- }
- temp.name = "Empty"
- temp.preview = ""
-
- self.listOfTemplate.append(temp)
-
- self.selectTemplate = temp
- self.fileNameExtension = temp.ext
- self.navigationItem.rightBarButtonItem?.isEnabled = true
- }
-
- self.collectionView.reloadData()
-
- } else if errorCode != 0 {
- NCContentPresenter.shared.messageNotification("_error_", description: errorMessage, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- }
-
- }
-
- if self.editorId == k_editor_collabora {
-
- NCCommunication.shared.getTemplatesRichdocuments(typeTemplate: typeTemplate) { (account, templates, errorCode, errorDescription) in
-
- self.indicator.stopAnimating()
- if errorCode == 0 && account == self.appDelegate.activeAccount {
-
- for template in templates! {
-
- let temp = NCCommunicationEditorTemplates()
- temp.identifier = "\(template.templateId)"
- temp.delete = template.delete
- temp.ext = template.ext
- temp.name = template.name
- temp.preview = template.preview
- temp.type = template.type
-
- self.listOfTemplate.append(temp)
-
- // default: template empty
- if temp.preview == "" {
- self.selectTemplate = temp
- self.fileNameExtension = temp.ext
- self.navigationItem.rightBarButtonItem?.isEnabled = true
- }
- }
-
- self.collectionView.reloadData()
-
- } else if errorCode != 0 {
- NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.error, errorCode: errorCode)
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- }
- }
- }
-
- func getImageFromTemplate(name: String, preview: String, indexPath: IndexPath) {
-
- let fileNameLocalPath = CCUtility.getDirectoryUserData() + "/" + name + ".png"
- NCCommunication.shared.download(serverUrlFileName: preview, fileNameLocalPath: fileNameLocalPath, requestHandler: { (_) in
-
- }, progressHandler: { (_) in
-
- }) { (account, etag, date, lenght, error, errorCode, errorDescription) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount {
- self.collectionView.reloadItems(at: [indexPath])
- } else if errorCode != 0 {
- print("\(errorCode)")
- } else {
- print("[LOG] It has been changed user during networking process, error.")
- }
- }
- }
- }
|