//
//  DockCamStatus.m
//  DockCam
//
//  Created by Nicholas Riley on Thu Jun 27 2002.
//  Copyright (c) 2002 Nicholas Riley. All rights reserved.
//

#import "DockCamStatus.h"
#import "DockCamPrefs.h"

@implementation DockCamStatus

- (void)awakeFromNib;
{
    NSEnumerator *e = [[statusForm cells] objectEnumerator];
    NSCell *cell;
    while ( (cell = [e nextObject]) != nil) {
        [cell setFont: [NSFont systemFontOfSize: [NSFont smallSystemFontSize]]];
    }

    // XXX workaround for bug in 10.1.5 (and earlier?): autosave name set in IB doesn't show up
    [self setWindowFrameAutosaveName: @"DockCam Status"];
    [[self window] setHidesOnDeactivate: YES];
        
    if ([DockCamPrefs boolForPref: DCShowStatus])
        [self showWindow: self];
}

- (IBAction)showWindow:(id)sender;
{
    [DockCamPrefs setBool: YES forPref: DCShowStatus];
    [showHideStatusItem setAction: @selector(hideWindow:)];
    [showHideStatusItem setTitle: @"Hide Status"];
    // No need to subscribe to notifications, as the delegate we're already subscribed
    [[self window] orderFront: self];
}

- (IBAction)hideWindow:(id)sender;
{
    [[self window] orderOut: sender];
    [DockCamPrefs setBool: NO forPref: DCShowStatus];
    [showHideStatusItem setAction: @selector(showWindow:)];
    [showHideStatusItem setTitle: @"Show Status"];
}

- (void)windowWillClose:(NSNotification *)aNotification;
{
    [self hideWindow: self];
}

#pragma mark status changes

- (void)setLocation:(NSURL *)location;
{
    [locationCell setStringValue: [location absoluteString]];
    [actualSize setStringValue: @""];
    [retrievedDate setStringValue: @"never"];
    [failedDate setStringValue: @"never"];
    [failedReason setStringValue: @""];
}

- (void)setStatus:(NSString *)status;
{
    [statusCell setStringValue: status];
}

- (void)setRetrievedWithSize:(NSSize)size;
{
    static const unichar timesUnichar = 0x00d7;
    [retrievedDate setObjectValue: [NSDate date]];
    [self setStatus: @"Image retrieved successfully"];
    [actualSize setStringValue: [NSString stringWithFormat: @"%.0f %@ %.0f pixels",
        size.width, [NSString stringWithCharacters: &timesUnichar length: 1], size.height]];
}

- (void)setFailedWithReason:(NSString *)reason;
{
    [failedDate setObjectValue: [NSDate date]];
    [failedReason setStringValue: reason];
    [self setStatus: @"Image retrieval failed"];
    if (![[self window] isVisible])
        [self showWindow: self];
}

@end
