CCAdvanced.m 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 CLEAR CACHE -------------------------------------------------
  72. section = [XLFormSectionDescriptor formSection];
  73. [form addFormSection:section];
  74. section.footerTitle = NSLocalizedString(@"_clear_cache_footer_", nil);
  75. // Clear cache
  76. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"azzeracache" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_clear_cache_", nil)];
  77. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  78. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  79. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  80. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"delete"] multiplier:2 color:[NCBrandColor sharedInstance].icon] forKey:@"imageView.image"];
  81. row.action.formSelector = @selector(clearCache:);
  82. [section addFormRow:row];
  83. // Section EXIT --------------------------------------------------------
  84. section = [XLFormSectionDescriptor formSection];
  85. [form addFormSection:section];
  86. section.footerTitle = NSLocalizedString(@"_exit_footer_", nil);
  87. // Exit
  88. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"esci" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_exit_", nil)];
  89. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  90. [row.cellConfig setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  91. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  92. [row.cellConfig setObject:[CCGraphics changeThemingColorImage:[UIImage imageNamed:@"exit"] multiplier:2 color:[UIColor redColor]] forKey:@"imageView.image"];
  93. row.action.formSelector = @selector(exitNextcloud:);
  94. [section addFormRow:row];
  95. return [super initWithForm:form];
  96. }
  97. - (void)viewDidLoad
  98. {
  99. [super viewDidLoad];
  100. _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  101. }
  102. // Apparirà
  103. - (void)viewWillAppear:(BOOL)animated
  104. {
  105. [super viewWillAppear:animated];
  106. self.tableView.backgroundColor = [NCBrandColor sharedInstance].backgroundView;
  107. self.tableView.showsVerticalScrollIndicator = NO;
  108. // Color
  109. [appDelegate aspectNavigationControllerBar:self.navigationController.navigationBar online:[appDelegate.reachability isReachable] hidden:NO];
  110. [appDelegate aspectTabBar:self.tabBarController.tabBar hidden:NO];
  111. }
  112. - (void)changeTheming
  113. {
  114. if (self.isViewLoaded && self.view.window)
  115. [appDelegate changeTheming:self];
  116. }
  117. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  118. {
  119. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  120. if ([rowDescriptor.tag isEqualToString:@"showHiddenFiles"]) {
  121. [CCUtility setShowHiddenFiles:[[rowDescriptor.value valueData] boolValue]];
  122. // force reload
  123. [[NCManageDatabase sharedInstance] setClearAllDateReadDirectory];
  124. }
  125. if ([rowDescriptor.tag isEqualToString:@"formatCompatibility"]) {
  126. [CCUtility setFormatCompatibility:[[rowDescriptor.value valueData] boolValue]];
  127. }
  128. if ([rowDescriptor.tag isEqualToString:@"disablefilesapp"]) {
  129. [CCUtility setDisableFilesApp:[[rowDescriptor.value valueData] boolValue]];
  130. }
  131. }
  132. #pragma --------------------------------------------------------------------------------------------
  133. #pragma mark === Clear Cache ===
  134. #pragma --------------------------------------------------------------------------------------------
  135. - (void)removeAllFilesWithDB:(BOOL)withDB
  136. {
  137. [appDelegate maintenanceMode:YES];
  138. [self.hud visibleHudTitle:NSLocalizedString(@"_remove_cache_", nil) mode:MBProgressHUDModeIndeterminate color:nil];
  139. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.5 * NSEC_PER_SEC),dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  140. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorage] error:nil];
  141. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryUserData] error:nil];
  142. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryScan] error:nil];
  143. [self emptyDocumentsDirectory];
  144. NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
  145. for (NSString *file in tmpDirectory)
  146. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
  147. [KTVHTTPCache cacheDeleteAllCaches];
  148. //[self recalculateSize];
  149. // Clear Database
  150. [[NCManageDatabase sharedInstance] clearTable:[tableLocalFile class] account:appDelegate.activeAccount];
  151. if (withDB) {
  152. [[NCManageDatabase sharedInstance] clearTable:[tableActivity class] account:appDelegate.activeAccount];
  153. [[NCManageDatabase sharedInstance] clearTable:[tableActivitySubjectRich class] account:appDelegate.activeAccount];
  154. [[NCManageDatabase sharedInstance] clearTable:[tableActivityPreview class] account:appDelegate.activeAccount];
  155. [[NCManageDatabase sharedInstance] clearTable:[tableCapabilities class] account:appDelegate.activeAccount];
  156. [[NCManageDatabase sharedInstance] clearTable:[tableDirectory class] account:appDelegate.activeAccount];
  157. [[NCManageDatabase sharedInstance] clearTable:[tableE2eEncryption class] account:appDelegate.activeAccount];
  158. [[NCManageDatabase sharedInstance] clearTable:[tableExternalSites class] account:appDelegate.activeAccount];
  159. [[NCManageDatabase sharedInstance] clearTable:[tableGPS class] account:nil];
  160. [[NCManageDatabase sharedInstance] clearTable:[tableMetadata class] account:appDelegate.activeAccount];
  161. [[NCManageDatabase sharedInstance] clearTable:[tableMedia class] account:appDelegate.activeAccount];
  162. [[NCManageDatabase sharedInstance] clearTable:[tablePhotoLibrary class] account:appDelegate.activeAccount];
  163. [[NCManageDatabase sharedInstance] clearTable:[tableShare class] account:appDelegate.activeAccount];
  164. [[NCAutoUpload sharedInstance] alignPhotoLibrary];
  165. [appDelegate.filterFileID removeAllObjects];
  166. }
  167. [appDelegate maintenanceMode:NO];
  168. dispatch_async(dispatch_get_main_queue(), ^{
  169. // Close HUD
  170. [self.hud hideHud];
  171. // Inizialized home
  172. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil userInfo:nil];
  173. });
  174. });
  175. }
  176. - (void)clearCache:(XLFormRowDescriptor *)sender
  177. {
  178. [self deselectFormRow:sender];
  179. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:NSLocalizedString(@"_want_delete_cache_", nil) preferredStyle:UIAlertControllerStyleActionSheet];
  180. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_yes_", nil)
  181. style:UIAlertActionStyleDefault
  182. handler:^(UIAlertAction *action) {
  183. [self removeAllFilesWithDB:NO];
  184. }]];
  185. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_delete_cache_and_db_", nil)
  186. style:UIAlertActionStyleDefault
  187. handler:^(UIAlertAction *action) {
  188. [self removeAllFilesWithDB:YES];
  189. }]];
  190. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  191. }]];
  192. alertController.popoverPresentationController.sourceView = self.view;
  193. NSIndexPath *indexPath = [self.form indexPathOfFormRow:sender];
  194. CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
  195. alertController.popoverPresentationController.sourceRect = CGRectOffset(cellRect, -self.tableView.contentOffset.x, -self.tableView.contentOffset.y);
  196. [self presentViewController:alertController animated:YES completion:nil];
  197. }
  198. #pragma --------------------------------------------------------------------------------------------
  199. #pragma mark == Exit Nextcloud ==
  200. #pragma --------------------------------------------------------------------------------------------
  201. - (void)exitNextcloud:(XLFormRowDescriptor *)sender
  202. {
  203. [self deselectFormRow:sender];
  204. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:NSLocalizedString(@"_want_exit_", nil) preferredStyle:UIAlertControllerStyleActionSheet];
  205. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  206. [self.hud visibleIndeterminateHud];
  207. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.1 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  208. [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  209. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  210. [[CCNetworking sharedNetworking] invalidateAndCancelAllSession];
  211. [[NCManageDatabase sharedInstance] removeDB];
  212. [CCUtility deleteAllChainStore];
  213. [[NSFileManager defaultManager] removeItemAtPath:[CCUtility getDirectoryProviderStorage] error:nil];
  214. [self emptyDocumentsDirectory];
  215. [self emptyLibraryDirectory];
  216. [self emptyGroupApplicationSupport];
  217. NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
  218. for (NSString *file in tmpDirectory)
  219. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
  220. [self.hud hideHud];
  221. exit(0);
  222. });
  223. }]];
  224. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  225. }]];
  226. alertController.popoverPresentationController.sourceView = self.view;
  227. NSIndexPath *indexPath = [self.form indexPathOfFormRow:sender];
  228. CGRect cellRect = [self.tableView rectForRowAtIndexPath:indexPath];
  229. alertController.popoverPresentationController.sourceRect = CGRectOffset(cellRect, -self.tableView.contentOffset.x, -self.tableView.contentOffset.y);
  230. [self presentViewController:alertController animated:YES completion:nil];
  231. }
  232. #pragma --------------------------------------------------------------------------------------------
  233. #pragma mark == Utility ==
  234. #pragma --------------------------------------------------------------------------------------------
  235. - (void)emptyGroupApplicationSupport
  236. {
  237. NSString *file;
  238. NSURL *dirGroup = [CCUtility getDirectoryGroup];
  239. NSString *dirIniziale = [[dirGroup URLByAppendingPathComponent:k_appApplicationSupport] path];
  240. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  241. while (file = [enumerator nextObject])
  242. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  243. }
  244. - (void)emptyLibraryDirectory
  245. {
  246. NSString *file;
  247. NSString *dirIniziale;
  248. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  249. dirIniziale = [paths objectAtIndex:0];
  250. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  251. while (file = [enumerator nextObject])
  252. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  253. }
  254. - (void)emptyDocumentsDirectory
  255. {
  256. NSString *file;
  257. NSString *dirIniziale;
  258. dirIniziale = [CCUtility getDirectoryDocuments];
  259. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  260. while (file = [enumerator nextObject])
  261. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  262. }
  263. @end