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