/*
 * PrintDrop
 * Copyright (c) 2008, Blue Static <http://www.bluestatic.org>
 * 
 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU 
 * General Public License as published by the Free Software Foundation; either version 2 of the 
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without 
 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
 * General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along with this program; if not, 
 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
 */

#import "DraggableImageView.h"


@implementation DraggableImageView

/**
 * Register drag types
 */
- (void)awakeFromNib
{
	[self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
	[pathDisplay setStringValue:@""];
}

/**
 * Returns the current file path
 */
- (NSString *)filePath
{
	return filePath;
}

/**
 * Dealloc
 */
- (void)dealloc
{
	if (filePath != nil)
	{
		[filePath release];
	}
	[super dealloc];
}

/**
 * We're starting to drag; check and see if it's a valid type
 */
- (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
{
	NSPasteboard *pboard = [sender draggingPasteboard];
	NSDragOperation opMask = [sender draggingSourceOperationMask];
	
	if ([[pboard types] containsObject:NSFilenamesPboardType] && (opMask & NSDragOperationCopy))
	{
		NSString *file = [[pboard propertyListForType:NSFilenamesPboardType] objectAtIndex:0];
		if ([[[file substringFromIndex:[file length] - 4] lowercaseString] isEqualToString: @".pdf"])
		{
			return NSDragOperationCopy;
		}
	}
	
	return NSDragOperationNone;
}

/**
 * Dragging performed, set the image and filename
 */
- (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
{
	if ([self draggingEntered:sender] == NSDragOperationCopy)
	{
		NSString *path = [[[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType] objectAtIndex:0];
		
		if (filePath != nil)
		{
			[filePath release];
		}
		filePath = [path retain];
		[pathDisplay setStringValue:filePath];
		
		NSFileWrapper *fw = [[NSFileWrapper alloc] initWithPath:filePath];
		[self setImage:[fw icon]];
		[fw release];
		
		[printButton setEnabled:YES];
		
		return YES;
	}
	return NO;
}

@end
