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