2 // FileBrowserViewController.m
5 // Created by Axel Balley on 26/10/08.
6 // Copyright 2008 __MyCompanyName__. All rights reserved.
9 #import "FileBrowserViewController.h"
14 #include <sys/socket.h>
15 #include <sys/types.h>
22 #define FILETRANSFER_PORT 15000
30 @implementation FileTransferController
32 - (id) initWithNibName:(NSString *)name bundle:(NSBundle *)bundle browser:(FileBrowserViewController *)b
34 if (self=[super initWithNibName:name bundle:bundle])
37 service = [[NSNetService alloc] initWithDomain:@"local." type:@"_sctransfer._tcp" name:@"SuperCollider" port:FILETRANSFER_PORT];
40 sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
43 if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(int)))
45 printf("couldn't set option\n");
49 struct sockaddr_in addr;
50 memset(&addr, 0, sizeof(addr));
51 addr.sin_family = AF_INET;
52 addr.sin_addr.s_addr = htonl(INADDR_ANY);
53 addr.sin_port = htons(FILETRANSFER_PORT);
55 if (bind(sock, (sockaddr *) &addr, sizeof(addr)))
57 printf("couldn't bind !\n");
65 printf("couldn't listen\n");
74 - (void) close:(id)sender
76 [browser closeListen];
81 [label setText:@"Waiting for connection..."];
82 [progress setProgress:0.f];
86 - (void) viewWillDisappear:(BOOL)animated
98 thread = [[NSThread alloc] initWithTarget:self selector:@selector(threadProc:) object:nil];
112 - (void) threadProc:(id)arg
114 NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
116 struct sockaddr_in their_addr;
117 int len = sizeof(their_addr);
125 FD_SET(sock, &readfs);
129 if (select(sock+1, &readfs, 0, 0, &t)<=0) continue;
131 int connection_sock = accept(sock, (sockaddr *) &their_addr, (socklen_t *) &len);
132 if (connection_sock<0)
137 [self performSelectorOnMainThread:@selector(updateLabel:) withObject:@"Connected..." waitUntilDone:NO];
140 int ret = recvfrom(connection_sock, &h, sizeof(h), 0, (sockaddr *) &their_addr, (socklen_t *) &len);
143 printf("failed receiving header\n");
144 shutdown(connection_sock, 0);
148 int size = htonl(h.bytes);
149 int name_len = htonl(h.name_len);
151 if (!size || !name_len)
153 shutdown(connection_sock, 0);
157 char *name = (char *) malloc(name_len+1);
158 ret = recvfrom(connection_sock, name, name_len+1, 0, (sockaddr *) &their_addr, (socklen_t *) &len);
161 shutdown(connection_sock, 0);
165 [self performSelectorOnMainThread:@selector(updateLabel:) withObject:[NSString stringWithFormat:@"Receiving %s...", name] waitUntilDone:NO];
166 [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:0.f] waitUntilDone:NO];
168 char *buf = (char *) malloc(size);
173 int ret = recvfrom(connection_sock, buf+read, size-read, 0, (sockaddr *) &their_addr, (socklen_t *) &len);
176 printf("failed receiving !");
177 shutdown(connection_sock, 0);
181 [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:(float)(read/size)*0.5f] waitUntilDone:NO];
187 NSString *path = [browser path];
190 strcpy(output, [path UTF8String]);
193 strcat(output, name);
195 FILE *f = fopen(output, "w");
199 int ret = fwrite(buf, 1, size-written, f);
202 [self performSelectorOnMainThread:@selector(updateProgress:) withObject:[NSNumber numberWithFloat:(float)(written/size)+0.5f] waitUntilDone:NO];
206 [self performSelectorOnMainThread:@selector(updateLabel:) withObject:[NSString stringWithFormat:@"Received %s", name] waitUntilDone:NO];
209 printf("bye bye !\n");
214 - (void) updateLabel:(NSString *)string
216 [label setText:string];
219 - (void) updateProgress:(NSNumber *)val
221 [progress setProgress:[val floatValue]];
233 @implementation FileBrowserViewController
235 - (id)initWithCoder:(NSCoder *)decoder
237 if (self = [super initWithCoder:decoder])
246 - (id) initWithNibName:(NSString *)name bundle:(NSBundle *)bundle
248 if (self = [super initWithNibName:name bundle:bundle])
257 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
264 FileBrowserPageViewController *m = [[FileBrowserPageViewController alloc] initWithNibName:@"SuperCollider_BrowserPage" bundle:nil];
265 [self pushViewController:m animated:NO];
269 - (void) setPath:(NSString *)p
271 FileBrowserPageViewController *m = (FileBrowserPageViewController *) [self.viewControllers objectAtIndex:0];
275 m.title = @"Documents";
279 - (void) setTarget:(id)t withSelector:(SEL)s
285 - (void) didSelect:(NSString *) path
287 if (target && [target respondsToSelector:selector]) [target performSelector:selector withObject:path];
292 if (target) [target release];
298 @implementation FileBrowserPageViewController
300 - (id) initWithNibName:(NSString *)name bundle:(NSBundle *)bundle
302 if (self = [super initWithNibName:name bundle:bundle])
310 - (BOOL) shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)toInterfaceOrientation
315 - (void) setPath:(NSString *)p
317 if (path) [path release];
320 self.title = [path lastPathComponent];
327 self.navigationItem.rightBarButtonItem = refreshButton;
335 if (array) [array release];
336 array = [[[NSFileManager defaultManager] contentsOfDirectoryAtPath:path error:nil] retain];
341 - (void) triggerRefresh:(id)sender
346 - (void) flashScrollIndicators
348 [table flashScrollIndicators];
351 - (NSInteger) tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
353 return [array count];
356 - (UITableViewCell *) tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
358 UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"MyCell"];
361 cell = [[[UITableViewCell alloc] initWithFrame:CGRectZero reuseIdentifier:@"MyCell"] autorelease];
363 NSString *item = (NSString *)[array objectAtIndex:indexPath.row];
365 NSString *fullpath = [path stringByAppendingPathComponent:item];
366 NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullpath error:nil];
367 if ([attributes objectForKey:NSFileType]==NSFileTypeDirectory)
369 cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
371 else cell.accessoryType = UITableViewCellAccessoryNone;
375 - (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)newIndexPath
377 NSString *fullpath = [path stringByAppendingPathComponent:[array objectAtIndex:newIndexPath.row]];
379 NSDictionary *attributes = [[NSFileManager defaultManager] attributesOfItemAtPath:fullpath error:nil];
380 if ([attributes objectForKey:NSFileType]==NSFileTypeDirectory)
382 [tableView deselectRowAtIndexPath:newIndexPath animated:NO];
383 FileBrowserPageViewController *c = [[FileBrowserPageViewController alloc] initWithNibName:@"SuperCollider_BrowserPage" bundle:nil];
384 [c setPath:fullpath];
385 [self.navigationController pushViewController:c animated:YES];
390 [tableView deselectRowAtIndexPath:newIndexPath animated:YES];
391 [(FileBrowserViewController *) self.navigationController didSelect:fullpath];
396 if (path) [path release];
397 if (array) [array release];