CCManageAccount.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  1. //
  2. // CCManageAccount.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 12/03/15.
  6. // Copyright (c) 2017 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 "JDStatusBarNotification.h"
  26. #import "CCLogin.h"
  27. #import "NCAutoUpload.h"
  28. #import "NCBridgeSwift.h"
  29. #define actionSheetCancellaAccount 1
  30. @interface CCManageAccount () <CCLoginDelegate, CCLoginDelegateWeb>
  31. {
  32. AppDelegate *appDelegate;
  33. tableAccount *_tableAccount;
  34. }
  35. @end
  36. @implementation CCManageAccount
  37. -(id)init
  38. {
  39. XLFormDescriptor *form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_credentials_", nil)];
  40. XLFormSectionDescriptor *section;
  41. XLFormRowDescriptor *row;
  42. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  43. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  44. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  45. // Section : CLOUD ACCOUNT -------------------------------------------
  46. section = [XLFormSectionDescriptor formSectionWithTitle:@"cloud account"];
  47. [form addFormSection:section];
  48. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  49. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"pickerAccount" rowType:XLFormRowDescriptorTypePicker];
  50. row.height = 100;
  51. row.selectorOptions = listAccount;
  52. row.value = appDelegate.activeAccount;
  53. [section addFormRow:row];
  54. // Section : USER INFORMATION -------------------------------------------
  55. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_personal_information_", nil)];
  56. [form addFormSection:section];
  57. // Full Name
  58. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userfullname" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_full_name_", nil)];
  59. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  60. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  61. [section addFormRow:row];
  62. // Address
  63. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"useraddress" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_address_", nil)];
  64. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  65. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  66. [section addFormRow:row];
  67. // Phone
  68. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userphone" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_phone_", nil)];
  69. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  70. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  71. [section addFormRow:row];
  72. // Email
  73. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"useremail" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_email_", nil)];
  74. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  75. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  76. [section addFormRow:row];
  77. // Web
  78. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userweb" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_web_", nil)];
  79. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  80. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  81. [section addFormRow:row];
  82. // Twitter
  83. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"usertwitter" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_twitter_", nil)];
  84. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  85. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  86. [section addFormRow:row];
  87. // Section : MANAGE ACCOUNT -------------------------------------------
  88. if ([NCBrandOptions sharedInstance].disable_manage_account == NO) {
  89. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_manage_account_", nil)];
  90. [form addFormSection:section];
  91. // Modify Account
  92. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"changePassword" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_change_password_", nil)];
  93. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  94. [row.cellConfig setObject:[UIImage imageNamed:@"settingsAccountModify"] forKey:@"imageView.image"];
  95. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  96. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  97. row.action.formSelector = @selector(changePassword:);
  98. if (listAccount.count == 0) row.disabled = @YES;
  99. [section addFormRow:row];
  100. // Brand
  101. if ([NCBrandOptions sharedInstance].disable_multiaccount == NO) {
  102. // New Account nextcloud
  103. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"addAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_account_", nil)];
  104. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  105. [row.cellConfig setObject:[UIImage imageNamed:@"settingsAccountNextcloud"] forKey:@"imageView.image"];
  106. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  107. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  108. row.action.formSelector = @selector(addAccount:);
  109. [section addFormRow:row];
  110. }
  111. // delete Account
  112. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"delAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_delete_account_", nil)];
  113. if (listAccount.count > 0)
  114. [row.cellConfig setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  115. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  116. [row.cellConfig setObject:[UIImage imageNamed:@"settingsAccountDelete"] forKey:@"imageView.image"];
  117. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  118. row.action.formSelector = @selector(answerDelAccount:);
  119. if (listAccount.count == 0) row.disabled = @YES;
  120. [section addFormRow:row];
  121. }
  122. return [super initWithForm:form];
  123. }
  124. // Apparirà
  125. - (void)viewWillAppear:(BOOL)animated
  126. {
  127. [super viewWillAppear:animated];
  128. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  129. self.tableView.showsVerticalScrollIndicator = NO;
  130. // Color
  131. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  132. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  133. [self UpdateForm];
  134. }
  135. - (void)changeTheming
  136. {
  137. [appDelegate changeTheming:self];
  138. }
  139. #pragma --------------------------------------------------------------------------------------------
  140. #pragma mark === Delegate ===
  141. #pragma --------------------------------------------------------------------------------------------
  142. -(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  143. {
  144. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  145. if ([rowDescriptor.tag isEqualToString:@"pickerAccount"] && oldValue && newValue) {
  146. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  147. NSString *accountNow = pickerAccount.rowDescriptor.value;
  148. if (![newValue isEqualToString:oldValue] && ![newValue isEqualToString:@""] && ![newValue isEqualToString:appDelegate.activeAccount])
  149. [self ChangeDefaultAccount:newValue];
  150. if ([newValue isEqualToString:@""]) {
  151. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  152. if ([listAccount count] > 0)
  153. [self ChangeDefaultAccount:listAccount[0]];
  154. }
  155. }
  156. }
  157. #pragma --------------------------------------------------------------------------------------------
  158. #pragma mark === Delegate Login ===
  159. #pragma --------------------------------------------------------------------------------------------
  160. - (void)loginSuccess:(NSInteger)loginType
  161. {
  162. if (loginType == loginAddForced) {
  163. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil];
  164. }
  165. }
  166. - (void)loginClose
  167. {
  168. appDelegate.activeLogin = nil;
  169. }
  170. - (void)loginWebClose
  171. {
  172. appDelegate.activeLoginWeb = nil;
  173. }
  174. #pragma --------------------------------------------------------------------------------------------
  175. #pragma mark === Add Account ===
  176. #pragma --------------------------------------------------------------------------------------------
  177. - (void)addAccount:(XLFormRowDescriptor *)sender
  178. {
  179. [self deselectFormRow:sender];
  180. // Verify session in progress
  181. if ([[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND sessionTaskIdentifier > 0", appDelegate.activeAccount] sorted:nil ascending:NO] count] > 0) {
  182. [JDStatusBarNotification showWithStatus:NSLocalizedString(@"_transfers_in_queue_", nil) dismissAfter:k_dismissAfterSecond styleName:JDStatusBarStyleDefault];
  183. return;
  184. }
  185. [appDelegate.netQueue cancelAllOperations];
  186. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  187. [appDelegate openLoginView:self loginType:loginAdd];
  188. });
  189. }
  190. - (void)addAccountForced
  191. {
  192. [appDelegate openLoginView:self loginType:loginAddForced];
  193. }
  194. #pragma --------------------------------------------------------------------------------------------
  195. #pragma mark === Modify Account ===
  196. #pragma --------------------------------------------------------------------------------------------
  197. - (void)changePassword:(XLFormRowDescriptor *)sender
  198. {
  199. [self deselectFormRow:sender];
  200. // Verify session in progress
  201. if ([[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND sessionTaskIdentifier > 0", appDelegate.activeAccount] sorted:nil ascending:NO] count] > 0) {
  202. [JDStatusBarNotification showWithStatus:NSLocalizedString(@"_transfers_in_queue_", nil) dismissAfter:k_dismissAfterSecond styleName:JDStatusBarStyleDefault];
  203. return;
  204. }
  205. [appDelegate.netQueue cancelAllOperations];
  206. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  207. [appDelegate openLoginView:self loginType:loginModifyPasswordUser];
  208. });
  209. }
  210. #pragma --------------------------------------------------------------------------------------------
  211. #pragma mark === Delete Account ===
  212. #pragma --------------------------------------------------------------------------------------------
  213. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  214. {
  215. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  216. [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
  217. if (buttonIndex == 0 && actionSheet.tag == actionSheetCancellaAccount) {
  218. NSString *accountNow = pickerAccount.rowDescriptor.value;
  219. [self deleteAccount:accountNow];
  220. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  221. if ([listAccount count] > 0)
  222. [self ChangeDefaultAccount:listAccount[0]];
  223. else {
  224. [self addAccountForced];
  225. }
  226. }
  227. }
  228. - (void)deleteAccount:(NSString *)account
  229. {
  230. // Verify session in progress
  231. if ([[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND sessionTaskIdentifier > 0", appDelegate.activeAccount] sorted:nil ascending:NO] count] > 0) {
  232. [JDStatusBarNotification showWithStatus:NSLocalizedString(@"_transfers_in_queue_", nil) dismissAfter:k_dismissAfterSecond styleName:JDStatusBarStyleDefault];
  233. return;
  234. }
  235. [appDelegate.netQueue cancelAllOperations];
  236. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  237. [[NCManageDatabase sharedInstance] clearTable:[tableAccount class] account:account];
  238. [[NCManageDatabase sharedInstance] clearTable:[tableActivity class] account:account];
  239. [[NCManageDatabase sharedInstance] clearTable:[tableCapabilities class] account:account];
  240. [[NCManageDatabase sharedInstance] clearTable:[tableDirectory class] account:account];
  241. [[NCManageDatabase sharedInstance] clearTable:[tableE2eEncryption class] account:account];
  242. [[NCManageDatabase sharedInstance] clearTable:[tableExternalSites class] account:account];
  243. [[NCManageDatabase sharedInstance] clearTable:[tableLocalFile class] account:account];
  244. [[NCManageDatabase sharedInstance] clearTable:[tableMetadata class] account:account];
  245. [[NCManageDatabase sharedInstance] clearTable:[tablePhotoLibrary class] account:account];
  246. [[NCManageDatabase sharedInstance] clearTable:[tableQueueDownload class] account:account];
  247. [[NCManageDatabase sharedInstance] clearTable:[tableQueueUpload class] account:account];
  248. [[NCManageDatabase sharedInstance] clearTable:[tableShare class] account:account];
  249. // Clear active user
  250. [appDelegate settingActiveAccount:nil activeUrl:nil activeUser:nil activeUserID:nil activePassword:nil];
  251. });
  252. }
  253. - (void)answerDelAccount:(XLFormRowDescriptor *)sender
  254. {
  255. [self deselectFormRow:sender];
  256. UIActionSheet *actionSheet1 = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"_want_delete_",nil)
  257. delegate:self
  258. cancelButtonTitle:NSLocalizedString(@"_no_delete_",nil)
  259. destructiveButtonTitle:NSLocalizedString(@"_yes_delete_",nil)
  260. otherButtonTitles:nil];
  261. actionSheet1.tag = actionSheetCancellaAccount;
  262. [actionSheet1 showInView:self.view.window.rootViewController.view];
  263. }
  264. #pragma --------------------------------------------------------------------------------------------
  265. #pragma mark === Change Default Account ===
  266. #pragma --------------------------------------------------------------------------------------------
  267. - (void)ChangeDefaultAccount:(NSString *)account
  268. {
  269. // Verify session in progress
  270. if ([[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account = %@ AND sessionTaskIdentifier > 0", appDelegate.activeAccount] sorted:nil ascending:NO] count] > 0) {
  271. [JDStatusBarNotification showWithStatus:NSLocalizedString(@"_transfers_in_queue_", nil) dismissAfter:k_dismissAfterSecond styleName:JDStatusBarStyleDefault];
  272. return;
  273. }
  274. [appDelegate.netQueue cancelAllOperations];
  275. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  276. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  277. if (tableAccount) {
  278. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:tableAccount.password];
  279. // Init home
  280. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil];
  281. [self UpdateForm];
  282. }
  283. });
  284. }
  285. #pragma --------------------------------------------------------------------------------------------
  286. #pragma mark === Update Form ===
  287. #pragma --------------------------------------------------------------------------------------------
  288. - (void)UpdateForm
  289. {
  290. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  291. if (listAccount == nil) {
  292. [self addAccountForced];
  293. return;
  294. }
  295. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  296. pickerAccount.rowDescriptor.selectorOptions = listAccount;
  297. pickerAccount.rowDescriptor.value = appDelegate.activeAccount;
  298. UIImage *avatar = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/avatar.png", appDelegate.directoryUser]];
  299. if (avatar) {
  300. avatar = [CCGraphics scaleImage:avatar toSize:CGSizeMake(40, 40) isAspectRation:YES];
  301. CCAvatar *avatarImageView = [[CCAvatar alloc] initWithImage:avatar borderColor:[UIColor lightGrayColor] borderWidth:0.5];
  302. CGSize imageSize = avatarImageView.bounds.size;
  303. UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
  304. CGContextRef context = UIGraphicsGetCurrentContext();
  305. [avatarImageView.layer renderInContext:context];
  306. avatar = UIGraphicsGetImageFromCurrentImageContext();
  307. UIGraphicsEndImageContext();
  308. } else {
  309. avatar = [UIImage imageNamed:@"avatarBN"];
  310. }
  311. [pickerAccount.rowDescriptor.cellConfig setObject:avatar forKey:@"imageView.image"];
  312. // --
  313. _tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  314. XLFormRowDescriptor *rowUserFullName = [self.form formRowWithTag:@"userfullname"];
  315. XLFormRowDescriptor *rowUserAddress = [self.form formRowWithTag:@"useraddress"];
  316. XLFormRowDescriptor *rowUserPhone = [self.form formRowWithTag:@"userphone"];
  317. XLFormRowDescriptor *rowUserEmail = [self.form formRowWithTag:@"useremail"];
  318. XLFormRowDescriptor *rowUserWeb = [self.form formRowWithTag:@"userweb"];
  319. XLFormRowDescriptor *rowUserTwitter = [self.form formRowWithTag:@"usertwitter"];
  320. rowUserFullName.value = _tableAccount.displayName;
  321. if ([_tableAccount.displayName isEqualToString:@""] || _tableAccount.displayName == nil) rowUserFullName.hidden = @YES;
  322. else rowUserFullName.hidden = @NO;
  323. rowUserAddress.value = _tableAccount.address;
  324. if ([_tableAccount.address isEqualToString:@""] || _tableAccount.address == nil) rowUserAddress.hidden = @YES;
  325. else rowUserAddress.hidden = @NO;
  326. rowUserPhone.value = _tableAccount.phone;
  327. if ([_tableAccount.phone isEqualToString:@""] || _tableAccount.phone == nil) rowUserPhone.hidden = @YES;
  328. else rowUserPhone.hidden = @NO;
  329. rowUserEmail.value = _tableAccount.email;
  330. if ([_tableAccount.email isEqualToString:@""] || _tableAccount.email == nil) rowUserEmail.hidden = @YES;
  331. else rowUserEmail.hidden = @NO;
  332. rowUserWeb.value = _tableAccount.webpage;
  333. if ([_tableAccount.webpage isEqualToString:@""] || _tableAccount.webpage == nil) rowUserWeb.hidden = @YES;
  334. else rowUserWeb.hidden = @NO;
  335. rowUserTwitter.value = _tableAccount.twitter;
  336. if ([_tableAccount.twitter isEqualToString:@""] || _tableAccount.twitter == nil) rowUserTwitter.hidden = @YES;
  337. else rowUserTwitter.hidden = @NO;
  338. [self.tableView reloadData];
  339. [self performSelector:@selector(reloadData) withObject:nil afterDelay:1];
  340. }
  341. - (void)reloadData
  342. {
  343. [self.tableView reloadData];
  344. }
  345. @end