CCManageAccount.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. //
  2. // CCManageAccount.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 12/03/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 <JDStatusBarNotification/JDStatusBarNotification.h>
  24. #import "CCManageAccount.h"
  25. #import "AppDelegate.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. if (listAccount.count > 0) {
  52. row.selectorOptions = listAccount;
  53. row.value = appDelegate.activeAccount;
  54. } else {
  55. row.selectorOptions = [[NSArray alloc] initWithObjects:@"", nil];
  56. }
  57. [section addFormRow:row];
  58. // Section : USER INFORMATION -------------------------------------------
  59. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_personal_information_", nil)];
  60. [form addFormSection:section];
  61. // Full Name
  62. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userfullname" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_full_name_", nil)];
  63. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  64. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  65. [section addFormRow:row];
  66. // Address
  67. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"useraddress" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_address_", nil)];
  68. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  69. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  70. [section addFormRow:row];
  71. // Phone
  72. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userphone" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_phone_", nil)];
  73. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  74. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  75. [section addFormRow:row];
  76. // Email
  77. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"useremail" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_email_", nil)];
  78. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  79. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  80. [section addFormRow:row];
  81. // Web
  82. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userweb" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_web_", nil)];
  83. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  84. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  85. [section addFormRow:row];
  86. // Twitter
  87. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"usertwitter" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_twitter_", nil)];
  88. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  89. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  90. [section addFormRow:row];
  91. // Section : MANAGE ACCOUNT -------------------------------------------
  92. if ([NCBrandOptions sharedInstance].disable_manage_account == NO) {
  93. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_manage_account_", nil)];
  94. [form addFormSection:section];
  95. // Modify Account
  96. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"changePassword" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_change_password_", nil)];
  97. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  98. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"rename"] multiplier:2 color:[NCBrandColor sharedInstance].icon] forKey:@"imageView.image"];
  99. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  100. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  101. row.action.formSelector = @selector(changePassword:);
  102. if (listAccount.count == 0) row.disabled = @YES;
  103. [section addFormRow:row];
  104. // Brand
  105. if ([NCBrandOptions sharedInstance].disable_multiaccount == NO) {
  106. // New Account nextcloud
  107. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"addAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_account_", nil)];
  108. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  109. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"add"] multiplier:2 color:[NCBrandColor sharedInstance].icon] forKey:@"imageView.image"];
  110. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  111. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  112. row.action.formSelector = @selector(addAccount:);
  113. [section addFormRow:row];
  114. }
  115. // delete Account
  116. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"delAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_delete_account_", nil)];
  117. [row.cellConfig setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  118. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  119. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor redColor]] forKey:@"imageView.image"];
  120. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  121. row.action.formSelector = @selector(answerDelAccount:);
  122. if (listAccount.count == 0) row.disabled = @YES;
  123. [section addFormRow:row];
  124. }
  125. return [super initWithForm:form];
  126. }
  127. // Apparirà
  128. - (void)viewWillAppear:(BOOL)animated
  129. {
  130. [super viewWillAppear:animated];
  131. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  132. self.tableView.showsVerticalScrollIndicator = NO;
  133. // Color
  134. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  135. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  136. [self UpdateForm];
  137. }
  138. - (void)changeTheming
  139. {
  140. [appDelegate changeTheming:self];
  141. }
  142. #pragma --------------------------------------------------------------------------------------------
  143. #pragma mark === Delegate ===
  144. #pragma --------------------------------------------------------------------------------------------
  145. -(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  146. {
  147. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  148. if ([rowDescriptor.tag isEqualToString:@"pickerAccount"] && oldValue && newValue) {
  149. if (![newValue isEqualToString:oldValue] && ![newValue isEqualToString:@""] && ![newValue isEqualToString:appDelegate.activeAccount])
  150. [self ChangeDefaultAccount:newValue];
  151. if ([newValue isEqualToString:@""]) {
  152. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  153. if ([listAccount count] > 0)
  154. [self ChangeDefaultAccount:listAccount[0]];
  155. }
  156. }
  157. }
  158. #pragma --------------------------------------------------------------------------------------------
  159. #pragma mark === Delegate Login ===
  160. #pragma --------------------------------------------------------------------------------------------
  161. - (void)loginSuccess:(NSInteger)loginType
  162. {
  163. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil userInfo:nil];
  164. [appDelegate subscribingNextcloudServerPushNotification];
  165. }
  166. #pragma --------------------------------------------------------------------------------------------
  167. #pragma mark === Add Account ===
  168. #pragma --------------------------------------------------------------------------------------------
  169. - (void)addAccount:(XLFormRowDescriptor *)sender
  170. {
  171. [self deselectFormRow:sender];
  172. [appDelegate openLoginView:self delegate:self loginType:k_login_Add selector:k_intro_login];
  173. }
  174. #pragma --------------------------------------------------------------------------------------------
  175. #pragma mark === Modify Account ===
  176. #pragma --------------------------------------------------------------------------------------------
  177. - (void)changePassword:(XLFormRowDescriptor *)sender
  178. {
  179. [self deselectFormRow:sender];
  180. [appDelegate openLoginView:self delegate:self loginType:k_login_Modify_Password selector:k_intro_login];
  181. }
  182. #pragma --------------------------------------------------------------------------------------------
  183. #pragma mark === Delete Account ===
  184. #pragma --------------------------------------------------------------------------------------------
  185. - (void)deleteAccount:(NSString *)account
  186. {
  187. [appDelegate unsubscribingNextcloudServerPushNotification];
  188. [[NCManageDatabase sharedInstance] clearTable:[tableAccount class] account:account];
  189. [[NCManageDatabase sharedInstance] clearTable:[tableActivity class] account:account];
  190. [[NCManageDatabase sharedInstance] clearTable:[tableActivitySubjectRich class] account:account];
  191. [[NCManageDatabase sharedInstance] clearTable:[tableCapabilities class] account:account];
  192. [[NCManageDatabase sharedInstance] clearTable:[tableDirectory class] account:account];
  193. [[NCManageDatabase sharedInstance] clearTable:[tableE2eEncryption class] account:account];
  194. [[NCManageDatabase sharedInstance] clearTable:[tableExternalSites class] account:account];
  195. [[NCManageDatabase sharedInstance] clearTable:[tableLocalFile class] account:account];
  196. [[NCManageDatabase sharedInstance] clearTable:[tableMetadata class] account:account];
  197. [[NCManageDatabase sharedInstance] clearTable:[tableMedia class] account:account];
  198. [[NCManageDatabase sharedInstance] clearTable:[tablePhotoLibrary class] account:account];
  199. [[NCManageDatabase sharedInstance] clearTable:[tableShare class] account:account];
  200. // Clear active user
  201. [appDelegate settingActiveAccount:nil activeUrl:nil activeUser:nil activeUserID:nil activePassword:nil];
  202. }
  203. - (void)answerDelAccount:(XLFormRowDescriptor *)sender
  204. {
  205. [self deselectFormRow:sender];
  206. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_want_delete_",nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  207. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  208. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  209. NSString *accountNow = pickerAccount.rowDescriptor.value;
  210. [self deleteAccount:accountNow];
  211. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  212. if ([listAccount count] > 0)
  213. [self ChangeDefaultAccount:listAccount[0]];
  214. else {
  215. [appDelegate openLoginView:self delegate:self loginType:k_login_Add_Forced selector:k_intro_login];
  216. }
  217. }]];
  218. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  219. [alertController dismissViewControllerAnimated:YES completion:nil];
  220. }]];
  221. alertController.popoverPresentationController.sourceView = self.view;
  222. NSIndexPath *indexPath = [self.form indexPathOfFormRow:sender];
  223. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  224. [self presentViewController:alertController animated:YES completion:nil];
  225. }
  226. #pragma --------------------------------------------------------------------------------------------
  227. #pragma mark === Change Default Account ===
  228. #pragma --------------------------------------------------------------------------------------------
  229. - (void)ChangeDefaultAccount:(NSString *)account
  230. {
  231. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  232. if (tableAccount) {
  233. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:tableAccount.password];
  234. // Init home
  235. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil userInfo:nil];
  236. [self UpdateForm];
  237. [appDelegate subscribingNextcloudServerPushNotification];
  238. }
  239. }
  240. #pragma --------------------------------------------------------------------------------------------
  241. #pragma mark === Update Form ===
  242. #pragma --------------------------------------------------------------------------------------------
  243. - (void)UpdateForm
  244. {
  245. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  246. if (listAccount.count == 0) {
  247. [appDelegate openLoginView:self delegate:self loginType:k_login_Add_Forced selector:k_intro_login];
  248. return;
  249. }
  250. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  251. pickerAccount.rowDescriptor.selectorOptions = listAccount;
  252. pickerAccount.rowDescriptor.value = appDelegate.activeAccount;
  253. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@-%@.png", [CCUtility getDirectoryUserData], [CCUtility getStringUser:appDelegate.activeUser activeUrl:appDelegate.activeUrl], appDelegate.activeUser];
  254. UIImage *avatar = [UIImage imageWithContentsOfFile:fileNamePath];
  255. if (avatar) {
  256. avatar = [CCGraphics scaleImage:avatar toSize:CGSizeMake(40, 40) isAspectRation:YES];
  257. CCAvatar *avatarImageView = [[CCAvatar alloc] initWithImage:avatar borderColor:[UIColor lightGrayColor] borderWidth:0.5];
  258. CGSize imageSize = avatarImageView.bounds.size;
  259. UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
  260. CGContextRef context = UIGraphicsGetCurrentContext();
  261. [avatarImageView.layer renderInContext:context];
  262. avatar = UIGraphicsGetImageFromCurrentImageContext();
  263. UIGraphicsEndImageContext();
  264. } else {
  265. avatar = [UIImage imageNamed:@"avatarBN"];
  266. }
  267. [pickerAccount.rowDescriptor.cellConfig setObject:avatar forKey:@"imageView.image"];
  268. // --
  269. _tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  270. XLFormRowDescriptor *rowUserFullName = [self.form formRowWithTag:@"userfullname"];
  271. XLFormRowDescriptor *rowUserAddress = [self.form formRowWithTag:@"useraddress"];
  272. XLFormRowDescriptor *rowUserPhone = [self.form formRowWithTag:@"userphone"];
  273. XLFormRowDescriptor *rowUserEmail = [self.form formRowWithTag:@"useremail"];
  274. XLFormRowDescriptor *rowUserWeb = [self.form formRowWithTag:@"userweb"];
  275. XLFormRowDescriptor *rowUserTwitter = [self.form formRowWithTag:@"usertwitter"];
  276. rowUserFullName.value = _tableAccount.displayName;
  277. if ([_tableAccount.displayName isEqualToString:@""] || _tableAccount.displayName == nil) rowUserFullName.hidden = @YES;
  278. else rowUserFullName.hidden = @NO;
  279. rowUserAddress.value = _tableAccount.address;
  280. if ([_tableAccount.address isEqualToString:@""] || _tableAccount.address == nil) rowUserAddress.hidden = @YES;
  281. else rowUserAddress.hidden = @NO;
  282. rowUserPhone.value = _tableAccount.phone;
  283. if ([_tableAccount.phone isEqualToString:@""] || _tableAccount.phone == nil) rowUserPhone.hidden = @YES;
  284. else rowUserPhone.hidden = @NO;
  285. rowUserEmail.value = _tableAccount.email;
  286. if ([_tableAccount.email isEqualToString:@""] || _tableAccount.email == nil) rowUserEmail.hidden = @YES;
  287. else rowUserEmail.hidden = @NO;
  288. rowUserWeb.value = _tableAccount.webpage;
  289. if ([_tableAccount.webpage isEqualToString:@""] || _tableAccount.webpage == nil) rowUserWeb.hidden = @YES;
  290. else rowUserWeb.hidden = @NO;
  291. rowUserTwitter.value = _tableAccount.twitter;
  292. if ([_tableAccount.twitter isEqualToString:@""] || _tableAccount.twitter == nil) rowUserTwitter.hidden = @YES;
  293. else rowUserTwitter.hidden = @NO;
  294. [self.tableView reloadData];
  295. [self performSelector:@selector(reloadData) withObject:nil afterDelay:1];
  296. }
  297. - (void)reloadData
  298. {
  299. [self.tableView reloadData];
  300. }
  301. @end