CCCreateCloud.swift 50 KB

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