CCActions.swift 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  1. //
  2. // CCActions.swift
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 06/02/17.
  6. // Copyright (c) 2017 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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. @objc protocol CCActionsRenameDelegate {
  25. func renameSuccess(_ metadataNet: CCMetadataNet)
  26. func renameMoveFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger)
  27. }
  28. @objc protocol CCActionsSearchDelegate {
  29. func searchSuccessFailure(_ metadataNet: CCMetadataNet, metadatas: [Any], message: NSString, errorCode: NSInteger)
  30. }
  31. @objc protocol CCActionsListingFavoritesDelegate {
  32. func listingFavoritesSuccessFailure(_ metadataNet: CCMetadataNet, metadatas: [Any], message: NSString, errorCode: NSInteger)
  33. }
  34. class CCActions: NSObject {
  35. //MARK: Shared Instance
  36. @objc static let sharedInstance: CCActions = {
  37. let instance = CCActions()
  38. return instance
  39. }()
  40. //MARK: Local Variable
  41. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  42. //MARK: Init
  43. override init() {
  44. }
  45. // --------------------------------------------------------------------------------------------
  46. // MARK: Rename File or Folder
  47. // --------------------------------------------------------------------------------------------
  48. @objc func renameFileOrFolder(_ metadata: tableMetadata, fileName: String, delegate: AnyObject) {
  49. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  50. let fileName = CCUtility.removeForbiddenCharactersServer(fileName)!
  51. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  52. return
  53. }
  54. if fileName.count == 0 {
  55. return
  56. }
  57. if metadata.fileNameView == fileName {
  58. return
  59. }
  60. // Verify if exists the fileName TO
  61. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: self.appDelegate.activeUser, withUserID: self.appDelegate.activeUserID, withPassword: self.appDelegate.activePassword, withUrl: self.appDelegate.activeUrl)
  62. ocNetworking?.readFile(fileName, serverUrl: serverUrl, account: self.appDelegate.activeAccount, success: { (metadata) in
  63. let alertController = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: UIAlertControllerStyle.alert)
  64. let okAction = UIAlertAction(title: "OK", style: UIAlertActionStyle.default) {
  65. (result : UIAlertAction) -> Void in
  66. }
  67. alertController.addAction(okAction)
  68. delegate.present(alertController, animated: true, completion: nil)
  69. }, failure: { (message, errorCode) in
  70. metadataNet.action = actionMoveFileOrFolder
  71. metadataNet.delegate = delegate
  72. metadataNet.directory = metadata.directory
  73. metadataNet.fileID = metadata.fileID
  74. metadataNet.fileName = metadata.fileName
  75. metadataNet.fileNameTo = fileName
  76. metadataNet.fileNameView = metadata.fileNameView
  77. metadataNet.selector = selectorRename
  78. metadataNet.serverUrl = serverUrl
  79. metadataNet.serverUrlTo = serverUrl
  80. self.appDelegate.addNetworkingOperationQueue(self.appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  81. })
  82. }
  83. @objc func renameSuccess(_ metadataNet: CCMetadataNet) {
  84. // Rename metadata
  85. _ = NCManageDatabase.sharedInstance.renameMetadata(fileNameTo: metadataNet.fileNameTo, fileID: metadataNet.fileID)
  86. if metadataNet.directory == true {
  87. let serverUrl = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addFileName: metadataNet.fileName)
  88. let serverUrlTo = CCUtility.stringAppendServerUrl(metadataNet.serverUrl, addFileName: metadataNet.fileNameTo)
  89. guard let directoryTable = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "serverUrl == %@", serverUrl!)) else {
  90. metadataNet.delegate?.renameMoveFileOrFolderFailure(metadataNet, message: "Internal error, ServerUrl not found" as NSString, errorCode: 0)
  91. return
  92. }
  93. NCManageDatabase.sharedInstance.setDirectory(serverUrl: serverUrl!, serverUrlTo: serverUrlTo!, etag: nil, fileID: nil, encrypted: directoryTable.e2eEncrypted)
  94. } else {
  95. NCManageDatabase.sharedInstance.setLocalFile(fileID: metadataNet.fileID, date: nil, exifDate: nil, exifLatitude: nil, exifLongitude: nil, fileName: metadataNet.fileNameTo, etag: nil)
  96. // Move file system
  97. do {
  98. try FileManager.default.moveItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadataNet.fileID) + "/" + metadataNet.fileName, toPath: CCUtility.getDirectoryProviderStorageFileID(metadataNet.fileID) + "/" + metadataNet.fileNameTo)
  99. } catch let error {
  100. print("error: \(error)")
  101. }
  102. do {
  103. try FileManager.default.moveItem(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadataNet.fileID, fileNameView: metadataNet.fileName), toPath: CCUtility.getDirectoryProviderStorageIconFileID(metadataNet.fileID, fileNameView: metadataNet.fileNameTo))
  104. } catch let error {
  105. print("error: \(error)")
  106. }
  107. }
  108. metadataNet.delegate?.renameSuccess(metadataNet)
  109. }
  110. @objc func renameMoveFileOrFolderFailure(_ metadataNet: CCMetadataNet, message: NSString, errorCode: NSInteger) {
  111. if message.length > 0 {
  112. var title : String = ""
  113. if metadataNet.selector == selectorRename {
  114. title = "_delete_"
  115. }
  116. if metadataNet.selector == selectorMove {
  117. title = "_move_"
  118. }
  119. appDelegate.messageNotification(title, description: message as String, visible: true, delay:TimeInterval(k_dismissAfterSecond), type:TWMessageBarMessageType.error, errorCode: errorCode)
  120. }
  121. metadataNet.delegate?.renameMoveFileOrFolderFailure(metadataNet, message: message as NSString, errorCode: errorCode)
  122. }
  123. // --------------------------------------------------------------------------------------------
  124. // MARK: Search
  125. // --------------------------------------------------------------------------------------------
  126. @objc func search(_ serverUrl: String, fileName: String, etag: String, depth: String, date: Date?, contenType: [String]?, selector: String, delegate: AnyObject) {
  127. guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(serverUrl) else {
  128. return
  129. }
  130. // Search DAV API
  131. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  132. metadataNet.action = actionSearch
  133. metadataNet.contentType = contenType
  134. metadataNet.date = date
  135. metadataNet.delegate = delegate
  136. metadataNet.directoryID = directoryID
  137. metadataNet.fileName = fileName
  138. metadataNet.etag = etag
  139. metadataNet.depth = depth
  140. metadataNet.priority = Operation.QueuePriority.high.rawValue
  141. metadataNet.selector = selector
  142. metadataNet.serverUrl = serverUrl
  143. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  144. }
  145. @objc func searchSuccessFailure(_ metadataNet: CCMetadataNet, metadatas: [tableMetadata], message: NSString, errorCode: NSInteger) {
  146. metadataNet.delegate?.searchSuccessFailure(metadataNet, metadatas: metadatas, message: message, errorCode: errorCode)
  147. }
  148. // --------------------------------------------------------------------------------------------
  149. // MARK: Linsting Favorites
  150. // --------------------------------------------------------------------------------------------
  151. @objc func listingFavorites(_ serverUrl: String, delegate: AnyObject) {
  152. let metadataNet: CCMetadataNet = CCMetadataNet.init(account: appDelegate.activeAccount)
  153. metadataNet.action = actionListingFavorites
  154. metadataNet.delegate = delegate
  155. metadataNet.serverUrl = serverUrl
  156. appDelegate.addNetworkingOperationQueue(appDelegate.netQueue, delegate: self, metadataNet: metadataNet)
  157. }
  158. @objc func listingFavoritesSuccessFailure(_ metadataNet: CCMetadataNet, metadatas: [tableMetadata], message: NSString, errorCode: NSInteger) {
  159. metadataNet.delegate?.listingFavoritesSuccessFailure(metadataNet, metadatas: metadatas, message: message, errorCode: errorCode)
  160. }
  161. }