]> git.saurik.com Git - uikittools.git/blob - uicache.mm
Unregister old apps.
[uikittools.git] / uicache.mm
1 #import <Foundation/Foundation.h>
2
3 #include <notify.h>
4 #include <sys/types.h>
5 #include <sys/stat.h>
6 #include <unistd.h>
7
8 #include <objc/runtime.h>
9
10 #include <MobileCoreServices/LSApplicationWorkspace.h>
11
12 @interface NSMutableArray (Cydia)
13 - (void) addInfoDictionary:(NSDictionary *)info;
14 @end
15
16 @implementation NSMutableArray (Cydia)
17
18 - (void) addInfoDictionary:(NSDictionary *)info {
19 [self addObject:info];
20 }
21
22 @end
23
24 @interface NSMutableDictionary (Cydia)
25 - (void) addInfoDictionary:(NSDictionary *)info;
26 @end
27
28 @implementation NSMutableDictionary (Cydia)
29
30 - (void) addInfoDictionary:(NSDictionary *)info {
31 NSString *bundle = [info objectForKey:@"CFBundleIdentifier"];
32 [self setObject:info forKey:bundle];
33 }
34
35 @end
36
37 int main() {
38 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
39
40 NSString *path([NSString stringWithFormat:@"%@/Library/Caches/com.apple.mobile.installation.plist", NSHomeDirectory()]);
41
42 Class $LSApplicationWorkspace(objc_getClass("LSApplicationWorkspace"));
43 LSApplicationWorkspace *workspace($LSApplicationWorkspace == nil ? nil : [$LSApplicationWorkspace defaultWorkspace]);
44
45 if (NSMutableDictionary *cache = [NSMutableDictionary dictionaryWithContentsOfFile:path]) {
46 NSFileManager *manager = [NSFileManager defaultManager];
47 NSError *error = nil;
48
49 NSMutableArray *bundles([NSMutableArray arrayWithCapacity:16]);
50
51 id system = [cache objectForKey:@"System"];
52 if (system == nil)
53 goto error;
54
55 [system removeAllObjects];
56
57 if (NSArray *apps = [manager contentsOfDirectoryAtPath:@"/Applications" error:&error]) {
58 for (NSString *app in apps)
59 if ([app hasSuffix:@".app"]) {
60 NSString *path = [@"/Applications" stringByAppendingPathComponent:app];
61 NSString *plist = [path stringByAppendingPathComponent:@"Info.plist"];
62
63 if (NSMutableDictionary *info = [NSMutableDictionary dictionaryWithContentsOfFile:plist]) {
64 if (NSString *bundle = [info objectForKey:@"CFBundleIdentifier"]) {
65 [bundles addObject:path];
66 [info setObject:path forKey:@"Path"];
67 [info setObject:@"System" forKey:@"ApplicationType"];
68 [system addInfoDictionary:info];
69 } else
70 fprintf(stderr, "%s missing CFBundleIdentifier", [app UTF8String]);
71 }
72 }
73 } else goto error;
74
75 [cache writeToFile:path atomically:YES];
76
77 if (workspace != nil)
78 for (NSString *bundle in bundles) {
79 [workspace unregisterApplication:[NSURL fileURLWithPath:bundle]];
80 [workspace registerApplication:[NSURL fileURLWithPath:bundle]];
81 }
82
83 if (false) error:
84 fprintf(stderr, "%s\n", error == nil ? strerror(errno) : [[error localizedDescription] UTF8String]);
85 } else fprintf(stderr, "cannot open cache file. incorrect user?\n");
86
87 notify_post("com.apple.mobile.application_installed");
88
89 [pool release];
90
91 return 0;
92 }