CCAdvanced.m 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548
  1. //
  2. // CCManageHelp.m
  3. // Crypto Cloud Technology Nextcloud
  4. //
  5. // Created by Marino Faggiana on 06/11/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 "CCAdvanced.h"
  24. #import "CCUtility.h"
  25. #import "AppDelegate.h"
  26. #import "NCBridgeSwift.h"
  27. @interface CCAdvanced ()
  28. @end
  29. @implementation CCAdvanced
  30. -(id)init
  31. {
  32. XLFormDescriptor *form ;
  33. XLFormSectionDescriptor *section;
  34. XLFormRowDescriptor *row;
  35. [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(changeTheming) name:@"changeTheming" object:nil];
  36. form = [XLFormDescriptor formDescriptorWithTitle:NSLocalizedString(@"_advanced_", nil)];
  37. // Section ACTIVITY -------------------------------------------------
  38. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_activity_", nil)];
  39. [form addFormSection:section];
  40. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"activityVerboseHigh" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_help_activity_verbose_", nil)];
  41. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  42. [row.cellConfig setObject:[UIImage imageNamed:@"settingsActivityHigh"] forKey:@"imageView.image"];
  43. if ([CCUtility getActivityVerboseHigh]) row.value = @"1";
  44. else row.value = @"0";
  45. [section addFormRow:row];
  46. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"sendMailActivity" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_help_activity_mail_", nil)];
  47. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  48. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  49. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  50. [row.cellConfig setObject:[UIImage imageNamed:@"settingsSendActivity"] forKey:@"imageView.image"];
  51. row.action.formSelector = @selector(sendMail:);
  52. [section addFormRow:row];
  53. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"clearActivityLog" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_help_activity_clear_", nil)];
  54. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  55. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  56. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  57. [row.cellConfig setObject:[UIImage imageNamed:@"settingsClearActivity"] forKey:@"imageView.image"];
  58. row.action.formSelector = @selector(clearActivity:);
  59. [section addFormRow:row];
  60. // Section OTTIMIZATIONS -------------------------------------------------
  61. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_optimized_photos_", nil)];
  62. [form addFormSection:section];
  63. section.footerTitle = NSLocalizedString(@"_optimized_photos_how_", nil);
  64. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"optimizedphoto" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_optimized_photos_", nil)];
  65. if ([CCUtility getOptimizedPhoto]) row.value = @"1";
  66. else row.value = @"0";
  67. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  68. [section addFormRow:row];
  69. section = [XLFormSectionDescriptor formSectionWithTitle:NSLocalizedString(@"_upload_del_photos_", nil)];
  70. [form addFormSection:section];
  71. section.footerTitle = NSLocalizedString(@"_upload_del_photos_how_", nil);
  72. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"uploadremovephoto" rowType:XLFormRowDescriptorTypeBooleanSwitch title:NSLocalizedString(@"_upload_del_photos_", nil)];
  73. if ([CCUtility getUploadAndRemovePhoto]) row.value = @"1";
  74. else row.value = @"0";
  75. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  76. [section addFormRow:row];
  77. // Section CLEAR CACHE -------------------------------------------------
  78. section = [XLFormSectionDescriptor formSection];
  79. [form addFormSection:section];
  80. // Clear cache
  81. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"azzeracache" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_clear_cache_no_size_", nil)];
  82. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  83. [row.cellConfig setObject:[UIColor blackColor] forKey:@"textLabel.textColor"];
  84. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  85. [row.cellConfig setObject:[UIImage imageNamed:@"settingsClearCache"] forKey:@"imageView.image"];
  86. row.action.formSelector = @selector(clearCache:);
  87. [section addFormRow:row];
  88. // Section EXIT --------------------------------------------------------
  89. section = [XLFormSectionDescriptor formSection];
  90. [form addFormSection:section];
  91. // Exit
  92. row = [XLFormRowDescriptor formRowDescriptorWithTag:@"esci" rowType:XLFormRowDescriptorTypeButton title:NSLocalizedString(@"_exit_", nil)];
  93. [row.cellConfig setObject:@(NSTextAlignmentLeft) forKey:@"textLabel.textAlignment"];
  94. [row.cellConfig setObject:[UIColor redColor] forKey:@"textLabel.textColor"];
  95. [row.cellConfig setObject:[UIFont systemFontOfSize:15.0]forKey:@"textLabel.font"];
  96. [row.cellConfig setObject:[UIImage imageNamed:@"settingsExit"] forKey:@"imageView.image"];
  97. row.action.formSelector = @selector(exitNextcloud:);
  98. [section addFormRow:row];
  99. return [super initWithForm:form];
  100. }
  101. - (void)viewDidLoad
  102. {
  103. [super viewDidLoad];
  104. _hud = [[CCHud alloc] initWithView:[[[UIApplication sharedApplication] delegate] window]];
  105. }
  106. // Apparirà
  107. - (void)viewWillAppear:(BOOL)animated
  108. {
  109. [super viewWillAppear:animated];
  110. self.tableView.backgroundColor = [NCBrandColor sharedInstance].tableBackground;
  111. // Color
  112. [app aspectNavigationControllerBar:self.navigationController.navigationBar encrypted:NO online:[app.reachability isReachable] hidden:NO];
  113. [app aspectTabBar:self.tabBarController.tabBar hidden:NO];
  114. [self recalculateSize];
  115. }
  116. - (void)changeTheming
  117. {
  118. if (self.isViewLoaded && self.view.window)
  119. [app changeTheming:self];
  120. }
  121. - (void)formRowDescriptorValueHasChanged:(XLFormRowDescriptor *)rowDescriptor oldValue:(id)oldValue newValue:(id)newValue
  122. {
  123. [super formRowDescriptorValueHasChanged:rowDescriptor oldValue:oldValue newValue:newValue];
  124. if ([rowDescriptor.tag isEqualToString:@"activityVerboseHigh"]) {
  125. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  126. [CCUtility setActivityVerboseHigh:true];
  127. } else {
  128. [CCUtility setActivityVerboseHigh:false];
  129. }
  130. // Clear Date read Activity for force reload datasource
  131. app.activeActivity.storeDateFirstActivity = nil;
  132. }
  133. if ([rowDescriptor.tag isEqualToString:@"optimizedphoto"]) {
  134. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  135. [CCUtility setOptimizedPhoto:YES];
  136. } else {
  137. [CCUtility setOptimizedPhoto:NO];
  138. }
  139. }
  140. if ([rowDescriptor.tag isEqualToString:@"uploadremovephoto"]) {
  141. if ([[rowDescriptor.value valueData] boolValue] == YES) {
  142. [CCUtility setUploadAndRemovePhoto:YES];
  143. } else {
  144. [CCUtility setUploadAndRemovePhoto:NO];
  145. }
  146. }
  147. }
  148. #pragma --------------------------------------------------------------------------------------------
  149. #pragma mark === Mail ===
  150. #pragma --------------------------------------------------------------------------------------------
  151. - (void) mailComposeController:(MFMailComposeViewController *)vc didFinishWithResult:(MFMailComposeResult)result error:(NSError *)error
  152. {
  153. switch (result)
  154. {
  155. case MFMailComposeResultCancelled:
  156. [app messageNotification:@"_info_" description:@"_mail_deleted_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeSuccess errorCode: error.code];
  157. break;
  158. case MFMailComposeResultSaved:
  159. [app messageNotification:@"_info_" description:@"_mail_saved_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeSuccess errorCode: error.code];
  160. break;
  161. case MFMailComposeResultSent:
  162. [app messageNotification:@"_info_" description:@"_mail_sent_" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeSuccess errorCode: error.code];
  163. break;
  164. case MFMailComposeResultFailed: {
  165. NSString *msg = [NSString stringWithFormat:NSLocalizedString(@"_mail_failure_", nil), [error localizedDescription]];
  166. [app messageNotification:@"_error_" description:msg visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode: error.code];
  167. }
  168. break;
  169. default:
  170. break;
  171. }
  172. // Close the Mail Interface
  173. [self dismissViewControllerAnimated:YES completion:NULL];
  174. }
  175. - (void)sendMail:(XLFormRowDescriptor *)sender
  176. {
  177. [self deselectFormRow:sender];
  178. // Email Subject
  179. NSString *emailTitle = NSLocalizedString(@"_information_req_", nil);
  180. // Email Content
  181. NSString *messageBody;
  182. // File Attachment
  183. NSString *fileAttachment = @"";
  184. // Email Recipents
  185. NSArray *toRecipents;
  186. NSArray *activities = [[NCManageDatabase sharedInstance] getActivityWithPredicate:[NSPredicate predicateWithFormat:@"account = %@", app.activeAccount]];
  187. if ([activities count] == 0) {
  188. [app messageNotification:@"_info_" description:@"No activity found" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeInfo errorCode:0];
  189. return;
  190. }
  191. for (tableActivity *activity in activities) {
  192. NSString *date, *type, *actionFile, *note;
  193. date = [[NSDateFormatter localizedStringFromDate:activity.date dateStyle:NSDateFormatterShortStyle timeStyle:NSDateFormatterMediumStyle] stringByPaddingToLength:22 withString:@" " startingAtIndex:0];
  194. if ([activity.type isEqual: k_activityTypeInfo]) type = @"Info ";
  195. if ([activity.type isEqual: k_activityTypeSuccess]) type = @"Success";
  196. if ([activity.type isEqual: k_activityTypeFailure]) type = @"Failure";
  197. actionFile = [[NSString stringWithFormat:@"%@ %@", activity.action, activity.file] stringByPaddingToLength:100 withString:@" " startingAtIndex:0];
  198. if (activity.idActivity == 0) note = [NSString stringWithFormat:@"%@ Selector: %@", activity.note, activity.selector];
  199. else note = activity.note;
  200. note = [note stringByPaddingToLength:200 withString:@" " startingAtIndex:0];
  201. fileAttachment = [fileAttachment stringByAppendingString:[NSString stringWithFormat:@"| %@ | %@ | %@ | %@ |\n", date, type, actionFile, note]];
  202. }
  203. messageBody = [NSString stringWithFormat:@"\n\n\n%@ Version %@ (%@)", [NCBrandOptions sharedInstance].brand, [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"], [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleVersion"]];
  204. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  205. NSString *documentsDirectory = [paths objectAtIndex:0];
  206. NSError *error;
  207. // fix CCAdvanced.m line 276 2.17.2 (00005)
  208. if ([fileAttachment writeToFile:[documentsDirectory stringByAppendingPathComponent:@"activity.txt"] atomically:YES encoding:NSUTF8StringEncoding error:&error] && [MFMailComposeViewController canSendMail]) {
  209. toRecipents = [NSArray arrayWithObject:[NCBrandOptions sharedInstance].mailMe];
  210. MFMailComposeViewController *mc = [[MFMailComposeViewController alloc] init];
  211. mc.mailComposeDelegate = self;
  212. [mc setSubject:emailTitle];
  213. [mc setMessageBody:messageBody isHTML:NO];
  214. [mc setToRecipients:toRecipents];
  215. NSData *noteData = [NSData dataWithContentsOfFile:[documentsDirectory stringByAppendingPathComponent:@"activity.txt"]];
  216. [mc addAttachmentData:noteData mimeType:@"text/plain" fileName:@"activity.txt"];
  217. // Present mail view controller on screen
  218. [self presentViewController:mc animated:YES completion:NULL];
  219. } else {
  220. [app messageNotification:@"_error_" description:@"Impossible create file body" visible:YES delay:k_dismissAfterSecond type:TWMessageBarMessageTypeError errorCode:0];
  221. }
  222. }
  223. #pragma --------------------------------------------------------------------------------------------
  224. #pragma mark === Clear Activity ===
  225. #pragma --------------------------------------------------------------------------------------------
  226. - (void)clearActivity:(XLFormRowDescriptor *)sender
  227. {
  228. [self deselectFormRow:sender];
  229. [[NCManageDatabase sharedInstance] clearTable:[tableActivity class] account:app.activeAccount];
  230. [app.activeActivity reloadDatasource];
  231. }
  232. #pragma --------------------------------------------------------------------------------------------
  233. #pragma mark === Clear Cache ===
  234. #pragma --------------------------------------------------------------------------------------------
  235. - (void)clearCache:(XLFormRowDescriptor *)sender
  236. {
  237. [self deselectFormRow:sender];
  238. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:NSLocalizedString(@"_want_delete_cache_", nil) preferredStyle:UIAlertControllerStyleActionSheet];
  239. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  240. [self.hud visibleHudTitle:NSLocalizedString(@"_remove_cache_", nil) mode:MBProgressHUDModeIndeterminate color:nil];
  241. [app cancelAllOperations];
  242. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  243. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 3 * NSEC_PER_SEC),dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  244. [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  245. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  246. [[NCManageDatabase sharedInstance] clearTable:[tableActivity class] account:app.activeAccount];
  247. [[NCManageDatabase sharedInstance] clearTable:[tableAutoUpload class] account:app.activeAccount];
  248. [[NCManageDatabase sharedInstance] clearTable:[tableCapabilities class] account:app.activeAccount];
  249. [[NCManageDatabase sharedInstance] clearTable:[tableDirectory class] account:app.activeAccount];
  250. [[NCManageDatabase sharedInstance] clearTable:[tableExternalSites class] account:app.activeAccount];
  251. [[NCManageDatabase sharedInstance] clearTable:[tableGPS class] account:nil];
  252. [[NCManageDatabase sharedInstance] clearTable:[tableLocalFile class] account:app.activeAccount];
  253. [[NCManageDatabase sharedInstance] clearTable:[tableMetadata class] account:app.activeAccount];
  254. [[NCManageDatabase sharedInstance] clearTable:[tableShare class] account:app.activeAccount];
  255. [self emptyUserDirectoryUser:app.activeUser url:app.activeUrl];
  256. [self emptyLocalDirectory];
  257. NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
  258. for (NSString *file in tmpDirectory)
  259. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
  260. [self recalculateSize];
  261. dispatch_async(dispatch_get_main_queue(), ^{
  262. // Close HUD
  263. [self.hud hideHud];
  264. // Inizialized home
  265. [[NSNotificationCenter defaultCenter] postNotificationOnMainThreadName:@"initializeMain" object:nil];
  266. });
  267. });
  268. }]];
  269. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  270. }]];
  271. //if iPhone
  272. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  273. [self presentViewController:alertController animated:YES completion:nil];
  274. }
  275. //if iPad
  276. else {
  277. // Change Rect to position Popover
  278. UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:alertController];
  279. [popup presentPopoverFromRect:[self.tableView rectForRowAtIndexPath:[self.form indexPathOfFormRow:sender]] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  280. }
  281. }
  282. - (void)recalculateSize
  283. {
  284. dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
  285. self.form.delegate = nil;
  286. XLFormRowDescriptor *rowAzzeraCache = [self.form formRowWithTag:@"azzeracache"];
  287. NSString *size = [CCUtility transformedSize:[[self getUserDirectorySize] longValue]];
  288. rowAzzeraCache.title = [NSString stringWithFormat:NSLocalizedString(@"_clear_cache_", nil), size];
  289. //rowAzzeraCache.title = NSLocalizedString(@"_clear_cache_no_size_", nil);
  290. [self.tableView performSelectorOnMainThread:@selector(reloadData) withObject:nil waitUntilDone:NO];
  291. self.form.delegate = self;
  292. });
  293. }
  294. #pragma --------------------------------------------------------------------------------------------
  295. #pragma mark == Exit Nextcloud ==
  296. #pragma --------------------------------------------------------------------------------------------
  297. - (void)exitNextcloud:(XLFormRowDescriptor *)sender
  298. {
  299. [self deselectFormRow:sender];
  300. UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"" message:NSLocalizedString(@"_want_exit_", nil) preferredStyle:UIAlertControllerStyleActionSheet];
  301. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_ok_", nil) style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
  302. [self.hud visibleIndeterminateHud];
  303. dispatch_after(dispatch_time(DISPATCH_TIME_NOW, 0.01 * NSEC_PER_SEC), dispatch_get_main_queue(), ^(void) {
  304. [app cancelAllOperations];
  305. [[CCNetworking sharedNetworking] settingSessionsDownload:YES upload:YES taskStatus:k_taskStatusCancel activeAccount:app.activeAccount activeUser:app.activeUser activeUrl:app.activeUrl];
  306. [[NSURLCache sharedURLCache] setMemoryCapacity:0];
  307. [[NSURLCache sharedURLCache] setDiskCapacity:0];
  308. [[CCNetworking sharedNetworking] invalidateAndCancelAllSession];
  309. [[NCManageDatabase sharedInstance] removeDB];
  310. [CCUtility deleteAllChainStore];
  311. [self emptyDocumentsDirectory];
  312. [self emptyLibraryDirectory];
  313. [self emptyGroupApplicationSupport];
  314. NSArray* tmpDirectory = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:NSTemporaryDirectory() error:NULL];
  315. for (NSString *file in tmpDirectory)
  316. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@%@", NSTemporaryDirectory(), file] error:NULL];
  317. [self.hud hideHud];
  318. exit(0);
  319. });
  320. }]];
  321. [alertController addAction: [UIAlertAction actionWithTitle:NSLocalizedString(@"_cancel_", nil) style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
  322. }]];
  323. //if iPhone
  324. if (UI_USER_INTERFACE_IDIOM() == UIUserInterfaceIdiomPhone) {
  325. [self presentViewController:alertController animated:YES completion:nil];
  326. }
  327. //if iPad
  328. else {
  329. // Change Rect to position Popover
  330. UIPopoverController *popup = [[UIPopoverController alloc] initWithContentViewController:alertController];
  331. [popup presentPopoverFromRect:[self.tableView rectForRowAtIndexPath:[self.form indexPathOfFormRow:sender]] inView:self.view permittedArrowDirections:UIPopoverArrowDirectionAny animated:YES];
  332. }
  333. }
  334. #pragma --------------------------------------------------------------------------------------------
  335. #pragma mark == Utility ==
  336. #pragma --------------------------------------------------------------------------------------------
  337. - (void)emptyGroupApplicationSupport
  338. {
  339. NSString *file;
  340. NSURL *dirGroup = [[NSFileManager defaultManager] containerURLForSecurityApplicationGroupIdentifier:[NCBrandOptions sharedInstance].capabilitiesGroups];
  341. NSString *dirIniziale = [[dirGroup URLByAppendingPathComponent:appApplicationSupport] path];
  342. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  343. while (file = [enumerator nextObject])
  344. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  345. }
  346. - (void)emptyLibraryDirectory
  347. {
  348. NSString *file;
  349. NSString *dirIniziale;
  350. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSLibraryDirectory, NSUserDomainMask, YES);
  351. dirIniziale = [paths objectAtIndex:0];
  352. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  353. while (file = [enumerator nextObject])
  354. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  355. }
  356. - (void)emptyDocumentsDirectory
  357. {
  358. NSString *file;
  359. NSString *dirIniziale;
  360. NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  361. dirIniziale = [paths objectAtIndex:0];
  362. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  363. while (file = [enumerator nextObject])
  364. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  365. }
  366. - (void)emptyUserDirectoryUser:(NSString *)user url:(NSString *)url
  367. {
  368. NSString *file;
  369. NSString *dirIniziale;
  370. dirIniziale = [CCUtility getDirectoryActiveUser:user activeUrl:url];
  371. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  372. while (file = [enumerator nextObject]) {
  373. NSString *ext = [[file pathExtension] lowercaseString];
  374. // Do not remove ICO
  375. if ([ext isEqualToString:@"ico"])
  376. continue;
  377. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  378. }
  379. }
  380. - (void)emptyLocalDirectory
  381. {
  382. NSString *file;
  383. NSString *dirIniziale;
  384. dirIniziale = [CCUtility getDirectoryLocal];
  385. NSDirectoryEnumerator *enumerator = [[NSFileManager defaultManager] enumeratorAtPath:dirIniziale];
  386. while (file = [enumerator nextObject])
  387. [[NSFileManager defaultManager] removeItemAtPath:[NSString stringWithFormat:@"%@/%@", dirIniziale, file] error:nil];
  388. }
  389. - (NSNumber *)getUserDirectorySize
  390. {
  391. NSString *directoryUser = [CCUtility getDirectoryActiveUser:app.activeUser activeUrl:app.activeUrl];
  392. NSURL *directoryURL = [NSURL fileURLWithPath:directoryUser];
  393. unsigned long long count = 0;
  394. NSNumber *value = nil;
  395. if (! directoryURL) return 0;
  396. // Get dimension Document
  397. for (NSURL *url in [[NSFileManager defaultManager] enumeratorAtURL:directoryURL includingPropertiesForKeys:@[NSURLFileSizeKey] options:0 errorHandler:NULL]) {
  398. if ([url getResourceValue:&value forKey:NSURLFileSizeKey error:nil]) {
  399. count += [value longLongValue];
  400. } else {
  401. return nil;
  402. }
  403. }
  404. return @(count);
  405. }
  406. @end