Put NSAutoreleasePool usage around other distributed notification observer methods
[adiumx.git] / Source / ESFileTransferRequestPromptController.m
blobb39456d724db826f9966abb012b0453ca4566158
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         [self retain];
84         NSString        *localFilename = nil;
85         BOOL            finished = NO;
86         
87         [fileTransfer retain];
88         
89         switch (action) {                       
90                 case AISaveFile: /* Save */
91                 {
92                         localFilename = [[[adium preferenceController] userPreferredDownloadFolder] stringByAppendingPathComponent:[fileTransfer remoteFilename]];
93                         
94                         /* If the file doesn't exist, we're done.  If it does, fall through to AISaveFileAs
95                         * triggering a Save As... panel.
96                         */
97                         if (![[NSFileManager defaultManager] fileExistsAtPath:localFilename]) {
98                                 finished = YES;
99                                 break;
100                         }
101                 }
102                 case AISaveFileAs: /* Save As... */
103                 {
104                         //Prompt for a location to save
105                         NSSavePanel *savePanel = [NSSavePanel savePanel];
106                         int returnCode = [savePanel runModalForDirectory:[[adium preferenceController] userPreferredDownloadFolder]
107                                                                                                                                                            file:[fileTransfer remoteFilename]];
108                         //Only need to take action if the user pressed OK; if she pressed cancel, just return to our window.
109                         if (returnCode == NSOKButton) {
110                                 localFilename = [savePanel filename];
111                                 finished = YES;
112                         }
113                         
114                         break;
115                 }
116                 case AICancel: /* Closed = Cancel */
117                 {
118                         /* File name remains nil and the transfer will therefore be cancelled */
119                         finished = YES;
120                         break;
121                 }
122         }
123         
124         BOOL remotelyCanceled = [fileTransfer isStopped];
125         [fileTransfer release];
126         if(remotelyCanceled) {
127                 [self release];
128                 return;
129         }
130         
131         if (finished) {
132                 [target performSelector:selector
133                                          withObject:fileTransfer
134                                          withObject:localFilename];
135                 
136                 [fileTransfer setFileTransferRequestPromptController:nil];
137                 [self release];
138         }
141 - (ESFileTransfer *)fileTransfer
143         return fileTransfer;
146 @end