From 1da96792eecc4252c90c706576fbfc1f1afd3860 Mon Sep 17 00:00:00 2001 From: Otyr Ugla Date: Thu, 12 Feb 2009 09:28:42 -0600 Subject: [PATCH] Added an "Add" button to the bottom of the Whitelist table, wired up entirely with bindings. Changed format of whitelist pref to be site info array-of-dictionaries to support this (and reworked the migration code). Removed remnants of add/remove code that isn't needed now that bindings can do (most of) that work. --- Plugin/CTFWhitelistWindowController.h | 14 +- Plugin/CTFWhitelistWindowController.m | 62 +------ Plugin/English.lproj/WhitelistPanel.xib | 208 +++++++++++++++--------- Plugin/Plugin.m | 95 ++++++++--- 4 files changed, 213 insertions(+), 166 deletions(-) diff --git a/Plugin/CTFWhitelistWindowController.h b/Plugin/CTFWhitelistWindowController.h index 4ac1990d..e92224d1 100755 --- a/Plugin/CTFWhitelistWindowController.h +++ b/Plugin/CTFWhitelistWindowController.h @@ -3,13 +3,13 @@ @interface CTFWhitelistWindowController : NSWindowController { IBOutlet NSArrayController *_controller; - NSMutableArray *_sites; +// NSMutableArray *_sites; } - -- (IBAction) addWhitelistSite: (id) sender; -- (IBAction) removeWhitelistSite: (id) sender; - -- (void) saveWhitelist: (id) sender; -- (void) whitelistChanged: (NSNotification *) note; +// +//- (IBAction) addWhitelistSite: (id) sender; +//- (IBAction) removeWhitelistSite: (id) sender; +// +//- (void) saveWhitelist: (id) sender; +//- (void) whitelistChanged: (NSNotification *) note; @end diff --git a/Plugin/CTFWhitelistWindowController.m b/Plugin/CTFWhitelistWindowController.m index d5f8a0c1..00cb1e1e 100755 --- a/Plugin/CTFWhitelistWindowController.m +++ b/Plugin/CTFWhitelistWindowController.m @@ -1,75 +1,19 @@ #import "CTFWhitelistWindowController.h" -extern NSString *sHostWhitelistDefaultsKey; -extern NSString *sCTFWhitelistAdditionMade; - @implementation CTFWhitelistWindowController - (id)init { - NSBundle * myBundle = [NSBundle bundleForClass:[self class]]; - NSString * nibPath = [myBundle pathForResource:@"WhitelistPanel" ofType:@"nib"]; - if (nibPath == nil) - { + NSBundle *myBundle = [NSBundle bundleForClass:[self class]]; + NSString *nibPath = [myBundle pathForResource:@"WhitelistPanel" ofType:@"nib"]; + if (nibPath == nil) { [self dealloc]; return nil; } self = [super initWithWindowNibPath: nibPath owner: self]; - - [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(whitelistChanged:) name: sCTFWhitelistAdditionMade object: nil]; - _sites = [[NSMutableArray array] retain]; - - [self whitelistChanged: nil]; return self; } -- (void) dealloc { - [_sites release]; - [super dealloc]; -} - -- (void) whitelistChanged: (NSNotification *) note { - NSArray *currentSites = [[NSUserDefaults standardUserDefaults] valueForKey: sHostWhitelistDefaultsKey]; - NSEnumerator *enumerator = [currentSites objectEnumerator]; - NSString *site; - - [_sites removeAllObjects]; - - while (site = [enumerator nextObject]) { - [_sites addObject: [NSMutableDictionary dictionaryWithObject: site forKey: @"description"]]; - } - [_controller setContent: _sites]; -} - -- (IBAction) removeWhitelistSite: (id) sender { - [_controller remove: nil]; - [self saveWhitelist: nil]; -} - -- (IBAction) addWhitelistSite: (id) sender { - [_controller insertObject: [NSMutableDictionary dictionaryWithObject: @"" forKey: @"description"] atArrangedObjectIndex: _sites.count]; - [_controller setSelectionIndex: _sites.count - 1]; - [self saveWhitelist: nil]; -} - -- (void) saveWhitelist: (id) sender { - NSMutableArray *sites = [NSMutableArray array]; - NSEnumerator *enumerator = [_sites objectEnumerator]; - NSDictionary *site; - - while (site = [enumerator nextObject]) { - [sites addObject: [site valueForKey: @"description"]]; - } - - NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; - - [defaults setValue: sites forKey: sHostWhitelistDefaultsKey]; -} - -- (void) windowWillClose: (NSNotification *) notification { - [self saveWhitelist: nil]; -} - @end diff --git a/Plugin/English.lproj/WhitelistPanel.xib b/Plugin/English.lproj/WhitelistPanel.xib index 768aee34..7a1061fc 100755 --- a/Plugin/English.lproj/WhitelistPanel.xib +++ b/Plugin/English.lproj/WhitelistPanel.xib @@ -8,7 +8,7 @@ 353.00 YES - + YES @@ -36,15 +36,15 @@ NSApplication - 31 + 15 2 - {{80, 360}, {527, 343}} + {{80, 361}, {527, 342}} -534248448 Q2xpY2sgdG8gRmxhc2gg4oCUIFNldHRpbmdzA NSPanel {3.40282e+38, 3.40282e+38} - {420, 200} + {420, 250} 274 @@ -52,7 +52,7 @@ YES - 274 + 8466 YES @@ -63,26 +63,26 @@ 4352 - {470, 151} + {485, 151} YES 256 - {470, 17} + {485, 17} - 256 - {{471, 0}, {16, 17}} + -2147483392 + {{-26, 0}, {16, 17}} YES - 4.670000e+02 + 4.820000e+02 4.000000e+01 2.000000e+03 @@ -110,7 +110,7 @@ 337772096 - 2048 + 133120 Text Cell LucidaGrande @@ -153,7 +153,7 @@ 1.700000e+01 - 1388314624 + 1119879168 2 4 15 @@ -161,7 +161,7 @@ YES - {{1, 17}, {470, 151}} + {{1, 17}, {485, 151}} @@ -170,8 +170,8 @@ - 256 - {{471, 17}, {15, 151}} + -2147483392 + {{471, 17}, {15, 152}} _doScroller: @@ -195,7 +195,7 @@ YES - {{1, 0}, {470, 17}} + {{1, 0}, {485, 17}} @@ -207,7 +207,7 @@ {{20, 47}, {487, 169}} - 18 + 530 @@ -217,14 +217,14 @@ - 289 - {{345, 15}, {162, 26}} + 292 + {{82, 19}, {63, 26}} YES 67239424 134348800 - Remove from Whitelist + Remove -2038284033 @@ -235,15 +235,35 @@ 25 - + 292 - {{18, 20}, {283, 17}} + {{20, 19}, {63, 26}} + + YES + + 67239424 + 134348800 + Add + + + -2038284033 + 162 + + + 200 + 25 + + + + + 289 + {{227, 22}, {283, 17}} YES 67239424 - 4194304 + 71303168 Option-click Flash to add to whitelist @@ -596,16 +616,16 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA - {{65, 232}, {397, 115}} + {{65, 231}, {397, 115}} NSView - {527, 343} + {527, 342} {{0, 0}, {1280, 778}} - {420, 216} + {420, 272} {3.40282e+38, 3.40282e+38} ClickToFlash_settingsWindow @@ -615,10 +635,11 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA YES - description + site + kind - NSString YES + YES YES YES @@ -630,22 +651,6 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA YES - - - value: arrangedObjects.description - - - - - - value: arrangedObjects.description - value - arrangedObjects.description - 2 - - - 18 - window @@ -696,35 +701,76 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA - contentArray: values.ClickToFlash_whitelist + selectedTag: values.ClickToFlash_sifrMode + + + + + + selectedTag: values.ClickToFlash_sifrMode + selectedTag + values.ClickToFlash_sifrMode + 2 + + + 112 + + + + add: + + + + 115 + + + + contentArray: values.ClickToFlash_siteInfo - contentArray: values.ClickToFlash_whitelist + contentArray: values.ClickToFlash_siteInfo contentArray - values.ClickToFlash_whitelist + values.ClickToFlash_siteInfo + + NSHandlesContentAsCompoundValue + + 2 - 49 + 127 - selectedTag: values.ClickToFlash_sifrMode - - + value: arrangedObjects.site + + - - - selectedTag: values.ClickToFlash_sifrMode - selectedTag - values.ClickToFlash_sifrMode + + + value: arrangedObjects.site + value + arrangedObjects.site + + YES + + YES + NSAllowsEditingMultipleValuesSelection + NSNullPlaceholder + + + YES + + double-click to set + + 2 - 112 + 130 @@ -771,9 +817,10 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA YES - - + + + @@ -952,6 +999,20 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA + + 113 + + + YES + + + + + + 114 + + + @@ -979,6 +1040,8 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA 106.IBPluginDependency 107.IBPluginDependency 11.IBPluginDependency + 113.IBPluginDependency + 114.IBPluginDependency 12.IBPluginDependency 13.IBPluginDependency 14.IBPluginDependency @@ -1001,14 +1064,14 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilderKit com.apple.InterfaceBuilderKit - {{47, 390}, {527, 343}} + {{38, 380}, {527, 342}} com.apple.InterfaceBuilder.CocoaPlugin - {{47, 390}, {527, 343}} + {{38, 380}, {527, 342}} {196, 240} {{202, 428}, {480, 270}} - - {420, 200} + + {420, 250} com.apple.InterfaceBuilder.CocoaPlugin ToolTip @@ -1042,6 +1105,8 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin + com.apple.InterfaceBuilder.CocoaPlugin ToolTip @@ -1078,7 +1143,7 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA - 112 + 130 @@ -1086,21 +1151,6 @@ AAMAAAABAAEAAAFTAAMAAAAEAAAFwgAAAAAACAAIAAgACAABAAEAAQABA CTFWhitelistWindowController NSWindowController - - YES - - YES - addWhitelistSite: - removeWhitelistSite: - saveWhitelist: - - - YES - id - id - id - - _controller NSArrayController diff --git a/Plugin/Plugin.m b/Plugin/Plugin.m index e764ba18..f6dd7f6e 100644 --- a/Plugin/Plugin.m +++ b/Plugin/Plugin.m @@ -36,12 +36,12 @@ of this software and associated documentation files (the "Software"), to deal static NSString *sFlashNewMIMEType = @"application/futuresplash"; // NSUserDefaults keys - NSString *sHostWhitelistDefaultsKey = @"ClickToFlash_whitelist"; +static NSString *sHostSiteInfoDefaultsKey = @"ClickToFlash_siteInfo"; static NSString *sAllowSifrDefaultsKey = @"ClickToFlash_allowSifr"; static NSString *sUseYouTubeH264DefaultsKey = @"ClickToFlash_useYouTubeH264"; // NSNotification names - NSString *sCTFWhitelistAdditionMade = @"CTFWhitelistAdditionMade"; +static NSString *sCTFWhitelistAdditionMade = @"CTFWhitelistAdditionMade"; static NSString *sSifrModeDefaultsKey = @"ClickToFlash_sifrMode"; static NSString *sSifr2Test = @"sIFR != null && typeof sIFR == \"function\""; @@ -51,13 +51,17 @@ of this software and associated documentation files (the "Software"), to deal static NSString *sSifr2AddOnJSFilename = @"sifr2-addons"; static NSString *sSifr3AddOnJSFilename = @"sifr3-addons"; -typedef enum -{ +typedef enum { CTFSifrModeDoNothing = 0, CTFSifrModeAllowSifr = 1, CTFSifrModeDeSifr = 2 } CTFSifrMode; +typedef enum { + CTFSiteKindWhitelist = 0 +} CTGSiteKind; + + @interface CTFClickToFlashPlugin (Internal) - (void) _convertTypesForFlashContainer; - (void) _convertTypesForContainer; @@ -65,7 +69,7 @@ - (void) _replaceSelfWithElement: (DOMElement*) newElement; - (void) _drawBackground; - (BOOL) _isOptionPressed; - (BOOL) _isHostWhitelisted; -- (NSMutableArray *)_hostWhitelist; +- (NSMutableArray *) _mutableSiteInfo; - (void) _alertDone; - (void) _abortAlert; - (void) _addHostToWhitelist; @@ -83,6 +87,47 @@ - (void) _disableSIFR; @end +#pragma mark - +#pragma mark Whitelist Utility Functions + + + // Simple ForEach macro to make life easier on those porting to Tiger + // than using Leopard's fast enumeration and "in" keyword: +#define CTFForEachObject( Type, varName, container ) \ + NSEnumerator* feoEnum_##__LINE__ = [ container objectEnumerator ]; \ + Type* varName; \ + while( varName = [ feoEnum_##__LINE__ nextObject ] ) + +static NSUInteger indexOfItemForSite( NSArray* arr, NSString* site ) +{ + int i = 0; + CTFForEachObject( NSDictionary, item, arr ) { + if( [ [ item objectForKey: @"site" ] isEqualToString: site ] ) + return i; + ++i; + } + + return NSNotFound; +} + +static NSDictionary* itemForSite( NSArray* arr, NSString* site ) +{ + NSUInteger index = indexOfItemForSite( arr, site ); + + if( index != NSNotFound ) + return [ arr objectAtIndex: index ]; + + return nil; +} + +static NSDictionary* whitelistItemForSite( NSString* site ) +{ + return [ NSDictionary dictionaryWithObjectsAndKeys: site, @"site", + [ NSNumber numberWithInt: CTFSiteKindWhitelist ], @"kind", + nil ]; +} + + @implementation CTFClickToFlashPlugin @@ -98,6 +143,7 @@ + (NSView *)plugInViewWithArguments:(NSDictionary *)arguments #pragma mark - #pragma mark Initialization and Superclass Overrides + - (void) _migrateWhitelist { // Migrate from the old location to the new location. We'll leave @@ -108,12 +154,17 @@ - (void) _migrateWhitelist id oldWhitelist = [ defaults objectForKey: @"ClickToFlash.whitelist" ]; if( oldWhitelist ) { - id newWhitelist = [ defaults objectForKey: sHostWhitelistDefaultsKey ]; + id newWhitelist = [ defaults objectForKey: sHostSiteInfoDefaultsKey ]; if( newWhitelist == nil ) { - [ defaults setObject: oldWhitelist forKey: sHostWhitelistDefaultsKey ]; - [ defaults removeObjectForKey: @"ClickToFlash.whitelist"]; + NSMutableArray* newWhitelist = [ NSMutableArray arrayWithCapacity: [ oldWhitelist count ] ]; + CTFForEachObject( NSString, site, oldWhitelist ) { + [ newWhitelist addObject: whitelistItemForSite( site ) ]; + } + [ defaults setObject: newWhitelist forKey: sHostSiteInfoDefaultsKey ]; } + + [ defaults removeObjectForKey: @"ClickToFlash.whitelist"]; } } @@ -363,13 +414,13 @@ - (void)addToWhitelistAlertDidEnd:(NSAlert *)alert returnCode:(int)returnCode co - (BOOL) _isHostWhitelisted { - NSArray *hostWhitelist = [[NSUserDefaults standardUserDefaults] stringArrayForKey:sHostWhitelistDefaultsKey]; - return hostWhitelist && [hostWhitelist containsObject:self.host]; + NSArray *hostWhitelist = [[NSUserDefaults standardUserDefaults] arrayForKey:sHostSiteInfoDefaultsKey]; + return hostWhitelist && itemForSite(hostWhitelist, self.host) != nil; } -- (NSMutableArray *)_hostWhitelist +- (NSMutableArray *) _mutableSiteInfo { - NSMutableArray *hostWhitelist = [[[[NSUserDefaults standardUserDefaults] stringArrayForKey:sHostWhitelistDefaultsKey] mutableCopy] autorelease]; + NSMutableArray *hostWhitelist = [[[[NSUserDefaults standardUserDefaults] arrayForKey:sHostSiteInfoDefaultsKey] mutableCopy] autorelease]; if (hostWhitelist == nil) { hostWhitelist = [NSMutableArray array]; } @@ -378,17 +429,21 @@ - (NSMutableArray *)_hostWhitelist - (void) _addHostToWhitelist { - NSMutableArray *hostWhitelist = [self _hostWhitelist]; - [hostWhitelist addObject:self.host]; - [[NSUserDefaults standardUserDefaults] setObject:hostWhitelist forKey:sHostWhitelistDefaultsKey]; + NSMutableArray *siteInfo = [self _mutableSiteInfo]; + [siteInfo addObject: whitelistItemForSite(self.host)]; + [[NSUserDefaults standardUserDefaults] setObject: siteInfo forKey: sHostSiteInfoDefaultsKey]; [[NSNotificationCenter defaultCenter] postNotificationName: sCTFWhitelistAdditionMade object: self]; } - (void) _removeHostFromWhitelist { - NSMutableArray *hostWhitelist = [self _hostWhitelist]; - [hostWhitelist removeObject:self.host]; - [[NSUserDefaults standardUserDefaults] setObject:hostWhitelist forKey:sHostWhitelistDefaultsKey]; + NSMutableArray *siteInfo = [self _mutableSiteInfo]; + NSUInteger foundIndex = indexOfItemForSite(siteInfo, self.host); + + if(foundIndex != NSNotFound) { + [siteInfo removeObjectAtIndex: foundIndex]; + [[NSUserDefaults standardUserDefaults] setObject: siteInfo forKey: sHostSiteInfoDefaultsKey]; + } } - (void) _whitelistAdditionMade: (NSNotification*) notification @@ -649,9 +704,7 @@ - (NSDictionary*) _flashVarDictionary: (NSString*) flashvarString NSArray* args = [ flashvarString componentsSeparatedByString: @"&" ]; - NSEnumerator* objEnum = [ args objectEnumerator ]; - NSString* oneArg; - while( oneArg = [ objEnum nextObject ] ) { + CTFForEachObject( NSString, oneArg, args ) { NSRange sepRange = [ oneArg rangeOfString: @"=" ]; if( sepRange.location != NSNotFound ) { NSString* key = [ oneArg substringToIndex: sepRange.location ];