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