NCMainCommon.swift 51 KB

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