CCManageAccount.m 12 KB

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