source: trunk/macosx/AddWindowController.m @ 5150

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

streamline code from last commit

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