CCNote.m 6.9 KB

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