NCCreateFormUploadFileText.swift 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. //
  2. // NCCreateFormUploadFileText.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 14/11/2018.
  6. // Copyright © 2018 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. class NCCreateFormUploadFileText: XLFormViewController, NCSelectDelegate {
  25. var serverUrl = ""
  26. var titleServerUrl = ""
  27. var fileName = ""
  28. var text = ""
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. convenience init(serverUrl: String, text: String, fileName: String) {
  31. self.init()
  32. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  33. titleServerUrl = "/"
  34. } else {
  35. titleServerUrl = (serverUrl as NSString).lastPathComponent
  36. }
  37. self.fileName = fileName
  38. self.serverUrl = serverUrl
  39. self.text = text
  40. }
  41. // MARK: - View Life Cycle
  42. override func viewDidLoad() {
  43. super.viewDidLoad()
  44. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  45. self.navigationItem.rightBarButtonItem = saveButton
  46. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  47. // Theming view
  48. NotificationCenter.default.addObserver(self, selector: #selector(changeTheming), name: NSNotification.Name(rawValue: k_notificationCenter_changeTheming), object: nil)
  49. changeTheming()
  50. }
  51. @objc func changeTheming() {
  52. appDelegate.changeTheming(self, tableView: tableView, collectionView: nil, form: true)
  53. initializeForm()
  54. }
  55. //MARK: XLForm
  56. func initializeForm() {
  57. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_text_upload_title_", comment: "")) as XLFormDescriptor
  58. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  59. var section : XLFormSectionDescriptor
  60. var row : XLFormRowDescriptor
  61. // Section: Destination Folder
  62. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  63. form.addFormSection(section)
  64. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  65. row.action.formSelector = #selector(changeDestinationFolder(_:))
  66. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  67. row.cellConfig["imageView.image"] = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, width: 50, height: 50, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  68. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  69. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  70. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  71. section.addFormRow(row)
  72. // Section: File Name
  73. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  74. form.addFormSection(section)
  75. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  76. row.value = self.fileName
  77. row.cellConfig["backgroundColor"] = NCBrandColor.sharedInstance.backgroundForm
  78. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  79. row.cellConfig["textLabel.textColor"] = NCBrandColor.sharedInstance.textView
  80. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  81. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  82. row.cellConfig["textField.textColor"] = NCBrandColor.sharedInstance.textView
  83. section.addFormRow(row)
  84. self.form = form
  85. }
  86. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  87. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  88. if formRow.tag == "fileName" {
  89. self.form.delegate = nil
  90. if let fileNameNew = formRow.value {
  91. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as? String)
  92. }
  93. formRow.value = self.fileName
  94. self.title = fileName
  95. self.updateFormRow(formRow)
  96. self.form.delegate = self
  97. }
  98. }
  99. override func textFieldDidBeginEditing(_ textField: UITextField) {
  100. let cell = textField.formDescriptorCell()
  101. let tag = cell?.rowDescriptor.tag
  102. if tag == "fileName" {
  103. CCUtility.selectFileName(from: textField)
  104. }
  105. }
  106. // MARK: - Action
  107. func dismissSelect(serverUrl: String?, metadata: tableMetadata?, type: String, buttonType: String) {
  108. if serverUrl != nil {
  109. self.serverUrl = serverUrl!
  110. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  111. self.titleServerUrl = "/"
  112. } else {
  113. self.titleServerUrl = (serverUrl! as NSString).lastPathComponent
  114. }
  115. // Update
  116. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  117. row.title = self.titleServerUrl
  118. self.updateFormRow(row)
  119. }
  120. }
  121. @objc func save() {
  122. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  123. guard let name = rowFileName.value else {
  124. return
  125. }
  126. let ext = (name as! NSString).pathExtension.uppercased()
  127. var fileNameSave = ""
  128. if (ext == "") {
  129. fileNameSave = name as! String + ".txt"
  130. } else if (CCUtility.isDocumentModifiableExtension(ext)) {
  131. fileNameSave = name as! String
  132. } else {
  133. fileNameSave = (name as! NSString).deletingPathExtension + ".txt"
  134. }
  135. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameView == %@", appDelegate.activeAccount, self.serverUrl, fileNameSave))
  136. if (metadata != nil) {
  137. let alertController = UIAlertController(title: fileNameSave, message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: .alert)
  138. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in
  139. }
  140. let overwriteAction = UIAlertAction(title: NSLocalizedString("_overwrite_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  141. self.dismissAndUpload(fileNameSave, ocId: metadata!.ocId, serverUrl: self.serverUrl)
  142. }
  143. alertController.addAction(cancelAction)
  144. alertController.addAction(overwriteAction)
  145. self.present(alertController, animated: true, completion:nil)
  146. } else {
  147. let ocId = CCUtility.createMetadataID(fromAccount: appDelegate.activeAccount, serverUrl: self.serverUrl, fileNameView: fileNameSave, directory: false)!
  148. dismissAndUpload(fileNameSave, ocId: ocId, serverUrl: serverUrl)
  149. }
  150. }
  151. func dismissAndUpload(_ fileNameSave: String, ocId: String, serverUrl: String) {
  152. self.dismiss(animated: true, completion: {
  153. let data = self.text.data(using: .utf8)
  154. let success = FileManager.default.createFile(atPath: CCUtility.getDirectoryProviderStorageOcId(ocId, fileNameView: fileNameSave), contents: data, attributes: nil)
  155. if success {
  156. let metadataForUpload = tableMetadata()
  157. metadataForUpload.account = self.appDelegate.activeAccount
  158. metadataForUpload.date = NSDate()
  159. metadataForUpload.ocId = ocId
  160. metadataForUpload.fileName = fileNameSave
  161. metadataForUpload.fileNameView = fileNameSave
  162. metadataForUpload.serverUrl = serverUrl
  163. metadataForUpload.session = k_upload_session
  164. metadataForUpload.sessionSelector = selectorUploadFile
  165. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  166. NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  167. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, ocId: nil, action: Int32(k_action_NULL))
  168. self.appDelegate.startLoadAutoDownloadUpload()
  169. } else {
  170. NCContentPresenter.shared.messageNotification("_error_", description: "_error_creation_file_", delay: TimeInterval(k_dismissAfterSecond), type: NCContentPresenter.messageType.info, errorCode: 0)
  171. }
  172. })
  173. }
  174. func cancel() {
  175. self.dismiss(animated: true, completion: nil)
  176. }
  177. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  178. self.deselectFormRow(sender)
  179. let storyboard = UIStoryboard(name: "NCSelect", bundle: nil)
  180. let navigationController = storyboard.instantiateInitialViewController() as! UINavigationController
  181. let viewController = navigationController.topViewController as! NCSelect
  182. viewController.delegate = self
  183. viewController.hideButtonCreateFolder = false
  184. viewController.includeDirectoryE2EEncryption = true
  185. viewController.includeImages = false
  186. viewController.layoutViewSelect = k_layout_view_move
  187. viewController.selectFile = false
  188. viewController.titleButtonDone = NSLocalizedString("_select_", comment: "")
  189. viewController.type = ""
  190. navigationController.modalPresentationStyle = UIModalPresentationStyle.fullScreen
  191. self.present(navigationController, animated: true, completion: nil)
  192. }
  193. }