source: trunk/macosx/InfoWindowController.m @ 10372

Last change on this file since 10372 was 10372, checked in by livings124, 12 years ago

remove unnecessary setHidden:'s

  • Property svn:keywords set to Date Rev Author Id
File size: 18.8 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 10372 2010-03-14 02:59:37Z livings124 $
3 *
4 * Copyright (c) 2006-2010 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 "InfoWindowController.h"
26#import "InfoViewController.h"
27#import "InfoGeneralViewController.h"
28#import "InfoActivityViewController.h"
29#import "InfoTrackersViewController.h"
30#import "InfoPeersViewController.h"
31#import "InfoFileViewController.h"
32#import "InfoOptionsViewController.h"
33#import "InfoTabButtonCell.h"
34#import "NSApplicationAdditions.h"
35#import "NSStringAdditions.h"
36#import "Torrent.h"
37
38#define TAB_INFO_IDENT @"Info"
39#define TAB_ACTIVITY_IDENT @"Activity"
40#define TAB_TRACKER_IDENT @"Tracker"
41#define TAB_PEERS_IDENT @"Peers"
42#define TAB_FILES_IDENT @"Files"
43#define TAB_OPTIONS_IDENT @"Options"
44
45#define TAB_MIN_HEIGHT 250
46
47#define INVALID -99
48
49typedef enum
50{
51    TAB_GENERAL_TAG = 0,
52    TAB_ACTIVITY_TAG = 1,
53    TAB_TRACKERS_TAG = 2,
54    TAB_PEERS_TAG = 3,
55    TAB_FILE_TAG = 4,
56    TAB_OPTIONS_TAG = 5
57} tabTag;
58
59@interface InfoWindowController (Private)
60
61- (void) resetInfo;
62- (void) resetInfoForTorrent: (NSNotification *) notification;
63
64@end
65
66@implementation InfoWindowController
67
68- (id) init
69{
70    self = [super initWithWindowNibName: @"InfoWindow"];
71    return self;
72}
73
74- (void) awakeFromNib
75{
76    //window location and size
77    NSPanel * window = (NSPanel *)[self window];
78   
79    const CGFloat windowHeight = NSHeight([window frame]);
80   
81    #warning check if this is still needed
82    [window setFrameAutosaveName: @"InspectorWindow"];
83    [window setFrameUsingName: @"InspectorWindow"];
84   
85    NSRect windowRect = [window frame];
86    windowRect.origin.y -= windowHeight - NSHeight(windowRect);
87    windowRect.size.height = windowHeight;
88    [window setFrame: windowRect display: NO];
89   
90    [window setBecomesKeyOnlyIfNeeded: YES];
91   
92    //set tab images
93    [[fTabMatrix cellWithTag: TAB_GENERAL_TAG] setIcon: [NSImage imageNamed: @"InfoGeneral.png"]];
94    [[fTabMatrix cellWithTag: TAB_ACTIVITY_TAG] setIcon: [NSImage imageNamed: @"InfoActivity.png"]];
95    [[fTabMatrix cellWithTag: TAB_TRACKERS_TAG] setIcon: [NSImage imageNamed: @"InfoTracker.png"]];
96    [[fTabMatrix cellWithTag: TAB_PEERS_TAG] setIcon: [NSImage imageNamed: @"InfoPeers.png"]];
97    [[fTabMatrix cellWithTag: TAB_FILE_TAG] setIcon: [NSImage imageNamed: @"InfoFiles.png"]];
98    [[fTabMatrix cellWithTag: TAB_OPTIONS_TAG] setIcon: [NSImage imageNamed: @"InfoOptions.png"]];
99   
100    //set tab tooltips
101    [fTabMatrix setToolTip: NSLocalizedString(@"General Info", "Inspector -> tab") forCell: [fTabMatrix cellWithTag: TAB_GENERAL_TAG]];
102    [fTabMatrix setToolTip: NSLocalizedString(@"Activity", "Inspector -> tab") forCell: [fTabMatrix cellWithTag: TAB_ACTIVITY_TAG]];
103    [fTabMatrix setToolTip: NSLocalizedString(@"Trackers", "Inspector -> tab") forCell: [fTabMatrix cellWithTag: TAB_TRACKERS_TAG]];
104    [fTabMatrix setToolTip: NSLocalizedString(@"Peers", "Inspector -> tab") forCell: [fTabMatrix cellWithTag: TAB_PEERS_TAG]];
105    [fTabMatrix setToolTip: NSLocalizedString(@"Files", "Inspector -> tab") forCell: [fTabMatrix cellWithTag: TAB_FILE_TAG]];
106    [fTabMatrix setToolTip: NSLocalizedString(@"Options", "Inspector -> tab") forCell: [fTabMatrix cellWithTag: TAB_OPTIONS_TAG]];
107   
108    //set selected tab
109    fCurrentTabTag = INVALID;
110    NSString * identifier = [[NSUserDefaults standardUserDefaults] stringForKey: @"InspectorSelected"];
111    NSInteger tag;
112    if ([identifier isEqualToString: TAB_INFO_IDENT])
113        tag = TAB_GENERAL_TAG;
114    else if ([identifier isEqualToString: TAB_ACTIVITY_IDENT])
115        tag = TAB_ACTIVITY_TAG;
116    else if ([identifier isEqualToString: TAB_TRACKER_IDENT])
117        tag = TAB_TRACKERS_TAG;
118    else if ([identifier isEqualToString: TAB_PEERS_IDENT])
119        tag = TAB_PEERS_TAG;
120    else if ([identifier isEqualToString: TAB_FILES_IDENT])
121        tag = TAB_FILE_TAG;
122    else if ([identifier isEqualToString: TAB_OPTIONS_IDENT])
123        tag = TAB_OPTIONS_TAG;
124    else //safety
125    {
126        [[NSUserDefaults standardUserDefaults] setObject: TAB_INFO_IDENT forKey: @"InspectorSelected"];
127        tag = TAB_GENERAL_TAG;
128    }
129    [fTabMatrix selectCellWithTag: tag];
130    [self setTab: nil];
131   
132    //set blank inspector
133    [self setInfoForTorrents: [NSArray array]];
134   
135    //allow for update notifications
136    NSNotificationCenter * nc = [NSNotificationCenter defaultCenter];
137    [nc addObserver: self selector: @selector(resetInfoForTorrent:) name: @"ResetInspector" object: nil];
138    [nc addObserver: self selector: @selector(updateInfoStats) name: @"UpdateStats" object: nil];
139    [nc addObserver: self selector: @selector(updateOptions) name: @"UpdateOptions" object: nil];
140}
141
142- (void) dealloc
143{
144    [[NSNotificationCenter defaultCenter] removeObserver: self];
145   
146    if ([fViewController respondsToSelector: @selector(saveViewSize)])
147        [fViewController saveViewSize];
148   
149    [fGeneralViewController dealloc];
150    [fActivityViewController dealloc];
151    [fTrackersViewController dealloc];
152    [fPeersViewController dealloc];
153    [fFileViewController dealloc];
154    [fOptionsViewController dealloc];
155   
156    [fTorrents release];
157   
158    [super dealloc];
159}
160
161- (void) setInfoForTorrents: (NSArray *) torrents
162{
163    if (fTorrents && [fTorrents isEqualToArray: torrents])
164        return;
165   
166    [fTorrents release];
167    fTorrents = [torrents retain];
168   
169    [self resetInfo];
170}
171
172- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
173{
174    NSRect windowRect = [window frame];
175    windowRect.size.width = [window minSize].width;
176    return windowRect;
177}
178
179- (NSSize) windowWillResize: (NSWindow *) window toSize: (NSSize) proposedFrameSize
180{
181    //this is an edge-case - just stop the animation
182    [fPeersViewController stopWebSeedAnimation];
183   
184    return proposedFrameSize;
185}
186
187- (void) windowWillClose: (NSNotification *) notification
188{
189    if ([NSApp isOnSnowLeopardOrBetter] && fCurrentTabTag == TAB_FILE_TAG
190        && ([QLPreviewPanelSL sharedPreviewPanelExists] && [[QLPreviewPanelSL sharedPreviewPanel] isVisible]))
191        [[QLPreviewPanelSL sharedPreviewPanel] reloadData];
192}
193
194- (void) setTab: (id) sender
195{
196    const NSInteger oldTabTag = fCurrentTabTag;
197    fCurrentTabTag = [fTabMatrix selectedTag];
198    if (fCurrentTabTag == oldTabTag)
199        return;
200   
201    //take care of old view
202    CGFloat oldHeight = 0;
203    if (oldTabTag != INVALID)
204    {
205        //deselect old tab item
206        [(InfoTabButtonCell *)[fTabMatrix cellWithTag: oldTabTag] setSelectedTab: NO];
207       
208        if ([fViewController respondsToSelector: @selector(saveViewSize)])
209            [fViewController saveViewSize];
210       
211        if ([fViewController respondsToSelector: @selector(clearView)])
212            [fViewController clearView];
213       
214        NSView * oldView = [fViewController view];
215        oldHeight = NSHeight([oldView frame]);
216       
217        //remove old view
218        [oldView removeFromSuperview];
219    }
220   
221    //set new tab item
222    #warning get titles from view controller?
223    NSString * identifier, * title;
224    switch (fCurrentTabTag)
225    {
226        case TAB_GENERAL_TAG:
227            if (!fGeneralViewController)
228            {
229                fGeneralViewController = [[InfoGeneralViewController alloc] init];
230                [fGeneralViewController setInfoForTorrents: fTorrents];
231            }
232           
233            fViewController = fGeneralViewController;
234            identifier = TAB_INFO_IDENT;
235            title = NSLocalizedString(@"General Info", "Inspector -> title");
236            break;
237        case TAB_ACTIVITY_TAG:
238            if (!fActivityViewController)
239            {
240                fActivityViewController = [[InfoActivityViewController alloc] init];
241                [fActivityViewController setInfoForTorrents: fTorrents];
242            }
243           
244            fViewController = fActivityViewController;
245            identifier = TAB_ACTIVITY_IDENT;
246            title = NSLocalizedString(@"Activity", "Inspector -> title");
247            break;
248        case TAB_TRACKERS_TAG:
249            if (!fTrackersViewController)
250            {
251                fTrackersViewController = [[InfoTrackersViewController alloc] init];
252                [fTrackersViewController setInfoForTorrents: fTorrents];
253            }
254           
255            fViewController = fTrackersViewController;
256            identifier = TAB_TRACKER_IDENT;
257            title = NSLocalizedString(@"Trackers", "Inspector -> title");
258            break;
259        case TAB_PEERS_TAG:
260            if (!fPeersViewController)
261            {
262                fPeersViewController = [[InfoPeersViewController alloc] init];
263                [fPeersViewController setInfoForTorrents: fTorrents];
264            }
265           
266            fViewController = fPeersViewController;
267            identifier = TAB_PEERS_IDENT;
268            title = NSLocalizedString(@"Peers", "Inspector -> title");
269            break;
270        case TAB_FILE_TAG:
271            if (!fFileViewController)
272            {
273                fFileViewController = [[InfoFileViewController alloc] init];
274                [fFileViewController setInfoForTorrents: fTorrents];
275            }
276           
277            fViewController = fFileViewController;
278            identifier = TAB_FILES_IDENT;
279            title = NSLocalizedString(@"Files", "Inspector -> title");
280            break;
281        case TAB_OPTIONS_TAG:
282            if (!fOptionsViewController)
283            {
284                fOptionsViewController = [[InfoOptionsViewController alloc] init];
285                [fOptionsViewController setInfoForTorrents: fTorrents];
286            }
287           
288            fViewController = fOptionsViewController;
289            identifier = TAB_OPTIONS_IDENT;
290            title = NSLocalizedString(@"Options", "Inspector -> title");
291            break;
292        default:
293            NSAssert1(NO, @"Unknown info tab selected: %d", fCurrentTabTag);
294            return;
295    }
296   
297    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InspectorSelected"];
298   
299    NSWindow * window = [self window];
300   
301    [window setTitle: [NSString stringWithFormat: @"%@ - %@", title, NSLocalizedString(@"Torrent Inspector", "Inspector -> title")]];
302   
303    //selected tab item
304    [(InfoTabButtonCell *)[fTabMatrix selectedCell] setSelectedTab: YES];
305   
306    NSView * view = [fViewController view];
307   
308    [fViewController updateInfo];
309   
310    NSRect windowRect = [window frame], viewRect = [view frame];
311   
312    CGFloat difference = (NSHeight(viewRect) - oldHeight) * [window userSpaceScaleFactor];
313    windowRect.origin.y -= difference;
314    windowRect.size.height += difference;
315   
316    if ([fViewController respondsToSelector: @selector(saveViewSize)]) //a little bit hacky, but avoids requiring an extra method
317    {
318        [window setMinSize: NSMakeSize([window minSize].width, NSHeight(windowRect) - NSHeight(viewRect) + TAB_MIN_HEIGHT)];
319        [window setMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
320    }
321    else
322    {
323        [window setMinSize: NSMakeSize([window minSize].width, NSHeight(windowRect))];
324        [window setMaxSize: NSMakeSize(FLT_MAX, NSHeight(windowRect))];
325    }
326   
327    viewRect.size.width = NSWidth(windowRect);
328    [view setFrame: viewRect];
329   
330    [window setFrame: windowRect display: YES animate: oldTabTag != INVALID];
331    [[window contentView] addSubview: view];
332   
333    if ([NSApp isOnSnowLeopardOrBetter] && (fCurrentTabTag == TAB_FILE_TAG || oldTabTag == TAB_FILE_TAG)
334        && ([QLPreviewPanelSL sharedPreviewPanelExists] && [[QLPreviewPanelSL sharedPreviewPanel] isVisible]))
335        [[QLPreviewPanelSL sharedPreviewPanel] reloadData];
336}
337
338- (void) setNextTab
339{
340    NSInteger tag = [fTabMatrix selectedTag]+1;
341    if (tag >= [fTabMatrix numberOfColumns])
342        tag = 0;
343   
344    [fTabMatrix selectCellWithTag: tag];
345    [self setTab: nil];
346}
347
348- (void) setPreviousTab
349{
350    NSInteger tag = [fTabMatrix selectedTag]-1;
351    if (tag < 0)
352        tag = [fTabMatrix numberOfColumns]-1;
353   
354    [fTabMatrix selectCellWithTag: tag];
355    [self setTab: nil];
356}
357
358- (void) updateInfoStats
359{
360    [fViewController updateInfo];
361}
362
363- (void) updateOptions
364{
365    [fOptionsViewController updateOptions];
366}
367
368- (NSArray *) quickLookURLs
369{
370    return [fFileViewController quickLookURLs];
371}
372
373- (BOOL) canQuickLook
374{
375    if (fCurrentTabTag != TAB_FILE_TAG || ![[self window] isVisible] || ![NSApp isOnSnowLeopardOrBetter])
376        return NO;
377   
378    return [fFileViewController canQuickLook];
379}
380
381#warning uncomment (in header too)
382- (NSRect) quickLookSourceFrameForPreviewItem: (id /*<QLPreviewItem>*/) item
383{
384    return [fFileViewController quickLookSourceFrameForPreviewItem: item];
385}
386
387@end
388
389@implementation InfoWindowController (Private)
390
391- (void) resetInfo
392{
393    const NSUInteger numberSelected = [fTorrents count];
394    if (numberSelected != 1)
395    {
396        if (numberSelected > 0)
397        {
398            [fImageView setImage: [NSImage imageNamed: NSImageNameMultipleDocuments]];
399           
400            [fNameField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%d Torrents Selected",
401                                            "Inspector -> selected torrents"), numberSelected]];
402       
403            uint64_t size = 0;
404            NSUInteger fileCount = 0, magnetCount = 0;
405            for (Torrent * torrent in fTorrents)
406            {
407                size += [torrent size];
408                fileCount += [torrent fileCount];
409                if ([torrent isMagnet])
410                    ++magnetCount;
411            }
412           
413            NSMutableArray * fileStrings = [NSMutableArray arrayWithCapacity: 2];
414            if (fileCount > 0)
415            {
416                NSString * fileString;
417                if (fileCount == 1)
418                    fileString = NSLocalizedString(@"1 file", "Inspector -> selected torrents");
419                else
420                    fileString = [NSString stringWithFormat: NSLocalizedString(@"%d files", "Inspector -> selected torrents"), fileCount];
421                [fileStrings addObject: fileString];
422            }
423            if (magnetCount > 0)
424            {
425                NSString * magnetString;
426                if (magnetCount == 1)
427                    magnetString = NSLocalizedString(@"1 magnetized transfer", "Inspector -> selected torrents");
428                else
429                    magnetString = [NSString stringWithFormat: NSLocalizedString(@"%d magnetized transfers",
430                                    "Inspector -> selected torrents"), magnetCount];
431                [fileStrings addObject: magnetString];
432            }
433           
434            NSString * fileString = [fileStrings componentsJoinedByString: @" + "];
435           
436            if (magnetCount < numberSelected)
437            {
438                [fBasicInfoField setStringValue: [NSString stringWithFormat: @"%@, %@", fileString,
439                    [NSString stringWithFormat: NSLocalizedString(@"%@ total", "Inspector -> selected torrents"),
440                        [NSString stringForFileSize: size]]]];
441                [fBasicInfoField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes",
442                                                "Inspector -> selected torrents"), size]];
443            }
444            else
445            {
446                [fBasicInfoField setStringValue: fileString];
447                [fBasicInfoField setToolTip: nil];
448            }
449        }
450        else
451        {
452            [fImageView setImage: [NSImage imageNamed: @"NSApplicationIcon"]];
453           
454            [fNameField setStringValue: NSLocalizedString(@"No Torrents Selected", "Inspector -> selected torrents")];
455            [fBasicInfoField setStringValue: @""];
456            [fBasicInfoField setToolTip: @""];
457        }
458       
459        [fNameField setToolTip: nil];
460    }
461    else
462    {
463        Torrent * torrent = [fTorrents objectAtIndex: 0];
464       
465        if ([NSApp isOnSnowLeopardOrBetter])
466            [fImageView setImage: [torrent icon]];
467        else
468        {
469            NSImage * icon = [[torrent icon] copy];
470            [icon setFlipped: NO];
471            [fImageView setImage: icon];
472            [icon release];
473        }
474       
475        NSString * name = [torrent name];
476        [fNameField setStringValue: name];
477        [fNameField setToolTip: name];
478       
479        if (![torrent isMagnet])
480        {
481            NSString * basicString = [NSString stringForFileSize: [torrent size]];
482            if ([torrent isFolder])
483            {
484                NSString * fileString;
485                const NSInteger fileCount = [torrent fileCount];
486                if (fileCount == 1)
487                    fileString = NSLocalizedString(@"1 file", "Inspector -> selected torrents");
488                else
489                    fileString= [NSString stringWithFormat: NSLocalizedString(@"%d files", "Inspector -> selected torrents"), fileCount];
490                basicString = [NSString stringWithFormat: @"%@, %@", fileString, basicString];
491            }
492            [fBasicInfoField setStringValue: basicString];
493            [fBasicInfoField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "Inspector -> selected torrents"),
494                                            [torrent size]]];
495        }
496        else
497        {
498            [fBasicInfoField setStringValue: NSLocalizedString(@"Magnetized transfer", "Inspector -> selected torrents")];
499            [fBasicInfoField setToolTip: nil];
500        }
501    }
502   
503    [fGeneralViewController setInfoForTorrents: fTorrents];
504    [fActivityViewController setInfoForTorrents: fTorrents];
505    [fTrackersViewController setInfoForTorrents: fTorrents];
506    [fPeersViewController setInfoForTorrents: fTorrents];
507    [fFileViewController setInfoForTorrents: fTorrents];
508    [fOptionsViewController setInfoForTorrents: fTorrents];
509   
510    [fViewController updateInfo];
511}
512
513- (void) resetInfoForTorrent: (NSNotification *) notification
514{
515    if (fTorrents && [fTorrents containsObject: [notification object]])
516        [self resetInfo];
517}
518
519@end
Note: See TracBrowser for help on using the repository browser.