NCMainCommon.swift 52 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  1. //
  2. // NCMainCommon.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 18/07/18.
  6. // Copyright © 2018 Marino Faggiana. 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 Foundation
  24. class NCMainCommon: NSObject {
  25. @objc static let sharedInstance: NCMainCommon = {
  26. let instance = NCMainCommon()
  27. return instance
  28. }()
  29. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  30. //MARK: -
  31. @objc func triggerProgressTask(_ notification: Notification, sectionDataSourceFileIDIndexPath: NSDictionary, tableView: UITableView) {
  32. guard let dic = notification.userInfo else {
  33. return
  34. }
  35. let fileID = dic["fileID"] as! NSString
  36. _ = dic["serverUrl"] as! NSString
  37. let status = dic["status"] as! Int
  38. let progress = dic["progress"] as! CGFloat
  39. let totalBytes = dic["totalBytes"] as! Double
  40. let totalBytesExpected = dic["totalBytesExpected"] as! Double
  41. appDelegate.listProgressMetadata.setObject([progress as NSNumber, totalBytes as NSNumber, totalBytesExpected as NSNumber], forKey: fileID)
  42. guard let indexPath = sectionDataSourceFileIDIndexPath.object(forKey: fileID) else {
  43. return
  44. }
  45. if isValidIndexPath(indexPath as! IndexPath, tableView: tableView) {
  46. if let cell = tableView.cellForRow(at: indexPath as! IndexPath) as? CCCellMainTransfer {
  47. var image = ""
  48. if status == k_metadataStatusInDownload {
  49. image = "↓"
  50. } else if status == k_metadataStatusInUpload {
  51. image = "↑"
  52. }
  53. cell.labelInfoFile.text = CCUtility.transformedSize(totalBytesExpected) + " - " + image + CCUtility.transformedSize(totalBytes)
  54. cell.transferButton.progress = progress
  55. }
  56. }
  57. }
  58. @objc func cancelTransferMetadata(_ metadata: tableMetadata, reloadDatasource: Bool) {
  59. var actionReloadDatasource = k_action_NULL
  60. if metadata.session.count == 0 {
  61. return
  62. }
  63. guard let session = CCNetworking.shared().getSessionfromSessionDescription(metadata.session) else {
  64. return
  65. }
  66. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  67. return
  68. }
  69. // SESSION EXTENSION
  70. if metadata.session == k_download_session_extension || metadata.session == k_upload_session_extension {
  71. if (metadata.session == k_upload_session_extension) {
  72. do {
  73. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
  74. } catch { }
  75. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: metadata.directoryID)
  76. actionReloadDatasource = k_action_DEL
  77. } else {
  78. NCManageDatabase.sharedInstance.setMetadataSession("", sessionError: "", sessionSelector: "", sessionTaskIdentifier: Int(k_taskIdentifierDone), status: Int(k_metadataStatusNormal), predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  79. actionReloadDatasource = k_action_MOD
  80. }
  81. self.reloadDatasource(ServerUrl: serverUrl, fileID: metadata.fileID, action: actionReloadDatasource)
  82. return
  83. }
  84. session.getTasksWithCompletionHandler { (dataTasks, uploadTasks, downloadTasks) in
  85. var cancel = false
  86. // DOWNLOAD
  87. if metadata.session.count > 0 && metadata.session.contains("download") {
  88. for task in downloadTasks {
  89. if task.taskIdentifier == metadata.sessionTaskIdentifier {
  90. task.cancel()
  91. cancel = true
  92. }
  93. }
  94. if cancel == false {
  95. NCManageDatabase.sharedInstance.setMetadataSession("", sessionError: "", sessionSelector: "", sessionTaskIdentifier: Int(k_taskIdentifierDone), status: Int(k_metadataStatusNormal), predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  96. }
  97. actionReloadDatasource = k_action_MOD
  98. }
  99. // UPLOAD
  100. if metadata.session.count > 0 && metadata.session.contains("upload") {
  101. for task in uploadTasks {
  102. if task.taskIdentifier == metadata.sessionTaskIdentifier {
  103. task.cancel()
  104. cancel = true
  105. }
  106. }
  107. if cancel == false {
  108. do {
  109. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
  110. }
  111. catch { }
  112. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: metadata.directoryID)
  113. }
  114. actionReloadDatasource = k_action_DEL
  115. }
  116. if cancel == false {
  117. self.reloadDatasource(ServerUrl: serverUrl, fileID: metadata.fileID, action: actionReloadDatasource)
  118. }
  119. }
  120. }
  121. @objc func cancelAllTransfer() {
  122. // Delete k_metadataStatusWaitUpload OR k_metadataStatusUploadError
  123. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "account == %@ AND (status == %d OR status == %d)", appDelegate.activeAccount, k_metadataStatusWaitUpload, k_metadataStatusUploadError), clearDateReadDirectoryID: nil)
  124. if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "account == %@ AND status != %d AND status != %d", appDelegate.activeAccount, k_metadataStatusNormal, k_metadataStatusHide), sorted: "fileName", ascending: true) {
  125. for metadata in metadatas {
  126. // Modify
  127. if (metadata.status == k_metadataStatusWaitDownload || metadata.status == k_metadataStatusDownloadError) {
  128. metadata.session = ""
  129. metadata.sessionSelector = ""
  130. metadata.status = Int(k_metadataStatusNormal)
  131. _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
  132. }
  133. // Cancel Task
  134. if metadata.status == k_metadataStatusDownloading || metadata.status == k_metadataStatusUploading {
  135. cancelTransferMetadata(metadata, reloadDatasource: false)
  136. }
  137. }
  138. }
  139. self.reloadDatasource(ServerUrl: nil, fileID: nil, action: k_action_NULL)
  140. }
  141. //MARK: -
  142. func collectionViewCellForItemAt(_ indexPath: IndexPath, collectionView: UICollectionView, typeLayout: String, metadata: tableMetadata, serverUrl: String, isEditMode: Bool, selectFileID: [String], autoUploadFileName: String, autoUploadDirectory: String, hideButtonMore: Bool, source: UIViewController) -> UICollectionViewCell {
  143. var image: UIImage?
  144. var imagePreview = false
  145. if metadata.iconName.count > 0 {
  146. image = UIImage.init(named: metadata.iconName)
  147. } else {
  148. image = UIImage.init(named: "file")
  149. }
  150. if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileName)) {
  151. image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileName))
  152. imagePreview = true
  153. } else {
  154. if metadata.hasPreview == 1 && !CCUtility.fileProviderStorageIconExists(metadata.fileID, fileNameView: metadata.fileName) {
  155. NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, serverUrl: serverUrl, collectionView: collectionView, indexPath: indexPath)
  156. }
  157. }
  158. if typeLayout == k_layout_list {
  159. // LIST
  160. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "listCell", for: indexPath) as! NCListCell
  161. cell.delegate = source as? NCListCellDelegate
  162. cell.fileID = metadata.fileID
  163. cell.indexPath = indexPath
  164. cell.labelTitle.text = metadata.fileNameView
  165. cell.imageStatus.image = nil
  166. cell.imageLocal.image = nil
  167. cell.imageFavorite.image = nil
  168. // hide button more
  169. if hideButtonMore {
  170. cell.hideButtonMore()
  171. }
  172. if metadata.directory {
  173. if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
  174. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderAutomaticUpload"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  175. } else {
  176. cell.imageItem.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  177. }
  178. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
  179. let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
  180. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, lockServerUrl))
  181. // Status image: passcode
  182. if tableDirectory != nil && tableDirectory!.lock && CCUtility.getBlockCode() != nil {
  183. cell.imageStatus.image = UIImage.init(named: "passcode")
  184. }
  185. // Local image: offline
  186. if tableDirectory != nil && tableDirectory!.offline {
  187. cell.imageLocal.image = UIImage.init(named: "offlineFlag")
  188. }
  189. } else {
  190. cell.imageItem.image = image
  191. cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + " " + CCUtility.transformedSize(metadata.size)
  192. // image Local
  193. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  194. if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
  195. if tableLocalFile!.offline { cell.imageLocal.image = UIImage.init(named: "offlineFlag") }
  196. else { cell.imageLocal.image = UIImage.init(named: "local") }
  197. }
  198. }
  199. // image Favorite
  200. if metadata.favorite {
  201. cell.imageFavorite.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), multiplier: 2, color: NCBrandColor.sharedInstance.yellowFavorite)
  202. }
  203. if isEditMode {
  204. cell.imageItemLeftConstraint.constant = 45
  205. cell.imageSelect.isHidden = false
  206. if selectFileID.contains(metadata.fileID) {
  207. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: NCBrandColor.sharedInstance.brand)
  208. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  209. } else {
  210. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedNo"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  211. cell.backgroundView = nil
  212. }
  213. } else {
  214. cell.imageItemLeftConstraint.constant = 10
  215. cell.imageSelect.isHidden = true
  216. cell.backgroundView = nil
  217. }
  218. // Remove last separator
  219. if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
  220. cell.separator.isHidden = true
  221. } else {
  222. cell.separator.isHidden = false
  223. }
  224. return cell
  225. } else {
  226. // GRID
  227. let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "gridCell", for: indexPath) as! NCGridCell
  228. cell.delegate = source as? NCGridCellDelegate
  229. cell.fileID = metadata.fileID
  230. cell.indexPath = indexPath
  231. cell.labelTitle.text = metadata.fileNameView
  232. cell.imageStatus.image = nil
  233. cell.imageLocal.image = nil
  234. cell.imageFavorite.image = nil
  235. // hide button more
  236. if hideButtonMore {
  237. cell.hideButtonMore()
  238. }
  239. if metadata.directory {
  240. if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
  241. image = UIImage.init(named: "folderAutomaticUpload")
  242. } else {
  243. image = UIImage.init(named: "folder")
  244. }
  245. cell.imageItem.image = CCGraphics.changeThemingColorImage(image, width: image!.size.width*6, height: image!.size.height*6, scale: 3.0, color: NCBrandColor.sharedInstance.brandElement)
  246. cell.imageItem.contentMode = .center
  247. let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
  248. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, lockServerUrl))
  249. // Status image: passcode
  250. if tableDirectory != nil && tableDirectory!.lock && CCUtility.getBlockCode() != nil {
  251. cell.imageStatus.image = UIImage.init(named: "passcode")
  252. }
  253. // Local image: offline
  254. if tableDirectory != nil && tableDirectory!.offline {
  255. cell.imageLocal.image = UIImage.init(named: "offlineFlag")
  256. }
  257. } else {
  258. cell.imageItem.image = image
  259. if imagePreview == false {
  260. let width = cell.imageItem.image!.size.width * 2
  261. //let scale = UIScreen.main.scale
  262. cell.imageItem.image = NCUtility.sharedInstance.resizeImage(image: image!, newWidth: width)
  263. cell.imageItem.contentMode = .center
  264. }
  265. // image Local
  266. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  267. if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
  268. if tableLocalFile!.offline { cell.imageLocal.image = UIImage.init(named: "offlineFlag") }
  269. else { cell.imageLocal.image = UIImage.init(named: "local") }
  270. }
  271. }
  272. // image Favorite
  273. if metadata.favorite {
  274. cell.imageFavorite.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), multiplier: 2, color: NCBrandColor.sharedInstance.yellowFavorite)
  275. }
  276. if isEditMode {
  277. cell.imageSelect.isHidden = false
  278. if selectFileID.contains(metadata.fileID) {
  279. cell.imageSelect.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "checkedYes"), multiplier: 2, color: UIColor.white)
  280. cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
  281. } else {
  282. cell.imageSelect.isHidden = true
  283. cell.backgroundView = nil
  284. }
  285. } else {
  286. cell.imageSelect.isHidden = true
  287. cell.backgroundView = nil
  288. }
  289. return cell
  290. }
  291. }
  292. @objc func cellForRowAtIndexPath(_ indexPath: IndexPath, tableView: UITableView ,metadata: tableMetadata, metadataFolder: tableMetadata?, serverUrl: String, autoUploadFileName: String, autoUploadDirectory: String) -> UITableViewCell {
  293. // Create File System
  294. if metadata.directory {
  295. CCUtility.getDirectoryProviderStorageFileID(metadata.fileID)
  296. } else {
  297. CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView)
  298. }
  299. // CCCell
  300. if metadata.status == k_metadataStatusNormal {
  301. // NORMAL
  302. let cell = tableView.dequeueReusableCell(withIdentifier: "CellMain", for: indexPath) as! CCCellMain
  303. cell.separatorInset = UIEdgeInsets.init(top: 0, left: 60, bottom: 0, right: 0)
  304. cell.accessoryType = UITableViewCell.AccessoryType.none
  305. cell.file.image = nil
  306. cell.status.image = nil
  307. cell.favorite.image = nil
  308. cell.shared.image = nil
  309. cell.local.image = nil
  310. cell.imageTitleSegue = nil
  311. cell.shared.isUserInteractionEnabled = false
  312. cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  313. // change color selection
  314. let selectionColor = UIView()
  315. selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
  316. cell.selectedBackgroundView = selectionColor
  317. cell.tintColor = NCBrandColor.sharedInstance.brandElement
  318. cell.labelTitle.textColor = UIColor.black
  319. cell.labelTitle.text = metadata.fileNameView
  320. // Share
  321. let sharesLink = appDelegate.sharesLink.object(forKey: serverUrl + metadata.fileName)
  322. let sharesUserAndGroup = appDelegate.sharesUserAndGroup.object(forKey: serverUrl + metadata.fileName)
  323. var isShare = false
  324. var isMounted = false
  325. if metadataFolder != nil {
  326. isShare = metadata.permissions.contains(k_permission_shared) && !metadataFolder!.permissions.contains(k_permission_shared)
  327. isMounted = metadata.permissions.contains(k_permission_mounted) && !metadataFolder!.permissions.contains(k_permission_mounted)
  328. }
  329. if metadata.directory {
  330. // lable Info
  331. cell.labelInfoFile.text = CCUtility.dateDiff(metadata.date as Date)
  332. // File Image & Image Title Segue
  333. if metadata.e2eEncrypted {
  334. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderEncrypted"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  335. cell.imageTitleSegue = UIImage.init(named: "lock")
  336. } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
  337. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folderAutomaticUpload"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  338. cell.imageTitleSegue = UIImage.init(named: "media")
  339. } else if isShare {
  340. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_shared_with_me"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  341. cell.imageTitleSegue = UIImage.init(named: "share")
  342. } else if isMounted {
  343. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_external"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  344. cell.imageTitleSegue = UIImage.init(named: "shareMounted")
  345. } else if (sharesUserAndGroup != nil) {
  346. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_shared_with_me"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  347. cell.imageTitleSegue = UIImage.init(named: "share")
  348. } else if (sharesLink != nil) {
  349. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder_public"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  350. cell.imageTitleSegue = UIImage.init(named: "sharebylink")
  351. } else {
  352. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "folder"), multiplier: 3, color: NCBrandColor.sharedInstance.brandElement)
  353. }
  354. let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
  355. let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, lockServerUrl))
  356. // Local image: offline
  357. if tableDirectory != nil && tableDirectory!.offline {
  358. cell.local.image = UIImage.init(named: "offlineFlag")
  359. }
  360. // Status image: passcode
  361. if tableDirectory != nil && tableDirectory!.lock && CCUtility.getBlockCode() != nil {
  362. cell.status.image = UIImage.init(named: "passcode")
  363. }
  364. } else {
  365. let iconFileExists = FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  366. // Lable Info
  367. cell.labelInfoFile.text = CCUtility.dateDiff(metadata.date as Date) + " " + CCUtility.transformedSize(metadata.size)
  368. // File Image
  369. if iconFileExists {
  370. cell.file.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  371. } else {
  372. if metadata.iconName.count > 0 {
  373. cell.file.image = UIImage.init(named: metadata.iconName)
  374. } else {
  375. cell.file.image = UIImage.init(named: "file")
  376. }
  377. }
  378. // Local Image - Offline
  379. let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  380. if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
  381. if tableLocalFile!.offline { cell.local.image = UIImage.init(named: "offlineFlag") }
  382. else { cell.local.image = UIImage.init(named: "local") }
  383. }
  384. // Status image: encrypted
  385. let tableE2eEncryption = NCManageDatabase.sharedInstance.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND fileNameIdentifier == %@", appDelegate.activeAccount, metadata.fileName))
  386. if tableE2eEncryption != nil && NCUtility.sharedInstance.isEncryptedMetadata(metadata) {
  387. cell.status.image = UIImage.init(named: "encrypted")
  388. }
  389. // Share
  390. if (isShare) {
  391. cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  392. } else if (isMounted) {
  393. cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMounted"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  394. } else if (sharesLink != nil) {
  395. cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  396. } else if (sharesUserAndGroup != nil) {
  397. cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), multiplier: 2, color: NCBrandColor.sharedInstance.optionItem)
  398. }
  399. }
  400. //
  401. // File & Directory
  402. //
  403. // Favorite
  404. if metadata.favorite {
  405. cell.favorite.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), multiplier: 2, color: NCBrandColor.sharedInstance.yellowFavorite)
  406. }
  407. // More Image
  408. cell.more.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "more"), multiplier: 1, color: NCBrandColor.sharedInstance.optionItem)
  409. return cell
  410. } else {
  411. // TRASNFER
  412. let cell = tableView.dequeueReusableCell(withIdentifier: "CellMainTransfer", for: indexPath) as! CCCellMainTransfer
  413. cell.separatorInset = UIEdgeInsets.init(top: 0, left: 60, bottom: 0, right: 0)
  414. cell.accessoryType = UITableViewCell.AccessoryType.none
  415. cell.file.image = nil
  416. cell.status.image = nil
  417. cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  418. cell.labelTitle.textColor = UIColor.black
  419. cell.labelTitle.text = metadata.fileNameView
  420. cell.transferButton.tintColor = NCBrandColor.sharedInstance.optionItem
  421. var progress: CGFloat = 0.0
  422. var totalBytes: Double = 0.0
  423. //var totalBytesExpected : Double = 0
  424. let progressArray = appDelegate.listProgressMetadata.object(forKey: metadata.fileID) as? NSArray
  425. if progressArray != nil && progressArray?.count == 3 {
  426. progress = progressArray?.object(at: 0) as! CGFloat
  427. totalBytes = progressArray?.object(at: 1) as! Double
  428. //totalBytesExpected = progressArray?.object(at: 2) as! Double
  429. }
  430. // Write status on Label Info
  431. switch metadata.status {
  432. case Int(k_metadataStatusWaitDownload):
  433. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_download_", comment: "")
  434. progress = 0.0
  435. break
  436. case Int(k_metadataStatusInDownload):
  437. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_download_", comment: "")
  438. progress = 0.0
  439. break
  440. case Int(k_metadataStatusDownloading):
  441. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - ↓" + CCUtility.transformedSize(totalBytes)
  442. break
  443. case Int(k_metadataStatusWaitUpload):
  444. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_upload_", comment: "")
  445. progress = 0.0
  446. break
  447. case Int(k_metadataStatusInUpload):
  448. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_upload_", comment: "")
  449. progress = 0.0
  450. break
  451. case Int(k_metadataStatusUploading):
  452. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - ↑" + CCUtility.transformedSize(totalBytes)
  453. break
  454. default:
  455. cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size)
  456. progress = 0.0
  457. }
  458. let iconFileExists = FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  459. if iconFileExists {
  460. cell.file.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
  461. } else {
  462. if metadata.iconName.count > 0 {
  463. cell.file.image = UIImage.init(named: metadata.iconName)
  464. } else {
  465. cell.file.image = UIImage.init(named: "file")
  466. }
  467. }
  468. // Session Upload Extension
  469. if metadata.session == k_upload_session_extension && (metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading) {
  470. cell.labelTitle.isEnabled = false
  471. cell.labelInfoFile.isEnabled = false
  472. } else {
  473. cell.labelTitle.isEnabled = true
  474. cell.labelInfoFile.isEnabled = true
  475. }
  476. // downloadFile
  477. if metadata.status == k_metadataStatusWaitDownload || metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status == k_metadataStatusDownloadError {
  478. //
  479. }
  480. // downloadFile Error
  481. if metadata.status == k_metadataStatusDownloadError {
  482. cell.status.image = UIImage.init(named: "statuserror")
  483. if metadata.sessionError.count == 0 {
  484. cell.labelInfoFile.text = NSLocalizedString("_error_", comment: "") + ", " + NSLocalizedString("_file_not_downloaded_", comment: "")
  485. } else {
  486. cell.labelInfoFile.text = metadata.sessionError
  487. }
  488. }
  489. // uploadFile
  490. if metadata.status == k_metadataStatusWaitUpload || metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading || metadata.status == k_metadataStatusUploadError {
  491. if (!iconFileExists) {
  492. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "uploadCloud"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  493. }
  494. cell.labelTitle.isEnabled = false
  495. }
  496. // uploadFileError
  497. if metadata.status == k_metadataStatusUploadError {
  498. cell.labelTitle.isEnabled = false
  499. cell.status.image = UIImage.init(named: "statuserror")
  500. if !iconFileExists {
  501. cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "uploadCloud"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
  502. }
  503. if metadata.sessionError.count == 0 {
  504. cell.labelInfoFile.text = NSLocalizedString("_error_", comment: "") + ", " + NSLocalizedString("_file_not_uploaded_", comment: "")
  505. } else {
  506. cell.labelInfoFile.text = metadata.sessionError
  507. }
  508. }
  509. // Progress
  510. cell.transferButton.progress = progress
  511. return cell
  512. }
  513. }
  514. @objc func getMetadataFromSectionDataSourceIndexPath(_ indexPath: IndexPath?, sectionDataSource: CCSectionDataSourceMetadata?) -> tableMetadata? {
  515. guard let indexPath = indexPath else {
  516. return nil
  517. }
  518. guard let sectionDataSource = sectionDataSource else {
  519. return nil
  520. }
  521. let section = indexPath.section + 1
  522. let row = indexPath.row + 1
  523. let totSections = sectionDataSource.sections.count
  524. if totSections < section || section > totSections {
  525. return nil
  526. }
  527. let valueSection = sectionDataSource.sections.object(at: indexPath.section)
  528. guard let filesID = sectionDataSource.sectionArrayRow.object(forKey: valueSection) as? NSArray else {
  529. return nil
  530. }
  531. let totRows = filesID.count
  532. if totRows < row || row > totRows {
  533. return nil
  534. }
  535. let fileID = filesID.object(at: indexPath.row)
  536. let metadata = sectionDataSource.allRecordsDataSource.object(forKey: fileID) as? tableMetadata
  537. return metadata
  538. }
  539. @objc func reloadDatasource(ServerUrl: String?, fileID: String?, action: Int32) {
  540. DispatchQueue.main.async {
  541. if self.appDelegate.activeMain != nil {
  542. self.appDelegate.activeMain.reloadDatasource(ServerUrl, fileID: fileID, action: Int(action))
  543. }
  544. if self.appDelegate.activeFavorites != nil {
  545. self.appDelegate.activeFavorites.reloadDatasource(fileID, action: Int(action))
  546. }
  547. if self.appDelegate.activeTransfers != nil {
  548. self.appDelegate.activeTransfers.reloadDatasource(fileID, action: Int(action))
  549. }
  550. }
  551. }
  552. @objc func isValidIndexPath(_ indexPath: IndexPath, tableView: UITableView) -> Bool {
  553. return indexPath.section < tableView.numberOfSections && indexPath.row < tableView.numberOfRows(inSection: indexPath.section)
  554. }
  555. //MARK: -
  556. @objc func deleteFile(metadatas: NSArray, e2ee: Bool, serverUrl: String, folderFileID: String, completion: @escaping (_ errorCode: Int, _ message: String)->()) {
  557. var copyMetadatas = [tableMetadata]()
  558. for metadata in metadatas {
  559. copyMetadatas.append(tableMetadata.init(value: metadata))
  560. }
  561. if e2ee {
  562. DispatchQueue.global().async {
  563. let error = NCNetworkingEndToEnd.sharedManager().lockFolderEncrypted(onServerUrl: serverUrl, fileID: folderFileID, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl)
  564. DispatchQueue.main.async {
  565. if error == nil {
  566. self.delete(metadatas: copyMetadatas, serverUrl:serverUrl, e2ee: e2ee, completion: completion)
  567. } else {
  568. self.appDelegate.messageNotification("_delete_", description: error?.localizedDescription, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  569. return
  570. }
  571. }
  572. }
  573. } else {
  574. delete(metadatas: copyMetadatas, serverUrl:serverUrl, e2ee: e2ee, completion: completion)
  575. }
  576. }
  577. private func delete(metadatas: [tableMetadata], serverUrl: String, e2ee: Bool, completion: @escaping (_ errorCode: Int, _ message: String)->()) {
  578. var count: Int = 0
  579. var completionErrorCode: Int = 0
  580. var completionMessage = ""
  581. let ocNetworking = OCnetworking.init(delegate: nil, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  582. for metadata in metadatas {
  583. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  584. continue
  585. }
  586. self.appDelegate.filterFileID.add(metadata.fileID)
  587. let path = serverUrl + "/" + metadata.fileName
  588. ocNetworking?.deleteFileOrFolder(path, completion: { (message, errorCode) in
  589. count += 1
  590. if errorCode == 0 || errorCode == 404 {
  591. do {
  592. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
  593. } catch { }
  594. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: metadata.directoryID)
  595. NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  596. NCManageDatabase.sharedInstance.deletePhotos(fileID: metadata.fileID)
  597. if metadata.directory {
  598. NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName))
  599. }
  600. if (e2ee) {
  601. NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameIdentifier == %@", metadata.account, serverUrl, metadata.fileName))
  602. }
  603. self.appDelegate.filterFileID.remove(metadata.fileID)
  604. } else {
  605. completionErrorCode = errorCode
  606. completionMessage = ""
  607. if message != nil {
  608. completionMessage = message!
  609. }
  610. self.appDelegate.filterFileID.remove(metadata.fileID)
  611. }
  612. if count == metadatas.count {
  613. if e2ee {
  614. DispatchQueue.global().async {
  615. NCNetworkingEndToEnd.sharedManager().rebuildAndSendMetadata(onServerUrl: serverUrl, account: self.appDelegate.activeAccount, user: self.appDelegate.activeUser, userID: self.appDelegate.activeUserID, password: self.appDelegate.activePassword, url: self.appDelegate.activeUrl)
  616. DispatchQueue.main.async {
  617. completion(completionErrorCode, completionMessage)
  618. }
  619. }
  620. } else {
  621. completion(completionErrorCode, completionMessage)
  622. }
  623. }
  624. })
  625. }
  626. self.reloadDatasource(ServerUrl: serverUrl, fileID: nil, action: k_action_NULL)
  627. self.appDelegate.activeMedia.reloadDatasource(nil, action: Int(k_action_NULL))
  628. }
  629. }
  630. //MARK: -
  631. class CCMainTabBarController : UITabBarController, UITabBarControllerDelegate {
  632. override func viewDidLoad() {
  633. super.viewDidLoad()
  634. delegate = self
  635. }
  636. //Delegate methods
  637. func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
  638. let tabViewControllers = tabBarController.viewControllers!
  639. guard let toIndex = tabViewControllers.index(of: viewController) else {
  640. if let vc = viewController as? UINavigationController {
  641. vc.popToRootViewController(animated: true);
  642. }
  643. return false
  644. }
  645. animateToTab(toIndex: toIndex)
  646. return true
  647. }
  648. func animateToTab(toIndex: Int) {
  649. let tabViewControllers = viewControllers!
  650. let fromView = selectedViewController!.view!
  651. let toView = tabViewControllers[toIndex].view!
  652. let fromIndex = tabViewControllers.index(of: selectedViewController!)
  653. guard fromIndex != toIndex else {return}
  654. // Add the toView to the tab bar view
  655. fromView.superview?.addSubview(toView)
  656. fromView.superview?.backgroundColor = UIColor.white
  657. // Position toView off screen (to the left/right of fromView)
  658. let screenWidth = UIScreen.main.bounds.size.width;
  659. let scrollRight = toIndex > fromIndex!;
  660. let offset = (scrollRight ? screenWidth : -screenWidth)
  661. toView.center = CGPoint(x: (fromView.center.x) + offset, y: (toView.center.y))
  662. // Disable interaction during animation
  663. view.isUserInteractionEnabled = false
  664. UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIView.AnimationOptions.curveEaseOut, animations: {
  665. // Slide the views by -offset
  666. fromView.center = CGPoint(x: fromView.center.x - offset, y: fromView.center.y);
  667. toView.center = CGPoint(x: toView.center.x - offset, y: toView.center.y);
  668. }, completion: { finished in
  669. // Remove the old view from the tabbar view.
  670. fromView.removeFromSuperview()
  671. self.selectedIndex = toIndex
  672. self.view.isUserInteractionEnabled = true
  673. })
  674. }
  675. }
  676. //
  677. // https://stackoverflow.com/questions/44822558/ios-11-uitabbar-uitabbaritem-positioning-issue/46348796#46348796
  678. //
  679. extension UITabBar {
  680. // Workaround for iOS 11's new UITabBar behavior where on iPad, the UITabBar inside
  681. // the Master view controller shows the UITabBarItem icon next to the text
  682. override open var traitCollection: UITraitCollection {
  683. if UIDevice.current.userInterfaceIdiom == .pad {
  684. return UITraitCollection(horizontalSizeClass: .compact)
  685. }
  686. return super.traitCollection
  687. }
  688. }
  689. //MARK: -
  690. class NCNetworkingMain: NSObject, CCNetworkingDelegate {
  691. @objc static let sharedInstance: NCNetworkingMain = {
  692. let instance = NCNetworkingMain()
  693. return instance
  694. }()
  695. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  696. // DOWNLOAD
  697. func downloadStart(_ fileID: String!, account: String!, task: URLSessionDownloadTask!, serverUrl: String!) {
  698. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
  699. appDelegate.updateApplicationIconBadgeNumber()
  700. }
  701. func downloadFileSuccessFailure(_ fileName: String!, fileID: String!, serverUrl: String!, selector: String!, errorMessage: String!, errorCode: Int) {
  702. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", fileID)) else {
  703. return
  704. }
  705. if errorCode == 0 {
  706. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
  707. // Synchronized
  708. if selector == selectorDownloadSynchronize {
  709. //
  710. }
  711. // open View File
  712. if selector == selectorLoadFileView && UIApplication.shared.applicationState == UIApplication.State.active {
  713. if metadata.typeFile == k_metadataTypeFile_compress || metadata.typeFile == k_metadataTypeFile_unknown {
  714. if appDelegate.activeMain.view.window != nil {
  715. appDelegate.activeMain.open(in: metadata)
  716. }
  717. if appDelegate.activeFavorites.view.window != nil {
  718. appDelegate.activeFavorites.open(in: metadata)
  719. }
  720. } else {
  721. if appDelegate.activeMain.view.window != nil {
  722. appDelegate.activeMain.metadata = metadata;
  723. appDelegate.activeMain.shouldPerformSegue()
  724. }
  725. if appDelegate.activeFavorites.view.window != nil {
  726. appDelegate.activeFavorites.metadata = metadata;
  727. appDelegate.activeFavorites.shouldPerformSegue()
  728. }
  729. }
  730. }
  731. // Open in...
  732. if selector == selectorOpenIn && UIApplication.shared.applicationState == UIApplication.State.active {
  733. if appDelegate.activeMain.view.window != nil {
  734. appDelegate.activeMain.open(in: metadata)
  735. }
  736. if appDelegate.activeFavorites.view.window != nil {
  737. appDelegate.activeFavorites.open(in: metadata)
  738. }
  739. }
  740. // Save to Photo Album
  741. if selector == selectorSave {
  742. appDelegate.activeMain.save(toPhotoAlbum: metadata)
  743. }
  744. // Copy File
  745. if selector == selectorLoadCopy {
  746. appDelegate.activeMain.copyFile(toPasteboard: metadata)
  747. }
  748. // Set as available offline
  749. if selector == selectorLoadOffline {
  750. NCManageDatabase.sharedInstance.setLocalFile(fileID: metadata.fileID, offline: true)
  751. }
  752. //selectorLoadViewImage
  753. if selector == selectorLoadViewImage {
  754. if appDelegate.activeDetail != nil {
  755. appDelegate.activeDetail.downloadPhotoBrowserSuccessFailure(metadata, selector: selector, errorCode: errorCode)
  756. }
  757. if appDelegate.activeMedia != nil {
  758. appDelegate.activeMedia.downloadFileSuccessFailure(metadata.fileName, fileID: metadata.fileID, serverUrl: serverUrl, selector: selector, errorMessage: errorMessage, errorCode: errorCode)
  759. }
  760. }
  761. self.appDelegate.performSelector(onMainThread: #selector(self.appDelegate.loadAutoDownloadUpload), with: nil, waitUntilDone: true)
  762. } else {
  763. // File do not exists on server, remove in local
  764. if (errorCode == kOCErrorServerPathNotFound || errorCode == -1011) { // - 1011 = kCFURLErrorBadServerResponse
  765. do {
  766. try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
  767. } catch { }
  768. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID), clearDateReadDirectoryID: metadata.directoryID)
  769. NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
  770. NCManageDatabase.sharedInstance.deletePhotos(fileID: fileID)
  771. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_DEL))
  772. }
  773. if selector == selectorLoadViewImage {
  774. if appDelegate.activeDetail.view.window != nil {
  775. appDelegate.activeDetail.downloadPhotoBrowserSuccessFailure(metadata, selector: selector, errorCode: errorCode)
  776. }
  777. if appDelegate.activeMedia.view.window != nil {
  778. appDelegate.activeMedia.downloadFileSuccessFailure(metadata.fileName, fileID: metadata.fileID, serverUrl: serverUrl, selector: selector, errorMessage: errorMessage, errorCode: errorCode)
  779. }
  780. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
  781. }
  782. }
  783. }
  784. // UPLOAD
  785. func uploadStart(_ fileID: String!, account: String!, task: URLSessionUploadTask!, serverUrl: String!) {
  786. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
  787. appDelegate.updateApplicationIconBadgeNumber()
  788. }
  789. func uploadFileSuccessFailure(_ fileName: String!, fileID: String!, assetLocalIdentifier: String!, serverUrl: String!, selector: String!, errorMessage: String!, errorCode: Int) {
  790. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
  791. if errorCode == 0 {
  792. self.appDelegate.performSelector(onMainThread: #selector(self.appDelegate.loadAutoDownloadUpload), with: nil, waitUntilDone: true)
  793. } else {
  794. NCManageDatabase.sharedInstance.addActivityClient(fileName, fileID: assetLocalIdentifier, action: k_activityDebugActionUpload, selector: selector, note: errorMessage, type: k_activityTypeFailure, verbose: false, activeUrl: appDelegate.activeUrl)
  795. if errorCode != -999 && errorCode != kOCErrorServerUnauthorized && errorMessage != "" {
  796. appDelegate.messageNotification("_upload_file_", description: errorMessage, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
  797. }
  798. }
  799. }
  800. func downloadThumbnail(with metadata: tableMetadata, serverUrl: String, collectionView: UICollectionView, indexPath: IndexPath) {
  801. let width = NCUtility.sharedInstance.getScreenWidthForPreview()
  802. let height = NCUtility.sharedInstance.getScreenHeightForPreview()
  803. let ocNetworking = OCnetworking.init(delegate: self, metadataNet: nil, withUser: appDelegate.activeUser, withUserID: appDelegate.activeUserID, withPassword: appDelegate.activePassword, withUrl: appDelegate.activeUrl)
  804. ocNetworking?.downloadPreview(with: metadata, serverUrl: serverUrl, withWidth: width, andHeight: height, completion: { (message, errorCode) in
  805. if errorCode == 0 && CCUtility.fileProviderStorageIconExists(metadata.fileID, fileNameView: metadata.fileName) {
  806. collectionView.reloadItems(at: [indexPath])
  807. }
  808. })
  809. }
  810. }
  811. //MARK: -
  812. class NCFunctionMain: NSObject {
  813. @objc static let sharedInstance: NCFunctionMain = {
  814. let instance = NCFunctionMain()
  815. return instance
  816. }()
  817. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  818. @objc func synchronizeOffline() {
  819. let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true)
  820. if (directories != nil) {
  821. for directory: tableDirectory in directories! {
  822. CCSynchronize.shared()?.readFolder(directory.serverUrl, selector: selectorReadFolderWithDownload)
  823. }
  824. }
  825. let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "fileName", ascending: true)
  826. if (files != nil) {
  827. for file: tableLocalFile in files! {
  828. guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", file.fileID)) else {
  829. continue
  830. }
  831. guard let serverUrl = NCManageDatabase.sharedInstance.getServerUrl(metadata.directoryID) else {
  832. continue
  833. }
  834. CCSynchronize.shared()?.readFile(metadata.fileID, fileName: metadata.fileName, serverUrl: serverUrl, selector: selectorReadFileWithDownload)
  835. }
  836. }
  837. }
  838. }