source: trunk/macosx/InfoFileViewController.m @ 13297

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

Make the "all" and "none" file check buttons localizable

  • Property svn:keywords set to Date Rev Author Id
File size: 7.7 KB
Line 
1/******************************************************************************
2 * $Id: InfoFileViewController.m 13297 2012-05-14 00:24:39Z 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    //localize and place all and none buttons
64    [fCheckAllButton setTitle: NSLocalizedString(@"All", "inspector -> check all")];
65    [fUncheckAllButton setTitle: NSLocalizedString(@"None", "inspector -> check all")];
66   
67    CGRect checkAllFrame = [fCheckAllButton frame];
68    CGRect uncheckAllFrame = [fUncheckAllButton frame];
69    const CGFloat oldAllWidth = checkAllFrame.size.width;
70    const CGFloat oldNoneWidth = uncheckAllFrame.size.width;
71   
72    [fCheckAllButton sizeToFit];
73    [fUncheckAllButton sizeToFit];
74    const CGFloat newWidth = MAX([fCheckAllButton bounds].size.width, [fUncheckAllButton bounds].size.width);
75   
76    const CGFloat uncheckAllChange = newWidth - oldNoneWidth;
77    uncheckAllFrame.size.width = newWidth;
78    uncheckAllFrame.origin.x -= uncheckAllChange;
79    [fUncheckAllButton setFrame: uncheckAllFrame];
80   
81    const CGFloat checkAllChange = newWidth - oldAllWidth;
82    checkAllFrame.size.width = newWidth;
83    checkAllFrame.origin.x -= (checkAllChange + uncheckAllChange);
84    [fCheckAllButton setFrame: checkAllFrame];
85}
86
87- (void) dealloc
88{
89    [fTorrents release];
90   
91    [super dealloc];
92}
93
94- (void) setInfoForTorrents: (NSArray *) torrents
95{
96    //don't check if it's the same in case the metadata changed
97    [fTorrents release];
98    fTorrents = [torrents retain];
99   
100    fSet = NO;
101}
102
103- (void) updateInfo
104{
105    if (!fSet)
106        [self setupInfo];
107   
108    if ([fTorrents count] == 1)
109    {
110        [fFileController refresh];
111       
112        Torrent * torrent = [fTorrents objectAtIndex: 0];
113        if ([torrent isFolder])
114        {
115            const NSInteger filesCheckState = [torrent checkForFiles: [NSIndexSet indexSetWithIndexesInRange: NSMakeRange(0, [torrent fileCount])]];
116            [fCheckAllButton setEnabled: filesCheckState != NSOnState]; //if anything is unchecked
117            [fUncheckAllButton setEnabled: ![torrent allDownloaded]]; //if there are any checked files that aren't finished
118        }
119    }
120}
121
122- (void) saveViewSize
123{
124    [[NSUserDefaults standardUserDefaults] setFloat: NSHeight([[self view] frame]) forKey: @"InspectorContentHeightFiles"];
125}
126
127- (void) setFileFilterText: (id) sender
128{
129    [fFileController setFilterText: [sender stringValue]];
130}
131
132- (IBAction) checkAll: (id) sender
133{
134    [fFileController checkAll];
135}
136
137- (IBAction) uncheckAll: (id) sender
138{
139    [fFileController uncheckAll];
140}
141
142- (NSArray *) quickLookURLs
143{
144    FileOutlineView * fileOutlineView = [fFileController outlineView];
145    Torrent * torrent = [fTorrents objectAtIndex: 0];
146    NSIndexSet * indexes = [fileOutlineView selectedRowIndexes];
147    NSMutableArray * urlArray = [NSMutableArray arrayWithCapacity: [indexes count]];
148   
149    for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
150    {
151        FileListNode * item = [fileOutlineView itemAtRow: i];
152        if ([self canQuickLookFile: item])
153            [urlArray addObject: [NSURL fileURLWithPath: [torrent fileLocation: item]]];
154    }
155   
156    return urlArray;
157}
158
159- (BOOL) canQuickLook
160{
161    if ([fTorrents count] != 1)
162        return NO;
163   
164    Torrent * torrent = [fTorrents objectAtIndex: 0];
165    if (![torrent isFolder])
166        return NO;
167   
168    FileOutlineView * fileOutlineView = [fFileController outlineView];
169    NSIndexSet * indexes = [fileOutlineView selectedRowIndexes];
170   
171    for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
172        if ([self canQuickLookFile: [fileOutlineView itemAtRow: i]])
173            return YES;
174   
175    return NO;
176}
177
178- (NSRect) quickLookSourceFrameForPreviewItem: (id <QLPreviewItem>) item
179{
180    FileOutlineView * fileOutlineView = [fFileController outlineView];
181   
182    NSString * fullPath = [(NSURL *)item path];
183    Torrent * torrent = [fTorrents objectAtIndex: 0];
184    NSRange visibleRows = [fileOutlineView rowsInRect: [fileOutlineView bounds]];
185   
186    for (NSUInteger row = visibleRows.location; row < NSMaxRange(visibleRows); row++)
187    {
188        FileListNode * rowItem = [fileOutlineView itemAtRow: row];
189        if ([[torrent fileLocation: rowItem] isEqualToString: fullPath])
190        {
191            NSRect frame = [fileOutlineView iconRectForRow: row];
192           
193            if (!NSIntersectsRect([fileOutlineView visibleRect], frame))
194                return NSZeroRect;
195           
196            frame.origin = [fileOutlineView convertPoint: frame.origin toView: nil];
197            frame.origin = [[[self view] window] convertBaseToScreen: frame.origin];
198            frame.origin.y -= frame.size.height;
199            return frame;
200        }
201    }
202   
203    return NSZeroRect;
204}
205
206@end
207
208@implementation InfoFileViewController (Private)
209
210- (void) setupInfo
211{
212    [fFileFilterField setStringValue: @""];
213   
214    if ([fTorrents count] == 1)
215    {
216        Torrent * torrent = [fTorrents objectAtIndex: 0];
217       
218        [fFileController setTorrent: torrent];
219       
220        const BOOL isFolder = [torrent isFolder];
221        [fFileFilterField setEnabled: isFolder];
222       
223        if (!isFolder)
224        {
225            [fCheckAllButton setEnabled: NO];
226            [fUncheckAllButton setEnabled: NO];
227        }
228    }
229    else
230    {
231        [fFileController setTorrent: nil];
232       
233        [fFileFilterField setEnabled: NO];
234       
235        [fCheckAllButton setEnabled: NO];
236        [fUncheckAllButton setEnabled: NO];
237    }
238   
239    fSet = YES;
240}
241
242- (BOOL) canQuickLookFile: (FileListNode *) item
243{
244    Torrent * torrent = [fTorrents objectAtIndex: 0];
245    return ([item isFolder] || [torrent fileProgress: item] >= 1.0) && [torrent fileLocation: item];
246}
247
248@end
Note: See TracBrowser for help on using the repository browser.