transmission: update from 2.13 to 2.22
[tomato.git] / release / src / router / transmission / macosx / InfoFileViewController.m
blobe749c0bf8c3fff569b4bd9b238de84cc975f7935
1 /******************************************************************************
2  * $Id: InfoFileViewController.m 11617 2011-01-01 20:42:14Z livings124 $
3  *
4  * Copyright (c) 2010-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  *****************************************************************************/
25 #import "InfoFileViewController.h"
26 #import "FileListNode.h"
27 #import "FileOutlineController.h"
28 #import "FileOutlineView.h"
29 #import "Torrent.h"
31 @interface InfoFileViewController (Private)
33 - (void) setupInfo;
35 - (BOOL) canQuickLookFile: (FileListNode *) item;
37 @end
39 @implementation InfoFileViewController
41 - (id) init
43     if ((self = [super initWithNibName: @"InfoFileView" bundle: nil]))
44     {
45         [self setTitle: NSLocalizedString(@"Files", "Inspector view -> title")];
46     }
47     
48     return self;
51 - (void) awakeFromNib
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")];
64 - (void) dealloc
66     [fTorrents release];
67     
68     [super dealloc];
71 - (void) setInfoForTorrents: (NSArray *) torrents
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;
80 - (void) updateInfo
82     if (!fSet)
83         [self setupInfo];
84     
85     if ([fTorrents count] == 1)
86         [fFileController reloadData];
89 - (void) saveViewSize
91     [[NSUserDefaults standardUserDefaults] setFloat: NSHeight([[self view] frame]) forKey: @"InspectorContentHeightFiles"];
94 - (void) setFileFilterText: (id) sender
96     [fFileController setFilterText: [sender stringValue]];
99 - (NSArray *) quickLookURLs
101     FileOutlineView * fileOutlineView = [fFileController outlineView];
102     Torrent * torrent = [fTorrents objectAtIndex: 0];
103     NSIndexSet * indexes = [fileOutlineView selectedRowIndexes];
104     NSMutableArray * urlArray = [NSMutableArray arrayWithCapacity: [indexes count]];
105     
106     for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
107     {
108         FileListNode * item = [fileOutlineView itemAtRow: i];
109         if ([self canQuickLookFile: item])
110             [urlArray addObject: [NSURL fileURLWithPath: [torrent fileLocation: item]]];
111     }
112     
113     return urlArray;
116 - (BOOL) canQuickLook
118     if ([fTorrents count] != 1)
119         return NO;
120     
121     Torrent * torrent = [fTorrents objectAtIndex: 0];
122     if (![torrent isFolder])
123         return NO;
124     
125     FileOutlineView * fileOutlineView = [fFileController outlineView];
126     NSIndexSet * indexes = [fileOutlineView selectedRowIndexes];
127     
128     for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
129         if ([self canQuickLookFile: [fileOutlineView itemAtRow: i]])
130             return YES;
131     
132     return NO;
135 #warning uncomment (in header too)
136 - (NSRect) quickLookSourceFrameForPreviewItem: (id /*<QLPreviewItem>*/) item
138     FileOutlineView * fileOutlineView = [fFileController outlineView];
139     
140     NSString * fullPath = [(NSURL *)item path];
141     Torrent * torrent = [fTorrents objectAtIndex: 0];
142     NSRange visibleRows = [fileOutlineView rowsInRect: [fileOutlineView bounds]];
143     
144     for (NSUInteger row = visibleRows.location; row < NSMaxRange(visibleRows); row++)
145     {
146         FileListNode * rowItem = [fileOutlineView itemAtRow: row];
147         if ([[torrent fileLocation: rowItem] isEqualToString: fullPath])
148         {
149             NSRect frame = [fileOutlineView iconRectForRow: row];
150             
151             if (!NSIntersectsRect([fileOutlineView visibleRect], frame))
152                 return NSZeroRect;
153             
154             frame.origin = [fileOutlineView convertPoint: frame.origin toView: nil];
155             frame.origin = [[[self view] window] convertBaseToScreen: frame.origin];
156             frame.origin.y -= frame.size.height;
157             return frame;
158         }
159     }
160     
161     return NSZeroRect;
164 @end
166 @implementation InfoFileViewController (Private)
168 - (void) setupInfo
170     [fFileFilterField setStringValue: @""];
171     
172     if ([fTorrents count] == 1)
173     {
174         Torrent * torrent = [fTorrents objectAtIndex: 0];
175         
176         [fFileController setTorrent: torrent];
177         [fFileFilterField setEnabled: [torrent isFolder]];
178     }
179     else
180     {
181         [fFileController setTorrent: nil];
182         [fFileFilterField setEnabled: NO];
183     }
184     
185     fSet = YES;
188 - (BOOL) canQuickLookFile: (FileListNode *) item
190     Torrent * torrent = [fTorrents objectAtIndex: 0];
191     return ([item isFolder] || [torrent fileProgress: item] >= 1.0) && [torrent fileLocation: item];
194 @end