CCManageAccount.m 13 KB

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