Clear the file after uploading (so people can't print the same document twice)
[printdrop.git] / Source / DraggableImageView.m
1 /*
2 * PrintDrop
3 * Copyright (c) 2008, Blue Static <http://www.bluestatic.org>
4 *
5 * This program is free software; you can redistribute it and/or modify it under the terms of the GNU
6 * General Public License as published by the Free Software Foundation; either version 2 of the
7 * License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
10 * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along with this program; if not,
14 * write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
15 */
16
17 #import "DraggableImageView.h"
18
19
20 @implementation DraggableImageView
21
22 /**
23 * Register drag types
24 */
25 - (void)awakeFromNib
26 {
27 [self registerForDraggedTypes:[NSArray arrayWithObject:NSFilenamesPboardType]];
28 [pathDisplay setStringValue:@""];
29 }
30
31 /**
32 * Returns the current file path
33 */
34 - (NSString *)filePath
35 {
36 return filePath;
37 }
38
39 /**
40 * Clears the file from selection
41 */
42 - (void)clearFile
43 {
44 [filePath release];
45 filePath = nil;
46 [pathDisplay setStringValue:@""];
47 [self setImage:nil];
48 [printButton setEnabled:NO];
49 }
50
51 /**
52 * Dealloc
53 */
54 - (void)dealloc
55 {
56 if (filePath != nil)
57 {
58 [filePath release];
59 }
60 [super dealloc];
61 }
62
63 /**
64 * We're starting to drag; check and see if it's a valid type
65 */
66 - (NSDragOperation)draggingEntered:(id <NSDraggingInfo>)sender
67 {
68 NSPasteboard *pboard = [sender draggingPasteboard];
69 NSDragOperation opMask = [sender draggingSourceOperationMask];
70
71 if ([[pboard types] containsObject:NSFilenamesPboardType] && (opMask & NSDragOperationCopy))
72 {
73 NSString *file = [[pboard propertyListForType:NSFilenamesPboardType] objectAtIndex:0];
74 if ([[[file substringFromIndex:[file length] - 4] lowercaseString] isEqualToString: @".pdf"])
75 {
76 return NSDragOperationCopy;
77 }
78 }
79
80 return NSDragOperationNone;
81 }
82
83 /**
84 * Dragging performed, set the image and filename
85 */
86 - (BOOL)performDragOperation:(id <NSDraggingInfo>)sender
87 {
88 if ([self draggingEntered:sender] == NSDragOperationCopy)
89 {
90 NSString *path = [[[sender draggingPasteboard] propertyListForType:NSFilenamesPboardType] objectAtIndex:0];
91
92 if (filePath != nil)
93 {
94 [filePath release];
95 }
96 filePath = [path retain];
97 [pathDisplay setStringValue:filePath];
98
99 NSFileWrapper *fw = [[NSFileWrapper alloc] initWithPath:filePath];
100 [self setImage:[fw icon]];
101 [fw release];
102
103 [printButton setEnabled:YES];
104
105 return YES;
106 }
107 return NO;
108 }
109
110 @end