CCManageAccount.m 13 KB

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