CCNote.m 7.3 KB

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