-
Notifications
You must be signed in to change notification settings - Fork 33
/
CSFileTypeList.m
202 lines (162 loc) · 5.47 KB
/
CSFileTypeList.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
#import "CSFileTypeList.h"
@implementation CSFileTypeList
-(id)initWithCoder:(NSCoder *)coder
{
if(self=[super initWithCoder:coder])
{
[self setDataSource:[[CSFileTypeListSource alloc] init]];
}
return self;
}
-(id)initWithFrame:(NSRect)frame
{
if(self=[super initWithFrame:frame])
{
NSLog(@"Custom view mode in IB not supported yet");
[self setDataSource:[[CSFileTypeListSource alloc] init]];
}
return self;
}
-(void)dealloc
{
[[self dataSource] release];
[super dealloc];
}
-(IBAction)selectAll:(id)sender
{
[(CSFileTypeListSource *)[self dataSource] claimAllTypes];
[self reloadData];
}
-(IBAction)deselectAll:(id)sender
{
[(CSFileTypeListSource *)[self dataSource] surrenderAllTypes];
[self reloadData];
}
@end
@implementation CSFileTypeListSource:NSObject
-(id)init
{
if(self=[super init])
{
filetypes=[[self readFileTypes] retain];
}
return self;
}
-(void)dealloc
{
[filetypes release];
[super dealloc];
}
-(NSArray *)readFileTypes
{
NSMutableArray *array=[NSMutableArray array];
NSArray *types=[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleDocumentTypes"];
NSArray *hidden=[[NSBundle mainBundle] objectForInfoDictionaryKey:@"CSHiddenDocumentTypes"];
NSEnumerator *enumerator=[types objectEnumerator];
NSDictionary *dict;
while(dict=[enumerator nextObject])
{
NSArray *types=[dict objectForKey:@"LSItemContentTypes"];
if(types)
{
NSString *description=[dict objectForKey:@"CFBundleTypeName"];
NSString *extensions=[[dict objectForKey:@"CFBundleTypeExtensions"] componentsJoinedByString:@", "];
NSString *type=[types objectAtIndex:0];
if(!hidden||![hidden containsObject:type])
[array addObject:[NSDictionary dictionaryWithObjectsAndKeys:
type,@"type",
description,@"description",
extensions,@"extensions",
nil]];
}
}
return [NSArray arrayWithArray:array];
}
-(int)numberOfRowsInTableView:(NSTableView *)table
{
return [filetypes count];
}
-(id)tableView:(NSTableView *)table objectValueForTableColumn:(NSTableColumn *)column row:(int)row
{
NSString *ident=[column identifier];
if([ident isEqual:@"enabled"])
{
NSString *self_id=[[NSBundle mainBundle] bundleIdentifier];
NSString *type=[[filetypes objectAtIndex:row] objectForKey:@"type"];
NSString *handler=[(id)LSCopyDefaultRoleHandlerForContentType((CFStringRef)type,kLSRolesViewer) autorelease];
return [NSNumber numberWithBool:[self_id isEqual:handler]];
}
else
{
return [[filetypes objectAtIndex:row] objectForKey:ident];
}
}
-(void)tableView:(NSTableView *)table setObjectValue:(id)object forTableColumn:(NSTableColumn *)column row:(int)row
{
NSString *ident=[column identifier];
if([ident isEqual:@"enabled"])
{
NSString *type=[[filetypes objectAtIndex:row] objectForKey:@"type"];
if([object boolValue]) [self claimType:type];
else [self surrenderType:type];
}
}
-(void)claimAllTypes
{
NSEnumerator *enumerator=[filetypes objectEnumerator];
NSDictionary *type;
while(type=[enumerator nextObject]) [self claimType:[type objectForKey:@"type"]];
}
-(void)surrenderAllTypes
{
NSEnumerator *enumerator=[filetypes objectEnumerator];
NSDictionary *type;
while(type=[enumerator nextObject]) [self surrenderType:[type objectForKey:@"type"]];
}
-(void)claimType:(NSString *)type
{
NSString *self_id=[[NSBundle mainBundle] bundleIdentifier];
NSString *oldhandler=[(id)LSCopyDefaultRoleHandlerForContentType((CFStringRef)type,kLSRolesViewer) autorelease];
if(oldhandler&&![oldhandler isEqual:self_id]) [[NSUserDefaults standardUserDefaults] setObject:oldhandler
forKey:[@"oldHandler." stringByAppendingString:type]];
[self setHandler:self_id forType:type];
}
-(void)surrenderType:(NSString *)type
{
NSString *self_id=[[NSBundle mainBundle] bundleIdentifier];
NSString *oldhandler=[[NSUserDefaults standardUserDefaults] stringForKey:[@"oldHandler." stringByAppendingString:type]];
if(oldhandler&&![oldhandler isEqual:self_id]) [self setHandler:oldhandler forType:type];
else [self removeHandlerForType:type];
}
-(void)setHandler:(NSString *)handler forType:(NSString *)type
{
LSSetDefaultRoleHandlerForContentType((CFStringRef)type,kLSRolesViewer,(CFStringRef)handler);
}
-(void)removeHandlerForType:(NSString *)type
{
NSMutableArray *handlers=[NSMutableArray array];
NSString *self_id=[[NSBundle mainBundle] bundleIdentifier];
[handlers addObjectsFromArray:[(id)LSCopyAllRoleHandlersForContentType((CFStringRef)type,kLSRolesViewer) autorelease]];
[handlers addObjectsFromArray:[(id)LSCopyAllRoleHandlersForContentType((CFStringRef)type,kLSRolesViewer) autorelease]];
NSString *ext=[(id)UTTypeCopyPreferredTagWithClass((CFStringRef)type,kUTTagClassFilenameExtension) autorelease];
NSString *filename=[NSString stringWithFormat:@"/tmp/CSFileTypeList%04x.%@",rand()&0xffff,ext];
[[NSFileManager defaultManager] createFileAtPath:filename contents:nil attributes:nil];
NSArray *apps=[(NSArray *)LSCopyApplicationURLsForURL((CFURLRef)[NSURL fileURLWithPath:filename],kLSRolesAll) autorelease];
[[NSFileManager defaultManager] removeFileAtPath:filename handler:nil];
NSEnumerator *enumerator=[apps objectEnumerator];
NSURL *url;
while(url=[enumerator nextObject])
{
NSString *identifier=[[NSBundle bundleWithPath:[url path]] bundleIdentifier];
if(identifier) [handlers addObject:identifier];
}
for(;;)
{
int index=[handlers indexOfObject:self_id];
if(index==NSNotFound) break;
[handlers removeObjectAtIndex:index];
}
if([handlers count]) [self setHandler:[handlers objectAtIndex:0] forType:type];
else [self setHandler:@"__dummy__" forType:type];
}
@end