DocumentPickerViewController.swift 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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.action = actionReadFolder
  87. metadataNet.serverUrl = self.localServerUrl;
  88. metadataNet.selector = selectorReadFolder;
  89. metadataNet.date = nil;
  90. let oc : OCnetworking = OCnetworking.init(delegate: self, metadataNet: metadataNet, withUser:activeUser , withPassword: activePassword, withUrl: activeUrl, withTypeCloud: typeCloud, oneByOne: true, activityIndicator: false)
  91. networkingOperationQueue.addOperation(oc)
  92. hud.visibleIndeterminateHud()
  93. }
  94. func readFolderFailure(_ metadataNet: CCMetadataNet!, message: String!, errorCode: Int) {
  95. hud.hideHud()
  96. print ("error")
  97. }
  98. func readFolderSuccess(_ metadataNet: CCMetadataNet!, permissions: String!, rev: String!, metadatas: [Any]!) {
  99. // remove all record
  100. let predicate = NSPredicate(format: "(account == %@) AND (directoryID == %@) AND ((session == NULL) OR (session == ''))", activeAccount!, metadataNet.directoryID)
  101. CCCoreData.deleteMetadata(with: predicate)
  102. for metadata in metadatas as! [CCMetadata] {
  103. // do not insert crypto file
  104. if CCUtility.isCryptoString(metadata.fileName) {
  105. continue
  106. }
  107. // plist + crypto = completed ?
  108. if CCUtility.isCryptoPlistString(metadata.fileName) && metadata.directory == false {
  109. var isCryptoComplete = false
  110. for completeMetadata in metadatas as! [CCMetadata] {
  111. if completeMetadata.fileName == CCUtility.trasformedFileNamePlist(inCrypto: metadata.fileName) {
  112. isCryptoComplete = true
  113. }
  114. }
  115. if isCryptoComplete == false {
  116. continue
  117. }
  118. }
  119. // Add record
  120. CCCoreData.add(metadata, activeAccount: activeAccount, activeUrl: activeUrl, typeCloud: typeCloud, context: nil)
  121. }
  122. // Get Datasource
  123. recordsTableMetadata = CCCoreData.getTableMetadata(with: NSPredicate(format: "(account == %@) AND (directoryID == %@)", activeAccount!, metadataNet.directoryID), fieldOrder: CCUtility.getOrderSettings()!, ascending: CCUtility.getAscendingSettings()) as? [TableMetadata]
  124. tableView.reloadData()
  125. hud.hideHud()
  126. }
  127. }
  128. // MARK: - UITableViewDelegate
  129. extension DocumentPickerViewController: UITableViewDelegate {
  130. func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat {
  131. return 60
  132. }
  133. }
  134. // MARK: - UITableViewDataSource
  135. extension DocumentPickerViewController: UITableViewDataSource {
  136. func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
  137. if (recordsTableMetadata == nil) {
  138. return 0
  139. } else {
  140. return recordsTableMetadata!.count
  141. }
  142. }
  143. func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
  144. let cell = tableView.dequeueReusableCell(withIdentifier: "Cell", for: indexPath) as! recordMetadataCell
  145. cell.separatorInset = UIEdgeInsetsMake(0, 60, 0, 0)
  146. let recordTableMetadata = recordsTableMetadata?[(indexPath as NSIndexPath).row]
  147. let metadata = CCCoreData.insertEntity(in: recordTableMetadata)!
  148. // File Image View
  149. let filePath = directoryUser!+"/"+metadata.fileID!+".ico"
  150. if (FileManager.default.fileExists(atPath: filePath)) {
  151. cell.fileImageView.image = UIImage(contentsOfFile: filePath)
  152. } else {
  153. cell.fileImageView.image = UIImage(named: metadata.iconName!)
  154. }
  155. // File Name
  156. cell.FileName.text = metadata.fileNamePrint!
  157. return cell
  158. }
  159. func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
  160. let recordTableMetadata = recordsTableMetadata?[(indexPath as NSIndexPath).row]
  161. let nextViewController = self.storyboard?.instantiateViewController(withIdentifier: "DocumentPickerViewController") as! DocumentPickerViewController
  162. nextViewController.localServerUrl = CCUtility.stringAppendServerUrl(localServerUrl!, addServerUrl: recordTableMetadata!.fileName)
  163. nextViewController.titleFolder = recordTableMetadata?.fileNamePrint
  164. self.navigationController?.pushViewController(nextViewController, animated: true)
  165. }
  166. }
  167. // MARK: - Class UITableViewCell
  168. class recordMetadataCell: UITableViewCell {
  169. @IBOutlet weak var fileImageView: UIImageView!
  170. @IBOutlet weak var FileName : UILabel!
  171. }
  172. // MARK: - Class providerSession
  173. class providerSession {
  174. class var sharedInstance : providerSession {
  175. struct Static {
  176. static let instance = providerSession()
  177. }
  178. return Static.instance
  179. }
  180. private init() {
  181. let dirGroup = FileManager.default.containerURL(forSecurityApplicationGroupIdentifier: capabilitiesGroups)
  182. let pathDB = dirGroup?.appendingPathComponent(appDatabase).appendingPathComponent("cryptocloud")
  183. MagicalRecord.setupCoreDataStackWithAutoMigratingSqliteStore(at: pathDB!)
  184. MagicalRecord.setLoggingLevel(MagicalRecordLoggingLevel.off)
  185. }
  186. }