forked from pieter/gitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPBWebController.m
205 lines (166 loc) · 6.09 KB
/
PBWebController.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
//
// PBWebController.m
// GitX
//
// Created by Pieter de Bie on 08-10-08.
// Copyright 2008 __MyCompanyName__. All rights reserved.
//
#import "PBWebController.h"
#import "PBGitRepository.h"
#import "PBGitXProtocol.h"
#import "PBGitDefaults.h"
#include <SystemConfiguration/SCNetworkReachability.h>
@interface PBWebController()
- (void)preferencesChangedWithNotification:(NSNotification *)theNotification;
@end
@implementation PBWebController
@synthesize startFile, repository;
- (void) awakeFromNib
{
NSString *path = [NSString stringWithFormat:@"html/views/%@", startFile];
NSString* file = [[NSBundle mainBundle] pathForResource:@"index" ofType:@"html" inDirectory:path];
NSURLRequest * request = [NSURLRequest requestWithURL:[NSURL fileURLWithPath:file]];
callbacks = [NSMapTable mapTableWithKeyOptions:(NSPointerFunctionsObjectPointerPersonality|NSPointerFunctionsStrongMemory) valueOptions:(NSPointerFunctionsObjectPointerPersonality|NSPointerFunctionsStrongMemory)];
NSNotificationCenter *nc = [NSNotificationCenter defaultCenter];
[nc addObserver:self
selector:@selector(preferencesChangedWithNotification:)
name:NSUserDefaultsDidChangeNotification
object:nil];
finishedLoading = NO;
[view setUIDelegate:self];
[view setFrameLoadDelegate:self];
[view setResourceLoadDelegate:self];
[[view mainFrame] loadRequest:request];
}
- (WebScriptObject *) script
{
return [view windowScriptObject];
}
- (void) closeView
{
if (view)
[view close];
}
# pragma mark Delegate methods
- (void)webView:(WebView *)sender didClearWindowObject:(WebScriptObject *)windowObject forFrame:(WebFrame *)frame
{
id script = [view windowScriptObject];
[script setValue: self forKey:@"Controller"];
}
- (void) webView:(id) v didFinishLoadForFrame:(id) frame
{
finishedLoading = YES;
if ([self respondsToSelector:@selector(didLoad)])
[self performSelector:@selector(didLoad)];
}
- (void)webView:(WebView *)webView addMessageToConsole:(NSDictionary *)dictionary
{
NSLog(@"Error from webkit: %@", dictionary);
}
- (NSURLRequest *)webView:(WebView *)sender
resource:(id)identifier
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *)redirectResponse
fromDataSource:(WebDataSource *)dataSource
{
if (!self.repository)
return request;
// TODO: Change this to canInitWithRequest
if ([[[request URL] scheme] isEqualToString:@"GitX"]) {
NSMutableURLRequest *newRequest = [request mutableCopy];
[newRequest setRepository:self.repository];
return newRequest;
}
return request;
}
+ (BOOL)isSelectorExcludedFromWebScript:(SEL)aSelector
{
return NO;
}
+ (BOOL)isKeyExcludedFromWebScript:(const char *)name {
return NO;
}
#pragma mark Functions to be used from JavaScript
- (void) log: (NSString*) logMessage
{
NSLog(@"%@", logMessage);
}
- (BOOL) isReachable:(NSString *)hostname
{
SCNetworkConnectionFlags flags;
if (!SCNetworkCheckReachabilityByName([hostname cStringUsingEncoding:NSASCIIStringEncoding], &flags))
return FALSE;
// If a connection is required, then it's not reachable
if (flags & (kSCNetworkFlagsConnectionRequired | kSCNetworkFlagsConnectionAutomatic | kSCNetworkFlagsInterventionRequired))
return FALSE;
return flags > 0;
}
- (BOOL) isFeatureEnabled:(NSString *)feature
{
if([feature isEqualToString:@"gravatar"])
return [PBGitDefaults isGravatarEnabled];
else if([feature isEqualToString:@"gist"])
return [PBGitDefaults isGistEnabled];
else if([feature isEqualToString:@"confirmGist"])
return [PBGitDefaults confirmPublicGists];
else if([feature isEqualToString:@"publicGist"])
return [PBGitDefaults isGistPublic];
else
return YES;
}
#pragma mark Using async function from JS
- (void) runCommand:(WebScriptObject *)arguments inRepository:(PBGitRepository *)repo callBack:(WebScriptObject *)callBack
{
// The JS bridge does not handle JS Arrays, even though the docs say it does. So, we convert it ourselves.
int length = [[arguments valueForKey:@"length"] intValue];
NSMutableArray *realArguments = [NSMutableArray arrayWithCapacity:length];
int i = 0;
for (i = 0; i < length; i++)
[realArguments addObject:[arguments webScriptValueAtIndex:i]];
NSFileHandle *handle = [repo handleInWorkDirForArguments:realArguments];
[callbacks setObject:callBack forKey:handle];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(JSRunCommandDone:) name:NSFileHandleReadToEndOfFileCompletionNotification object:handle];
[handle readToEndOfFileInBackgroundAndNotify];
}
- (void) callSelector:(NSString *)selectorString onObject:(id)object callBack:(WebScriptObject *)callBack
{
NSArray *arguments = [NSArray arrayWithObjects:selectorString, object, nil];
NSThread *thread = [[NSThread alloc] initWithTarget:self selector:@selector(runInThread:) object:arguments];
[callbacks setObject:callBack forKey:thread];
[thread start];
}
- (void) runInThread:(NSArray *)arguments
{
SEL selector = NSSelectorFromString([arguments objectAtIndex:0]);
id object = [arguments objectAtIndex:1];
id ret = [object performSelector:selector];
NSArray *returnArray = [NSArray arrayWithObjects:[NSThread currentThread], ret, nil];
[self performSelectorOnMainThread:@selector(threadFinished:) withObject:returnArray waitUntilDone:NO];
}
- (void) returnCallBackForObject:(id)object withData:(id)data
{
WebScriptObject *a = [callbacks objectForKey: object];
if (!a) {
NSLog(@"Could not find a callback for object: %@", object);
return;
}
[callbacks removeObjectForKey:object];
[a callWebScriptMethod:@"call" withArguments:[NSArray arrayWithObjects:@"", data, nil]];
}
- (void) threadFinished:(NSArray *)arguments
{
[self returnCallBackForObject:[arguments objectAtIndex:0] withData:[arguments objectAtIndex:1]];
}
- (void) JSRunCommandDone:(NSNotification *)notification
{
NSString *data = [[NSString alloc] initWithData:[[notification userInfo] valueForKey:NSFileHandleNotificationDataItem] encoding:NSUTF8StringEncoding];
[self returnCallBackForObject:[notification object] withData:data];
}
- (void) preferencesChanged
{
}
- (void)preferencesChangedWithNotification:(NSNotification *)theNotification
{
[self preferencesChanged];
}
@end