CCManageAccount.m 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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:[UIColor blackColor] 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:[UIColor blackColor] 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. self.tableView.backgroundColor = COLOR_TABLE_BACKGROUND;
  89. // Color
  90. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  91. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  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. #ifdef LOGIN_WEB
  122. _loginWeb = [CCLoginWeb new];
  123. _loginWeb.delegate = self;
  124. _loginWeb.loginType = loginAdd;
  125. [_loginWeb presentModalWithDefaultTheme:self];
  126. #else
  127. _loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  128. _loginVC.delegate = self;
  129. _loginVC.loginType = loginAdd;
  130. [self presentViewController:_loginVC animated:YES completion:nil];
  131. #endif
  132. }
  133. - (void)addAccountFoced
  134. {
  135. #ifdef LOGIN_WEB
  136. _loginWeb = [CCLoginWeb new];
  137. _loginWeb.delegate = self;
  138. _loginWeb.loginType = loginAddForced;
  139. dispatch_async(dispatch_get_main_queue(), ^ {
  140. [_loginWeb presentModalWithDefaultTheme:self];
  141. });
  142. #else
  143. _loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  144. _loginVC.delegate = self;
  145. _loginVC.loginType = loginAddForced;
  146. dispatch_async(dispatch_get_main_queue(), ^ {
  147. [self presentViewController:_loginVC animated:YES completion:nil];
  148. });
  149. #endif
  150. }
  151. #pragma --------------------------------------------------------------------------------------------
  152. #pragma mark === Modify Account ===
  153. #pragma --------------------------------------------------------------------------------------------
  154. - (void)changePassword:(XLFormRowDescriptor *)sender
  155. {
  156. [self deselectFormRow:sender];
  157. [app cancelAllOperations];
  158. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  159. #ifdef LOGIN_WEB
  160. _loginWeb = [CCLoginWeb new];
  161. _loginWeb.delegate = self;
  162. _loginWeb.loginType = loginModifyPasswordUser;
  163. dispatch_async(dispatch_get_main_queue(), ^ {
  164. [_loginWeb presentModalWithDefaultTheme:self];
  165. });
  166. #else
  167. _loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  168. _loginVC.delegate = self;
  169. _loginVC.loginType = loginModifyPasswordUser;
  170. dispatch_async(dispatch_get_main_queue(), ^ {
  171. [self presentViewController:_loginVC animated:YES completion:nil];
  172. });
  173. #endif
  174. [self UpdateForm];
  175. }
  176. #pragma --------------------------------------------------------------------------------------------
  177. #pragma mark === Delete Account ===
  178. #pragma --------------------------------------------------------------------------------------------
  179. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  180. {
  181. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  182. NSString *accountNow = pickerAccount.rowDescriptor.value;
  183. NSArray *listAccount = [CCCoreData getAllAccount];
  184. [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
  185. if (buttonIndex == 0 && actionSheet.tag == actionSheetCancellaAccount) {
  186. [app cancelAllOperations];
  187. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  188. [self deleteAccount:accountNow];
  189. // Clear active user
  190. [app settingActiveAccount:nil activeUrl:nil activeUser:nil activePassword:nil];
  191. listAccount = [CCCoreData getAllAccount];
  192. if ([listAccount count] > 0) [self ChangeDefaultAccount:listAccount[0]];
  193. else {
  194. [self addAccountFoced];
  195. return;
  196. }
  197. }
  198. }
  199. - (void)deleteAccount:(NSString *)account
  200. {
  201. [CCCoreData deleteAccount:account];
  202. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", account];
  203. [CCCoreData deleteMetadataWithPredicate:predicate];
  204. [CCCoreData deleteLocalFileWithPredicate:predicate];
  205. [CCCoreData deleteDirectoryFromPredicate:predicate];
  206. }
  207. - (void)answerDelAccount:(XLFormRowDescriptor *)sender
  208. {
  209. [self deselectFormRow:sender];
  210. UIActionSheet *actionSheet1 = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"_want_delete_",nil)
  211. delegate:self
  212. cancelButtonTitle:NSLocalizedString(@"_no_delete_",nil)
  213. destructiveButtonTitle:NSLocalizedString(@"_yes_delete_",nil)
  214. otherButtonTitles:nil];
  215. actionSheet1.tag = actionSheetCancellaAccount;
  216. [actionSheet1 showInView:self.view.window.rootViewController.view];
  217. }
  218. #pragma --------------------------------------------------------------------------------------------
  219. #pragma mark === Change Default Account ===
  220. #pragma --------------------------------------------------------------------------------------------
  221. - (void)ChangeDefaultAccount:(NSString *)account
  222. {
  223. 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) {
  224. [app messageNotification:@"_transfers_in_queue_" description:nil visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo];
  225. [self UpdateForm];
  226. return;
  227. }
  228. // removed this -> ?????
  229. [app cancelAllOperations];
  230. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  231. // removed this -> ?????
  232. // change account
  233. TableAccount *tableAccount = [CCCoreData setActiveAccount:account];
  234. if (tableAccount)
  235. [app settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activePassword:tableAccount.password];
  236. // Init home
  237. [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
  238. [self UpdateForm];
  239. }
  240. #pragma --------------------------------------------------------------------------------------------
  241. #pragma mark === Update Form ===
  242. #pragma --------------------------------------------------------------------------------------------
  243. - (void)UpdateForm
  244. {
  245. NSArray *listAccount = [CCCoreData getAllAccount];
  246. if (listAccount == nil) {
  247. [self addAccountFoced];
  248. return;
  249. }
  250. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  251. pickerAccount.rowDescriptor.selectorOptions = listAccount;
  252. pickerAccount.rowDescriptor.value = app.activeAccount;
  253. [self.tableView reloadData];
  254. [self performSelector:@selector(reloadData) withObject:nil afterDelay:1];
  255. }
  256. - (void)reloadData
  257. {
  258. [self.tableView reloadData];
  259. }
  260. @end