source: trunk/macosx/AddMagnetWindowController.m

Last change on this file was 13602, checked in by livings124, 9 years ago

#2794 Don't apply auto-grouping until torrent is demagnetized

  • Property svn:keywords set to Date Rev Author Id
File size: 12.6 KB
Line 
1/******************************************************************************
2 * $Id: AddMagnetWindowController.m 13602 2012-10-30 00:22:10Z mikedld $
3 *
4 * Copyright (c) 2010-2012 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 "AddMagnetWindowController.h"
26#import "Controller.h"
27#import "ExpandedPathToIconTransformer.h"
28#import "GroupsController.h"
29#import "NSStringAdditions.h"
30#import "Torrent.h"
31
32#define POPUP_PRIORITY_HIGH 0
33#define POPUP_PRIORITY_NORMAL 1
34#define POPUP_PRIORITY_LOW 2
35
36@interface AddMagnetWindowController (Private)
37
38- (void) confirmAdd;
39
40- (void) setDestinationPath: (NSString *) destination determinationType: (TorrentDeterminationType) determinationType;
41
42- (void) setGroupsMenu;
43- (void) changeGroupValue: (id) sender;
44
45- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
46
47@end
48
49@implementation AddMagnetWindowController
50
51- (id) initWithTorrent: (Torrent *) torrent destination: (NSString *) path controller: (Controller *) controller
52{
53    if ((self = [super initWithWindowNibName: @"AddMagnetWindow"]))
54    {
55        fTorrent = torrent;
56        fDestination = [[path stringByExpandingTildeInPath] retain];
57       
58        fController = controller;
59       
60        fGroupValue = [torrent groupValue];
61        fGroupDeterminationType = TorrentDeterminationAutomatic;
62    }
63    return self;
64}
65
66- (void) awakeFromNib
67{
68    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:)
69        name: @"UpdateGroups" object: nil];
70   
71    NSString * name = [fTorrent name];
72    [[self window] setTitle: name];
73    [fNameField setStringValue: name];
74    [fNameField setToolTip: name];
75   
76    [self setGroupsMenu];
77    [fGroupPopUp selectItemWithTag: fGroupValue];
78   
79    NSInteger priorityIndex;
80    switch ([fTorrent priority])
81    {
82        case TR_PRI_HIGH: priorityIndex = POPUP_PRIORITY_HIGH; break;
83        case TR_PRI_NORMAL: priorityIndex = POPUP_PRIORITY_NORMAL; break;
84        case TR_PRI_LOW: priorityIndex = POPUP_PRIORITY_LOW; break;
85        default: NSAssert1(NO, @"Unknown priority for adding torrent: %d", [fTorrent priority]);
86    }
87    [fPriorityPopUp selectItemAtIndex: priorityIndex];
88   
89    [fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
90   
91    if (fDestination)
92        [self setDestinationPath: fDestination determinationType: TorrentDeterminationAutomatic];
93    else
94    {
95        [fLocationField setStringValue: @""];
96        [fLocationImageView setImage: nil];
97    }
98   
99    #warning when 10.7-only, switch to auto layout
100    [fMagnetLinkLabel sizeToFit];
101   
102    const CGFloat downloadToLabelOldWidth = [fDownloadToLabel frame].size.width;
103    [fDownloadToLabel sizeToFit];
104    const CGFloat changeDestOldWidth = [fChangeDestinationButton frame].size.width;
105    [fChangeDestinationButton sizeToFit];
106    NSRect changeDestFrame = [fChangeDestinationButton frame];
107    changeDestFrame.origin.x -= changeDestFrame.size.width - changeDestOldWidth;
108    [fChangeDestinationButton setFrame: changeDestFrame];
109   
110    NSRect downloadToBoxFrame = [fDownloadToBox frame];
111    const CGFloat downloadToBoxSizeDiff = ([fDownloadToLabel frame].size.width - downloadToLabelOldWidth) + (changeDestFrame.size.width - changeDestOldWidth);
112    downloadToBoxFrame.size.width -= downloadToBoxSizeDiff;
113    downloadToBoxFrame.origin.x -= downloadToLabelOldWidth - [fDownloadToLabel frame].size.width;
114    [fDownloadToBox setFrame: downloadToBoxFrame];
115   
116    NSRect groupPopUpFrame = [fGroupPopUp frame];
117    NSRect priorityPopUpFrame = [fPriorityPopUp frame];
118    const CGFloat popUpOffset = groupPopUpFrame.origin.x - NSMaxX([fGroupLabel frame]);
119    [fGroupLabel sizeToFit];
120    [fPriorityLabel sizeToFit];
121    NSRect groupLabelFrame = [fGroupLabel frame];
122    NSRect priorityLabelFrame = [fPriorityLabel frame];
123    //first bring them both to the left edge
124    groupLabelFrame.origin.x = MIN(groupLabelFrame.origin.x, priorityLabelFrame.origin.x);
125    priorityLabelFrame.origin.x = MIN(groupLabelFrame.origin.x, priorityLabelFrame.origin.x);
126    //then align on the right
127    const CGFloat labelWidth = MAX(groupLabelFrame.size.width, priorityLabelFrame.size.width);
128    groupLabelFrame.origin.x += labelWidth - groupLabelFrame.size.width;
129    priorityLabelFrame.origin.x += labelWidth - priorityLabelFrame.size.width;
130    groupPopUpFrame.origin.x = NSMaxX(groupLabelFrame) + popUpOffset;
131    priorityPopUpFrame.origin.x = NSMaxX(priorityLabelFrame) + popUpOffset;
132    [fGroupLabel setFrame: groupLabelFrame];
133    [fGroupPopUp setFrame: groupPopUpFrame];
134    [fPriorityLabel setFrame: priorityLabelFrame];
135    [fPriorityPopUp setFrame: priorityPopUpFrame];
136   
137    const CGFloat minButtonWidth = 82.0;
138    const CGFloat oldAddButtonWidth = [fAddButton bounds].size.width;
139    const CGFloat oldCancelButtonWidth = [fCancelButton bounds].size.width;
140    [fAddButton sizeToFit];
141    [fCancelButton sizeToFit];
142    NSRect addButtonFrame = [fAddButton frame];
143    NSRect cancelButtonFrame = [fCancelButton frame];
144    CGFloat buttonWidth = MAX(addButtonFrame.size.width, cancelButtonFrame.size.width);
145    buttonWidth = MAX(buttonWidth, minButtonWidth);
146    addButtonFrame.size.width = buttonWidth;
147    cancelButtonFrame.size.width = buttonWidth;
148    const CGFloat addButtonWidthIncrease = buttonWidth - oldAddButtonWidth;
149    addButtonFrame.origin.x -= addButtonWidthIncrease;
150    cancelButtonFrame.origin.x -= addButtonWidthIncrease + (buttonWidth - oldCancelButtonWidth);
151    [fAddButton setFrame: addButtonFrame];
152    [fCancelButton setFrame: cancelButtonFrame];
153   
154    [fStartCheck sizeToFit];
155}
156
157- (void) windowDidLoad
158{
159    //if there is no destination, prompt for one right away
160    if (!fDestination)
161        [self setDestination: nil];
162}
163
164- (void) dealloc
165{
166    [[NSNotificationCenter defaultCenter] removeObserver: self];
167   
168    [fDestination release];
169   
170    [super dealloc];
171}
172
173- (Torrent *) torrent
174{
175    return fTorrent;
176}
177
178- (void) setDestination: (id) sender
179{
180    NSOpenPanel * panel = [NSOpenPanel openPanel];
181
182    [panel setPrompt: NSLocalizedString(@"Select", "Open torrent -> prompt")];
183    [panel setAllowsMultipleSelection: NO];
184    [panel setCanChooseFiles: NO];
185    [panel setCanChooseDirectories: YES];
186    [panel setCanCreateDirectories: YES];
187   
188    [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
189                        "Add -> select destination folder"), [fTorrent name]]];
190   
191    [panel beginSheetModalForWindow: [self window] completionHandler: ^(NSInteger result) {
192        if (result == NSFileHandlingPanelOKButton)
193            [self setDestinationPath: [[[panel URLs] objectAtIndex: 0] path] determinationType:TorrentDeterminationUserSpecified];
194        else
195        {
196            if (!fDestination)
197                [self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
198        }
199    }];
200}
201
202- (void) add: (id) sender
203{
204    if ([[fDestination lastPathComponent] isEqualToString: [fTorrent name]]
205        && [[NSUserDefaults standardUserDefaults] boolForKey: @"WarningFolderDataSameName"])
206    {
207        NSAlert * alert = [[NSAlert alloc] init];
208        [alert setMessageText: NSLocalizedString(@"The destination directory and root data directory have the same name.",
209                                "Add torrent -> same name -> title")];
210        [alert setInformativeText: NSLocalizedString(@"If you are attempting to use already existing data,"
211            " the root data directory should be inside the destination directory.", "Add torrent -> same name -> message")];
212        [alert setAlertStyle: NSWarningAlertStyle];
213        [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Add torrent -> same name -> button")];
214        [alert addButtonWithTitle: NSLocalizedString(@"Add", "Add torrent -> same name -> button")];
215        [alert setShowsSuppressionButton: YES];
216       
217        [alert beginSheetModalForWindow: [self window] modalDelegate: self
218            didEndSelector: @selector(sameNameAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
219    }
220    else
221        [self confirmAdd];
222}
223
224- (void) cancelAdd: (id) sender
225{
226    [[self window] performClose: sender];
227}
228
229//only called on cancel
230- (BOOL) windowShouldClose: (id) window
231{
232    [fController askOpenMagnetConfirmed: self add: NO];
233    return YES;
234}
235
236- (void) changePriority: (id) sender
237{
238    tr_priority_t priority;
239    switch ([sender indexOfSelectedItem])
240    {
241        case POPUP_PRIORITY_HIGH: priority = TR_PRI_HIGH; break;
242        case POPUP_PRIORITY_NORMAL: priority = TR_PRI_NORMAL; break;
243        case POPUP_PRIORITY_LOW: priority = TR_PRI_LOW; break;
244        default: NSAssert1(NO, @"Unknown priority tag for adding torrent: %ld", [sender tag]);
245    }
246    [fTorrent setPriority: priority];
247}
248
249- (void) updateGroupMenu: (NSNotification *) notification
250{
251    [self setGroupsMenu];
252    if (![fGroupPopUp selectItemWithTag: fGroupValue])
253    {
254        fGroupValue = -1;
255                fGroupDeterminationType = TorrentDeterminationAutomatic;
256        [fGroupPopUp selectItemWithTag: fGroupValue];
257    }
258}
259
260@end
261
262@implementation AddMagnetWindowController (Private)
263
264- (void) confirmAdd
265{
266    [fTorrent setGroupValue: fGroupValue determinationType: fGroupDeterminationType];
267   
268    if ([fStartCheck state] == NSOnState)
269        [fTorrent startTransfer];
270   
271    [self close];
272    [fController askOpenMagnetConfirmed: self add: YES]; //ensure last, since it releases this controller
273}
274
275- (void) setDestinationPath: (NSString *) destination determinationType: (TorrentDeterminationType) determinationType
276{
277    destination = [destination stringByExpandingTildeInPath];
278    if (!fDestination || ![fDestination isEqualToString: destination])
279    {
280        [fDestination release];
281        fDestination = [destination retain];
282       
283        [fTorrent changeDownloadFolderBeforeUsing: fDestination determinationType: determinationType];
284    }
285   
286    [fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
287    [fLocationField setToolTip: fDestination];
288   
289    ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
290    [fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
291    [iconTransformer release];
292}
293
294- (void) setGroupsMenu
295{
296    NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
297    [fGroupPopUp setMenu: groupMenu];
298}
299
300- (void) changeGroupValue: (id) sender
301{
302    NSInteger previousGroup = fGroupValue;
303    fGroupValue = [sender tag];
304    fGroupDeterminationType = TorrentDeterminationUserSpecified;
305
306    if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue])
307        [self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue] determinationType: TorrentDeterminationAutomatic];
308    else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
309        [self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"] determinationType: TorrentDeterminationAutomatic];
310    else;
311}
312
313- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
314{
315    if ([[alert suppressionButton] state] == NSOnState)
316        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
317   
318    [alert release];
319   
320    if (returnCode == NSAlertSecondButtonReturn)
321        [self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
322}
323
324@end
Note: See TracBrowser for help on using the repository browser.