1/******************************************************************************
2 * Copyright (c) 2010-2012 Transmission authors and contributors
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 *****************************************************************************/
22
23#import "InfoGeneralViewController.h"
24#import "NSStringAdditions.h"
25#import "Torrent.h"
26
27@interface InfoGeneralViewController (Private)
28
29- (void) setupInfo;
30
31@end
32
33@implementation InfoGeneralViewController
34
35- (id) init
36{
37    if ((self = [super initWithNibName: @"InfoGeneralView" bundle: nil]))
38    {
39        [self setTitle: NSLocalizedString(@"General Info", "Inspector view -> title")];
40    }
41
42    return self;
43}
44
45
46- (void) awakeFromNib
47{
48    #warning remove when 10.7-only with auto layout
49    [fInfoSectionLabel sizeToFit];
50    [fWhereSectionLabel sizeToFit];
51
52    NSArray * labels = @[ fPiecesLabel, fHashLabel, fSecureLabel, fCreatorLabel, fDateCreatedLabel, fCommentLabel, fDataLocationLabel ];
53
54    CGFloat oldMaxWidth = 0.0, originX, newMaxWidth = 0.0;
55    for (NSTextField * label in labels)
56    {
57        const NSRect oldFrame = [label frame];
58        if (oldFrame.size.width > oldMaxWidth)
59        {
60            oldMaxWidth = oldFrame.size.width;
61            originX = oldFrame.origin.x;
62        }
63
64        [label sizeToFit];
65        const CGFloat newWidth = [label bounds].size.width;
66        if (newWidth > newMaxWidth)
67            newMaxWidth = newWidth;
68    }
69
70    for (NSTextField * label in labels)
71    {
72        NSRect frame = [label frame];
73        frame.origin.x = originX + (newMaxWidth - frame.size.width);
74        [label setFrame: frame];
75    }
76
77    NSArray * fields = @[ fPiecesField, fHashField, fSecureField, fCreatorField, fDateCreatedField, fCommentScrollView, fDataLocationField ];
78
79    const CGFloat widthIncrease = newMaxWidth - oldMaxWidth;
80    for (NSView * field in fields) {
81        NSRect frame = [field frame];
82        frame.origin.x += widthIncrease;
83        frame.size.width -= widthIncrease;
84        [field setFrame: frame];
85    }
86}
87
88- (void) setInfoForTorrents: (NSArray *) torrents
89{
90    //don't check if it's the same in case the metadata changed
91    fTorrents = torrents;
92
93    fSet = NO;
94}
95
96- (void) updateInfo
97{
98    if (!fSet)
99        [self setupInfo];
100
101    if ([fTorrents count] != 1)
102        return;
103
104    Torrent * torrent = fTorrents[0];
105
106    NSString * location = [torrent dataLocation];
107    [fDataLocationField setStringValue: location ? [location stringByAbbreviatingWithTildeInPath] : @""];
108    [fDataLocationField setToolTip: location ? location : @""];
109
110    [fRevealDataButton setHidden: !location];
111}
112
113- (void) revealDataFile: (id) sender
114{
115    Torrent * torrent = fTorrents[0];
116    NSString * location = [torrent dataLocation];
117    if (!location)
118        return;
119
120    NSURL * file = [NSURL fileURLWithPath: location];
121    [[NSWorkspace sharedWorkspace] activateFileViewerSelectingURLs: @[file]];
122}
123
124@end
125
126@implementation InfoGeneralViewController (Private)
127
128- (void) setupInfo
129{
130    if ([fTorrents count] == 1)
131    {
132        Torrent * torrent = fTorrents[0];
133
134        #warning candidate for localizedStringWithFormat (although then we'll get two commas)
135        NSString * piecesString = ![torrent isMagnet] ? [NSString stringWithFormat: @"%ld, %@", [torrent pieceCount],
136                                        [NSString stringForFileSize: [torrent pieceSize]]] : @"";
137        [fPiecesField setStringValue: piecesString];
138
139        NSString * hashString = [torrent hashString];
140        [fHashField setStringValue: hashString];
141        [fHashField setToolTip: hashString];
142        [fSecureField setStringValue: [torrent privateTorrent]
143                        ? NSLocalizedString(@"Private Torrent, non-tracker peer discovery disabled", "Inspector -> private torrent")
144                        : NSLocalizedString(@"Public Torrent", "Inspector -> private torrent")];
145
146        NSString * commentString = [torrent comment];
147        [fCommentView setString: commentString];
148
149        NSString * creatorString = [torrent creator];
150        [fCreatorField setStringValue: creatorString];
151        [fDateCreatedField setObjectValue: [torrent dateCreated]];
152    }
153    else
154    {
155        [fPiecesField setStringValue: @""];
156        [fHashField setStringValue: @""];
157        [fHashField setToolTip: nil];
158        [fSecureField setStringValue: @""];
159        [fCommentView setString: @""];
160
161        [fCreatorField setStringValue: @""];
162        [fDateCreatedField setStringValue: @""];
163
164        [fDataLocationField setStringValue: @""];
165        [fDataLocationField setToolTip: nil];
166
167        [fRevealDataButton setHidden: YES];
168    }
169
170    fSet = YES;
171}
172
173@end
174
175