CCManageAccount.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444
  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. 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:[UIImage imageNamed:@"rename"] 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:[UIImage imageNamed:@"add"] 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:@"delete"] 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];
  164. }
  165. - (void)loginClose
  166. {
  167. appDelegate.activeLogin = nil;
  168. }
  169. - (void)loginWebClose
  170. {
  171. appDelegate.activeLoginWeb = nil;
  172. }
  173. #pragma --------------------------------------------------------------------------------------------
  174. #pragma mark === Add Account ===
  175. #pragma --------------------------------------------------------------------------------------------
  176. - (void)addAccount:(XLFormRowDescriptor *)sender
  177. {
  178. [self deselectFormRow:sender];
  179. NSInteger transferInprogress = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND (status == %d OR status == %d OR status == %d OR status == %d)", appDelegate.activeAccount, k_metadataStatusInDownload, k_metadataStatusDownloading, k_metadataStatusInUpload, k_metadataStatusUploading] sorted:@"fileName" ascending:true] count];
  180. if (transferInprogress > 0) {
  181. [JDStatusBarNotification showWithStatus:NSLocalizedString(@"_transfers_in_queue_", nil) dismissAfter:k_dismissAfterSecond styleName:JDStatusBarStyleDefault];
  182. return;
  183. }
  184. [appDelegate.netQueue cancelAllOperations];
  185. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  186. [appDelegate openLoginView:self loginType:loginAdd];
  187. });
  188. }
  189. - (void)addAccountForced
  190. {
  191. [appDelegate openLoginView:self loginType:loginAddForced];
  192. }
  193. #pragma --------------------------------------------------------------------------------------------
  194. #pragma mark === Modify Account ===
  195. #pragma --------------------------------------------------------------------------------------------
  196. - (void)changePassword:(XLFormRowDescriptor *)sender
  197. {
  198. [self deselectFormRow:sender];
  199. NSInteger transferInprogress = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND (status == %d OR status == %d OR status == %d OR status == %d)", appDelegate.activeAccount, k_metadataStatusInDownload, k_metadataStatusDownloading, k_metadataStatusInUpload, k_metadataStatusUploading] sorted:@"fileName" ascending:true] count];
  200. if (transferInprogress > 0) {
  201. [JDStatusBarNotification showWithStatus:NSLocalizedString(@"_transfers_in_queue_", nil) dismissAfter:k_dismissAfterSecond styleName:JDStatusBarStyleDefault];
  202. return;
  203. }
  204. [appDelegate.netQueue cancelAllOperations];
  205. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  206. [appDelegate openLoginView:self loginType:loginModifyPasswordUser];
  207. });
  208. }
  209. #pragma --------------------------------------------------------------------------------------------
  210. #pragma mark === Delete Account ===
  211. #pragma --------------------------------------------------------------------------------------------
  212. - (void)deleteAccount:(NSString *)account
  213. {
  214. NSInteger transferInprogress = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND (status == %d OR status == %d OR status == %d OR status == %d)", appDelegate.activeAccount, k_metadataStatusInDownload, k_metadataStatusDownloading, k_metadataStatusInUpload, k_metadataStatusUploading] sorted:@"fileName" ascending:true] count];
  215. if (transferInprogress > 0) {
  216. [JDStatusBarNotification showWithStatus:NSLocalizedString(@"_transfers_in_queue_", nil) dismissAfter:k_dismissAfterSecond styleName:JDStatusBarStyleDefault];
  217. return;
  218. }
  219. [appDelegate.netQueue cancelAllOperations];
  220. [[NCManageDatabase sharedInstance] clearTable:[tableAccount class] account:account];
  221. [[NCManageDatabase sharedInstance] clearTable:[tableActivity class] account:account];
  222. [[NCManageDatabase sharedInstance] clearTable:[tableCapabilities class] account:account];
  223. [[NCManageDatabase sharedInstance] clearTable:[tableDirectory class] account:account];
  224. [[NCManageDatabase sharedInstance] clearTable:[tableE2eEncryption class] account:account];
  225. [[NCManageDatabase sharedInstance] clearTable:[tableExternalSites class] account:account];
  226. [[NCManageDatabase sharedInstance] clearTable:[tableLocalFile class] account:account];
  227. [[NCManageDatabase sharedInstance] clearTable:[tableMetadata class] account:account];
  228. [[NCManageDatabase sharedInstance] clearTable:[tablePhotoLibrary class] account:account];
  229. [[NCManageDatabase sharedInstance] clearTable:[tableShare class] account:account];
  230. // Clear active user
  231. [appDelegate settingActiveAccount:nil activeUrl:nil activeUser:nil activeUserID:nil activePassword:nil];
  232. }
  233. - (void)answerDelAccount:(XLFormRowDescriptor *)sender
  234. {
  235. [self deselectFormRow:sender];
  236. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_want_delete_",nil) message:nil preferredStyle:UIAlertControllerStyleActionSheet];
  237. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  238. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  239. NSString *accountNow = pickerAccount.rowDescriptor.value;
  240. [self deleteAccount:accountNow];
  241. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  242. if ([listAccount count] > 0)
  243. [self ChangeDefaultAccount:listAccount[0]];
  244. else {
  245. [self addAccountForced];
  246. }
  247. }]];
  248. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  249. [alertController dismissViewControllerAnimated:YES completion:nil];
  250. }]];
  251. alertController.popoverPresentationController.sourceView = self.view;
  252. NSIndexPath *indexPath = [self.form indexPathOfFormRow:sender];
  253. alertController.popoverPresentationController.sourceRect = [self.tableView rectForRowAtIndexPath:indexPath];
  254. [self presentViewController:alertController animated:YES completion:nil];
  255. }
  256. #pragma --------------------------------------------------------------------------------------------
  257. #pragma mark === Change Default Account ===
  258. #pragma --------------------------------------------------------------------------------------------
  259. - (void)ChangeDefaultAccount:(NSString *)account
  260. {
  261. NSInteger transferInprogress = [[[NCManageDatabase sharedInstance] getMetadatasWithPredicate:[NSPredicate predicateWithFormat:@"account == %@ AND (status == %d OR status == %d OR status == %d OR status == %d)", appDelegate.activeAccount, k_metadataStatusInDownload, k_metadataStatusDownloading, k_metadataStatusInUpload, k_metadataStatusUploading] sorted:@"fileName" ascending:true] count];
  262. if (transferInprogress > 0) {
  263. [JDStatusBarNotification showWithStatus:NSLocalizedString(@"_transfers_in_queue_", nil) dismissAfter:k_dismissAfterSecond styleName:JDStatusBarStyleDefault];
  264. return;
  265. }
  266. [appDelegate.netQueue cancelAllOperations];
  267. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.3 * NSEC_PER_SEC), dispatch_get_main_queue(), ^{
  268. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  269. if (tableAccount) {
  270. [appDelegate settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activeUserID:tableAccount.userID activePassword:tableAccount.password];
  271. // Init home
  272. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil];
  273. [self UpdateForm];
  274. }
  275. });
  276. }
  277. #pragma --------------------------------------------------------------------------------------------
  278. #pragma mark === Update Form ===
  279. #pragma --------------------------------------------------------------------------------------------
  280. - (void)UpdateForm
  281. {
  282. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  283. if (listAccount.count == 0) {
  284. [self addAccountForced];
  285. return;
  286. }
  287. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  288. pickerAccount.rowDescriptor.selectorOptions = listAccount;
  289. pickerAccount.rowDescriptor.value = appDelegate.activeAccount;
  290. UIImage *avatar = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/avatar.png", appDelegate.directoryUser]];
  291. if (avatar) {
  292. avatar = [CCGraphics scaleImage:avatar toSize:CGSizeMake(40, 40) isAspectRation:YES];
  293. CCAvatar *avatarImageView = [[CCAvatar alloc] initWithImage:avatar borderColor:[UIColor lightGrayColor] borderWidth:0.5];
  294. CGSize imageSize = avatarImageView.bounds.size;
  295. UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
  296. CGContextRef context = UIGraphicsGetCurrentContext();
  297. [avatarImageView.layer renderInContext:context];
  298. avatar = UIGraphicsGetImageFromCurrentImageContext();
  299. UIGraphicsEndImageContext();
  300. } else {
  301. avatar = [UIImage imageNamed:@"avatarBN"];
  302. }
  303. [pickerAccount.rowDescriptor.cellConfig setObject:avatar forKey:@"imageView.image"];
  304. // --
  305. _tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  306. XLFormRowDescriptor *rowUserFullName = [self.form formRowWithTag:@"userfullname"];
  307. XLFormRowDescriptor *rowUserAddress = [self.form formRowWithTag:@"useraddress"];
  308. XLFormRowDescriptor *rowUserPhone = [self.form formRowWithTag:@"userphone"];
  309. XLFormRowDescriptor *rowUserEmail = [self.form formRowWithTag:@"useremail"];
  310. XLFormRowDescriptor *rowUserWeb = [self.form formRowWithTag:@"userweb"];
  311. XLFormRowDescriptor *rowUserTwitter = [self.form formRowWithTag:@"usertwitter"];
  312. rowUserFullName.value = _tableAccount.displayName;
  313. if ([_tableAccount.displayName isEqualToString:@""] || _tableAccount.displayName == nil) rowUserFullName.hidden = @YES;
  314. else rowUserFullName.hidden = @NO;
  315. rowUserAddress.value = _tableAccount.address;
  316. if ([_tableAccount.address isEqualToString:@""] || _tableAccount.address == nil) rowUserAddress.hidden = @YES;
  317. else rowUserAddress.hidden = @NO;
  318. rowUserPhone.value = _tableAccount.phone;
  319. if ([_tableAccount.phone isEqualToString:@""] || _tableAccount.phone == nil) rowUserPhone.hidden = @YES;
  320. else rowUserPhone.hidden = @NO;
  321. rowUserEmail.value = _tableAccount.email;
  322. if ([_tableAccount.email isEqualToString:@""] || _tableAccount.email == nil) rowUserEmail.hidden = @YES;
  323. else rowUserEmail.hidden = @NO;
  324. rowUserWeb.value = _tableAccount.webpage;
  325. if ([_tableAccount.webpage isEqualToString:@""] || _tableAccount.webpage == nil) rowUserWeb.hidden = @YES;
  326. else rowUserWeb.hidden = @NO;
  327. rowUserTwitter.value = _tableAccount.twitter;
  328. if ([_tableAccount.twitter isEqualToString:@""] || _tableAccount.twitter == nil) rowUserTwitter.hidden = @YES;
  329. else rowUserTwitter.hidden = @NO;
  330. [self.tableView reloadData];
  331. [self performSelector:@selector(reloadData) withObject:nil afterDelay:1];
  332. }
  333. - (void)reloadData
  334. {
  335. [self.tableView reloadData];
  336. }
  337. @end