source: trunk/macosx/AddWindowController.m @ 4716

Last change on this file since 4716 was 4716, checked in by livings124, 14 years ago

add the standard close button to the Add window

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