source: trunk/macosx/AddWindowController.m @ 7427

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

cleanup for the group menu filling adjustment

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