/
CTFWhitelist.m
208 lines (162 loc) · 6.39 KB
/
CTFWhitelist.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
/*
The MIT License
Copyright (c) 2008-2009 ClickToFlash Developers
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#import "CTFWhitelist.h"
#import "CTFUtilities.h"
#import "CTFMenubarMenuController.h"
#import "CTFUserDefaultsController.h"
#import "CTFPreferencesDictionary.h"
// NSNotification names
static NSString *sCTFWhitelistAdditionMade = @"CTFWhitelistAdditionMade";
// CTFUserDefaultsController keys
static NSString *sHostSiteInfoDefaultsKey = @"siteInfo";
typedef enum {
CTFSiteKindWhitelist = 0
} CTGSiteKind;
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( Whitelist )
- (void) _migrateWhitelist
{
// Migrate from the old location to the new location. We'll leave
// this in for a couple builds (being added for 1.4) and then remove
// it assuming those who care would have upgraded.
NSUserDefaults* defaults = [ NSUserDefaults standardUserDefaults ];
id oldWhitelist = [ defaults objectForKey: @"ClickToFlash.whitelist" ];
if( oldWhitelist ) {
id newWhitelist = [ defaults objectForKey: sHostSiteInfoDefaultsKey ];
if( newWhitelist == nil ) {
NSMutableArray* newWhitelist = [ NSMutableArray arrayWithCapacity: [ oldWhitelist count ] ];
CTFForEachObject( NSString, site, oldWhitelist ) {
[ newWhitelist addObject: whitelistItemForSite( site ) ];
}
[ defaults setObject: newWhitelist forKey: sHostSiteInfoDefaultsKey ];
}
[ defaults removeObjectForKey: @"ClickToFlash.whitelist"];
}
}
- (void) _addWhitelistObserver
{
[[NSNotificationCenter defaultCenter] addObserver: self
selector: @selector( _whitelistAdditionMade: )
name: sCTFWhitelistAdditionMade
object: nil];
}
- (void) _alertDone
{
[ _activeAlert release ];
_activeAlert = nil;
}
- (void) _abortAlert
{
if( _activeAlert ) {
[ NSApp endSheet: [ _activeAlert window ] returnCode: NSAlertSecondButtonReturn ];
[ self _alertDone ];
}
}
- (void) _askToAddCurrentSiteToWhitelist
{
NSString *title = NSLocalizedString(@"Always load Flash for this site?", @"Always load Flash for this site? alert title");
NSString *message = [NSString stringWithFormat:NSLocalizedString(@"Add %@ to the whitelist?", @"Add <sitename> to the whitelist? alert message"), [self host]];
NSAlert *alert = [[NSAlert alloc] init];
[alert addButtonWithTitle:NSLocalizedString(@"Add to Whitelist", @"Add to Whitelist button")];
[alert addButtonWithTitle:NSLocalizedString(@"Cancel", @"Cancel button")];
[alert setMessageText:title];
[alert setInformativeText:message];
[alert setAlertStyle:NSInformationalAlertStyle];
[alert beginSheetModalForWindow:[self window]
modalDelegate:self
didEndSelector:@selector(_addToWhitelistAlertDidEnd:returnCode:contextInfo:)
contextInfo:nil];
_activeAlert = alert;
}
- (void) _addToWhitelistAlertDidEnd: (NSAlert *)alert returnCode: (int)returnCode contextInfo: (void *)contextInfo
{
if (returnCode == NSAlertFirstButtonReturn)
{
[self _addHostToWhitelist];
}
[ self _alertDone ];
}
- (BOOL) _isHostWhitelisted
{
// Nil hosts whitelisted by default (e.g. Dashboard)
if (![self host])
{
return YES;
}
return [self _isWhiteListedForHostString: [self host]];
}
- (BOOL) _isWhiteListedForHostString:(NSString *)hostString
{
NSArray *hostWhitelist = [[CTFUserDefaultsController standardUserDefaults] arrayForKey: sHostSiteInfoDefaultsKey];
return hostWhitelist && itemForSite(hostWhitelist, hostString) != nil;
}
- (NSMutableArray *) _mutableSiteInfo
{
NSMutableArray *hostWhitelist = [[[[CTFUserDefaultsController standardUserDefaults] arrayForKey: sHostSiteInfoDefaultsKey] mutableCopy] autorelease];
if (hostWhitelist == nil) {
hostWhitelist = [NSMutableArray array];
}
return hostWhitelist;
}
- (void) _addHostToWhitelist
{
NSMutableArray *siteInfo = [self _mutableSiteInfo];
[siteInfo addObject: whitelistItemForSite([self host])];
[[CTFUserDefaultsController standardUserDefaults] setValue:siteInfo forKeyPath:@"values.siteInfo"];
[[NSNotificationCenter defaultCenter] postNotificationName: sCTFWhitelistAdditionMade object: self];
}
- (void) _whitelistAdditionMade: (NSNotification*) notification
{
if ([self _isHostWhitelisted])
[self _convertTypesForContainer];
}
- (IBAction)addToWhitelist:(id)sender;
{
if ([self _isHostWhitelisted])
return;
[self _addHostToWhitelist];
}
- (IBAction) editWhitelist: (id)sender;
{
[ [ CTFMenubarMenuController sharedController ] showSettingsWindow: self ];
}
@end