-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathLingr_HatAppDelegate.m
239 lines (192 loc) · 7.32 KB
/
Lingr_HatAppDelegate.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
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
//
// Lingr_HatAppDelegate.m
// Lingr Hat
//
// Created by Kohichi Aoki on 11/2/10.
// Copyright 2010 drikin.com. All rights reserved.
//
#import "Lingr_HatAppDelegate.h"
#pragma mark -
#pragma mark Application Definitions
#define LH_LINGR_BASEURL @"http://lingr.com/"
const float LH_NON_ACTIVE_DEFAULT_TIMER_INTERVAL = 30.0;
@implementation Lingr_HatAppDelegate
@synthesize window, mainWebView, singleShotTimer;
#pragma mark -
#pragma mark Registry Settings
+ (void)setupDefaults {
NSDictionary *userDefaultsValuesDict;
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSDictionary *initialValuesDict;
// set UserDefaults Default
userDefaultsValuesDict = [NSDictionary dictionaryWithObjectsAndKeys:
/* Default Values Here VALUE,KEY and VALUE MUST BE OBJECT */
[NSNumber numberWithBool:NO], @"bgScroll",
[NSNumber numberWithFloat:LH_NON_ACTIVE_DEFAULT_TIMER_INTERVAL], @"checkLogInterval",
[NSNumber numberWithInt:800], @"windowWidth",
[NSNumber numberWithInt:700], @"windowHeight",
/* Default Values Ended */
nil];
[defaults registerDefaults:userDefaultsValuesDict];
// enable for Reset
NSArray *resettableUserDefaultsKeys = [NSArray arrayWithObjects:@"bgScroll",
@"checkLogInterval",
@"windowWidth",
@"windowHeight",
nil];
initialValuesDict=[userDefaultsValuesDict dictionaryWithValuesForKeys:resettableUserDefaultsKeys];
[[NSUserDefaultsController sharedUserDefaultsController] setInitialValues:initialValuesDict];
}
#pragma mark -
#pragma mark Application lifecycle
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
// Insert code here to initialize your application
// initialize member variables
numOfUnreadMessages = 0;
bEnableCheking = NO;
// set UserDefault
[Lingr_HatAppDelegate setupDefaults];
// init member variables
self.singleShotTimer = nil;
// set URL
[mainWebView setMainFrameURL:LH_LINGR_BASEURL];
}
- (void)applicationDidBecomeActive:(NSNotification *)aNotification {
[self cancelSingleshotTimer];
[self disableLogging];
// Reset unread and disable UnreadChecking
bEnableCheking = NO;
// Hide Notification Badge
NSDockTile *dockTile = [NSApp dockTile];
[dockTile setBadgeLabel:nil];
}
- (void)applicationDidResignActive:(NSNotification *)aNotification {
[self cancelSingleshotTimer];
// invoke oneshot timer
float fIntervalSec = [[NSUserDefaults standardUserDefaults] floatForKey:@"checkLogInterval"];
fIntervalSec = fIntervalSec == 0.0 ? LH_NON_ACTIVE_DEFAULT_TIMER_INTERVAL : fIntervalSec;
self.singleShotTimer = [NSTimer scheduledTimerWithTimeInterval:fIntervalSec
target:self
selector:@selector(enableLogging)
userInfo:nil
repeats:NO];
[self.singleShotTimer retain];
[self tryToInsertInjectionCode]; //but Inject is only once
}
#pragma mark -
#pragma mark WebPolicyDelegate
- (void) webView : (WebView *) sender
decidePolicyForNewWindowAction : (NSDictionary *) info
request : (NSURLRequest *) request
newFrameName : (NSString *) frameName
decisionListener : (id<WebPolicyDecisionListener>) listener
{
[listener ignore];
[[NSWorkspace sharedWorkspace] openURL:[request URL]];
}
- (void) webView:(WebView *)webView
decidePolicyForNavigationAction:(NSDictionary *)actionInformation
request:(NSURLRequest *)request
frame:(WebFrame *)frame
decisionListener:(id<WebPolicyDecisionListener>)listener
{
NSURL *url = [request URL];
if( [[url scheme] isEqualToString:@"lingr"] ) {
[self incommingMessages];
}
[listener use];
}
#pragma mark -
#pragma mark Methods
- (void)enableLogging
{
// change message color to gray
NSMutableString *script = [NSMutableString stringWithString:@"\
var d = document.getElementsByClassName('decorated'); \
for (var i = 0; i < d.length; i++) { \
var p = d[i].getElementsByTagName('p'); \
for (var j = 0; j < p.length; j++) { \
p[j].style.color = 'DarkGray'; \
} \
} \
"];
// bgScroll
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"bgScroll"]) {
[script appendString:@"lingr.ui.getActiveRoom = function() {};"];
}
// evaluate javascript
[[mainWebView windowScriptObject] evaluateWebScript:script];
// enable counting unread messages
numOfUnreadMessages = 0;
bEnableCheking = YES;
}
- (void)disableLogging
{
// bgScroll
if (![[NSUserDefaults standardUserDefaults] boolForKey:@"bgScroll"]) {
NSString *script = @"\
lingr.ui.getActiveRoom = function() {return extractId($$('#left .active')[0]);}; \
";
[[mainWebView windowScriptObject] evaluateWebScript:script];
}
// disable counting unread messages
bEnableCheking = NO;
}
-(void)tryToInsertInjectionCode
{
// injection for counting unread messages
// once for all
static BOOL bInjected = NO;
if (bInjected == NO) {
NSString *script = @"\
lingr.ui.insertMessageFunc = lingr.ui.insertMessage;\
lingr.ui.insertMessage = function(a,b) {\
var result = this.insertMessageFunc(a,b);\
if (this.getActiveRoom() == a.room) {\
if (a.type != 'system') {\
document.location.href = 'lingr://insertMessage/';\
}\
}\
return result;\
};\
";
id result = [[mainWebView windowScriptObject] evaluateWebScript:script];
if ([result isMemberOfClass:[WebUndefined class]]) {
NSLog(@"Injection Error occurs");
} else {
NSLog(@"Success injection ");
bInjected = YES;
}
}
}
- (void)cancelSingleshotTimer
{
// cancel timer which has not invoked
if (self.singleShotTimer != nil) {
[self.singleShotTimer invalidate];
[self.singleShotTimer release];
self.singleShotTimer = nil;
}
}
- (void)webView:(WebView *)sender runJavaScriptAlertPanelWithMessage:(NSString *)message
{
NSRunAlertPanel(@"JavaScript alert", message, @"OK", nil, nil);
}
- (BOOL)webView:(WebView *)sender runJavaScriptConfirmPanelWithMessage:(NSString *)message
{
int choice = NSRunAlertPanel(@"JavaScript confirm", message, @"OK", @"Cancel", nil);
return choice == NSAlertDefaultReturn;
}
#pragma mark -
#pragma mark Event Actions
-(void)incommingMessages
{
if ([NSApp isActive] || (!bEnableCheking)) {
return;
}
numOfUnreadMessages += 1;
NSDockTile *dockTile = [NSApp dockTile];
[dockTile setBadgeLabel:[NSString stringWithFormat:@"%d", numOfUnreadMessages]];
[NSApp requestUserAttention:NSInformationalRequest];
}
@end