1/******************************************************************************
2 * $Id: InfoGeneralViewController.m 13583 2012-10-19 03:52:59Z 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 "InfoGeneralViewController.h"
26#import "NSStringAdditions.h"
27#import "Torrent.h"
28
29@interface InfoGeneralViewController (Private)
30
31- (void) setupInfo;
32
33@end
34
35@implementation InfoGeneralViewController
36
37- (id) init
38{
39    if ((self = [super initWithNibName: @"InfoGeneralView" bundle: nil]))
40    {
41        [self setTitle: NSLocalizedString(@"General Info", "Inspector view -> title")];
42    }
43    
44    return self;
45}
46
47- (void) dealloc
48{
49    [fTorrents release];
50    
51    [super dealloc];
52}
53
54#warning enable after 2.7
55/*
56- (void) awakeFromNib
57{
58    #warning remove when 10.7-only with auto layout
59    [fInfoSectionLabel sizeToFit];
60    [fWhereSectionLabel sizeToFit];
61    
62    NSArray * labels = @[ fPiecesLabel, fHashLabel, fSecureLabel, fCreatorLabel, fDateCreatedLabel, fCommentLabel, fDataLocationLabel ];
63    
64    CGFloat oldMaxWidth = 0.0, originX, newMaxWidth = 0.0;
65    for (NSTextField * label in labels)
66    {
67        const NSRect oldFrame = [label frame];
68        if (oldFrame.size.width > oldMaxWidth)
69        {
70            oldMaxWidth = oldFrame.size.width;
71            originX = oldFrame.origin.x;
72        }
73        
74        [label sizeToFit];
75        const CGFloat newWidth = [label bounds].size.width;
76        if (newWidth > newMaxWidth)
77            newMaxWidth = newWidth;
78    }
79    
80    for (NSTextField * label in labels)
81    {
82        NSRect frame = [label frame];
83        frame.origin.x = originX + (newMaxWidth - frame.size.width);
84        [label setFrame: frame];
85    }
86    
87    NSArray * fields = @[ fPiecesField, fHashField, fSecureField, fCreatorField, fDateCreatedField, fCommentScrollView, fDataLocationField ];
88    
89    const CGFloat widthIncrease = newMaxWidth - oldMaxWidth;
90    for (NSView * field in fields) {
91        NSRect frame = [field frame];
92        frame.origin.x += widthIncrease;
93        frame.size.width -= widthIncrease;
94        [field setFrame: frame];
95    }
96}*/
97
98- (void) setInfoForTorrents: (NSArray *) torrents
99{
100    //don't check if it's the same in case the metadata changed
101    [fTorrents release];
102    fTorrents = [torrents retain];
103    
104    fSet = NO;
105}
106
107- (void) updateInfo
108{
109    if (!fSet)
110        [self setupInfo];
111    
112    if ([fTorrents count] != 1)
113        return;
114    
115    Torrent * torrent = [fTorrents objectAtIndex: 0];
116    
117    NSString * location = [torrent dataLocation];
118    [fDataLocationField setStringValue: location ? [location stringByAbbreviatingWithTildeInPath] : @""];
119    [fDataLocationField setToolTip: location ? location : @""];
120    
121    [fRevealDataButton setHidden: !location];
122}
123
124- (void) revealDataFile: (id) sender
125{
126    Torrent * torrent = [fTorrents objectAtIndex: 0];
127    NSString * location = [torrent dataLocation];
128    if (!location)
129        return;
130    
131    NSURL * file = [NSURL fileURLWithPath: location];
132    [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs: [NSArray arrayWithObject: file]];
133}
134
135@end
136
137@implementation InfoGeneralViewController (Private)
138
139- (void) setupInfo
140{
141    if ([fTorrents count] == 1)
142    {
143        Torrent * torrent = [fTorrents objectAtIndex: 0];
144        
145        NSString * piecesString = ![torrent isMagnet] ? [NSString stringWithFormat: @"%ld, %@", [torrent pieceCount],
146                                        [NSString stringForFileSize: [torrent pieceSize]]] : @"";
147        [fPiecesField setStringValue: piecesString];
148                                        
149        NSString * hashString = [torrent hashString];
150        [fHashField setStringValue: hashString];
151        [fHashField setToolTip: hashString];
152        [fSecureField setStringValue: [torrent privateTorrent]
153                        ? NSLocalizedString(@"Private Torrent, non-tracker peer discovery disabled", "Inspector -> private torrent")
154                        : NSLocalizedString(@"Public Torrent", "Inspector -> private torrent")];
155        
156        NSString * commentString = [torrent comment];
157        [fCommentView setString: commentString];
158        
159        NSString * creatorString = [torrent creator];
160        [fCreatorField setStringValue: creatorString];
161        [fDateCreatedField setObjectValue: [torrent dateCreated]];
162    }
163    else
164    {
165        [fPiecesField setStringValue: @""];
166        [fHashField setStringValue: @""];
167        [fHashField setToolTip: nil];
168        [fSecureField setStringValue: @""];
169        [fCommentView setString: @""];
170        
171        [fCreatorField setStringValue: @""];
172        [fDateCreatedField setStringValue: @""];
173        
174        [fDataLocationField setStringValue: @""];
175        [fDataLocationField setToolTip: nil];
176        
177        [fRevealDataButton setHidden: YES];
178    }
179    
180    fSet = YES;
181}
182
183@end
184
185