CCManageAccount.m 18 KB

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