source: trunk/macosx/InfoWindowController.m @ 10369

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

add an optional clearView method to the InfoViewController? protocol

  • Property svn:keywords set to Date Rev Author Id
File size: 20.0 KB
Line 
1/******************************************************************************
2 * $Id: InfoWindowController.m 10369 2010-03-14 01:52:45Z 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    //save resizeable view height
145    NSString * resizeSaveKey = nil;
146    switch (fCurrentTabTag)
147    {
148        case TAB_TRACKERS_TAG:
149            resizeSaveKey = @"InspectorContentHeightTracker";
150            break;
151        case TAB_PEERS_TAG:
152            resizeSaveKey = @"InspectorContentHeightPeers";
153            break;
154        case TAB_FILE_TAG:
155            resizeSaveKey = @"InspectorContentHeightFiles";
156            break;
157    }
158    if (resizeSaveKey)
159        [[NSUserDefaults standardUserDefaults] setFloat: [[fViewController view] frame].size.height forKey: resizeSaveKey];
160   
161    [[NSNotificationCenter defaultCenter] removeObserver: self];
162   
163    [fTorrents release];
164   
165    [super dealloc];
166}
167
168- (void) setInfoForTorrents: (NSArray *) torrents
169{
170    if (fTorrents && [fTorrents isEqualToArray: torrents])
171        return;
172   
173    [fTorrents release];
174    fTorrents = [torrents retain];
175   
176    [self resetInfo];
177}
178
179- (NSRect) windowWillUseStandardFrame: (NSWindow *) window defaultFrame: (NSRect) defaultFrame
180{
181    NSRect windowRect = [window frame];
182    windowRect.size.width = [window minSize].width;
183    return windowRect;
184}
185
186- (NSSize) windowWillResize: (NSWindow *) window toSize: (NSSize) proposedFrameSize
187{
188    //this is an edge-case - just stop the animation
189    [fPeersViewController stopWebSeedAnimation];
190   
191    return proposedFrameSize;
192}
193
194- (void) windowWillClose: (NSNotification *) notification
195{
196    if ([NSApp isOnSnowLeopardOrBetter] && fCurrentTabTag == TAB_FILE_TAG
197        && ([QLPreviewPanelSL sharedPreviewPanelExists] && [[QLPreviewPanelSL sharedPreviewPanel] isVisible]))
198        [[QLPreviewPanelSL sharedPreviewPanel] reloadData];
199}
200
201- (void) setTab: (id) sender
202{
203    const NSInteger oldTabTag = fCurrentTabTag;
204    fCurrentTabTag = [fTabMatrix selectedTag];
205    if (fCurrentTabTag == oldTabTag)
206        return;
207   
208    //take care of old view
209    CGFloat oldHeight = 0;
210    NSString * oldResizeSaveKey = nil;
211    if (oldTabTag != INVALID)
212    {
213        //deselect old tab item
214        [(InfoTabButtonCell *)[fTabMatrix cellWithTag: oldTabTag] setSelectedTab: NO];
215       
216        if ([fViewController respondsToSelector: @selector(clearView)])
217            [fViewController clearView];
218       
219        switch (oldTabTag)
220        {
221            case TAB_TRACKERS_TAG:
222                oldResizeSaveKey = @"InspectorContentHeightTracker";
223                break;
224            case TAB_PEERS_TAG:
225                oldResizeSaveKey = @"InspectorContentHeightPeers";
226                break;
227            case TAB_FILE_TAG:
228                oldResizeSaveKey = @"InspectorContentHeightFiles";
229                break;
230        }
231       
232        NSView * oldView = [fViewController view];
233        oldHeight = NSHeight([oldView frame]);
234        if (oldResizeSaveKey)
235            [[NSUserDefaults standardUserDefaults] setFloat: oldHeight forKey: oldResizeSaveKey];
236       
237        //remove old view
238        [oldView setHidden: YES];
239        [oldView removeFromSuperview];
240    }
241   
242    //set new tab item
243    #warning get titles from view controller?
244    NSString * resizeSaveKey = nil;
245    NSString * identifier, * title;
246    switch (fCurrentTabTag)
247    {
248        case TAB_GENERAL_TAG:
249            if (!fGeneralViewController)
250            {
251                fGeneralViewController = [[InfoGeneralViewController alloc] init];
252                [fGeneralViewController setInfoForTorrents: fTorrents];
253            }
254           
255            fViewController = fGeneralViewController;
256            identifier = TAB_INFO_IDENT;
257            title = NSLocalizedString(@"General Info", "Inspector -> title");
258            break;
259        case TAB_ACTIVITY_TAG:
260            if (!fActivityViewController)
261            {
262                fActivityViewController = [[InfoActivityViewController alloc] init];
263                [fActivityViewController setInfoForTorrents: fTorrents];
264            }
265           
266            fViewController = fActivityViewController;
267            identifier = TAB_ACTIVITY_IDENT;
268            title = NSLocalizedString(@"Activity", "Inspector -> title");
269            break;
270        case TAB_TRACKERS_TAG:
271            if (!fTrackersViewController)
272            {
273                fTrackersViewController = [[InfoTrackersViewController alloc] init];
274                [fTrackersViewController setInfoForTorrents: fTorrents];
275            }
276           
277            fViewController = fTrackersViewController;
278            identifier = TAB_TRACKER_IDENT;
279            title = NSLocalizedString(@"Trackers", "Inspector -> title");
280            resizeSaveKey = @"InspectorContentHeightTracker";
281            break;
282        case TAB_PEERS_TAG:
283            if (!fPeersViewController)
284            {
285                fPeersViewController = [[InfoPeersViewController alloc] init];
286                [fPeersViewController setInfoForTorrents: fTorrents];
287            }
288           
289            fViewController = fPeersViewController;
290            identifier = TAB_PEERS_IDENT;
291            title = NSLocalizedString(@"Peers", "Inspector -> title");
292            resizeSaveKey = @"InspectorContentHeightPeers";
293            break;
294        case TAB_FILE_TAG:
295            if (!fFileViewController)
296            {
297                fFileViewController = [[InfoFileViewController alloc] init];
298                [fFileViewController setInfoForTorrents: fTorrents];
299            }
300           
301            fViewController = fFileViewController;
302            identifier = TAB_FILES_IDENT;
303            title = NSLocalizedString(@"Files", "Inspector -> title");
304            resizeSaveKey = @"InspectorContentHeightFiles";
305            break;
306        case TAB_OPTIONS_TAG:
307            if (!fOptionsViewController)
308            {
309                fOptionsViewController = [[InfoOptionsViewController alloc] init];
310                [fOptionsViewController setInfoForTorrents: fTorrents];
311            }
312           
313            fViewController = fOptionsViewController;
314            identifier = TAB_OPTIONS_IDENT;
315            title = NSLocalizedString(@"Options", "Inspector -> title");
316            break;
317        default:
318            NSAssert1(NO, @"Unknown info tab selected: %d", fCurrentTabTag);
319            return;
320    }
321   
322    [[NSUserDefaults standardUserDefaults] setObject: identifier forKey: @"InspectorSelected"];
323   
324    NSWindow * window = [self window];
325   
326    [window setTitle: [NSString stringWithFormat: @"%@ - %@", title, NSLocalizedString(@"Torrent Inspector", "Inspector -> title")]];
327   
328    //selected tab item
329    [(InfoTabButtonCell *)[fTabMatrix selectedCell] setSelectedTab: YES];
330   
331    NSView * view = [fViewController view];
332   
333    [fViewController updateInfo];
334   
335    NSRect windowRect = [window frame], viewRect = [view frame];
336   
337    if (resizeSaveKey)
338    {
339        CGFloat height = [[NSUserDefaults standardUserDefaults] floatForKey: resizeSaveKey];
340        if (height != 0.0)
341            viewRect.size.height = MAX(height, TAB_MIN_HEIGHT);
342    }
343   
344    CGFloat difference = (viewRect.size.height - oldHeight) * [window userSpaceScaleFactor];
345    windowRect.origin.y -= difference;
346    windowRect.size.height += difference;
347   
348    if (resizeSaveKey)
349    {
350        if (!oldResizeSaveKey)
351        {
352            [window setMinSize: NSMakeSize([window minSize].width, windowRect.size.height - viewRect.size.height + TAB_MIN_HEIGHT)];
353            [window setMaxSize: NSMakeSize(FLT_MAX, FLT_MAX)];
354        }
355    }
356    else
357    {
358        [window setMinSize: NSMakeSize([window minSize].width, windowRect.size.height)];
359        [window setMaxSize: NSMakeSize(FLT_MAX, windowRect.size.height)];
360    }
361   
362    viewRect.size.width = windowRect.size.width;
363    [view setFrame: viewRect];
364   
365    [window setFrame: windowRect display: YES animate: oldTabTag != INVALID];
366    [[window contentView] addSubview: view];
367   
368    [view setHidden: NO];
369   
370    if ([NSApp isOnSnowLeopardOrBetter] && (fCurrentTabTag == TAB_FILE_TAG || oldTabTag == TAB_FILE_TAG)
371        && ([QLPreviewPanelSL sharedPreviewPanelExists] && [[QLPreviewPanelSL sharedPreviewPanel] isVisible]))
372        [[QLPreviewPanelSL sharedPreviewPanel] reloadData];
373}
374
375- (void) setNextTab
376{
377    NSInteger tag = [fTabMatrix selectedTag]+1;
378    if (tag >= [fTabMatrix numberOfColumns])
379        tag = 0;
380   
381    [fTabMatrix selectCellWithTag: tag];
382    [self setTab: nil];
383}
384
385- (void) setPreviousTab
386{
387    NSInteger tag = [fTabMatrix selectedTag]-1;
388    if (tag < 0)
389        tag = [fTabMatrix numberOfColumns]-1;
390   
391    [fTabMatrix selectCellWithTag: tag];
392    [self setTab: nil];
393}
394
395- (void) updateInfoStats
396{
397    [fViewController updateInfo];
398}
399
400- (void) updateOptions
401{
402    [fOptionsViewController updateOptions];
403}
404
405- (NSArray *) quickLookURLs
406{
407    return [fFileViewController quickLookURLs];
408}
409
410- (BOOL) canQuickLook
411{
412    if (fCurrentTabTag != TAB_FILE_TAG || ![[self window] isVisible] || ![NSApp isOnSnowLeopardOrBetter])
413        return NO;
414   
415    return [fFileViewController canQuickLook];
416}
417
418#warning uncomment (in header too)
419- (NSRect) quickLookSourceFrameForPreviewItem: (id /*<QLPreviewItem>*/) item
420{
421    return [fFileViewController quickLookSourceFrameForPreviewItem: item];
422}
423
424@end
425
426@implementation InfoWindowController (Private)
427
428- (void) resetInfo
429{
430    const NSUInteger numberSelected = [fTorrents count];
431    if (numberSelected != 1)
432    {
433        if (numberSelected > 0)
434        {
435            [fImageView setImage: [NSImage imageNamed: NSImageNameMultipleDocuments]];
436           
437            [fNameField setStringValue: [NSString stringWithFormat: NSLocalizedString(@"%d Torrents Selected",
438                                            "Inspector -> selected torrents"), numberSelected]];
439       
440            uint64_t size = 0;
441            NSUInteger fileCount = 0, magnetCount = 0;
442            for (Torrent * torrent in fTorrents)
443            {
444                size += [torrent size];
445                fileCount += [torrent fileCount];
446                if ([torrent isMagnet])
447                    ++magnetCount;
448            }
449           
450            NSMutableArray * fileStrings = [NSMutableArray arrayWithCapacity: 2];
451            if (fileCount > 0)
452            {
453                NSString * fileString;
454                if (fileCount == 1)
455                    fileString = NSLocalizedString(@"1 file", "Inspector -> selected torrents");
456                else
457                    fileString = [NSString stringWithFormat: NSLocalizedString(@"%d files", "Inspector -> selected torrents"), fileCount];
458                [fileStrings addObject: fileString];
459            }
460            if (magnetCount > 0)
461            {
462                NSString * magnetString;
463                if (magnetCount == 1)
464                    magnetString = NSLocalizedString(@"1 magnetized transfer", "Inspector -> selected torrents");
465                else
466                    magnetString = [NSString stringWithFormat: NSLocalizedString(@"%d magnetized transfers",
467                                    "Inspector -> selected torrents"), magnetCount];
468                [fileStrings addObject: magnetString];
469            }
470           
471            NSString * fileString = [fileStrings componentsJoinedByString: @" + "];
472           
473            if (magnetCount < numberSelected)
474            {
475                [fBasicInfoField setStringValue: [NSString stringWithFormat: @"%@, %@", fileString,
476                    [NSString stringWithFormat: NSLocalizedString(@"%@ total", "Inspector -> selected torrents"),
477                        [NSString stringForFileSize: size]]]];
478                [fBasicInfoField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes",
479                                                "Inspector -> selected torrents"), size]];
480            }
481            else
482            {
483                [fBasicInfoField setStringValue: fileString];
484                [fBasicInfoField setToolTip: nil];
485            }
486        }
487        else
488        {
489            [fImageView setImage: [NSImage imageNamed: @"NSApplicationIcon"]];
490           
491            [fNameField setStringValue: NSLocalizedString(@"No Torrents Selected", "Inspector -> selected torrents")];
492            [fBasicInfoField setStringValue: @""];
493            [fBasicInfoField setToolTip: @""];
494        }
495       
496        [fNameField setToolTip: nil];
497    }
498    else
499    {
500        Torrent * torrent = [fTorrents objectAtIndex: 0];
501       
502        if ([NSApp isOnSnowLeopardOrBetter])
503            [fImageView setImage: [torrent icon]];
504        else
505        {
506            NSImage * icon = [[torrent icon] copy];
507            [icon setFlipped: NO];
508            [fImageView setImage: icon];
509            [icon release];
510        }
511       
512        NSString * name = [torrent name];
513        [fNameField setStringValue: name];
514        [fNameField setToolTip: name];
515       
516        if (![torrent isMagnet])
517        {
518            NSString * basicString = [NSString stringForFileSize: [torrent size]];
519            if ([torrent isFolder])
520            {
521                NSString * fileString;
522                const NSInteger fileCount = [torrent fileCount];
523                if (fileCount == 1)
524                    fileString = NSLocalizedString(@"1 file", "Inspector -> selected torrents");
525                else
526                    fileString= [NSString stringWithFormat: NSLocalizedString(@"%d files", "Inspector -> selected torrents"), fileCount];
527                basicString = [NSString stringWithFormat: @"%@, %@", fileString, basicString];
528            }
529            [fBasicInfoField setStringValue: basicString];
530            [fBasicInfoField setToolTip: [NSString stringWithFormat: NSLocalizedString(@"%llu bytes", "Inspector -> selected torrents"),
531                                            [torrent size]]];
532        }
533        else
534        {
535            [fBasicInfoField setStringValue: NSLocalizedString(@"Magnetized transfer", "Inspector -> selected torrents")];
536            [fBasicInfoField setToolTip: nil];
537        }
538    }
539   
540    [fGeneralViewController setInfoForTorrents: fTorrents];
541    [fActivityViewController setInfoForTorrents: fTorrents];
542    [fTrackersViewController setInfoForTorrents: fTorrents];
543    [fPeersViewController setInfoForTorrents: fTorrents];
544    [fFileViewController setInfoForTorrents: fTorrents];
545    [fOptionsViewController setInfoForTorrents: fTorrents];
546   
547    [fViewController updateInfo];
548}
549
550- (void) resetInfoForTorrent: (NSNotification *) notification
551{
552    if (fTorrents && [fTorrents containsObject: [notification object]])
553        [self resetInfo];
554}
555
556@end
Note: See TracBrowser for help on using the repository browser.