NCShareExtension+NCAccountRequestDelegate.swift 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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: NCAccountRequestDelegate {
  26. // MARK: - Account
  27. func showAccountPicker() {
  28. let accounts = self.database.getAllAccountOrderAlias()
  29. guard accounts.count > 1,
  30. let vcAccountRequest = UIStoryboard(name: "NCAccountRequest", bundle: nil).instantiateInitialViewController() as? NCAccountRequest else { return }
  31. // Only here change the active account
  32. for account in accounts {
  33. account.active = account.account == session.account
  34. }
  35. vcAccountRequest.activeAccount = self.session.account
  36. vcAccountRequest.accounts = accounts.sorted { sorg, dest -> Bool in
  37. return sorg.active && !dest.active
  38. }
  39. vcAccountRequest.enableTimerProgress = false
  40. vcAccountRequest.enableAddAccount = false
  41. vcAccountRequest.delegate = self
  42. vcAccountRequest.dismissDidEnterBackground = true
  43. let screenHeighMax = UIScreen.main.bounds.height - (UIScreen.main.bounds.height / 5)
  44. let height = min(CGFloat(accounts.count * Int(vcAccountRequest.heightCell) + 45), screenHeighMax)
  45. let popup = NCPopupViewController(contentController: vcAccountRequest, popupWidth: 300, popupHeight: height + 20)
  46. self.present(popup, animated: true)
  47. }
  48. func accountRequestAddAccount() { }
  49. func accountRequestChangeAccount(account: String, controller: UIViewController?) {
  50. guard let tableAccount = self.database.getTableAccount(predicate: NSPredicate(format: "account == %@", account)),
  51. let capabilities = self.database.setCapabilities(account: account) else {
  52. cancel(with: NCShareExtensionError.noAccount)
  53. return
  54. }
  55. self.account = account
  56. // COLORS
  57. NCBrandColor.shared.settingThemingColor(account: account)
  58. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeTheming, userInfo: ["account": account])
  59. // NETWORKING
  60. NextcloudKit.shared.setup(delegate: NCNetworking.shared)
  61. NextcloudKit.shared.appendSession(account: tableAccount.account,
  62. urlBase: tableAccount.urlBase,
  63. user: tableAccount.user,
  64. userId: tableAccount.userId,
  65. password: NCKeychain().getPassword(account: tableAccount.account),
  66. userAgent: userAgent,
  67. nextcloudVersion: capabilities.capabilityServerVersionMajor,
  68. groupIdentifier: NCBrandOptions.shared.capabilitiesGroup)
  69. // get auto upload folder
  70. autoUploadFileName = self.database.getAccountAutoUploadFileName()
  71. autoUploadDirectory = self.database.getAccountAutoUploadDirectory(session: session)
  72. serverUrl = utilityFileSystem.getHomeServer(session: session)
  73. reloadDatasource(withLoadFolder: true)
  74. setNavigationBar(navigationTitle: NCBrandOptions.shared.brand)
  75. }
  76. }
  77. extension NCShareExtension: NCCreateFormUploadConflictDelegate {
  78. func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
  79. guard let metadatas = metadatas else {
  80. uploadStarted = false
  81. uploadMetadata.removeAll()
  82. return
  83. }
  84. self.uploadMetadata.append(contentsOf: metadatas)
  85. self.upload()
  86. }
  87. }
  88. extension NCShareExtension: NCShareCellDelegate {
  89. func renameFile(named fileName: String, account: String) {
  90. let alert = UIAlertController.renameFile(fileName: fileName, account: account) { [self] newFileName in
  91. guard let fileIx = self.filesName.firstIndex(of: fileName),
  92. !self.filesName.contains(newFileName),
  93. utilityFileSystem.moveFile(atPath: (NSTemporaryDirectory() + fileName), toPath: (NSTemporaryDirectory() + newFileName)) else {
  94. return showAlert(title: "_single_file_conflict_title_", description: "'\(fileName)' -> '\(newFileName)'")
  95. }
  96. filesName[fileIx] = newFileName
  97. tableView.reloadData()
  98. }
  99. present(alert, animated: true)
  100. }
  101. func removeFile(named fileName: String) {
  102. guard let index = self.filesName.firstIndex(of: fileName) else {
  103. return showAlert(title: "_file_not_found_", description: fileName)
  104. }
  105. self.filesName.remove(at: index)
  106. if self.filesName.isEmpty {
  107. cancel(with: NCShareExtensionError.noFiles)
  108. } else {
  109. self.setCommandView()
  110. }
  111. }
  112. }