Upstream tarball 10080
[amule.git] / cocoa-mule / AppController.mm
blob31534d145a2e75e0e7f6279f3cd7b8dad34ff369
1 #import "AppController.h"
3 #import "LoginDialogController.h"
4 #import "DownloadsViewController.h"
5 #import "AddLinkDialogController.h"
7 #include <unistd.h>
8 #include <signal.h>
10 @implementation AppController
12 - (IBAction)show_Networks:(id)sender {
13         [m_main_tabview selectTabViewItemAtIndex: 2];
14         
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];
21 //      [stream open];
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];
53         if ( dlgResult ) {
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];
57         }
60 - (bool)askCoreParams {
61         LoginDialogController *dlg = [[LoginDialogController alloc] init];
62         
63         bool dlgResult = [dlg showDlg:nil];
65         m_targetaddr = dlg.host;
66         m_targetport = dlg.port;
67         m_corepass = dlg.pass;
68         
69         return dlgResult;
72 - (void)applicationWillTerminate:(NSNotification *)aNotification {
73         //
74         // Save gui state in all views
75         //
76         if ([m_dload_controller respondsToSelector:@selector(saveGui)]) {
77                 [m_dload_controller performSelector:@selector(saveGui)];
78         }
79         
80         //
81         // Save main window
82         //
83         [[NSUserDefaults standardUserDefaults] setInteger:[m_main_tabview indexOfTabViewItem: [m_main_tabview selectedTabViewItem] ] forKey:@"activeTab"];
84         
85         //
86         // If we have slave daemon process, terminate it
87         //
88         if ( m_daemon_pid ) {
89                 //
90                 // started in local mode - terminate daemon
91                 //
92                 kill(m_daemon_pid, SIGTERM);
93         }
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);
108         
109         
110         [[NSApplication sharedApplication] setDelegate:self];
111         
112         [self restoreMainWindow];
113         
114         m_targetaddr = 0;
115         m_targetport = 0;
116         m_corepass = nil;
117         
118         m_daemon_pid = 0;
119         if ( (mode != nil) && ([mode compare:@"guitest"] == NSOrderedSame) ) {
120                 NSLog(@"Started in GUI test mode - will not connect to core");
121                 return;
122         }       
124         if ( (mode != nil) && ([mode compare:@"remote"] == NSOrderedSame) ) {
125                 m_targetaddr = @"127.0.0.1";
126                 m_targetaddr = @"localhost";
127                 m_targetport = 4712;
128                 if (![self askCoreParams] ) {
129                         // "Cancel" selected on login dialog
130                         [[NSApplication sharedApplication] terminate:self];
131                 }
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);
136         } else {
137                 NSLog(@"Local mode selected - starting daemon\n");
138                 m_targetaddr = @"127.0.0.1";
139                 m_targetport = 4712;
140                 if ( [self startDaemon] == -1 ) {
141                         NSRunCriticalAlertPanel(@"Daemon startup error", 
142                                                         @"Unable to start core daemon (amuled)",
143                                                         @"OK", nil,nil);
144                         [[NSApplication sharedApplication] terminate:self];
145                 }
146         }
147         
148         m_connection = [ECRemoteConnection remoteConnection];
149         [m_connection retain];
150         m_data = [amuleData initWithConnection:m_connection];
151         [m_data retain];
153         //
154         // bind datastructure to GUI controllers
155         //
156         [m_dload_controller linkAmuleData:m_data];
157         [m_search_controller linkAmuleData:m_data];
158         [m_shared_controller linkAmuleData:m_data];
159         //
160         // daemon (either local or remote) must be running by now
161         //
162         // try to connect 3 times, hopefully giving daemon enough
163         // time to start listening for EC
164         //
165         for(int i = 0; i < 3; i++) {
166                 sleep(1);
167                 [m_connection connectToAddress:m_targetaddr withPort:m_targetport];
168                 [m_connection sendLogin:@"123456"];
169                 if ( !m_connection.error ) {
170                         break;
171                 }
172         }
173         if ( m_connection.error ) {
174                 NSRunCriticalAlertPanel(@"Connection error", 
175                         @"Unable to start communication with daemon",
176                         @"OK", nil,nil);
177                 exit(-1);
178         }
183 - (int)startDaemon {
184         int pid = fork();
185         if ( pid == -1 ) {
186                 // fork failed
187                 return -1;
188         } else if ( pid > 0 ) {
189                 sleep(2);
190                 NSLog(@"Parent running, calling waitpid for pid %d\n", pid);
191                 // parent
192                 int status;
193                 switch ( waitpid(pid, &status, WNOHANG) ) {
194                         case -1:
195                                 NSLog(@"waitpid() call failed with code %d\n", errno);
196                                 break;
197                         case 0:
198                                 NSLog(@"Daemon running on pid %d status %d\n", pid, status);
199                                 m_daemon_pid = pid;
200                                 break;
201                         default:
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);
206                                         return -1;
207                                 } else if ( WIFSIGNALED(status) ) {
208                                         int sig_num = WTERMSIG(status);
209                                         NSLog(@"Child process terminated on signal %d", sig_num);
210                                         return -1;
211                                 } else if ( WIFSTOPPED(status) ) {
212                                         NSLog(@"Child process stopped. Not supposed to happen.");
213                                         return -1;
214                                 } else {
215                                         NSLog(@"Should not get here: child status unknown = %x", status);
216                                 }
217                                 break;
218                 }
219                 return 0;
220         } else {
221                 // child
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");
226                 exit(-1);
227         }
228         return 0;
231 @end