CCManageAccount.m 12 KB

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