CCAdvanced.m 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. //
  2. // CCManageHelp.m
  3. // Nextcloud iOS
  4. //
  5. // Created by Marino Faggiana on 06/11/15.
  6. // Copyright (c) 2017 Marino Faggiana. All rights reserved.
  7. //
  8. // Author Marino Faggiana <marino.faggiana@nextcloud.com>
  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 "CCAdvanced.h"
  24. #import "CCUtility.h"
  25. #import "AppDelegate.h"
  26. #import <KTVHTTPCache/KTVHTTPCache.h>
  27. #import "NCBridgeSwift.h"
  28. @interface CCAdvanced ()
  29. {
  30. AppDelegate *appDelegate;
  31. }
  32. @end
  33. @implementation CCAdvanced
  34. - (id)init
  35. {
  36. XLFormDescriptor *form ;
  37. XLFormSectionDescriptor *section;
  38. XLFormRowDescriptor *row;
  39. appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
  40. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  41. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_advanced_", nil)];
  42. // Section HIDDEN FILES -------------------------------------------------
  43. section = [XLFormSectionDescriptor formSection];
  44. [form addFormSection:section];
  45. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"showHiddenFiles" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_show_hidden_files_", nil)];
  46. if ([CCUtility getShowHiddenFiles]) row.value = @"1";
  47. else row.value = @"0";
  48. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  49. [section addFormRow:row];
  50. // Format Compatibility
  51. section = [XLFormSectionDescriptor formSection];
  52. [form addFormSection:section];
  53. section.footerTitle = NSLocalizedString(@"_format_compatibility_footer_", nil);
  54. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"formatCompatibility" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_format_compatibility_", nil)];
  55. if ([CCUtility getFormatCompatibility]) row.value = @"1";
  56. else row.value = @"0";
  57. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  58. [section addFormRow:row];
  59. // Section : Files App --------------------------------------------------------------
  60. if (![NCBrandOptions sharedInstance].disable_openin_file) {
  61. section = [XLFormSectionDescriptor formSection];
  62. [form addFormSection:section];
  63. section.footerTitle = NSLocalizedString(@"_disable_files_app_footer_", nil);
  64. // Disable Files App
  65. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"disablefilesapp" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_disable_files_app_", nil)];
  66. if ([CCUtility getDisableFilesApp]) row.value = @"1";
  67. else row.value = @"0";
  68. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  69. [section addFormRow:row];
  70. }
  71. // Section : Privacy --------------------------------------------------------------
  72. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_privacy_", nil)];
  73. [form addFormSection:section];
  74. section.footerTitle = NSLocalizedString(@"_privacy_footer_", nil);
  75. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"crashservice" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_crashservice_title_", nil)];
  76. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  77. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"crashservice"] width:50 height:50 color:[NCBrandColor sharedInstance].icon] forKey:@"imageView.image"];
  78. if ([CCUtility getDisableCrashservice]) row.value = @"1";
  79. else row.value = @"0";
  80. [section addFormRow:row];
  81. // Section CLEAR CACHE -------------------------------------------------
  82. section = [XLFormSectionDescriptor formSection];
  83. [form addFormSection:section];
  84. section.footerTitle = NSLocalizedString(@"_clear_cache_footer_", nil);
  85. // Clear cache
  86. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"azzeracache" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_clear_cache_", nil)];
  87. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  88. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  89. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  90. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"trash"] width:50 height:50 color:[NCBrandColor sharedInstance].icon] forKey:@"imageView.image"];
  91. row.action.formSelector = @selector(clearCache:);
  92. [section addFormRow:row];
  93. // Section EXIT --------------------------------------------------------
  94. section = [XLFormSectionDescriptor formSection];
  95. [form addFormSection:section];
  96. section.footerTitle = NSLocalizedString(@"_exit_footer_", nil);
  97. // Exit
  98. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"esci" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_exit_", nil)];
  99. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  100. [row.cellConfig setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  101. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  102. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"exit"] width:50 height:50 color:[UIColor redColor]] forKey:@"imageView.image"];
  103. row.action.formSelector = @selector(exitNextcloud:);
  104. [section addFormRow:row];
  105. return [super initWithForm:form];
  106. }
  107. - (void)viewDidLoad
  108. {
  109. [super viewDidLoad];
  110. _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  111. }
  112. // Apparirà
  113. - (void)viewWillAppear:(BOOL)animated
  114. {
  115. [super viewWillAppear:animated];
  116. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  117. self.tableView.showsVerticalScrollIndicator = NO;
  118. // Color
  119. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  120. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  121. }
  122. - (void)changeTheming
  123. {
  124. if (self.isViewLoaded && self.view.window)
  125. [appDelegate changeTheming:self];
  126. }
  127. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  128. {
  129. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  130. if ([rowDescriptor.tag isEqualToString:@"showHiddenFiles"]) {
  131. [CCUtility setShowHiddenFiles:[[rowDescriptor.value valueData] boolValue]];
  132. // force reload
  133. [[NCManageDatabase sharedInstance] setClearAllDateReadDirectory];
  134. }
  135. if ([rowDescriptor.tag isEqualToString:@"formatCompatibility"]) {
  136. [CCUtility setFormatCompatibility:[[rowDescriptor.value valueData] boolValue]];
  137. }
  138. if ([rowDescriptor.tag isEqualToString:@"disablefilesapp"]) {
  139. [CCUtility setDisableFilesApp:[[rowDescriptor.value valueData] boolValue]];
  140. }
  141. if ([rowDescriptor.tag isEqualToString:@"crashservice"]) {
  142. [CCUtility setDisableCrashservice:[[rowDescriptor.value valueData] boolValue]];
  143. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:NSLocalizedString(@"_crashservice_title_", nil) message:NSLocalizedString(@"_crashservice_alert_", nil) preferredStyle:UIAlertControllerStyleAlert];
  144. UIAlertAction *okAction = [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
  145. exit(0);
  146. }];
  147. [alertController addAction:okAction];
  148. [self presentViewController:alertController animated:YES completion:nil];
  149. }
  150. }
  151. #pragma --------------------------------------------------------------------------------------------
  152. #pragma mark === Clear Cache ===
  153. #pragma --------------------------------------------------------------------------------------------
  154. - (void)removeAllFilesWithDB:(BOOL)withDB
  155. {
  156. [appDelegate maintenanceMode:YES];
  157. [self.hud visibleHudTitle:NSLocalizedString(@"_remove_cache_", nil) mode:MBProgressHUDModeIndeterminate color:nil];
  158. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC),dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  159. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorage] error:nil];
  160. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryUserData] error:nil];
  161. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryScan] error:nil];
  162. [self emptyDocumentsDirectory];
  163. NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
  164. for (NSString *file in tmpDirectory)
  165. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
  166. [KTVHTTPCache cacheDeleteAllCaches];
  167. //[self recalculateSize];
  168. // Clear Database
  169. [[NCManageDatabase sharedInstance] clearTable:[tableLocalFile class] account:appDelegate.activeAccount];
  170. if (withDB) {
  171. [[NCManageDatabase sharedInstance] clearTable:[tableActivity class] account:appDelegate.activeAccount];
  172. [[NCManageDatabase sharedInstance] clearTable:[tableActivitySubjectRich class] account:appDelegate.activeAccount];
  173. [[NCManageDatabase sharedInstance] clearTable:[tableActivityPreview class] account:appDelegate.activeAccount];
  174. [[NCManageDatabase sharedInstance] clearTable:[tableCapabilities class] account:appDelegate.activeAccount];
  175. [[NCManageDatabase sharedInstance] clearTable:[tableDirectory class] account:appDelegate.activeAccount];
  176. [[NCManageDatabase sharedInstance] clearTable:[tableE2eEncryption class] account:appDelegate.activeAccount];
  177. [[NCManageDatabase sharedInstance] clearTable:[tableExternalSites class] account:appDelegate.activeAccount];
  178. [[NCManageDatabase sharedInstance] clearTable:[tableGPS class] account:nil];
  179. [[NCManageDatabase sharedInstance] clearTable:[tableMetadata class] account:appDelegate.activeAccount];
  180. [[NCManageDatabase sharedInstance] clearTable:[tableMedia class] account:appDelegate.activeAccount];
  181. [[NCManageDatabase sharedInstance] clearTable:[tablePhotoLibrary class] account:appDelegate.activeAccount];
  182. [[NCManageDatabase sharedInstance] clearTable:[tableShare class] account:appDelegate.activeAccount];
  183. [[NCAutoUpload sharedInstance] alignPhotoLibrary];
  184. [appDelegate.filterFileID removeAllObjects];
  185. }
  186. [appDelegate maintenanceMode:NO];
  187. dispatch_async(dispatch_get_main_queue(), ^{
  188. // Close HUD
  189. [self.hud hideHud];
  190. // Inizialized home
  191. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil userInfo:nil];
  192. });
  193. });
  194. }
  195. - (void)clearCache:(XLFormRowDescriptor *)sender
  196. {
  197. [self deselectFormRow:sender];
  198. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:NSLocalizedString(@"_want_delete_cache_", nil) preferredStyle:UIAlertControllerStyleActionSheet];
  199. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil)
  200. style:UIAlertActionStyleDefault
  201. handler:^(UIAlertAction *action) {
  202. [self removeAllFilesWithDB:NO];
  203. }]];
  204. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_cache_and_db_", nil)
  205. style:UIAlertActionStyleDefault
  206. handler:^(UIAlertAction *action) {
  207. [self removeAllFilesWithDB:YES];
  208. }]];
  209. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  210. }]];
  211. alertController.popoverPresentationController.sourceView = self.view;
  212. NSIndexPath *indexPath = [self.form indexPathOfFormRow:sender];
  213. CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
  214. alertController.popoverPresentationController.sourceRect = CGRectOffset(cellRect, -self.tableView.contentOffset.x, -self.tableView.contentOffset.y);
  215. [self presentViewController:alertController animated:YES completion:nil];
  216. }
  217. #pragma --------------------------------------------------------------------------------------------
  218. #pragma mark == Exit Nextcloud ==
  219. #pragma --------------------------------------------------------------------------------------------
  220. - (void)exitNextcloud:(XLFormRowDescriptor *)sender
  221. {
  222. [self deselectFormRow:sender];
  223. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:NSLocalizedString(@"_want_exit_", nil) preferredStyle:UIAlertControllerStyleActionSheet];
  224. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  225. [self.hud visibleIndeterminateHud];
  226. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  227. [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  228. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  229. [[CCNetworking sharedNetworking] invalidateAndCancelAllSession];
  230. [[NCManageDatabase sharedInstance] removeDB];
  231. [CCUtility deleteAllChainStore];
  232. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorage] error:nil];
  233. [self emptyDocumentsDirectory];
  234. [self emptyLibraryDirectory];
  235. [self emptyGroupApplicationSupport];
  236. NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
  237. for (NSString *file in tmpDirectory)
  238. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
  239. [self.hud hideHud];
  240. exit(0);
  241. });
  242. }]];
  243. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  244. }]];
  245. alertController.popoverPresentationController.sourceView = self.view;
  246. NSIndexPath *indexPath = [self.form indexPathOfFormRow:sender];
  247. CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
  248. alertController.popoverPresentationController.sourceRect = CGRectOffset(cellRect, -self.tableView.contentOffset.x, -self.tableView.contentOffset.y);
  249. [self presentViewController:alertController animated:YES completion:nil];
  250. }
  251. #pragma --------------------------------------------------------------------------------------------
  252. #pragma mark == Utility ==
  253. #pragma --------------------------------------------------------------------------------------------
  254. - (void)emptyGroupApplicationSupport
  255. {
  256. NSString *file;
  257. NSURL *dirGroup = [CCUtility getDirectoryGroup];
  258. NSString *dirIniziale = [[dirGroup URLByAppendingPathComponent:k_appApplicationSupport] path];
  259. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  260. while (file = [enumerator nextObject])
  261. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  262. }
  263. - (void)emptyLibraryDirectory
  264. {
  265. NSString *file;
  266. NSString *dirIniziale;
  267. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  268. dirIniziale = [paths objectAtIndex:0];
  269. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  270. while (file = [enumerator nextObject])
  271. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  272. }
  273. - (void)emptyDocumentsDirectory
  274. {
  275. NSString *file;
  276. NSString *dirIniziale;
  277. dirIniziale = [CCUtility getDirectoryDocuments];
  278. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  279. while (file = [enumerator nextObject])
  280. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  281. }
  282. @end