CCCreateCloud.swift 28 KB

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