1 #import "AppController.h"
3 #import "LoginDialogController.h"
4 #import "DownloadsViewController.h"
9 @implementation AppController
11 - (IBAction)show_Networks:(id)sender {
12 [m_main_tabview selectTabViewItemAtIndex: 2];
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];
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];
53 bool dlgResult = [dlg showDlg:nil];
55 m_targetaddr = dlg.host;
56 m_targetport = dlg.port;
57 m_corepass = dlg.pass;
62 - (void)applicationWillTerminate:(NSNotification *)aNotification {
64 // Save gui state in all views
66 if ([m_dload_controller respondsToSelector:@selector(saveGui)]) {
67 [m_dload_controller performSelector:@selector(saveGui)];
73 [[NSUserDefaults standardUserDefaults] setInteger:[m_main_tabview indexOfTabViewItem: [m_main_tabview selectedTabViewItem] ] forKey:@"activeTab"];
76 // If we have slave daemon process, terminate it
80 // started in local mode - terminate daemon
82 kill(m_daemon_pid, SIGTERM);
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);
100 [[NSApplication sharedApplication] setDelegate:self];
102 [self restoreMainWindow];
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");
115 if ( (mode != nil) && ([mode compare:@"remote"] == NSOrderedSame) ) {
116 m_targetaddr = @"127.0.0.1";
117 m_targetaddr = @"localhost";
119 if (![self askCoreParams] ) {
120 // "Cancel" selected on login dialog
121 [[NSApplication sharedApplication] terminate:self];
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);
128 NSLog(@"Local mode selected - starting daemon\n");
129 m_targetaddr = @"127.0.0.1";
131 if ( [self startDaemon] == -1 ) {
132 NSRunCriticalAlertPanel(@"Daemon startup error",
133 @"Unable to start core daemon (amuled)",
135 [[NSApplication sharedApplication] terminate:self];
139 m_connection = [ECRemoteConnection remoteConnection];
140 [m_connection retain];
141 m_data = [amuleData initWithConnection:m_connection];
145 // bind datastructure to GUI controllers
147 [m_dload_controller setFileSet:m_data.downloads];
148 [m_data.downloads setGuiController:m_dload_controller];
151 // daemon (either local or remote) must be running by now
153 // try to connect 3 times, hopefully giving daemon enough
154 // time to start listening for EC
156 for(int i = 0; i < 3; i++) {
158 [m_connection connectToAddress:m_targetaddr withPort:m_targetport];
159 [m_connection sendLogin:@"123456"];
160 if ( !m_connection.error ) {
164 if ( m_connection.error ) {
165 NSRunCriticalAlertPanel(@"Connection error",
166 @"Unable to start communication with daemon",
179 } else if ( pid > 0 ) {
181 NSLog(@"Parent running, calling waitpid for pid %d\n", pid);
184 switch ( waitpid(pid, &status, WNOHANG) ) {
186 NSLog(@"waitpid() call failed with code %d\n", errno);
189 NSLog(@"Daemon running on pid %d status %d\n", pid, status);
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);
198 } else if ( WIFSIGNALED(status) ) {
199 int sig_num = WTERMSIG(status);
200 NSLog(@"Child process terminated on signal %d", sig_num);
202 } else if ( WIFSTOPPED(status) ) {
203 NSLog(@"Child process stopped. Not supposed to happen.");
206 NSLog(@"Should not get here: child status unknown = %x", status);
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");