#import "imageFileView.h"

@interface NSObject (fudge)
- (void)imageViewDidFinishEditing:(imageFileView *)view;
@end

@implementation imageFileView

- (void)awakeFromNib
{
    //myDelegate = nil;
    filepath = nil;
}

- (NSString *)filepath
{
    return filepath;
}

- (id)delegate
{
    return myDelegate;
}

- (void) setDelegate:(id)del
{
    [myDelegate release];
    myDelegate = [del retain];
}

- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
    NSArray *sufs = [NSArray arrayWithObjects:@".jpg", @".jpeg", @".gif", @".tiff", @".tif", @".pct", @".pict", @".png", nil];
    NSPasteboard *pboard = [sender draggingPasteboard];
    if ([[pboard types] containsObject:NSFilenamesPboardType]) {
        NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
        if ([files count]) {
            NSString *path = [files objectAtIndex:0];
            int i, n = [sufs count];
            for (i=0;i<n;i++) {
                if ([[path lowercaseString] hasSuffix:[sufs objectAtIndex:i]])
                    return NSDragOperationCopy;
            }
        }
    }
    return NSDragOperationNone;
}

- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
    //NSLog(@"dragged");
    NSPasteboard *pboard = [sender draggingPasteboard];
    //NSDragOperation sourceDragMask = [sender draggingSourceOperationMask];
    if ( [[pboard types] containsObject:NSFilenamesPboardType] ) {
        NSArray *files = [pboard propertyListForType:NSFilenamesPboardType];
        if ([files count]) {
            NSString *path = [files objectAtIndex:0];
            NSImage *image = [[NSImage alloc] initWithContentsOfFile:path];
            if (image) {
                [filepath release];
                filepath = [path retain];
                [image release];
                if ([[self delegate] respondsToSelector:@selector(imageViewDidFinishEditing:)])
                    [[self delegate] imageViewDidFinishEditing:self];
                return YES;
            }
        }
    }
    return NO;
}

@end
