CCNote.m 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. //
  2. // CCNoteViewController.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 25/11/14.
  6. // Copyright (c) 2014 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 "CCNote.h"
  24. #import "AppDelegate.h"
  25. @interface CCNote()
  26. {
  27. NSMutableDictionary *field;
  28. CCTemplates *templates;
  29. NSString *initHtml;
  30. }
  31. @end
  32. @implementation CCNote
  33. - (id)initWithDelegate:(id <CCNoteDelegate>)delegate fileName:(NSString *)fileName uuid:(NSString *)uuid rev:(NSString *)rev fileID:(NSString *)fileID modelReadOnly:(BOOL)modelReadOnly isLocal:(BOOL)isLocal serverUrl:(NSString *)serverUrl
  34. {
  35. self = [super init];
  36. if (self){
  37. self.delegate = delegate;
  38. self.fileName = fileName;
  39. self.isLocal = isLocal;
  40. self.rev = rev;
  41. self.fileID = fileID;
  42. self.uuid = uuid;
  43. self.serverUrl = serverUrl;
  44. CCCrypto *crypto = [[CCCrypto alloc] init];
  45. // if fileName read Crypto File
  46. if (fileName)
  47. field = [crypto getDictionaryEncrypted:fileName uuid:uuid isLocal:isLocal directoryUser:app.directoryUser];
  48. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelPressed:)];
  49. if (modelReadOnly) self.navigationItem.rightBarButtonItem = nil;
  50. else self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePressed:)];
  51. }
  52. return self;
  53. }
  54. - (void)viewDidLoad
  55. {
  56. [super viewDidLoad];
  57. if ([field objectForKey:@"titolo"]) {
  58. self.titolo = [field objectForKey:@"titolo"];
  59. self.shouldShowKeyboard = NO;
  60. } else {
  61. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  62. [formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
  63. self.titolo = [NSString stringWithFormat:@"Note %@", [formatter stringFromDate:[NSDate date]]];
  64. self.shouldShowKeyboard = YES;
  65. }
  66. //
  67. [self setHTML:[field objectForKey:@"note"]];
  68. //
  69. self.toolbarItemTintColor = COLOR_BRAND;
  70. templates = [[CCTemplates alloc] init];
  71. [templates setImageTitle:self.titolo conNavigationItem:self.navigationItem reachability:[app.reachability isReachable]];
  72. // Color
  73. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  74. self.view.backgroundColor = [UIColor whiteColor];
  75. }
  76. - (void)viewDidAppear:(BOOL)animated
  77. {
  78. [super viewDidAppear:animated];
  79. initHtml = [self getHTML];
  80. if (self.fileName && !field) [self performSelector:@selector(cancelPressed:) withObject:nil afterDelay:0.5];
  81. }
  82. #pragma --------------------------------------------------------------------------------------------
  83. #pragma mark - ==== IBAction ====
  84. #pragma --------------------------------------------------------------------------------------------
  85. - (IBAction)cancelPressed:(UIBarButtonItem * __unused)button
  86. {
  87. if ([initHtml isEqualToString:[self getHTML]] == NO) {
  88. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_save_exit_", nil) preferredStyle:UIAlertControllerStyleAlert];
  89. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil)
  90. style:UIAlertActionStyleDefault
  91. handler:^(UIAlertAction *action) {
  92. [self dismissViewControllerAnimated:YES completion:nil];
  93. }]];
  94. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_no_", nil)
  95. style:UIAlertActionStyleDefault
  96. handler:^(UIAlertAction *action) {
  97. return;
  98. }]];
  99. [self presentViewController:alertController animated:YES completion:nil];
  100. } else
  101. [self dismissViewControllerAnimated:YES completion:nil];
  102. }
  103. - (IBAction)savePressed:(UIBarButtonItem * __unused)button
  104. {
  105. NSString *fileNameModel;
  106. NSString *html = [self getHTML];
  107. if ([html length] > 0) {
  108. fileNameModel = [templates salvaNote:html titolo:self.titolo fileName:self.fileName uuid:self.uuid];
  109. if (fileNameModel) {
  110. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  111. metadataNet.action = actionUploadTemplate;
  112. metadataNet.serverUrl = self.serverUrl;
  113. metadataNet.fileName = [CCUtility trasformedFileNamePlistInCrypto:fileNameModel];
  114. metadataNet.fileNamePrint = _titolo;
  115. metadataNet.pathFolder = NSTemporaryDirectory();
  116. metadataNet.rev = self.rev;
  117. metadataNet.session = upload_session_foreground;
  118. metadataNet.taskStatus = k_taskStatusResume;
  119. // 2
  120. [app addNetworkingOperationQueue:app.netQueue delegate:self.delegate metadataNet:metadataNet];
  121. [self dismissViewControllerAnimated:YES completion:nil];
  122. }
  123. }
  124. }
  125. @end