CCManageAccount.m 19 KB

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