1 #import "AppController.h"
3 #import "LoginDialogController.h"
4 #import "DownloadsViewController.h"
5 #import "AddLinkDialogController.h"
10 @implementation AppController
12 - (IBAction)show_Networks:(id)sender {
13 [m_main_tabview selectTabViewItemAtIndex: 2];
16 // ECLoginPacket *p = [ECLoginPacket loginPacket:@"123456" withVersion:@"0.1"];
17 // NSOutputStream *stream = [NSOutputStream outputStreamToMemory];
18 // uint8_t buffer[1024];
19 // memset(buffer, 0, 1024);
20 // NSOutputStream *stream = [NSOutputStream outputStreamToBuffer:buffer capacity:1024];
22 // [p writeToSocket:stream];
23 // id data = [stream propertyForKey:NSStreamDataWrittenToMemoryStreamKey];
24 // int off = [data length];
25 // NSLog(@"off=%d", off);
26 // [m_connection sendPacket:p];
29 - (IBAction)show_Search:(id)sender {
30 [m_main_tabview selectTabViewItemAtIndex: 1];
33 - (IBAction)show_Sharing:(id)sender {
34 [m_main_tabview selectTabViewItemAtIndex: 3];
37 - (IBAction)show_Stats:(id)sender {
38 [m_main_tabview selectTabViewItemAtIndex: 4];
41 - (IBAction)show_Xfers:(id)sender {
42 [m_main_tabview selectTabViewItemAtIndex: 0];
45 -(IBAction)show_Preferences:(id)sender {
48 -(IBAction)show_About:(id)sender {
51 - (IBAction)addLink:(id)sender;{
52 bool dlgResult = [m_add_link_dlg showDlg:nil];
54 ECPacket *packet = [ECPacket packetWithOpcode:EC_OP_ADD_LINK];
55 [packet.subtags addObject:[ECTagString tagFromString:[m_add_link_dlg link] withName:EC_TAG_ED2K_ID] ];
56 [m_connection sendPacket:packet];
60 - (bool)askCoreParams {
61 LoginDialogController *dlg = [[LoginDialogController alloc] init];
63 bool dlgResult = [dlg showDlg:nil];
65 m_targetaddr = dlg.host;
66 m_targetport = dlg.port;
67 m_corepass = dlg.pass;
72 - (void)applicationWillTerminate:(NSNotification *)aNotification {
74 // Save gui state in all views
76 if ([m_dload_controller respondsToSelector:@selector(saveGui)]) {
77 [m_dload_controller performSelector:@selector(saveGui)];
83 [[NSUserDefaults standardUserDefaults] setInteger:[m_main_tabview indexOfTabViewItem: [m_main_tabview selectedTabViewItem] ] forKey:@"activeTab"];
86 // If we have slave daemon process, terminate it
90 // started in local mode - terminate daemon
92 kill(m_daemon_pid, SIGTERM);
95 NSLog(@"Exiting ...\n");
98 - (void)restoreMainWindow {
99 int activeTab = [[NSUserDefaults standardUserDefaults] integerForKey:@"activeTab"];
100 [m_main_tabview selectTabViewItemAtIndex: activeTab];
104 - (void)awakeFromNib {
105 NSUserDefaults *args = [NSUserDefaults standardUserDefaults];
106 NSString *mode = [args stringForKey:@"mode"];
107 NSLog(@"amule controller started, mode = [%@]\n", mode);
110 [[NSApplication sharedApplication] setDelegate:self];
112 [self restoreMainWindow];
119 if ( (mode != nil) && ([mode compare:@"guitest"] == NSOrderedSame) ) {
120 NSLog(@"Started in GUI test mode - will not connect to core");
124 if ( (mode != nil) && ([mode compare:@"remote"] == NSOrderedSame) ) {
125 m_targetaddr = @"127.0.0.1";
126 m_targetaddr = @"localhost";
128 if (![self askCoreParams] ) {
129 // "Cancel" selected on login dialog
130 [[NSApplication sharedApplication] terminate:self];
132 [[NSUserDefaults standardUserDefaults] setObject:m_targetaddr forKey:@"LastTargetHost"];
133 [[NSUserDefaults standardUserDefaults] setInteger:m_targetport forKey:@"LastTargetPort"];
134 [[NSUserDefaults standardUserDefaults] synchronize];
135 NSLog(@"Remote mode selected, target=%@:%d\n", m_targetaddr, m_targetport);
137 NSLog(@"Local mode selected - starting daemon\n");
138 m_targetaddr = @"127.0.0.1";
140 if ( [self startDaemon] == -1 ) {
141 NSRunCriticalAlertPanel(@"Daemon startup error",
142 @"Unable to start core daemon (amuled)",
144 [[NSApplication sharedApplication] terminate:self];
148 m_connection = [ECRemoteConnection remoteConnection];
149 [m_connection retain];
150 m_data = [amuleData initWithConnection:m_connection];
154 // bind datastructure to GUI controllers
156 [m_dload_controller linkAmuleData:m_data];
157 [m_search_controller linkAmuleData:m_data];
158 [m_shared_controller linkAmuleData:m_data];
160 // daemon (either local or remote) must be running by now
162 // try to connect 3 times, hopefully giving daemon enough
163 // time to start listening for EC
165 for(int i = 0; i < 3; i++) {
167 [m_connection connectToAddress:m_targetaddr withPort:m_targetport];
168 [m_connection sendLogin:@"123456"];
169 if ( !m_connection.error ) {
173 if ( m_connection.error ) {
174 NSRunCriticalAlertPanel(@"Connection error",
175 @"Unable to start communication with daemon",
188 } else if ( pid > 0 ) {
190 NSLog(@"Parent running, calling waitpid for pid %d\n", pid);
193 switch ( waitpid(pid, &status, WNOHANG) ) {
195 NSLog(@"waitpid() call failed with code %d\n", errno);
198 NSLog(@"Daemon running on pid %d status %d\n", pid, status);
202 //NSLog(@"waitpid returned pid=%d status=%x\n", pid, status);
203 if ( WIFEXITED(status) ) {
204 int exit_code = WEXITSTATUS(status);
205 NSLog(@"Daemon exec failed - child process exited with status %d", exit_code);
207 } else if ( WIFSIGNALED(status) ) {
208 int sig_num = WTERMSIG(status);
209 NSLog(@"Child process terminated on signal %d", sig_num);
211 } else if ( WIFSTOPPED(status) ) {
212 NSLog(@"Child process stopped. Not supposed to happen.");
215 NSLog(@"Should not get here: child status unknown = %x", status);
222 NSLog(@"Child running, calling execlp\n");
223 //execlp("/usr/bin/touch", "/usr/bin/touch", "xxxx", 0);
224 execlp("/Users/lfroen/prog/amule/src/amuled", "/Users/lfroen/prog/amule/src/amuled", 0);
225 NSLog(@"execlp() failed\n");