source: trunk/macosx/AddWindowController.m @ 9288

Last change on this file since 9288 was 9288, checked in by livings124, 13 years ago

more #import cleanup

  • Property svn:keywords set to Date Rev Author Id
File size: 10.7 KB
Line 
1/******************************************************************************
2 * $Id: AddWindowController.m 9288 2009-10-11 03:36:50Z livings124 $
3 *
4 * Copyright (c) 2008-2009 Transmission authors and contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the "Software"),
8 * to deal in the Software without restriction, including without limitation
9 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 * and/or sell copies of the Software, and to permit persons to whom the
11 * Software is furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 * DEALINGS IN THE SOFTWARE.
23 *****************************************************************************/
24
25#import "AddWindowController.h"
26#import "Controller.h"
27#import "ExpandedPathToIconTransformer.h"
28#import "FileOutlineController.h"
29#import "GroupsController.h"
30#import "NSStringAdditions.h"
31#import "Torrent.h"
32
33#define UPDATE_SECONDS 1.0
34
35@interface AddWindowController (Private)
36
37- (void) confirmAdd;
38
39- (void) setDestinationPath: (NSString *) destination;
40
41- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo;
42
43- (void) setGroupsMenu;
44- (void) changeGroupValue: (id) sender;
45
46- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
47
48@end
49
50@implementation AddWindowController
51
52- (id) initWithTorrent: (Torrent *) torrent destination: (NSString *) path lockDestination: (BOOL) lockDestination
53    controller: (Controller *) controller torrentFile: (NSString *) torrentFile
54    deleteTorrent: (BOOL) deleteTorrent canToggleDelete: (BOOL) canToggleDelete
55{
56    if ((self = [super initWithWindowNibName: @"AddWindow"]))
57    {
58        fTorrent = torrent;
59        fDestination = [[path stringByExpandingTildeInPath] retain];
60        fLockDestination = lockDestination;
61       
62        fController = controller;
63       
64        fTorrentFile = [[torrentFile stringByExpandingTildeInPath] retain];
65       
66        fDeleteTorrent = deleteTorrent;
67        fDeleteEnable = canToggleDelete;
68       
69        fGroupValue = [torrent groupValue];
70    }
71    return self;
72}
73
74- (void) awakeFromNib
75{
76    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateStatusField:)
77        name: @"TorrentFileCheckChange" object: fTorrent];
78   
79    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:)
80        name: @"UpdateGroups" object: nil];
81   
82    [fFileController setTorrent: fTorrent];
83   
84    NSString * name = [fTorrent name];
85    [[self window] setTitle: name];
86    [fNameField setStringValue: name];
87   
88    [fIconView setImage: [fTorrent icon]];
89   
90    [self updateStatusField: nil];
91   
92    [self setGroupsMenu];
93    [fGroupPopUp selectItemWithTag: fGroupValue];
94   
95    [fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
96   
97    [fDeleteCheck setState: fDeleteTorrent ? NSOnState : NSOffState];
98    [fDeleteCheck setEnabled: fDeleteEnable];
99   
100    if (fDestination)
101        [self setDestinationPath: fDestination];
102    else
103    {
104        [fLocationField setStringValue: @""];
105        [fLocationImageView setImage: nil];
106    }
107   
108    fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: fFileController
109                selector: @selector(reloadData) userInfo: nil repeats: YES];
110}
111
112- (void) windowDidLoad
113{
114    //if there is no destination, prompt for one right away
115    if (!fDestination)
116        [self setDestination: nil];
117}
118
119- (void) dealloc
120{
121    [[NSNotificationCenter defaultCenter] removeObserver: self];
122   
123    [fTimer invalidate];
124   
125    [fDestination release];
126   
127    [super dealloc];
128}
129
130- (Torrent *) torrent
131{
132    return fTorrent;
133}
134
135- (void) setDestination: (id) sender
136{
137    NSOpenPanel * panel = [NSOpenPanel openPanel];
138
139    [panel setPrompt: NSLocalizedString(@"Select", "Open torrent -> prompt")];
140    [panel setAllowsMultipleSelection: NO];
141    [panel setCanChooseFiles: NO];
142    [panel setCanChooseDirectories: YES];
143    [panel setCanCreateDirectories: YES];
144   
145    [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
146                        "Add -> select destination folder"), [fTorrent name]]];
147   
148    [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [self window] modalDelegate: self
149            didEndSelector: @selector(folderChoiceClosed:returnCode:contextInfo:) contextInfo: nil];
150}
151
152- (void) add: (id) sender
153{
154    if ([[fDestination lastPathComponent] isEqualToString: [fTorrent name]]
155        && [[NSUserDefaults standardUserDefaults] boolForKey: @"WarningFolderDataSameName"])
156    {
157        NSAlert * alert = [[NSAlert alloc] init];
158        [alert setMessageText: NSLocalizedString(@"The destination directory and root data directory have the same name.",
159                                "Add torrent -> same name -> title")];
160        [alert setInformativeText: NSLocalizedString(@"If you are attempting to use already existing data,"
161            " the root data directory should be inside the destination directory.", "Add torrent -> same name -> message")];
162        [alert setAlertStyle: NSWarningAlertStyle];
163        [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Add torrent -> same name -> button")];
164        [alert addButtonWithTitle: NSLocalizedString(@"Add", "Add torrent -> same name -> button")];
165        [alert setShowsSuppressionButton: YES];
166       
167        [alert beginSheetModalForWindow: [self window] modalDelegate: self
168            didEndSelector: @selector(sameNameAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
169    }
170    else
171        [self confirmAdd];
172}
173
174- (void) cancelAdd: (id) sender
175{
176    [[self window] performClose: sender];
177}
178
179//only called on cancel
180- (BOOL) windowShouldClose: (id) window
181{
182    [fTimer invalidate];
183    fTimer = nil;
184   
185    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
186   
187    [fController askOpenConfirmed: self add: NO];
188    return YES;
189}
190
191- (void) verifyLocalData: (id) sender
192{
193    [fTorrent resetCache];
194    [fFileController reloadData];
195}
196
197- (void) updateStatusField: (NSNotification *) notification
198{
199    NSString * statusString = [NSString stringForFileSize: [fTorrent size]];
200    if ([fTorrent isFolder])
201    {
202        NSString * fileString;
203        NSInteger count = [fTorrent fileCount];
204        if (count != 1)
205            fileString = [NSString stringWithFormat: NSLocalizedString(@"%d files", "Add torrent -> info"), count];
206        else
207            fileString = NSLocalizedString(@"1 file", "Add torrent -> info");
208       
209        NSString * selectedString = [NSString stringWithFormat: NSLocalizedString(@"%@ selected", "Add torrent -> info"),
210                                        [NSString stringForFileSize: [fTorrent totalSizeSelected]]];
211       
212        statusString = [NSString stringWithFormat: @"%@, %@ (%@)", fileString, statusString, selectedString];
213    }
214   
215    [fStatusField setStringValue: statusString];
216}
217
218- (void) updateGroupMenu: (NSNotification *) notification
219{
220    [self setGroupsMenu];
221    if (![fGroupPopUp selectItemWithTag: fGroupValue])
222    {
223        fGroupValue = -1;
224        [fGroupPopUp selectItemWithTag: fGroupValue];
225    }
226}
227
228@end
229
230@implementation AddWindowController (Private)
231
232- (void) confirmAdd
233{
234    [fTimer invalidate];
235    fTimer = nil;
236   
237    [fTorrent setWaitToStart: [fStartCheck state] == NSOnState];
238    [fTorrent setGroupValue: fGroupValue];
239   
240    if (fTorrentFile && [fDeleteCheck state] == NSOnState)
241        [Torrent trashFile: fTorrentFile];
242   
243    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
244   
245    [self close];
246    [fController askOpenConfirmed: self add: YES]; //ensure last, since it releases this controller
247}
248
249- (void) setDestinationPath: (NSString *) destination
250{
251    destination = [destination stringByExpandingTildeInPath];
252    if (!fDestination || ![fDestination isEqualToString: destination])
253    {
254        [fDestination release];
255        fDestination = [destination retain];
256       
257        [fTorrent changeDownloadFolder: fDestination];
258    }
259   
260    [fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
261    [fLocationField setToolTip: fDestination];
262   
263    ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
264    [fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
265    [iconTransformer release];
266}
267
268- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo
269{
270    if (code == NSOKButton)
271    {
272        fLockDestination = NO;
273        [self setDestinationPath: [[openPanel filenames] objectAtIndex: 0]];
274    }
275    else
276    {
277        if (!fDestination)
278            [self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
279    }
280}
281
282- (void) setGroupsMenu
283{
284    NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
285    [fGroupPopUp setMenu: groupMenu];
286}
287
288- (void) changeGroupValue: (id) sender
289{
290    NSInteger previousGroup = fGroupValue;
291    fGroupValue = [sender tag];
292   
293    if (!fLockDestination)
294    {
295        if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue])
296            [self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue]];
297        else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
298            [self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"]];
299        else;
300    }
301}
302
303- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
304{
305    if ([[alert suppressionButton] state] == NSOnState)
306        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
307   
308    [alert release];
309   
310    if (returnCode == NSAlertSecondButtonReturn)
311        [self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
312}
313
314@end
Note: See TracBrowser for help on using the repository browser.