CCCreateCloud.swift 49 KB

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