-
Notifications
You must be signed in to change notification settings - Fork 16
/
AppDelegate.m
200 lines (153 loc) · 6.86 KB
/
AppDelegate.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
#import "AppDelegate.h"
@interface AppDelegate (PrivateMethods)
- (void)loadUserScripts;
- (void)handleNetworkErrorForWebView:(WebView *)webView;
@end
@implementation AppDelegate
@synthesize window, webView;
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
[webView setFrameLoadDelegate:self];
[webView setPolicyDelegate:self];
[webView setUIDelegate:self];
// webview -> notification center bridge
notificationProvider = [[NotificationProvider alloc] init];
[webView _setNotificationProvider:notificationProvider];
// user agent
NSString *version = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"CFBundleShortVersionString"];
[webView setApplicationNameForUserAgent:[NSString stringWithFormat:@"nimbus/%@", version]];
// listen for title changes
[webView addObserver:self
forKeyPath:@"mainFrameTitle"
options:NSKeyValueObservingOptionNew
context:NULL];
console = [[JSConsole alloc] init];
[self loadUserScripts];
[[NSURLCache sharedURLCache] removeAllCachedResponses];
url = [[NSUserDefaults standardUserDefaults] valueForKey:@"url"];
if (!url) url = @"https://www.irccloud.com/";
NSLog(@"Connecting to %@", url);
[webView setMainFrameURL:url];
}
- (BOOL)applicationShouldHandleReopen:(NSApplication *)sender hasVisibleWindows:(BOOL)flag {
if (flag == NO) {
[window makeKeyAndOrderFront:self];
}
return YES;
}
#pragma mark -
- (void)loadUserScripts {
NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *folder = @"~/Library/Application Support/nimbus/Scripts/";
folder = [folder stringByExpandingTildeInPath];
if ([fileManager fileExistsAtPath:folder] == NO) {
[fileManager createDirectoryAtPath:folder withIntermediateDirectories:YES attributes:nil error:NULL];
}
NSArray *files = [fileManager contentsOfDirectoryAtPath:folder error:NULL];
NSMutableArray *scripts = [[NSMutableArray alloc] initWithCapacity:[files count]];
for (NSString* file in files) {
[scripts addObject:[folder stringByAppendingPathComponent:file]];
}
userScripts = [[NSArray alloc] initWithArray:scripts];
[scripts release];
}
#pragma mark -
- (void)titleDidChange:(NSString *)title {
NSUInteger unread = 0;
if ([title length] == 0) {
NSLog(@"WARNING: title changed to an empty string.");
return;
}
if ([[title substringToIndex:1] isEqualToString:@"*"]) {
title = [title substringFromIndex:2];
} else if ([[title substringToIndex:1] isEqualToString:@"("]) {
NSRange range = [title rangeOfString:@")"];
range.length = range.location - 1;
range.location = 1;
unread = [[title substringWithRange:range] intValue];
title = [title substringFromIndex:range.location + range.length + 2];
}
NSString *badge = nil;
if (unread > 0) {
badge = [NSString stringWithFormat:@"%ld", unread];
}
NSRange pipepos = [title rangeOfString:@" | IRCCloud" options:NSBackwardsSearch];
if (pipepos.location != NSNotFound) {
title = [title substringToIndex:pipepos.location];
}
[[[NSApplication sharedApplication] dockTile] setBadgeLabel:badge];
[window setTitle:title];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if ([keyPath isEqualToString:@"mainFrameTitle"]) {
[self titleDidChange:[change valueForKey:@"new"]];
}
}
#pragma mark FrameLoadDelegate
- (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowScriptObject forFrame:(WebFrame *)frame {
[windowScriptObject setValue:console forKey:@"console"];
// disable notification sound
[windowScriptObject evaluateWebScript:@"HTMLAudioElement.prototype.play = function(){}"];
// inject userscripts
for (NSString *script in userScripts) {
NSLog(@"loading script: %@", script);
[windowScriptObject evaluateWebScript:[NSString stringWithContentsOfFile:script usedEncoding:nil error:NULL]];
}
}
- (void)handleNetworkError:(NSError *)error forWebView:(WebView *)view {
NSString *failingURL = [error.userInfo valueForKey:@"NSErrorFailingURLStringKey"];
if (!failingURL || [failingURL rangeOfString:url options:NSAnchoredSearch].location == NSNotFound) {
// If the URL isn't an irccloud URL, we don't care if it is down, the irccloud interface should still work.
return;
}
NSAlert *alert = [NSAlert alertWithMessageText:@"Unable to connect to IRCCloud" defaultButton:@"Retry" alternateButton:@"Quit" otherButton:nil informativeTextWithFormat:@"Check your internet connection."];
[alert beginSheetModalForWindow:window completionHandler:^(NSModalResponse responseCode) {
if (responseCode == NSModalResponseOK) {
[view setMainFrameURL:url];
} else {
[NSApp stop:self];
}
}];
}
- (void)webView:(WebView *)sender didFailLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
[self handleNetworkError:error forWebView:sender];
}
- (void)webView:(WebView *)sender didFailProvisionalLoadWithError:(NSError *)error forFrame:(WebFrame *)frame {
[self handleNetworkError:error forWebView:sender];
}
#pragma mark WebPolicyDelegate
- (void)webView:(WebView *)webView decidePolicyForNewWindowAction:(NSDictionary *)actionInformation request:(NSURLRequest *)request
newFrameName:(NSString *)frameName decisionListener:(id <WebPolicyDecisionListener>)listener {
// route all links that request a new window to default browser
[listener ignore];
[[NSWorkspace sharedWorkspace] openURL:[request URL]];
}
#pragma mark WebUIDelegate
- (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
NSAlert *alert = [NSAlert alertWithMessageText:@"Please confirm" defaultButton:@"Yes" alternateButton:@"No" otherButton:nil informativeTextWithFormat:@"%@", message];
return [alert runModal] == NSAlertDefaultReturn;
}
- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
NSAlert *alert = [NSAlert alertWithMessageText:@"Nimbus" defaultButton:@"Ok" alternateButton:nil otherButton:nil informativeTextWithFormat:@"%@", message];
[alert runModal];
}
- (void)webView:(WebView *)webView addMessageToConsole:(NSDictionary *)dictionary {
NSLog(@"ERROR: %@", [dictionary objectForKey:@"message"]);
}
- (void)webView:(WebView *)sender runOpenPanelForFileButtonWithResultListener:(id < WebOpenPanelResultListener >)resultListener
{
NSOpenPanel* openDlg = [NSOpenPanel openPanel];
[openDlg setCanChooseFiles:YES];
[openDlg setCanChooseDirectories:NO];
if ([openDlg runModal] == NSOKButton) {
NSArray* files = [[openDlg URLs] valueForKey:@"relativePath"];
[resultListener chooseFilenames:files];
}
}
#pragma mark -
- (void)dealloc {
[console release];
[notificationProvider release];
[userScripts release];
[super dealloc];
}
@end