source: trunk/macosx/AddMagnetWindowController.m @ 12943

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

remove most instances of deprecated filename and filenames methods

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