AIHyperlinks universal building
[adiumx.git] / Source / ESFileTransferRequestPromptController.m
blobaba32f67f02e9f890dc5b69be13dc010f7bc81d9
1 /*
2  * Adium is the legal property of its developers, whose names are listed in the copyright file included
3  * with this source distribution.
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 License,
7  * 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 even
10  * the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
11  * 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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
15  */
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;
31 @end
33 @implementation ESFileTransferRequestPromptController
36  * @brief Display a prompt for a file transfer to save, save as, or cancel
37  *
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
41  */
42 + (NSWindowController *)displayPromptForFileTransfer:(ESFileTransfer *)inFileTransfer
43                                                                          notifyingTarget:(id)inTarget
44                                                                                         selector:(SEL)inSelector
46         ESFileTransferRequestPromptController   *promptController;
47         NSWindowController                                              *windowController = nil;
48         
49         if ((promptController = [[self alloc] initForFileTransfer:inFileTransfer
50                                                                                           notifyingTarget:inTarget
51                                                                                                          selector:inSelector])) {
52                 windowController = [promptController windowController];
53         }
54         
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;
66                 
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];
73                 
74                 if(fileSize){
75                         NSString        *fileSizeString;
76                         
77                         fileSizeString = [[adium fileTransferController] stringForSize:fileSize];
78                         filenameDisplay = [NSString stringWithFormat:@"%@ (%@)",remoteFilename,fileSizeString];
79                 }else{
80                         filenameDisplay = remoteFilename;
81                 }
82                 
83                 message = [NSAttributedString stringWithString:
84                         [NSString stringWithFormat:AILocalizedString(@"%@ requests to send you %@",nil),
85                                 [[fileTransfer contact] formattedUID],
86                                 filenameDisplay]];
87                         
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)
92                                                                                                                                                                           onWindow:nil
93                                                                                                                                                          withMessageHeader:nil
94                                                                                                                                                                         andMessage:message
95                                                                                                                                                                                 target:self
96                                                                                                                                                                           userInfo:nil] retain];
97         }
99         return self;
102 - (void)dealloc
104         [fileTransfer release];
105         [target release];
106         [windowController release];
108         [super dealloc];
111 - (ESTextAndButtonsWindowController *)windowController
113         return windowController;
117 * @brief Window was closed, either by a button being clicked or the user closing it
118  */
119 - (BOOL)textAndButtonsWindowDidEnd:(NSWindow *)window returnCode:(AITextAndButtonsReturnCode)returnCode userInfo:(id)userInfo
121         NSString        *localFilename = nil;
122         BOOL            finished = NO;
124         switch(returnCode){                     
125                 case AITextAndButtonsDefaultReturn: /* Save */
126                 {
127                         localFilename = [[[adium preferenceController] userPreferredDownloadFolder] stringByAppendingPathComponent:[fileTransfer remoteFilename]];
128                         
129                         /* If the file doesn't exist, we're done.  If it does, fall through to AITextAndButtonsOtherReturn
130                          * triggering a Save As... panel.
131                         */
132                         if(![[NSFileManager defaultManager] fileExistsAtPath:localFilename]){
133                                 finished = YES;
134                                 break;
135                         }
136                 }
137                 case AITextAndButtonsOtherReturn: /* Save As... */
138                 {
139                         //Prompt for a location to save
140                         [[[NSSavePanel savePanel] retain] beginSheetForDirectory:[[adium preferenceController] userPreferredDownloadFolder]
141                                                                                                                                 file:[fileTransfer remoteFilename]
142                                                                                                           modalForWindow:[windowController window]
143                                                                                                            modalDelegate:self
144                                                                                                           didEndSelector:@selector(savePanelDidEnd:returnCode:contextInfo:)
145                                                                                                                  contextInfo:nil];
146                         break;
147                 }
148                 case AITextAndButtonsAlternateReturn: /* Cancel */                      
149                 case AITextAndButtonsClosedWithoutResponse: /* Closed = Cancel */
150                 {
151                         /* File name remains nil and the transfer will therefore be canceled */
152                         finished = YES;
153                         break;
154                 }
155         }
157         if(finished){
158                 [target performSelector:selector
159                                          withObject:fileTransfer
160                                          withObject:localFilename];
161                 
162                 //Release our instance  
163                 [self autorelease];
164                 
165                 //Close the window
166                 return YES;
167         }
168         
169         //Don't close the window if we're not finished.
170         return NO;
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:)
184                                                            withObject:nil
185                                                            afterDelay:0];
186                 [self autorelease];
187         }
189         [savePanel release];
192 @end