source: trunk/macosx/AddWindowController.m @ 7185

Last change on this file since 7185 was 7185, checked in by livings124, 12 years ago

remove references and dead code to the group window being its own window; other rearrangements of code

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