Upstream tarball 9372
[amule.git] / cocoa-mule / AppController.mm
blob22d073967c766acb3d365d8d3b4d10b88428754e
1 #import "AppController.h"
3 #import "LoginDialogController.h"
4 #import "DownloadsViewController.h"
6 #include <unistd.h>
7 #include <signal.h>
9 @implementation AppController
11 - (IBAction)show_Networks:(id)sender {
12         [m_main_tabview selectTabViewItemAtIndex: 2];
13         
15 //      ECLoginPacket *p = [ECLoginPacket loginPacket:@"123456" withVersion:@"0.1"];
16 //      NSOutputStream *stream = [NSOutputStream outputStreamToMemory];
17 //      uint8_t buffer[1024];
18 //      memset(buffer, 0, 1024);
19 //      NSOutputStream *stream = [NSOutputStream outputStreamToBuffer:buffer capacity:1024];
20 //      [stream open];
21 //      [p writeToSocket:stream];
22 //      id data = [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
23 //      int off = [data length];
24 //      NSLog(@"off=%d", off);
25 //      [m_connection sendPacket:p];
28 - (IBAction)show_Search:(id)sender {
29         [m_main_tabview selectTabViewItemAtIndex: 1];
32 - (IBAction)show_Sharing:(id)sender {
33         [m_main_tabview selectTabViewItemAtIndex: 3];
36 - (IBAction)show_Stats:(id)sender {
37         [m_main_tabview selectTabViewItemAtIndex: 4];
40 - (IBAction)show_Xfers:(id)sender {
41         [m_main_tabview selectTabViewItemAtIndex: 0];
44 -(IBAction)show_Preferences:(id)sender {
47 -(IBAction)show_About:(id)sender {
50 - (bool)askCoreParams {
51         LoginDialogController *dlg = [[LoginDialogController alloc] init];
52         
53         bool dlgResult = [dlg showDlg:nil];
55         m_targetaddr = dlg.host;
56         m_targetport = dlg.port;
57         m_corepass = dlg.pass;
58         
59         return dlgResult;
62 - (void)applicationWillTerminate:(NSNotification *)aNotification {
63         //
64         // Save gui state in all views
65         //
66         if ([m_dload_controller respondsToSelector:@selector(saveGui)]) {
67                 [m_dload_controller performSelector:@selector(saveGui)];
68         }
69         
70         //
71         // Save main window
72         //
73         [[NSUserDefaults standardUserDefaults] setInteger:[m_main_tabview indexOfTabViewItem: [m_main_tabview selectedTabViewItem] ] forKey:@"activeTab"];
74         
75         //
76         // If we have slave daemon process, terminate it
77         //
78         if ( m_daemon_pid ) {
79                 //
80                 // started in local mode - terminate daemon
81                 //
82                 kill(m_daemon_pid, SIGTERM);
83         }
85         NSLog(@"Exiting ...\n");
88 - (void)restoreMainWindow {
89         int activeTab = [[NSUserDefaults standardUserDefaults] integerForKey:@"activeTab"];
90         [m_main_tabview selectTabViewItemAtIndex: activeTab];
94 - (void)awakeFromNib {
95         NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
96         NSString *mode = [args stringForKey:@"mode"];
97         NSLog(@"amule controller started, mode = [%@]\n", mode);
98         
99         
100         [[NSApplication sharedApplication] setDelegate:self];
101         
102         [self restoreMainWindow];
103         
104         m_targetaddr = 0;
105         m_targetport = 0;
106         m_corepass = nil;
107         
108         m_daemon_pid = 0;
109         if ( (mode != nil) && ([mode compare:@"guitest"] == NSOrderedSame) ) {
110                 [m_dload_tableview reloadData];
111                 NSLog(@"Started in GUI test mode - will not connect to core");
112                 return;
113         }       
115         if ( (mode != nil) && ([mode compare:@"remote"] == NSOrderedSame) ) {
116                 m_targetaddr = @"127.0.0.1";
117                 m_targetaddr = @"localhost";
118                 m_targetport = 4712;
119                 if (![self askCoreParams] ) {
120                         // "Cancel" selected on login dialog
121                         [[NSApplication sharedApplication] terminate:self];
122                 }
123                 [[NSUserDefaults standardUserDefaults] setObject:m_targetaddr forKey:@"LastTargetHost"];
124                 [[NSUserDefaults standardUserDefaults] setInteger:m_targetport forKey:@"LastTargetPort"];
125                 [[NSUserDefaults standardUserDefaults] synchronize];
126                 NSLog(@"Remote mode selected, target=%@:%d\n", m_targetaddr, m_targetport);
127         } else {
128                 NSLog(@"Local mode selected - starting daemon\n");
129                 m_targetaddr = @"127.0.0.1";
130                 m_targetport = 4712;
131                 if ( [self startDaemon] == -1 ) {
132                         NSRunCriticalAlertPanel(@"Daemon startup error", 
133                                                         @"Unable to start core daemon (amuled)",
134                                                         @"OK", nil,nil);
135                         [[NSApplication sharedApplication] terminate:self];
136                 }
137         }
138         
139         m_connection = [ECRemoteConnection remoteConnection];
140         [m_connection retain];
141         m_data = [amuleData initWithConnection:m_connection];
142         [m_data retain];
144         //
145         // bind datastructure to GUI controllers
146         //
147         [m_dload_controller setFileSet:m_data.downloads];
148         [m_data.downloads setGuiController:m_dload_controller];
149         
150         //
151         // daemon (either local or remote) must be running by now
152         //
153         // try to connect 3 times, hopefully giving daemon enough
154         // time to start listening for EC
155         //
156         for(int i = 0; i < 3; i++) {
157                 sleep(1);
158                 [m_connection connectToAddress:m_targetaddr withPort:m_targetport];
159                 [m_connection sendLogin:@"123456"];
160                 if ( !m_connection.error ) {
161                         break;
162                 }
163         }
164         if ( m_connection.error ) {
165                 NSRunCriticalAlertPanel(@"Connection error", 
166                         @"Unable to start communication with daemon",
167                         @"OK", nil,nil);
168                 exit(-1);
169         }
174 - (int)startDaemon {
175         int pid = fork();
176         if ( pid == -1 ) {
177                 // fork failed
178                 return -1;
179         } else if ( pid > 0 ) {
180                 sleep(2);
181                 NSLog(@"Parent running, calling waitpid for pid %d\n", pid);
182                 // parent
183                 int status;
184                 switch ( waitpid(pid, &status, WNOHANG) ) {
185                         case -1:
186                                 NSLog(@"waitpid() call failed with code %d\n", errno);
187                                 break;
188                         case 0:
189                                 NSLog(@"Daemon running on pid %d status %d\n", pid, status);
190                                 m_daemon_pid = pid;
191                                 break;
192                         default:
193                                 //NSLog(@"waitpid returned pid=%d status=%x\n", pid, status);
194                                 if ( WIFEXITED(status) ) {
195                                         int exit_code = WEXITSTATUS(status);
196                                         NSLog(@"Daemon exec failed - child process exited with status %d", exit_code);
197                                         return -1;
198                                 } else if ( WIFSIGNALED(status) ) {
199                                         int sig_num = WTERMSIG(status);
200                                         NSLog(@"Child process terminated on signal %d", sig_num);
201                                         return -1;
202                                 } else if ( WIFSTOPPED(status) ) {
203                                         NSLog(@"Child process stopped. Not supposed to happen.");
204                                         return -1;
205                                 } else {
206                                         NSLog(@"Should not get here: child status unknown = %x", status);
207                                 }
208                                 break;
209                 }
210                 return 0;
211         } else {
212                 // child
213                 NSLog(@"Child running, calling execlp\n");
214                 //execlp("/usr/bin/touch", "/usr/bin/touch", "xxxx", 0);
215                 execlp("/Users/lfroen/prog/amule/src/amuled", "/Users/lfroen/prog/amule/src/amuled", 0);
216                 NSLog(@"execlp() failed\n");
217                 exit(-1);
218         }
219         return 0;
222 @end