NCShareExtension+NCDelegate.swift 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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. cancel(with: NCShareExtensionError.noAccount)
  63. return
  64. }
  65. self.activeAccount = activeAccount
  66. // COLORS
  67. NCBrandColor.shared.settingThemingColor(account: activeAccount.account)
  68. NCBrandColor.shared.createUserColors()
  69. // NETWORKING
  70. NCCommunicationCommon.shared.setup(
  71. account: activeAccount.account,
  72. user: activeAccount.user,
  73. userId: activeAccount.userId,
  74. password: CCUtility.getPassword(activeAccount.account),
  75. urlBase: activeAccount.urlBase,
  76. userAgent: CCUtility.getUserAgent(),
  77. webDav: NCUtilityFileSystem.shared.getWebDAV(account: activeAccount.account),
  78. nextcloudVersion: 0,
  79. delegate: NCNetworking.shared)
  80. // get auto upload folder
  81. autoUploadFileName = NCManageDatabase.shared.getAccountAutoUploadFileName()
  82. autoUploadDirectory = NCManageDatabase.shared.getAccountAutoUploadDirectory(urlBase: activeAccount.urlBase, account: activeAccount.account)
  83. serverUrl = NCUtilityFileSystem.shared.getHomeServer(account: activeAccount.account)
  84. layoutForView = NCUtility.shared.getLayoutForView(key: keyLayout, serverUrl: serverUrl)
  85. reloadDatasource(withLoadFolder: true)
  86. setNavigationBar(navigationTitle: NCBrandOptions.shared.brand)
  87. }
  88. }
  89. extension NCShareExtension: NCShareCellDelegate, NCRenameFileDelegate, NCListCellDelegate {
  90. func removeFile(named fileName: String) {
  91. guard let index = self.filesName.firstIndex(of: fileName) else {
  92. return showAlert(title: "_file_not_found_", description: fileName)
  93. }
  94. self.filesName.remove(at: index)
  95. if self.filesName.isEmpty {
  96. cancel(with: NCShareExtensionError.noFiles)
  97. } else {
  98. self.setCommandView()
  99. }
  100. }
  101. func renameFile(named fileName: String) {
  102. guard let vcRename = UIStoryboard(name: "NCRenameFile", bundle: nil).instantiateInitialViewController() as? NCRenameFile else { return }
  103. let resultInternalType = NCCommunicationCommon.shared.getInternalType(fileName: fileName, mimeType: "", directory: false)
  104. vcRename.delegate = self
  105. vcRename.fileName = fileName
  106. if let previewImage = UIImage.downsample(imageAt: URL(fileURLWithPath: NSTemporaryDirectory() + fileName), to: CGSize(width: 140, height: 140)) {
  107. vcRename.imagePreview = previewImage
  108. } else {
  109. vcRename.imagePreview = UIImage(named: resultInternalType.iconName) ?? NCBrandColor.cacheImages.file
  110. }
  111. let popup = NCPopupViewController(contentController: vcRename, popupWidth: vcRename.width, popupHeight: vcRename.height)
  112. self.present(popup, animated: true)
  113. }
  114. func rename(fileName: String, fileNameNew: String) {
  115. guard fileName != fileNameNew else { return }
  116. guard let fileIx = self.filesName.firstIndex(of: fileName),
  117. !self.filesName.contains(fileNameNew),
  118. NCUtilityFileSystem.shared.moveFile(atPath: (NSTemporaryDirectory() + fileName), toPath: (NSTemporaryDirectory() + fileNameNew)) else {
  119. return showAlert(title: "_single_file_conflict_title_", description: "'\(fileName)' -> '\(fileNameNew)'")
  120. }
  121. filesName[fileIx] = fileNameNew
  122. tableView.reloadData()
  123. }
  124. }
  125. extension NCShareExtension: NCCreateFormUploadConflictDelegate {
  126. func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
  127. guard let metadatas = metadatas else {
  128. uploadStarted = false
  129. uploadMetadata.removeAll()
  130. return
  131. }
  132. self.uploadMetadata.append(contentsOf: metadatas)
  133. self.upload()
  134. }
  135. }