CCManageAccount.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. //
  2. // CCManageAccount.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 12/03/15.
  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 "CCManageAccount.h"
  24. #import "AppDelegate.h"
  25. #import "CCLogin.h"
  26. #define actionSheetCancellaAccount 1
  27. @implementation CCManageAccount
  28. - (id)initWithCoder:(NSCoder *)aDecoder
  29. {
  30. if (self = [super initWithCoder:aDecoder]) {
  31. [self initializeForm];
  32. }
  33. return self;
  34. }
  35. - (void)initializeForm
  36. {
  37. XLFormDescriptor *form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_credentials_", nil)];
  38. XLFormSectionDescriptor *section;
  39. XLFormRowDescriptor *row;
  40. NSArray *listAccount = [CCCoreData getAllAccount];
  41. section = [XLFormSectionDescriptor formSectionWithTitle:@"cloud account"];
  42. [form addFormSection:section];
  43. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  44. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"pickerAccount" rowType:XLFormRowDescriptorTypePicker];
  45. row.selectorOptions = listAccount;
  46. row.value = app.activeAccount;
  47. [section addFormRow:row];
  48. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_manage_account_", nil)];
  49. [form addFormSection:section];
  50. // Modify Account
  51. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"modifyAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_modify_account_", nil)];
  52. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  53. [row.cellConfig setObject:[UIImage imageNamed:image_settingsAccountModify] forKey:@"imageView.image"];
  54. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  55. [row.cellConfig setObject:COLOR_BRAND forKey:@"textLabel.textColor"];
  56. row.action.formSelector = @selector(modifyAccount:);
  57. if (listAccount.count == 0) row.disabled = @YES;
  58. [section addFormRow:row];
  59. // New Account nextcloud
  60. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"addAccountNextcloud" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_nextcloud_", nil)];
  61. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  62. [row.cellConfig setObject:[UIImage imageNamed:image_settingsAccountNextcloud] forKey:@"imageView.image"];
  63. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  64. [row.cellConfig setObject:COLOR_BRAND forKey:@"textLabel.textColor"];
  65. row.action.formSelector = @selector(addAccount:);
  66. [section addFormRow:row];
  67. // delete Account
  68. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"delAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_delete_account_", nil)];
  69. if (listAccount.count > 0) [row.cellConfig setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  70. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  71. [row.cellConfig setObject:[UIImage imageNamed:image_settingsAccountDelete] forKey:@"imageView.image"];
  72. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  73. row.action.formSelector = @selector(answerDelAccount:);
  74. if (listAccount.count == 0) row.disabled = @YES;
  75. [section addFormRow:row];
  76. self.form = form;
  77. }
  78. // Apparirà
  79. - (void)viewWillAppear:(BOOL)animated
  80. {
  81. [super viewWillAppear:animated];
  82. // Color
  83. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  84. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  85. }
  86. // E' apparsa
  87. - (void)viewDidAppear:(BOOL)animated
  88. {
  89. [super viewDidAppear:animated];
  90. [self UpdateForm];
  91. }
  92. #pragma --------------------------------------------------------------------------------------------
  93. #pragma mark === Delegate ===
  94. #pragma --------------------------------------------------------------------------------------------
  95. -(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  96. {
  97. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  98. if ([rowDescriptor.tag isEqualToString:@"pickerAccount"]){
  99. // cambiamo default account se oldvalue != newValue
  100. if (![newValue isEqualToString:oldValue]) [self ChangeDefaultAccount:newValue];
  101. }
  102. }
  103. #pragma --------------------------------------------------------------------------------------------
  104. #pragma mark === Delegate Login ===
  105. #pragma --------------------------------------------------------------------------------------------
  106. - (void)loginSuccess:(NSInteger)loginType
  107. {
  108. if (loginType == loginAddForced)
  109. [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
  110. }
  111. #pragma --------------------------------------------------------------------------------------------
  112. #pragma mark === Add Account ===
  113. #pragma --------------------------------------------------------------------------------------------
  114. - (void)addAccount:(XLFormRowDescriptor *)sender
  115. {
  116. [self deselectFormRow:sender];
  117. [app cancelAllOperations];
  118. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  119. CCLogin *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  120. loginVC.delegate = self;
  121. loginVC.loginType = loginAdd;
  122. [self presentViewController:loginVC animated:YES completion:nil];
  123. }
  124. - (void)addAccountFoced
  125. {
  126. CCLogin *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  127. loginVC.delegate = self;
  128. loginVC.loginType = loginAddForced;
  129. dispatch_async(dispatch_get_main_queue(), ^ {
  130. [self presentViewController:loginVC animated:YES completion:nil];
  131. });
  132. }
  133. #pragma --------------------------------------------------------------------------------------------
  134. #pragma mark === Modify Account ===
  135. #pragma --------------------------------------------------------------------------------------------
  136. - (void)modifyAccount:(XLFormRowDescriptor *)sender
  137. {
  138. [self deselectFormRow:sender];
  139. [app cancelAllOperations];
  140. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  141. CCLogin *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier: @"CCLoginNextcloud"];
  142. loginVC.delegate = self;
  143. loginVC.loginType = loginModifyPasswordUser;
  144. [self presentViewController:loginVC animated:YES completion:nil];
  145. [self UpdateForm];
  146. }
  147. #pragma --------------------------------------------------------------------------------------------
  148. #pragma mark === Delete Account ===
  149. #pragma --------------------------------------------------------------------------------------------
  150. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  151. {
  152. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  153. NSString *accountNow = pickerAccount.rowDescriptor.value;
  154. NSArray *listAccount = [CCCoreData getAllAccount];
  155. [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
  156. if (buttonIndex == 0 && actionSheet.tag == actionSheetCancellaAccount) {
  157. [app cancelAllOperations];
  158. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  159. [self deleteAccount:accountNow];
  160. // Clear active user
  161. [app settingActiveAccount:nil activeUrl:nil activeUser:nil activePassword:nil];
  162. listAccount = [CCCoreData getAllAccount];
  163. if ([listAccount count] > 0) [self ChangeDefaultAccount:listAccount[0]];
  164. else {
  165. [self addAccountFoced];
  166. return;
  167. }
  168. }
  169. }
  170. - (void)deleteAccount:(NSString *)account
  171. {
  172. [CCCoreData deleteAccount:account];
  173. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", account];
  174. [CCCoreData deleteMetadataWithPredicate:predicate];
  175. [CCCoreData deleteLocalFileWithPredicate:predicate];
  176. [CCCoreData deleteDirectoryFromPredicate:predicate];
  177. }
  178. - (void)answerDelAccount:(XLFormRowDescriptor *)sender
  179. {
  180. [self deselectFormRow:sender];
  181. UIActionSheet *actionSheet1 = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"_want_delete_",nil)
  182. delegate:self
  183. cancelButtonTitle:NSLocalizedString(@"_no_delete_",nil)
  184. destructiveButtonTitle:NSLocalizedString(@"_yes_delete_",nil)
  185. otherButtonTitles:nil];
  186. actionSheet1.tag = actionSheetCancellaAccount;
  187. [actionSheet1 showInView:self.view.window.rootViewController.view];
  188. }
  189. #pragma --------------------------------------------------------------------------------------------
  190. #pragma mark === Change Default Account ===
  191. #pragma --------------------------------------------------------------------------------------------
  192. - (void)ChangeDefaultAccount:(NSString *)account
  193. {
  194. if ([app.netQueue operationCount] > 0 || [app.netQueueDownload operationCount] > 0 || [app.netQueueDownloadWWan operationCount] > 0 || [app.netQueueUpload operationCount] > 0 || [app.netQueueUploadWWan operationCount] > 0 || [CCCoreData countTableAutomaticUploadForAccount:app.activeAccount selector:nil] > 0) {
  195. [app messageNotification:@"_transfers_in_queue_" description:nil visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
  196. [self UpdateForm];
  197. return;
  198. }
  199. // removed this -> ?????
  200. [app cancelAllOperations];
  201. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  202. // removed this -> ?????
  203. // change account
  204. TableAccount *tableAccount = [CCCoreData setActiveAccount:account];
  205. if (tableAccount)
  206. [app settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activePassword:tableAccount.password];
  207. // Init home
  208. [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
  209. [self UpdateForm];
  210. }
  211. #pragma --------------------------------------------------------------------------------------------
  212. #pragma mark === Update Form ===
  213. #pragma --------------------------------------------------------------------------------------------
  214. - (void)UpdateForm
  215. {
  216. NSArray *listAccount = [CCCoreData getAllAccount];
  217. if (listAccount == nil) {
  218. [self addAccountFoced];
  219. return;
  220. }
  221. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  222. pickerAccount.rowDescriptor.selectorOptions = listAccount;
  223. pickerAccount.rowDescriptor.value = app.activeAccount;
  224. [self.tableView reloadData];
  225. [self performSelector:@selector(reloadData) withObject:nil afterDelay:1];
  226. }
  227. - (void)reloadData
  228. {
  229. [self.tableView reloadData];
  230. }
  231. @end