WindowController.m 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // WindowController.m
  3. // MG
  4. //
  5. // Created by Tim Debo on 5/19/14.
  6. //
  7. //
  8. #import "WindowController.h"
  9. #import "WebViewDelegate.h"
  10. #import "JSON.h"
  11. @interface WindowController ()
  12. @property (nonatomic, readwrite, strong) NSMutableDictionary* settings;
  13. @property (nonatomic, readwrite, strong) NSMutableDictionary* pluginObjects;
  14. @property (nonatomic, readwrite, strong) NSArray* startupPluginNames;
  15. @property (nonatomic, readwrite, strong) NSDictionary* pluginsMap;
  16. @property (nonatomic, readwrite, assign) BOOL loadFromString;
  17. @property (readwrite, assign) BOOL initialized;
  18. -(void) setWindowParams;
  19. @end
  20. @interface WebPreferences (WebPreferencesPrivate)
  21. - (void)_setLocalStorageDatabasePath:(NSString *)path;
  22. - (void) setLocalStorageEnabled: (BOOL) localStorageEnabled;
  23. - (void) setDatabasesEnabled:(BOOL)databasesEnabled;
  24. - (void) setDeveloperExtrasEnabled:(BOOL)developerExtrasEnabled;
  25. - (void) setWebGLEnabled:(BOOL)webGLEnabled;
  26. - (void) setOfflineWebApplicationCacheEnabled:(BOOL)offlineWebApplicationCacheEnabled;
  27. @end
  28. @implementation WindowController
  29. @synthesize webView, url, initialized, webViewDelegate, jsContext;
  30. - (id)initWithWindow:(NSWindow *)aWindow
  31. {
  32. self = [super initWithWindow:aWindow];
  33. if (self) {
  34. }
  35. return self;
  36. }
  37. - (void)windowDidLoad
  38. {
  39. [super windowDidLoad];
  40. [self.webView setMainFrameURL:[self.url absoluteString]];
  41. }
  42. - (id) initWithURL:(NSString *) relativeURL{
  43. self = [super initWithWindowNibName:@"MainWindow"];
  44. self.url = [NSURL URLWithString:relativeURL relativeToURL:[[NSBundle mainBundle] resourceURL]];
  45. [self.window setFrameAutosaveName:@"MacGapWindow"];
  46. return self;
  47. }
  48. -(id) initWithRequest: (NSURLRequest *)request{
  49. self = [super initWithWindowNibName:@"MainWindow"];
  50. [[self.webView mainFrame] loadRequest:request];
  51. return self;
  52. }
  53. - (void) awakeFromNib
  54. {
  55. WebPreferences *webPrefs = [WebPreferences standardPreferences];
  56. NSString *cappBundleName = [[[NSBundle mainBundle] infoDictionary] objectForKey:@"CFBundleName"];
  57. NSString *applicationSupportFile = [@"~/Library/Application Support/" stringByExpandingTildeInPath];
  58. NSString *savePath = [NSString pathWithComponents:[NSArray arrayWithObjects:applicationSupportFile, cappBundleName, @"LocalStorage", nil]];
  59. NSString *configPath = [[NSBundle mainBundle] pathForResource:@"./public/config" ofType:@"json"];
  60. NSMutableDictionary *config = [[[NSString alloc] initWithContentsOfFile:configPath encoding:NSUTF8StringEncoding error:NULL] JSONObject];
  61. NSDictionary *plugins = [config objectForKey:@"plugins"];
  62. self.pluginsMap = plugins;
  63. self.settings = config;
  64. [webPrefs _setLocalStorageDatabasePath:savePath];
  65. [webPrefs setLocalStorageEnabled:YES];
  66. [webPrefs setDatabasesEnabled:YES];
  67. [webPrefs setDeveloperExtrasEnabled:[[NSUserDefaults standardUserDefaults] boolForKey: @"developer"]];
  68. [webPrefs setOfflineWebApplicationCacheEnabled:YES];
  69. [webPrefs setWebGLEnabled:YES];
  70. [self.webView setPreferences:webPrefs];
  71. NSHTTPCookieStorage *cookieStorage = [NSHTTPCookieStorage
  72. sharedHTTPCookieStorage];
  73. [cookieStorage setCookieAcceptPolicy:NSHTTPCookieAcceptPolicyAlways];
  74. [self.webView setApplicationNameForUserAgent: @"MacGap"];
  75. self.webViewDelegate = [[WebViewDelegate alloc] initWithMenu:[NSApp mainMenu]];
  76. self.webViewDelegate.windowController = self;
  77. [self.webView setFrameLoadDelegate:self.webViewDelegate];
  78. [self.webView setUIDelegate:self.webViewDelegate];
  79. [self.webView setResourceLoadDelegate:self.webViewDelegate];
  80. [self.webView setDownloadDelegate:self.webViewDelegate];
  81. [self.webView setPolicyDelegate:self.webViewDelegate];
  82. [self.webView setDrawsBackground:NO];
  83. [self.webView setShouldCloseWithWindow:NO];
  84. [self.webView setGroupName:@"MacGap"];
  85. self.pluginObjects = [[NSMutableDictionary alloc] initWithCapacity:20];
  86. }
  87. - (void) setWindowParams
  88. {
  89. NSDictionary* params = [self.settings objectForKey:@"window"];
  90. NSRect frame = [[self window] frame];
  91. if([params objectForKey:@"width"] != nil) {
  92. frame.size.width = [[params objectForKey:@"width"] doubleValue];
  93. }
  94. if([params objectForKey:@"height"]) {
  95. frame.size.height = [[params objectForKey:@"height"] doubleValue];
  96. }
  97. if([params objectForKey:@"min_width"] && [params objectForKey:@"min_height"]) {
  98. [self.window setMinSize: NSMakeSize( [[params objectForKey:@"min_width"] doubleValue], [[params objectForKey:@"min_height"] doubleValue] ) ];
  99. }
  100. if([params objectForKey:@"max_width"] && [params objectForKey:@"max_height"]) {
  101. [self.window setMaxSize: NSMakeSize( [[params objectForKey:@"max_width"] doubleValue], [[params objectForKey:@"max_height"] doubleValue] ) ];
  102. }
  103. if([params objectForKey:@"title"]) {
  104. [[self window] setTitle: [params objectForKey:@"title"]];
  105. }
  106. if([[params objectForKey:@"position"] isEqualToString:@"center"]) {
  107. [[self window] center];
  108. }
  109. if([params objectForKey:@"opaque"]) {
  110. [[self window] setOpaque: [[params objectForKey:@"opaque"] boolValue]];
  111. }
  112. if([params objectForKey:@"alpha"]) {
  113. NSColor *backgroundColor = [NSColor colorWithDeviceRed:0.0 green:0.0 blue:0.0 alpha:[[params objectForKey:@"alpha"] doubleValue]];
  114. [[self window] setBackgroundColor: backgroundColor];
  115. }
  116. [[self window] setFrame:frame display: YES];
  117. }
  118. #pragma mark -
  119. #pragma mark Plugin Registration
  120. - (void)registerPlugin:(Command*)plugin withClassName:(NSString*)className
  121. {
  122. if ([plugin respondsToSelector:@selector(setWindowController:)]) {
  123. [plugin setWindowController:self];
  124. }
  125. [self.pluginObjects setObject:plugin forKey:className];
  126. [plugin initializePlugin];
  127. }
  128. - (void)registerPlugin:(Command*)plugin withPluginName:(NSString*)pluginName
  129. {
  130. if ([plugin respondsToSelector:@selector(setWindowController:)]) {
  131. [plugin setWindowController:self];
  132. }
  133. NSString* className = NSStringFromClass([plugin class]);
  134. [self.pluginObjects setObject:plugin forKey:className];
  135. [self.pluginsMap setValue:className forKey:[pluginName lowercaseString]];
  136. [plugin initializePlugin];
  137. }
  138. - (id)getCommandInstance:(NSString*)pluginName
  139. {
  140. NSString* className = [self.pluginsMap objectForKey:[pluginName lowercaseString]];
  141. if (className == nil) {
  142. className = [self.pluginsMap objectForKey:pluginName];
  143. if(className == nil)
  144. return nil;
  145. }
  146. id obj = [self.pluginObjects objectForKey:className];
  147. if (!obj) {
  148. obj = [[NSClassFromString(className)alloc] initWithWebView:webView];
  149. if (obj != nil) {
  150. [self registerPlugin:obj withClassName:className];
  151. } else {
  152. NSLog(@"Plugin class %@ (pluginName: %@) does not exist.", className, pluginName);
  153. }
  154. }
  155. return obj;
  156. }
  157. @end