CCCreateCloud.swift 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074
  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.openAssetsPickerController()
  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.openImportDocumentPicker()
  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. let storyboard = UIStoryboard(name: "NCText", bundle: nil)
  60. let controller = storyboard.instantiateViewController(withIdentifier: "NCText")
  61. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  62. appDelegate.activeMain.present(controller, animated: true, completion: nil)
  63. })
  64. if #available(iOS 11.0, *) {
  65. actionSheet.addButton(withTitle: NSLocalizedString("_scans_document_", comment: ""), image: CCGraphics.changeThemingColorImage(UIImage(named: "scan"), multiplier:2, color: colorGray), backgroundColor: NCBrandColor.sharedInstance.backgroundView, height: 50.0, type: AHKActionSheetButtonType.default, handler: {(AHKActionSheet) -> Void in
  66. NCCreateScanDocument.sharedInstance.openScannerDocument(viewController: appDelegate.activeMain, openScan: true)
  67. })
  68. }
  69. 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
  70. appDelegate.activeMain.createFolder()
  71. })
  72. actionSheet.show()
  73. }
  74. }
  75. // MARK: -
  76. @objc protocol createFormUploadAssetsDelegate {
  77. func dismissFormUploadAssets()
  78. }
  79. // MARK: -
  80. class CreateFormUploadAssets: XLFormViewController, CCMoveDelegate {
  81. var serverUrl : String = ""
  82. var titleServerUrl : String?
  83. var assets: NSMutableArray = []
  84. var cryptated : Bool = false
  85. var session : String = ""
  86. weak var delegate: createFormUploadAssetsDelegate?
  87. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  88. @objc convenience init(serverUrl : String, assets : NSMutableArray, cryptated : Bool, session : String, delegate: createFormUploadAssetsDelegate) {
  89. self.init()
  90. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  91. titleServerUrl = "/"
  92. } else {
  93. titleServerUrl = (serverUrl as NSString).lastPathComponent
  94. }
  95. self.serverUrl = serverUrl
  96. self.assets = assets
  97. self.cryptated = cryptated
  98. self.session = session
  99. self.delegate = delegate
  100. self.initializeForm()
  101. }
  102. //MARK: XLFormDescriptorDelegate
  103. func initializeForm() {
  104. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_upload_photos_videos_", comment: "")) as XLFormDescriptor
  105. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  106. var section : XLFormSectionDescriptor
  107. var row : XLFormRowDescriptor
  108. // Section: Destination Folder
  109. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  110. form.addFormSection(section)
  111. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  112. row.action.formSelector = #selector(changeDestinationFolder(_:))
  113. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  114. row.cellConfig["imageView.image"] = imageFolder
  115. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.left.rawValue
  116. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  117. row.cellConfig["textLabel.textColor"] = UIColor.black
  118. section.addFormRow(row)
  119. // User folder Media
  120. row = XLFormRowDescriptor(tag: "useFolderMedia", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_use_folder_media_", comment: ""))
  121. row.value = 0
  122. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  123. row.cellConfig["textLabel.textColor"] = UIColor.black
  124. section.addFormRow(row)
  125. // Use Sub folder
  126. row = XLFormRowDescriptor(tag: "useSubFolder", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_autoupload_create_subfolder_", comment: ""))
  127. let tableAccount = NCManageDatabase.sharedInstance.getAccountActive()
  128. if tableAccount?.autoUploadCreateSubfolder == true {
  129. row.value = 1
  130. } else {
  131. row.value = 0
  132. }
  133. row.hidden = "$\("useFolderMedia") == 0"
  134. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  135. row.cellConfig["textLabel.textColor"] = UIColor.black
  136. section.addFormRow(row)
  137. // Section Mode filename
  138. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_mode_filename_", comment: ""))
  139. form.addFormSection(section)
  140. // Maintain the original fileName
  141. row = XLFormRowDescriptor(tag: "maintainOriginalFileName", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_maintain_original_filename_", comment: ""))
  142. row.value = CCUtility.getOriginalFileName(k_keyFileNameOriginal)
  143. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  144. row.cellConfig["textLabel.textColor"] = UIColor.black
  145. section.addFormRow(row)
  146. // Add File Name Type
  147. row = XLFormRowDescriptor(tag: "addFileNameType", rowType: XLFormRowDescriptorTypeBooleanSwitch, title: NSLocalizedString("_add_filenametype_", comment: ""))
  148. row.value = CCUtility.getFileNameType(k_keyFileNameType)
  149. row.hidden = "$\("maintainOriginalFileName") == 1"
  150. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  151. row.cellConfig["textLabel.textColor"] = UIColor.black
  152. section.addFormRow(row)
  153. // Section: Rename File Name
  154. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  155. form.addFormSection(section)
  156. row = XLFormRowDescriptor(tag: "maskFileName", rowType: XLFormRowDescriptorTypeAccount, title: (NSLocalizedString("_filename_", comment: ""))+":")
  157. let fileNameMask : String = CCUtility.getFileNameMask(k_keyFileNameMask)
  158. if fileNameMask.count > 0 {
  159. row.value = fileNameMask
  160. }
  161. row.hidden = "$\("maintainOriginalFileName") == 1"
  162. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  163. row.cellConfig["textLabel.textColor"] = UIColor.black
  164. section.addFormRow(row)
  165. // Section: Preview File Name
  166. row = XLFormRowDescriptor(tag: "previewFileName", rowType: XLFormRowDescriptorTypeTextView, title: "")
  167. row.height = 180
  168. row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "backgroundColor" as NSCopying)
  169. row.cellConfig.setObject(NCBrandColor.sharedInstance.backgroundView, forKey: "textView.backgroundColor" as NSCopying)
  170. row.disabled = true
  171. row.cellConfig.setObject(UIFont.systemFont(ofSize: 15.0), forKey: "textLabel.font" as NSCopying)
  172. section.addFormRow(row)
  173. self.form = form
  174. }
  175. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  176. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  177. if formRow.tag == "useFolderMedia" {
  178. if (formRow.value! as AnyObject).boolValue == true {
  179. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  180. buttonDestinationFolder.hidden = true
  181. } else{
  182. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  183. buttonDestinationFolder.hidden = false
  184. }
  185. }
  186. else if formRow.tag == "useSubFolder" {
  187. if (formRow.value! as AnyObject).boolValue == true {
  188. } else{
  189. }
  190. }
  191. else if formRow.tag == "maintainOriginalFileName" {
  192. CCUtility.setOriginalFileName((formRow.value! as AnyObject).boolValue, key: k_keyFileNameOriginal)
  193. self.reloadForm()
  194. }
  195. else if formRow.tag == "addFileNameType" {
  196. CCUtility.setFileNameType((formRow.value! as AnyObject).boolValue, key: k_keyFileNameType)
  197. self.reloadForm()
  198. }
  199. else if formRow.tag == "maskFileName" {
  200. let fileName = formRow.value as? String
  201. self.form.delegate = nil
  202. if let fileName = fileName {
  203. formRow.value = CCUtility.removeForbiddenCharactersServer(fileName)
  204. }
  205. self.form.delegate = self
  206. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  207. previewFileName.value = self.previewFileName(valueRename: formRow.value as? String)
  208. // reload cell
  209. if fileName != nil {
  210. if newValue as! String != formRow.value as! String {
  211. self.reloadFormRow(formRow)
  212. appDelegate.messageNotification("_info_", description: "_forbidden_characters_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  213. }
  214. }
  215. self.reloadFormRow(previewFileName)
  216. }
  217. }
  218. // MARK: - View Life Cycle
  219. override func viewDidLoad() {
  220. super.viewDidLoad()
  221. let cancelButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_cancel_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(cancel))
  222. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
  223. self.navigationItem.leftBarButtonItem = cancelButton
  224. self.navigationItem.rightBarButtonItem = saveButton
  225. self.navigationController?.navigationBar.isTranslucent = false
  226. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  227. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  228. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
  229. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  230. self.reloadForm()
  231. }
  232. override func viewWillDisappear(_ animated: Bool)
  233. {
  234. super.viewWillDisappear(animated)
  235. self.delegate?.dismissFormUploadAssets()
  236. }
  237. func reloadForm() {
  238. self.form.delegate = nil
  239. let buttonDestinationFolder : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  240. buttonDestinationFolder.title = self.titleServerUrl
  241. let maskFileName : XLFormRowDescriptor = self.form.formRow(withTag: "maskFileName")!
  242. let previewFileName : XLFormRowDescriptor = self.form.formRow(withTag: "previewFileName")!
  243. previewFileName.value = self.previewFileName(valueRename: maskFileName.value as? String)
  244. self.tableView.reloadData()
  245. self.form.delegate = self
  246. }
  247. // MARK: - Action
  248. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  249. self.serverUrl = serverUrlTo
  250. if let title = title {
  251. self.titleServerUrl = title
  252. } else {
  253. self.titleServerUrl = "/"
  254. }
  255. self.reloadForm()
  256. }
  257. @objc func save() {
  258. self.dismiss(animated: true, completion: {
  259. let useFolderPhotoRow : XLFormRowDescriptor = self.form.formRow(withTag: "useFolderMedia")!
  260. let useSubFolderRow : XLFormRowDescriptor = self.form.formRow(withTag: "useSubFolder")!
  261. var useSubFolder : Bool = false
  262. if (useFolderPhotoRow.value! as AnyObject).boolValue == true {
  263. self.serverUrl = NCManageDatabase.sharedInstance.getAccountAutoUploadPath(self.appDelegate.activeUrl)
  264. useSubFolder = (useSubFolderRow.value! as AnyObject).boolValue
  265. }
  266. self.appDelegate.activeMain.uploadFileAsset(self.assets, serverUrl: self.serverUrl, useSubFolder: useSubFolder, session: self.session)
  267. })
  268. }
  269. @objc func cancel() {
  270. self.dismiss(animated: true, completion: nil)
  271. }
  272. // MARK: - Utility
  273. func previewFileName(valueRename : String?) -> String {
  274. var returnString: String = ""
  275. let asset = assets[0] as! PHAsset
  276. if (CCUtility.getOriginalFileName(k_keyFileNameOriginal)) {
  277. return (NSLocalizedString("_filename_", comment: "") + ": " + (asset.value(forKey: "filename") as! String))
  278. } else if let valueRename = valueRename {
  279. let valueRenameTrimming = valueRename.trimmingCharacters(in: CharacterSet.whitespacesAndNewlines)
  280. if valueRenameTrimming.count > 0 {
  281. self.form.delegate = nil
  282. CCUtility.setFileNameMask(valueRename, key: k_keyFileNameMask)
  283. self.form.delegate = self
  284. returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  285. } else {
  286. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  287. returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  288. }
  289. } else {
  290. CCUtility.setFileNameMask("", key: k_keyFileNameMask)
  291. returnString = CCUtility.createFileName(asset.value(forKey: "filename"), fileDate: asset.creationDate, fileType: asset.mediaType, keyFileName: nil, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)
  292. }
  293. return String(format: NSLocalizedString("_preview_filename_", comment: ""), "MM,MMM,DD,YY,YYYY and HH,hh,mm,ss,ampm") + ":" + "\n\n" + returnString
  294. }
  295. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  296. self.deselectFormRow(sender)
  297. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  298. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  299. let viewController : CCMove = navigationController.topViewController as! CCMove
  300. viewController.delegate = self;
  301. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  302. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  303. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  304. viewController.move.title = NSLocalizedString("_select_", comment: "");
  305. viewController.networkingOperationQueue = appDelegate.netQueue
  306. // E2EE
  307. viewController.includeDirectoryE2EEncryption = true;
  308. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  309. self.present(navigationController, animated: true, completion: nil)
  310. }
  311. }
  312. // MARK: -
  313. class CreateFormUploadFileText: XLFormViewController, CCMoveDelegate {
  314. var serverUrl = ""
  315. var titleServerUrl = ""
  316. var fileName = ""
  317. var text = ""
  318. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  319. convenience init(serverUrl: String, text: String, fileName: String) {
  320. self.init()
  321. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  322. titleServerUrl = "/"
  323. } else {
  324. titleServerUrl = (serverUrl as NSString).lastPathComponent
  325. }
  326. self.fileName = fileName
  327. self.serverUrl = serverUrl
  328. self.text = text
  329. initializeForm()
  330. }
  331. //MARK: XLFormDescriptorDelegate
  332. func initializeForm() {
  333. let form : XLFormDescriptor = XLFormDescriptor() as XLFormDescriptor
  334. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  335. var section : XLFormSectionDescriptor
  336. var row : XLFormRowDescriptor
  337. // Section: Destination Folder
  338. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  339. form.addFormSection(section)
  340. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  341. row.action.formSelector = #selector(changeDestinationFolder(_:))
  342. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  343. row.cellConfig["imageView.image"] = imageFolder
  344. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.left.rawValue
  345. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  346. row.cellConfig["textLabel.textColor"] = UIColor.black
  347. section.addFormRow(row)
  348. // Section: File Name
  349. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  350. form.addFormSection(section)
  351. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount)
  352. row.value = self.fileName
  353. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 14.0)
  354. row.cellConfig["textLabel.textColor"] = UIColor.black
  355. section.addFormRow(row)
  356. self.form = form
  357. }
  358. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  359. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  360. if formRow.tag == "fileName" {
  361. self.form.delegate = nil
  362. if let fileNameNew = formRow.value {
  363. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as! String)
  364. }
  365. formRow.value = self.fileName
  366. self.title = fileName
  367. self.updateFormRow(formRow)
  368. self.form.delegate = self
  369. }
  370. }
  371. // MARK: - View Life Cycle
  372. override func viewDidLoad() {
  373. super.viewDidLoad()
  374. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
  375. self.navigationItem.rightBarButtonItem = saveButton
  376. self.navigationController?.navigationBar.isTranslucent = false
  377. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  378. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  379. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
  380. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  381. }
  382. // MARK: - Action
  383. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  384. self.serverUrl = serverUrlTo
  385. if let title = title {
  386. self.titleServerUrl = title
  387. } else {
  388. self.titleServerUrl = "/"
  389. }
  390. // Update
  391. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  392. row.title = self.titleServerUrl
  393. self.updateFormRow(row)
  394. }
  395. @objc func save() {
  396. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  397. guard let name = rowFileName.value else {
  398. return
  399. }
  400. let ext = (name as! NSString).pathExtension.uppercased()
  401. var fileNameSave = ""
  402. if (ext == "") {
  403. fileNameSave = name as! String + ".txt"
  404. } else if (CCUtility.isDocumentModifiableExtension(ext)) {
  405. fileNameSave = name as! String
  406. } else {
  407. fileNameSave = (name as! NSString).deletingPathExtension + ".txt"
  408. }
  409. guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl) else {
  410. return
  411. }
  412. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave))
  413. if (metadata != nil) {
  414. let alertController = UIAlertController(title: fileNameSave, message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: .alert)
  415. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in
  416. }
  417. let overwriteAction = UIAlertAction(title: NSLocalizedString("_overwrite_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  418. self.dismissAndUpload(fileNameSave, fileID: metadata!.fileID, directoryID: directoryID)
  419. }
  420. alertController.addAction(cancelAction)
  421. alertController.addAction(overwriteAction)
  422. self.present(alertController, animated: true, completion:nil)
  423. } else {
  424. let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl)!
  425. dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
  426. }
  427. }
  428. func dismissAndUpload(_ fileNameSave: String, fileID: String, directoryID: String) {
  429. self.dismiss(animated: true, completion: {
  430. let data = self.text.data(using: .utf8)
  431. let success = FileManager.default.createFile(atPath: CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave), contents: data, attributes: nil)
  432. if success {
  433. let metadataForUpload = tableMetadata()
  434. metadataForUpload.account = self.appDelegate.activeAccount
  435. metadataForUpload.date = NSDate()
  436. metadataForUpload.directoryID = directoryID
  437. metadataForUpload.fileID = fileID
  438. metadataForUpload.fileName = fileNameSave
  439. metadataForUpload.fileNameView = fileNameSave
  440. metadataForUpload.session = k_upload_session
  441. metadataForUpload.sessionSelector = selectorUploadFile
  442. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  443. _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  444. self.appDelegate.perform(#selector(self.appDelegate.loadAutoDownloadUpload), on: Thread.main, with: nil, waitUntilDone: true)
  445. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL))
  446. } else {
  447. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  448. }
  449. })
  450. }
  451. func cancel() {
  452. self.dismiss(animated: true, completion: nil)
  453. }
  454. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  455. self.deselectFormRow(sender)
  456. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  457. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  458. let viewController : CCMove = navigationController.topViewController as! CCMove
  459. viewController.delegate = self;
  460. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  461. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  462. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  463. viewController.move.title = NSLocalizedString("_select_", comment: "");
  464. viewController.networkingOperationQueue = appDelegate.netQueue
  465. // E2EE
  466. viewController.includeDirectoryE2EEncryption = true;
  467. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  468. self.present(navigationController, animated: true, completion: nil)
  469. }
  470. }
  471. //MARK: -
  472. class CreateFormUploadScanDocument: XLFormViewController, CCMoveDelegate {
  473. var serverUrl = ""
  474. var titleServerUrl = ""
  475. var arrayImages = [UIImage]()
  476. var fileName = "scan.pdf"
  477. var password : PDFPassword = ""
  478. var compressionQuality: Double = 0.5
  479. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  480. convenience init(serverUrl: String, arrayImages: [UIImage]) {
  481. self.init()
  482. if serverUrl == CCUtility.getHomeServerUrlActiveUrl(appDelegate.activeUrl) {
  483. titleServerUrl = "/"
  484. } else {
  485. titleServerUrl = (serverUrl as NSString).lastPathComponent
  486. }
  487. self.serverUrl = serverUrl
  488. self.arrayImages = arrayImages
  489. initializeForm()
  490. }
  491. //MARK: XLFormDescriptorDelegate
  492. func initializeForm() {
  493. let form : XLFormDescriptor = XLFormDescriptor(title: NSLocalizedString("_save_settings_", comment: "")) as XLFormDescriptor
  494. form.rowNavigationOptions = XLFormRowNavigationOptions.stopDisableRow
  495. var section : XLFormSectionDescriptor
  496. var row : XLFormRowDescriptor
  497. // Section: Destination Folder
  498. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_save_path_", comment: ""))
  499. form.addFormSection(section)
  500. row = XLFormRowDescriptor(tag: "ButtonDestinationFolder", rowType: XLFormRowDescriptorTypeButton, title: self.titleServerUrl)
  501. row.action.formSelector = #selector(changeDestinationFolder(_:))
  502. let imageFolder = CCGraphics.changeThemingColorImage(UIImage(named: "folder")!, multiplier:2, color: NCBrandColor.sharedInstance.brandElement) as UIImage
  503. row.cellConfig["imageView.image"] = imageFolder
  504. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.left.rawValue
  505. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  506. row.cellConfig["textLabel.textColor"] = UIColor.black
  507. section.addFormRow(row)
  508. // Section: Quality
  509. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_quality_image_title_", comment: ""))
  510. form.addFormSection(section)
  511. row = XLFormRowDescriptor(tag: "compressionQuality", rowType: XLFormRowDescriptorTypeSlider)
  512. row.value = 0.5
  513. row.title = NSLocalizedString("_quality_medium_", comment: "")
  514. row.cellConfig["slider.minimumTrackTintColor"] = NCBrandColor.sharedInstance.brand
  515. row.cellConfig["slider.maximumValue"] = 1
  516. row.cellConfig["slider.minimumValue"] = 0
  517. row.cellConfig["steps"] = 2
  518. row.cellConfig["textLabel.textAlignment"] = NSTextAlignment.center.rawValue
  519. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 15.0)
  520. row.cellConfig["textLabel.textColor"] = UIColor.black
  521. section.addFormRow(row)
  522. // Section: Password
  523. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_password_", comment: ""))
  524. form.addFormSection(section)
  525. row = XLFormRowDescriptor(tag: "password", rowType: XLFormRowDescriptorTypeAccount)
  526. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 14.0)
  527. row.cellConfig["textLabel.textColor"] = UIColor.black
  528. section.addFormRow(row)
  529. // Section: File Name
  530. section = XLFormSectionDescriptor.formSection(withTitle: NSLocalizedString("_filename_", comment: ""))
  531. form.addFormSection(section)
  532. row = XLFormRowDescriptor(tag: "fileName", rowType: XLFormRowDescriptorTypeAccount)
  533. row.value = self.fileName
  534. row.cellConfig["textLabel.font"] = UIFont.systemFont(ofSize: 14.0)
  535. row.cellConfig["textLabel.textColor"] = UIColor.black
  536. section.addFormRow(row)
  537. self.form = form
  538. }
  539. override func formRowDescriptorValueHasChanged(_ formRow: XLFormRowDescriptor!, oldValue: Any!, newValue: Any!) {
  540. super.formRowDescriptorValueHasChanged(formRow, oldValue: oldValue, newValue: newValue)
  541. if formRow.tag == "fileName" {
  542. self.form.delegate = nil
  543. if let fileNameNew = formRow.value {
  544. self.fileName = CCUtility.removeForbiddenCharactersServer(fileNameNew as! String)
  545. }
  546. formRow.value = self.fileName
  547. self.updateFormRow(formRow)
  548. self.form.delegate = self
  549. }
  550. if formRow.tag == "compressionQuality" {
  551. self.form.delegate = nil
  552. //let row : XLFormRowDescriptor = self.form.formRow(withTag: "descriptionQuality")!
  553. let newQuality = newValue as? NSNumber
  554. compressionQuality = (newQuality?.doubleValue)!
  555. if compressionQuality >= 0.0 && compressionQuality <= 0.3 {
  556. formRow.title = NSLocalizedString("_quality_low_", comment: "")
  557. compressionQuality = 0.1
  558. } else if compressionQuality >= 0.4 && compressionQuality <= 0.6 {
  559. formRow.title = NSLocalizedString("_quality_medium_", comment: "")
  560. compressionQuality = 0.5
  561. } else if compressionQuality >= 0.7 && compressionQuality <= 1.0 {
  562. formRow.title = NSLocalizedString("_quality_high_", comment: "")
  563. compressionQuality = 0.8
  564. }
  565. self.updateFormRow(formRow)
  566. self.form.delegate = self
  567. }
  568. if formRow.tag == "password" {
  569. let stringPassword = newValue as? String
  570. if stringPassword != nil {
  571. password = PDFPassword(stringPassword!)
  572. } else {
  573. password = PDFPassword("")
  574. }
  575. }
  576. }
  577. // MARK: - View Life Cycle
  578. override func viewDidLoad() {
  579. super.viewDidLoad()
  580. let saveButton : UIBarButtonItem = UIBarButtonItem(title: NSLocalizedString("_save_", comment: ""), style: UIBarButtonItemStyle.plain, target: self, action: #selector(save))
  581. self.navigationItem.rightBarButtonItem = saveButton
  582. self.navigationController?.navigationBar.isTranslucent = false
  583. self.navigationController?.navigationBar.barTintColor = NCBrandColor.sharedInstance.brand
  584. self.navigationController?.navigationBar.tintColor = NCBrandColor.sharedInstance.brandText
  585. self.navigationController?.navigationBar.titleTextAttributes = [NSAttributedStringKey.foregroundColor: NCBrandColor.sharedInstance.brandText]
  586. self.tableView.separatorStyle = UITableViewCellSeparatorStyle.none
  587. // self.tableView.sectionHeaderHeight = 10
  588. // self.tableView.sectionFooterHeight = 10
  589. // self.tableView.backgroundColor = NCBrandColor.sharedInstance.backgroundView
  590. // let row : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  591. // let rowCell = row.cell(forForm: self)
  592. // rowCell.becomeFirstResponder()
  593. }
  594. // MARK: - Action
  595. func moveServerUrl(to serverUrlTo: String!, title: String!) {
  596. self.serverUrl = serverUrlTo
  597. if let title = title {
  598. self.titleServerUrl = title
  599. } else {
  600. self.titleServerUrl = "/"
  601. }
  602. // Update
  603. let row : XLFormRowDescriptor = self.form.formRow(withTag: "ButtonDestinationFolder")!
  604. row.title = self.titleServerUrl
  605. self.updateFormRow(row)
  606. }
  607. @objc func save() {
  608. let rowFileName : XLFormRowDescriptor = self.form.formRow(withTag: "fileName")!
  609. guard let name = rowFileName.value else {
  610. return
  611. }
  612. let ext = (name as! NSString).pathExtension.uppercased()
  613. var fileNameSave = ""
  614. if (ext == "") {
  615. fileNameSave = name as! String + ".pdf"
  616. } else {
  617. fileNameSave = (name as! NSString).deletingPathExtension + ".pdf"
  618. }
  619. guard let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl) else {
  620. return
  621. }
  622. let metadata = NCManageDatabase.sharedInstance.getMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave))
  623. if (metadata != nil) {
  624. let alertController = UIAlertController(title: fileNameSave, message: NSLocalizedString("_file_already_exists_", comment: ""), preferredStyle: .alert)
  625. let cancelAction = UIAlertAction(title: NSLocalizedString("_cancel_", comment: ""), style: .default) { (action:UIAlertAction) in
  626. }
  627. let overwriteAction = UIAlertAction(title: NSLocalizedString("_overwrite_", comment: ""), style: .cancel) { (action:UIAlertAction) in
  628. NCManageDatabase.sharedInstance.deleteMetadata(predicate: NSPredicate(format: "directoryID == %@ AND fileNameView == %@", directoryID, fileNameSave), clearDateReadDirectoryID: directoryID)
  629. self.dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
  630. }
  631. alertController.addAction(cancelAction)
  632. alertController.addAction(overwriteAction)
  633. self.present(alertController, animated: true, completion:nil)
  634. } else {
  635. let directoryID = NCManageDatabase.sharedInstance.getDirectoryID(self.serverUrl)!
  636. dismissAndUpload(fileNameSave, fileID: directoryID + fileNameSave, directoryID: directoryID)
  637. }
  638. }
  639. func dismissAndUpload(_ fileNameSave: String, fileID: String, directoryID: String) {
  640. var pdfPages = [PDFPage]()
  641. guard let fileNameGeneratePDF = CCUtility.getDirectoryProviderStorageFileID(fileID, fileNameView: fileNameSave) else {
  642. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  643. return
  644. }
  645. //Generate PDF
  646. for image in self.arrayImages {
  647. guard let data = UIImageJPEGRepresentation(image, CGFloat(compressionQuality)) else {
  648. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  649. return
  650. }
  651. let page = PDFPage.image(UIImage(data: data)!)
  652. pdfPages.append(page)
  653. }
  654. do {
  655. try PDFGenerator.generate(pdfPages, to: fileNameGeneratePDF, password: password)
  656. } catch {
  657. self.appDelegate.messageNotification("_error_", description: "_error_creation_file_", visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.info, errorCode: 0)
  658. return
  659. }
  660. //Create metadata for upload
  661. let metadataForUpload = tableMetadata()
  662. metadataForUpload.account = self.appDelegate.activeAccount
  663. metadataForUpload.date = NSDate()
  664. metadataForUpload.directoryID = directoryID
  665. metadataForUpload.fileID = fileID
  666. metadataForUpload.fileName = fileNameSave
  667. metadataForUpload.fileNameView = fileNameSave
  668. metadataForUpload.session = k_upload_session
  669. metadataForUpload.sessionSelector = selectorUploadFile
  670. metadataForUpload.status = Int(k_metadataStatusWaitUpload)
  671. _ = NCManageDatabase.sharedInstance.addMetadata(metadataForUpload)
  672. self.appDelegate.perform(#selector(self.appDelegate.loadAutoDownloadUpload), on: Thread.main, with: nil, waitUntilDone: true)
  673. NCMainCommon.sharedInstance.reloadDatasource(ServerUrl: self.serverUrl, fileID: nil, action: Int32(k_action_NULL))
  674. self.dismiss(animated: true, completion: nil)
  675. }
  676. func cancel() {
  677. self.dismiss(animated: true, completion: nil)
  678. }
  679. @objc func changeDestinationFolder(_ sender: XLFormRowDescriptor) {
  680. self.deselectFormRow(sender)
  681. let storyboard : UIStoryboard = UIStoryboard(name: "CCMove", bundle: nil)
  682. let navigationController = storyboard.instantiateViewController(withIdentifier: "CCMove") as! UINavigationController
  683. let viewController : CCMove = navigationController.topViewController as! CCMove
  684. viewController.delegate = self;
  685. viewController.tintColor = NCBrandColor.sharedInstance.brandText
  686. viewController.barTintColor = NCBrandColor.sharedInstance.brand
  687. viewController.tintColorTitle = NCBrandColor.sharedInstance.brandText
  688. viewController.move.title = NSLocalizedString("_select_", comment: "");
  689. viewController.networkingOperationQueue = appDelegate.netQueue
  690. // E2EE
  691. viewController.includeDirectoryE2EEncryption = true;
  692. navigationController.modalPresentationStyle = UIModalPresentationStyle.formSheet
  693. self.present(navigationController, animated: true, completion: nil)
  694. }
  695. }
  696. class NCCreateScanDocument : NSObject, ImageScannerControllerDelegate {
  697. @objc static let sharedInstance: NCCreateScanDocument = {
  698. let instance = NCCreateScanDocument()
  699. return instance
  700. }()
  701. let appDelegate = UIApplication.shared.delegate as! AppDelegate
  702. var viewController: UIViewController?
  703. var openScan: Bool = false
  704. @available(iOS 10, *)
  705. func openScannerDocument(viewController: UIViewController, openScan: Bool) {
  706. self.viewController = viewController
  707. self.openScan = openScan
  708. let scannerVC = ImageScannerController()
  709. scannerVC.imageScannerDelegate = self
  710. self.viewController?.present(scannerVC, animated: true, completion: nil)
  711. }
  712. @available(iOS 10, *)
  713. func imageScannerController(_ scanner: ImageScannerController, didFinishScanningWithResults results: ImageScannerResults) {
  714. let fileName = CCUtility.createFileName("scan.png", fileDate: Date(), fileType: PHAssetMediaType.image, keyFileName: k_keyFileNameMask, keyFileNameType: k_keyFileNameType, keyFileNameOriginal: k_keyFileNameOriginal)!
  715. let fileNamePath = CCUtility.getDirectoryScan() + "/" + fileName
  716. // A4 74 DPI : 595 x 842 px
  717. var image = results.scannedImage
  718. let imageWidthInPixels = image.size.width * results.scannedImage.scale
  719. let imageHeightInPixels = image.size.height * results.scannedImage.scale
  720. if imageWidthInPixels > 595 || imageHeightInPixels > 842 {
  721. image = CCGraphics.scale(image, to: CGSize(width: 595, height: 842), isAspectRation: true)
  722. }
  723. do {
  724. try UIImagePNGRepresentation(image)?.write(to: NSURL.fileURL(withPath: fileNamePath), options: .atomic)
  725. } catch { }
  726. scanner.dismiss(animated: true, completion: {
  727. if (self.openScan) {
  728. let storyboard = UIStoryboard(name: "Scan", bundle: nil)
  729. let controller = storyboard.instantiateInitialViewController()!
  730. controller.modalPresentationStyle = UIModalPresentationStyle.pageSheet
  731. self.viewController?.present(controller, animated: true, completion: nil)
  732. }
  733. })
  734. }
  735. @available(iOS 10, *)
  736. func imageScannerControllerDidCancel(_ scanner: ImageScannerController) {
  737. scanner.dismiss(animated: true, completion: nil)
  738. }
  739. @available(iOS 10, *)
  740. func imageScannerController(_ scanner: ImageScannerController, didFailWithError error: Error) {
  741. appDelegate.messageNotification("_error_", description: error.localizedDescription, visible: true, delay: TimeInterval(k_dismissAfterSecond), type: TWMessageBarMessageType.error, errorCode: Int(k_CCErrorInternalError))
  742. }
  743. }