CCCreateCloud.swift 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045
  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. #if DEBUG
  56. if #available(iOS 11.0, *) {
  57. 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
  58. NCCreateScanDocument.sharedInstance.openScannerDocument(viewController: appDelegate.activeMain, openScan: true)
  59. })
  60. }
  61. #endif
  62. 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
  63. appDelegate.activeMain.openImportDocumentPicker()
  64. })
  65. 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
  66. let storyboard = UIStoryboard(name: "NCText", bundle: nil)
  67. let controller = storyboard.instantiateViewController(withIdentifier: "NCText")
  68. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  69. appDelegate.activeMain.present(controller, animated: true, completion: nil)
  70. })
  71. 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
  72. appDelegate.activeMain.createFolder()
  73. })
  74. actionSheet.show()
  75. }
  76. }
  77. // MARK: -
  78. @objc protocol createFormUploadAssetsDelegate {
  79. func dismissFormUploadAssets()
  80. }
  81. // MARK: -
  82. class CreateFormUploadAssets: XLFormViewController, CCMoveDelegate {
  83. var serverUrl : String = ""
  84. var titleServerUrl : String?
  85. var assets: NSMutableArray = []
  86. var cryptated : Bool = false
  87. var session : String = ""
  88. weak var delegate: createFormUploadAssetsDelegate?
  89. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  90. @objc convenience init(serverUrl : String, assets : NSMutableArray, cryptated : Bool, session : String, delegate: createFormUploadAssetsDelegate) {
  91. self.init()
  92. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  93. titleServerUrl = "/"
  94. } else {
  95. titleServerUrl = (serverUrl as NSString).lastPathComponent
  96. }
  97. self.serverUrl = serverUrl
  98. self.assets = assets
  99. self.cryptated = cryptated
  100. self.session = session
  101. self.delegate = delegate
  102. self.initializeForm()
  103. }
  104. //MARK: XLFormDescriptorDelegate
  105. func initializeForm() {
  106. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  107. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  108. var section : XLFormSectionDescriptor
  109. var row : XLFormRowDescriptor
  110. // Section: Destination Folder
  111. section = XLFormSectionDescriptor.formSection()
  112. form.addFormSection(section)
  113. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  114. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  115. row.cellConfig.setObject(imageFolder, forKey: "imageView.image" as NSCopying)
  116. row.cellConfig.setObject(UIColor.black, forKey: "textLabel.textColor" as NSCopying)
  117. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  118. row.action.formSelector = #selector(changeDestinationFolder(_:))
  119. section.addFormRow(row)
  120. // Section Switch
  121. section = XLFormSectionDescriptor.formSection()
  122. form.addFormSection(section)
  123. // Folder Photo
  124. row = XLFormRowDescriptor(tag: "useFolderMedia", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_use_folder_media_", comment: ""))
  125. row.value = 0
  126. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  127. section.addFormRow(row)
  128. // Use Sub folder
  129. row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  130. row.hidden = "$\("useFolderMedia") == 0"
  131. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  132. let tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
  133. if tableAccount?.autoUploadCreateSubfolder == true {
  134. row.value = 1
  135. } else {
  136. row.value = 0
  137. }
  138. section.addFormRow(row)
  139. // Maintain the original fileName
  140. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  141. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginal)
  142. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  143. section.addFormRow(row)
  144. // Add File Name Type
  145. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  146. row.hidden = "$\("maintainOriginalFileName") == 1"
  147. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  148. row.value = CCUtility.getFileNameType(k_keyFileNameType)
  149. section.addFormRow(row)
  150. // Section: Rename File Name
  151. section = XLFormSectionDescriptor.formSection()
  152. form.addFormSection(section)
  153. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: ""))+":")
  154. row.hidden = "$\("maintainOriginalFileName") == 1"
  155. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  156. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
  157. if fileNameMask.count > 0 {
  158. row.value = fileNameMask
  159. }
  160. section.addFormRow(row)
  161. // Section: Preview File Name
  162. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  163. row.height = 180
  164. row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "backgroundColor" as NSCopying)
  165. row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "textView.backgroundColor" as NSCopying)
  166. row.disabled = true
  167. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  168. section.addFormRow(row)
  169. self.form = form
  170. }
  171. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  172. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  173. if formRow.tag == "useFolderMedia" {
  174. if (formRow.value! as AnyObject).boolValue == true {
  175. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  176. buttonDestinationFolder.hidden = true
  177. } else{
  178. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  179. buttonDestinationFolder.hidden = false
  180. }
  181. }
  182. else if formRow.tag == "useSubFolder" {
  183. if (formRow.value! as AnyObject).boolValue == true {
  184. } else{
  185. }
  186. }
  187. else if formRow.tag == "maintainOriginalFileName" {
  188. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: k_keyFileNameOriginal)
  189. self.reloadForm()
  190. }
  191. else if formRow.tag == "addFileNameType" {
  192. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
  193. self.reloadForm()
  194. }
  195. else if formRow.tag == "maskFileName" {
  196. let fileName = formRow.value as? String
  197. self.form.delegate = nil
  198. if let fileName = fileName {
  199. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  200. }
  201. self.form.delegate = self
  202. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  203. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  204. // reload cell
  205. if fileName != nil {
  206. if newValue as! String != formRow.value as! String {
  207. self.reloadFormRow(formRow)
  208. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  209. }
  210. }
  211. self.reloadFormRow(previewFileName)
  212. }
  213. }
  214. // MARK: - View Life Cycle
  215. override func viewDidLoad() {
  216. super.viewDidLoad()
  217. let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancel))
  218. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
  219. self.navigationItem.leftBarButtonItem = cancelButton
  220. self.navigationItem.rightBarButtonItem = saveButton
  221. self.navigationController?.navigationBar.isTranslucent = false
  222. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  223. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  224. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
  225. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  226. self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  227. self.reloadForm()
  228. }
  229. override func viewWillDisappear(_ animated: Bool)
  230. {
  231. super.viewWillDisappear(animated)
  232. self.delegate?.dismissFormUploadAssets()
  233. }
  234. func reloadForm() {
  235. self.form.delegate = nil
  236. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  237. buttonDestinationFolder.title = self.titleServerUrl
  238. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  239. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  240. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  241. self.tableView.reloadData()
  242. self.form.delegate = self
  243. }
  244. // MARK: - Action
  245. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  246. self.serverUrl = serverUrlTo
  247. if let title = title {
  248. self.titleServerUrl = title
  249. } else {
  250. self.titleServerUrl = "/"
  251. }
  252. self.reloadForm()
  253. }
  254. @objc func save() {
  255. self.dismiss(animated: true, completion: {
  256. let useFolderPhotoRow : XLFormRowDescriptor = self.form.formRow(withTag: "useFolderMedia")!
  257. let useSubFolderRow : XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
  258. var useSubFolder : Bool = false
  259. if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
  260. self.serverUrl = NCManageDatabase.sharedInstance.getAccountAutoUploadPath(self.appDelegate.activeUrl)
  261. useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
  262. }
  263. self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
  264. })
  265. }
  266. @objc func cancel() {
  267. self.dismiss(animated: true, completion: nil)
  268. }
  269. // MARK: - Utility
  270. func previewFileName(valueRename : String?) -> String {
  271. var returnString: String = ""
  272. let asset = assets[0] as! PHAsset
  273. if (CCUtility.getOriginalFileName(k_keyFileNameOriginal)) {
  274. return (NSLocalizedString("_filename_", comment: "") + ": " + (asset.value(forKey: "filename") as! String))
  275. } else if let valueRename = valueRename {
  276. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  277. if valueRenameTrimming.count > 0 {
  278. self.form.delegate = nil
  279. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameMask)
  280. self.form.delegate = self
  281. returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  282. } else {
  283. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  284. returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  285. }
  286. } else {
  287. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  288. returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  289. }
  290. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  291. }
  292. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  293. self.deselectFormRow(sender)
  294. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  295. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  296. let viewController : CCMove = navigationController.topViewController as! CCMove
  297. viewController.delegate = self;
  298. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  299. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  300. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  301. viewController.move.title = NSLocalizedString("_select_", comment: "");
  302. viewController.networkingOperationQueue = appDelegate.netQueue
  303. // E2EE
  304. viewController.includeDirectoryE2EEncryption = true;
  305. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  306. self.present(navigationController, animated: true, completion: nil)
  307. }
  308. }
  309. // MARK: -
  310. class CreateFormUploadFileText: XLFormViewController, CCMoveDelegate {
  311. var serverUrl = ""
  312. var titleServerUrl = ""
  313. var fileName = ""
  314. var text = ""
  315. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  316. convenience init(serverUrl: String, text: String, fileName: String) {
  317. self.init()
  318. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  319. titleServerUrl = "/"
  320. } else {
  321. titleServerUrl = (serverUrl as NSString).lastPathComponent
  322. }
  323. self.fileName = fileName
  324. self.serverUrl = serverUrl
  325. self.text = text
  326. initializeForm()
  327. }
  328. //MARK: XLFormDescriptorDelegate
  329. func initializeForm() {
  330. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  331. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  332. var section : XLFormSectionDescriptor
  333. var row : XLFormRowDescriptor
  334. // Section: Destination Folder
  335. section = XLFormSectionDescriptor.formSection()
  336. form.addFormSection(section)
  337. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  338. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  339. row.cellConfig.setObject(imageFolder, forKey: "imageView.image" as NSCopying)
  340. row.cellConfig.setObject(UIColor.black, forKey: "textLabel.textColor" as NSCopying)
  341. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  342. row.action.formSelector = #selector(changeDestinationFolder(_:))
  343. section.addFormRow(row)
  344. // Section: File Name
  345. section = XLFormSectionDescriptor.formSection()
  346. form.addFormSection(section)
  347. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  348. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  349. row.value = fileName
  350. section.addFormRow(row)
  351. self.form = form
  352. }
  353. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  354. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  355. if formRow.tag == "fileName" {
  356. self.form.delegate = nil
  357. if let fileNameNew = formRow.value {
  358. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as! String)
  359. }
  360. self.title = fileName
  361. self.form.delegate = self
  362. }
  363. }
  364. // MARK: - View Life Cycle
  365. override func viewDidLoad() {
  366. super.viewDidLoad()
  367. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
  368. self.navigationItem.rightBarButtonItem = saveButton
  369. self.navigationController?.navigationBar.isTranslucent = false
  370. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  371. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  372. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
  373. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  374. self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  375. self.reloadForm()
  376. }
  377. func reloadForm() {
  378. self.form.delegate = nil
  379. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  380. buttonDestinationFolder.title = self.titleServerUrl
  381. self.title = fileName
  382. self.tableView.reloadData()
  383. self.form.delegate = self
  384. }
  385. // MARK: - Action
  386. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  387. self.serverUrl = serverUrlTo
  388. if let title = title {
  389. self.titleServerUrl = title
  390. } else {
  391. self.titleServerUrl = "/"
  392. }
  393. self.reloadForm()
  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. }