CCCreateCloud.swift 51 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198
  1. //
  2. // CCCreateCloud.swift
  3. // Nextcloud
  4. //
  5. // Created by Marino Faggiana on 09/01/17.
  6. // Copyright © 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <m.faggiana@twsweb.it>
  9. //
  10. // This program is free software: you can redistribute it and/or modify
  11. // it under the terms of the GNU General Public License as published by
  12. // the Free Software Foundation, either version 3 of the License, or
  13. // (at your option) any later version.
  14. //
  15. // This program is distributed in the hope that it will be useful,
  16. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. // GNU General Public License for more details.
  19. //
  20. // You should have received a copy of the GNU General Public License
  21. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. //
  23. import Foundation
  24. // MARK: -
  25. class CreateMenuAdd: NSObject {
  26. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  27. let fontButton = [NSAttributedString.Key.font:UIFont(name: "HelveticaNeue", size: 16)!, NSAttributedString.Key.foregroundColor: UIColor.black]
  28. let fontEncrypted = [NSAttributedString.Key.font:UIFont(name: "HelveticaNeue", size: 16)!, NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.encrypted as UIColor]
  29. let fontCancel = [NSAttributedString.Key.font:UIFont(name: "HelveticaNeue-Bold", size: 17)!, NSAttributedString.Key.foregroundColor: UIColor.black]
  30. let fontDisable = [NSAttributedString.Key.font:UIFont(name: "HelveticaNeue", size: 16)!, NSAttributedString.Key.foregroundColor: UIColor.darkGray]
  31. var colorIcon = NCBrandColor.sharedInstance.brandElement
  32. @objc init (themingColor : UIColor) {
  33. super.init()
  34. colorIcon = themingColor
  35. }
  36. @objc func createMenu(view : UIView) {
  37. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  38. let actionSheet = AHKActionSheet.init(view: view, title: nil)!
  39. actionSheet.animationDuration = 0.2
  40. actionSheet.automaticallyTintButtonImages = 0
  41. actionSheet.buttonHeight = 50.0
  42. actionSheet.cancelButtonHeight = 50.0
  43. actionSheet.separatorHeight = 5.0
  44. actionSheet.separatorColor = NCBrandColor.sharedInstance.seperator
  45. actionSheet.buttonTextAttributes = fontButton
  46. actionSheet.encryptedButtonTextAttributes = fontEncrypted
  47. actionSheet.cancelButtonTextAttributes = fontCancel
  48. actionSheet.disableButtonTextAttributes = fontDisable
  49. actionSheet.cancelButtonTitle = NSLocalizedString("_cancel_", comment: "")
  50. actionSheet.addButton(withTitle: NSLocalizedString("_upload_photos_videos_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "media"), multiplier:2, color: NCBrandColor.sharedInstance.icon), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  51. appDelegate.activeMain.openAssetsPickerController()
  52. })
  53. actionSheet.addButton(withTitle: NSLocalizedString("_upload_file_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "file"), multiplier:2, color: NCBrandColor.sharedInstance.icon), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  54. appDelegate.activeMain.openImportDocumentPicker()
  55. })
  56. actionSheet.addButton(withTitle: NSLocalizedString("_upload_file_text_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "file_txt"), multiplier:2, color: NCBrandColor.sharedInstance.icon), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  57. let storyboard = UIStoryboard(name: "NCText", bundle: nil)
  58. let controller = storyboard.instantiateViewController(withIdentifier: "NCText")
  59. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  60. appDelegate.activeMain.present(controller, animated: true, completion: nil)
  61. })
  62. if #available(iOS 11.0, *) {
  63. actionSheet.addButton(withTitle: NSLocalizedString("_scans_document_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "scan"), multiplier:2, color: NCBrandColor.sharedInstance.icon), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  64. NCCreateScanDocument.sharedInstance.openScannerDocument(viewController: appDelegate.activeMain, openScan: true)
  65. })
  66. }
  67. 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
  68. appDelegate.activeMain.createFolder()
  69. })
  70. actionSheet.show()
  71. }
  72. }
  73. // MARK: -
  74. @objc protocol createFormUploadAssetsDelegate {
  75. func dismissFormUploadAssets()
  76. }
  77. // MARK: -
  78. class CreateFormUploadAssets: XLFormViewController, CCMoveDelegate {
  79. var serverUrl : String = ""
  80. var titleServerUrl : String?
  81. var assets: NSMutableArray = []
  82. var cryptated : Bool = false
  83. var session : String = ""
  84. weak var delegate: createFormUploadAssetsDelegate?
  85. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  86. @objc convenience init(serverUrl : String, assets : NSMutableArray, cryptated : Bool, session : String, delegate: createFormUploadAssetsDelegate) {
  87. self.init()
  88. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  89. titleServerUrl = "/"
  90. } else {
  91. titleServerUrl = (serverUrl as NSString).lastPathComponent
  92. }
  93. self.serverUrl = serverUrl
  94. self.assets = assets
  95. self.cryptated = cryptated
  96. self.session = session
  97. self.delegate = delegate
  98. self.initializeForm()
  99. }
  100. //MARK: XLFormDescriptorDelegate
  101. func initializeForm() {
  102. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_upload_photos_videos_", comment: "")) as XLFormDescriptor
  103. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  104. var section : XLFormSectionDescriptor
  105. var row : XLFormRowDescriptor
  106. // Section: Destination Folder
  107. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  108. form.addFormSection(section)
  109. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  110. row.action.formSelector = #selector(changeDestinationFolder(_:))
  111. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  112. row.cellConfig["imageView.image"] = imageFolder
  113. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  114. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  115. section.addFormRow(row)
  116. // User folder Autoupload
  117. row = XLFormRowDescriptor(tag: "useFolderAutoUpload", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_use_folder_auto_upload_", comment: ""))
  118. row.value = 0
  119. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  120. section.addFormRow(row)
  121. // Use Sub folder
  122. row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  123. let tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
  124. if tableAccount?.autoUploadCreateSubfolder == true {
  125. row.value = 1
  126. } else {
  127. row.value = 0
  128. }
  129. row.hidden = "$\("useFolderAutoUpload") == 0"
  130. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  131. section.addFormRow(row)
  132. // Section Mode filename
  133. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  134. form.addFormSection(section)
  135. // Maintain the original fileName
  136. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  137. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginal)
  138. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  139. section.addFormRow(row)
  140. // Add File Name Type
  141. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  142. row.value = CCUtility.getFileNameType(k_keyFileNameType)
  143. row.hidden = "$\("maintainOriginalFileName") == 1"
  144. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  145. section.addFormRow(row)
  146. // Section: Rename File Name
  147. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  148. form.addFormSection(section)
  149. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: "")))
  150. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
  151. if fileNameMask.count > 0 {
  152. row.value = fileNameMask
  153. }
  154. row.hidden = "$\("maintainOriginalFileName") == 1"
  155. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  156. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  157. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  158. section.addFormRow(row)
  159. // Section: Preview File Name
  160. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  161. row.height = 180
  162. row.disabled = true
  163. row.cellConfig["textView.backgroundColor"] = NCBrandColor.sharedInstance.backgroundView
  164. row.cellConfig["textView.font"] = UIFont.systemFont(ofSize: 14.0)
  165. section.addFormRow(row)
  166. self.form = form
  167. }
  168. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  169. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  170. if formRow.tag == "useFolderAutoUpload" {
  171. if (formRow.value! as AnyObject).boolValue == true {
  172. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  173. buttonDestinationFolder.hidden = true
  174. } else{
  175. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  176. buttonDestinationFolder.hidden = false
  177. }
  178. }
  179. else if formRow.tag == "useSubFolder" {
  180. if (formRow.value! as AnyObject).boolValue == true {
  181. } else{
  182. }
  183. }
  184. else if formRow.tag == "maintainOriginalFileName" {
  185. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: k_keyFileNameOriginal)
  186. self.reloadForm()
  187. }
  188. else if formRow.tag == "addFileNameType" {
  189. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
  190. self.reloadForm()
  191. }
  192. else if formRow.tag == "maskFileName" {
  193. let fileName = formRow.value as? String
  194. self.form.delegate = nil
  195. if let fileName = fileName {
  196. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  197. }
  198. self.form.delegate = self
  199. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  200. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  201. // reload cell
  202. if fileName != nil {
  203. if newValue as! String != formRow.value as! String {
  204. self.reloadFormRow(formRow)
  205. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  206. }
  207. }
  208. self.reloadFormRow(previewFileName)
  209. }
  210. }
  211. // MARK: - View Life Cycle
  212. override func viewDidLoad() {
  213. super.viewDidLoad()
  214. let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(cancel))
  215. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  216. self.navigationItem.leftBarButtonItem = cancelButton
  217. self.navigationItem.rightBarButtonItem = saveButton
  218. self.navigationController?.navigationBar.isTranslucent = false
  219. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  220. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  221. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  222. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  223. self.reloadForm()
  224. }
  225. override func viewWillDisappear(_ animated: Bool)
  226. {
  227. super.viewWillDisappear(animated)
  228. self.delegate?.dismissFormUploadAssets()
  229. }
  230. func reloadForm() {
  231. self.form.delegate = nil
  232. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  233. buttonDestinationFolder.title = self.titleServerUrl
  234. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  235. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  236. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  237. self.tableView.reloadData()
  238. self.form.delegate = self
  239. }
  240. // MARK: - Action
  241. func moveServerUrl(to serverUrlTo: String!, title: String!, type: String!) {
  242. self.serverUrl = serverUrlTo
  243. if let title = title {
  244. self.titleServerUrl = title
  245. } else {
  246. self.titleServerUrl = "/"
  247. }
  248. self.reloadForm()
  249. }
  250. @objc func save() {
  251. self.dismiss(animated: true, completion: {
  252. let useFolderPhotoRow : XLFormRowDescriptor = self.form.formRow(withTag: "useFolderAutoUpload")!
  253. let useSubFolderRow : XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
  254. var useSubFolder : Bool = false
  255. if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
  256. self.serverUrl = NCManageDatabase.sharedInstance.getAccountAutoUploadPath(self.appDelegate.activeUrl)
  257. useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
  258. }
  259. self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
  260. })
  261. }
  262. @objc func cancel() {
  263. self.dismiss(animated: true, completion: nil)
  264. }
  265. // MARK: - Utility
  266. func previewFileName(valueRename : String?) -> String {
  267. var returnString: String = ""
  268. let asset = assets[0] as! PHAsset
  269. if (CCUtility.getOriginalFileName(k_keyFileNameOriginal)) {
  270. return (NSLocalizedString("_filename_", comment: "") + ": " + (asset.value(forKey: "filename") as! String))
  271. } else if let valueRename = valueRename {
  272. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  273. if valueRenameTrimming.count > 0 {
  274. self.form.delegate = nil
  275. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameMask)
  276. self.form.delegate = self
  277. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  278. } else {
  279. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  280. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  281. }
  282. } else {
  283. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  284. returnString = CCUtility.createFileName(asset.value(forKey: "filename") as! String?, fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  285. }
  286. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  287. }
  288. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  289. self.deselectFormRow(sender)
  290. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  291. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  292. let viewController : CCMove = navigationController.topViewController as! CCMove
  293. viewController.delegate = self;
  294. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  295. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  296. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  297. viewController.move.title = NSLocalizedString("_select_", comment: "");
  298. viewController.networkingOperationQueue = appDelegate.netQueue
  299. // E2EE
  300. viewController.includeDirectoryE2EEncryption = true;
  301. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  302. self.present(navigationController, animated: true, completion: nil)
  303. }
  304. }
  305. // MARK: -
  306. class CreateFormUploadFileText: XLFormViewController, CCMoveDelegate {
  307. var serverUrl = ""
  308. var titleServerUrl = ""
  309. var fileName = ""
  310. var text = ""
  311. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  312. convenience init(serverUrl: String, text: String, fileName: String) {
  313. self.init()
  314. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  315. titleServerUrl = "/"
  316. } else {
  317. titleServerUrl = (serverUrl as NSString).lastPathComponent
  318. }
  319. self.fileName = fileName
  320. self.serverUrl = serverUrl
  321. self.text = text
  322. initializeForm()
  323. }
  324. //MARK: XLFormDescriptorDelegate
  325. func initializeForm() {
  326. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  327. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  328. var section : XLFormSectionDescriptor
  329. var row : XLFormRowDescriptor
  330. // Section: Destination Folder
  331. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  332. form.addFormSection(section)
  333. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  334. row.action.formSelector = #selector(changeDestinationFolder(_:))
  335. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  336. row.cellConfig["imageView.image"] = imageFolder
  337. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  338. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  339. section.addFormRow(row)
  340. // Section: File Name
  341. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  342. form.addFormSection(section)
  343. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  344. row.value = self.fileName
  345. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  346. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  347. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  348. section.addFormRow(row)
  349. self.form = form
  350. }
  351. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  352. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  353. if formRow.tag == "fileName" {
  354. self.form.delegate = nil
  355. if let fileNameNew = formRow.value {
  356. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as? String)
  357. }
  358. formRow.value = self.fileName
  359. self.title = fileName
  360. self.updateFormRow(formRow)
  361. self.form.delegate = self
  362. }
  363. }
  364. // MARK: - View Life Cycle
  365. override func viewDidLoad() {
  366. super.viewDidLoad()
  367. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  368. self.navigationItem.rightBarButtonItem = saveButton
  369. self.navigationController?.navigationBar.isTranslucent = false
  370. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  371. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  372. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  373. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  374. }
  375. // MARK: - Action
  376. func moveServerUrl(to serverUrlTo: String!, title: String!, type: String!) {
  377. self.serverUrl = serverUrlTo
  378. if let title = title {
  379. self.titleServerUrl = title
  380. } else {
  381. self.titleServerUrl = "/"
  382. }
  383. // Update
  384. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  385. row.title = self.titleServerUrl
  386. self.updateFormRow(row)
  387. }
  388. @objc func save() {
  389. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  390. guard let name = rowFileName.value else {
  391. return
  392. }
  393. let ext = (name as! NSString).pathExtension.uppercased()
  394. var fileNameSave = ""
  395. if (ext == "") {
  396. fileNameSave = name as! String + ".txt"
  397. } else if (CCUtility.isDocumentModifiableExtension(ext)) {
  398. fileNameSave = name as! String
  399. } else {
  400. fileNameSave = (name as! NSString).deletingPathExtension + ".txt"
  401. }
  402. guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl) else {
  403. return
  404. }
  405. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave))
  406. if (metadata != nil) {
  407. let alertController = UIAlertController(title: fileNameSave, message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: .alert)
  408. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in
  409. }
  410. let overwriteAction = UIAlertAction(title: NSLocalizedString("_overwrite_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  411. self.dismissAndUpload(fileNameSave, fileID: metadata!.fileID, directoryID: directoryID)
  412. }
  413. alertController.addAction(cancelAction)
  414. alertController.addAction(overwriteAction)
  415. self.present(alertController, animated: true, completion:nil)
  416. } else {
  417. let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl)!
  418. dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
  419. }
  420. }
  421. func dismissAndUpload(_ fileNameSave: String, fileID: String, directoryID: String) {
  422. self.dismiss(animated: true, completion: {
  423. let data = self.text.data(using: .utf8)
  424. let success = FileManager.default.createFile(atPath: CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave), contents: data, attributes: nil)
  425. if success {
  426. let metadataForUpload = tableMetadata()
  427. metadataForUpload.account = self.appDelegate.activeAccount
  428. metadataForUpload.date = NSDate()
  429. metadataForUpload.directoryID = directoryID
  430. metadataForUpload.fileID = fileID
  431. metadataForUpload.fileName = fileNameSave
  432. metadataForUpload.fileNameView = fileNameSave
  433. metadataForUpload.session = k_upload_session
  434. metadataForUpload.sessionSelector = selectorUploadFile
  435. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  436. _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  437. self.appDelegate.perform(#selector(self.appDelegate.loadAutoDownloadUpload), on: Thread.main, with: nil, waitUntilDone: true)
  438. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL))
  439. } else {
  440. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  441. }
  442. })
  443. }
  444. func cancel() {
  445. self.dismiss(animated: true, completion: nil)
  446. }
  447. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  448. self.deselectFormRow(sender)
  449. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  450. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  451. let viewController : CCMove = navigationController.topViewController as! CCMove
  452. viewController.delegate = self;
  453. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  454. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  455. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  456. viewController.move.title = NSLocalizedString("_select_", comment: "");
  457. viewController.networkingOperationQueue = appDelegate.netQueue
  458. // E2EE
  459. viewController.includeDirectoryE2EEncryption = true;
  460. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  461. self.present(navigationController, animated: true, completion: nil)
  462. }
  463. }
  464. //MARK: -
  465. class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
  466. enum typeDpiQuality {
  467. case low
  468. case medium
  469. case hight
  470. }
  471. var dpiQuality: typeDpiQuality = typeDpiQuality.medium
  472. var serverUrl = ""
  473. var titleServerUrl = ""
  474. var arrayImages = [UIImage]()
  475. var fileName = CCUtility.createFileNameDate("scan", extension: "pdf")
  476. var password : PDFPassword = ""
  477. var fileType = "PDF"
  478. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  479. convenience init(serverUrl: String, arrayImages: [UIImage]) {
  480. self.init()
  481. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  482. titleServerUrl = "/"
  483. } else {
  484. titleServerUrl = (serverUrl as NSString).lastPathComponent
  485. }
  486. self.serverUrl = serverUrl
  487. self.arrayImages = arrayImages
  488. initializeForm()
  489. }
  490. //MARK: XLFormDescriptorDelegate
  491. func initializeForm() {
  492. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_save_settings_", comment: "")) as XLFormDescriptor
  493. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  494. var section : XLFormSectionDescriptor
  495. var row : XLFormRowDescriptor
  496. // Section: Destination Folder
  497. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  498. form.addFormSection(section)
  499. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  500. row.action.formSelector = #selector(changeDestinationFolder(_:))
  501. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  502. row.cellConfig["imageView.image"] = imageFolder
  503. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.right.rawValue
  504. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  505. section.addFormRow(row)
  506. // Section: Quality
  507. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_quality_image_title_", comment: ""))
  508. form.addFormSection(section)
  509. row = XLFormRowDescriptor(tag: "compressionQuality", rowType: XLFormRowDescriptorTypeSlider)
  510. row.value = 0.5
  511. row.title = NSLocalizedString("_quality_medium_", comment: "")
  512. row.cellConfig["slider.minimumTrackTintColor"] = NCBrandColor.sharedInstance.brand
  513. row.cellConfig["slider.maximumValue"] = 1
  514. row.cellConfig["slider.minimumValue"] = 0
  515. row.cellConfig["steps"] = 2
  516. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.center.rawValue
  517. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  518. section.addFormRow(row)
  519. // Section: Password
  520. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_pdf_password_", comment: ""))
  521. form.addFormSection(section)
  522. row = XLFormRowDescriptor(tag: "password", rowType: XLFormRowDescriptorTypePassword, title: NSLocalizedString("_password_", comment: ""))
  523. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  524. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  525. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  526. section.addFormRow(row)
  527. // Section: File
  528. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_file_creation_", comment: ""))
  529. form.addFormSection(section)
  530. if arrayImages.count == 1 {
  531. row = XLFormRowDescriptor(tag: "filetype", rowType: XLFormRowDescriptorTypeSelectorSegmentedControl, title: NSLocalizedString("_file_type_", comment: ""))
  532. row.selectorOptions = ["PDF","JPG"]
  533. row.value = "PDF"
  534. row.cellConfig["tintColor"] = NCBrandColor.sharedInstance.brand
  535. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  536. section.addFormRow(row)
  537. }
  538. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  539. row.value = self.fileName
  540. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  541. row.cellConfig["textField.textAlignment"] = NSTextAlignment.right.rawValue
  542. row.cellConfig["textField.font"] = UIFont.systemFont(ofSize: 15.0)
  543. section.addFormRow(row)
  544. self.form = form
  545. }
  546. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  547. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  548. if formRow.tag == "fileName" {
  549. self.form.delegate = nil
  550. let fileNameNew = newValue as? String
  551. if fileNameNew != nil {
  552. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew)
  553. } else {
  554. self.fileName = ""
  555. }
  556. formRow.value = self.fileName
  557. self.updateFormRow(formRow)
  558. self.form.delegate = self
  559. }
  560. if formRow.tag == "compressionQuality" {
  561. self.form.delegate = nil
  562. //let row : XLFormRowDescriptor = self.form.formRow(withTag: "descriptionQuality")!
  563. let newQuality = newValue as? NSNumber
  564. let compressionQuality = (newQuality?.doubleValue)!
  565. if compressionQuality >= 0.0 && compressionQuality <= 0.3 {
  566. formRow.title = NSLocalizedString("_quality_low_", comment: "")
  567. dpiQuality = typeDpiQuality.low
  568. } else if compressionQuality > 0.3 && compressionQuality <= 0.6 {
  569. formRow.title = NSLocalizedString("_quality_medium_", comment: "")
  570. dpiQuality = typeDpiQuality.medium
  571. } else if compressionQuality > 0.6 && compressionQuality <= 1.0 {
  572. formRow.title = NSLocalizedString("_quality_high_", comment: "")
  573. dpiQuality = typeDpiQuality.hight
  574. }
  575. self.updateFormRow(formRow)
  576. self.form.delegate = self
  577. }
  578. if formRow.tag == "password" {
  579. let stringPassword = newValue as? String
  580. if stringPassword != nil {
  581. password = PDFPassword(stringPassword!)
  582. } else {
  583. password = PDFPassword("")
  584. }
  585. }
  586. if formRow.tag == "filetype" {
  587. fileType = newValue as! String
  588. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  589. let rowPassword : XLFormRowDescriptor = self.form.formRow(withTag: "password")!
  590. // rowFileName
  591. guard var name = rowFileName.value else {
  592. return
  593. }
  594. if name as! String == "" {
  595. name = CCUtility.createFileNameDate("scan", extension: "pdf")
  596. }
  597. let ext = (name as! NSString).pathExtension.uppercased()
  598. var newFileName = ""
  599. if (ext == "") {
  600. newFileName = name as! String + "." + fileType.lowercased()
  601. } else {
  602. newFileName = (name as! NSString).deletingPathExtension + "." + fileType.lowercased()
  603. }
  604. rowFileName.value = newFileName
  605. self.updateFormRow(rowFileName)
  606. // rowPassword
  607. if fileType == "JPG" {
  608. rowPassword.value = ""
  609. password = PDFPassword("")
  610. rowPassword.disabled = true
  611. } else {
  612. rowPassword.disabled = false
  613. }
  614. self.updateFormRow(rowPassword)
  615. }
  616. }
  617. // MARK: - View Life Cycle
  618. override func viewDidLoad() {
  619. super.viewDidLoad()
  620. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItem.Style.plain, target: self, action: #selector(save))
  621. self.navigationItem.rightBarButtonItem = saveButton
  622. self.navigationController?.navigationBar.isTranslucent = false
  623. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  624. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  625. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedString.Key.foregroundColor: NCBrandColor.sharedInstance.brandText]
  626. self.tableView.separatorStyle = UITableViewCell.SeparatorStyle.none
  627. // self.tableView.sectionHeaderHeight = 10
  628. // self.tableView.sectionFooterHeight = 10
  629. // self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  630. // let row : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  631. // let rowCell = row.cell(forForm: self)
  632. // rowCell.becomeFirstResponder()
  633. }
  634. // MARK: - Action
  635. func moveServerUrl(to serverUrlTo: String!, title: String!, type: String!) {
  636. self.serverUrl = serverUrlTo
  637. if let title = title {
  638. self.titleServerUrl = title
  639. } else {
  640. self.titleServerUrl = "/"
  641. }
  642. // Update
  643. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  644. row.title = self.titleServerUrl
  645. self.updateFormRow(row)
  646. }
  647. @objc func save() {
  648. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  649. guard let name = rowFileName.value else {
  650. return
  651. }
  652. if name as! String == "" {
  653. return
  654. }
  655. let ext = (name as! NSString).pathExtension.uppercased()
  656. var fileNameSave = ""
  657. if (ext == "") {
  658. fileNameSave = name as! String + "." + fileType.lowercased()
  659. } else {
  660. fileNameSave = (name as! NSString).deletingPathExtension + "." + fileType.lowercased()
  661. }
  662. guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl) else {
  663. return
  664. }
  665. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave))
  666. if (metadata != nil) {
  667. let alertController = UIAlertController(title: fileNameSave, message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: .alert)
  668. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in
  669. }
  670. let overwriteAction = UIAlertAction(title: NSLocalizedString("_overwrite_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  671. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave), clearDateReadDirectoryID: directoryID)
  672. self.dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
  673. }
  674. alertController.addAction(cancelAction)
  675. alertController.addAction(overwriteAction)
  676. self.present(alertController, animated: true, completion:nil)
  677. } else {
  678. let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl)!
  679. dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
  680. }
  681. }
  682. func dismissAndUpload(_ fileNameSave: String, fileID: String, directoryID: String) {
  683. guard let fileNameGenerateExport = CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave) else {
  684. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  685. return
  686. }
  687. if fileType == "PDF" {
  688. var pdfPages = [PDFPage]()
  689. //Generate PDF
  690. for var image in self.arrayImages {
  691. image = changeImageFromQuality(image, dpiQuality: dpiQuality)
  692. guard let data = image.jpegData(compressionQuality: 0.5) else {
  693. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  694. return
  695. }
  696. let page = PDFPage.image(UIImage(data: data)!)
  697. pdfPages.append(page)
  698. }
  699. do {
  700. try PDFGenerator.generate(pdfPages, to: fileNameGenerateExport, password: password)
  701. } catch {
  702. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  703. return
  704. }
  705. }
  706. if fileType == "JPG" {
  707. let image = changeImageFromQuality(self.arrayImages[0], dpiQuality: dpiQuality)
  708. guard let data = image.jpegData(compressionQuality: CGFloat(0.5)) else {
  709. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  710. return
  711. }
  712. do {
  713. try data.write(to: NSURL.fileURL(withPath: fileNameGenerateExport), options: .atomic)
  714. } catch {
  715. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  716. return
  717. }
  718. }
  719. //Create metadata for upload
  720. let metadataForUpload = tableMetadata()
  721. metadataForUpload.account = self.appDelegate.activeAccount
  722. metadataForUpload.date = NSDate()
  723. metadataForUpload.directoryID = directoryID
  724. metadataForUpload.fileID = fileID
  725. metadataForUpload.fileName = fileNameSave
  726. metadataForUpload.fileNameView = fileNameSave
  727. metadataForUpload.session = k_upload_session
  728. metadataForUpload.sessionSelector = selectorUploadFile
  729. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  730. _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  731. self.appDelegate.perform(#selector(self.appDelegate.loadAutoDownloadUpload), on: Thread.main, with: nil, waitUntilDone: true)
  732. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL))
  733. // Request delete all image scanned
  734. let alertController = UIAlertController(title: "", message: NSLocalizedString("_delete_all_scanned_images_", comment: ""), preferredStyle: .alert)
  735. let actionYes = UIAlertAction(title: NSLocalizedString("_yes_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  736. let path = CCUtility.getDirectoryScan()!
  737. do {
  738. let filePaths = try FileManager.default.contentsOfDirectory(atPath: path)
  739. for filePath in filePaths {
  740. try FileManager.default.removeItem(atPath: path + filePath)
  741. }
  742. } catch let error as NSError {
  743. print("Could not clear temp folder: \(error.debugDescription)")
  744. }
  745. self.dismiss(animated: true, completion: nil)
  746. }
  747. let actionNo = UIAlertAction(title: NSLocalizedString("_no_delete_", comment: ""), style: .default) { (action:UIAlertAction) in
  748. self.dismiss(animated: true, completion: nil)
  749. }
  750. alertController.addAction(actionYes)
  751. alertController.addAction(actionNo)
  752. self.present(alertController, animated: true, completion:nil)
  753. }
  754. func cancel() {
  755. self.dismiss(animated: true, completion: nil)
  756. }
  757. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  758. self.deselectFormRow(sender)
  759. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  760. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  761. let viewController : CCMove = navigationController.topViewController as! CCMove
  762. viewController.delegate = self;
  763. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  764. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  765. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  766. viewController.move.title = NSLocalizedString("_select_", comment: "");
  767. viewController.networkingOperationQueue = appDelegate.netQueue
  768. // E2EE
  769. viewController.includeDirectoryE2EEncryption = true;
  770. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  771. self.present(navigationController, animated: true, completion: nil)
  772. }
  773. func changeImageFromQuality(_ image: UIImage, dpiQuality: typeDpiQuality) -> UIImage {
  774. let imageWidthInPixels = image.size.width * image.scale
  775. let imageHeightInPixels = image.size.height * image.scale
  776. switch dpiQuality {
  777. case typeDpiQuality.low: // 72 DPI
  778. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  779. return CCGraphics.scale(image, to: CGSize(width: 595, height: 842), isAspectRation: true)
  780. }
  781. case typeDpiQuality.medium: // 150 DPI
  782. if imageWidthInPixels > 1240 || imageHeightInPixels > 1754 {
  783. return CCGraphics.scale(image, to: CGSize(width: 1240, height: 1754), isAspectRation: true)
  784. }
  785. case typeDpiQuality.hight: // 200 DPI
  786. if imageWidthInPixels > 1654 || imageHeightInPixels > 2339 {
  787. return CCGraphics.scale(image, to: CGSize(width: 1654, height: 2339), isAspectRation: true)
  788. }
  789. }
  790. return image
  791. }
  792. }
  793. class NCCreateScanDocument : NSObject, ImageScannerControllerDelegate {
  794. @objc static let sharedInstance: NCCreateScanDocument = {
  795. let instance = NCCreateScanDocument()
  796. return instance
  797. }()
  798. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  799. var viewController: UIViewController?
  800. var openScan: Bool = false
  801. @available(iOS 10, *)
  802. func openScannerDocument(viewController: UIViewController, openScan: Bool) {
  803. self.viewController = viewController
  804. self.openScan = openScan
  805. let scannerVC = ImageScannerController()
  806. scannerVC.imageScannerDelegate = self
  807. self.viewController?.present(scannerVC, animated: true, completion: nil)
  808. }
  809. @available(iOS 10, *)
  810. func imageScannerController(_ scanner: ImageScannerController, didFinishScanningWithResults results: ImageScannerResults) {
  811. let fileName = CCUtility.createFileName("scan.png", fileDate: Date(), fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)!
  812. let fileNamePath = CCUtility.getDirectoryScan() + "/" + fileName
  813. do {
  814. try results.scannedImage.pngData()?.write(to: NSURL.fileURL(withPath: fileNamePath), options: .atomic)
  815. } catch { }
  816. scanner.dismiss(animated: true, completion: {
  817. if (self.openScan) {
  818. let storyboard = UIStoryboard(name: "Scan", bundle: nil)
  819. let controller = storyboard.instantiateInitialViewController()!
  820. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  821. self.viewController?.present(controller, animated: true, completion: nil)
  822. }
  823. })
  824. }
  825. @available(iOS 10, *)
  826. func imageScannerControllerDidCancel(_ scanner: ImageScannerController) {
  827. scanner.dismiss(animated: true, completion: nil)
  828. }
  829. @available(iOS 10, *)
  830. func imageScannerController(_ scanner: ImageScannerController, didFailWithError error: Error) {
  831. appDelegate.messageNotification("_error_", description: error.localizedDescription, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  832. }
  833. }