/*@DISCLAIMER@*/ // $Id$ #import "ConnectionController.h" #import "common.h" #import "NSApplication+Extensions.h" #import "DAVOperation.h" @interface ConnectionController (PrivateAPI) - (void)setup; - (void)updateTableColumns; @end @implementation ConnectionController + (id)controller { return [[self alloc] init]; /* LEAK -> windowDidClose gets this straight */ } - (id)init { self = [super init]; if(self) { [NSBundle loadNibNamed:@"Connection" owner:self]; NSAssert(self->window, @"Problem loading Connection.nib!"); } return self; } - (void)dealloc { [self->urls release]; [self->properties release]; [self->content release]; [super dealloc]; } /* setup */ - (void)setup { self->properties = [[NSMutableArray alloc] initWithCapacity:6]; self->content = [[NSMutableArray alloc] initWithCapacity:20]; } - (void)awakeFromNib { [self setup]; self->urls = [[[NSApp sharedController] urls] mutableCopy]; [self->urlComboBox reloadData]; if([self->urls count] > 0) [self->urlComboBox selectItemAtIndex:0]; [self updateTableColumns]; } - (void)updateTableColumns { NSArray *tableColumns; unsigned i, count; tableColumns = [[self->contentTableView tableColumns] copy]; count = [tableColumns count]; for(i = 0; i < count; i++) { NSTableColumn *tc = [tableColumns objectAtIndex:i]; [self->contentTableView removeTableColumn:tc]; } [tableColumns release]; tableColumns = [[NSApp sharedController] properties]; count = [tableColumns count]; for(i = 0; i < count; i++) { NSTableColumn *tc; NSDictionary *d; NSString *prop; d = [tableColumns objectAtIndex:i]; prop = [d objectForKey:@"propname"]; tc = [[NSTableColumn alloc] initWithIdentifier:prop]; [[tc headerCell] setStringValue:prop]; [self->contentTableView addTableColumn:tc]; [tc release]; [self->properties addObject:[[d mutableCopy] autorelease]]; } [self->contentTableView reloadData]; } /* helper */ - (NSArray *)davProperties { NSMutableArray *a; unsigned i, count; count = [self->properties count]; a = [NSMutableArray arrayWithCapacity:count]; for(i = 0; i < count; i++) { NSDictionary *d; NSArray *p; d = [self->properties objectAtIndex:i]; p = [NSArray arrayWithObjects:[d objectForKey:@"ns"], [d objectForKey:@"propname"], nil]; [a addObject:p]; } return a; } /* Actions */ - (IBAction)connectToURL:(id)sender { NSString *url; DAVOperation *dj; url = [self->urlComboBox stringValue]; if(![self->urls containsObject:url]) { [self->urls addObject:url]; [self->urlComboBox reloadData]; [[NSApp sharedController] saveURLs:self->urls]; } dj = [[DAVPropFindOperation alloc] initWithURL:url andPropertyNames:[self davProperties]]; [dj setDelegate:self]; [self->content removeAllObjects]; NSLog(@"%s start", __PRETTY_FUNCTION__); [dj start]; } - (IBAction)configureProperties:(id)sender { [NSApp beginSheet:self->configurePropertiesPanel modalForWindow:self->window modalDelegate:self didEndSelector:@selector(sheetDidEnd:returnCode:contextInfo:) contextInfo:NULL]; } - (void)sheetDidEnd:(NSWindow *)_sheet returnCode:(int)_rc contextInfo:(void *)_context { [_sheet orderOut:self]; } - (IBAction)addProperty:(id)sender { NSMutableDictionary *d; NSTableColumn *tc; unsigned idx; d = [[NSMutableDictionary alloc] initWithCapacity:2]; [d setObject:@"" forKey:@"propname"]; [d setObject:@"DAV:" forKey:@"ns"]; [self->properties addObject:d]; [d release]; [self->propertiesTableView reloadData]; idx = [self->properties count] - 1; [self->propertiesTableView selectRow:idx byExtendingSelection:NO]; [self->propertiesTableView editColumn:0 row:idx withEvent:nil select:YES]; tc = [[NSTableColumn alloc] initWithIdentifier:@""]; [self->contentTableView addTableColumn:tc]; [tc release]; [self->contentTableView reloadData]; } - (IBAction)removeProperty:(id)sender { NSString *ident; NSTableColumn *tc; unsigned idx; idx = [self->propertiesTableView selectedRow]; if(idx == NSNotFound) { NSBeep(); return; } ident = [[self->properties objectAtIndex:idx] objectForKey:@"propname"]; [self->properties removeObjectAtIndex:idx]; [self->propertiesTableView reloadData]; tc = [self->contentTableView tableColumnWithIdentifier:ident]; [self->contentTableView removeTableColumn:tc]; [self->contentTableView reloadData]; } - (IBAction)endConfigurePropertiesPanel:(id)sender { [NSApp endSheet:self->configurePropertiesPanel]; } - (void)showWindow { [self->window makeKeyAndOrderFront:self]; } /* NSWindow delegate */ - (void)windowWillClose:(NSNotification *)_notif { AppController *c; c = [NSApp sharedController]; [c saveURLs:self->urls]; [c saveProperties:self->properties]; [self autorelease]; } /* NSTableView DATASOURCE */ - (int)numberOfRowsInTableView:(NSTableView *)_tv { if(_tv != self->propertiesTableView) return [self->content count]; return [self->properties count]; } - (id)tableView:(NSTableView *)_tv objectValueForTableColumn:(NSTableColumn *)_column row:(int)_rowIdx { NSDictionary *d; NSString *ident; id value; if(_tv == self->propertiesTableView) d = [self->properties objectAtIndex:_rowIdx]; else d = [self->content objectAtIndex:_rowIdx]; if(!d) return @""; ident = [_column identifier]; if(!ident) return @""; value = [d valueForKey:ident]; if(!value) return @""; return value; } - (void)tableView:(NSTableView *)_tv setObjectValue:(id)_value forTableColumn:(NSTableColumn *)_column row:(int)_rowIdx { if(_tv == self->propertiesTableView) { NSMutableDictionary *d; NSString *ident; d = [self->properties objectAtIndex:_rowIdx]; ident = [_column identifier]; if([ident isEqualToString:@"propname"]) { NSTableColumn *tc; NSString *oldId; oldId = [d objectForKey:ident]; tc = [self->contentTableView tableColumnWithIdentifier:oldId]; if(tc) { [tc setIdentifier:_value]; [[tc headerCell] setStringValue:_value]; [self->contentTableView reloadData]; } } [d setObject:_value forKey:ident]; } } /* NSTableView delegate */ - (void)tableViewSelectionDidChange:(NSNotification *)_notif { NSTableView *tv; tv = [_notif object]; if(tv == self->propertiesTableView) { BOOL hasSelection = ([tv selectedRow] != -1 ? YES : NO); [self->removePropertyButton setEnabled:hasSelection]; } } - (BOOL)tableView:(NSTableView *)_tv shouldEditTableColumn:(NSTableColumn *)_column row:(int)_rowIdx { return _tv == self->propertiesTableView; } /* NSComboBox DATASOURCE */ - (int)numberOfItemsInComboBox:(NSComboBox *)_cb { return [self->urls count]; } - (id)comboBox:(NSComboBox *)_comboBox objectValueForItemAtIndex:(int)_idx { return [self->urls objectAtIndex:_idx]; } /* DAVOp */ - (void)operation:(DAVOperation *)_op didReceiveProperties:(NSDictionary *)_props forURI:(NSString *)_uri { NSMutableDictionary *d; NSEnumerator *keyEnum; NSString *key; unsigned count; count = [self->properties count]; d = [[NSMutableDictionary alloc] initWithCapacity:count]; keyEnum = [_props keyEnumerator]; while((key = [keyEnum nextObject])) { NSString *ident; NSString *value; NSRange r; value = [_props objectForKey:key]; r = [key rangeOfString:@"}"]; /* strip namespace */ if(r.length > 0) { ident = [key substringFromIndex:NSMaxRange(r)]; } else { ident = key; } [d setObject:value forKey:ident]; #if 0 NSLog(@"got: %@", d); #endif } [self->content addObject:d]; [d release]; #if 0 NSLog(@"%s props:%@", __PRETTY_FUNCTION__, _props); #endif } - (void)operation:(DAVOperation *)_op provideLogin:(NSString **)_login andPassword:(NSString **)_pw { #if 1 *_login = @"test.et.di.cete-lyon"; *_pw = @"test"; #else *_login = @"helge"; *_pw = @"rxroot"; #endif } - (void)operationDidFinish:(DAVOperation *)_op { [self->contentTableView reloadData]; [_op autorelease]; } @end