DocumentPickerViewController.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. //
  2. // DocumentPickerViewController.swift
  3. // Picker
  4. //
  5. // Created by Marino Faggiana on 27/12/16.
  6. // Copyright © 2016 TWS. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  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 UIKit
  24. class DocumentPickerViewController: UIDocumentPickerExtensionViewController, CCNetworkingDelegate, OCNetworkingDelegate {
  25. // MARK: - Properties
  26. var provider : providerSession?
  27. var metadata : CCMetadata?
  28. var recordsTableMetadata : [TableMetadata]?
  29. var titleFolder : String?
  30. var activeAccount : String?
  31. var activeUrl : String?
  32. var activeUser : String?
  33. var activePassword : String?
  34. var activeUID : String?
  35. var activeAccessToken : String?
  36. var directoryUser : String?
  37. var typeCloud : String?
  38. var serverUrl : String?
  39. var localServerUrl : String?
  40. lazy var networkingOperationQueue : OperationQueue = {
  41. var queue = OperationQueue()
  42. queue.name = "it.twsweb.cryptocloud.queue"
  43. queue.maxConcurrentOperationCount = 1
  44. return queue
  45. }()
  46. var hud : CCHud!
  47. // MARK: - IBOutlets
  48. @IBOutlet weak var tableView: UITableView!
  49. // MARK: - View Life Cycle
  50. override func viewDidLoad() {
  51. provider = providerSession.sharedInstance
  52. if let record = CCCoreData.getActiveAccount() {
  53. activeAccount = record.account!
  54. activePassword = record.password!
  55. activeUrl = record.url!
  56. activeUser = record.user!
  57. typeCloud = record.typeCloud!
  58. directoryUser = CCUtility.getDirectoryActiveUser(activeUser!, activeUrl: activeUrl!)
  59. if (localServerUrl == nil) {
  60. localServerUrl = CCUtility.getHomeServerUrlActiveUrl(activeUrl!, typeCloud: typeCloud!)
  61. } else {
  62. self.navigationItem.title = titleFolder
  63. }
  64. } else {
  65. // Close error no account return nil
  66. let deadlineTime = DispatchTime.now() + 0.1
  67. DispatchQueue.main.asyncAfter(deadline: deadlineTime) {
  68. let alert = UIAlertController(title: NSLocalizedString("_error_", comment: ""), message: NSLocalizedString("_no_active_account_", comment: ""), preferredStyle: .alert)
  69. alert.addAction(UIAlertAction(title: NSLocalizedString("_ok_", comment: ""), style: .default) { action in
  70. self.dismissGrantingAccess(to: nil)
  71. })
  72. self.present(alert, animated: true, completion: nil)
  73. }
  74. return
  75. }
  76. // MARK: - init Object
  77. CCNetworking.shared().settingDelegate(self)
  78. hud = CCHud.init(view: self.navigationController?.view)
  79. // COLOR_SEPARATOR_TABLE
  80. self.tableView.separatorColor = UIColor(colorLiteralRed: 153.0/255.0, green: 153.0/255.0, blue: 153.0/255.0, alpha: 0.2)
  81. readFolder()
  82. }
  83. // MARK: - Read folder
  84. func readFolder() {
  85. let metadataNet = CCMetadataNet()
  86. metadataNet.account = activeAccount
  87. metadataNet.action = actionReadFolder
  88. metadataNet.serverUrl = self.localServerUrl;
  89. metadataNet.selector = selectorReadFolder;
  90. metadataNet.date = nil;
  91. let ocNetworking : OCnetworking = OCnetworking.init(delegate: self, metadataNet: metadataNet, withUser: activeUser, withPassword: activePassword, withUrl: activeUrl, withTypeCloud: typeCloud, oneByOne: true, activityIndicator: false)
  92. networkingOperationQueue.addOperation(ocNetworking)
  93. hud.visibleIndeterminateHud()
  94. }
  95. func readFolderFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  96. hud.hideHud()
  97. print ("error")
  98. }
  99. func readFolderSuccess(_ metadataNet: CCMetadataNet!, permissions: String!, rev: String!, metadatas: [Any]!) {
  100. // remove all record
  101. let predicate = NSPredicate(format: "(account == %@) AND (directoryID == %@) AND ((session == NULL) OR (session == ''))", activeAccount!, metadataNet.directoryID)
  102. CCCoreData.deleteMetadata(with: predicate)
  103. for metadata in metadatas as! [CCMetadata] {
  104. // do not insert crypto file
  105. if CCUtility.isCryptoString(metadata.fileName) {
  106. continue
  107. }
  108. // plist + crypto = completed ?
  109. if CCUtility.isCryptoPlistString(metadata.fileName) && metadata.directory == false {
  110. var isCryptoComplete = false
  111. for completeMetadata in metadatas as! [CCMetadata] {
  112. if completeMetadata.fileName == CCUtility.trasformedFileNamePlist(inCrypto: metadata.fileName) {
  113. isCryptoComplete = true
  114. }
  115. }
  116. if isCryptoComplete == false {
  117. continue
  118. }
  119. }
  120. // Add record
  121. CCCoreData.add(metadata, activeAccount: activeAccount, activeUrl: activeUrl, typeCloud: typeCloud, context: nil)
  122. }
  123. // Get Datasource
  124. recordsTableMetadata = CCCoreData.getTableMetadata(with: NSPredicate(format: "(account == %@) AND (directoryID == %@)", activeAccount!, metadataNet.directoryID), fieldOrder: CCUtility.getOrderSettings()!, ascending: CCUtility.getAscendingSettings()) as? [TableMetadata]
  125. tableView.reloadData()
  126. hud.hideHud()
  127. }
  128. }
  129. // MARK: - UITableViewDelegate
  130. extension DocumentPickerViewController: UITableViewDelegate {
  131. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  132. return 60
  133. }
  134. }
  135. // MARK: - UITableViewDataSource
  136. extension DocumentPickerViewController: UITableViewDataSource {
  137. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  138. if (recordsTableMetadata == nil) {
  139. return 0
  140. } else {
  141. return recordsTableMetadata!.count
  142. }
  143. }
  144. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  145. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! recordMetadataCell
  146. cell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0)
  147. let recordTableMetadata = recordsTableMetadata?[(indexPath as NSIndexPath).row]
  148. let metadata = CCCoreData.insertEntity(in: recordTableMetadata)!
  149. // File Image View
  150. let filePath = directoryUser!+"/"+metadata.fileID!+".ico"
  151. if (FileManager.default.fileExists(atPath: filePath)) {
  152. cell.fileImageView.image = UIImage(contentsOfFile: filePath)
  153. } else {
  154. cell.fileImageView.image = UIImage(named: metadata.iconName!)
  155. }
  156. // File Name
  157. cell.FileName.text = metadata.fileNamePrint!
  158. return cell
  159. }
  160. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  161. let recordTableMetadata = recordsTableMetadata?[(indexPath as NSIndexPath).row]
  162. let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "DocumentPickerViewController") as! DocumentPickerViewController
  163. nextViewController.localServerUrl = CCUtility.stringAppendServerUrl(localServerUrl!, addServerUrl: recordTableMetadata!.fileName)
  164. nextViewController.titleFolder = recordTableMetadata?.fileNamePrint
  165. self.navigationController?.pushViewController(nextViewController, animated: true)
  166. }
  167. }
  168. // MARK: - Class UITableViewCell
  169. class recordMetadataCell: UITableViewCell {
  170. @IBOutlet weak var fileImageView: UIImageView!
  171. @IBOutlet weak var FileName : UILabel!
  172. }
  173. // MARK: - Class providerSession
  174. class providerSession {
  175. class var sharedInstance : providerSession {
  176. struct Static {
  177. static let instance = providerSession()
  178. }
  179. return Static.instance
  180. }
  181. private init() {
  182. let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: capabilitiesGroups)
  183. let pathDB = dirGroup?.appendingPathComponent(appDatabase).appendingPathComponent("cryptocloud")
  184. MagicalRecord.setupCoreDataStackWithAutoMigratingSqliteStore(at: pathDB!)
  185. MagicalRecord.setLoggingLevel(MagicalRecordLoggingLevel.off)
  186. }
  187. }