{{{
[adiumx.git] / Source / ESFileTransferRequestPromptController.m
blob86ea84225243d14d6e4c9908fa47c7e84b944afc
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 "ESFileTransferRequestPromptController.h"
18 #import "ESFileTransferController.h"
19 #import "ESFileTransfer.h"
20 #import "ESTextAndButtonsWindowController.h"
21 #import <Adium/AIPreferenceControllerProtocol.h>
22 #import <Adium/AIListContact.h>
23 #import <Adium/AIContentControllerProtocol.h>
24 #import <AIUtilities/AIApplicationAdditions.h>
25 #import <AIUtilities/AIAttributedStringAdditions.h>
26 #import <AIUtilities/AIStringAdditions.h>
28 @interface ESFileTransferRequestPromptController (PRIVATE)
29 - (id)initForFileTransfer:(ESFileTransfer *)inFileTransfer
30                   notifyingTarget:(id)inTarget
31                                  selector:(SEL)inSelector;
32 - (ESTextAndButtonsWindowController *)windowController;
33 @end
35 @implementation ESFileTransferRequestPromptController
37 /*!
38  * @brief Display a prompt for a file transfer to save, save as, or cancel
39  *
40  * @param inFileTransfer The file transfer
41  * @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.
42  */
43 + (void)displayPromptForFileTransfer:(ESFileTransfer *)inFileTransfer
44                                                                          notifyingTarget:(id)inTarget
45                                                                                         selector:(SEL)inSelector
46 {       
47         [[[self alloc] initForFileTransfer:inFileTransfer
48                                            notifyingTarget:inTarget
49                                                           selector:inSelector] autorelease];
52 - (id)initForFileTransfer:(ESFileTransfer *)inFileTransfer
53                   notifyingTarget:(id)inTarget
54                                  selector:(SEL)inSelector
56         if ((self = [super init])) {
57                 fileTransfer = [inFileTransfer retain];
58                 target       = [inTarget retain];
59                 selector     =  inSelector;
60                 
61                 [fileTransfer setFileTransferRequestPromptController:self];
62                 AILog(@"%@: Requeseting file transfer %@", self, fileTransfer);
63                 [[adium contentController] receiveContentObject:fileTransfer];
64         }
66         return self;
69 - (void)dealloc
71         [fileTransfer release];
72         [target release];
74         [super dealloc];
77 /*!
78  * @brief The user did something with the file transfer request
79  */
80 - (void)handleFileTransferAction:(AIFileTransferAction)action
82         NSString        *localFilename = nil;
83         BOOL            finished = NO;
84         
85         switch (action) {                       
86                 case AISaveFile: /* Save */
87                 {
88                         localFilename = [[[adium preferenceController] userPreferredDownloadFolder] stringByAppendingPathComponent:[fileTransfer remoteFilename]];
89                         
90                         /* If the file doesn't exist, we're done.  If it does, fall through to AISaveFileAs
91                         * triggering a Save As... panel.
92                         */
93                         if (![[NSFileManager defaultManager] fileExistsAtPath:localFilename]) {
94                                 finished = YES;
95                                 break;
96                         }
97                 }
98                 case AISaveFileAs: /* Save As... */
99                 {
100                         //Prompt for a location to save
101                         NSSavePanel *savePanel = [NSSavePanel savePanel];
102                         int returnCode = [savePanel runModalForDirectory:[[adium preferenceController] userPreferredDownloadFolder]
103                                                                                                                                                            file:[fileTransfer remoteFilename]];
104                         //Only need to take action if the user pressed OK; if she pressed cancel, just return to our window.
105                         if (returnCode == NSOKButton) {
106                                 localFilename = [savePanel filename];
107                                 finished = YES;
108                         }
109                         
110                         break;
111                 }
112                 case AICancel: /* Closed = Cancel */
113                 {
114                         /* File name remains nil and the transfer will therefore be cancelled */
115                         finished = YES;
116                         break;
117                 }
118         }
119         
120         if (finished) {
121                 [self retain];
122                 [target performSelector:selector
123                                          withObject:fileTransfer
124                                          withObject:localFilename];
125                 
126                 [fileTransfer setFileTransferRequestPromptController:nil];
127                 [self release];
128         }
131 - (ESFileTransfer *)fileTransfer
133         return fileTransfer;
136 @end