CCNote.m 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. NSString *_saveFileID;
  31. }
  32. @end
  33. @implementation CCNote
  34. - (id)initWithDelegate:(id <CCNoteDelegate>)delegate fileName:(NSString *)fileName uuid:(NSString *)uuid fileID:(NSString *)fileID isLocal:(BOOL)isLocal serverUrl:(NSString *)serverUrl
  35. {
  36. self = [super init];
  37. if (self){
  38. self.delegate = delegate;
  39. self.fileName = fileName;
  40. self.isLocal = isLocal;
  41. self.fileID = fileID;
  42. self.uuid = uuid;
  43. self.serverUrl = serverUrl;
  44. // if fileName read Crypto File
  45. if (fileName)
  46. field = [[CCCrypto sharedManager] getDictionaryEncrypted:fileName uuid:uuid isLocal:isLocal directoryUser:app.directoryUser];
  47. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelPressed:)];
  48. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePressed:)];
  49. }
  50. return self;
  51. }
  52. - (void)viewDidLoad
  53. {
  54. [super viewDidLoad];
  55. if ([field objectForKey:@"titolo"]) {
  56. self.titolo = [field objectForKey:@"titolo"];
  57. self.shouldShowKeyboard = NO;
  58. } else {
  59. NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
  60. [formatter setDateFormat:@"yyyy-MM-dd HH-mm-ss"];
  61. self.titolo = [NSString stringWithFormat:@"Note %@", [formatter stringFromDate:[NSDate date]]];
  62. self.shouldShowKeyboard = YES;
  63. }
  64. //
  65. [self setHTML:[field objectForKey:@"note"]];
  66. //
  67. self.toolbarItemTintColor = COLOR_BRAND;
  68. templates = [[CCTemplates alloc] init];
  69. [templates setImageTitle:self.titolo conNavigationItem:self.navigationItem reachability:[app.reachability isReachable]];
  70. // Color
  71. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  72. self.view.backgroundColor = [UIColor whiteColor];
  73. }
  74. - (void)viewDidAppear:(BOOL)animated
  75. {
  76. [super viewDidAppear:animated];
  77. initHtml = [self getHTML];
  78. if (self.fileName && !field) [self performSelector:@selector(cancelPressed:) withObject:nil afterDelay:0.5];
  79. }
  80. #pragma --------------------------------------------------------------------------------------------
  81. #pragma mark - ==== IBAction ====
  82. #pragma --------------------------------------------------------------------------------------------
  83. - (IBAction)cancelPressed:(UIBarButtonItem * __unused)button
  84. {
  85. if ([initHtml isEqualToString:[self getHTML]] == NO) {
  86. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_info_", nil) message:NSLocalizedString(@"_save_exit_", nil) preferredStyle:UIAlertControllerStyleAlert];
  87. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil)
  88. style:UIAlertActionStyleDefault
  89. handler:^(UIAlertAction *action) {
  90. [self dismissViewControllerAnimated:YES completion:nil];
  91. }]];
  92. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_no_", nil)
  93. style:UIAlertActionStyleDefault
  94. handler:^(UIAlertAction *action) {
  95. return;
  96. }]];
  97. [self presentViewController:alertController animated:YES completion:nil];
  98. } else
  99. [self dismissViewControllerAnimated:YES completion:nil];
  100. }
  101. - (IBAction)savePressed:(UIBarButtonItem * __unused)button
  102. {
  103. NSString *fileNameModel;
  104. NSString *html = [self getHTML];
  105. if ([html length] > 0) {
  106. fileNameModel = [templates salvaNote:html titolo:self.titolo fileName:self.fileName uuid:self.uuid];
  107. if (fileNameModel) {
  108. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  109. metadataNet.action = actionUploadTemplate;
  110. metadataNet.serverUrl = self.serverUrl;
  111. metadataNet.fileName = [CCUtility trasformedFileNamePlistInCrypto:fileNameModel];
  112. metadataNet.fileNamePrint = _titolo;
  113. metadataNet.pathFolder = NSTemporaryDirectory();
  114. metadataNet.session = k_upload_session_foreground;
  115. metadataNet.taskStatus = k_taskStatusResume;
  116. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  117. }
  118. }
  119. }
  120. - (void)uploadFileFailure:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  121. {
  122. if (![_saveFileID isEqualToString:fileID]) {
  123. _saveFileID = fileID;
  124. [app messageNotification:@"_upload_file_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError];
  125. // remove the file
  126. [CCCoreData deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"(fileID == %@) AND (account == %@)", fileID, app.activeAccount]];
  127. [self.delegate readFolderWithForced:YES serverUrl:self.serverUrl];
  128. }
  129. }
  130. - (void)uploadFileSuccess:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  131. {
  132. [self dismissViewControllerAnimated:YES completion:nil];
  133. }
  134. @end