source: trunk/macosx/AddWindowController.m @ 13309

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

#4912 Filter and check all/uncheck all in Add Transfer window

  • Property svn:keywords set to Date Rev Author Id
File size: 13.1 KB
RevLine 
[4462]1/******************************************************************************
2 * $Id: AddWindowController.m 13309 2012-05-20 00:19:55Z livings124 $
3 *
[13162]4 * Copyright (c) 2008-2012 Transmission authors and contributors
[4462]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"
[9288]26#import "Controller.h"
27#import "ExpandedPathToIconTransformer.h"
[7013]28#import "FileOutlineController.h"
[5335]29#import "GroupsController.h"
[4462]30#import "NSStringAdditions.h"
[9288]31#import "Torrent.h"
[4462]32
[7013]33#define UPDATE_SECONDS 1.0
[4583]34
[10161]35#define POPUP_PRIORITY_HIGH 0
36#define POPUP_PRIORITY_NORMAL 1
37#define POPUP_PRIORITY_LOW 2
38
[4462]39@interface AddWindowController (Private)
40
[10056]41- (void) updateFiles;
42
[4625]43- (void) confirmAdd;
44
[7315]45- (void) setDestinationPath: (NSString *) destination;
46
[4510]47- (void) setGroupsMenu;
48- (void) changeGroupValue: (id) sender;
[4462]49
[6974]50- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo;
[4625]51
[4462]52@end
53
54@implementation AddWindowController
55
[7363]56- (id) initWithTorrent: (Torrent *) torrent destination: (NSString *) path lockDestination: (BOOL) lockDestination
[8843]57    controller: (Controller *) controller torrentFile: (NSString *) torrentFile
58    deleteTorrent: (BOOL) deleteTorrent canToggleDelete: (BOOL) canToggleDelete
[4462]59{
60    if ((self = [super initWithWindowNibName: @"AddWindow"]))
61    {
62        fTorrent = torrent;
[13251]63        fDestination = [[path stringByExpandingTildeInPath] retain];
[7363]64        fLockDestination = lockDestination;
[4462]65       
66        fController = controller;
[4468]67       
[13251]68        fTorrentFile = [[torrentFile stringByExpandingTildeInPath] retain];
[4510]69       
[10465]70        fDeleteTorrentInitial = deleteTorrent;
71        fDeleteEnableInitial = canToggleDelete;
[8843]72       
[7315]73        fGroupValue = [torrent groupValue];
[10344]74       
75        [fVerifyIndicator setUsesThreadedAnimation: YES];
[4462]76    }
77    return self;
78}
79
80- (void) awakeFromNib
81{
[13309]82    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateCheckButtons:) name: @"TorrentFileCheckChange" object: fTorrent];
[5149]83   
[13309]84    [[NSNotificationCenter defaultCenter] addObserver: self selector: @selector(updateGroupMenu:) name: @"UpdateGroups" object: nil];
[4470]85   
[4474]86    [fFileController setTorrent: fTorrent];
87   
[4462]88    NSString * name = [fTorrent name];
89    [[self window] setTitle: name];
90    [fNameField setStringValue: name];
[10488]91    [fNameField setToolTip: name];
[4462]92   
[9013]93    [fIconView setImage: [fTorrent icon]];
[4462]94   
[13309]95    [fFileFilterField setEnabled: [fTorrent isFolder]];
[4462]96   
[13309]97    [self updateCheckButtons: nil];
98   
[4510]99    [self setGroupsMenu];
[7315]100    [fGroupPopUp selectItemWithTag: fGroupValue];
[4469]101   
[10283]102    NSInteger priorityIndex;
[10161]103    switch ([fTorrent priority])
104    {
[10283]105        case TR_PRI_HIGH: priorityIndex = POPUP_PRIORITY_HIGH; break;
106        case TR_PRI_NORMAL: priorityIndex = POPUP_PRIORITY_NORMAL; break;
107        case TR_PRI_LOW: priorityIndex = POPUP_PRIORITY_LOW; break;
[10161]108        default: NSAssert1(NO, @"Unknown priority for adding torrent: %d", [fTorrent priority]);
109    }
[10283]110    [fPriorityPopUp selectItemAtIndex: priorityIndex];
[10161]111   
[4462]112    [fStartCheck setState: [[NSUserDefaults standardUserDefaults] boolForKey: @"AutoStartDownload"] ? NSOnState : NSOffState];
113   
[10465]114    [fDeleteCheck setState: fDeleteTorrentInitial ? NSOnState : NSOffState];
115    [fDeleteCheck setEnabled: fDeleteEnableInitial];
[4478]116   
[4462]117    if (fDestination)
[7315]118        [self setDestinationPath: fDestination];
[4462]119    else
120    {
[4489]121        [fLocationField setStringValue: @""];
122        [fLocationImageView setImage: nil];
[4462]123    }
[4583]124   
[10056]125    fTimer = [NSTimer scheduledTimerWithTimeInterval: UPDATE_SECONDS target: self
126                selector: @selector(updateFiles) userInfo: nil repeats: YES];
127    [self updateFiles];
[4462]128}
129
[4489]130- (void) windowDidLoad
131{
132    //if there is no destination, prompt for one right away
133    if (!fDestination)
134        [self setDestination: nil];
135}
136
[4462]137- (void) dealloc
[4584]138{
[4470]139    [[NSNotificationCenter defaultCenter] removeObserver: self];
140   
[4583]141    [fTimer invalidate];
[13251]142   
143    [fDestination release];
144    [fTorrentFile release];
145   
146    [super dealloc];
[4462]147}
148
[4509]149- (Torrent *) torrent
150{
151    return fTorrent;
152}
153
[4462]154- (void) setDestination: (id) sender
155{
156    NSOpenPanel * panel = [NSOpenPanel openPanel];
157
158    [panel setPrompt: NSLocalizedString(@"Select", "Open torrent -> prompt")];
159    [panel setAllowsMultipleSelection: NO];
160    [panel setCanChooseFiles: NO];
161    [panel setCanChooseDirectories: YES];
162    [panel setCanCreateDirectories: YES];
163   
164    [panel setMessage: [NSString stringWithFormat: NSLocalizedString(@"Select the download folder for \"%@\"",
165                        "Add -> select destination folder"), [fTorrent name]]];
166   
[13108]167    [panel beginSheetModalForWindow: [self window] completionHandler: ^(NSInteger result) {
168        if (result == NSFileHandlingPanelOKButton)
169        {
170            fLockDestination = NO;
171            [self setDestinationPath: [[[panel URLs] objectAtIndex: 0] path]];
172        }
173        else
174        {
175            if (!fDestination)
176                [self performSelectorOnMainThread: @selector(cancelAdd:) withObject: nil waitUntilDone: NO];
177        }
178    }];
[4462]179}
180
181- (void) add: (id) sender
182{
[4625]183    if ([[fDestination lastPathComponent] isEqualToString: [fTorrent name]]
184        && [[NSUserDefaults standardUserDefaults] boolForKey: @"WarningFolderDataSameName"])
185    {
186        NSAlert * alert = [[NSAlert alloc] init];
187        [alert setMessageText: NSLocalizedString(@"The destination directory and root data directory have the same name.",
188                                "Add torrent -> same name -> title")];
189        [alert setInformativeText: NSLocalizedString(@"If you are attempting to use already existing data,"
190            " the root data directory should be inside the destination directory.", "Add torrent -> same name -> message")];
191        [alert setAlertStyle: NSWarningAlertStyle];
192        [alert addButtonWithTitle: NSLocalizedString(@"Cancel", "Add torrent -> same name -> button")];
193        [alert addButtonWithTitle: NSLocalizedString(@"Add", "Add torrent -> same name -> button")];
[7508]194        [alert setShowsSuppressionButton: YES];
[4625]195       
196        [alert beginSheetModalForWindow: [self window] modalDelegate: self
197            didEndSelector: @selector(sameNameAlertDidEnd:returnCode:contextInfo:) contextInfo: nil];
198    }
199    else
200        [self confirmAdd];
[4462]201}
202
203- (void) cancelAdd: (id) sender
204{
[4716]205    [[self window] performClose: sender];
206}
207
208//only called on cancel
209- (BOOL) windowShouldClose: (id) window
210{
[4583]211    [fTimer invalidate];
212    fTimer = nil;
213   
[5030]214    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
215   
[4509]216    [fController askOpenConfirmed: self add: NO];
[5030]217    return YES;
[4462]218}
219
[13309]220- (void) setFileFilterText: (id) sender
221{
222    [fFileController setFilterText: [sender stringValue]];
223}
224
225- (IBAction) checkAll: (id) sender
226{
227    [fFileController checkAll];
228}
229
230- (IBAction) uncheckAll: (id) sender
231{
232    [fFileController uncheckAll];
233}
234
[4583]235- (void) verifyLocalData: (id) sender
236{
237    [fTorrent resetCache];
[10056]238    [self updateFiles];
[4583]239}
240
[10161]241- (void) changePriority: (id) sender
242{
243    tr_priority_t priority;
[10283]244    switch ([sender indexOfSelectedItem])
[10161]245    {
246        case POPUP_PRIORITY_HIGH: priority = TR_PRI_HIGH; break;
247        case POPUP_PRIORITY_NORMAL: priority = TR_PRI_NORMAL; break;
248        case POPUP_PRIORITY_LOW: priority = TR_PRI_LOW; break;
249        default: NSAssert1(NO, @"Unknown priority tag for adding torrent: %d", [sender tag]);
250    }
251    [fTorrent setPriority: priority];
252}
253
[13309]254- (void) updateCheckButtons: (NSNotification *) notification
[5149]255{
256    NSString * statusString = [NSString stringForFileSize: [fTorrent size]];
[5973]257    if ([fTorrent isFolder])
[5149]258    {
[13309]259        //check buttons
260        //keep synced with identical code in InfoFileViewController.m
261        const NSInteger filesCheckState = [fTorrent checkForFiles: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [fTorrent fileCount])]];
262        [fCheckAllButton setEnabled: filesCheckState != NSOnState]; //if anything is unchecked
263        [fUncheckAllButton setEnabled: ![fTorrent allDownloaded]]; //if there are any checked files that aren't finished
264       
265        //status field
[5149]266        NSString * fileString;
[6974]267        NSInteger count = [fTorrent fileCount];
[5149]268        if (count != 1)
[11418]269            fileString = [NSString stringWithFormat: NSLocalizedString(@"%@ files", "Add torrent -> info"),
270                            [NSString formattedUInteger: count]];
[5149]271        else
[5559]272            fileString = NSLocalizedString(@"1 file", "Add torrent -> info");
[5150]273       
[5526]274        NSString * selectedString = [NSString stringWithFormat: NSLocalizedString(@"%@ selected", "Add torrent -> info"),
275                                        [NSString stringForFileSize: [fTorrent totalSizeSelected]]];
276       
277        statusString = [NSString stringWithFormat: @"%@, %@ (%@)", fileString, statusString, selectedString];
[5149]278    }
279   
280    [fStatusField setStringValue: statusString];
281}
282
[4471]283- (void) updateGroupMenu: (NSNotification *) notification
[4470]284{
[4510]285    [self setGroupsMenu];
286    if (![fGroupPopUp selectItemWithTag: fGroupValue])
287    {
288        fGroupValue = -1;
289        [fGroupPopUp selectItemWithTag: fGroupValue];
290    }
[4470]291}
292
[4462]293@end
294
295@implementation AddWindowController (Private)
296
[10056]297- (void) updateFiles
298{
299    [fTorrent update];
300   
[13129]301    [fFileController refresh];
[10056]302   
[13309]303    [self updateCheckButtons: nil]; //call in case button state changed by checking
304   
[10056]305    if ([fTorrent isChecking])
[12689]306    {
[10344]307        const BOOL waiting = [fTorrent isCheckingWaiting];
308        [fVerifyIndicator setIndeterminate: waiting];
[12689]309        if (waiting)
310            [fVerifyIndicator startAnimation: self];
311        else
[10344]312            [fVerifyIndicator setDoubleValue: [fTorrent checkingProgress]];
[10056]313    }
[12688]314    else {
315        [fVerifyIndicator setIndeterminate: YES]; //we want to hide when stopped, which only applies when indeterminate
[10344]316        [fVerifyIndicator stopAnimation: self];
[12688]317    }
[10056]318}
319
[4625]320- (void) confirmAdd
321{
322    [fTimer invalidate];
323    fTimer = nil;
[7315]324    [fTorrent setGroupValue: fGroupValue];
[4625]325   
[8843]326    if (fTorrentFile && [fDeleteCheck state] == NSOnState)
327        [Torrent trashFile: fTorrentFile];
[4625]328   
[12612]329    if ([fStartCheck state] == NSOnState)
330        [fTorrent startTransfer];
331   
[5030]332    [fFileController setTorrent: nil]; //avoid a crash when window tries to update
333   
334    [self close];
335    [fController askOpenConfirmed: self add: YES]; //ensure last, since it releases this controller
[4625]336}
337
[7212]338- (void) setDestinationPath: (NSString *) destination
[7210]339{
[7315]340    destination = [destination stringByExpandingTildeInPath];
341    if (!fDestination || ![fDestination isEqualToString: destination])
342    {
[13251]343        [fDestination release];
344        fDestination = [destination retain];
[7315]345       
[9342]346        [fTorrent changeDownloadFolderBeforeUsing: fDestination];
[7315]347    }
[7210]348   
349    [fLocationField setStringValue: [fDestination stringByAbbreviatingWithTildeInPath]];
350    [fLocationField setToolTip: fDestination];
351   
352    ExpandedPathToIconTransformer * iconTransformer = [[ExpandedPathToIconTransformer alloc] init];
353    [fLocationImageView setImage: [iconTransformer transformedValue: fDestination]];
[13251]354    [iconTransformer release];
[7210]355}
356
[4510]357- (void) setGroupsMenu
358{
[5335]359    NSMenu * groupMenu = [[GroupsController groups] groupMenuWithTarget: self action: @selector(changeGroupValue:) isSmall: NO];
[7185]360    [fGroupPopUp setMenu: groupMenu];
[4510]361}
362
363- (void) changeGroupValue: (id) sender
364{
[7212]365    NSInteger previousGroup = fGroupValue;
[4510]366    fGroupValue = [sender tag];
[7363]367   
368    if (!fLockDestination)
369    {
370        if ([[GroupsController groups] usesCustomDownloadLocationForIndex: fGroupValue])
371            [self setDestinationPath: [[GroupsController groups] customDownloadLocationForIndex: fGroupValue]];
372        else if ([fDestination isEqualToString: [[GroupsController groups] customDownloadLocationForIndex: previousGroup]])
373            [self setDestinationPath: [[NSUserDefaults standardUserDefaults] stringForKey: @"DownloadFolder"]];
374        else;
375    }
[4510]376}
377
[6974]378- (void) sameNameAlertDidEnd: (NSAlert *) alert returnCode: (NSInteger) returnCode contextInfo: (void *) contextInfo
[4625]379{
[7508]380    if ([[alert suppressionButton] state] == NSOnState)
[4625]381        [[NSUserDefaults standardUserDefaults] setBool: NO forKey: @"WarningFolderDataSameName"];
382   
[13251]383    [alert release];
[4625]384   
385    if (returnCode == NSAlertSecondButtonReturn)
386        [self performSelectorOnMainThread: @selector(confirmAdd) withObject: nil waitUntilDone: NO];
387}
388
[4462]389@end
Note: See TracBrowser for help on using the repository browser.