PageRenderTime 27ms CodeModel.GetById 2ms app.highlight 22ms RepoModel.GetById 1ms app.codeStats 0ms

/Tools/NativeHost/AppController.m

http://github.com/cacaodev/cappuccino
Objective C | 221 lines | 165 code | 47 blank | 9 comment | 13 complexity | 89ae9046d1d85e99ea19e9a063bc43f7 MD5 | raw file
  1//
  2//  AppController.m
  3//  NativeHost
  4//
  5//  Created by Francisco Tolmasky on 6/4/09.
  6//  Copyright 2009 280 North, Inc. All rights reserved.
  7//
  8
  9#import "Console.h"
 10#import "Server.h"
 11#import "AppController.h"
 12#import "NSURL+Additions.h"
 13#import "BridgedMethods.h"
 14#import "WebWindow.h"
 15#import "WebScripObject+Objective-J.h"
 16
 17extern int interactive;
 18
 19int SERVER_PORT = 9191;
 20NSString *SERVER_PASSWORD = nil;
 21NSString *SERVER_USER = nil;
 22
 23
 24@implementation AppController
 25
 26- (id)init
 27{
 28    self = [super init];
 29
 30    if (self)
 31        openingURLStrings = [NSMutableArray new];
 32
 33    return self;
 34}
 35
 36- (void)dealloc
 37{
 38    [webView release];
 39    [webViewWindow release];
 40
 41    [openingURLStrings release];
 42
 43    [super dealloc];
 44}
 45
 46- (BOOL)application:(NSApplication *)anApplication openFile:(NSString *)aFilename
 47{
 48    id canOpenFilesImmediately = [[webView windowScriptObject] evaluateWebScript:@"CPApp && CPApp._finishedLaunching"];
 49
 50    NSString *path = [[[NSURL fileURLWithPath:aFilename] HTTPFileSystemURL] absoluteString];
 51    if ([canOpenFilesImmediately isKindOfClass:[NSNumber class]] && [canOpenFilesImmediately boolValue])
 52        [[webView windowScriptObject] evaluateObjectiveJ:[NSString stringWithFormat:@"[CPApp _openURL:[CPURL URLWithString:\"%@\"]]", path]];
 53    else
 54        [openingURLStrings addObject:path];
 55
 56    return NO;
 57}
 58
 59- (void)applicationWillFinishLaunching:(NSNotification *)aNotification
 60{
 61    NSUserDefaults  * defaults = [NSUserDefaults standardUserDefaults];
 62
 63    [defaults registerDefaults:[NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:@"WebKitDeveloperExtras"]];
 64
 65    [self startServer];
 66}
 67
 68- (void)applicationDidFinishLaunching:(NSNotification *)notification
 69{
 70    [self startCappuccinoApplication];
 71}
 72
 73- (void)applicationDidResignActive:(NSNotification *)aNotification
 74{
 75    // FIXME: handle this better.
 76    [[self windowScriptObject] evaluateObjectiveJ:@"[CPApp _willResignActive];"];
 77    [[self windowScriptObject] evaluateObjectiveJ:@"CPApp._isActive = NO;"];
 78    [[self windowScriptObject] evaluateObjectiveJ:@"[CPApp _didResignActive];"];
 79}
 80
 81- (void)applicationDidBecomeActive:(NSNotification *)aNotification
 82{
 83    // FIXME: handle this better.
 84    [[self windowScriptObject] evaluateObjectiveJ:@"[CPApp _willBecomeActive];"];
 85    [[self windowScriptObject] evaluateObjectiveJ:@"CPApp._isActive = YES;"];
 86    [[self windowScriptObject] evaluateObjectiveJ:@"[CPApp _didBecomeActive];"];
 87}
 88
 89- (NSArray *)openingURLStrings
 90{
 91    return openingURLStrings;
 92}
 93
 94- (NSView *)keyView
 95{
 96    return [[[webView mainFrame] frameView] documentView];
 97}
 98
 99- (WebView *)webView
100{
101    return webView;
102}
103
104- (WebScriptObject *)windowScriptObject
105{
106    return [webView windowScriptObject];
107}
108
109- (void)startServer
110{
111    server = [[Server alloc] init];
112
113    if (![server start])
114        exit(0);
115
116	if (interactive)
117	{
118        stdinFileHandle = [[NSFileHandle fileHandleWithStandardInput] retain];
119        [[NSNotificationCenter defaultCenter]
120         addObserver:self
121         selector:@selector(didReadStdin:)
122         name:NSFileHandleReadCompletionNotification
123         object:stdinFileHandle];
124
125        fprintf(stdout, "objj> ");
126        fflush(stdout);
127        [stdinFileHandle readInBackgroundAndNotify];
128    }
129}
130
131- (void)didReadStdin:(NSNotification *)aNotification
132{
133    NSString *string = [[NSString alloc] initWithData:[[aNotification userInfo] objectForKey:NSFileHandleNotificationDataItem] encoding:NSASCIIStringEncoding];
134    NSString *result = [[self windowScriptObject] evaluateObjectiveJReturningString:string];
135    [string release];
136    if ([result respondsToSelector:@selector(UTF8String)])
137        fprintf(stdout, "%s\n", [result UTF8String]);
138    fprintf(stdout, "objj> ");
139    fflush(stdout);
140    [stdinFileHandle readInBackgroundAndNotify];
141}
142
143- (NSURL *)baseURL
144{
145    return baseURL;
146}
147
148- (void)startCappuccinoApplication
149{
150    NSString *initialURL = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NHInitialURL"];
151    BOOL prependServer = [[[NSBundle mainBundle] objectForInfoDictionaryKey:@"NHInitialURLPrependServer"] boolValue];
152
153    if (!initialURL)
154    {
155        NSString *initialResource = [[NSBundle mainBundle] objectForInfoDictionaryKey:@"NHInitialResource"];
156        
157        if (!initialResource)
158            initialResource = @"Application/index.html";
159
160        initialURL = [[NSString stringWithFormat:@"file://%@/%@", [[NSBundle mainBundle] resourcePath], initialResource] stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
161        baseURL = [[NSURL alloc] initWithString:@"file:///"];
162    }
163    else if (prependServer)
164    {
165        initialURL = [NSString stringWithFormat:@"http://127.0.0.1:%d/%@", SERVER_PORT, initialURL];
166        baseURL = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"http://127.0.0.1:%d/", SERVER_PORT]];
167    }
168
169	NHLog(@"STARTUP", [initialURL description]);
170	
171    [[webView mainFrame] loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:initialURL]]];
172}
173
174- (void)applicationWillTerminate:(NSNotification *)aNotification
175{
176    [server stop];
177}
178
179- (void)configureLocalStorage
180{
181    WebPreferences *webConfiguration = [WebPreferences standardPreferences];
182    NSBundle *mainBundle = [NSBundle mainBundle];
183    NSString *cappBundleName = [mainBundle objectForInfoDictionaryKey:@"CPBundleName"];
184    NSString *applicationSupportFile = [@"~/Library/Application Support/" stringByExpandingTildeInPath];
185    NSString *savePath = [NSString pathWithComponents:[NSArray arrayWithObjects:applicationSupportFile, cappBundleName, @"LocalStorage", nil]];
186    
187    if ([webConfiguration respondsToSelector:@selector(setLocalStorageDatabasePath:)])
188        [webConfiguration setLocalStorageDatabasePath:savePath];
189    else
190        [webConfiguration _setLocalStorageDatabasePath:savePath];
191    
192    [webView setPreferences:webConfiguration];
193}
194
195- (void)awakeFromNib
196{
197    webView = [[WebView alloc] init];
198
199    [webView setUIDelegate:self];
200    [webView setFrameLoadDelegate:self];
201    [webView setResourceLoadDelegate:self];
202    [webView setPolicyDelegate:self];
203	
204	[self configureLocalStorage];
205	
206    webViewWindow = [[NSWindow alloc] init];
207
208    [webViewWindow setContentView:webView];
209}
210
211@end
212
213void NHLog(NSString *type, NSString *message)
214{
215    NSArray *lines = [message componentsSeparatedByString:@"\n"];
216    for (NSString *line in lines)
217    {
218        if ([line length] > 0)
219            NSLog(@"%10@: %@", type, line);
220    }
221}