source: trunk/macosx/AddWindowController.m @ 9013

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

remove redundant copy and a deprecated method

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