NCShareExtension+NCAccountRequestDelegate.swift 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. // CAPABILITIES
  57. database.setCapabilities(account: account)
  58. // COLORS
  59. NCBrandColor.shared.settingThemingColor(account: account)
  60. NotificationCenter.default.postOnMainThread(name: NCGlobal.shared.notificationCenterChangeTheming, userInfo: ["account": account])
  61. // NETWORKING
  62. NextcloudKit.shared.setup(delegate: NCNetworking.shared)
  63. NextcloudKit.shared.appendSession(account: tableAccount.account,
  64. urlBase: tableAccount.urlBase,
  65. user: tableAccount.user,
  66. userId: tableAccount.userId,
  67. password: NCKeychain().getPassword(account: tableAccount.account),
  68. userAgent: userAgent,
  69. nextcloudVersion: capabilities.capabilityServerVersionMajor,
  70. groupIdentifier: NCBrandOptions.shared.capabilitiesGroup)
  71. // SESSION
  72. NCSession.shared.appendSession(account: tableAccount.account, urlBase: tableAccount.urlBase, user: tableAccount.user, userId: tableAccount.userId)
  73. // get auto upload folder
  74. autoUploadFileName = self.database.getAccountAutoUploadFileName()
  75. autoUploadDirectory = self.database.getAccountAutoUploadDirectory(session: session)
  76. serverUrl = utilityFileSystem.getHomeServer(session: session)
  77. reloadDatasource(withLoadFolder: true)
  78. setNavigationBar(navigationTitle: NCBrandOptions.shared.brand)
  79. }
  80. }
  81. extension NCShareExtension: NCCreateFormUploadConflictDelegate {
  82. func dismissCreateFormUploadConflict(metadatas: [tableMetadata]?) {
  83. guard let metadatas = metadatas else {
  84. uploadStarted = false
  85. uploadMetadata.removeAll()
  86. return
  87. }
  88. self.uploadMetadata.append(contentsOf: metadatas)
  89. self.upload()
  90. }
  91. }
  92. extension NCShareExtension: NCShareCellDelegate {
  93. func showRenameFileDialog(named fileName: String, account: String) {
  94. let alert = UIAlertController.renameFile(fileName: fileName, account: account) { [self] newFileName in
  95. renameFile(oldName: fileName, newName: newFileName, account: account)
  96. }
  97. present(alert, animated: true)
  98. }
  99. func renameFile(oldName: String, newName: String, account: String) {
  100. guard let fileIx = self.filesName.firstIndex(of: oldName),
  101. !self.filesName.contains(newName),
  102. utilityFileSystem.moveFile(atPath: (NSTemporaryDirectory() + oldName), toPath: (NSTemporaryDirectory() + newName)) else {
  103. return showAlert(title: "_single_file_conflict_title_", description: "'\(oldName)' -> '\(newName)'")
  104. }
  105. filesName[fileIx] = newName
  106. tableView.reloadData()
  107. }
  108. func removeFile(named fileName: String) {
  109. guard let index = self.filesName.firstIndex(of: fileName) else {
  110. return showAlert(title: "_file_not_found_", description: fileName)
  111. }
  112. self.filesName.remove(at: index)
  113. if self.filesName.isEmpty {
  114. cancel(with: NCShareExtensionError.noFiles)
  115. } else {
  116. self.setCommandView()
  117. }
  118. }
  119. }