source: trunk/macosx/AddMagnetWindowController.m @ 13108

Last change on this file since 13108 was 13108, checked in by livings124, 10 years ago

eliminate all warnings by switching NSOpenPanel/NSSavePanels to use completion handler blocks

  • Property svn:keywords set to Date Rev Author Id
File size: 9.0 KB
Line 
1/******************************************************************************
2 * $Id: AddMagnetWindowController.m 13108 2011-12-11 22:31:01Z livings124 $
3 *
4 * Copyright (c) 2010-2011 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;
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    }
62    return self;
63}
64
65- (void) awakeFromNib
66{
67    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:)
68        name: @"UpdateGroups" object: nil];
69   
70    NSString * name = [fTorrent name];
71    [[self window] setTitle: name];
72    [fNameField setStringValue: name];
73    [fNameField setToolTip: name];
74   
75    [self setGroupsMenu];
76    [fGroupPopUp selectItemWithTag: fGroupValue];
77   
78    NSInteger priorityIndex;
79    switch ([fTorrent priority])
80    {
81        case TR_PRI_HIGH: priorityIndex = POPUP_PRIORITY_HIGH; break;
82        case TR_PRI_NORMAL: priorityIndex = POPUP_PRIORITY_NORMAL; break;
83        case TR_PRI_LOW: priorityIndex = POPUP_PRIORITY_LOW; break;
84        default: NSAssert1(NO, @"Unknown priority for adding torrent: %d", [fTorrent priority]);
85    }
86    [fPriorityPopUp selectItemAtIndex: priorityIndex];
87   
88    [fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
89   
90    if (fDestination)
91        [self setDestinationPath: fDestination];
92    else
93    {
94        [fLocationField setStringValue: @""];
95        [fLocationImageView setImage: nil];
96    }
97}
98
99- (void) windowDidLoad
100{
101    //if there is no destination, prompt for one right away
102    if (!fDestination)
103        [self setDestination: nil];
104}
105
106- (void) dealloc
107{
108    [[NSNotificationCenter defaultCenter] removeObserver: self];
109   
110    [fDestination release];
111   
112    [super dealloc];
113}
114
115- (Torrent *) torrent
116{
117    return fTorrent;
118}
119
120- (void) setDestination: (id) sender
121{
122    NSOpenPanel * panel = [NSOpenPanel openPanel];
123
124    [panel setPrompt: NSLocalizedString(@"Select", "Open torrent -> prompt")];
125    [panel setAllowsMultipleSelection: NO];
126    [panel setCanChooseFiles: NO];
127    [panel setCanChooseDirectories: YES];
128    [panel setCanCreateDirectories: YES];
129   
130    [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
131                        "Add -> select destination folder"), [fTorrent name]]];
132   
133    [panel beginSheetModalForWindow: [self window] completionHandler: ^(NSInteger result) {
134        if (result == NSFileHandlingPanelOKButton)
135            [self setDestinationPath: [[[panel URLs] objectAtIndex: 0] path]];
136        else
137        {
138            if (!fDestination)
139                [self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
140        }
141    }];
142}
143
144- (void) add: (id) sender
145{
146    if ([[fDestination lastPathComponent] isEqualToString: [fTorrent name]]
147        && [[NSUserDefaults standardUserDefaults] boolForKey: @"WarningFolderDataSameName"])
148    {
149        NSAlert * alert = [[NSAlert alloc] init];
150        [alert setMessageText: NSLocalizedString(@"The destination directory and root data directory have the same name.",
151                                "Add torrent -> same name -> title")];
152        [alert setInformativeText: NSLocalizedString(@"If you are attempting to use already existing data,"
153            " the root data directory should be inside the destination directory.", "Add torrent -> same name -> message")];
154        [alert setAlertStyle: NSWarningAlertStyle];
155        [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Add torrent -> same name -> button")];
156        [alert addButtonWithTitle: NSLocalizedString(@"Add", "Add torrent -> same name -> button")];
157        [alert setShowsSuppressionButton: YES];
158       
159        [alert beginSheetModalForWindow: [self window] modalDelegate: self
160            didEndSelector: @selector(sameNameAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
161    }
162    else
163        [self confirmAdd];
164}
165
166- (void) cancelAdd: (id) sender
167{
168    [[self window] performClose: sender];
169}
170
171//only called on cancel
172- (BOOL) windowShouldClose: (id) window
173{
174    [fController askOpenMagnetConfirmed: self add: NO];
175    return YES;
176}
177
178- (void) changePriority: (id) sender
179{
180    tr_priority_t priority;
181    switch ([sender indexOfSelectedItem])
182    {
183        case POPUP_PRIORITY_HIGH: priority = TR_PRI_HIGH; break;
184        case POPUP_PRIORITY_NORMAL: priority = TR_PRI_NORMAL; break;
185        case POPUP_PRIORITY_LOW: priority = TR_PRI_LOW; break;
186        default: NSAssert1(NO, @"Unknown priority tag for adding torrent: %d", [sender tag]);
187    }
188    [fTorrent setPriority: priority];
189}
190
191- (void) updateGroupMenu: (NSNotification *) notification
192{
193    [self setGroupsMenu];
194    if (![fGroupPopUp selectItemWithTag: fGroupValue])
195    {
196        fGroupValue = -1;
197        [fGroupPopUp selectItemWithTag: fGroupValue];
198    }
199}
200
201@end
202
203@implementation AddMagnetWindowController (Private)
204
205- (void) confirmAdd
206{
207    [fTorrent setGroupValue: fGroupValue];
208   
209    if ([fStartCheck state] == NSOnState)
210        [fTorrent startTransfer];
211   
212    [self close];
213    [fController askOpenMagnetConfirmed: self add: YES]; //ensure last, since it releases this controller
214}
215
216- (void) setDestinationPath: (NSString *) destination
217{
218    destination = [destination stringByExpandingTildeInPath];
219    if (!fDestination || ![fDestination isEqualToString: destination])
220    {
221        [fDestination release];
222        fDestination = [destination retain];
223       
224        [fTorrent changeDownloadFolderBeforeUsing: fDestination];
225    }
226   
227    [fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
228    [fLocationField setToolTip: fDestination];
229   
230    ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
231    [fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
232    [iconTransformer release];
233}
234
235- (void) setGroupsMenu
236{
237    NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
238    [fGroupPopUp setMenu: groupMenu];
239}
240
241- (void) changeGroupValue: (id) sender
242{
243    NSInteger previousGroup = fGroupValue;
244    fGroupValue = [sender tag];
245   
246    if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue])
247        [self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue]];
248    else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
249        [self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"]];
250    else;
251}
252
253- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
254{
255    if ([[alert suppressionButton] state] == NSOnState)
256        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
257   
258    [alert release];
259   
260    if (returnCode == NSAlertSecondButtonReturn)
261        [self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
262}
263
264@end
Note: See TracBrowser for help on using the repository browser.