CCManageAccount.m 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474
  1. //
  2. // CCManageAccount.m
  3. // Crypto Cloud Technology Nextcloud
  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 "CCLogin.h"
  26. #import "NCAutoUpload.h"
  27. #import "NCBridgeSwift.h"
  28. #define actionSheetCancellaAccount 1
  29. @interface CCManageAccount () <CCLoginDelegate, CCLoginDelegateWeb>
  30. {
  31. tableAccount *_tableAccount;
  32. CCLoginWeb *_loginWeb;
  33. CCLogin *_loginVC;
  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. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  43. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  44. // Section : CLOUD ACCOUNT -------------------------------------------
  45. section = [XLFormSectionDescriptor formSectionWithTitle:@"cloud account"];
  46. [form addFormSection:section];
  47. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  48. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"pickerAccount" rowType:XLFormRowDescriptorTypePicker];
  49. row.height = 100;
  50. row.selectorOptions = listAccount;
  51. row.value = app.activeAccount;
  52. [section addFormRow:row];
  53. // Section : USER INFORMATION -------------------------------------------
  54. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_personal_information_", nil)];
  55. [form addFormSection:section];
  56. // Full Name
  57. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userfullname" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_full_name_", nil)];
  58. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  59. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"detailTextLabel.font"];
  60. [section addFormRow:row];
  61. // Address
  62. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"useraddress" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_address_", 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. // Phone
  67. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userphone" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_phone_", 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. // Email
  72. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"useremail" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_email_", 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. // Web
  77. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"userweb" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_web_", 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. // Twitter
  82. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"usertwitter" rowType:XLFormRowDescriptorTypeInfo title:NSLocalizedString(@"_user_twitter_", 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. // Section : MANAGE ACCOUNT -------------------------------------------
  87. if ([NCBrandOptions sharedInstance].disable_manage_account == NO) {
  88. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_manage_account_", nil)];
  89. [form addFormSection:section];
  90. // Modify Account
  91. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"changePassword" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_change_password_", nil)];
  92. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  93. [row.cellConfig setObject:[UIImage imageNamed:@"settingsAccountModify"] forKey:@"imageView.image"];
  94. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  95. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  96. row.action.formSelector = @selector(changePassword:);
  97. if (listAccount.count == 0) row.disabled = @YES;
  98. [section addFormRow:row];
  99. // Brand
  100. if ([NCBrandOptions sharedInstance].disable_multiaccount == NO) {
  101. // New Account nextcloud
  102. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"addAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_account_", nil)];
  103. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  104. [row.cellConfig setObject:[UIImage imageNamed:@"settingsAccountNextcloud"] forKey:@"imageView.image"];
  105. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  106. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  107. row.action.formSelector = @selector(addAccount:);
  108. [section addFormRow:row];
  109. }
  110. // delete Account
  111. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"delAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_delete_account_", nil)];
  112. if (listAccount.count > 0)
  113. [row.cellConfig setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  114. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  115. [row.cellConfig setObject:[UIImage imageNamed:@"settingsAccountDelete"] forKey:@"imageView.image"];
  116. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  117. row.action.formSelector = @selector(answerDelAccount:);
  118. if (listAccount.count == 0) row.disabled = @YES;
  119. [section addFormRow:row];
  120. }
  121. return [super initWithForm:form];
  122. }
  123. // Apparirà
  124. - (void)viewWillAppear:(BOOL)animated
  125. {
  126. [super viewWillAppear:animated];
  127. self.tableView.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
  128. // Color
  129. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO 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"]){
  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. // Align Photo Library
  155. if (loginType != loginModifyPasswordUser)
  156. [[NCAutoUpload sharedInstance] alignPhotoLibrary];
  157. if (loginType == loginAddForced)
  158. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil];
  159. }
  160. #pragma --------------------------------------------------------------------------------------------
  161. #pragma mark === Add Account ===
  162. #pragma --------------------------------------------------------------------------------------------
  163. - (void)addAccount:(XLFormRowDescriptor *)sender
  164. {
  165. [self deselectFormRow:sender];
  166. [app cancelAllOperations];
  167. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  168. // Brand
  169. if ([NCBrandOptions sharedInstance].use_login_web) {
  170. _loginWeb = [CCLoginWeb new];
  171. _loginWeb.delegate = self;
  172. _loginWeb.loginType = loginAdd;
  173. [_loginWeb presentModalWithDefaultTheme:self];
  174. } else {
  175. _loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  176. _loginVC.delegate = self;
  177. _loginVC.loginType = loginAdd;
  178. [self presentViewController:_loginVC animated:YES completion:nil];
  179. }
  180. }
  181. - (void)addAccountFoced
  182. {
  183. // Brand
  184. if ([NCBrandOptions sharedInstance].use_login_web) {
  185. _loginWeb = [CCLoginWeb new];
  186. _loginWeb.delegate = self;
  187. _loginWeb.loginType = loginAddForced;
  188. dispatch_async(dispatch_get_main_queue(), ^ {
  189. [_loginWeb presentModalWithDefaultTheme:self];
  190. });
  191. } else {
  192. _loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  193. _loginVC.delegate = self;
  194. _loginVC.loginType = loginAddForced;
  195. dispatch_async(dispatch_get_main_queue(), ^ {
  196. [self presentViewController:_loginVC animated:YES completion:nil];
  197. });
  198. }
  199. }
  200. #pragma --------------------------------------------------------------------------------------------
  201. #pragma mark === Modify Account ===
  202. #pragma --------------------------------------------------------------------------------------------
  203. - (void)changePassword:(XLFormRowDescriptor *)sender
  204. {
  205. [self deselectFormRow:sender];
  206. [app cancelAllOperations];
  207. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  208. // Brand
  209. if ([NCBrandOptions sharedInstance].use_login_web) {
  210. _loginWeb = [CCLoginWeb new];
  211. _loginWeb.delegate = self;
  212. _loginWeb.loginType = loginModifyPasswordUser;
  213. dispatch_async(dispatch_get_main_queue(), ^ {
  214. [_loginWeb presentModalWithDefaultTheme:self];
  215. });
  216. } else {
  217. _loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  218. _loginVC.delegate = self;
  219. _loginVC.loginType = loginModifyPasswordUser;
  220. dispatch_async(dispatch_get_main_queue(), ^ {
  221. [self presentViewController:_loginVC animated:YES completion:nil];
  222. });
  223. }
  224. [self UpdateForm];
  225. }
  226. #pragma --------------------------------------------------------------------------------------------
  227. #pragma mark === Delete Account ===
  228. #pragma --------------------------------------------------------------------------------------------
  229. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  230. {
  231. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  232. [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
  233. if (buttonIndex == 0 && actionSheet.tag == actionSheetCancellaAccount) {
  234. NSString *accountNow = pickerAccount.rowDescriptor.value;
  235. [self deleteAccount:accountNow];
  236. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  237. if ([listAccount count] > 0) [self ChangeDefaultAccount:listAccount[0]];
  238. else {
  239. [self addAccountFoced];
  240. }
  241. }
  242. }
  243. - (void)deleteAccount:(NSString *)account
  244. {
  245. [app cancelAllOperations];
  246. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  247. [[NCManageDatabase sharedInstance] clearTable:[tableAccount class] account:account];
  248. [[NCManageDatabase sharedInstance] clearTable:[tableActivity class] account:account];
  249. [[NCManageDatabase sharedInstance] clearTable:[tableAutoUpload class] account:account];
  250. [[NCManageDatabase sharedInstance] clearTable:[tableCapabilities class] account:account];
  251. [[NCManageDatabase sharedInstance] clearTable:[tableDirectory class] account:app.activeAccount];
  252. [[NCManageDatabase sharedInstance] clearTable:[tableExternalSites class] account:account];
  253. [[NCManageDatabase sharedInstance] clearTable:[tableLocalFile class] account:app.activeAccount];
  254. [[NCManageDatabase sharedInstance] clearTable:[tableMetadata class] account:account];
  255. [[NCManageDatabase sharedInstance] clearTable:[tableShare class] account:account];
  256. // Clear active user
  257. [app settingActiveAccount:nil activeUrl:nil activeUser:nil activePassword:nil];
  258. }
  259. - (void)answerDelAccount:(XLFormRowDescriptor *)sender
  260. {
  261. [self deselectFormRow:sender];
  262. UIActionSheet *actionSheet1 = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"_want_delete_",nil)
  263. delegate:self
  264. cancelButtonTitle:NSLocalizedString(@"_no_delete_",nil)
  265. destructiveButtonTitle:NSLocalizedString(@"_yes_delete_",nil)
  266. otherButtonTitles:nil];
  267. actionSheet1.tag = actionSheetCancellaAccount;
  268. [actionSheet1 showInView:self.view.window.rootViewController.view];
  269. }
  270. #pragma --------------------------------------------------------------------------------------------
  271. #pragma mark === Change Default Account ===
  272. #pragma --------------------------------------------------------------------------------------------
  273. - (void)ChangeDefaultAccount:(NSString *)account
  274. {
  275. if ([app.netQueue operationCount] > 0 || [app.netQueueDownload operationCount] > 0 || [app.netQueueDownloadWWan operationCount] > 0 || [app.netQueueUpload operationCount] > 0 || [app.netQueueUploadWWan operationCount] > 0 || [[NCManageDatabase sharedInstance] countAutoUploadWithSession:nil] > 0) {
  276. [app messageNotification:@"_transfers_in_queue_" description:nil visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  277. [self UpdateForm];
  278. return;
  279. }
  280. // removed this -> ?????
  281. [app cancelAllOperations];
  282. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  283. // removed this -> ?????
  284. // change account
  285. tableAccount *tableAccount = [[NCManageDatabase sharedInstance] setAccountActive:account];
  286. if (tableAccount)
  287. [app settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activePassword:tableAccount.password];
  288. // Init home
  289. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil];
  290. [self UpdateForm];
  291. }
  292. #pragma --------------------------------------------------------------------------------------------
  293. #pragma mark === Update Form ===
  294. #pragma --------------------------------------------------------------------------------------------
  295. - (void)UpdateForm
  296. {
  297. NSArray *listAccount = [[NCManageDatabase sharedInstance] getAccounts];
  298. if (listAccount == nil) {
  299. [self addAccountFoced];
  300. return;
  301. }
  302. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  303. pickerAccount.rowDescriptor.selectorOptions = listAccount;
  304. pickerAccount.rowDescriptor.value = app.activeAccount;
  305. UIImage *avatar = [UIImage imageWithContentsOfFile:[NSString stringWithFormat:@"%@/avatar.png", app.directoryUser]];
  306. if (avatar) {
  307. avatar = [CCGraphics scaleImage:avatar toSize:CGSizeMake(40, 40) isAspectRation:YES];
  308. CCAvatar *avatarImageView = [[CCAvatar alloc] initWithImage:avatar borderColor:[UIColor lightGrayColor] borderWidth:0.5];
  309. CGSize imageSize = avatarImageView.bounds.size;
  310. UIGraphicsBeginImageContextWithOptions(imageSize, NO, 0);
  311. CGContextRef context = UIGraphicsGetCurrentContext();
  312. [avatarImageView.layer renderInContext:context];
  313. avatar = UIGraphicsGetImageFromCurrentImageContext();
  314. UIGraphicsEndImageContext();
  315. } else {
  316. avatar = [UIImage imageNamed:@"avatarBN"];
  317. }
  318. [pickerAccount.rowDescriptor.cellConfig setObject:avatar forKey:@"imageView.image"];
  319. // --
  320. _tableAccount = [[NCManageDatabase sharedInstance] getAccountActive];
  321. XLFormRowDescriptor *rowUserFullName = [self.form formRowWithTag:@"userfullname"];
  322. XLFormRowDescriptor *rowUserAddress = [self.form formRowWithTag:@"useraddress"];
  323. XLFormRowDescriptor *rowUserPhone = [self.form formRowWithTag:@"userphone"];
  324. XLFormRowDescriptor *rowUserEmail = [self.form formRowWithTag:@"useremail"];
  325. XLFormRowDescriptor *rowUserWeb = [self.form formRowWithTag:@"userweb"];
  326. XLFormRowDescriptor *rowUserTwitter = [self.form formRowWithTag:@"usertwitter"];
  327. rowUserFullName.value = _tableAccount.displayName;
  328. if ([_tableAccount.displayName isEqualToString:@""] || _tableAccount.displayName == nil) rowUserFullName.hidden = @YES;
  329. else rowUserFullName.hidden = @NO;
  330. rowUserAddress.value = _tableAccount.address;
  331. if ([_tableAccount.address isEqualToString:@""] || _tableAccount.address == nil) rowUserAddress.hidden = @YES;
  332. else rowUserAddress.hidden = @NO;
  333. rowUserPhone.value = _tableAccount.phone;
  334. if ([_tableAccount.phone isEqualToString:@""] || _tableAccount.phone == nil) rowUserPhone.hidden = @YES;
  335. else rowUserPhone.hidden = @NO;
  336. rowUserEmail.value = _tableAccount.email;
  337. if ([_tableAccount.email isEqualToString:@""] || _tableAccount.email == nil) rowUserEmail.hidden = @YES;
  338. else rowUserEmail.hidden = @NO;
  339. rowUserWeb.value = _tableAccount.webpage;
  340. if ([_tableAccount.webpage isEqualToString:@""] || _tableAccount.webpage == nil) rowUserWeb.hidden = @YES;
  341. else rowUserWeb.hidden = @NO;
  342. rowUserTwitter.value = _tableAccount.twitter;
  343. if ([_tableAccount.twitter isEqualToString:@""] || _tableAccount.twitter == nil) rowUserTwitter.hidden = @YES;
  344. else rowUserTwitter.hidden = @NO;
  345. [self.tableView reloadData];
  346. [self performSelector:@selector(reloadData) withObject:nil afterDelay:1];
  347. }
  348. - (void)reloadData
  349. {
  350. [self.tableView reloadData];
  351. }
  352. /*
  353. - (void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath
  354. {
  355. XLFormRowDescriptor *row = [self.form formRowAtIndex:indexPath];
  356. if ([row.tag isEqualToString:@"pickerAccount"]) {
  357. // set background color in here
  358. }
  359. }
  360. */
  361. @end