CCCreateCloud.swift 33 KB

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