#import "ApplicationController.h"

@implementation ApplicationController

-(PreferencesController*)preferencesController
{
    return preferencesController;
}

- (void) netServiceWillPublish: (NSNetService *) netService {
    //NSRunAlertPanel(@"Rendezvous service published.", @"You can now reach the service on the network using the name \"CyTV\".", @"OK", nil, nil);
}

- (void) netService: (NSNetService *) netService
         didNotPublish: (NSDictionary *) errorDict {
    // show an error message
    NSRunAlertPanel(@"Could not publish a Rendezvous service.", @"You can still access the server via IP address or computer name.", @"OK", nil, nil);
}

- (void) netServiceDidStop: (NSNetService *) netService {
    // ignore
}

- (NSNetService *)publishServiceWithPort:(int)port {
    NSString* serviceName = [[NSHost currentHost] name];
    if ([serviceName hasSuffix: @".local."]) {
        serviceName = [serviceName substringToIndex: [serviceName length] - 7];
    }
    if ([serviceName hasSuffix: @".local"]) {
        serviceName = [serviceName substringToIndex: [serviceName length] - 6];
    }
    NSNetService * netService = [[NSNetService alloc] initWithDomain: @""
                                                      type: @"_cytv._tcp"
                                                      name: serviceName
                                                      port: port];
    [netService setDelegate:self];
    [netService publish];
    
    return netService;
}


/**
 * Displays an error message.
 */
- (void)displayError:(NSString*)title details:(NSString*)details {
    NSRunAlertPanel(title, details, nil, nil, nil);
}


/**
 * Writes the host's RSA public key to a file.
 */

- (IBAction)exportServerKey:(id)sender {
	NSString* serverKeyFile = [@"~/Library/Application Support/CyTV/server_rsa_key.pub"
							   stringByExpandingTildeInPath];
    NSSavePanel* savePanel = [NSSavePanel savePanel];
    [savePanel setRequiredFileType:@"ssh_key"];
    [savePanel setTitle:@"Export server key"];
    [savePanel setCanSelectHiddenExtension:YES];
    if ([savePanel runModal] == NSFileHandlingPanelOKButton) {
        NSString* serverKey =
            [NSString stringWithContentsOfFile:serverKeyFile];
        if (serverKey == nil) {
            NSRunAlertPanel(@"Cannot read the RSA host key file",
                            @"Please check whether the file '%@' is readable by the current user.",
                            nil, nil, nil, serverKeyFile);
            return;
        }
        NSArray* serverKeyComponents =
            [serverKey componentsSeparatedByString:@" "];
        if ([serverKeyComponents count] < 2) {
            NSRunAlertPanel(@"Cannot parse the RSA host key file",
                            @"The file looks like this: '%@'.",
                            nil, nil, nil, serverKey);
            return;
        }
        serverKey = [[[[serverKeyComponents objectAtIndex:0]
                       stringByAppendingString:@" "]
                      stringByAppendingString:
                      [serverKeyComponents objectAtIndex:1]]
                     stringByAppendingString:@"\n"];
        NSString* filename = [savePanel filename];
        if (![[serverKey dataUsingEncoding:NSUTF8StringEncoding]
              writeToFile:filename atomically:NO]) {
            NSRunAlertPanel(@"Cannot save the server key",
                            @"Please check if you have permission to write the file '%@'.",
                            nil, nil, nil, filename);
            return;
        }
    }
}

@end
