CCCreateCloud.swift 51 KB

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