CCManageAccount.m 13 KB

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