1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531 |
- //
- // NCMainCommon.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 18/07/18.
- // Copyright © 2018 Marino Faggiana. All rights reserved.
- //
- // Author Marino Faggiana <marino.faggiana@nextcloud.com>
- //
- // This program is free software: you can redistribute it and/or modify
- // it under the terms of the GNU General Public License as published by
- // the Free Software Foundation, either version 3 of the License, or
- // (at your option) any later version.
- //
- // This program is distributed in the hope that it will be useful,
- // but WITHOUT ANY WARRANTY; without even the implied warranty of
- // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- // GNU General Public License for more details.
- //
- // You should have received a copy of the GNU General Public License
- // along with this program. If not, see <http://www.gnu.org/licenses/>.
- //
- import Foundation
- import TLPhotoPicker
- import ZIPFoundation
- //MARK: - Main Common
- class NCMainCommon: NSObject, PhotoEditorDelegate, NCAudioRecorderViewControllerDelegate, UIDocumentInteractionControllerDelegate {
-
- @objc static let sharedInstance: NCMainCommon = {
- let instance = NCMainCommon()
- return instance
- }()
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- var metadataEditPhoto: tableMetadata?
- var docController: UIDocumentInteractionController?
- lazy var operationQueueReloadDatasource: OperationQueue = {
- let queue = OperationQueue()
- queue.name = "Reload main datasource queue"
- queue.maxConcurrentOperationCount = 1
- return queue
- }()
-
- //MARK: -
-
- @objc func triggerProgressTask(_ notification: Notification, sectionDataSourceFileIDIndexPath: NSDictionary, tableView: UITableView, viewController: UIViewController, serverUrlViewController: String?) {
-
- if viewController.viewIfLoaded?.window == nil {
- return
- }
-
- guard let dic = notification.userInfo else {
- return
- }
-
- let account = dic["account"] as? NSString ?? ""
- let fileID = dic["fileID"] as? NSString ?? ""
- let serverUrl = dic["serverUrl"] as? String ?? ""
- let status = dic["status"] as? Int ?? Int(k_taskIdentifierDone)
- let progress = dic["progress"] as? CGFloat ?? 0
- let totalBytes = dic["totalBytes"] as? Double ?? 0
- let totalBytesExpected = dic["totalBytesExpected"] as? Double ?? 0
-
- if (account != self.appDelegate.activeAccount! as NSString) && !(viewController is CCTransfers) {
- return
- }
-
- if serverUrlViewController != nil && serverUrlViewController! != serverUrl {
- return
- }
-
- appDelegate.listProgressMetadata.setObject([progress as NSNumber, totalBytes as NSNumber, totalBytesExpected as NSNumber], forKey: fileID)
-
- guard let indexPath = sectionDataSourceFileIDIndexPath.object(forKey: fileID) else {
- return
- }
-
- if isValidIndexPath(indexPath as! IndexPath, view: tableView) {
-
- if let cell = tableView.cellForRow(at: indexPath as! IndexPath) as? CCCellMainTransfer {
-
- var image = ""
-
- if status == k_metadataStatusInDownload {
- image = "↓"
- } else if status == k_metadataStatusInUpload {
- image = "↑"
- }
-
- cell.labelInfoFile?.text = CCUtility.transformedSize(totalBytesExpected) + " - " + image + CCUtility.transformedSize(totalBytes)
- cell.transferButton?.progress = progress
- }
- }
- }
-
- @objc func cancelTransferMetadata(_ metadata: tableMetadata, reloadDatasource: Bool, uploadStatusForcedStart: Bool) {
-
- var actionReloadDatasource = k_action_NULL
- var metadata = metadata
-
- if metadata.session.count == 0 {
- return
- }
-
- guard let session = CCNetworking.shared().getSessionfromSessionDescription(metadata.session) else {
- return
- }
-
- // SESSION EXTENSION
- if metadata.session == k_download_session_extension || metadata.session == k_upload_session_extension {
-
- if (metadata.session == k_upload_session_extension) {
-
- do {
- try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
- } catch { }
-
- NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
-
- actionReloadDatasource = k_action_DEL
-
- } else {
-
- NCManageDatabase.sharedInstance.setMetadataSession("", sessionError: "", sessionSelector: "", sessionTaskIdentifier: Int(k_taskIdentifierDone), status: Int(k_metadataStatusNormal), predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
-
- actionReloadDatasource = k_action_MOD
- }
-
- self.reloadDatasource(ServerUrl: metadata.serverUrl, fileID: metadata.fileID, action: actionReloadDatasource)
-
- return
- }
-
- session.getTasksWithCompletionHandler { (dataTasks, uploadTasks, downloadTasks) in
-
- var cancel = false
-
- // DOWNLOAD
- if metadata.session.count > 0 && metadata.session.contains("download") {
- for task in downloadTasks {
- if task.taskIdentifier == metadata.sessionTaskIdentifier {
- task.cancel()
- cancel = true
- }
- }
- if cancel == false {
- NCManageDatabase.sharedInstance.setMetadataSession("", sessionError: "", sessionSelector: "", sessionTaskIdentifier: Int(k_taskIdentifierDone), status: Int(k_metadataStatusNormal), predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
- }
- actionReloadDatasource = k_action_MOD
- }
-
- // UPLOAD
- if metadata.session.count > 0 && metadata.session.contains("upload") {
- for task in uploadTasks {
- if task.taskIdentifier == metadata.sessionTaskIdentifier {
- if uploadStatusForcedStart {
- metadata.status = Int(k_metadataStatusUploadForcedStart)
- metadata = NCManageDatabase.sharedInstance.addMetadata(metadata) ?? metadata
- }
- task.cancel()
- cancel = true
- }
- }
- if cancel == false {
- do {
- try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
- }
- catch { }
- NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
- }
- actionReloadDatasource = k_action_DEL
- }
-
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
- self.reloadDatasource(ServerUrl: metadata.serverUrl, fileID: metadata.fileID, action: actionReloadDatasource)
- }
- }
- }
-
- @objc func cancelAllTransfer(view: UIView) {
-
- // Delete k_metadataStatusWaitUpload OR k_metadataStatusUploadError
- NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "status == %d OR status == %d", appDelegate.activeAccount, k_metadataStatusWaitUpload, k_metadataStatusUploadError))
-
- NCUtility.sharedInstance.startActivityIndicator(view: view, bottom: 0)
-
- DispatchQueue.main.asyncAfter(deadline: .now() + 0.3) {
- if let metadatas = NCManageDatabase.sharedInstance.getMetadatas(predicate: NSPredicate(format: "status != %d AND status != %d", k_metadataStatusNormal, k_metadataStatusHide), sorted: "fileName", ascending: true) {
-
- for metadata in metadatas {
-
- // Modify
- if (metadata.status == k_metadataStatusWaitDownload || metadata.status == k_metadataStatusDownloadError) {
- metadata.session = ""
- metadata.sessionSelector = ""
- metadata.status = Int(k_metadataStatusNormal)
-
- _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
- }
-
- // Cancel Task
- if metadata.status == k_metadataStatusDownloading || metadata.status == k_metadataStatusUploading {
- self.cancelTransferMetadata(metadata, reloadDatasource: false, uploadStatusForcedStart: false)
- }
- }
- }
- }
-
- DispatchQueue.main.asyncAfter(deadline: .now() + 1) {
- self.reloadDatasource(ServerUrl: nil, fileID: nil, action: k_action_NULL)
- NCUtility.sharedInstance.stopActivityIndicator()
- }
- }
-
- //MARK: -
-
- func collectionViewCellForItemAt(_ indexPath: IndexPath, collectionView: UICollectionView, cell: UICollectionViewCell, metadata: tableMetadata, metadataFolder: tableMetadata?, serverUrl: String, isEditMode: Bool, selectFileID: [String], autoUploadFileName: String, autoUploadDirectory: String, hideButtonMore: Bool, downloadThumbnail: Bool,source: UIViewController) {
-
- var image: UIImage?
- var isImagePreviewLoaded = false
-
- // Image Preview
- if FileManager().fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileName)) {
- image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileName))
- isImagePreviewLoaded = true
- } else {
- if metadata.iconName.count > 0 {
- image = UIImage.init(named: metadata.iconName)
- } else {
- image = UIImage.init(named: "file")
- }
- }
-
- // Download preview
- if downloadThumbnail {
- NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, view: collectionView, indexPath: indexPath)
- }
-
- // Share
- let sharesLink = appDelegate.sharesLink.object(forKey: serverUrl + metadata.fileName) as? String
- let sharesUserAndGroup = appDelegate.sharesUserAndGroup.object(forKey: serverUrl + metadata.fileName) as? String
- var isShare = false
- var isMounted = false
-
- if metadataFolder != nil {
- isShare = metadata.permissions.contains(k_permission_shared) && !metadataFolder!.permissions.contains(k_permission_shared)
- isMounted = metadata.permissions.contains(k_permission_mounted) && !metadataFolder!.permissions.contains(k_permission_mounted)
- }
-
- if cell is NCListCell {
-
- let cell = cell as! NCListCell
-
- cell.delegate = source as? NCListCellDelegate
-
- cell.fileID = metadata.fileID
- cell.indexPath = indexPath
- cell.labelTitle.text = metadata.fileNameView
- cell.imageStatus.image = nil
- cell.imageLocal.image = nil
- cell.imageFavorite.image = nil
- cell.imageShare.image = nil
- cell.hide(buttonMore: hideButtonMore, hideImageShare: true)
-
- if metadata.directory {
-
- if metadata.e2eEncrypted {
- image = UIImage.init(named: "folderEncrypted")
- } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
- image = UIImage.init(named: "folderAutomaticUpload")
- } else if isShare {
- image = UIImage.init(named: "folder_shared_with_me")
- } else if isMounted {
- image = UIImage.init(named: "folder_external")
- } else if (sharesUserAndGroup != nil) {
- image = UIImage.init(named: "folder_shared_with_me")
- } else if (sharesLink != nil) {
- image = UIImage.init(named: "folder_public")
- } else {
- image = UIImage.init(named: "folder")
- }
-
- cell.imageItem.image = CCGraphics.changeThemingColorImage(image, width: image!.size.width*2, height: image!.size.height*2, color: NCBrandColor.sharedInstance.brandElement)
- cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date)
-
- let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
- let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, lockServerUrl))
-
- // Status image: passcode
- if tableDirectory != nil && tableDirectory!.lock && CCUtility.getBlockCode() != nil {
- cell.imageStatus.image = UIImage.init(named: "passcode")
- }
-
- // Local image: offline
- if tableDirectory != nil && tableDirectory!.offline {
- cell.imageLocal.image = UIImage.init(named: "offlineFlag")
- }
-
- } else {
-
- cell.imageItem.image = image
- cell.labelInfo.text = CCUtility.dateDiff(metadata.date as Date) + ", " + CCUtility.transformedSize(metadata.size)
-
- // image Local
- let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
- if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
- if tableLocalFile!.offline { cell.imageLocal.image = UIImage.init(named: "offlineFlag") }
- else { cell.imageLocal.image = UIImage.init(named: "local") }
- }
-
- // Share
- if (isShare) {
- cell.imageShare.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- cell.hide(buttonMore: hideButtonMore, hideImageShare: false)
- } else if (isMounted) {
- cell.imageShare.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMounted"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- cell.hide(buttonMore: hideButtonMore, hideImageShare: false)
- } else if (sharesLink != nil) {
- cell.imageShare.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- cell.hide(buttonMore: hideButtonMore, hideImageShare: false)
- } else if (sharesUserAndGroup != nil) {
- cell.imageShare.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- cell.hide(buttonMore: hideButtonMore, hideImageShare: false)
- }
- }
-
- // image Favorite
- if metadata.favorite {
- cell.imageFavorite.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), multiplier: 2, color: NCBrandColor.sharedInstance.yellowFavorite)
- }
-
- if isEditMode {
- cell.imageItemLeftConstraint.constant = 45
- cell.imageSelect.isHidden = false
-
- if selectFileID.contains(metadata.fileID) {
- cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
- cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
- } else {
- cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedNo"), to: CGSize(width: 50, height: 50), isAspectRation: true)
- cell.backgroundView = nil
- }
- } else {
- cell.imageItemLeftConstraint.constant = 10
- cell.imageSelect.isHidden = true
- cell.backgroundView = nil
- }
-
- // Remove last separator
- if collectionView.numberOfItems(inSection: indexPath.section) == indexPath.row + 1 {
- cell.separator.isHidden = true
- } else {
- cell.separator.isHidden = false
- }
-
- } else if cell is NCGridCell {
-
- let cell = cell as! NCGridCell
- cell.delegate = source as? NCGridCellDelegate
-
- cell.fileID = metadata.fileID
- cell.indexPath = indexPath
- cell.labelTitle.text = metadata.fileNameView
- cell.imageStatus.image = nil
- cell.imageLocal.image = nil
- cell.imageFavorite.image = nil
- cell.imageShare.image = nil
- cell.hide(buttonMore: hideButtonMore, hideImageShare: true)
-
- if metadata.directory {
-
- if metadata.e2eEncrypted {
- image = UIImage.init(named: "folderEncrypted")
- } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
- image = UIImage.init(named: "folderAutomaticUpload")
- } else if isShare {
- image = UIImage.init(named: "folder_shared_with_me")
- } else if isMounted {
- image = UIImage.init(named: "folder_external")
- } else if (sharesUserAndGroup != nil) {
- image = UIImage.init(named: "folder_shared_with_me")
- } else if (sharesLink != nil) {
- image = UIImage.init(named: "folder_public")
- } else {
- image = UIImage.init(named: "folder")
- }
-
- cell.imageItem.image = CCGraphics.changeThemingColorImage(image, width: image!.size.width*2, height: image!.size.height*2, color: NCBrandColor.sharedInstance.brandElement)
-
- let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
- let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, lockServerUrl))
-
- // Status image: passcode
- if tableDirectory != nil && tableDirectory!.lock && CCUtility.getBlockCode() != nil {
- cell.imageStatus.image = UIImage.init(named: "passcode")
- }
-
- // Local image: offline
- if tableDirectory != nil && tableDirectory!.offline {
- cell.imageLocal.image = UIImage.init(named: "offlineFlag")
- }
-
- } else {
-
- cell.imageItem.image = image
- if isImagePreviewLoaded == false {
- let width = cell.imageItem.image!.size.width * 2
- cell.imageItem.image = NCUtility.sharedInstance.resizeImage(image: image!, newWidth: width)
- }
-
- // image Local
- let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
- if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
- if tableLocalFile!.offline { cell.imageLocal.image = UIImage.init(named: "offlineFlag") }
- else { cell.imageLocal.image = UIImage.init(named: "local") }
- }
-
- // Share
- if (isShare) {
- cell.imageShare.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- cell.hide(buttonMore: hideButtonMore, hideImageShare: false)
- } else if (isMounted) {
- cell.imageShare.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMounted"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- cell.hide(buttonMore: hideButtonMore, hideImageShare: false)
- } else if (sharesLink != nil) {
- cell.imageShare.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- cell.hide(buttonMore: hideButtonMore, hideImageShare: false)
- } else if (sharesUserAndGroup != nil) {
- cell.imageShare.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- cell.hide(buttonMore: hideButtonMore, hideImageShare: false)
- }
- }
-
- // image Favorite
- if metadata.favorite {
- cell.imageFavorite.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), multiplier: 2, color: NCBrandColor.sharedInstance.yellowFavorite)
- }
-
- if isEditMode {
- cell.imageSelect.isHidden = false
- if selectFileID.contains(metadata.fileID) {
- cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
- cell.backgroundView = NCUtility.sharedInstance.cellBlurEffect(with: cell.bounds)
- } else {
- cell.imageSelect.isHidden = true
- cell.backgroundView = nil
- }
- } else {
- cell.imageSelect.isHidden = true
- cell.backgroundView = nil
- }
-
- } else if cell is NCGridMediaCell {
-
- let cell = cell as! NCGridMediaCell
-
- cell.imageStatus.image = nil
- cell.imageLocal.image = nil
- cell.imageFavorite.image = nil
- cell.imageItem.image = image
-
- // image Local
- let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
- if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
- if tableLocalFile!.offline { cell.imageLocal.image = UIImage.init(named: "offlineFlag") }
- else { cell.imageLocal.image = UIImage.init(named: "local") }
- }
-
- // image Favorite
- if metadata.favorite {
- cell.imageFavorite.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), multiplier: 2, color: NCBrandColor.sharedInstance.yellowFavorite)
- }
-
- if isEditMode {
- cell.imageSelect.isHidden = false
- if selectFileID.contains(metadata.fileID) {
- cell.imageSelect.image = CCGraphics.scale(UIImage.init(named: "checkedYes"), to: CGSize(width: 50, height: 50), isAspectRation: true)
- cell.imageVisualEffect.isHidden = false
- cell.imageVisualEffect.alpha = 0.4
- } else {
- cell.imageSelect.isHidden = true
- cell.imageVisualEffect.isHidden = true
- }
- } else {
- cell.imageSelect.isHidden = true
- cell.imageVisualEffect.isHidden = true
- }
- }
- }
-
- @objc func cellForRowAtIndexPath(_ indexPath: IndexPath, tableView: UITableView ,metadata: tableMetadata, metadataFolder: tableMetadata?, serverUrl: String, autoUploadFileName: String, autoUploadDirectory: String) -> UITableViewCell {
-
- var image: UIImage?
- // Create File System
- if metadata.directory {
- CCUtility.getDirectoryProviderStorageFileID(metadata.fileID)
- } else {
- CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView)
- }
-
- // CCCell
- if metadata.status == k_metadataStatusNormal {
-
- // NORMAL
-
- let cell = tableView.dequeueReusableCell(withIdentifier: "CellMain", for: indexPath) as! CCCellMain
- cell.separatorInset = UIEdgeInsets.init(top: 0, left: 60, bottom: 0, right: 0)
- cell.accessoryType = UITableViewCell.AccessoryType.none
- cell.file.image = nil
- cell.status.image = nil
- cell.favorite.image = nil
- cell.shared.image = nil
- cell.local.image = nil
- cell.shared.isUserInteractionEnabled = false
-
- cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView
-
- // change color selection
- let selectionColor = UIView()
- selectionColor.backgroundColor = NCBrandColor.sharedInstance.getColorSelectBackgrond()
- cell.selectedBackgroundView = selectionColor
- cell.tintColor = NCBrandColor.sharedInstance.brandElement
-
- cell.labelTitle.textColor = UIColor.black
- cell.labelTitle.text = metadata.fileNameView
-
- // Download preview
- NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata, view: tableView, indexPath: indexPath)
-
- // Share
- let sharesLink = appDelegate.sharesLink.object(forKey: serverUrl + metadata.fileName) as? String
- let sharesUserAndGroup = appDelegate.sharesUserAndGroup.object(forKey: serverUrl + metadata.fileName) as? String
- var isShare = false
- var isMounted = false
-
- if metadataFolder != nil {
- isShare = metadata.permissions.contains(k_permission_shared) && !metadataFolder!.permissions.contains(k_permission_shared)
- isMounted = metadata.permissions.contains(k_permission_mounted) && !metadataFolder!.permissions.contains(k_permission_mounted)
- }
-
- if metadata.directory {
-
- // lable Info
- cell.labelInfoFile.text = CCUtility.dateDiff(metadata.date as Date)
-
- // File Image & Image Title Segue
- if metadata.e2eEncrypted {
- image = UIImage.init(named: "folderEncrypted")
- } else if metadata.fileName == autoUploadFileName && serverUrl == autoUploadDirectory {
- image = UIImage.init(named: "folderAutomaticUpload")
- } else if isShare {
- image = UIImage.init(named: "folder_shared_with_me")
- } else if isMounted {
- image = UIImage.init(named: "folder_external")
- } else if (sharesUserAndGroup != nil) {
- image = UIImage.init(named: "folder_shared_with_me")
- } else if (sharesLink != nil) {
- image = UIImage.init(named: "folder_public")
- } else {
- image = UIImage.init(named: "folder")
- }
-
- cell.file.image = CCGraphics.changeThemingColorImage(image, width: image!.size.width*2, height: image!.size.height*2, color: NCBrandColor.sharedInstance.brandElement)
-
- let lockServerUrl = CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName)!
- let tableDirectory = NCManageDatabase.sharedInstance.getTableDirectory(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@", appDelegate.activeAccount, lockServerUrl))
- // Local image: offline
- if tableDirectory != nil && tableDirectory!.offline {
- cell.local.image = UIImage.init(named: "offlineFlag")
- }
- // Status image: passcode
- if tableDirectory != nil && tableDirectory!.lock && CCUtility.getBlockCode() != nil {
- cell.status.image = UIImage.init(named: "passcode")
- }
-
- } else {
-
- let iconFileExists = FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
-
- // Lable Info
- cell.labelInfoFile.text = CCUtility.dateDiff(metadata.date as Date) + ", " + CCUtility.transformedSize(metadata.size)
-
- // File Image
- if iconFileExists {
- cell.file.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
- } else {
- if metadata.iconName.count > 0 {
- cell.file.image = UIImage.init(named: metadata.iconName)
- } else {
- cell.file.image = UIImage.init(named: "file")
- }
- }
-
- // Local Image - Offline
- let tableLocalFile = NCManageDatabase.sharedInstance.getTableLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
- if tableLocalFile != nil && CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
- if tableLocalFile!.offline { cell.local.image = UIImage.init(named: "offlineFlag") }
- else { cell.local.image = UIImage.init(named: "local") }
- }
-
- // Status image: encrypted
- let tableE2eEncryption = NCManageDatabase.sharedInstance.getE2eEncryption(predicate: NSPredicate(format: "account == %@ AND fileNameIdentifier == %@", appDelegate.activeAccount, metadata.fileName))
- if tableE2eEncryption != nil && NCUtility.sharedInstance.isEncryptedMetadata(metadata) {
- cell.status.image = UIImage.init(named: "encrypted")
- }
-
- // Share
- if (isShare) {
- cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- } else if (isMounted) {
- cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "shareMounted"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- } else if (sharesLink != nil) {
- cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "sharebylink"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- } else if (sharesUserAndGroup != nil) {
- cell.shared.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "share"), width: 50, height: 50, color: NCBrandColor.sharedInstance.optionItem)
- }
- }
-
- //
- // File & Directory
- //
-
- // Favorite
- if metadata.favorite {
- cell.favorite.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "favorite"), multiplier: 2, color: NCBrandColor.sharedInstance.yellowFavorite)
- }
-
- // More Image
- cell.more.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "more"), multiplier: 1, color: NCBrandColor.sharedInstance.optionItem)
-
- return cell
-
- } else {
-
- // TRASNFER
-
- let cell = tableView.dequeueReusableCell(withIdentifier: "CellMainTransfer", for: indexPath) as! CCCellMainTransfer
- cell.separatorInset = UIEdgeInsets.init(top: 0, left: 60, bottom: 0, right: 0)
- cell.accessoryType = UITableViewCell.AccessoryType.none
- cell.file.image = nil
- cell.status.image = nil
- cell.user.image = nil
-
- cell.backgroundColor = NCBrandColor.sharedInstance.backgroundView
- cell.labelTitle.textColor = UIColor.black
- cell.labelTitle.text = metadata.fileNameView
-
- cell.transferButton.tintColor = NCBrandColor.sharedInstance.optionItem
-
- var progress: CGFloat = 0.0
- var totalBytes: Double = 0.0
- //var totalBytesExpected : Double = 0
- let progressArray = appDelegate.listProgressMetadata.object(forKey: metadata.fileID) as? NSArray
- if progressArray != nil && progressArray?.count == 3 {
- progress = progressArray?.object(at: 0) as! CGFloat
- totalBytes = progressArray?.object(at: 1) as! Double
- //totalBytesExpected = progressArray?.object(at: 2) as! Double
- }
-
- // Write status on Label Info
- switch metadata.status {
- case Int(k_metadataStatusWaitDownload):
- cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_download_", comment: "")
- progress = 0.0
- break
- case Int(k_metadataStatusInDownload):
- cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_download_", comment: "")
- progress = 0.0
- break
- case Int(k_metadataStatusDownloading):
- cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - ↓" + CCUtility.transformedSize(totalBytes)
- break
- case Int(k_metadataStatusWaitUpload):
- cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_wait_upload_", comment: "")
- progress = 0.0
- break
- case Int(k_metadataStatusInUpload):
- cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - " + NSLocalizedString("_status_in_upload_", comment: "")
- progress = 0.0
- break
- case Int(k_metadataStatusUploading):
- cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size) + " - ↑" + CCUtility.transformedSize(totalBytes)
- break
- default:
- cell.labelInfoFile.text = CCUtility.transformedSize(metadata.size)
- progress = 0.0
- }
-
- let iconFileExists = FileManager.default.fileExists(atPath: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
- if iconFileExists {
- cell.file.image = UIImage.init(contentsOfFile: CCUtility.getDirectoryProviderStorageIconFileID(metadata.fileID, fileNameView: metadata.fileNameView))
- } else {
- if metadata.iconName.count > 0 {
- cell.file.image = UIImage.init(named: metadata.iconName)
- } else {
- cell.file.image = UIImage.init(named: "file")
- }
- }
-
- // Session Upload Extension
- if metadata.session == k_upload_session_extension && (metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading) {
-
- cell.labelTitle.isEnabled = false
- cell.labelInfoFile.isEnabled = false
-
- } else {
-
- cell.labelTitle.isEnabled = true
- cell.labelInfoFile.isEnabled = true
- }
-
- // downloadFile
- if metadata.status == k_metadataStatusWaitDownload || metadata.status == k_metadataStatusInDownload || metadata.status == k_metadataStatusDownloading || metadata.status == k_metadataStatusDownloadError {
- //
- }
-
- // downloadFile Error
- if metadata.status == k_metadataStatusDownloadError {
-
- cell.status.image = UIImage.init(named: "statuserror")
-
- if metadata.sessionError.count == 0 {
- cell.labelInfoFile.text = NSLocalizedString("_error_", comment: "") + ", " + NSLocalizedString("_file_not_downloaded_", comment: "")
- } else {
- cell.labelInfoFile.text = metadata.sessionError
- }
- }
-
- // uploadFile
- if metadata.status == k_metadataStatusWaitUpload || metadata.status == k_metadataStatusInUpload || metadata.status == k_metadataStatusUploading || metadata.status == k_metadataStatusUploadError {
-
- if (!iconFileExists) {
- cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "uploadCloud"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
- }
-
- cell.labelTitle.isEnabled = false
- }
-
- // uploadFileError
- if metadata.status == k_metadataStatusUploadError {
-
- cell.labelTitle.isEnabled = false
- cell.status.image = UIImage.init(named: "statuserror")
-
- if !iconFileExists {
- cell.file.image = CCGraphics.changeThemingColorImage(UIImage.init(named: "uploadCloud"), multiplier: 2, color: NCBrandColor.sharedInstance.brandElement)
- }
-
- if metadata.sessionError.count == 0 {
- cell.labelInfoFile.text = NSLocalizedString("_error_", comment: "") + ", " + NSLocalizedString("_file_not_uploaded_", comment: "")
- } else {
- cell.labelInfoFile.text = metadata.sessionError
- }
- }
-
- // Progress
- cell.transferButton.progress = progress
-
- // User
- if metadata.account != appDelegate.activeAccount {
- let tableAccount = NCManageDatabase.sharedInstance.getAccount(predicate: NSPredicate(format: "account == %@", metadata.account))
- if tableAccount != nil {
- let fileNamePath = CCUtility.getDirectoryUserData() + "/" + CCUtility.getStringUser(tableAccount!.user, activeUrl: tableAccount!.url) + "-" + tableAccount!.user + ".png"
- var avatar = UIImage.init(contentsOfFile: fileNamePath)
- if avatar != nil {
- let avatarImageView = CCAvatar.init(image: avatar, borderColor: UIColor.black, borderWidth: 0.5)
- let imageSize = avatarImageView?.bounds.size
- UIGraphicsBeginImageContext(imageSize!)
- let context = UIGraphicsGetCurrentContext()
- avatarImageView?.layer.render(in: context!)
- avatar = UIGraphicsGetImageFromCurrentImageContext()
- UIGraphicsEndImageContext()
- cell.user.image = avatar
- }
- }
- }
-
- return cell
- }
- }
-
- @objc func getMetadataFromSectionDataSourceIndexPath(_ indexPath: IndexPath?, sectionDataSource: CCSectionDataSourceMetadata?) -> tableMetadata? {
-
- guard let indexPath = indexPath else {
- return nil
- }
-
- guard let sectionDataSource = sectionDataSource else {
- return nil
- }
-
- let section = indexPath.section + 1
- let row = indexPath.row + 1
- let totSections = sectionDataSource.sections.count
-
- if totSections < section || section > totSections {
- return nil
- }
-
- let valueSection = sectionDataSource.sections.object(at: indexPath.section)
- guard let filesID = sectionDataSource.sectionArrayRow.object(forKey: valueSection) as? NSArray else {
- return nil
- }
-
- let totRows = filesID.count
- if totRows < row || row > totRows {
- return nil
- }
-
- let fileID = filesID.object(at: indexPath.row)
- let metadata = sectionDataSource.allRecordsDataSource.object(forKey: fileID) as? tableMetadata
-
- return metadata
- }
-
- @objc func reloadDatasource(ServerUrl: String?, fileID: String?, action: Int32) {
-
- if operationQueueReloadDatasource.operationCount > 0 {
- return
- }
-
-
- if self.appDelegate.activeMain != nil && ServerUrl != nil && self.appDelegate.activeMain.serverUrl == ServerUrl {
- self.operationQueueReloadDatasource.addOperation {
- DispatchQueue.main.async {
- self.appDelegate.activeMain.reloadDatasource(ServerUrl, fileID: fileID, action: Int(action))
- }
- }
- }
- if self.appDelegate.activeFavorites != nil && self.appDelegate.activeFavorites.viewIfLoaded?.window != nil {
- self.operationQueueReloadDatasource.addOperation {
- DispatchQueue.main.async {
- self.appDelegate.activeFavorites.reloadDatasource(fileID, action: Int(action))
- }
- }
- }
- if self.appDelegate.activeTransfers != nil && self.appDelegate.activeTransfers.viewIfLoaded?.window != nil {
- self.operationQueueReloadDatasource.addOperation {
- DispatchQueue.main.async {
- self.appDelegate.activeTransfers.reloadDatasource(fileID, action: Int(action))
- }
- }
- }
- }
-
- @objc func isValidIndexPath(_ indexPath: IndexPath, view: Any) -> Bool {
-
- if view is UICollectionView {
- return indexPath.section < (view as! UICollectionView).numberOfSections && indexPath.row < (view as! UICollectionView).numberOfItems(inSection: indexPath.section)
- }
-
- if view is UITableView {
- return indexPath.section < (view as! UITableView).numberOfSections && indexPath.row < (view as! UITableView).numberOfRows(inSection: indexPath.section)
- }
-
- return true
- }
-
- //MARK: -
-
- @objc func deleteFile(metadatas: NSArray, e2ee: Bool, serverUrl: String, folderFileID: String, completion: @escaping (_ errorCode: Int, _ message: String)->()) {
-
- var copyMetadatas = [tableMetadata]()
-
- for metadata in metadatas {
- copyMetadatas.append(tableMetadata.init(value: metadata))
- }
-
- if e2ee {
- DispatchQueue.global().async {
- 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)
- DispatchQueue.main.async {
- if error == nil {
- self.delete(metadatas: copyMetadatas, serverUrl:serverUrl, e2ee: e2ee, completion: completion)
- } else {
- self.appDelegate.messageNotification("_delete_", description: error?.localizedDescription, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
- return
- }
- }
- }
- } else {
- delete(metadatas: copyMetadatas, serverUrl:serverUrl, e2ee: e2ee, completion: completion)
- }
- }
-
- private func delete(metadatas: [tableMetadata], serverUrl: String, e2ee: Bool, completion: @escaping (_ errorCode: Int, _ message: String)->()) {
-
- var count: Int = 0
- var completionErrorCode: Int = 0
- var completionMessage = ""
-
- for metadata in metadatas {
-
- self.appDelegate.filterFileID.add(metadata.fileID)
-
- let path = metadata.serverUrl + "/" + metadata.fileName
-
- OCNetworking.sharedManager().deleteFileOrFolder(withAccount: appDelegate.activeAccount, path: path, completion: { (account, message, errorCode) in
-
- if account == self.appDelegate.activeAccount {
-
- count += 1
- if errorCode == 0 || errorCode == kOCErrorServerPathNotFound {
-
- do {
- try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
- } catch { }
-
- NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
- NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
-
- if metadata.directory {
- NCManageDatabase.sharedInstance.deleteDirectoryAndSubDirectory(serverUrl: CCUtility.stringAppendServerUrl(serverUrl, addFileName: metadata.fileName), account: metadata.account)
- }
-
- if (e2ee) {
- NCManageDatabase.sharedInstance.deleteE2eEncryption(predicate: NSPredicate(format: "account == %@ AND serverUrl == %@ AND fileNameIdentifier == %@", metadata.account, serverUrl, metadata.fileName))
- }
-
- self.appDelegate.filterFileID.remove(metadata.fileID)
-
- } else {
-
- completionErrorCode = errorCode
- completionMessage = ""
- if message != nil {
- completionMessage = message!
- }
-
- self.appDelegate.filterFileID.remove(metadata.fileID)
- }
-
- if count == metadatas.count {
- if e2ee {
- DispatchQueue.global().async {
- 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)
- DispatchQueue.main.async {
- completion(completionErrorCode, completionMessage)
- }
- }
- } else {
- completion(completionErrorCode, completionMessage)
- }
- }
- }
- })
- }
-
- self.reloadDatasource(ServerUrl: serverUrl, fileID: nil, action: k_action_NULL)
- }
-
- @objc func editPhoto(_ metadata: tableMetadata, viewController: UIViewController) {
- guard let path = CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView) else {
- return
- }
- guard let image = UIImage(contentsOfFile: path) else {
- return
- }
-
- self.metadataEditPhoto = metadata
- let photoEditor = PhotoEditorViewController(nibName:"PhotoEditorViewController",bundle: Bundle(for: PhotoEditorViewController.self))
-
- photoEditor.image = image
- photoEditor.photoEditorDelegate = self
- photoEditor.hiddenControls = [.save, .share, .sticker]
-
- photoEditor.cancelButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorCancel")!, multiplier:2, color: .white)
- photoEditor.cropButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorCrop")!, multiplier:2, color: .white)
- photoEditor.drawButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorDraw")!, multiplier:2, color: .white)
- photoEditor.textButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorText")!, multiplier:2, color: .white)
- photoEditor.clearButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorClear")!, multiplier:2, color: .white)
- photoEditor.continueButtonImage = CCGraphics.changeThemingColorImage(UIImage(named: "photoEditorDone")!, multiplier:2, color: .white)
-
- viewController.present(photoEditor, animated: true, completion: nil)
- }
-
- func doneEditing(image: UIImage) {
- guard let metadata = self.metadataEditPhoto else {
- return
- }
- guard let path = CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView) else {
- return
- }
- guard let filetype = NCUtility.sharedInstance.isEditImage(metadata.fileNameView as NSString) else {
- return
- }
- if filetype == "PNG" {
- do {
- try image.pngData()?.write(to: path.url, options: .atomic)
- } catch { return }
- } else if filetype == "JPG" {
- let imageData = image.jpegData(compressionQuality: 1)
- do {
- try imageData?.write(to: path.url)
- } catch { return }
- }
- // write icon
- CCGraphics.createNewImage(from: metadata.fileNameView, fileID: metadata.fileID, extension: filetype, filterGrayScale: false, typeFile: metadata.typeFile, writeImage: true)
- // upload
- metadata.session = k_upload_session
- metadata.sessionSelector = selectorUploadFile
- metadata.status = Int(k_metadataStatusWaitUpload)
-
- _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
- }
-
- func canceledEditing() {
- print("Canceled")
- }
-
- //MARK: - OpenIn
-
- @objc func downloadOpenIn(metadata: tableMetadata) {
-
- if CCUtility.fileProviderStorageExists(metadata.fileID, fileNameView: metadata.fileNameView) {
-
- openIn(metadata: metadata)
-
- } else {
-
- metadata.session = k_download_session
- metadata.sessionError = ""
- metadata.sessionSelector = selectorOpenIn
- metadata.status = Int(k_metadataStatusWaitDownload)
-
- _ = NCManageDatabase.sharedInstance.addMetadata(metadata)
- reloadDatasource(ServerUrl: metadata.serverUrl, fileID: metadata.fileID, action: k_action_MOD)
- }
- }
- func openIn(metadata: tableMetadata) {
-
- docController = UIDocumentInteractionController(url: NSURL(fileURLWithPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView)) as URL)
- docController?.delegate = self
-
- guard let splitViewController = self.appDelegate.window?.rootViewController as? UISplitViewController else {
- return
- }
-
- guard let view = splitViewController.viewControllers.first?.view else {
- return
- }
-
- guard let frame = splitViewController.viewControllers.first?.view.frame else {
- return
- }
-
- docController?.presentOptionsMenu(from: frame, in: view, animated: true)
- }
-
- //MARK: - NCAudioRecorder
-
- func startAudioRecorder() {
-
- let fileName = CCUtility.createFileNameDate(NSLocalizedString("_voice_memo_filename_", comment: ""), extension: "m4a")!
- let viewController = UIStoryboard(name: "NCAudioRecorderViewController", bundle: nil).instantiateInitialViewController() as! NCAudioRecorderViewController
-
- viewController.delegate = self
- viewController.createRecorder(fileName: fileName)
- viewController.modalTransitionStyle = .crossDissolve
- viewController.modalPresentationStyle = UIModalPresentationStyle.overCurrentContext
-
- self.appDelegate.window.rootViewController?.present(viewController, animated: true, completion: nil)
- }
-
- func didFinishRecording(_ viewController: NCAudioRecorderViewController, fileName: String) {
-
- guard let navigationController = UIStoryboard(name: "NCCreateFormUploadVoiceNote", bundle: nil).instantiateInitialViewController() else { return }
- navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
-
- let viewController = (navigationController as! UINavigationController).topViewController as! NCCreateFormUploadVoiceNote
- viewController.setup(serverUrl: appDelegate.activeMain.serverUrl, fileNamePath: NSTemporaryDirectory() + fileName, fileName: fileName)
- self.appDelegate.window.rootViewController?.present(navigationController, animated: true, completion: nil)
- }
- }
-
- //MARK: - Main TabBarController
- class CCMainTabBarController : UITabBarController, UITabBarControllerDelegate {
-
- override func viewDidLoad() {
-
- super.viewDidLoad()
- delegate = self
- }
-
- //Delegate methods
- func tabBarController(_ tabBarController: UITabBarController, shouldSelect viewController: UIViewController) -> Bool {
-
- let tabViewControllers = tabBarController.viewControllers!
- guard let toIndex = tabViewControllers.firstIndex(of: viewController) else {
-
- if let vc = viewController as? UINavigationController {
- vc.popToRootViewController(animated: true);
- }
-
- return false
- }
-
- animateToTab(toIndex: toIndex)
-
- return true
- }
-
- func animateToTab(toIndex: Int) {
-
- let tabViewControllers = viewControllers!
- let fromView = selectedViewController!.view!
- let toView = tabViewControllers[toIndex].view!
- let fromIndex = tabViewControllers.firstIndex(of: selectedViewController!)
-
- guard fromIndex != toIndex else {return}
-
- // Add the toView to the tab bar view
- fromView.superview?.addSubview(toView)
- fromView.superview?.backgroundColor = UIColor.white
-
- // Position toView off screen (to the left/right of fromView)
- let screenWidth = UIScreen.main.bounds.size.width;
- let scrollRight = toIndex > fromIndex!;
- let offset = (scrollRight ? screenWidth : -screenWidth)
- toView.center = CGPoint(x: (fromView.center.x) + offset, y: (toView.center.y))
-
- // Disable interaction during animation
- view.isUserInteractionEnabled = false
-
- UIView.animate(withDuration: 0.3, delay: 0.0, usingSpringWithDamping: 1, initialSpringVelocity: 0, options: UIView.AnimationOptions.curveEaseOut, animations: {
-
- // Slide the views by -offset
- fromView.center = CGPoint(x: fromView.center.x - offset, y: fromView.center.y);
- toView.center = CGPoint(x: toView.center.x - offset, y: toView.center.y);
-
- }, completion: { finished in
-
- // Remove the old view from the tabbar view.
- fromView.removeFromSuperview()
- self.selectedIndex = toIndex
- self.view.isUserInteractionEnabled = true
- })
- }
- }
- //
- // https://stackoverflow.com/questions/44822558/ios-11-uitabbar-uitabbaritem-positioning-issue/46348796#46348796
- //
- extension UITabBar {
- // Workaround for iOS 11's new UITabBar behavior where on iPad, the UITabBar inside
- // the Master view controller shows the UITabBarItem icon next to the text
- override open var traitCollection: UITraitCollection {
- if UIDevice.current.userInterfaceIdiom == .pad {
- return UITraitCollection(horizontalSizeClass: .compact)
- }
- return super.traitCollection
- }
- }
- //MARK: - Networking Main
- class NCNetworkingMain: NSObject, CCNetworkingDelegate {
- @objc static let sharedInstance: NCNetworkingMain = {
- let instance = NCNetworkingMain()
- return instance
- }()
-
- lazy var operationQueueNetworkingMain: OperationQueue = {
- let queue = OperationQueue()
- queue.name = "com.nextcloud.operationQueueNetworkingMain"
- queue.maxConcurrentOperationCount = 1
- return queue
- }()
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- // DOWNLOAD
- func downloadStart(_ fileID: String!, account: String!, task: URLSessionDownloadTask!, serverUrl: String!) {
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
- appDelegate.updateApplicationIconBadgeNumber()
- }
-
- func downloadFileSuccessFailure(_ fileName: String!, fileID: String!, serverUrl: String!, selector: String!, errorMessage: String!, errorCode: Int) {
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", fileID)) else {
- return
- }
-
- if metadata.account != appDelegate.activeAccount {
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
- return
- }
-
- if errorCode == 0 {
-
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
-
- // Synchronized
- if selector == selectorDownloadSynchronize {
- appDelegate.updateApplicationIconBadgeNumber()
- appDelegate.startLoadAutoDownloadUpload()
- return
- }
-
- // Modify Photo
- if selector == selectorDownloadEditPhoto {
- NCMainCommon.sharedInstance.editPhoto(metadata, viewController: appDelegate.activeMain)
- return
- }
-
- // open View File
- if selector == selectorLoadFileView && UIApplication.shared.applicationState == UIApplication.State.active {
-
- var uti = CCUtility.insertTypeFileIconName(metadata.fileNameView, metadata: metadata)
- if uti == nil {
- uti = ""
- } else if uti!.contains("opendocument") && !NCViewerRichdocument.sharedInstance.isRichDocument(metadata) {
- metadata.typeFile = k_metadataTypeFile_unknown
- }
-
- if metadata.typeFile == k_metadataTypeFile_imagemeter {
-
- let source = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID, fileNameView: metadata.fileNameView))
- let destination = URL(fileURLWithPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
-
- try? FileManager().unzipItem(at: source, to: destination)
-
- let nameArchiveImagemeter = (metadata.fileNameView as NSString).deletingPathExtension
- let pathArchiveImagemeter = CCUtility.getDirectoryProviderStorageFileID(metadata.fileID) + "/" + nameArchiveImagemeter
- let annoPath = (pathArchiveImagemeter + "/anno-" + nameArchiveImagemeter + ".imm")
-
- if let fileHandle = FileHandle(forReadingAtPath: annoPath) {
- let dataFormat = fileHandle.readData(ofLength: 1)
- if dataFormat.starts(with: [0x01]) {
- appDelegate.messageNotification("_error_", description: "File format binary error, library imagemeter not present. 🤷♂️", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- return;
- }
- let dataZip = fileHandle.readData(ofLength: 4)
- if dataZip.starts(with: [0x50, 0x4b, 0x03, 0x04]) {
- try? FileManager().unzipItem(at: annoPath.url, to: pathArchiveImagemeter.url)
- }
- fileHandle.closeFile()
- }
- }
-
- if metadata.typeFile == k_metadataTypeFile_compress || metadata.typeFile == k_metadataTypeFile_unknown {
- NCMainCommon.sharedInstance.openIn(metadata: metadata)
-
- } else {
-
- if appDelegate.activeMain.view.window != nil {
- appDelegate.activeMain.shouldPerformSegue(metadata)
- }
- if appDelegate.activeFavorites.view.window != nil {
- appDelegate.activeFavorites.shouldPerformSegue(metadata)
- }
- }
- }
-
- // Open in...
- if selector == selectorOpenIn && UIApplication.shared.applicationState == UIApplication.State.active {
- NCMainCommon.sharedInstance.openIn(metadata: metadata)
- }
-
- // Save to Photo Album
- if selector == selectorSave {
-
- appDelegate.activeMain.save(toPhotoAlbum: metadata)
- }
-
- // Copy File
- if selector == selectorLoadCopy {
-
- appDelegate.activeMain.copyFile(toPasteboard: metadata)
- }
-
- // Set as available offline
- if selector == selectorLoadOffline {
-
- NCManageDatabase.sharedInstance.setLocalFile(fileID: metadata.fileID, offline: true)
- }
-
- //selectorLoadViewImage
- if selector == selectorLoadViewImage {
-
- if appDelegate.activeDetail != nil {
- appDelegate.activeDetail.downloadPhotoBrowserSuccessFailure(metadata, selector: selector, errorCode: errorCode)
- }
- }
-
- appDelegate.startLoadAutoDownloadUpload()
-
- } else {
-
- // File do not exists on server, remove in local
- if (errorCode == kOCErrorServerPathNotFound || errorCode == -1011) { // - 1011 = kCFURLErrorBadServerResponse
-
- do {
- try FileManager.default.removeItem(atPath: CCUtility.getDirectoryProviderStorageFileID(metadata.fileID))
- } catch { }
-
- NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
- NCManageDatabase.sharedInstance.deleteLocalFile(predicate: NSPredicate(format: "fileID == %@", metadata.fileID))
-
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_DEL))
- }
-
- if selector == selectorLoadViewImage {
-
- if appDelegate.activeDetail.view.window != nil {
- appDelegate.activeDetail.downloadPhotoBrowserSuccessFailure(metadata, selector: selector, errorCode: errorCode)
- }
-
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
- }
- }
-
- appDelegate.startLoadAutoDownloadUpload()
- }
-
- // UPLOAD
-
- func uploadStart(_ fileID: String!, account: String!, task: URLSessionUploadTask!, serverUrl: String!) {
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
- appDelegate.updateApplicationIconBadgeNumber()
- }
-
- func uploadFileSuccessFailure(_ fileName: String!, fileID: String!, assetLocalIdentifier: String!, serverUrl: String!, selector: String!, errorMessage: String!, errorCode: Int) {
-
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", fileID)) else {
- return
- }
-
- if metadata.account != appDelegate.activeAccount {
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
- return
- }
-
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: serverUrl, fileID: fileID, action: Int32(k_action_MOD))
-
- if errorCode == 0 {
- appDelegate.startLoadAutoDownloadUpload()
- } else {
- if errorCode != -999 && errorCode != kOCErrorServerUnauthorized && errorMessage != "" {
- appDelegate.messageNotification("_upload_file_", description: errorMessage, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: errorCode)
- }
- }
- }
-
- @objc func downloadThumbnail(with metadata: tableMetadata, view: Any, indexPath: IndexPath) {
- operationQueueNetworkingMain.addOperation(NCOperationNetworkingMain.init(metadata: metadata, view: view, indexPath: indexPath, networkingFunc: "downloadThumbnail"))
- }
-
- func downloadThumbnail(with metadata: tableMetadata, view: Any, indexPath: IndexPath, closure: @escaping () -> ()) {
-
- if !metadata.isInvalidated && metadata.hasPreview == 1 && (!CCUtility.fileProviderStorageIconExists(metadata.fileID, fileNameView: metadata.fileName) || metadata.typeFile == k_metadataTypeFile_document) {
-
- let width = NCUtility.sharedInstance.getScreenWidthForPreview()
- let height = NCUtility.sharedInstance.getScreenHeightForPreview()
-
- OCNetworking.sharedManager().downloadPreview(withAccount: metadata.account, metadata: metadata, withWidth: width, andHeight: height, completion: { (account, image, message, errorCode) in
-
- if errorCode == 0 && account == self.appDelegate.activeAccount && !metadata.isInvalidated && CCUtility.fileProviderStorageIconExists(metadata.fileID, fileNameView: metadata.fileName) {
-
- if view is UICollectionView && NCMainCommon.sharedInstance.isValidIndexPath(indexPath, view: view) {
- if let cell = (view as! UICollectionView).cellForItem(at: indexPath) {
- if cell is NCListCell {
- (cell as! NCListCell).imageItem.image = image
- } else if cell is NCGridCell {
- (cell as! NCGridCell).imageItem.image = image
- } else if cell is NCGridMediaCell {
- (cell as! NCGridMediaCell).imageItem.image = image
- }
- }
- }
-
- if view is UITableView && CCUtility.fileProviderStorageIconExists(metadata.fileID, fileNameView: metadata.fileName) && NCMainCommon.sharedInstance.isValidIndexPath(indexPath, view: view) {
- if let cell = (view as! UITableView).cellForRow(at: indexPath) {
- if cell is CCCellMainTransfer {
- (cell as! CCCellMainTransfer).file.image = image
- } else if cell is CCCellMain {
- (cell as! CCCellMain).file.image = image
- }
- }
- }
- }
-
- return closure()
- })
- }
-
- return closure()
- }
- }
- //MARK: - Operation Networking Main
- class NCOperationNetworkingMain: Operation {
-
- private var _executing : Bool = false
- override var isExecuting : Bool {
- get { return _executing }
- set {
- guard _executing != newValue else { return }
- willChangeValue(forKey: "isExecuting")
- _executing = newValue
- didChangeValue(forKey: "isExecuting")
- }
- }
- private var _finished : Bool = false
- override var isFinished : Bool {
- get { return _finished }
- set {
- guard _finished != newValue else { return }
- willChangeValue(forKey: "isFinished")
- _finished = newValue
- didChangeValue(forKey: "isFinished")
- }
- }
-
- private var metadata: tableMetadata?
- private var view: Any?
- private var indexPath: IndexPath?
- private var networkingFunc: String = ""
- init(metadata: tableMetadata?, view: Any?, indexPath: IndexPath?, networkingFunc: String) {
- super.init()
-
- if metadata != nil { self.metadata = metadata! }
- if view != nil { self.view = view! }
- if indexPath != nil { self.indexPath = indexPath! }
-
- self.networkingFunc = networkingFunc
- }
-
- override func start() {
- if !Thread.isMainThread {
-
- self.performSelector(onMainThread:#selector(start), with: nil, waitUntilDone: false)
-
- } else {
-
- isExecuting = true
-
- if isCancelled {
- finish()
- } else {
- poolNetworking()
- }
- }
- }
-
- func finish() {
- isExecuting = false
- isFinished = true
- }
-
- override func cancel() {
- super.cancel()
-
- if isExecuting {
- complete()
- }
- }
-
- func complete() {
- finish()
-
- UIApplication.shared.isNetworkActivityIndicatorVisible = false
- }
-
- func poolNetworking() {
- UIApplication.shared.isNetworkActivityIndicatorVisible = true
-
- switch networkingFunc {
- case "downloadThumbnail":
- NCNetworkingMain.sharedInstance.downloadThumbnail(with: metadata!, view: view!, indexPath: indexPath!) {
- self.complete()
- }
- default:
- print("error")
- }
- }
- }
- //MARK: - Function Main
- class NCFunctionMain: NSObject {
-
- @objc static let sharedInstance: NCFunctionMain = {
- let instance = NCFunctionMain()
- return instance
- }()
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
-
- @objc func synchronizeOffline() {
-
- let directories = NCManageDatabase.sharedInstance.getTablesDirectory(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "serverUrl", ascending: true)
- if (directories != nil) {
- for directory: tableDirectory in directories! {
- CCSynchronize.shared()?.readFolder(directory.serverUrl, selector: selectorReadFolderWithDownload, account: appDelegate.activeAccount)
- }
- }
-
- let files = NCManageDatabase.sharedInstance.getTableLocalFiles(predicate: NSPredicate(format: "account == %@ AND offline == true", appDelegate.activeAccount), sorted: "fileName", ascending: true)
- if (files != nil) {
- for file: tableLocalFile in files! {
- guard let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "fileID == %@", file.fileID)) else {
- continue
- }
- CCSynchronize.shared()?.readFile(metadata.fileID, fileName: metadata.fileName, serverUrl: metadata.serverUrl, selector: selectorReadFileWithDownload, account: appDelegate.activeAccount)
- }
- }
- }
- }
|