CCNote.m 6.9 KB

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