2 * Adium is the legal property of its developers, whose names are listed in the copyright file included
3 * with this source distribution.
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 License,
7 * or (at your option) any later version.
9 * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
10 * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
11 * Public License for more details.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 #import "AIPreferenceController.h"
18 #import "ESFileTransferController.h"
19 #import "ESFileTransferRequestPromptController.h"
20 #import <AIUtilities/CBApplicationAdditions.h>
21 #import <Adium/AIListContact.h>
22 #import <Adium/ESFileTransfer.h>
23 #import <Adium/ESTextAndButtonsWindowController.h>
24 #import <AIUtilities/AIAttributedStringAdditions.h>
26 @interface ESFileTransferRequestPromptController (PRIVATE)
27 - (id)initForFileTransfer:(ESFileTransfer *)inFileTransfer
28 notifyingTarget:(id)inTarget
29 selector:(SEL)inSelector;
30 - (ESTextAndButtonsWindowController *)windowController;
33 @implementation ESFileTransferRequestPromptController
36 * @brief Display a prompt for a file transfer to save, save as, or cancel
38 * @param inFileTransfer The file transfer
39 * @param inSelector A selector, which must accept two arguments. The first will be inFileTransfer. The second will be the filename to save to, or nil to cancel.
40 * @result The NSWindowController for the displayed prompt
42 + (NSWindowController *)displayPromptForFileTransfer:(ESFileTransfer *)inFileTransfer
43 notifyingTarget:(id)inTarget
44 selector:(SEL)inSelector
46 ESFileTransferRequestPromptController *promptController;
47 NSWindowController *windowController = nil;
49 if ((promptController = [[self alloc] initForFileTransfer:inFileTransfer
50 notifyingTarget:inTarget
51 selector:inSelector])) {
52 windowController = [promptController windowController];
55 return windowController;
58 - (id)initForFileTransfer:(ESFileTransfer *)inFileTransfer
59 notifyingTarget:(id)inTarget
60 selector:(SEL)inSelector
62 if((self = [super init])){
63 fileTransfer = [inFileTransfer retain];
64 target = [inTarget retain];
65 selector = inSelector;
67 NSAttributedString *message;
68 NSString *filenameDisplay;
69 NSString *remoteFilename = [fileTransfer remoteFilename];
71 //Display the name of the file, with the file's size if available
72 unsigned long long fileSize = [fileTransfer size];
75 NSString *fileSizeString;
77 fileSizeString = [[adium fileTransferController] stringForSize:fileSize];
78 filenameDisplay = [NSString stringWithFormat:@"%@ (%@)",remoteFilename,fileSizeString];
80 filenameDisplay = remoteFilename;
83 message = [NSAttributedString stringWithString:
84 [NSString stringWithFormat:AILocalizedString(@"%@ requests to send you %@",nil),
85 [[fileTransfer contact] formattedUID],
88 windowController = [[ESTextAndButtonsWindowController showTextAndButtonsWindowWithTitle:AILocalizedString(@"File Transfer Request",nil)
89 defaultButton:AILocalizedString(@"Save",nil)
90 alternateButton:AILocalizedString(@"Cancel",nil)
91 otherButton:AILocalizedString(@"Save As...",nil)
96 userInfo:nil] retain];
104 [fileTransfer release];
106 [windowController release];
111 - (ESTextAndButtonsWindowController *)windowController
113 return windowController;
117 * @brief Window was closed, either by a button being clicked or the user closing it
119 - (BOOL)textAndButtonsWindowDidEnd:(NSWindow *)window returnCode:(AITextAndButtonsReturnCode)returnCode userInfo:(id)userInfo
121 NSString *localFilename = nil;
125 case AITextAndButtonsDefaultReturn: /* Save */
127 localFilename = [[[adium preferenceController] userPreferredDownloadFolder] stringByAppendingPathComponent:[fileTransfer remoteFilename]];
129 /* If the file doesn't exist, we're done. If it does, fall through to AITextAndButtonsOtherReturn
130 * triggering a Save As... panel.
132 if(![[NSFileManager defaultManager] fileExistsAtPath:localFilename]){
137 case AITextAndButtonsOtherReturn: /* Save As... */
139 //Prompt for a location to save
140 [[[NSSavePanel savePanel] retain] beginSheetForDirectory:[[adium preferenceController] userPreferredDownloadFolder]
141 file:[fileTransfer remoteFilename]
142 modalForWindow:[windowController window]
144 didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:)
148 case AITextAndButtonsAlternateReturn: /* Cancel */
149 case AITextAndButtonsClosedWithoutResponse: /* Closed = Cancel */
151 /* File name remains nil and the transfer will therefore be canceled */
158 [target performSelector:selector
159 withObject:fileTransfer
160 withObject:localFilename];
162 //Release our instance
169 //Don't close the window if we're not finished.
174 - (void)savePanelDidEnd:(NSSavePanel *)savePanel returnCode:(int)returnCode contextInfo:(void *)contextInfo
176 //Only need to take action if the user pressed OK; if she pressed cancel, just return to our window.
177 if(returnCode == NSOKButton){
178 [target performSelector:selector
179 withObject:fileTransfer
180 withObject:[savePanel filename]];
182 //close the prompt on the next run loop (for smooth animation of the sheet going back into the window)
183 [windowController performSelector:@selector(closeWindow:)