source: trunk/macosx/InfoFileViewController.m @ 13296

Last change on this file since 13296 was 13296, checked in by livings124, 9 years ago

#4902 Check all/Uncheck all buttons in the inspector

  • Property svn:keywords set to Date Rev Author Id
File size: 6.7 KB
Line 
1/******************************************************************************
2 * $Id: InfoFileViewController.m 13296 2012-05-14 00:00:19Z livings124 $
3 *
4 * Copyright (c) 2010-2012 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 "InfoFileViewController.h"
26#import "FileListNode.h"
27#import "FileOutlineController.h"
28#import "FileOutlineView.h"
29#import "Torrent.h"
30
31@interface InfoFileViewController (Private)
32
33- (void) setupInfo;
34
35- (BOOL) canQuickLookFile: (FileListNode *) item;
36
37@end
38
39@implementation InfoFileViewController
40
41- (id) init
42{
43    if ((self = [super initWithNibName: @"InfoFileView" bundle: nil]))
44    {
45        [self setTitle: NSLocalizedString(@"Files", "Inspector view -> title")];
46    }
47   
48    return self;
49}
50
51- (void) awakeFromNib
52{
53    const CGFloat height = [[NSUserDefaults standardUserDefaults] floatForKey: @"InspectorContentHeightFiles"];
54    if (height != 0.0)
55    {
56        NSRect viewRect = [[self view] frame];
57        viewRect.size.height = height;
58        [[self view] setFrame: viewRect];
59    }
60   
61    [[fFileFilterField cell] setPlaceholderString: NSLocalizedString(@"Filter", "inspector -> file filter")];
62}
63
64- (void) dealloc
65{
66    [fTorrents release];
67   
68    [super dealloc];
69}
70
71- (void) setInfoForTorrents: (NSArray *) torrents
72{
73    //don't check if it's the same in case the metadata changed
74    [fTorrents release];
75    fTorrents = [torrents retain];
76   
77    fSet = NO;
78}
79
80- (void) updateInfo
81{
82    if (!fSet)
83        [self setupInfo];
84   
85    if ([fTorrents count] == 1)
86    {
87        [fFileController refresh];
88       
89        Torrent * torrent = [fTorrents objectAtIndex: 0];
90        if ([torrent isFolder])
91        {
92            const NSInteger filesCheckState = [torrent checkForFiles: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [torrent fileCount])]];
93            [fCheckAllButton setEnabled: filesCheckState != NSOnState]; //if anything is unchecked
94            [fUncheckAllButton setEnabled: ![torrent allDownloaded]]; //if there are any checked files that aren't finished
95        }
96    }
97}
98
99- (void) saveViewSize
100{
101    [[NSUserDefaults standardUserDefaults] setFloat: NSHeight([[self view] frame]) forKey: @"InspectorContentHeightFiles"];
102}
103
104- (void) setFileFilterText: (id) sender
105{
106    [fFileController setFilterText: [sender stringValue]];
107}
108
109- (IBAction) checkAll: (id) sender
110{
111    [fFileController checkAll];
112}
113
114- (IBAction) uncheckAll: (id) sender
115{
116    [fFileController uncheckAll];
117}
118
119- (NSArray *) quickLookURLs
120{
121    FileOutlineView * fileOutlineView = [fFileController outlineView];
122    Torrent * torrent = [fTorrents objectAtIndex: 0];
123    NSIndexSet * indexes = [fileOutlineView selectedRowIndexes];
124    NSMutableArray * urlArray = [NSMutableArray arrayWithCapacity: [indexes count]];
125   
126    for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
127    {
128        FileListNode * item = [fileOutlineView itemAtRow: i];
129        if ([self canQuickLookFile: item])
130            [urlArray addObject: [NSURL fileURLWithPath: [torrent fileLocation: item]]];
131    }
132   
133    return urlArray;
134}
135
136- (BOOL) canQuickLook
137{
138    if ([fTorrents count] != 1)
139        return NO;
140   
141    Torrent * torrent = [fTorrents objectAtIndex: 0];
142    if (![torrent isFolder])
143        return NO;
144   
145    FileOutlineView * fileOutlineView = [fFileController outlineView];
146    NSIndexSet * indexes = [fileOutlineView selectedRowIndexes];
147   
148    for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
149        if ([self canQuickLookFile: [fileOutlineView itemAtRow: i]])
150            return YES;
151   
152    return NO;
153}
154
155- (NSRect) quickLookSourceFrameForPreviewItem: (id <QLPreviewItem>) item
156{
157    FileOutlineView * fileOutlineView = [fFileController outlineView];
158   
159    NSString * fullPath = [(NSURL *)item path];
160    Torrent * torrent = [fTorrents objectAtIndex: 0];
161    NSRange visibleRows = [fileOutlineView rowsInRect: [fileOutlineView bounds]];
162   
163    for (NSUInteger row = visibleRows.location; row < NSMaxRange(visibleRows); row++)
164    {
165        FileListNode * rowItem = [fileOutlineView itemAtRow: row];
166        if ([[torrent fileLocation: rowItem] isEqualToString: fullPath])
167        {
168            NSRect frame = [fileOutlineView iconRectForRow: row];
169           
170            if (!NSIntersectsRect([fileOutlineView visibleRect], frame))
171                return NSZeroRect;
172           
173            frame.origin = [fileOutlineView convertPoint: frame.origin toView: nil];
174            frame.origin = [[[self view] window] convertBaseToScreen: frame.origin];
175            frame.origin.y -= frame.size.height;
176            return frame;
177        }
178    }
179   
180    return NSZeroRect;
181}
182
183@end
184
185@implementation InfoFileViewController (Private)
186
187- (void) setupInfo
188{
189    [fFileFilterField setStringValue: @""];
190   
191    if ([fTorrents count] == 1)
192    {
193        Torrent * torrent = [fTorrents objectAtIndex: 0];
194       
195        [fFileController setTorrent: torrent];
196       
197        const BOOL isFolder = [torrent isFolder];
198        [fFileFilterField setEnabled: isFolder];
199       
200        if (!isFolder)
201        {
202            [fCheckAllButton setEnabled: NO];
203            [fUncheckAllButton setEnabled: NO];
204        }
205    }
206    else
207    {
208        [fFileController setTorrent: nil];
209       
210        [fFileFilterField setEnabled: NO];
211       
212        [fCheckAllButton setEnabled: NO];
213        [fUncheckAllButton setEnabled: NO];
214    }
215   
216    fSet = YES;
217}
218
219- (BOOL) canQuickLookFile: (FileListNode *) item
220{
221    Torrent * torrent = [fTorrents objectAtIndex: 0];
222    return ([item isFolder] || [torrent fileProgress: item] >= 1.0) && [torrent fileLocation: item];
223}
224
225@end
Note: See TracBrowser for help on using the repository browser.