CCCreateCloud.swift 49 KB

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