source: trunk/macosx/AddWindowController.m @ 10161

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

#2846 Priority Selection in "Add" window

  • Property svn:keywords set to Date Rev Author Id
File size: 12.0 KB
Line 
1/******************************************************************************
2 * $Id: AddWindowController.m 10161 2010-02-11 01:34:32Z livings124 $
3 *
4 * Copyright (c) 2008-2010 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 "ExpandedPathToIconTransformer.h"
28#import "FileOutlineController.h"
29#import "GroupsController.h"
30#import "NSStringAdditions.h"
31#import "Torrent.h"
32
33#define UPDATE_SECONDS 1.0
34
35#define POPUP_PRIORITY_HIGH 0
36#define POPUP_PRIORITY_NORMAL 1
37#define POPUP_PRIORITY_LOW 2
38
39@interface AddWindowController (Private)
40
41- (void) updateFiles;
42
43- (void) confirmAdd;
44
45- (void) setDestinationPath: (NSString *) destination;
46
47- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo;
48
49- (void) setGroupsMenu;
50- (void) changeGroupValue: (id) sender;
51
52- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
53
54@end
55
56@implementation AddWindowController
57
58- (id) initWithTorrent: (Torrent *) torrent destination: (NSString *) path lockDestination: (BOOL) lockDestination
59    controller: (Controller *) controller torrentFile: (NSString *) torrentFile
60    deleteTorrent: (BOOL) deleteTorrent canToggleDelete: (BOOL) canToggleDelete
61{
62    if ((self = [super initWithWindowNibName: @"AddWindow"]))
63    {
64        fTorrent = torrent;
65        fDestination = [[path stringByExpandingTildeInPath] retain];
66        fLockDestination = lockDestination;
67       
68        fController = controller;
69       
70        fTorrentFile = [[torrentFile stringByExpandingTildeInPath] retain];
71       
72        fDeleteTorrent = deleteTorrent;
73        fDeleteEnable = canToggleDelete;
74       
75        fGroupValue = [torrent groupValue];
76    }
77    return self;
78}
79
80- (void) awakeFromNib
81{
82    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateStatusField:)
83        name: @"TorrentFileCheckChange" object: fTorrent];
84   
85    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:)
86        name: @"UpdateGroups" object: nil];
87   
88    [fFileController setTorrent: fTorrent];
89   
90    NSString * name = [fTorrent name];
91    [[self window] setTitle: name];
92    [fNameField setStringValue: name];
93   
94    [fIconView setImage: [fTorrent icon]];
95   
96    [self updateStatusField: nil];
97   
98    [self setGroupsMenu];
99    [fGroupPopUp selectItemWithTag: fGroupValue];
100   
101    NSInteger priorityTag;
102    switch ([fTorrent priority])
103    {
104        case TR_PRI_HIGH: priorityTag = POPUP_PRIORITY_HIGH; break;
105        case TR_PRI_NORMAL: priorityTag = POPUP_PRIORITY_NORMAL; break;
106        case TR_PRI_LOW: priorityTag = POPUP_PRIORITY_LOW; break;
107        default: NSAssert1(NO, @"Unknown priority for adding torrent: %d", [fTorrent priority]);
108    }
109    [fPriorityPopUp selectItemWithTag: priorityTag];
110   
111    [fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
112   
113    [fDeleteCheck setState: fDeleteTorrent ? NSOnState : NSOffState];
114    [fDeleteCheck setEnabled: fDeleteEnable];
115   
116    if (fDestination)
117        [self setDestinationPath: fDestination];
118    else
119    {
120        [fLocationField setStringValue: @""];
121        [fLocationImageView setImage: nil];
122    }
123   
124    fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
125                selector: @selector(updateFiles) userInfo: nil repeats: YES];
126    [self updateFiles];
127}
128
129- (void) windowDidLoad
130{
131    //if there is no destination, prompt for one right away
132    if (!fDestination)
133        [self setDestination: nil];
134}
135
136- (void) dealloc
137{
138    [[NSNotificationCenter defaultCenter] removeObserver: self];
139   
140    [fTimer invalidate];
141   
142    [fDestination release];
143   
144    [super dealloc];
145}
146
147- (Torrent *) torrent
148{
149    return fTorrent;
150}
151
152- (void) setDestination: (id) sender
153{
154    NSOpenPanel * panel = [NSOpenPanel openPanel];
155
156    [panel setPrompt: NSLocalizedString(@"Select", "Open torrent -> prompt")];
157    [panel setAllowsMultipleSelection: NO];
158    [panel setCanChooseFiles: NO];
159    [panel setCanChooseDirectories: YES];
160    [panel setCanCreateDirectories: YES];
161   
162    [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
163                        "Add -> select destination folder"), [fTorrent name]]];
164   
165    [panel beginSheetForDirectory: nil file: nil types: nil modalForWindow: [self window] modalDelegate: self
166            didEndSelector: @selector(folderChoiceClosed:returnCode:contextInfo:) contextInfo: nil];
167}
168
169- (void) add: (id) sender
170{
171    if ([[fDestination lastPathComponent] isEqualToString: [fTorrent name]]
172        && [[NSUserDefaults standardUserDefaults] boolForKey: @"WarningFolderDataSameName"])
173    {
174        NSAlert * alert = [[NSAlert alloc] init];
175        [alert setMessageText: NSLocalizedString(@"The destination directory and root data directory have the same name.",
176                                "Add torrent -> same name -> title")];
177        [alert setInformativeText: NSLocalizedString(@"If you are attempting to use already existing data,"
178            " the root data directory should be inside the destination directory.", "Add torrent -> same name -> message")];
179        [alert setAlertStyle: NSWarningAlertStyle];
180        [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Add torrent -> same name -> button")];
181        [alert addButtonWithTitle: NSLocalizedString(@"Add", "Add torrent -> same name -> button")];
182        [alert setShowsSuppressionButton: YES];
183       
184        [alert beginSheetModalForWindow: [self window] modalDelegate: self
185            didEndSelector: @selector(sameNameAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
186    }
187    else
188        [self confirmAdd];
189}
190
191- (void) cancelAdd: (id) sender
192{
193    [[self window] performClose: sender];
194}
195
196//only called on cancel
197- (BOOL) windowShouldClose: (id) window
198{
199    [fTimer invalidate];
200    fTimer = nil;
201   
202    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
203   
204    [fController askOpenConfirmed: self add: NO];
205    return YES;
206}
207
208- (void) verifyLocalData: (id) sender
209{
210    [fTorrent resetCache];
211    [self updateFiles];
212}
213
214- (void) changePriority: (id) sender
215{
216    tr_priority_t priority;
217    switch ([sender tag])
218    {
219        case POPUP_PRIORITY_HIGH: priority = TR_PRI_HIGH; break;
220        case POPUP_PRIORITY_NORMAL: priority = TR_PRI_NORMAL; break;
221        case POPUP_PRIORITY_LOW: priority = TR_PRI_LOW; break;
222        default: NSAssert1(NO, @"Unknown priority tag for adding torrent: %d", [sender tag]);
223    }
224    [fTorrent setPriority: priority];
225}
226
227- (void) updateStatusField: (NSNotification *) notification
228{
229    NSString * statusString = [NSString stringForFileSize: [fTorrent size]];
230    if ([fTorrent isFolder])
231    {
232        NSString * fileString;
233        NSInteger count = [fTorrent fileCount];
234        if (count != 1)
235            fileString = [NSString stringWithFormat: NSLocalizedString(@"%d files", "Add torrent -> info"), count];
236        else
237            fileString = NSLocalizedString(@"1 file", "Add torrent -> info");
238       
239        NSString * selectedString = [NSString stringWithFormat: NSLocalizedString(@"%@ selected", "Add torrent -> info"),
240                                        [NSString stringForFileSize: [fTorrent totalSizeSelected]]];
241       
242        statusString = [NSString stringWithFormat: @"%@, %@ (%@)", fileString, statusString, selectedString];
243    }
244   
245    [fStatusField setStringValue: statusString];
246}
247
248- (void) updateGroupMenu: (NSNotification *) notification
249{
250    [self setGroupsMenu];
251    if (![fGroupPopUp selectItemWithTag: fGroupValue])
252    {
253        fGroupValue = -1;
254        [fGroupPopUp selectItemWithTag: fGroupValue];
255    }
256}
257
258@end
259
260@implementation AddWindowController (Private)
261
262- (void) updateFiles
263{
264    [fTorrent update];
265   
266    [fFileController reloadData];
267   
268    if ([fTorrent isChecking])
269    {
270        [fVerifyIndicator setHidden: NO];
271        [fVerifyIndicator setDoubleValue: [fTorrent checkingProgress]];
272    }
273    else
274        [fVerifyIndicator setHidden: YES];
275}
276
277- (void) confirmAdd
278{
279    [fTimer invalidate];
280    fTimer = nil;
281   
282    [fTorrent setWaitToStart: [fStartCheck state] == NSOnState];
283    [fTorrent setGroupValue: fGroupValue];
284   
285    if (fTorrentFile && [fDeleteCheck state] == NSOnState)
286        [Torrent trashFile: fTorrentFile];
287   
288    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
289   
290    [self close];
291    [fController askOpenConfirmed: self add: YES]; //ensure last, since it releases this controller
292}
293
294- (void) setDestinationPath: (NSString *) destination
295{
296    destination = [destination stringByExpandingTildeInPath];
297    if (!fDestination || ![fDestination isEqualToString: destination])
298    {
299        [fDestination release];
300        fDestination = [destination retain];
301       
302        [fTorrent changeDownloadFolderBeforeUsing: fDestination];
303    }
304   
305    [fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
306    [fLocationField setToolTip: fDestination];
307   
308    ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
309    [fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
310    [iconTransformer release];
311}
312
313- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo
314{
315    if (code == NSOKButton)
316    {
317        fLockDestination = NO;
318        [self setDestinationPath: [[openPanel filenames] objectAtIndex: 0]];
319    }
320    else
321    {
322        if (!fDestination)
323            [self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
324    }
325}
326
327- (void) setGroupsMenu
328{
329    NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
330    [fGroupPopUp setMenu: groupMenu];
331}
332
333- (void) changeGroupValue: (id) sender
334{
335    NSInteger previousGroup = fGroupValue;
336    fGroupValue = [sender tag];
337   
338    if (!fLockDestination)
339    {
340        if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue])
341            [self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue]];
342        else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
343            [self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"]];
344        else;
345    }
346}
347
348- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
349{
350    if ([[alert suppressionButton] state] == NSOnState)
351        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
352   
353    [alert release];
354   
355    if (returnCode == NSAlertSecondButtonReturn)
356        [self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
357}
358
359@end
Note: See TracBrowser for help on using the repository browser.