CCManageAccount.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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. }
  34. @end
  35. @implementation CCManageAccount
  36. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  37. {
  38. self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  39. if (self) {
  40. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  41. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  42. }
  43. return self;
  44. }
  45. - (void)initializeForm
  46. {
  47. XLFormDescriptor *form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_credentials_", nil)];
  48. XLFormSectionDescriptor *section;
  49. XLFormRowDescriptor *row;
  50. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  51. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  52. // Section : ACCOUNTS -------------------------------------------
  53. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_accounts_", nil)];
  54. [form addFormSection:section];
  55. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  56. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"pickerAccount" rowType:XLFormRowDescriptorTypePicker];
  57. row.height = 100;
  58. if (listAccount.count > 0) {
  59. row.selectorOptions = listAccount;
  60. row.value = tableAccount.account;
  61. } else {
  62. row.selectorOptions = [[NSArray alloc] initWithObjects:@"", nil];
  63. }
  64. // Avatar
  65. NSString *fileNamePath = [NSString stringWithFormat:@"%@/%@-%@.png", [CCUtility getDirectoryUserData], [CCUtility getStringUser:appDelegate.activeUser activeUrl:appDelegate.activeUrl], appDelegate.activeUser];
  66. UIImage *avatar = [UIImage imageWithContentsOfFile:fileNamePath];
  67. if (avatar) {
  68. avatar = [CCGraphics scaleImage:avatar toSize:CGSizeMake(40, 40) isAspectRation:YES];
  69. CCAvatar *avatarImageView = [[CCAvatar alloc] initWithImage:avatar borderColor:[UIColor lightGrayColor] borderWidth:0.5];
  70. CGSize imageSize = avatarImageView.bounds.size;
  71. UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
  72. CGContextRef context = UIGraphicsGetCurrentContext();
  73. [avatarImageView.layer renderInContext:context];
  74. avatar = UIGraphicsGetImageFromCurrentImageContext();
  75. UIGraphicsEndImageContext();
  76. } else {
  77. avatar = [UIImage imageNamed:@"avatarBN"];
  78. }
  79. [row.cellConfig setObject:avatar forKey:@"imageView.image"];
  80. [section addFormRow:row];
  81. // Section : USER INFORMATION -------------------------------------------
  82. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_personal_information_", nil)];
  83. [form addFormSection:section];
  84. // Full Name
  85. if ([tableAccount.displayName length] > 0) {
  86. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userfullname" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_full_name_", nil)];
  87. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  88. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  89. row.value = tableAccount.displayName;
  90. [section addFormRow:row];
  91. }
  92. // Address
  93. if ([tableAccount.address length] > 0) {
  94. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"useraddress" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_address_", nil)];
  95. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  96. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  97. row.value = tableAccount.address;
  98. [section addFormRow:row];
  99. }
  100. // City
  101. if ([tableAccount.city length] > 0) {
  102. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"usercity" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_city_", nil)];
  103. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  104. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  105. row.value = tableAccount.city;
  106. [section addFormRow:row];
  107. }
  108. // Country
  109. if ([tableAccount.country length] > 0) {
  110. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"usercity" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_country_", nil)];
  111. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  112. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  113. row.value = tableAccount.country;
  114. [section addFormRow:row];
  115. }
  116. // Zip
  117. if ([tableAccount.zip length] > 0) {
  118. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userzip" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_zip_", nil)];
  119. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  120. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  121. row.value = tableAccount.zip;
  122. [section addFormRow:row];
  123. }
  124. // Phone
  125. if ([tableAccount.phone length] > 0) {
  126. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userphone" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_phone_", nil)];
  127. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  128. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  129. row.value = tableAccount.phone;
  130. [section addFormRow:row];
  131. }
  132. // Email
  133. if ([tableAccount.email length] > 0) {
  134. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"useremail" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_email_", nil)];
  135. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  136. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  137. row.value = tableAccount.email;
  138. [section addFormRow:row];
  139. }
  140. // Web
  141. if ([tableAccount.webpage length] > 0) {
  142. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userweb" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_web_", nil)];
  143. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  144. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  145. row.value = tableAccount.webpage;
  146. [section addFormRow:row];
  147. }
  148. // Twitter
  149. if ([tableAccount.twitter length] > 0) {
  150. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"usertwitter" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_twitter_", nil)];
  151. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  152. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  153. row.value = tableAccount.twitter;
  154. [section addFormRow:row];
  155. }
  156. // Section : THIRT PART -------------------------------------------
  157. if ([NCBrandOptions.sharedInstance.brandInitials containsString:@"hc"]) {
  158. section = [XLFormSectionDescriptor formSection];
  159. [form addFormSection:section];
  160. // Business Type
  161. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userbusinesstype" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_businesstype_", nil)];
  162. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  163. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  164. row.value = tableAccount.businessType;
  165. [section addFormRow:row];
  166. // Business Size
  167. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userbusinesssize" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_businesssize_", nil)];
  168. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  169. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  170. row.value = @(tableAccount.businessSize).stringValue;
  171. [section addFormRow:row];
  172. // Role
  173. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userrole" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_role_", nil)];
  174. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  175. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  176. row.value = tableAccount.role;
  177. [section addFormRow:row];
  178. // Company
  179. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"usercompany" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_company_", nil)];
  180. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  181. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  182. row.value = tableAccount.company;
  183. [section addFormRow:row];
  184. }
  185. // Section : MANAGE ACCOUNT -------------------------------------------
  186. if ([NCBrandOptions sharedInstance].disable_manage_account == NO) {
  187. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_manage_account_", nil)];
  188. [form addFormSection:section];
  189. // Modify Account
  190. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"changePassword" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_change_password_", nil)];
  191. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  192. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"rename"] multiplier:2 color:[NCBrandColor sharedInstance].icon] forKey:@"imageView.image"];
  193. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  194. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  195. row.action.formSelector = @selector(changePassword:);
  196. if (listAccount.count == 0) row.disabled = @YES;
  197. [section addFormRow:row];
  198. // Brand
  199. if ([NCBrandOptions sharedInstance].disable_multiaccount == NO) {
  200. // New Account nextcloud
  201. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"addAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_account_", nil)];
  202. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  203. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"add"] multiplier:2 color:[NCBrandColor sharedInstance].icon] forKey:@"imageView.image"];
  204. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  205. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  206. row.action.formSelector = @selector(addAccount:);
  207. [section addFormRow:row];
  208. }
  209. // delete Account
  210. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"delAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_delete_account_", nil)];
  211. [row.cellConfig setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  212. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  213. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[UIColor redColor]] forKey:@"imageView.image"];
  214. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  215. row.action.formSelector = @selector(deleteAccount:);
  216. if (listAccount.count == 0) row.disabled = @YES;
  217. [section addFormRow:row];
  218. }
  219. self.form = form;
  220. // Open Login
  221. if (listAccount.count == 0) {
  222. [appDelegate openLoginView:self delegate:self loginType:k_login_Add_Forced selector:k_intro_login];
  223. }
  224. }
  225. // Apparirà
  226. - (void)viewWillAppear:(BOOL)animated
  227. {
  228. [super viewWillAppear:animated];
  229. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  230. self.tableView.showsVerticalScrollIndicator = NO;
  231. // Color
  232. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  233. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  234. [self initializeForm];
  235. }
  236. - (void)changeTheming
  237. {
  238. [appDelegate changeTheming:self];
  239. }
  240. #pragma --------------------------------------------------------------------------------------------
  241. #pragma mark === Delegate ===
  242. #pragma --------------------------------------------------------------------------------------------
  243. -(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  244. {
  245. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  246. if ([rowDescriptor.tag isEqualToString:@"pickerAccount"] && oldValue && newValue) {
  247. if (![newValue isEqualToString:oldValue] && ![newValue isEqualToString:@""] && ![newValue isEqualToString:appDelegate.activeAccount]) {
  248. [self ChangeDefaultAccount:newValue];
  249. }
  250. if ([newValue isEqualToString:@""]) {
  251. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  252. if ([listAccount count] > 0) {
  253. [self ChangeDefaultAccount:listAccount[0]];
  254. }
  255. }
  256. }
  257. }
  258. #pragma --------------------------------------------------------------------------------------------
  259. #pragma mark === Delegate Login ===
  260. #pragma --------------------------------------------------------------------------------------------
  261. - (void)loginSuccess:(NSInteger)loginType
  262. {
  263. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil userInfo:nil];
  264. }
  265. #pragma --------------------------------------------------------------------------------------------
  266. #pragma mark === Add Account ===
  267. #pragma --------------------------------------------------------------------------------------------
  268. - (void)addAccount:(XLFormRowDescriptor *)sender
  269. {
  270. [self deselectFormRow:sender];
  271. [appDelegate openLoginView:self delegate:self loginType:k_login_Add selector:k_intro_login];
  272. }
  273. #pragma --------------------------------------------------------------------------------------------
  274. #pragma mark === Modify Account ===
  275. #pragma --------------------------------------------------------------------------------------------
  276. - (void)changePassword:(XLFormRowDescriptor *)sender
  277. {
  278. [self deselectFormRow:sender];
  279. [appDelegate openLoginView:self delegate:self loginType:k_login_Modify_Password selector:k_intro_login];
  280. }
  281. #pragma --------------------------------------------------------------------------------------------
  282. #pragma mark === Delete Account ===
  283. #pragma --------------------------------------------------------------------------------------------
  284. - (void)deleteAccount:(XLFormRowDescriptor *)sender
  285. {
  286. [self deselectFormRow:sender];
  287. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_want_delete_",nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  288. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  289. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  290. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] getAccountWithPredicate:[NSPredicate predicateWithFormat:@"account == %@", pickerAccount.rowDescriptor.value]];
  291. if (tableAccount) {
  292. [appDelegate unsubscribingNextcloudServerPushNotification:tableAccount.account url:tableAccount.url withSubscribing:false];
  293. [[NCManageDatabase sharedInstance] clearTable:[tableAccount class] account:tableAccount.account];
  294. [[NCManageDatabase sharedInstance] clearTable:[tableActivity class] account:tableAccount.account];
  295. [[NCManageDatabase sharedInstance] clearTable:[tableActivitySubjectRich class] account:tableAccount.account];
  296. [[NCManageDatabase sharedInstance] clearTable:[tableCapabilities class] account:tableAccount.account];
  297. [[NCManageDatabase sharedInstance] clearTable:[tableDirectory class] account:tableAccount.account];
  298. [[NCManageDatabase sharedInstance] clearTable:[tableE2eEncryption class] account:tableAccount.account];
  299. [[NCManageDatabase sharedInstance] clearTable:[tableExternalSites class] account:tableAccount.account];
  300. [[NCManageDatabase sharedInstance] clearTable:[tableLocalFile class] account:tableAccount.account];
  301. [[NCManageDatabase sharedInstance] clearTable:[tableMetadata class] account:tableAccount.account];
  302. [[NCManageDatabase sharedInstance] clearTable:[tableMedia class] account:tableAccount.account];
  303. [[NCManageDatabase sharedInstance] clearTable:[tablePhotoLibrary class] account:tableAccount.account];
  304. [[NCManageDatabase sharedInstance] clearTable:[tableShare class] account:tableAccount.account];
  305. // Clear active user
  306. [appDelegate settingActiveAccount:nil activeUrl:nil activeUser:nil activeUserID:nil activePassword:nil];
  307. }
  308. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  309. if ([listAccount count] > 0) {
  310. [self ChangeDefaultAccount:listAccount[0]];
  311. } else {
  312. [self initializeForm];
  313. }
  314. }]];
  315. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) { }]];
  316. alertController.popoverPresentationController.sourceView = self.view;
  317. NSIndexPath *indexPath = [self.form indexPathOfFormRow:sender];
  318. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  319. [self presentViewController:alertController animated:YES completion:nil];
  320. }
  321. #pragma --------------------------------------------------------------------------------------------
  322. #pragma mark === Change Default Account ===
  323. #pragma --------------------------------------------------------------------------------------------
  324. - (void)ChangeDefaultAccount:(NSString *)account
  325. {
  326. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  327. if (tableAccount) {
  328. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:[CCUtility getPassword:tableAccount.account]];
  329. // Init home
  330. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil userInfo:nil];
  331. }
  332. [self initializeForm];
  333. }
  334. @end