CCAccountWeb.m 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. //
  2. // CCAccountWeb.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 24/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 "CCAccountWeb.h"
  24. #import "AppDelegate.h"
  25. #import "NCBridgeSwift.h"
  26. @interface CCAccountWeb()
  27. {
  28. XLFormDescriptor *form ;
  29. NSTimer* myTimer;
  30. NSMutableDictionary *field;
  31. CCTemplates *templates;
  32. NSString *_saveEtag;
  33. }
  34. @end
  35. @implementation CCAccountWeb
  36. - (id)initWithDelegate:(id <CCAccountWebDelegate>)delegate fileName:(NSString *)fileName uuid:(NSString *)uuid fileID:(NSString *)fileID isLocal:(BOOL)isLocal serverUrl:(NSString *)serverUrl
  37. {
  38. self = [super init];
  39. if (self) {
  40. self.delegate = delegate;
  41. self.fileName = fileName;
  42. self.isLocal = isLocal;
  43. self.fileID = fileID;
  44. self.uuid = uuid;
  45. self.serverUrl = serverUrl;
  46. // if fileName read Crypto File
  47. if (fileName)
  48. field = [[CCCrypto sharedManager] getDictionaryEncrypted:fileName uuid:uuid isLocal:isLocal directoryUser:app.directoryUser];
  49. XLFormSectionDescriptor *section;
  50. XLFormRowDescriptor *row;
  51. // Information - Section
  52. form = [XLFormDescriptor formDescriptor];
  53. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_title_", nil)];
  54. [form addFormSection:section];
  55. if (!fileName) form.assignFirstResponderOnShow = YES;
  56. // Title
  57. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"titolo" rowType:XLFormRowDescriptorTypeText];
  58. [row.cellConfig setObject:[NCBrandColor sharedInstance].cryptocloud forKey:@"textField.textColor"];
  59. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  60. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  61. row.value = [field objectForKey:@"titolo"];
  62. row.required = YES;
  63. [section addFormRow:row];
  64. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_web_account_data_", nil)];
  65. [form addFormSection:section];
  66. // url
  67. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"url" rowType:XLFormRowDescriptorTypeText title:NSLocalizedString(@"_url:_", nil)];
  68. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textField.textColor"];
  69. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  70. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  71. row.value = [field objectForKey:@"url"];
  72. [section addFormRow:row];
  73. // Login
  74. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"login" rowType:XLFormRowDescriptorTypeText title:NSLocalizedString(@"_login:_", nil)];
  75. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textField.textColor"];
  76. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  77. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  78. row.value = [field objectForKey:@"login"];
  79. [section addFormRow:row];
  80. // Password
  81. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"password" rowType:XLFormRowDescriptorTypeText title:NSLocalizedString(@"_password:_", nil)];
  82. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textField.textColor"];
  83. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  84. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textField.font"];
  85. row.value = [field objectForKey:@"password"];
  86. [section addFormRow:row];
  87. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_notes_", nil)];
  88. [form addFormSection:section];
  89. // Note
  90. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"note" rowType:XLFormRowDescriptorTypeTextView];
  91. row.value = [field objectForKey:@"note"];
  92. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textView.textColor"];
  93. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textView.font"];
  94. [section addFormRow:row];
  95. self.form = form;
  96. self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemCancel target:self action:@selector(cancelPressed:)];
  97. self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemSave target:self action:@selector(savePressed:)];
  98. }
  99. return self;
  100. }
  101. - (void)viewDidLoad
  102. {
  103. [super viewDidLoad];
  104. templates = [[CCTemplates alloc] init];
  105. [templates setImageTitle:NSLocalizedString(@"_web_account_", nil) conNavigationItem:self.navigationItem reachability:[app.reachability isReachable]];
  106. // Color
  107. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  108. }
  109. - (void)viewDidAppear:(BOOL)animated
  110. {
  111. [super viewDidAppear:animated];
  112. if (self.fileName && !field) [self performSelector:@selector(cancelPressed:) withObject:nil afterDelay:0.5];
  113. }
  114. - (void)didSelectFormRow:(XLFormRowDescriptor *)formRow
  115. {
  116. [super didSelectFormRow:formRow];
  117. if ([formRow.tag isEqual:@"mytag"]) {
  118. // do your thing for your row.
  119. }
  120. }
  121. #pragma --------------------------------------------------------------------------------------------
  122. #pragma mark - ==== IBAction ====
  123. #pragma --------------------------------------------------------------------------------------------
  124. - (IBAction)cancelPressed:(UIBarButtonItem * __unused)button
  125. {
  126. [self dismissViewControllerAnimated:YES completion:nil];
  127. }
  128. - (IBAction)savePressed:(UIBarButtonItem * __unused)button
  129. {
  130. NSString *fileNameModel;
  131. NSArray *validationErrors = [self formValidationErrors];
  132. if (validationErrors.count > 0){
  133. UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:NSLocalizedString(@"_error_", nil) message:NSLocalizedString(@"_enter_title_", nil) delegate:self cancelButtonTitle:NSLocalizedString(@"_ok_", nil) otherButtonTitles:nil];
  134. [alertView show];
  135. return;
  136. }
  137. [self.tableView endEditing:YES];
  138. fileNameModel = [templates salvaForm:form fileName:self.fileName uuid:self.uuid modello:@"accountweb" icona:@"accountweb"];
  139. if (fileNameModel) {
  140. XLFormRowDescriptor *titolo = [self.form formRowWithTag:@"titolo"];
  141. CCMetadataNet *metadataNet = [[CCMetadataNet alloc] initWithAccount:app.activeAccount];
  142. metadataNet.action = actionUploadTemplate;
  143. metadataNet.serverUrl = self.serverUrl;
  144. metadataNet.fileName = [CCUtility trasformedFileNamePlistInCrypto:fileNameModel];
  145. metadataNet.fileNamePrint = titolo.value;
  146. metadataNet.pathFolder = NSTemporaryDirectory();
  147. metadataNet.session = k_upload_session_foreground;
  148. metadataNet.taskStatus = k_taskStatusResume;
  149. [app addNetworkingOperationQueue:app.netQueue delegate:self metadataNet:metadataNet];
  150. }
  151. }
  152. - (void)uploadFileFailure:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector message:(NSString *)message errorCode:(NSInteger)errorCode
  153. {
  154. if (![_saveEtag isEqualToString:fileID]) {
  155. _saveEtag = fileID;
  156. [app messageNotification:@"_upload_file_" description:message visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:errorCode];
  157. // remove the file
  158. [[NCManageDatabase sharedInstance] deleteMetadataWithPredicate:[NSPredicate predicateWithFormat:@"fileID = %@", fileID] clearDateReadDirectoryID:nil];
  159. [self.delegate readFolder:self.serverUrl];
  160. }
  161. }
  162. - (void)uploadFileSuccess:(CCMetadataNet *)metadataNet fileID:(NSString *)fileID serverUrl:(NSString *)serverUrl selector:(NSString *)selector selectorPost:(NSString *)selectorPost
  163. {
  164. [self dismissViewControllerAnimated:YES completion:nil];
  165. }
  166. @end