forked from belkadan/Webmailer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
URLHandlerController.m
221 lines (179 loc) · 6.21 KB
/
URLHandlerController.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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#import "URLHandlerController.h"
static const NSSize kIconSize = {16, 16};
@interface ComBelkadanWebmailer_URLHandlerController ()
- (void)chooseAppPanelDidEnd:(NSOpenPanel *)openPanel returnCode:(NSInteger)returnCode contextInfo:(void *)unused;
@property(readwrite,copy) NSString *scheme;
@property(readwrite,copy) NSString *fallbackBundleID;
@end
@implementation ComBelkadanWebmailer_URLHandlerController
@synthesize selectedBundleID, fallbackBundleID, scheme, shouldFullRefresh;
- (void)awakeFromNib
{
numberOfExtraItems = [popup numberOfItems];
if (self.scheme) [self refresh:nil];
}
- (void)dealloc
{
[scheme release];
[fallbackBundleID release];
[selectedBundleID release];
[super dealloc];
}
- (void)setScheme:(NSString *)newScheme fallbackBundleID:(NSString *)fallback
{
NSParameterAssert(newScheme != nil);
NSParameterAssert(fallback != nil);
if (![self.scheme isEqual:newScheme] || ![self.fallbackBundleID isEqual:fallback])
{
self.scheme = newScheme;
self.fallbackBundleID = fallback;
[self refresh:nil];
}
}
#pragma mark -
- (NSMenuItem *)menuItemForApplicationWithID:(NSString *)identifier name:(NSString *)name icon:(NSImage *)icon
{
NSAssert(identifier, @"Must have an identifier.");
if (!name) name = identifier;
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:name action:@selector(chooseApp:) keyEquivalent:@""];
[item setRepresentedObject:[identifier lowercaseString]];
[item setTarget:self];
[item setImage:icon];
return [item autorelease];
}
- (NSMenuItem *)menuItemForApplicationWithID:(NSString *)identifier mustExist:(BOOL)mustExist
{
NSWorkspace *workspace = [NSWorkspace sharedWorkspace];
NSString *path = [workspace absolutePathForAppBundleWithIdentifier:identifier];
if (!path && mustExist) return nil;
NSString *name;
NSImage *icon;
if (path != nil)
{
name = [[NSFileManager defaultManager] displayNameAtPath:path];
icon = [workspace iconForFile:path];
[icon setSize:kIconSize];
}
else
{
name = identifier;
icon = nil;
}
return [self menuItemForApplicationWithID:identifier name:name icon:icon];
}
- (IBAction)refresh:(id)sender
{
static NSArray *sortByTitleDescriptors = nil;
if (sortByTitleDescriptors == nil)
{
// This is not thread-safe, but locking to protect this is overkill.
// The possible one-time leak is preferable.
NSSortDescriptor *sortByTitle = [[NSSortDescriptor alloc] initWithKey:@"title" ascending:YES];
sortByTitleDescriptors = [[NSArray alloc] initWithObjects:sortByTitle, nil];
[sortByTitle release];
}
NSMenu *menu = [[NSMenu alloc] init];
NSMutableArray *menuItems = [[NSMutableArray alloc] init];
BOOL foundCurrentIdentifier = NO;
// Get all the relevant applications.
NSString *currentIdentifier = self.selectedBundleID;
if (!currentIdentifier || self.shouldFullRefresh)
{
CFStringRef defaultHandler = LSCopyDefaultHandlerForURLScheme((CFStringRef) self.scheme);
currentIdentifier = [NSMakeCollectable(defaultHandler) autorelease];
if (!currentIdentifier) currentIdentifier = self.fallbackBundleID;
}
NSArray *appIDs = (NSArray *) LSCopyAllHandlersForURLScheme((CFStringRef) self.scheme);
if (!appIDs)
{
appIDs = (NSArray *) CFArrayCreate(NULL, (const void **)¤tIdentifier, 1, &kCFTypeArrayCallBacks);
}
// Create menu items for all the applications.
for (NSString *nextID in appIDs)
{
NSMenuItem *item = [self menuItemForApplicationWithID:nextID mustExist:YES];
if (item)
{
[menuItems addObject:item];
// LaunchServices returns lowercase identifiers!
if (!foundCurrentIdentifier && [nextID compare:currentIdentifier options:NSCaseInsensitiveSearch] == NSOrderedSame)
{
foundCurrentIdentifier = YES;
}
}
}
CFRelease(appIDs);
// Sort the menu items, then add them to the menu.
[menuItems sortUsingDescriptors:sortByTitleDescriptors];
for (NSMenuItem *item in menuItems)
{
[menu addItem:item];
}
[menu addItem:[NSMenuItem separatorItem]];
[menuItems release];
// If we happen to have a selected item that didn't show up in LaunchServices,
// add it after the separator.
if (!foundCurrentIdentifier)
{
[menu addItem:[self menuItemForApplicationWithID:currentIdentifier mustExist:NO]];
}
// Transfer over any non-app items from the old menu.
NSInteger extraItemIndex = [popup numberOfItems] - numberOfExtraItems;
for (NSInteger i = 0; i < numberOfExtraItems; ++i) {
NSMenuItem *extraItem = [[popup itemAtIndex:extraItemIndex] retain];
[popup removeItemAtIndex:extraItemIndex];
[menu addItem:extraItem];
[extraItem release];
}
// Finally, put the new menu in place on the popup button.
[popup setMenu:menu];
[menu release];
self.selectedBundleID = currentIdentifier;
}
#pragma mark -
- (void)setSelectedBundleID:(NSString *)newID
{
[selectedBundleID autorelease];
selectedBundleID = [newID lowercaseString];
NSInteger index = [popup indexOfItemWithRepresentedObject:selectedBundleID];
if (index != -1)
{
[popup selectItemAtIndex:index];
}
else
{
[self refresh:nil];
}
}
- (IBAction)chooseApp:(id)sender
{
self.selectedBundleID = [sender representedObject];
}
- (IBAction)chooseOther:(id)sender
{
NSOpenPanel *openPanel = [NSOpenPanel openPanel];
[openPanel setResolvesAliases:YES];
[openPanel setAllowsMultipleSelection:NO];
[openPanel beginSheetForDirectory:nil file:nil types:[NSArray arrayWithObject:@"app"] modalForWindow:[popup window] modalDelegate:self didEndSelector:@selector(chooseAppPanelDidEnd:returnCode:contextInfo:) contextInfo:NULL];
}
- (void)chooseAppPanelDidEnd:(NSOpenPanel *)openPanel returnCode:(NSInteger)returnCode contextInfo:(void *)unused
{
if (returnCode == NSOKButton)
{
NSString *path = [openPanel filename];
NSBundle *bundle = [[NSBundle alloc] initWithPath:path];
NSString *bundleID = [bundle bundleIdentifier];
NSString *appName = [[NSFileManager defaultManager] displayNameAtPath:path];
NSImage *icon = [[NSWorkspace sharedWorkspace] iconForFile:path];
[icon setSize:kIconSize];
NSMenuItem *item = [self menuItemForApplicationWithID:bundleID name:appName icon:icon];
[[popup menu] insertItem:item atIndex:[popup numberOfItems] - numberOfExtraItems];
self.selectedBundleID = bundleID;
[bundle release];
}
else
{
[popup selectItemAtIndex:[popup indexOfItemWithRepresentedObject:self.selectedBundleID]];
}
}
@end