CCCreateCloud.swift 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689
  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: - CreateMenuAdd
  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.brand
  34. @objc init (themingColor : UIColor) {
  35. super.init()
  36. colorIcon = themingColor
  37. }
  38. @objc func createMenuPlain(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: "menuUploadPhoto"), color: colorGray), backgroundColor: UIColor.white, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  53. appDelegate.activeMain.returnCreate(Int(k_returnCreateFotoVideoPlain))
  54. })
  55. actionSheet.addButton(withTitle: NSLocalizedString("_upload_file_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "menuUploadFile"), color: colorGray), backgroundColor: UIColor.white, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  56. appDelegate.activeMain.returnCreate(Int(k_returnCreateFilePlain))
  57. })
  58. actionSheet.addButton(withTitle: NSLocalizedString("_upload_file_text_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "file_txt"), color: colorGray), backgroundColor: UIColor.white, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  59. appDelegate.activeMain.returnCreate(Int(k_returnCreateFileText))
  60. })
  61. actionSheet.addButton(withTitle: NSLocalizedString("_create_folder_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "folder"), color: colorIcon), backgroundColor: UIColor.white, height: 50.0 ,type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  62. appDelegate.activeMain.returnCreate(Int(k_returnCreateFolderPlain))
  63. })
  64. /*
  65. if appDelegate.isCryptoCloudMode {
  66. actionSheet.addButton(withTitle: NSLocalizedString("_upload_encrypted_mode", comment: ""), image: UIImage(named: "actionSheetLock"), backgroundColor: colorLightGray, height: 50.0, type: AHKActionSheetButtonType.encrypted, handler: {(AHKActionSheet) -> Void in
  67. self.createMenuEncrypted(view: view)
  68. })
  69. }
  70. */
  71. actionSheet.show()
  72. CCUtility.setCreateMenuEncrypted(false)
  73. }
  74. @objc func createMenuEncrypted(view : UIView) {
  75. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  76. let actionSheet = AHKActionSheet.init(view: view, title: nil)!
  77. actionSheet.animationDuration = 0.2
  78. actionSheet.buttonHeight = 50.0
  79. actionSheet.cancelButtonHeight = 50.0
  80. actionSheet.separatorHeight = 5.0
  81. actionSheet.separatorColor = NCBrandColor.sharedInstance.seperator
  82. actionSheet.buttonTextAttributes = fontButton
  83. actionSheet.encryptedButtonTextAttributes = fontEncrypted
  84. actionSheet.cancelButtonTextAttributes = fontCancel
  85. actionSheet.disableButtonTextAttributes = fontDisable
  86. actionSheet.cancelButtonTitle = NSLocalizedString("_cancel_", comment: "")
  87. actionSheet.addButton(withTitle: NSLocalizedString("_upload_photos_videos_", comment: ""), image: UIImage(named: "menuUploadPhotoCrypto"), backgroundColor: UIColor.white, height: 50.0, type: AHKActionSheetButtonType.encrypted, handler: {(AHKActionSheet) -> Void in
  88. appDelegate.activeMain.returnCreate(Int(k_returnCreateFotoVideoEncrypted))
  89. })
  90. actionSheet.addButton(withTitle: NSLocalizedString("_upload_file_", comment: ""), image: UIImage(named: "menuUploadFileCrypto"), backgroundColor: UIColor.white, height: 50.0, type: AHKActionSheetButtonType.encrypted, handler: {(AHKActionSheet) -> Void in
  91. appDelegate.activeMain.returnCreate(Int(k_returnCreateFileEncrypted))
  92. })
  93. actionSheet.addButton(withTitle: NSLocalizedString("_create_folder_", comment: ""), image: UIImage(named: "foldercrypto"), backgroundColor: UIColor.white, height: 50.0, type: AHKActionSheetButtonType.encrypted, handler: {(AHKActionSheet) -> Void in
  94. appDelegate.activeMain.returnCreate(Int(k_returnCreateFolderEncrypted))
  95. })
  96. actionSheet.addButton(withTitle: NSLocalizedString("_upload_plain_mode_", comment: ""), image: UIImage(named: "menuUploadPlainMode"), backgroundColor: colorLightGray, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  97. self.createMenuPlain(view: view)
  98. })
  99. actionSheet.show()
  100. CCUtility.setCreateMenuEncrypted(true)
  101. }
  102. }
  103. // MARK: - CreateFormUploadAssets
  104. @objc protocol createFormUploadAssetsDelegate {
  105. func dismissFormUploadAssets()
  106. }
  107. class CreateFormUploadAssets: XLFormViewController, CCMoveDelegate {
  108. var serverUrl : String = ""
  109. var titleServerUrl : String?
  110. var assets: NSMutableArray = []
  111. var cryptated : Bool = false
  112. var session : String = ""
  113. weak var delegate: createFormUploadAssetsDelegate?
  114. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  115. @objc convenience init(serverUrl : String, assets : NSMutableArray, cryptated : Bool, session : String, delegate: createFormUploadAssetsDelegate) {
  116. self.init()
  117. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  118. titleServerUrl = "/"
  119. } else {
  120. titleServerUrl = (serverUrl as NSString).lastPathComponent
  121. }
  122. self.serverUrl = serverUrl
  123. self.assets = assets
  124. self.cryptated = cryptated
  125. self.session = session
  126. self.delegate = delegate
  127. self.initializeForm()
  128. }
  129. //MARK: XLFormDescriptorDelegate
  130. func initializeForm() {
  131. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  132. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  133. var section : XLFormSectionDescriptor
  134. var row : XLFormRowDescriptor
  135. // Section: Destination Folder
  136. section = XLFormSectionDescriptor.formSection()
  137. form.addFormSection(section)
  138. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  139. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, color: NCBrandColor.sharedInstance.brand) as UIImage
  140. row.cellConfig.setObject(imageFolder, forKey: "imageView.image" as NSCopying)
  141. row.cellConfig.setObject(UIColor.black, forKey: "textLabel.textColor" as NSCopying)
  142. row.action.formSelector = #selector(changeDestinationFolder(_:))
  143. section.addFormRow(row)
  144. // Section: Folder Photo
  145. section = XLFormSectionDescriptor.formSection()
  146. form.addFormSection(section)
  147. row = XLFormRowDescriptor(tag: "useFolderPhoto", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_photo_camera_", comment: ""))
  148. row.value = 0
  149. section.addFormRow(row)
  150. row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  151. row.hidden = "$\("useFolderPhoto") == 0"
  152. let tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
  153. if tableAccount?.autoUploadCreateSubfolder == true {
  154. row.value = 1
  155. } else {
  156. row.value = 0
  157. }
  158. section.addFormRow(row)
  159. // Section: Add File Name Type
  160. section = XLFormSectionDescriptor.formSection()
  161. form.addFormSection(section)
  162. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_filenametype_photo_video_", comment: ""))
  163. row.value = CCUtility.getFileNameType(k_keyFileNameType)
  164. section.addFormRow(row)
  165. // Section: Rename File Name
  166. section = XLFormSectionDescriptor.formSection()
  167. form.addFormSection(section)
  168. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  169. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
  170. if fileNameMask.count > 0 {
  171. row.value = fileNameMask
  172. }
  173. section.addFormRow(row)
  174. // Section: Preview File Name
  175. //section = XLFormSectionDescriptor.formSection()
  176. //form.addFormSection(section)
  177. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  178. row.height = 180
  179. row.cellConfig.setObject(NCBrandColor.sharedInstance.tableBackground, forKey: "backgroundColor" as NSCopying)
  180. row.cellConfig.setObject(NCBrandColor.sharedInstance.tableBackground, forKey: "textView.backgroundColor" as NSCopying)
  181. row.disabled = true
  182. section.addFormRow(row)
  183. self.form = form
  184. }
  185. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  186. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  187. if formRow.tag == "useFolderPhoto" {
  188. if (formRow.value! as AnyObject).boolValue == true {
  189. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  190. buttonDestinationFolder.hidden = true
  191. } else{
  192. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  193. buttonDestinationFolder.hidden = false
  194. }
  195. }
  196. else if formRow.tag == "useSubFolder" {
  197. if (formRow.value! as AnyObject).boolValue == true {
  198. } else{
  199. }
  200. }
  201. else if formRow.tag == "addFileNameType" {
  202. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
  203. self.reloadForm()
  204. }
  205. else if formRow.tag == "maskFileName" {
  206. let fileName = formRow.value as? String
  207. self.form.delegate = nil
  208. if let fileName = fileName {
  209. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  210. }
  211. self.form.delegate = self
  212. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  213. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  214. // reload cell
  215. if fileName != nil {
  216. if newValue as! String != formRow.value as! String {
  217. self.reloadFormRow(formRow)
  218. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  219. }
  220. }
  221. self.reloadFormRow(previewFileName)
  222. }
  223. }
  224. // MARK: - View Life Cycle
  225. override func viewDidLoad() {
  226. super.viewDidLoad()
  227. let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancel))
  228. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
  229. self.navigationItem.leftBarButtonItem = cancelButton
  230. self.navigationItem.rightBarButtonItem = saveButton
  231. self.navigationController?.navigationBar.isTranslucent = false
  232. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  233. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.navigationBarText
  234. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.navigationBarText]
  235. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  236. self.tableView.backgroundColor = NCBrandColor.sharedInstance.tableBackground
  237. self.reloadForm()
  238. }
  239. override func viewWillDisappear(_ animated: Bool)
  240. {
  241. super.viewWillDisappear(animated)
  242. self.delegate?.dismissFormUploadAssets()
  243. }
  244. func reloadForm() {
  245. self.form.delegate = nil
  246. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  247. buttonDestinationFolder.title = self.titleServerUrl
  248. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  249. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  250. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  251. self.tableView.reloadData()
  252. self.form.delegate = self
  253. }
  254. //MARK: TableView
  255. override func tableView(_ tableView: UITableView, titleForHeaderInSection section: Int) -> String? {
  256. switch section {
  257. case 0:
  258. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  259. if buttonDestinationFolder.isHidden() {
  260. return ""
  261. } else {
  262. return " " + NSLocalizedString("_destination_folder_", comment: "")
  263. }
  264. case 1:
  265. return " " + NSLocalizedString("_use_folder_photos_", comment: "")
  266. case 2:
  267. return " " + NSLocalizedString("_add_filenametype_", comment: "")
  268. case 3:
  269. return " " + NSLocalizedString("_rename_filename_", comment: "")
  270. case 4:
  271. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm")
  272. default:
  273. return ""
  274. }
  275. }
  276. override func tableView(_ tableView: UITableView, titleForFooterInSection section: Int) -> String? {
  277. switch section {
  278. /*
  279. case 2:
  280. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  281. let text = self.writePreviewFileName(buttonDestinationFolder)
  282. return text
  283. */
  284. default:
  285. return ""
  286. }
  287. }
  288. // MARK: - Action
  289. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  290. self.serverUrl = serverUrlTo
  291. if let title = title {
  292. self.titleServerUrl = title
  293. } else {
  294. self.titleServerUrl = "/"
  295. }
  296. self.reloadForm()
  297. }
  298. @objc func save() {
  299. self.dismiss(animated: true, completion: {
  300. let useFolderPhotoRow : XLFormRowDescriptor = self.form.formRow(withTag: "useFolderPhoto")!
  301. let useSubFolderRow : XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
  302. var useSubFolder : Bool = false
  303. if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
  304. self.serverUrl = NCManageDatabase.sharedInstance.getAccountAutoUploadPath(self.appDelegate.activeUrl)
  305. useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
  306. }
  307. self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
  308. })
  309. }
  310. @objc func cancel() {
  311. self.dismiss(animated: true, completion: nil)
  312. }
  313. // MARK: - Utility
  314. func previewFileName(valueRename : String?) -> String {
  315. var returnString : String = ""
  316. let asset = assets[0] as! PHAsset
  317. if let valueRename = valueRename {
  318. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  319. if valueRenameTrimming.count > 0 {
  320. self.form.delegate = nil
  321. CCUtility.setFileNameMask(valueRenameTrimming, key: k_keyFileNameMask)
  322. self.form.delegate = self
  323. returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType)
  324. } else {
  325. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  326. returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType)
  327. }
  328. } else {
  329. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  330. returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType)
  331. }
  332. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  333. }
  334. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  335. self.deselectFormRow(sender)
  336. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  337. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  338. let viewController : CCMove = navigationController.topViewController as! CCMove
  339. viewController.delegate = self;
  340. viewController.tintColor = NCBrandColor.sharedInstance.navigationBarText
  341. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  342. viewController.tintColorTitle = NCBrandColor.sharedInstance.navigationBarText
  343. viewController.move.title = NSLocalizedString("_select_", comment: "");
  344. viewController.networkingOperationQueue = appDelegate.netQueue
  345. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  346. self.present(navigationController, animated: true, completion: nil)
  347. }
  348. }
  349. class CreateFormUploadFile: XLFormViewController, CCMoveDelegate {
  350. var serverUrl = ""
  351. var titleServerUrl = ""
  352. var fileName = ""
  353. var text = ""
  354. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  355. convenience init(serverUrl: String, text: String, fileName: String) {
  356. self.init()
  357. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  358. titleServerUrl = "/"
  359. } else {
  360. titleServerUrl = (serverUrl as NSString).lastPathComponent
  361. }
  362. self.fileName = fileName
  363. self.serverUrl = serverUrl
  364. self.text = text
  365. initializeForm()
  366. }
  367. //MARK: XLFormDescriptorDelegate
  368. func initializeForm() {
  369. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  370. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  371. var section : XLFormSectionDescriptor
  372. var row : XLFormRowDescriptor
  373. // Section: Destination Folder
  374. section = XLFormSectionDescriptor.formSection()
  375. form.addFormSection(section)
  376. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  377. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, color: NCBrandColor.sharedInstance.brand) as UIImage
  378. row.cellConfig.setObject(imageFolder, forKey: "imageView.image" as NSCopying)
  379. row.cellConfig.setObject(UIColor.black, forKey: "textLabel.textColor" as NSCopying)
  380. row.action.formSelector = #selector(changeDestinationFolder(_:))
  381. section.addFormRow(row)
  382. // Section: File Name
  383. section = XLFormSectionDescriptor.formSection()
  384. form.addFormSection(section)
  385. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount, title: NSLocalizedString("_filename_", comment: ""))
  386. row.value = fileName
  387. section.addFormRow(row)
  388. self.form = form
  389. }
  390. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  391. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  392. if formRow.tag == "fileName" {
  393. self.form.delegate = nil
  394. if let fileNameNew = formRow.value {
  395. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as! String)
  396. }
  397. self.title = fileName
  398. self.form.delegate = self
  399. }
  400. }
  401. // MARK: - View Life Cycle
  402. override func viewDidLoad() {
  403. super.viewDidLoad()
  404. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
  405. self.navigationItem.rightBarButtonItem = saveButton
  406. self.navigationController?.navigationBar.isTranslucent = false
  407. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  408. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.navigationBarText
  409. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.navigationBarText]
  410. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  411. self.tableView.backgroundColor = NCBrandColor.sharedInstance.tableBackground
  412. self.reloadForm()
  413. }
  414. func reloadForm() {
  415. self.form.delegate = nil
  416. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  417. buttonDestinationFolder.title = self.titleServerUrl
  418. self.title = fileName
  419. self.tableView.reloadData()
  420. self.form.delegate = self
  421. }
  422. // MARK: - Action
  423. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  424. self.serverUrl = serverUrlTo
  425. if let title = title {
  426. self.titleServerUrl = title
  427. } else {
  428. self.titleServerUrl = "/"
  429. }
  430. self.reloadForm()
  431. }
  432. @objc func save() {
  433. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  434. guard let name = rowFileName.value else {
  435. return
  436. }
  437. let ext = (name as! NSString).pathExtension.uppercased()
  438. var fileNameSave = ""
  439. switch ext
  440. {
  441. case "":
  442. fileNameSave = name as! String + ".txt"
  443. case "TXT":
  444. fileNameSave = name as! String
  445. default:
  446. fileNameSave = (name as! NSString).deletingPathExtension + ".txt"
  447. }
  448. self.dismiss(animated: true, completion: {
  449. let data = self.text.data(using: .utf8)
  450. let success = FileManager.default.createFile(atPath: "\(self.appDelegate.directoryUser!)/\(fileNameSave)", contents: data, attributes: nil)
  451. if success {
  452. CCNetworking.shared().uploadFile(fileNameSave, serverUrl: self.serverUrl, session: k_upload_session, taskStatus: Int(k_taskStatusResume), selector: nil, selectorPost: nil, errorCode: 0, delegate: self.appDelegate.activeMain)
  453. } else {
  454. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  455. }
  456. })
  457. }
  458. func cancel() {
  459. self.dismiss(animated: true, completion: nil)
  460. }
  461. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  462. self.deselectFormRow(sender)
  463. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  464. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  465. let viewController : CCMove = navigationController.topViewController as! CCMove
  466. viewController.delegate = self;
  467. viewController.tintColor = NCBrandColor.sharedInstance.navigationBarText
  468. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  469. viewController.tintColorTitle = NCBrandColor.sharedInstance.navigationBarText
  470. viewController.move.title = NSLocalizedString("_select_", comment: "");
  471. viewController.networkingOperationQueue = appDelegate.netQueue
  472. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  473. self.present(navigationController, animated: true, completion: nil)
  474. }
  475. }