CCCreateCloud.swift 33 KB

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