/
AboutWindowController.mm
330 lines (277 loc) · 11.3 KB
/
AboutWindowController.mm
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
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
#import "AboutWindowController.h"
#import <OakAppKit/OakUIConstructionFunctions.h>
#import <OakFoundation/OakFoundation.h>
#import <OakFoundation/NSString Additions.h>
#import <BundlesManager/BundlesManager.h>
#import <license/LicenseManager.h>
#import <ns/ns.h>
static NSString* const kUserDefaultsReleaseNotesDigestKey = @"releaseNotesDigest";
static NSData* Digest (NSString* someString)
{
char const* str = [someString UTF8String];
char md[CC_SHA1_DIGEST_LENGTH];
CC_SHA1((unsigned char*)str, strlen(str), (unsigned char*)md);
return [NSData dataWithBytes:md length:sizeof(md)];
}
// ============================
// = JavaScript Bridge Object =
// ============================
@interface AboutWindowJSBridge : NSObject
{
NSString* version;
NSString* copyright;
NSString* licensees;
}
- (void)addLicense;
@end
@implementation AboutWindowJSBridge
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector { return aSelector != @selector(addLicense); }
+ (BOOL)isKeyExcludedFromWebScript:(char const*)name { return strcmp(name, "version") != 0 && strcmp(name, "copyright") != 0 && strcmp(name, "licensees") != 0; }
+ (NSString*)webScriptNameForSelector:(SEL)aSelector { return NSStringFromSelector(aSelector); }
+ (NSString*)webScriptNameForKey:(char const*)name { return @(name); }
- (NSString*)version
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
}
- (NSString*)copyright
{
return [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NSHumanReadableCopyright"];
}
- (NSString*)licensees
{
return LicenseManager.sharedInstance.owner;
}
- (void)addLicense
{
[LicenseManager.sharedInstance showAddLicenseWindow:self];
}
@end
// ============================
@interface AboutWindowController () <NSWindowDelegate, NSToolbarDelegate, WebFrameLoadDelegate, WebPolicyDelegate>
@property (nonatomic) NSToolbar* toolbar;
@property (nonatomic) WebView* webView;
@property (nonatomic) NSString* selectedPage;
@end
@implementation AboutWindowController
+ (instancetype)sharedInstance
{
static AboutWindowController* sharedInstance = [self new];
return sharedInstance;
}
+ (void)showChangesIfUpdated
{
NSURL* url = [[NSBundle mainBundle] URLForResource:@"Changes" withExtension:@"html"];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^{
if(NSString* releaseNotes = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL])
{
NSData* lastDigest = [[NSUserDefaults standardUserDefaults] dataForKey:kUserDefaultsReleaseNotesDigestKey];
NSData* currentDigest = Digest(releaseNotes);
dispatch_async(dispatch_get_main_queue(), ^{
if(lastDigest && ![lastDigest isEqualToData:currentDigest])
[[AboutWindowController sharedInstance] showChangesWindow:self];
[[NSUserDefaults standardUserDefaults] setObject:currentDigest forKey:kUserDefaultsReleaseNotesDigestKey];
});
}
});
}
- (id)init
{
NSRect visibleRect = [[NSScreen mainScreen] visibleFrame];
NSRect rect = NSMakeRect(0, 0, std::min<CGFloat>(700, NSWidth(visibleRect)), std::min<CGFloat>(800, NSHeight(visibleRect)));
CGFloat dy = NSHeight(visibleRect) - NSHeight(rect);
rect.origin.y = round(NSMinY(visibleRect) + dy*3/4);
rect.origin.x = NSMaxY(visibleRect) - NSMaxY(rect);
NSWindow* win = [[NSWindow alloc] initWithContentRect:rect styleMask:(NSWindowStyleMaskTitled|NSWindowStyleMaskClosable|NSWindowStyleMaskResizable|NSWindowStyleMaskMiniaturizable) backing:NSBackingStoreBuffered defer:NO];
if((self = [super initWithWindow:win]))
{
self.toolbar = [[NSToolbar alloc] initWithIdentifier:@"About TextMate"];
[self.toolbar setAllowsUserCustomization:NO];
[self.toolbar setDisplayMode:NSToolbarDisplayModeLabelOnly];
[self.toolbar setDelegate:self];
[win setToolbar:self.toolbar];
NSView* contentView = [[NSView alloc] initWithFrame:NSZeroRect];
[win setTitle:@"About TextMate"];
[win setContentView:contentView];
[win setFrameAutosaveName:@"BundlesReleaseNotes"];
[win setDelegate:self];
[win setAutorecalculatesKeyViewLoop:YES];
self.webView = [[WebView alloc] initWithFrame:[contentView bounds]];
self.webView.translatesAutoresizingMaskIntoConstraints = NO;
self.webView.frameLoadDelegate = self;
self.webView.policyDelegate = self;
[contentView addSubview:self.webView];
NSString* const kAboutWindowPreferencesIdentifier = @"About Window Preferences Identifier";
WebPreferences* webViewPrefs = [[WebPreferences alloc] initWithIdentifier:kAboutWindowPreferencesIdentifier];
webViewPrefs.plugInsEnabled = NO;
webViewPrefs.usesPageCache = NO;
webViewPrefs.cacheModel = WebCacheModelDocumentViewer;
self.webView.preferencesIdentifier = kAboutWindowPreferencesIdentifier;
NSDictionary* views = @{ @"webView" : self.webView };
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[webView(>=200)]|" options:NSLayoutFormatAlignAllTop metrics:nil views:views]];
[contentView addConstraints:[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[webView(>=200)]|" options:NSLayoutFormatAlignAllLeading metrics:nil views:views]];
}
return self;
}
- (void)dealloc
{
_webView.policyDelegate = nil;
_webView.frameLoadDelegate = nil;
[[_webView mainFrame] stopLoading];
}
- (void)showAboutWindow:(id)sender
{
self.selectedPage = @"About";
[self showWindow:self];
}
- (void)showChangesWindow:(id)sender
{
self.selectedPage = @"Changes";
[self showWindow:self];
NSURL* url = [[NSBundle mainBundle] URLForResource:@"Changes" withExtension:@"html"];
if(NSString* releaseNotes = [NSString stringWithContentsOfURL:url encoding:NSUTF8StringEncoding error:NULL])
[[NSUserDefaults standardUserDefaults] setObject:Digest(releaseNotes) forKey:kUserDefaultsReleaseNotesDigestKey];
}
- (void)setSelectedPage:(NSString*)pageName
{
if(_selectedPage == pageName || [_selectedPage isEqualToString:pageName])
return;
_selectedPage = pageName;
NSDictionary* pages = @{
@"About" : @"About",
@"Changes" : @"Changes",
@"Bundles" : @"Bundles",
@"Registration" : @"Registration",
@"Legal" : @"Legal",
@"Contributions" : @"Contributions"
};
if(NSString* file = pages[pageName])
{
if(NSURL* url = [[NSBundle mainBundle] URLForResource:file withExtension:@"html"])
[[self.webView mainFrame] loadRequest:[NSURLRequest requestWithURL:url cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:60]];
[self.window setTitle:pageName];
[self.toolbar setSelectedItemIdentifier:pageName];
}
}
- (void)selectPageAtRelativeOffset:(NSInteger)offset
{
NSArray* allPages = [self toolbarSelectableItemIdentifiers:self.toolbar];
NSUInteger index = [allPages indexOfObject:self.selectedPage];
if(index != NSNotFound)
self.selectedPage = allPages[(index + allPages.count + offset) % allPages.count];
}
- (IBAction)selectNextTab:(id)sender { [self selectPageAtRelativeOffset:+1]; }
- (IBAction)selectPreviousTab:(id)sender { [self selectPageAtRelativeOffset:-1]; }
// ====================
// = Toolbar Delegate =
// ====================
- (void)didClickToolbarItem:(id)sender
{
NSString* identifier = nil;
if([sender respondsToSelector:@selector(itemIdentifier)])
identifier = [sender itemIdentifier];
else if([sender respondsToSelector:@selector(representedObject)])
identifier = [sender representedObject];
if(identifier)
self.selectedPage = identifier;
}
- (NSToolbarItem*)toolbar:(NSToolbar*)aToolbar itemForItemIdentifier:(NSString*)anIdentifier willBeInsertedIntoToolbar:(BOOL)flag
{
NSToolbarItem* res = [[NSToolbarItem alloc] initWithItemIdentifier:anIdentifier];
[res setLabel:anIdentifier];
[res setTarget:self];
[res setAction:@selector(didClickToolbarItem:)];
return res;
}
- (NSArray*)toolbarAllowedItemIdentifiers:(NSToolbar*)aToolbar
{
return @[ @"About", @"Changes", @"Bundles", NSToolbarFlexibleSpaceItemIdentifier, @"Registration", @"Legal", @"Contributions" ];
}
- (NSArray*)toolbarDefaultItemIdentifiers:(NSToolbar*)aToolbar
{
return [self toolbarAllowedItemIdentifiers:aToolbar];
}
- (NSArray*)toolbarSelectableItemIdentifiers:(NSToolbar*)aToolbar
{
return @[ @"About", @"Changes", @"Bundles", @"Registration", @"Legal", @"Contributions" ];
}
// ====================
- (void)updateSelectTabMenu:(NSMenu*)aMenu
{
if(![[self window] isKeyWindow])
{
[aMenu addItemWithTitle:@"No Tabs" action:@selector(nop:) keyEquivalent:@""];
return;
}
char key = 0;
for(NSString* label in [self toolbarSelectableItemIdentifiers:self.toolbar])
{
NSMenuItem* item = [aMenu addItemWithTitle:label action:@selector(didClickToolbarItem:) keyEquivalent:key < 9 ? [NSString stringWithFormat:@"%c", '0' + (++key % 10)] : @""];
[item setRepresentedObject:label];
[item setTarget:self];
[item setState:[label isEqualToString:[self.toolbar selectedItemIdentifier]] ? NSOnState : NSOffState];
}
}
// ====================
static NSDictionary* RemoveOldCommits (NSDictionary* src)
{
NSMutableDictionary* res = [src mutableCopy];
NSMutableArray* commits = [NSMutableArray array];
NSInteger year = [[[[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar] components:NSYearCalendarUnit fromDate:[NSDate date]] year];
NSMutableArray* years = [NSMutableArray array];
for(size_t i = year-2; i <= year; ++i)
[years addObject:[NSString stringWithFormat:@"%4zu-", i]];
for(NSDictionary* commit in src[@"commits"])
{
NSString* dateString = commit[@"date"];
for(NSString* prefix in years)
{
if([dateString hasPrefix:prefix]) // this is significantly faster than having to parse the date
[commits addObject:commit];
}
}
res[@"commits"] = commits;
return res;
}
- (void)webView:(WebView*)aWebView didFinishLoadForFrame:(WebFrame*)aFrame
{
if(![[self.toolbar selectedItemIdentifier] isEqualToString:@"Bundles"])
return;
bool first = true;
NSMutableString* str = [NSMutableString stringWithString:@"{\"bundles\":["];
for(Bundle* bundle in [BundlesManager sharedInstance].bundles)
{
if(!bundle.installed || !bundle.path)
continue;
NSError* err = NULL;
if(NSString* content = [NSString stringWithContentsOfFile:[bundle.path stringByAppendingPathComponent:@"Changes.json"] encoding:NSUTF8StringEncoding error:&err])
{
if(NSDictionary* obj = [NSJSONSerialization JSONObjectWithData:[content dataUsingEncoding:NSUTF8StringEncoding] options:0 error:&err])
{
if(NSData* data = [NSJSONSerialization dataWithJSONObject:RemoveOldCommits(obj) options:0 error:&err])
{
if(!std::exchange(first, false))
[str appendString:@","];
[str appendString:[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]];
continue;
}
}
}
NSLog(@"%@: %@", bundle.name, err.localizedDescription);
}
[str appendString:@"]}"];
WebScriptObject* scriptObject = [aWebView windowScriptObject];
[scriptObject callWebScriptMethod:@"setJSON" withArguments:@[ str ]];
}
- (void)webView:(WebView*)sender didClearWindowObject:(WebScriptObject*)windowScriptObject forFrame:(WebFrame*)frame
{
AboutWindowJSBridge* bridge = [[AboutWindowJSBridge alloc] init];
[windowScriptObject setValue:bridge forKey:@"TextMate"];
}
- (void)webView:(WebView*)sender decidePolicyForNavigationAction:(NSDictionary*)actionInformation request:(NSURLRequest*)request frame:(WebFrame*)frame decisionListener:(id <WebPolicyDecisionListener>)listener
{
if(![[request.URL scheme] isEqualToString:@"file"] && [[NSWorkspace sharedWorkspace] openURL:request.URL])
[listener ignore];
else if([NSURLConnection canHandleRequest:request])
[listener use];
}
@end