NCManageAutoUploadFileName.swift 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. //
  2. // NCManageAutoUploadFileName.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 19/07/17.
  6. // Copyright (c) 2017 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 Photos
  25. import NextcloudKit
  26. class NCManageAutoUploadFileName: XLFormViewController {
  27. let appDelegate = (UIApplication.shared.delegate as? AppDelegate)!
  28. let dateExample = Date()
  29. func initializeForm() {
  30. let form: XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  31. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  32. var section: XLFormSectionDescriptor
  33. var row: XLFormRowDescriptor
  34. // Section Mode filename
  35. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  36. form.addFormSection(section)
  37. // Maintain the original fileName
  38. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  39. row.value = NCKeychain().getOriginalFileName(key: NCGlobal.shared.keyFileNameOriginalAutoUpload)
  40. row.cellConfig["backgroundColor"] = UIColor.secondarySystemGroupedBackground
  41. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  42. row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.textColor
  43. section.addFormRow(row)
  44. // Add File Name Type
  45. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  46. row.value = NCKeychain().getFileNameType(key: NCGlobal.shared.keyFileNameAutoUploadType)
  47. row.hidden = "$\("maintainOriginalFileName") == 1"
  48. row.cellConfig["backgroundColor"] = UIColor.secondarySystemGroupedBackground
  49. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  50. row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.textColor
  51. section.addFormRow(row)
  52. // Section: Rename File Name
  53. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  54. form.addFormSection(section)
  55. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeText, title: (NSLocalizedString("_filename_", comment: "")))
  56. let fileNameMask: String = NCKeychain().getFileNameMask(key: NCGlobal.shared.keyFileNameAutoUploadMask)
  57. if !fileNameMask.isEmpty {
  58. row.value = fileNameMask
  59. }
  60. row.hidden = "$\("maintainOriginalFileName") == 1"
  61. row.cellConfig["backgroundColor"] = UIColor.secondarySystemGroupedBackground
  62. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  63. row.cellConfig["textLabel.textColor"] = NCBrandColor.shared.textColor
  64. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  65. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  66. row.cellConfig["textField.textColor"] = NCBrandColor.shared.textColor
  67. section.addFormRow(row)
  68. // Section: Preview File Name
  69. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  70. row.height = 180
  71. row.disabled = true
  72. row.cellConfig["backgroundColor"] = UIColor.secondarySystemGroupedBackground
  73. row.cellConfig["textView.backgroundColor"] = UIColor.secondarySystemGroupedBackground
  74. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  75. row.cellConfig["textView.textColor"] = NCBrandColor.shared.textColor
  76. section.addFormRow(row)
  77. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  78. self.form = form
  79. }
  80. // MARK: - View Life Cycle
  81. override func viewDidLoad() {
  82. super.viewDidLoad()
  83. self.title = NSLocalizedString("_mode_filename_", comment: "")
  84. view.backgroundColor = .systemGroupedBackground
  85. tableView.backgroundColor = .systemGroupedBackground
  86. initializeForm()
  87. reloadForm()
  88. }
  89. // MARK: XLForm
  90. func reloadForm() {
  91. self.form.delegate = nil
  92. let maskFileName: XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  93. let previewFileName: XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  94. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  95. self.tableView.reloadData()
  96. self.form.delegate = self
  97. }
  98. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  99. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  100. if formRow.tag == "addFileNameType" {
  101. NCKeychain().setFileNameType(key: NCGlobal.shared.keyFileNameAutoUploadType, prefix: (formRow.value! as AnyObject).boolValue)
  102. self.reloadForm()
  103. } else if formRow.tag == "maintainOriginalFileName" {
  104. NCKeychain().setOriginalFileName(key: NCGlobal.shared.keyFileNameOriginalAutoUpload, value: (formRow.value! as AnyObject).boolValue)
  105. self.reloadForm()
  106. } else if formRow.tag == "maskFileName" {
  107. let fileName = formRow.value as? String
  108. self.form.delegate = nil
  109. if let fileName = fileName {
  110. formRow.value = NCUtility().removeForbiddenCharacters(fileName)
  111. }
  112. self.form.delegate = self
  113. let previewFileName: XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  114. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  115. // reload cell
  116. if fileName != nil {
  117. if newValue as? String != formRow.value as? String {
  118. self.reloadFormRow(formRow)
  119. let errorDescription = String(format: NSLocalizedString("_forbidden_characters_", comment: ""), NCGlobal.shared.forbiddenCharacters.joined(separator: " "))
  120. let error = NKError(errorCode: NCGlobal.shared.errorConflict, errorDescription: errorDescription)
  121. NCContentPresenter().showInfo(error: error)
  122. }
  123. }
  124. self.reloadFormRow(previewFileName)
  125. }
  126. }
  127. // MARK: - Utility
  128. func previewFileName(valueRename: String?) -> String {
  129. var returnString: String = ""
  130. if NCKeychain().getOriginalFileName(key: NCGlobal.shared.keyFileNameOriginalAutoUpload) {
  131. return (NSLocalizedString("_filename_", comment: "") + ": IMG_0001.JPG")
  132. } else if let valueRename = valueRename {
  133. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  134. if valueRenameTrimming.isEmpty {
  135. NCKeychain().setFileNameMask(key: NCGlobal.shared.keyFileNameAutoUploadMask, mask: "")
  136. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: NCGlobal.shared.keyFileNameAutoUploadType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginalAutoUpload, forcedNewFileName: false)
  137. } else {
  138. self.form.delegate = nil
  139. NCKeychain().setFileNameMask(key: NCGlobal.shared.keyFileNameAutoUploadMask, mask: valueRename)
  140. self.form.delegate = self
  141. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: NCGlobal.shared.keyFileNameAutoUploadMask, keyFileNameType: NCGlobal.shared.keyFileNameAutoUploadType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginalAutoUpload, forcedNewFileName: false)
  142. }
  143. } else {
  144. NCKeychain().setFileNameMask(key: NCGlobal.shared.keyFileNameAutoUploadMask, mask: "")
  145. returnString = CCUtility.createFileName("IMG_0001.JPG", fileDate: dateExample, fileType: PHAssetMediaType.image, keyFileName: nil, keyFileNameType: NCGlobal.shared.keyFileNameAutoUploadType, keyFileNameOriginal: NCGlobal.shared.keyFileNameOriginalAutoUpload, forcedNewFileName: false)
  146. }
  147. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  148. }
  149. }