123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993 |
- //
- // CCCreateCloud.swift
- // Nextcloud
- //
- // Created by Marino Faggiana on 09/01/17.
- // Copyright © 2017 TWS. All rights reserved.
- //
- // Author Marino Faggiana <m.faggiana@twsweb.it>
- //
- // 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
- // MARK: -
- class CreateMenuAdd: NSObject {
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- let fontButton = [NSAttributedStringKey.font:UIFont(name: "HelveticaNeue", size: 16)!, NSAttributedStringKey.foregroundColor: UIColor.black]
- let fontEncrypted = [NSAttributedStringKey.font:UIFont(name: "HelveticaNeue", size: 16)!, NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.encrypted as UIColor]
- let fontCancel = [NSAttributedStringKey.font:UIFont(name: "HelveticaNeue-Bold", size: 17)!, NSAttributedStringKey.foregroundColor: UIColor.black]
- let fontDisable = [NSAttributedStringKey.font:UIFont(name: "HelveticaNeue", size: 16)!, NSAttributedStringKey.foregroundColor: UIColor.darkGray]
- let colorLightGray = UIColor(red: 250.0/255.0, green: 250.0/255.0, blue: 250.0/255.0, alpha: 1)
- let colorGray = UIColor(red: 150.0/255.0, green: 150.0/255.0, blue: 150.0/255.0, alpha: 1)
- var colorIcon = NCBrandColor.sharedInstance.brandElement
-
- @objc init (themingColor : UIColor) {
-
- super.init()
- colorIcon = themingColor
- }
-
- @objc func createMenu(view : UIView) {
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- let actionSheet = AHKActionSheet.init(view: view, title: nil)!
-
- actionSheet.animationDuration = 0.2
- actionSheet.automaticallyTintButtonImages = 0
-
- actionSheet.buttonHeight = 50.0
- actionSheet.cancelButtonHeight = 50.0
- actionSheet.separatorHeight = 5.0
-
- actionSheet.separatorColor = NCBrandColor.sharedInstance.seperator
-
- actionSheet.buttonTextAttributes = fontButton
- actionSheet.encryptedButtonTextAttributes = fontEncrypted
- actionSheet.cancelButtonTextAttributes = fontCancel
- actionSheet.disableButtonTextAttributes = fontDisable
-
- actionSheet.cancelButtonTitle = NSLocalizedString("_cancel_", comment: "")
-
- actionSheet.addButton(withTitle: NSLocalizedString("_upload_photos_videos_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "media"), multiplier:2, color: colorGray), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
- appDelegate.activeMain.openAssetsPickerController()
- })
-
- #if DEBUG
- if #available(iOS 11.0, *) {
- actionSheet.addButton(withTitle: NSLocalizedString("_scans_document_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "scan"), multiplier:2, color: colorGray), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
- NCCreateScanDocument.sharedInstance.openScannerDocument(viewController: appDelegate.activeMain, openScan: true)
- })
- }
- #endif
-
- actionSheet.addButton(withTitle: NSLocalizedString("_upload_file_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "file"), multiplier:2, color: colorGray), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
- appDelegate.activeMain.openImportDocumentPicker()
- })
-
- actionSheet.addButton(withTitle: NSLocalizedString("_upload_file_text_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "file_txt"), multiplier:2, color: colorGray), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
-
- let storyboard = UIStoryboard(name: "NCText", bundle: nil)
- let controller = storyboard.instantiateViewController(withIdentifier: "NCText")
- controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
- appDelegate.activeMain.present(controller, animated: true, completion: nil)
- })
-
- actionSheet.addButton(withTitle: NSLocalizedString("_create_folder_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "folder"), multiplier:2, color: colorIcon), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0 ,type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
- appDelegate.activeMain.createFolder()
- })
-
- actionSheet.show()
- }
- }
- // MARK: -
- @objc protocol createFormUploadAssetsDelegate {
-
- func dismissFormUploadAssets()
- }
- // MARK: -
- class CreateFormUploadAssets: XLFormViewController, CCMoveDelegate {
-
- var serverUrl : String = ""
- var titleServerUrl : String?
- var assets: NSMutableArray = []
- var cryptated : Bool = false
- var session : String = ""
- weak var delegate: createFormUploadAssetsDelegate?
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- @objc convenience init(serverUrl : String, assets : NSMutableArray, cryptated : Bool, session : String, delegate: createFormUploadAssetsDelegate) {
-
- self.init()
-
- if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
- titleServerUrl = "/"
- } else {
- titleServerUrl = (serverUrl as NSString).lastPathComponent
- }
-
- self.serverUrl = serverUrl
- self.assets = assets
- self.cryptated = cryptated
- self.session = session
- self.delegate = delegate
-
- self.initializeForm()
- }
-
- //MARK: XLFormDescriptorDelegate
- func initializeForm() {
- let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
- form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
- var section : XLFormSectionDescriptor
- var row : XLFormRowDescriptor
-
- // Section: Destination Folder
-
- section = XLFormSectionDescriptor.formSection()
- form.addFormSection(section)
-
- row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
- let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
- row.cellConfig.setObject(imageFolder, forKey: "imageView.image" as NSCopying)
- row.cellConfig.setObject(UIColor.black, forKey: "textLabel.textColor" as NSCopying)
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- row.action.formSelector = #selector(changeDestinationFolder(_:))
- section.addFormRow(row)
-
- // Section Switch
-
- section = XLFormSectionDescriptor.formSection()
- form.addFormSection(section)
-
- // Folder Photo
-
- row = XLFormRowDescriptor(tag: "useFolderMedia", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_use_folder_media_", comment: ""))
- row.value = 0
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- section.addFormRow(row)
-
- // Use Sub folder
- row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
- row.hidden = "$\("useFolderMedia") == 0"
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
-
- let tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
-
- if tableAccount?.autoUploadCreateSubfolder == true {
- row.value = 1
- } else {
- row.value = 0
- }
- section.addFormRow(row)
- // Maintain the original fileName
-
- row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
- row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginal)
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- section.addFormRow(row)
-
- // Add File Name Type
- row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
- row.hidden = "$\("maintainOriginalFileName") == 1"
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- row.value = CCUtility.getFileNameType(k_keyFileNameType)
- section.addFormRow(row)
-
- // Section: Rename File Name
-
- section = XLFormSectionDescriptor.formSection()
- form.addFormSection(section)
-
- row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: ""))+":")
- row.hidden = "$\("maintainOriginalFileName") == 1"
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
- if fileNameMask.count > 0 {
- row.value = fileNameMask
- }
- section.addFormRow(row)
-
- // Section: Preview File Name
-
- row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
- row.height = 180
- row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "backgroundColor" as NSCopying)
- row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "textView.backgroundColor" as NSCopying)
- row.disabled = true
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- section.addFormRow(row)
-
- self.form = form
- }
-
- override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
-
- super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
-
- if formRow.tag == "useFolderMedia" {
-
- if (formRow.value! as AnyObject).boolValue == true {
-
- let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
- buttonDestinationFolder.hidden = true
-
- } else{
-
- let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
- buttonDestinationFolder.hidden = false
- }
- }
- else if formRow.tag == "useSubFolder" {
-
- if (formRow.value! as AnyObject).boolValue == true {
-
- } else{
-
- }
- }
- else if formRow.tag == "maintainOriginalFileName" {
- CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: k_keyFileNameOriginal)
- self.reloadForm()
- }
- else if formRow.tag == "addFileNameType" {
- CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
- self.reloadForm()
- }
- else if formRow.tag == "maskFileName" {
-
- let fileName = formRow.value as? String
-
- self.form.delegate = nil
-
- if let fileName = fileName {
- formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
- }
-
- self.form.delegate = self
-
- let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
- previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
-
- // reload cell
- if fileName != nil {
-
- if newValue as! String != formRow.value as! String {
-
- self.reloadFormRow(formRow)
-
- appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
- }
- }
-
- self.reloadFormRow(previewFileName)
- }
- }
-
- // MARK: - View Life Cycle
- override func viewDidLoad() {
-
- super.viewDidLoad()
-
- let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancel))
- let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
-
- self.navigationItem.leftBarButtonItem = cancelButton
- self.navigationItem.rightBarButtonItem = saveButton
-
- self.navigationController?.navigationBar.isTranslucent = false
- self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
- self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
- self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
-
- self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
-
- self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
-
- self.reloadForm()
- }
-
- override func viewWillDisappear(_ animated: Bool)
- {
- super.viewWillDisappear(animated)
- self.delegate?.dismissFormUploadAssets()
- }
- func reloadForm() {
-
- self.form.delegate = nil
-
- let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
- buttonDestinationFolder.title = self.titleServerUrl
-
- let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
- let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
- previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
-
- self.tableView.reloadData()
- self.form.delegate = self
- }
- // MARK: - Action
- func moveServerUrl(to serverUrlTo: String!, title: String!) {
-
- self.serverUrl = serverUrlTo
-
- if let title = title {
-
- self.titleServerUrl = title
-
- } else {
-
- self.titleServerUrl = "/"
- }
-
- self.reloadForm()
- }
-
- @objc func save() {
-
- self.dismiss(animated: true, completion: {
-
- let useFolderPhotoRow : XLFormRowDescriptor = self.form.formRow(withTag: "useFolderMedia")!
- let useSubFolderRow : XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
- var useSubFolder : Bool = false
-
- if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
-
- self.serverUrl = NCManageDatabase.sharedInstance.getAccountAutoUploadPath(self.appDelegate.activeUrl)
- useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
- }
-
- self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
- })
- }
- @objc func cancel() {
-
- self.dismiss(animated: true, completion: nil)
- }
-
- // MARK: - Utility
-
- func previewFileName(valueRename : String?) -> String {
-
- var returnString: String = ""
- let asset = assets[0] as! PHAsset
-
- if (CCUtility.getOriginalFileName(k_keyFileNameOriginal)) {
-
- return (NSLocalizedString("_filename_", comment: "") + ": " + (asset.value(forKey: "filename") as! String))
-
- } else if let valueRename = valueRename {
-
- let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
-
- if valueRenameTrimming.count > 0 {
-
- self.form.delegate = nil
- CCUtility.setFileNameMask(valueRename, key: k_keyFileNameMask)
- self.form.delegate = self
-
- returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
-
- } else {
-
- CCUtility.setFileNameMask("", key: k_keyFileNameMask)
- returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
- }
-
- } else {
-
- CCUtility.setFileNameMask("", key: k_keyFileNameMask)
- returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
- }
-
- return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
- }
-
- @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
-
- self.deselectFormRow(sender)
-
- let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
- let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
- let viewController : CCMove = navigationController.topViewController as! CCMove
-
- viewController.delegate = self;
- viewController.tintColor = NCBrandColor.sharedInstance.brandText
- viewController.barTintColor = NCBrandColor.sharedInstance.brand
- viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
- viewController.move.title = NSLocalizedString("_select_", comment: "");
- viewController.networkingOperationQueue = appDelegate.netQueue
- // E2EE
- viewController.includeDirectoryE2EEncryption = true;
-
- navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
- self.present(navigationController, animated: true, completion: nil)
- }
-
- }
- // MARK: -
- class CreateFormUploadFileText: XLFormViewController, CCMoveDelegate {
-
- var serverUrl = ""
- var titleServerUrl = ""
- var fileName = ""
- var text = ""
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
-
- convenience init(serverUrl: String, text: String, fileName: String) {
-
- self.init()
-
- if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
- titleServerUrl = "/"
- } else {
- titleServerUrl = (serverUrl as NSString).lastPathComponent
- }
-
- self.fileName = fileName
- self.serverUrl = serverUrl
- self.text = text
-
- initializeForm()
- }
-
- //MARK: XLFormDescriptorDelegate
-
- func initializeForm() {
-
- let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
- form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
-
- var section : XLFormSectionDescriptor
- var row : XLFormRowDescriptor
-
- // Section: Destination Folder
-
- section = XLFormSectionDescriptor.formSection()
- form.addFormSection(section)
- row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
- let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
- row.cellConfig.setObject(imageFolder, forKey: "imageView.image" as NSCopying)
- row.cellConfig.setObject(UIColor.black, forKey: "textLabel.textColor" as NSCopying)
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- row.action.formSelector = #selector(changeDestinationFolder(_:))
- section.addFormRow(row)
-
- // Section: File Name
-
- section = XLFormSectionDescriptor.formSection()
- form.addFormSection(section)
-
- row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- row.value = fileName
- section.addFormRow(row)
-
- self.form = form
- }
-
- override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
-
- super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
-
- if formRow.tag == "fileName" {
-
- self.form.delegate = nil
-
- if let fileNameNew = formRow.value {
- self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as! String)
- }
-
- self.title = fileName
- self.form.delegate = self
- }
- }
-
- // MARK: - View Life Cycle
-
- override func viewDidLoad() {
-
- super.viewDidLoad()
-
- let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
-
- self.navigationItem.rightBarButtonItem = saveButton
-
- self.navigationController?.navigationBar.isTranslucent = false
- self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
- self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
- self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
-
- self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
- self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
-
- self.reloadForm()
- }
-
- func reloadForm() {
-
- self.form.delegate = nil
-
- let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
- buttonDestinationFolder.title = self.titleServerUrl
-
- self.title = fileName
-
- self.tableView.reloadData()
-
- self.form.delegate = self
- }
-
- // MARK: - Action
-
- func moveServerUrl(to serverUrlTo: String!, title: String!) {
-
- self.serverUrl = serverUrlTo
-
- if let title = title {
-
- self.titleServerUrl = title
-
- } else {
-
- self.titleServerUrl = "/"
- }
-
- self.reloadForm()
- }
-
- @objc func save() {
-
- let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
- guard let name = rowFileName.value else {
- return
- }
- let ext = (name as! NSString).pathExtension.uppercased()
- var fileNameSave = ""
-
- if (ext == "") {
- fileNameSave = name as! String + ".txt"
- } else if (CCUtility.isDocumentModifiableExtension(ext)) {
- fileNameSave = name as! String
- } else {
- fileNameSave = (name as! NSString).deletingPathExtension + ".txt"
- }
-
- guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl) else {
- return
- }
- let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave))
-
- if (metadata != nil) {
-
- let alertController = UIAlertController(title: fileNameSave, message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: .alert)
-
- let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in
- }
-
- let overwriteAction = UIAlertAction(title: NSLocalizedString("_overwrite_", comment: ""), style: .cancel) { (action:UIAlertAction) in
- self.dismissAndUpload(fileNameSave, fileID: metadata!.fileID, directoryID: directoryID)
- }
-
- alertController.addAction(cancelAction)
- alertController.addAction(overwriteAction)
-
- self.present(alertController, animated: true, completion:nil)
-
- } else {
- let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl)!
- dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
- }
- }
-
- func dismissAndUpload(_ fileNameSave: String, fileID: String, directoryID: String) {
-
- self.dismiss(animated: true, completion: {
-
- let data = self.text.data(using: .utf8)
- let success = FileManager.default.createFile(atPath: CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave), contents: data, attributes: nil)
-
- if success {
-
- let metadataForUpload = tableMetadata()
-
- metadataForUpload.account = self.appDelegate.activeAccount
- metadataForUpload.date = NSDate()
- metadataForUpload.directoryID = directoryID
- metadataForUpload.fileID = fileID
- metadataForUpload.fileName = fileNameSave
- metadataForUpload.fileNameView = fileNameSave
- metadataForUpload.session = k_upload_session
- metadataForUpload.sessionSelector = selectorUploadFile
- metadataForUpload.status = Int(k_metadataStatusWaitUpload)
-
- _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
- self.appDelegate.perform(#selector(self.appDelegate.loadAutoDownloadUpload), on: Thread.main, with: nil, waitUntilDone: true)
-
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL))
-
- } else {
-
- self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
- }
- })
- }
-
- func cancel() {
-
- self.dismiss(animated: true, completion: nil)
- }
-
- @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
-
- self.deselectFormRow(sender)
-
- let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
- let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
- let viewController : CCMove = navigationController.topViewController as! CCMove
-
- viewController.delegate = self;
- viewController.tintColor = NCBrandColor.sharedInstance.brandText
- viewController.barTintColor = NCBrandColor.sharedInstance.brand
- viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
- viewController.move.title = NSLocalizedString("_select_", comment: "");
- viewController.networkingOperationQueue = appDelegate.netQueue
- // E2EE
- viewController.includeDirectoryE2EEncryption = true;
-
- navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
- self.present(navigationController, animated: true, completion: nil)
- }
- }
- //MARK: -
- class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
-
- var serverUrl = ""
- var titleServerUrl = ""
- var arrayFileName = [String]()
- var fileName = "scan.pdf"
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
-
- convenience init(serverUrl: String, arrayFileName: [String]) {
-
- self.init()
-
- if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
- titleServerUrl = "/"
- } else {
- titleServerUrl = (serverUrl as NSString).lastPathComponent
- }
-
- self.serverUrl = serverUrl
- self.arrayFileName = arrayFileName
-
- initializeForm()
- }
-
- //MARK: XLFormDescriptorDelegate
-
- func initializeForm() {
-
- let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
- form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
-
- var section : XLFormSectionDescriptor
- var row : XLFormRowDescriptor
-
- // Section: Destination Folder
-
- section = XLFormSectionDescriptor.formSection()
- form.addFormSection(section)
- row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
- let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
- row.cellConfig.setObject(imageFolder, forKey: "imageView.image" as NSCopying)
- row.cellConfig.setObject(UIColor.black, forKey: "textLabel.textColor" as NSCopying)
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- row.action.formSelector = #selector(changeDestinationFolder(_:))
- section.addFormRow(row)
-
- // Section: File Name
-
- section = XLFormSectionDescriptor.formSection()
- form.addFormSection(section)
-
- row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
- row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
- row.value = fileName
- section.addFormRow(row)
-
- self.form = form
- }
-
- override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
-
- super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
-
- if formRow.tag == "fileName" {
-
- self.form.delegate = nil
-
- if let fileNameNew = formRow.value {
- self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as! String)
- }
-
- self.title = fileName
-
- self.form.delegate = self
- }
- }
-
- // MARK: - View Life Cycle
-
- override func viewDidLoad() {
-
- super.viewDidLoad()
-
- let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
-
- self.navigationItem.rightBarButtonItem = saveButton
-
- self.navigationController?.navigationBar.isTranslucent = false
- self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
- self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
- self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
-
- self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
- self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
-
- self.reloadForm()
- }
-
- func reloadForm() {
-
- self.form.delegate = nil
-
- let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
- buttonDestinationFolder.title = self.titleServerUrl
-
- self.title = fileName
-
- self.tableView.reloadData()
-
- self.form.delegate = self
- }
-
- // MARK: - Action
-
- func moveServerUrl(to serverUrlTo: String!, title: String!) {
-
- self.serverUrl = serverUrlTo
-
- if let title = title {
-
- self.titleServerUrl = title
-
- } else {
-
- self.titleServerUrl = "/"
- }
-
- self.reloadForm()
- }
-
- @objc func save() {
-
- let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
- guard let name = rowFileName.value else {
- return
- }
- let ext = (name as! NSString).pathExtension.uppercased()
- var fileNameSave = ""
-
- if (ext == "") {
- fileNameSave = name as! String + ".pdf"
- } else {
- fileNameSave = (name as! NSString).deletingPathExtension + ".pdf"
- }
-
- guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl) else {
- return
- }
- let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave))
-
- if (metadata != nil) {
-
- let alertController = UIAlertController(title: fileNameSave, message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: .alert)
-
- let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in
- }
-
- let overwriteAction = UIAlertAction(title: NSLocalizedString("_overwrite_", comment: ""), style: .cancel) { (action:UIAlertAction) in
- self.dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
- }
-
- alertController.addAction(cancelAction)
- alertController.addAction(overwriteAction)
-
- self.present(alertController, animated: true, completion:nil)
-
- } else {
- let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl)!
- dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
- }
- }
-
- func dismissAndUpload(_ fileNameSave: String, fileID: String, directoryID: String) {
-
- var pdfPages = [PDFPage]()
- guard let fileNameGeneratePDF = CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave) else {
- self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
- return
- }
-
- //Generate PDF
- for fileNameImage in self.arrayFileName {
- let fileNameImagePath = CCUtility.getDirectoryScanSelect() + "/" + fileNameImage
- let page = PDFPage.imagePath(fileNameImagePath)
- pdfPages.append(page)
- }
-
- do {
- try PDFGenerator.generate(pdfPages, to: fileNameGeneratePDF)
- } catch {
- self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
- return
- }
-
- //Remove images processed
- for fileNameImage in self.arrayFileName {
- let fileNameImagePath = CCUtility.getDirectoryScanSelect() + "/" + fileNameImage
- CCUtility.removeFile(atPath: fileNameImagePath)
- }
-
- //Remove plist in reader for cache
- let filePlistReader = CCUtility.getDirectoryReaderMetadata() + "/" + (fileNameSave as NSString).deletingPathExtension + ".plist"
- CCUtility.removeFile(atPath: filePlistReader)
-
- let metadataForUpload = tableMetadata()
-
- metadataForUpload.account = self.appDelegate.activeAccount
- metadataForUpload.date = NSDate()
- metadataForUpload.directoryID = directoryID
- metadataForUpload.fileID = fileID
- metadataForUpload.fileName = fileNameSave
- metadataForUpload.fileNameView = fileNameSave
- metadataForUpload.session = k_upload_session
- metadataForUpload.sessionSelector = selectorUploadFile
- metadataForUpload.status = Int(k_metadataStatusWaitUpload)
-
- _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
- self.appDelegate.perform(#selector(self.appDelegate.loadAutoDownloadUpload), on: Thread.main, with: nil, waitUntilDone: true)
-
- NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL))
-
- self.dismiss(animated: true, completion: nil)
- }
-
- func cancel() {
-
- self.dismiss(animated: true, completion: nil)
- }
-
- @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
-
- self.deselectFormRow(sender)
-
- let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
- let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
- let viewController : CCMove = navigationController.topViewController as! CCMove
-
- viewController.delegate = self;
- viewController.tintColor = NCBrandColor.sharedInstance.brandText
- viewController.barTintColor = NCBrandColor.sharedInstance.brand
- viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
- viewController.move.title = NSLocalizedString("_select_", comment: "");
- viewController.networkingOperationQueue = appDelegate.netQueue
- // E2EE
- viewController.includeDirectoryE2EEncryption = true;
-
- navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
- self.present(navigationController, animated: true, completion: nil)
- }
- }
- class NCCreateScanDocument : NSObject, ImageScannerControllerDelegate {
-
- @objc static let sharedInstance: NCCreateScanDocument = {
- let instance = NCCreateScanDocument()
- return instance
- }()
-
- let appDelegate = UIApplication.shared.delegate as! AppDelegate
- var viewController: UIViewController?
- var openScan: Bool = false
-
- @available(iOS 10, *)
- func openScannerDocument(viewController: UIViewController, openScan: Bool) {
-
- self.viewController = viewController
- self.openScan = openScan
-
- let scannerVC = ImageScannerController()
- scannerVC.imageScannerDelegate = self
- self.viewController?.present(scannerVC, animated: true, completion: nil)
- }
-
- @available(iOS 10, *)
- func imageScannerController(_ scanner: ImageScannerController, didFinishScanningWithResults results: ImageScannerResults) {
-
- scanner.dismiss(animated: true, completion: {
-
- let fileName = CCUtility.createFileName("scan.png", fileDate: Date(), fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)!
- let fileNamePath = CCUtility.getDirectoryScan() + "/" + fileName
-
- do {
- try UIImagePNGRepresentation(results.scannedImage)?.write(to: NSURL.fileURL(withPath: fileNamePath), options: .atomic)
- } catch { }
-
- if (self.openScan) {
- let storyboard = UIStoryboard(name: "Scan", bundle: nil)
- let controller = storyboard.instantiateInitialViewController()!
-
- controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
- self.viewController?.present(controller, animated: true, completion: nil)
- }
- })
- }
-
- @available(iOS 10, *)
- func imageScannerControllerDidCancel(_ scanner: ImageScannerController) {
- scanner.dismiss(animated: true, completion: nil)
- }
-
- @available(iOS 10, *)
- func imageScannerController(_ scanner: ImageScannerController, didFailWithError error: Error) {
- appDelegate.messageNotification("_error_", description: error.localizedDescription, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
- print(error)
- }
- }
|