CCManageAccount.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322
  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 "CCLoginNCOC.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. CCLoginNCOC *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)addAccountOwnCloud:(XLFormRowDescriptor *)sender
  114. {
  115. [self deselectFormRow:sender];
  116. [app cancelAllOperations];
  117. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  118. CCLoginNCOC *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginOwnCloud"];
  119. [loginVC setModifyOnlyPassword:NO];
  120. [loginVC setTypeCloud:typeCloudOwnCloud];
  121. [self presentViewController:loginVC animated:YES completion:nil];
  122. }
  123. - (void)openLoginSetupVC
  124. {
  125. // remove any message
  126. [[NSNotificationCenter defaultCenter] removeObserver:self];
  127. CCLogin *viewController = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLogin"];
  128. dispatch_async(dispatch_get_main_queue(), ^ {
  129. [self presentViewController:viewController animated:YES completion:nil];
  130. });
  131. }
  132. #pragma --------------------------------------------------------------------------------------------
  133. #pragma mark === Modify Account ===
  134. #pragma --------------------------------------------------------------------------------------------
  135. - (void)modifyAccount:(XLFormRowDescriptor *)sender
  136. {
  137. NSString *vcName;
  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. if ([app.typeCloud isEqualToString:typeCloudNextcloud] || [app.typeCloud isEqualToString:typeCloudOwnCloud]) {
  142. if ([app.typeCloud isEqualToString:typeCloudNextcloud])
  143. vcName = @"CCLoginNextcloud";
  144. if ([app.typeCloud isEqualToString:typeCloudOwnCloud])
  145. vcName = @"CCLoginOwnCloud";
  146. CCLoginNCOC *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:vcName];
  147. [loginVC setModifyOnlyPassword:YES];
  148. [loginVC setTypeCloud:app.typeCloud];
  149. [self presentViewController:loginVC animated:YES completion:nil];
  150. }
  151. [self UpdateForm];
  152. }
  153. #pragma --------------------------------------------------------------------------------------------
  154. #pragma mark === Delete Account ===
  155. #pragma --------------------------------------------------------------------------------------------
  156. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  157. {
  158. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  159. NSString *accountNow = pickerAccount.rowDescriptor.value;
  160. NSArray *listAccount = [CCCoreData getAllAccount];
  161. [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
  162. if (buttonIndex == 0 && actionSheet.tag == actionSheetCancellaAccount) {
  163. [app cancelAllOperations];
  164. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  165. [self deleteAccount:accountNow];
  166. // Clear active user
  167. [app settingActiveAccount:nil activeUrl:nil activeUser:nil activePassword:nil activeUID:nil activeAccessToken:nil typeCloud:nil];
  168. listAccount = [CCCoreData getAllAccount];
  169. if ([listAccount count] > 0) [self ChangeDefaultAccount:listAccount[0]];
  170. else {
  171. [self openLoginSetupVC];
  172. return;
  173. }
  174. }
  175. }
  176. - (void)deleteAccount:(NSString *)account
  177. {
  178. [CCCoreData deleteAccount:account];
  179. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", account];
  180. [CCCoreData deleteMetadataWithPredicate:predicate];
  181. [CCCoreData deleteLocalFileWithPredicate:predicate];
  182. [CCCoreData deleteDirectoryFromPredicate:predicate];
  183. }
  184. - (void)answerDelAccount:(XLFormRowDescriptor *)sender
  185. {
  186. [self deselectFormRow:sender];
  187. UIActionSheet *actionSheet1 = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"_want_delete_",nil)
  188. delegate:self
  189. cancelButtonTitle:NSLocalizedString(@"_no_delete_",nil)
  190. destructiveButtonTitle:NSLocalizedString(@"_yes_delete_",nil)
  191. otherButtonTitles:nil];
  192. actionSheet1.tag = actionSheetCancellaAccount;
  193. [actionSheet1 showInView:self.view.window.rootViewController.view];
  194. }
  195. #pragma --------------------------------------------------------------------------------------------
  196. #pragma mark === Change Default Account ===
  197. #pragma --------------------------------------------------------------------------------------------
  198. - (void)ChangeDefaultAccount:(NSString *)account
  199. {
  200. 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) {
  201. [app messageNotification:@"_transfers_in_queue_" description:nil visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeInfo];
  202. [self UpdateForm];
  203. return;
  204. }
  205. // removed this -> ?????
  206. [app cancelAllOperations];
  207. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  208. // removed this -> ?????
  209. // change account
  210. TableAccount *tableAccount = [CCCoreData setActiveAccount:account];
  211. if (tableAccount)
  212. [app settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activePassword:tableAccount.password activeUID:tableAccount.uid activeAccessToken:tableAccount.token typeCloud:tableAccount.typeCloud];
  213. // Init home
  214. [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
  215. [self UpdateForm];
  216. }
  217. #pragma --------------------------------------------------------------------------------------------
  218. #pragma mark === Update Form ===
  219. #pragma --------------------------------------------------------------------------------------------
  220. - (void)UpdateForm
  221. {
  222. NSArray *listAccount = [CCCoreData getAllAccount];
  223. if (listAccount == nil) {
  224. [self openLoginSetupVC];
  225. return;
  226. }
  227. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  228. pickerAccount.rowDescriptor.selectorOptions = listAccount;
  229. pickerAccount.rowDescriptor.value = app.activeAccount;
  230. [self.tableView reloadData];
  231. [self performSelector:@selector(reloadData) withObject:nil afterDelay:1];
  232. }
  233. - (void)reloadData
  234. {
  235. [self.tableView reloadData];
  236. }
  237. @end