CCManageAccount.m 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 "CCLoginNCOC.h"
  26. #define actionSheetCancellaAccount 1
  27. @implementation CCManageAccount
  28. - (id)initWithCoder:(NSCoder *)aDecoder
  29. {
  30. if (self = [super initWithCoder:aDecoder]) {
  31. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(UpdateForm) name:@"updateFormManageAccount" object:nil];
  32. [self initializeForm];
  33. }
  34. return self;
  35. }
  36. - (void)initializeForm
  37. {
  38. XLFormDescriptor *form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_credentials_", nil)];
  39. XLFormSectionDescriptor *section;
  40. XLFormRowDescriptor *row;
  41. NSArray *listAccount = [CCCoreData getAllAccount];
  42. section = [XLFormSectionDescriptor formSectionWithTitle:@"cloud account"];
  43. [form addFormSection:section];
  44. form.rowNavigationOptions = XLFormRowNavigationOptionNone;
  45. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"pickerAccount" rowType:XLFormRowDescriptorTypePicker];
  46. row.selectorOptions = listAccount;
  47. row.value = app.activeAccount;
  48. [section addFormRow:row];
  49. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_manage_account_", nil)];
  50. [form addFormSection:section];
  51. // Modify Account
  52. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"modifyAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_modify_account_", nil)];
  53. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  54. [row.cellConfig setObject:[UIImage imageNamed:image_settingsAccountModify] forKey:@"imageView.image"];
  55. row.action.formSelector = @selector(modifyAccount:);
  56. if (listAccount.count == 0) row.disabled = @YES;
  57. [section addFormRow:row];
  58. #ifdef NC
  59. // New Account nextcloud
  60. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"addAccountNextcloud" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_nextcloud_", nil)];
  61. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  62. [row.cellConfig setObject:[UIImage imageNamed:image_settingsAccountNextcloud] forKey:@"imageView.image"];
  63. row.action.formSelector = @selector(addAccountNextcloud:);
  64. [section addFormRow:row];
  65. #endif
  66. #ifdef CC
  67. // New Account ownCloud
  68. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"addAccountOwnCloud" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_owncloud_", nil)];
  69. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  70. [row.cellConfig setObject:[UIImage imageNamed:image_settingsAccountOwnCloud] forKey:@"imageView.image"];
  71. row.action.formSelector = @selector(addAccountOwnCloud:);
  72. [section addFormRow:row];
  73. // New Account Dropbox
  74. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"addAccountDropbox" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_add_dropbox_", nil)];
  75. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  76. [row.cellConfig setObject:[UIImage imageNamed:image_settingsAccountDropbox] forKey:@"imageView.image"];
  77. row.action.formSelector = @selector(addAccountDropbox:);
  78. [section addFormRow:row];
  79. #endif
  80. // delete Account
  81. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"delAccount" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_delete_account_", nil)];
  82. if (listAccount.count > 0) [row.cellConfig setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  83. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  84. [row.cellConfig setObject:[UIImage imageNamed:image_settingsAccountDelete] forKey:@"imageView.image"];
  85. row.action.formSelector = @selector(answerDelAccount:);
  86. if (listAccount.count == 0) row.disabled = @YES;
  87. [section addFormRow:row];
  88. self.form = form;
  89. }
  90. // Apparirà
  91. - (void)viewWillAppear:(BOOL)animated
  92. {
  93. [super viewWillAppear:animated];
  94. // Color
  95. [CCAspect aspectNavigationControllerBar:self.navigationController.navigationBar hidden:NO];
  96. [CCAspect aspectTabBar:self.tabBarController.tabBar hidden:NO];
  97. }
  98. // E' apparsa
  99. - (void)viewDidAppear:(BOOL)animated
  100. {
  101. [super viewDidAppear:animated];
  102. [self UpdateForm];
  103. }
  104. #pragma --------------------------------------------------------------------------------------------
  105. #pragma mark === Delegate ===
  106. #pragma --------------------------------------------------------------------------------------------
  107. -(void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  108. {
  109. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  110. if ([rowDescriptor.tag isEqualToString:@"pickerAccount"]){
  111. // cambiamo default account se oldvalue != newValue
  112. if (![newValue isEqualToString:oldValue]) [self ChangeDefaultAccount:newValue];
  113. }
  114. }
  115. #pragma --------------------------------------------------------------------------------------------
  116. #pragma mark === Aggiungi Account ===
  117. #pragma --------------------------------------------------------------------------------------------
  118. /*** NEXTCLOUD ***/
  119. - (void)addAccountNextcloud:(XLFormRowDescriptor *)sender
  120. {
  121. [self deselectFormRow:sender];
  122. [app cancelAllOperations];
  123. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  124. CCLoginNCOC *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginNextcloud"];
  125. [loginVC setModifyOnlyPassword:NO];
  126. [loginVC setTypeCloud:typeCloudNextcloud];
  127. [self presentViewController:loginVC animated:YES completion:nil];
  128. }
  129. /*** OWNCLOUD ***/
  130. - (void)addAccountOwnCloud:(XLFormRowDescriptor *)sender
  131. {
  132. [self deselectFormRow:sender];
  133. [app cancelAllOperations];
  134. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  135. CCLoginNCOC *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLoginOwnCloud"];
  136. [loginVC setModifyOnlyPassword:NO];
  137. [loginVC setTypeCloud:typeCloudOwnCloud];
  138. [self presentViewController:loginVC animated:YES completion:nil];
  139. }
  140. #ifdef CC
  141. /*** DROPBOX ***/
  142. - (void)addAccountDropbox:(XLFormRowDescriptor *)sender
  143. {
  144. [self deselectFormRow:sender];
  145. [app cancelAllOperations];
  146. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  147. [[DBSession sharedSession] linkFromController:self];
  148. }
  149. #endif
  150. - (void)openLoginSetupVC
  151. {
  152. // remove any message
  153. [[NSNotificationCenter defaultCenter] removeObserver:self];
  154. CCLogin *viewController = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:@"CCLogin"];
  155. dispatch_async(dispatch_get_main_queue(), ^ {
  156. [self presentViewController:viewController animated:YES completion:nil];
  157. });
  158. }
  159. #pragma --------------------------------------------------------------------------------------------
  160. #pragma mark === Modify Account ===
  161. #pragma --------------------------------------------------------------------------------------------
  162. - (void)modifyAccount:(XLFormRowDescriptor *)sender
  163. {
  164. NSString *vcName;
  165. [self deselectFormRow:sender];
  166. [app cancelAllOperations];
  167. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  168. /*** NEXTCLOUD OWNCLOUD ***/
  169. if ([app.typeCloud isEqualToString:typeCloudNextcloud] || [app.typeCloud isEqualToString:typeCloudOwnCloud]) {
  170. if ([app.typeCloud isEqualToString:typeCloudNextcloud])
  171. vcName = @"CCLoginNextcloud";
  172. if ([app.typeCloud isEqualToString:typeCloudOwnCloud])
  173. vcName = @"CCLoginOwnCloud";
  174. CCLoginNCOC *loginVC = [[UIStoryboard storyboardWithName:@"CCLogin" bundle:nil] instantiateViewControllerWithIdentifier:vcName];
  175. [loginVC setModifyOnlyPassword:YES];
  176. [loginVC setTypeCloud:app.typeCloud];
  177. [self presentViewController:loginVC animated:YES completion:nil];
  178. }
  179. #ifdef CC
  180. /*** DROPBOX ***/
  181. if ([app.typeCloud isEqualToString:typeCloudDropbox]) {
  182. [[DBSession sharedSession] linkFromController:self];
  183. }
  184. #endif
  185. [self UpdateForm];
  186. }
  187. #pragma --------------------------------------------------------------------------------------------
  188. #pragma mark === Delete Account ===
  189. #pragma --------------------------------------------------------------------------------------------
  190. -(void)actionSheet:(UIActionSheet *)actionSheet clickedButtonAtIndex:(NSInteger)buttonIndex
  191. {
  192. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  193. NSString *accountNow = pickerAccount.rowDescriptor.value;
  194. NSArray *listAccount = [CCCoreData getAllAccount];
  195. [actionSheet dismissWithClickedButtonIndex:buttonIndex animated:YES];
  196. if (buttonIndex == 0 && actionSheet.tag == actionSheetCancellaAccount) {
  197. [app cancelAllOperations];
  198. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  199. #ifdef CC
  200. /*** DROPBOX ***/
  201. // Dropbox unlink UID
  202. if ([app.typeCloud isEqualToString:typeCloudDropbox])
  203. [[DBSession sharedSession] unlinkUserId:app.activeUID];
  204. #endif
  205. [self deleteAccount:accountNow];
  206. // Clear active user
  207. [app settingActiveAccount:nil activeUrl:nil activeUser:nil activePassword:nil activeUID:nil activeAccessToken:nil typeCloud:nil];
  208. listAccount = [CCCoreData getAllAccount];
  209. if ([listAccount count] > 0) [self ChangeDefaultAccount:listAccount[0]];
  210. else {
  211. [self openLoginSetupVC];
  212. return;
  213. }
  214. }
  215. }
  216. - (void)deleteAccount:(NSString *)account
  217. {
  218. [CCCoreData deleteAccount:account];
  219. NSPredicate *predicate = [NSPredicate predicateWithFormat:@"(account == %@)", account];
  220. [CCCoreData deleteMetadataWithPredicate:predicate];
  221. [CCCoreData deleteLocalFileWithPredicate:predicate];
  222. [CCCoreData deleteDirectoryFromPredicate:predicate];
  223. }
  224. - (void)answerDelAccount:(XLFormRowDescriptor *)sender
  225. {
  226. [self deselectFormRow:sender];
  227. UIActionSheet *actionSheet1 = [[UIActionSheet alloc] initWithTitle:NSLocalizedString(@"_want_delete_",nil)
  228. delegate:self
  229. cancelButtonTitle:NSLocalizedString(@"_no_delete_",nil)
  230. destructiveButtonTitle:NSLocalizedString(@"_yes_delete_",nil)
  231. otherButtonTitles:nil];
  232. actionSheet1.tag = actionSheetCancellaAccount;
  233. [actionSheet1 showInView:self.view.window.rootViewController.view];
  234. }
  235. #pragma --------------------------------------------------------------------------------------------
  236. #pragma mark === Change Default Account ===
  237. #pragma --------------------------------------------------------------------------------------------
  238. - (void)ChangeDefaultAccount:(NSString *)account
  239. {
  240. if ([app.netQueue operationCount] > 0 || [app.netQueueDownload operationCount] > 0 || [app.netQueueDownloadWWan operationCount] > 0 || [app.netQueueUpload operationCount] > 0 || [app.netQueueUploadWWan operationCount] > 0 || [app.netQueueUploadCameraAllPhoto operationCount] > 0) {
  241. [app messageNotification:@"_transfers_in_queue_" description:nil visible:YES delay:dismissAfterSecond type:TWMessageBarMessageTypeInfo];
  242. [self UpdateForm];
  243. return;
  244. }
  245. // removed this -> ?????
  246. [app cancelAllOperations];
  247. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  248. // removed this -> ?????
  249. // change account
  250. TableAccount *tableAccount = [CCCoreData setActiveAccount:account];
  251. if (tableAccount)
  252. [app settingActiveAccount:tableAccount.account activeUrl:tableAccount.url activeUser:tableAccount.user activePassword:tableAccount.password activeUID:tableAccount.uid activeAccessToken:tableAccount.token typeCloud:tableAccount.typeCloud];
  253. // Init home
  254. [[NSNotificationCenter defaultCenter] postNotificationName:@"initializeMain" object:nil];
  255. [self UpdateForm];
  256. }
  257. #pragma --------------------------------------------------------------------------------------------
  258. #pragma mark === Update Form ===
  259. #pragma --------------------------------------------------------------------------------------------
  260. - (void)UpdateForm
  261. {
  262. NSArray *listAccount = [CCCoreData getAllAccount];
  263. if (listAccount == nil) {
  264. [self openLoginSetupVC];
  265. return;
  266. }
  267. XLFormPickerCell *pickerAccount = (XLFormPickerCell *)[[self.form formRowWithTag:@"pickerAccount"] cellForFormController:self];
  268. pickerAccount.rowDescriptor.selectorOptions = listAccount;
  269. pickerAccount.rowDescriptor.value = app.activeAccount;
  270. [self.tableView reloadData];
  271. [self performSelector:@selector(reloadData) withObject:nil afterDelay:1];
  272. }
  273. - (void)reloadData
  274. {
  275. [self.tableView reloadData];
  276. }
  277. @end