CCManageAccount.m 12 KB

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