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