NCShareExtension+NCDelegate.swift 6.8 KB

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