Preparing to add widget support
[RExecServer.git] / RApplicationDelegate.m
blobe4896d4d58cc6129c28db02fcabb153de2ef0be9
1 //
2 //  RApplicationDelegate.m
3 //  RExecServer
4 //
5 //  Created by Byron Ellis on 6/26/07.
6 //  Copyright 2007 __MyCompanyName__. All rights reserved.
7 //
9 #import "RApplicationDelegate.h"
10 #import "RInterpreter.h"
12 #include <sys/types.h>
13 #include <sys/stat.h>
14 #include <Security/Authorization.h>
15 #include <Security/AuthorizationTags.h>
17 @implementation RApplicationDelegate
19 - (BOOL)applicationShouldOpenUntitledFile:(NSApplication*)sender { return NO; }
20 - (void)applicationWillFinishLaunching:(NSNotification*)aNotification {
21         RInterpreter *ri = [RInterpreter sharedInterpreter];
23         if(!(NULL != getenv("RNOTERM") && strncmp(getenv("RNOTERM"),"t",1)==0) && 
24                 ((NULL != getenv("EMACS") && strncmp(getenv("EMACS"),"t",1)==0) || (NULL != getenv("TERM"))))
25            [ri setAllowTerminal:YES];
26         if(NULL != getenv("RNOVEND"))
27                 [ri setVend:NO];
28         
29         /*
30         struct stat sb;
31         if(-1==lstat("/usr/bin/R-serve",&sb)) {
32                 switch(errno) {
33                         case ENOENT:{
34                                 NSAlert *alert = [NSAlert alertWithMessageText:@"RExecServer can create a symbolic link named R-server in /usr/bin. Would you like to do this now?"
35                                         defaultButton:@"Yes"
36                                         alternateButton:@"No" 
37                                         otherButton:nil informativeTextWithFormat:@""];
38                                 if(NSAlertDefaultReturn == [alert runModal]) {
39                                         
40                                         AuthorizationRef auth;
41                                         if(errAuthorizationSuccess == AuthorizationCreate(NULL,kAuthorizationEmptyEnvironment,kAuthorizationFlagDefaults,&auth)) {
42                                                 AuthorizationItem   item   = {kAuthorizationRightExecute,0,NULL,0};
43                                                 AuthorizationRights rights = {1,&item};
44                                                 if(errAuthorizationSuccess == AuthorizationCopyRights(auth,&rights,NULL,
45                                                         kAuthorizationFlagDefaults|kAuthorizationFlagInteractionAllowed|kAuthorizationFlagPreAuthorize|kAuthorizationFlagExtendRights
46                                                         ,NULL)) {
47                                                                 char *toolpath = "/bin/ln";
48                                                                 char *toolargs[3];
49                                                                 toolargs[0] = "-s"; 
50                                                                 toolargs[1] = (char*)[[[NSBundle mainBundle] pathForResource:@"RExecServer" ofType:@"sh"] UTF8String];
51                                                                 toolargs[2] = "/usr/bin/R-serve";
52                                                                 OSStatus status;
53                                                                 if(errAuthorizationSuccess != (status = AuthorizationExecuteWithPrivileges(auth,toolpath,kAuthorizationFlagDefaults,toolargs,NULL)))
54                                                                         NSLog(@"Unable to execute tool [%d]: %s %s %s %s",status, toolpath,toolargs[0],toolargs[1],toolargs[2]);
55                                                 }
56                                                 AuthorizationFree(auth,kAuthorizationFlagDefaults);
57                                         }
59                                 
60                                 
61                                         symlink([[NSBundle mainBundle] executablePath],"/usr/bin/R-serve");
62                                 }
63                                 break;
64                         }
65                 }
66         }
67         */
68            
70 - (void)applicationDidFinishLaunching:(NSNotification*)aNotification {
71         //Process the remaining events and then bail out to initialize R.
72         [NSApp postEvent:[NSEvent otherEventWithType:NSApplicationDefined 
73                                                                         location:NSMakePoint(0,0)
74                                                                          modifierFlags:0
75                                                                          timestamp:0 
76                                                                          windowNumber:0 
77                                                                          context:nil 
78                                                                          subtype:1337 
79                                                                          data1:0 
80                                                                          data2:0] atStart:NO];  
83 @end