CCCreateCloud.swift 45 KB

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