CCCreateCloud.swift 51 KB

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