source: trunk/macosx/AddWindowController.m @ 12612

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

first go at bringing the libT queue to the Mac client

  • Property svn:keywords set to Date Rev Author Id
File size: 12.4 KB
Line 
1/******************************************************************************
2 * $Id: AddWindowController.m 12612 2011-08-02 12:43:26Z 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 setDoubleValue: [fTorrent checkingProgress]];
279       
280        [fVerifyIndicator startAnimation: self];
281    }
282    else
283        [fVerifyIndicator stopAnimation: self];
284}
285
286- (void) confirmAdd
287{
288    [fTimer invalidate];
289    fTimer = nil;
290    [fTorrent setGroupValue: fGroupValue];
291   
292    if (fTorrentFile && [fDeleteCheck state] == NSOnState)
293        [Torrent trashFile: fTorrentFile];
294   
295    if ([fStartCheck state] == NSOnState)
296        [fTorrent startTransfer];
297   
298    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
299   
300    [self close];
301    [fController askOpenConfirmed: self add: YES]; //ensure last, since it releases this controller
302}
303
304- (void) setDestinationPath: (NSString *) destination
305{
306    destination = [destination stringByExpandingTildeInPath];
307    if (!fDestination || ![fDestination isEqualToString: destination])
308    {
309        [fDestination release];
310        fDestination = [destination retain];
311       
312        [fTorrent changeDownloadFolderBeforeUsing: fDestination];
313    }
314   
315    [fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
316    [fLocationField setToolTip: fDestination];
317   
318    ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
319    [fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
320    [iconTransformer release];
321}
322
323- (void) folderChoiceClosed: (NSOpenPanel *) openPanel returnCode: (NSInteger) code contextInfo: (void *) contextInfo
324{
325    if (code == NSOKButton)
326    {
327        fLockDestination = NO;
328        [self setDestinationPath: [[openPanel filenames] objectAtIndex: 0]];
329    }
330    else
331    {
332        if (!fDestination)
333            [self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
334    }
335}
336
337- (void) setGroupsMenu
338{
339    NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
340    [fGroupPopUp setMenu: groupMenu];
341}
342
343- (void) changeGroupValue: (id) sender
344{
345    NSInteger previousGroup = fGroupValue;
346    fGroupValue = [sender tag];
347   
348    if (!fLockDestination)
349    {
350        if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue])
351            [self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue]];
352        else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
353            [self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"]];
354        else;
355    }
356}
357
358- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
359{
360    if ([[alert suppressionButton] state] == NSOnState)
361        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
362   
363    [alert release];
364   
365    if (returnCode == NSAlertSecondButtonReturn)
366        [self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
367}
368
369@end
Note: See TracBrowser for help on using the repository browser.