NCShareExtension+NCDelegate.swift 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. //
  2. // NCShareExtension.swift
  3. // Share
  4. //
  5. // Created by Marino Faggiana on 04.01.2022.
  6. // Copyright © 2022 Henrik Storch. All rights reserved.
  7. //
  8. // Author Henrik Storch <henrik.storch@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 NCCommunication
  24. import UIKit
  25. extension NCShareExtension: NCEmptyDataSetDelegate, NCAccountRequestDelegate {
  26. // MARK: - Empty
  27. func emptyDataSetView(_ view: NCEmptyView) {
  28. if networkInProgress {
  29. view.emptyImage.image = UIImage(named: "networkInProgress")?.image(color: .gray, size: UIScreen.main.bounds.width)
  30. view.emptyTitle.text = NSLocalizedString("_request_in_progress_", comment: "")
  31. view.emptyDescription.text = ""
  32. } else {
  33. view.emptyImage.image = UIImage(named: "folder")?.image(color: NCBrandColor.shared.brandElement, size: UIScreen.main.bounds.width)
  34. view.emptyTitle.text = NSLocalizedString("_files_no_folders_", comment: "")
  35. view.emptyDescription.text = ""
  36. }
  37. }
  38. // MARK: - Account
  39. func showAccountPicker() {
  40. let accounts = NCManageDatabase.shared.getAllAccountOrderAlias()
  41. guard accounts.count > 1,
  42. let vcAccountRequest = UIStoryboard(name: "NCAccountRequest", bundle: nil).instantiateInitialViewController() as? NCAccountRequest else { return }
  43. // Only here change the active account
  44. for account in accounts {
  45. account.active = account.account == self.activeAccount.account
  46. }
  47. vcAccountRequest.activeAccount = self.activeAccount
  48. vcAccountRequest.accounts = accounts.sorted { sorg, dest -> Bool in
  49. return sorg.active && !dest.active
  50. }
  51. vcAccountRequest.enableTimerProgress = false
  52. vcAccountRequest.enableAddAccount = false
  53. vcAccountRequest.delegate = self
  54. vcAccountRequest.dismissDidEnterBackground = true
  55. let screenHeighMax = UIScreen.main.bounds.height - (UIScreen.main.bounds.height / 5)
  56. let height = min(CGFloat(accounts.count * Int(vcAccountRequest.heightCell) + 45), screenHeighMax)
  57. let popup = NCPopupViewController(contentController: vcAccountRequest, popupWidth: 300, popupHeight: height + 20)
  58. self.present(popup, animated: true)
  59. }
  60. func accountRequestChangeAccount(account: String) {
  61. guard let activeAccount = NCManageDatabase.shared.getAccount(predicate: NSPredicate(format: "account == %@", account)) else {
  62. extensionContext?.cancelRequest(withError: NCShareExtensionError.noAccount)
  63. return
  64. }
  65. self.activeAccount = activeAccount
  66. // NETWORKING
  67. NCCommunicationCommon.shared.setup(
  68. account: activeAccount.account,
  69. user: activeAccount.user,
  70. userId: activeAccount.userId,
  71. password: CCUtility.getPassword(activeAccount.account),
  72. urlBase: activeAccount.urlBase,
  73. userAgent: CCUtility.getUserAgent(),
  74. webDav: NCUtilityFileSystem.shared.getWebDAV(account: activeAccount.account),
  75. nextcloudVersion: 0,
  76. delegate: NCNetworking.shared)
  77. // get auto upload folder
  78. autoUploadFileName = NCManageDatabase.shared.getAccountAutoUploadFileName()
  79. autoUploadDirectory = NCManageDatabase.shared.getAccountAutoUploadDirectory(urlBase: activeAccount.urlBase, account: activeAccount.account)
  80. serverUrl = NCUtilityFileSystem.shared.getHomeServer(account: activeAccount.account)
  81. layoutForView = NCUtility.shared.getLayoutForView(key: keyLayout, serverUrl: serverUrl)
  82. reloadDatasource(withLoadFolder: true)
  83. setNavigationBar(navigationTitle: NCBrandOptions.shared.brand)
  84. }
  85. }
  86. extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate, NCListCellDelegate {
  87. // Bug in Swift?
  88. var uploadStated: Bool { uploadStarted }
  89. func removeFile(named fileName: String) {
  90. guard let index = self.filesName.firstIndex(of: fileName) else {
  91. return showAlert(title: "_file_not_found_", description: fileName)
  92. }
  93. self.filesName.remove(at: index)
  94. if self.filesName.isEmpty {
  95. self.extensionContext?.cancelRequest(withError: NCShareExtensionError.noFiles)
  96. } else {
  97. self.setCommandView()
  98. }
  99. }
  100. func renameFile(named fileName: String) {
  101. guard let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile else { return }
  102. let resultInternalType = NCCommunicationCommon.shared.getInternalType(fileName: fileName, mimeType: "", directory: false)
  103. vcRename.delegate = self
  104. vcRename.fileName = fileName
  105. let img = UIImage(contentsOfFile: (NSTemporaryDirectory() + fileName)) ?? UIImage(named: resultInternalType.iconName) ?? NCBrandColor.cacheImages.file
  106. vcRename.imagePreview = img
  107. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  108. self.present(popup, animated: true)
  109. }
  110. func rename(fileName: String, fileNameNew: String) {
  111. guard let fileIx = self.filesName.firstIndex(of: fileName),
  112. !self.filesName.contains(fileNameNew),
  113. NCUtilityFileSystem.shared.moveFile(atPath: (NSTemporaryDirectory() + fileName), toPath: (NSTemporaryDirectory() + fileNameNew)) else {
  114. return showAlert(title: "_single_file_conflict_title_", description: "'\(fileName)' -> '\(fileNameNew)'")
  115. }
  116. filesName[fileIx] = fileNameNew
  117. tableView.reloadData()
  118. }
  119. }
  120. extension NCShareExtension: NCCreateFormUploadConflictDelegate {
  121. func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
  122. metadatas?.forEach { self.upload($0) }
  123. uploadDispatchGroup?.leave()
  124. }
  125. }