William Bowling is sharing code with you
Bitbucket is a code hosting site. Unlimited public and private repositories. Free for small teams.
Don't show this again1 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 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 | //
// AIURLHandlerAdvancedPreferences.m
// Adium
//
// Created by Zachary West on 2009-04-02.
//
#import "AIURLHandlerAdvancedPreferences.h"
#import "AIPreferenceWindowController.h"
#import <AIUtilities/AIImageTextCell.h>
#import <AIUtilities/AIImageAdditions.h>
#import <AIUtilities/AIArrayAdditions.h>
#import <AIUtilities/AIMenuAdditions.h>
#import <AIUtilities/AIImageDrawingAdditions.h>
#import <Adium/AIAccountControllerProtocol.h>
#import <Adium/AIService.h>
#import <Adium/AIServiceIcons.h>
@interface AIURLHandlerAdvancedPreferences()
- (void)configureTableView;
- (void)initializeServiceInformationForSchemes:(NSArray *)schemes;
- (NSMenu *)applicationMenuForScheme:(NSString *)scheme;
- (NSArray *)applicationDictionaryArrayForScheme:(NSString *)scheme;
- (NSImage *)serviceImageForScheme:(NSString *)scheme;
- (NSString *)serviceNameForScheme:(NSString *)scheme;
@end
@implementation AIURLHandlerAdvancedPreferences
#pragma mark Preference pane settings
- (AIPreferenceCategory)category
{
return AIPref_Advanced;
}
- (NSString *)label{
return AILocalizedString(@"Default Client",nil);
}
- (NSString *)nibName{
return @"AIURLHandlerPreferences";
}
- (NSImage *)image{
return [NSImage imageNamed:@"pref-accounts" forClass:[AIPreferenceWindowController class]];
}
- (void)viewDidLoad
{
[servicesList release];
servicesList = [((AIURLHandlerPlugin *)plugin).uniqueSchemes retain];
[self configureTableView];
[self initializeServiceInformationForSchemes:servicesList];
[button_setDefault setLocalizedString:AILocalizedString(@"Set Default for All", nil)];
[checkBox_enforceDefault setLocalizedString:AILocalizedString(@"Always set Adium as the default", nil)];
[checkBox_enforceDefault setState:[[adium.preferenceController preferenceForKey:PREF_KEY_ENFORCE_DEFAULT
group:GROUP_URL_HANDLING] boolValue]];
[super viewDidLoad];
}
- (void)dealloc
{
[servicesList release];
[services release];
[super dealloc];
}
#pragma mark Actions
- (IBAction)setDefault:(id)sender
{
[plugin setAdiumAsDefault];
[tableView reloadData];
}
- (IBAction)enforceDefault:(id)sender
{
[adium.preferenceController setPreference:[NSNumber numberWithBool:[sender state]]
forKey:PREF_KEY_ENFORCE_DEFAULT
group:GROUP_URL_HANDLING];
if ([sender state]) {
[plugin setAdiumAsDefault];
}
}
#pragma mark Scheme information
- (void)initializeServiceInformationForSchemes:(NSArray *)schemes
{
[services release]; services = [[NSMutableDictionary alloc] init];
for (NSString *scheme in schemes) {
[services setObject:[NSMutableDictionary dictionary] forKey:scheme];
}
}
- (NSMenu *)applicationMenuForScheme:(NSString *)scheme
{
NSMutableDictionary *servicesInformation = [services objectForKey:scheme];
NSMenu *menu = [servicesInformation objectForKey:@"applicationsMenu"];
if (!menu) {
menu = [[[NSMenu alloc] init] autorelease];
for (NSDictionary *application in [self applicationDictionaryArrayForScheme:scheme]) {
NSMenuItem *menuItem = [menu addItemWithTitle:[application objectForKey:@"ApplicationName"]
target:nil
action:nil
keyEquivalent:@""];
[menuItem setImage:[[application objectForKey:@"ApplicationImage"] imageByScalingForMenuItem]];
[menuItem setRepresentedObject:[application objectForKey:@"BundleID"]];
}
[servicesInformation setObject:menu forKey:@"applicationsMenu"];
}
return menu;
}
- (NSArray *)applicationDictionaryArrayForScheme:(NSString *)scheme
{
NSMutableDictionary *servicesInformation = [services objectForKey:scheme];
NSArray *applications = [servicesInformation objectForKey:@"applications"];
if (!applications) {
NSArray *applicationArray = [(NSArray *)LSCopyAllHandlersForURLScheme((CFStringRef)scheme) autorelease];
NSMutableArray *mutableApplications = [NSMutableArray array];
for (NSString *bundleID in applicationArray) {
// File System Ref for this bundle ID
FSRef fileSystemRef;
OSStatus err = LSFindApplicationForInfo(kLSUnknownCreator, (CFStringRef)bundleID, NULL, &fileSystemRef, NULL);
if (err == kLSApplicationNotFoundErr) {
return nil;
}
// Application Name
HFSUniStr255 name;
OSErr informationError = FSGetCatalogInfo(&fileSystemRef, kFSCatInfoNone, NULL, &name, NULL, NULL);
if (informationError) {
return nil;
}
NSString *applicationName = [NSString stringWithCharacters:name.unicode length:name.length];
// Application Image
IconRef iconRef;
err = GetIconRefFromFileInfo(&fileSystemRef, 0, NULL, kFSCatInfoNone, NULL, kIconServicesNoBadgeFlag, &iconRef, NULL);
if (err) {
return nil;
}
NSImage *image = [[[NSImage alloc] initWithIconRef:iconRef] autorelease];
[mutableApplications addObject:[NSDictionary dictionaryWithObjectsAndKeys:bundleID.lowercaseString, @"BundleID",
applicationName, @"ApplicationName",
image, @"ApplicationImage", nil]];
}
[servicesInformation setObject:mutableApplications forKey:@"applications"];
applications = mutableApplications;
}
return applications;
}
- (NSImage *)serviceImageForScheme:(NSString *)scheme
{
NSMutableDictionary *servicesInformation = [services objectForKey:scheme];
NSImage *image = [servicesInformation objectForKey:@"image"];
if (!image) {
AIService *service = [adium.accountController firstServiceWithServiceID:[plugin serviceIDForScheme:scheme]];
image = [AIServiceIcons serviceIconForService:service type:AIServiceIconLarge direction:AIIconNormal];
[servicesInformation setObject:image forKey:@"image"];
}
return image;
}
- (NSString *)serviceNameForScheme:(NSString *)scheme
{
NSMutableDictionary *servicesInformation = [services objectForKey:scheme];
NSString *longServiceName = [servicesInformation objectForKey:@"name"];
if (!longServiceName) {
AIService *service = [adium.accountController firstServiceWithServiceID:[plugin serviceIDForScheme:scheme]];
longServiceName = [service longDescription];
[servicesInformation setObject:longServiceName forKey:@"name"];
}
return longServiceName;
}
#pragma mark Table view Delegate
- (void)configureTableView
{
AIImageTextCell *imageTextCell = [[AIImageTextCell alloc] init];
[imageTextCell setFont:[NSFont systemFontOfSize:[NSFont smallSystemFontSize]]];
[[tableView tableColumnWithIdentifier:@"service"] setDataCell:imageTextCell];
[imageTextCell release];
}
- (NSInteger)numberOfRowsInTableView:(NSTableView *)aTableView
{
return servicesList.count;
}
- (void)tableView:(NSTableView *)tableView willDisplayCell:(id)cell forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *identifier = tableColumn.identifier;
NSString *scheme = [servicesList objectAtIndex:row];
if ([identifier isEqualToString:@"service"]) {
// Configure to display the service icon and service name.
[cell setImage:[self serviceImageForScheme:scheme]];
} else if ([identifier isEqualToString:@"applications"]) {
NSMenu *menu = [self applicationMenuForScheme:scheme];
NSString *defaultApplication = [plugin defaultApplicationBundleIDForScheme:scheme];
// Letting the NSPopupButtonCell handle state causes some buggy results. Do it ourself.
for (NSMenuItem *menuItem in menu.itemArray) {
[menuItem setState:[menuItem.representedObject isEqualToString:defaultApplication]];
}
[cell setMenu:menu];
[cell setAltersStateOfSelectedItem:NO];
[cell selectItemAtIndex:[cell indexOfItemWithRepresentedObject:defaultApplication]];
}
}
- (id)tableView:(NSTableView *)aTableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *identifier = tableColumn.identifier;
NSString *scheme = [servicesList objectAtIndex:row];
if ([identifier isEqualToString:@"service"]) {
return [self serviceNameForScheme:scheme];
}
return nil;
}
- (void)tableView:(NSTableView *)aTableView setObjectValue:(id)object forTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
NSString *identifier = tableColumn.identifier;
NSString *scheme = [servicesList objectAtIndex:row];
if ([identifier isEqualToString:@"applications"]) {
[plugin setDefaultForScheme:scheme
toBundleID:[[[self applicationMenuForScheme:scheme] itemAtIndex:[object integerValue]] representedObject]];
}
}
@end
|