TrollStore/Shared/TSListControllerShared.m

223 lines
6.8 KiB
Mathematica
Raw Permalink Normal View History

2022-09-22 23:38:58 +08:00
#import "TSListControllerShared.h"
#import "TSUtil.h"
2022-10-30 06:45:30 +08:00
#import "TSPresentationDelegate.h"
2022-09-22 23:38:58 +08:00
@implementation TSListControllerShared
- (BOOL)isTrollStore
{
return YES;
}
- (NSString*)getTrollStoreVersion
{
if([self isTrollStore])
{
return [NSBundle.mainBundle objectForInfoDictionaryKey:@"CFBundleVersion"];
}
else
{
NSString* trollStorePath = trollStoreAppPath();
if(!trollStorePath) return nil;
NSBundle* trollStoreBundle = [NSBundle bundleWithPath:trollStorePath];
return [trollStoreBundle objectForInfoDictionaryKey:@"CFBundleVersion"];
}
}
- (void)downloadTrollStoreAndRun:(void (^)(NSString* localTrollStoreTarPath))doHandler
2022-09-22 23:38:58 +08:00
{
NSURL* trollStoreURL = [NSURL URLWithString:@"https://github.com/opa334/TrollStore/releases/latest/download/TrollStore.tar"];
NSURLRequest* trollStoreRequest = [NSURLRequest requestWithURL:trollStoreURL];
NSURLSessionDownloadTask* downloadTask = [NSURLSession.sharedSession downloadTaskWithRequest:trollStoreRequest completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error)
{
if(error)
{
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:@"Error" message:[NSString stringWithFormat:@"Error downloading TrollStore: %@", error] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* closeAction = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil];
[errorAlert addAction:closeAction];
dispatch_async(dispatch_get_main_queue(), ^
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate stopActivityWithCompletion:^
2022-09-22 23:38:58 +08:00
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate presentViewController:errorAlert animated:YES completion:nil];
2022-09-22 23:38:58 +08:00
}];
});
}
else
{
NSString* tarTmpPath = [NSTemporaryDirectory() stringByAppendingPathComponent:@"TrollStore.tar"];
[[NSFileManager defaultManager] removeItemAtPath:tarTmpPath error:nil];
2022-09-22 23:38:58 +08:00
[[NSFileManager defaultManager] copyItemAtPath:location.path toPath:tarTmpPath error:nil];
doHandler(tarTmpPath);
}
}];
2022-09-22 23:38:58 +08:00
[downloadTask resume];
}
2022-09-22 23:38:58 +08:00
- (void)_installTrollStoreComingFromUpdateFlow:(BOOL)update
{
if(update)
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate startActivity:@"Updating TrollStore"];
}
else
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate startActivity:@"Installing TrollStore"];
}
[self downloadTrollStoreAndRun:^(NSString* tmpTarPath)
{
int ret = spawnRoot(rootHelperPath(), @[@"install-trollstore", tmpTarPath], nil, nil);
[[NSFileManager defaultManager] removeItemAtPath:tmpTarPath error:nil];
if(ret == 0)
{
respring();
if([self isTrollStore])
{
exit(0);
2022-09-22 23:38:58 +08:00
}
else
{
dispatch_async(dispatch_get_main_queue(), ^
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate stopActivityWithCompletion:^
2022-09-22 23:38:58 +08:00
{
[self reloadSpecifiers];
2022-09-22 23:38:58 +08:00
}];
});
}
}
else
{
dispatch_async(dispatch_get_main_queue(), ^
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate stopActivityWithCompletion:^
{
UIAlertController* errorAlert = [UIAlertController alertControllerWithTitle:@"Error" message:[NSString stringWithFormat:@"Error installing TrollStore: trollstorehelper returned %d", ret] preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* closeAction = [UIAlertAction actionWithTitle:@"Close" style:UIAlertActionStyleDefault handler:nil];
[errorAlert addAction:closeAction];
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate presentViewController:errorAlert animated:YES completion:nil];
}];
});
}
2022-09-22 23:38:58 +08:00
}];
}
- (void)installTrollStorePressed
{
[self _installTrollStoreComingFromUpdateFlow:NO];
2022-09-22 23:38:58 +08:00
}
- (void)updateTrollStorePressed
{
[self _installTrollStoreComingFromUpdateFlow:YES];
2022-09-22 23:38:58 +08:00
}
- (void)rebuildIconCachePressed
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate startActivity:@"Rebuilding Icon Cache"];
2022-09-22 23:38:58 +08:00
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
spawnRoot(rootHelperPath(), @[@"refresh-all"], nil, nil);
2022-09-22 23:38:58 +08:00
dispatch_async(dispatch_get_main_queue(), ^
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate stopActivityWithCompletion:nil];
2022-09-22 23:38:58 +08:00
});
});
}
- (void)refreshAppRegistrationsPressed
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate startActivity:@"Refreshing"];
2022-09-22 23:38:58 +08:00
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
spawnRoot(rootHelperPath(), @[@"refresh"], nil, nil);
2022-09-22 23:38:58 +08:00
respring();
dispatch_async(dispatch_get_main_queue(), ^
{
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate stopActivityWithCompletion:nil];
2022-09-22 23:38:58 +08:00
});
});
}
- (void)uninstallPersistenceHelperPressed
{
if([self isTrollStore])
{
spawnRoot(rootHelperPath(), @[@"uninstall-persistence-helper"], nil, nil);
2022-09-22 23:38:58 +08:00
[self reloadSpecifiers];
}
else
{
UIAlertController* uninstallWarningAlert = [UIAlertController alertControllerWithTitle:@"Warning" message:@"Uninstalling the persistence helper will revert this app back to it's original state, you will however no longer be able to persistently refresh the TrollStore app registrations. Continue?" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[uninstallWarningAlert addAction:cancelAction];
UIAlertAction* continueAction = [UIAlertAction actionWithTitle:@"Continue" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action)
{
spawnRoot(rootHelperPath(), @[@"uninstall-persistence-helper"], nil, nil);
2022-09-22 23:38:58 +08:00
exit(0);
}];
[uninstallWarningAlert addAction:continueAction];
2022-10-30 06:45:30 +08:00
[TSPresentationDelegate presentViewController:uninstallWarningAlert animated:YES completion:nil];
2022-09-22 23:38:58 +08:00
}
}
- (void)handleUninstallation
{
if([self isTrollStore])
{
exit(0);
}
else
{
[self reloadSpecifiers];
}
}
2023-01-21 20:52:39 +08:00
- (NSMutableArray*)argsForUninstallingTrollStore
{
return @[@"uninstall-trollstore"].mutableCopy;
}
2022-09-22 23:38:58 +08:00
- (void)uninstallTrollStorePressed
{
2022-11-20 06:22:20 +08:00
UIAlertController* uninstallAlert = [UIAlertController alertControllerWithTitle:@"Uninstall" message:@"You are about to uninstall TrollStore, do you want to preserve the apps installed by it?" preferredStyle:UIAlertControllerStyleAlert];
2022-09-22 23:38:58 +08:00
2022-11-20 06:22:20 +08:00
UIAlertAction* uninstallAllAction = [UIAlertAction actionWithTitle:@"Uninstall TrollStore, Uninstall Apps" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action)
2022-09-22 23:38:58 +08:00
{
2023-01-21 20:52:39 +08:00
NSMutableArray* args = [self argsForUninstallingTrollStore];
2023-02-15 02:57:05 +08:00
spawnRoot(rootHelperPath(), args, nil, nil);
2022-09-22 23:38:58 +08:00
[self handleUninstallation];
}];
2022-11-20 06:22:20 +08:00
[uninstallAlert addAction:uninstallAllAction];
UIAlertAction* preserveAppsAction = [UIAlertAction actionWithTitle:@"Uninstall TrollStore, Preserve Apps" style:UIAlertActionStyleDestructive handler:^(UIAlertAction* action)
{
2023-01-21 20:52:39 +08:00
NSMutableArray* args = [self argsForUninstallingTrollStore];
[args addObject:@"preserve-apps"];
spawnRoot(rootHelperPath(), args, nil, nil);
2022-11-20 06:22:20 +08:00
[self handleUninstallation];
}];
[uninstallAlert addAction:preserveAppsAction];
UIAlertAction* cancelAction = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:nil];
[uninstallAlert addAction:cancelAction];
2022-09-22 23:38:58 +08:00
2022-11-20 06:22:20 +08:00
[TSPresentationDelegate presentViewController:uninstallAlert animated:YES completion:nil];
2022-09-22 23:38:58 +08:00
}
@end