source: trunk/macosx/AddWindowController.m @ 5030

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

hopefully fix a crash with the open window

  • Property svn:keywords set to Date Rev Author Id
File size: 10.4 KB
Line 
1/******************************************************************************
2 * $Id: AddWindowController.m 5030 2008-02-13 19:33:04Z 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    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
204   
205    [fController askOpenConfirmed: self add: NO];
206    return YES;
207}
208
209- (void) verifyLocalData: (id) sender
210{
211    [fTorrent resetCache];
212    [fFileController reloadData];
213}
214
215- (void) updateGroupMenu: (NSNotification *) notification
216{
217    [self setGroupsMenu];
218    if (![fGroupPopUp selectItemWithTag: fGroupValue])
219    {
220        fGroupValue = -1;
221        [fGroupPopUp selectItemWithTag: fGroupValue];
222    }
223}
224
225- (void) showGroupsWindow: (id) sender
226{
227    [fGroupPopUp selectItemWithTag: fGroupValue];
228    [fController showGroups: sender];
229}
230
231@end
232
233@implementation AddWindowController (Private)
234
235- (void) confirmAdd
236{
237    [fTimer invalidate];
238    fTimer = nil;
239   
240    [fTorrent setWaitToStart: [fStartCheck state] == NSOnState];
241    [fTorrent setGroupValue: [[fGroupPopUp selectedItem] tag]];
242   
243    if ([fDeleteCheck state] == NSOnState)
244        [fTorrent trashTorrent];
245   
246    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
247   
248    [self close];
249    [fController askOpenConfirmed: self add: YES]; //ensure last, since it releases this controller
250}
251
252- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (int) code contextInfo: (void *) contextInfo
253{
254    if (code == NSOKButton)
255    {
256        [fDestination release];
257        fDestination = [[[openPanel filenames] objectAtIndex: 0] retain];
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        [fTorrent changeDownloadFolder: fDestination];
267    }
268    else
269    {
270        if (!fDestination)
271            [self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
272    }
273}
274
275- (void) setGroupsMenu
276{
277    NSMenu * menu = [fGroupPopUp menu];
278   
279    int i;
280    for (i = [menu numberOfItems]-1 - 2; i >= 0; i--)
281        [menu removeItemAtIndex: i];
282       
283    NSMenu * groupMenu = [[GroupsWindowController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
284    [menu appendItemsFromMenu: groupMenu atIndexes: [NSIndexSet indexSetWithIndexesInRange:
285            NSMakeRange(0, [groupMenu numberOfItems])] atBottom: NO];
286}
287
288- (void) changeGroupValue: (id) sender
289{
290    fGroupValue = [sender tag];
291}
292
293- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (int) returnCode contextInfo: (void *) contextInfo
294{
295    if (([NSApp isOnLeopardOrBetter] ? [[alert suppressionButton] state] == NSOnState : returnCode == NSAlertThirdButtonReturn))
296        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
297   
298    [alert release];
299   
300    if (returnCode == NSAlertSecondButtonReturn)
301        [self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
302}
303
304@end
Note: See TracBrowser for help on using the repository browser.