forked from jnordberg/irccloudapp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.m
148 lines (108 loc) · 4.61 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
#import "AppDelegate.h"
@interface AppDelegate (PrivateMethods)
- (void)loadUserScripts;
@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];
NSString *url = [[NSUserDefaults standardUserDefaults] valueForKey:@"url"];
if (!url) url = @"https://alpha.irccloud.com/";
NSLog(@"Connecting to %@", url);
[webView setMainFrameURL:url];
}
#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 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"];
// inject userscripts
for (NSString *script in userScripts) {
NSLog(@"loading script: %@", script);
[windowScriptObject evaluateWebScript:[NSString stringWithContentsOfFile:script usedEncoding:nil error:NULL]];
}
}
#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 {
NSInteger result = NSRunAlertPanel(@"Please confirm", message, @"Yes", @"No", nil);
return result == NSAlertDefaultReturn;
}
- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message initiatedByFrame:(WebFrame *)frame {
NSRunAlertPanel(@"Nimbus", message, @"Ok", nil, nil);
}
- (void)webView:(WebView *)webView addMessageToConsole:(NSDictionary *)dictionary {
NSLog(@"ERROR: %@", [dictionary objectForKey:@"message"]);
}
#pragma mark -
- (void)dealloc {
[console release];
[notificationProvider release];
[userScripts release];
[super dealloc];
}
@end