NCRenameFile.swift 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. //
  2. // NCRenameFile.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 26/02/21.
  6. // Copyright © 2021 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. import NCCommunication
  25. class NCRenameFile: UIViewController, UITextFieldDelegate {
  26. @IBOutlet weak var titleLabel: UILabel!
  27. @IBOutlet weak var separatorHeightContraint: NSLayoutConstraint!
  28. @IBOutlet weak var previewFile: UIImageView!
  29. @IBOutlet weak var fileNameWithoutExt: UITextField!
  30. @IBOutlet weak var point: UILabel!
  31. @IBOutlet weak var ext: UITextField!
  32. @IBOutlet weak var fileNameWithoutExtTrailingContraint: NSLayoutConstraint!
  33. @IBOutlet weak var cancelButton: UIButton!
  34. @IBOutlet weak var renameButton: UIButton!
  35. var metadata: tableMetadata?
  36. // MARK: - Life Cycle
  37. override func viewDidLoad() {
  38. super.viewDidLoad()
  39. if let metadata = self.metadata {
  40. titleLabel.text = NSLocalizedString("_rename_file_", comment: "")
  41. separatorHeightContraint.constant = 0.3
  42. fileNameWithoutExt.text = metadata.fileNameWithoutExt
  43. fileNameWithoutExt.delegate = self
  44. fileNameWithoutExt.becomeFirstResponder()
  45. ext.text = metadata.ext
  46. ext.delegate = self
  47. if metadata.directory {
  48. previewFile.image = NCCollectionCommon.images.cellFolderImage
  49. ext.isHidden = true
  50. point.isHidden = true
  51. fileNameWithoutExtTrailingContraint.constant = 20
  52. } else {
  53. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag)) {
  54. previewFile.image = UIImage(contentsOfFile: CCUtility.getDirectoryProviderStorageIconOcId(metadata.ocId, etag: metadata.etag))
  55. } else {
  56. if metadata.iconName.count > 0 {
  57. previewFile.image = UIImage.init(named: metadata.iconName)
  58. } else {
  59. previewFile.image = NCCollectionCommon.images.cellFileImage
  60. }
  61. }
  62. fileNameWithoutExtTrailingContraint.constant = 90
  63. }
  64. }
  65. previewFile.layer.cornerRadius = 10
  66. previewFile.layer.masksToBounds = true
  67. cancelButton.setTitle(NSLocalizedString("_cancel_", comment: ""), for: .normal)
  68. cancelButton.setTitleColor(.gray, for: .normal)
  69. cancelButton.layer.cornerRadius = 15
  70. cancelButton.layer.masksToBounds = true
  71. cancelButton.layer.backgroundColor = NCBrandColor.shared.graySoft.withAlphaComponent(0.2).cgColor
  72. cancelButton.layer.borderWidth = 0.3
  73. cancelButton.layer.borderColor = UIColor.gray.cgColor
  74. renameButton.setTitle(NSLocalizedString("_rename_", comment: ""), for: .normal)
  75. renameButton.setTitleColor(NCBrandColor.shared.brandText, for: .normal)
  76. renameButton.layer.cornerRadius = 15
  77. renameButton.layer.masksToBounds = true
  78. renameButton.layer.backgroundColor = NCBrandColor.shared.brand.cgColor
  79. }
  80. override func viewWillAppear(_ animated: Bool) {
  81. super.viewWillAppear(animated)
  82. }
  83. override func viewDidAppear(_ animated: Bool) {
  84. super.viewDidAppear(animated)
  85. if metadata == nil {
  86. dismiss(animated: true)
  87. }
  88. fileNameWithoutExt.selectAll(nil)
  89. }
  90. func textFieldShouldReturn(_ textField: UITextField) -> Bool {
  91. textField.resignFirstResponder()
  92. return true
  93. }
  94. // MARK: - Action
  95. @IBAction func cancel(_ sender: Any) {
  96. dismiss(animated: true)
  97. }
  98. @IBAction func rename(_ sender: Any) {
  99. guard let metadata = metadata else { return }
  100. var newFileNameWithoutExt = ""
  101. var newExt = ""
  102. var fileNameNew = ""
  103. if fileNameWithoutExt.text == nil || fileNameWithoutExt.text?.count == 0 {
  104. self.fileNameWithoutExt.text = metadata.fileNameWithoutExt
  105. return
  106. } else {
  107. newFileNameWithoutExt = fileNameWithoutExt.text!
  108. }
  109. if metadata.directory {
  110. fileNameNew = newFileNameWithoutExt
  111. renameMetadata(metadata, fileNameNew: fileNameNew)
  112. } else {
  113. if ext.text == nil || ext.text?.count == 0 {
  114. self.ext.text = metadata.ext
  115. return
  116. } else {
  117. newExt = ext.text!
  118. }
  119. if newExt != metadata.ext {
  120. let message = String(format: NSLocalizedString("_rename_ext_message_", comment: ""), newExt, metadata.ext)
  121. let alertController = UIAlertController(title: NSLocalizedString("_rename_ext_title_", comment: ""), message: message, preferredStyle: .alert)
  122. var title = NSLocalizedString("_use_", comment: "") + " ." + newExt
  123. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  124. fileNameNew = newFileNameWithoutExt + "." + newExt
  125. self.renameMetadata(metadata, fileNameNew: fileNameNew)
  126. }))
  127. title = NSLocalizedString("_keep_", comment: "") + " ." + metadata.ext
  128. alertController.addAction(UIAlertAction(title: title, style: .default, handler: { action in
  129. self.ext.text = metadata.ext
  130. }))
  131. self.present(alertController, animated: true)
  132. } else {
  133. fileNameNew = newFileNameWithoutExt + "." + newExt
  134. renameMetadata(metadata, fileNameNew: fileNameNew)
  135. }
  136. }
  137. }
  138. // MARK: - Networking
  139. func renameMetadata(_ metadata: tableMetadata, fileNameNew: String) {
  140. NCUtility.shared.startActivityIndicator(view: nil)
  141. NCNetworking.shared.renameMetadata(metadata, fileNameNew: fileNameNew, urlBase: metadata.urlBase, viewController: self) { (errorCode, errorDescription) in
  142. NCUtility.shared.stopActivityIndicator()
  143. if errorCode == 0 {
  144. self.dismiss(animated: true)
  145. } else {
  146. NCContentPresenter.shared.messageNotification("_error_", description: errorDescription, delay: NCGlobal.shared.dismissAfterSecond, type: NCContentPresenter.messageType.error, errorCode: errorCode)
  147. }
  148. }
  149. }
  150. }