source: trunk/macosx/TorrentTableView.m @ 8036

Last change on this file since 8036 was 8036, checked in by livings124, 13 years ago

add an option in the per-torrent action menu to follow the global bandwidth limit

  • Property svn:keywords set to Date Rev Author Id
File size: 29.4 KB
Line 
1/******************************************************************************
2 * $Id: TorrentTableView.m 8036 2009-03-05 22:48:01Z livings124 $
3 *
4 * Copyright (c) 2005-2009 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 "TorrentTableView.h"
26#import "TorrentCell.h"
27#import "Torrent.h"
28#import "TorrentGroup.h"
29#import "FileListNode.h"
30#import "QuickLookController.h"
31
32#define MAX_GROUP 999999
33
34#define ACTION_MENU_GLOBAL_TAG 101
35#define ACTION_MENU_UNLIMITED_TAG 102
36#define ACTION_MENU_LIMIT_TAG 103
37
38#define GROUP_SPEED_IMAGE_COLUMN_WIDTH 8.0f
39#define GROUP_RATIO_IMAGE_COLUMN_WIDTH 10.0f
40
41#define TOGGLE_PROGRESS_SECONDS 0.175
42
43@interface TorrentTableView (Private)
44
45- (BOOL) pointInGroupStatusRect: (NSPoint) point;
46
47- (void) setGroupStatusColumns;
48
49- (void) createFileMenu: (NSMenu *) menu forFiles: (NSArray *) files;
50
51@end
52
53@implementation TorrentTableView
54
55- (id) initWithCoder: (NSCoder *) decoder
56{
57    if ((self = [super initWithCoder: decoder]))
58    {
59        fDefaults = [NSUserDefaults standardUserDefaults];
60       
61        fTorrentCell = [[TorrentCell alloc] init];
62       
63        NSData * groupData = [fDefaults dataForKey: @"CollapsedGroups"];
64        if (groupData)
65            fCollapsedGroups = [[NSUnarchiver unarchiveObjectWithData: groupData] mutableCopy];
66        else
67            fCollapsedGroups = [[NSMutableIndexSet alloc] init];
68       
69        fMouseControlRow = -1;
70        fMouseRevealRow = -1;
71        fMouseActionRow = -1;
72        fActionPushedRow = -1;
73       
74        [self setDelegate: self];
75       
76        fPiecesBarPercent = [fDefaults boolForKey: @"PiecesBar"] ? 1.0f : 0.0f;
77    }
78   
79    return self;
80}
81
82- (void) dealloc
83{
84    [fCollapsedGroups release];
85   
86    [fPiecesBarAnimation release];
87    [fMenuTorrent release];
88   
89    [fSelectedValues release];
90   
91    [fTorrentCell release];
92   
93    [super dealloc];
94}
95
96- (void) awakeFromNib
97{
98    //set group columns to show ratio, needs to be in awakeFromNib to size columns correctly
99    [self setGroupStatusColumns];
100}
101
102- (BOOL) isGroupCollapsed: (NSInteger) value
103{
104    if (value == -1)
105        value = MAX_GROUP;
106   
107    return [fCollapsedGroups containsIndex: value];
108}
109
110- (void) removeCollapsedGroup: (NSInteger) value
111{
112    if (value == -1)
113        value = MAX_GROUP;
114   
115    [fCollapsedGroups removeIndex: value];
116}
117
118- (void) removeAllCollapsedGroups
119{
120    [fCollapsedGroups removeAllIndexes];
121}
122
123- (void) saveCollapsedGroups
124{
125    [fDefaults setObject: [NSArchiver archivedDataWithRootObject: fCollapsedGroups] forKey: @"CollapsedGroups"];
126}
127
128- (BOOL) outlineView: (NSOutlineView *) outlineView isGroupItem: (id) item
129{
130    return ![item isKindOfClass: [Torrent class]];
131}
132
133- (CGFloat) outlineView: (NSOutlineView *) outlineView heightOfRowByItem: (id) item
134{
135    return [item isKindOfClass: [Torrent class]] ? [self rowHeight] : GROUP_SEPARATOR_HEIGHT;
136}
137
138- (NSCell *) outlineView: (NSOutlineView *) outlineView dataCellForTableColumn: (NSTableColumn *) tableColumn item: (id) item
139{
140    BOOL group = ![item isKindOfClass: [Torrent class]];
141    if (!tableColumn)
142        return !group ? fTorrentCell : nil;
143    else
144        return group ? [tableColumn dataCellForRow: [self rowForItem: item]] : nil;
145}
146
147- (void) outlineView: (NSOutlineView *) outlineView willDisplayCell: (id) cell forTableColumn: (NSTableColumn *) tableColumn
148    item: (id) item
149{
150    if ([item isKindOfClass: [Torrent class]])
151    {
152        [cell setRepresentedObject: item];
153       
154        const NSInteger row = [self rowForItem: item];
155        [cell setControlHover: row == fMouseControlRow];
156        [cell setRevealHover: row == fMouseRevealRow];
157        [cell setActionHover: row == fMouseActionRow];
158        [cell setActionPushed: row == fActionPushedRow];
159    }
160    else
161    {
162        NSString * ident = [tableColumn identifier];
163        if ([ident isEqualToString: @"UL Image"] || [ident isEqualToString: @"DL Image"])
164        {
165            //ensure arrows are white only when selected
166            NSImage * image = [cell image];
167            BOOL template = [cell backgroundStyle] == NSBackgroundStyleLowered;
168            if ([image isTemplate] != template)
169            {
170                [image setTemplate: template];
171                [cell setImage: nil];
172                [cell setImage: image];
173            }
174        }
175    }
176}
177
178- (NSRect) frameOfCellAtColumn: (NSInteger) column row: (NSInteger) row
179{
180    if (column == -1)
181        return [self rectOfRow: row];
182    else
183    {
184        NSRect rect = [super frameOfCellAtColumn: column row: row];
185       
186        //adjust placement for proper vertical alignment
187        if (column == [self columnWithIdentifier: @"Group"])
188            rect.size.height -= 1.0f;
189       
190        return rect;
191    }
192}
193
194- (NSString *) outlineView: (NSOutlineView *) outlineView typeSelectStringForTableColumn: (NSTableColumn *) tableColumn item: (id) item
195{
196    return [item isKindOfClass: [Torrent class]] ? [item name]
197            : [[self preparedCellAtColumn: [self columnWithIdentifier: @"Group"] row: [self rowForItem: item]] stringValue];
198}
199
200- (NSString *) outlineView: (NSOutlineView *) outlineView toolTipForCell: (NSCell *) cell rect: (NSRectPointer) rect
201                tableColumn: (NSTableColumn *) column item: (id) item mouseLocation: (NSPoint) mouseLocation
202{
203    NSString * ident = [column identifier];
204    if ([ident isEqualToString: @"DL"] || [ident isEqualToString: @"DL Image"])
205        return NSLocalizedString(@"Download speed", "Torrent table -> group row -> tooltip");
206    else if ([ident isEqualToString: @"UL"] || [ident isEqualToString: @"UL Image"])
207        return [fDefaults boolForKey: @"DisplayGroupRowRatio"] ? NSLocalizedString(@"Ratio", "Torrent table -> group row -> tooltip")
208                : NSLocalizedString(@"Upload speed", "Torrent table -> group row -> tooltip");
209    else if (ident)
210    {
211        NSInteger count = [[item torrents] count];
212        if (count == 1)
213            return NSLocalizedString(@"1 transfer", "Torrent table -> group row -> tooltip");
214        else
215            return [NSString stringWithFormat: NSLocalizedString(@"%d transfers", "Torrent table -> group row -> tooltip"), count];
216    }
217    else
218        return nil;
219}
220
221- (void) updateTrackingAreas
222{
223    [super updateTrackingAreas];
224    [self removeButtonTrackingAreas];
225   
226    NSRange rows = [self rowsInRect: [self visibleRect]];
227    if (rows.length == 0)
228        return;
229   
230    NSPoint mouseLocation = [self convertPoint: [[self window] convertScreenToBase: [NSEvent mouseLocation]] fromView: nil];
231    for (NSUInteger row = rows.location; row < NSMaxRange(rows); row++)
232    {
233        if (![[self itemAtRow: row] isKindOfClass: [Torrent class]])
234            continue;
235       
236        NSDictionary * userInfo = [NSDictionary dictionaryWithObject: [NSNumber numberWithInt: row] forKey: @"Row"];
237        TorrentCell * cell = (TorrentCell *)[self preparedCellAtColumn: -1 row: row];
238        [cell addTrackingAreasForView: self inRect: [self rectOfRow: row] withUserInfo: userInfo mouseLocation: mouseLocation];
239    }
240}
241
242- (void) removeButtonTrackingAreas
243{
244    fMouseControlRow = -1;
245    fMouseRevealRow = -1;
246    fMouseActionRow = -1;
247   
248    for (NSTrackingArea * area in [self trackingAreas])
249    {
250        if ([area owner] == self && [[area userInfo] objectForKey: @"Row"])
251            [self removeTrackingArea: area];
252    }
253}
254
255- (void) setControlButtonHover: (NSInteger) row
256{
257    fMouseControlRow = row;
258    if (row >= 0)
259        [self setNeedsDisplayInRect: [self rectOfRow: row]];
260}
261
262- (void) setRevealButtonHover: (NSInteger) row
263{
264    fMouseRevealRow = row;
265    if (row >= 0)
266        [self setNeedsDisplayInRect: [self rectOfRow: row]];
267}
268
269- (void) setActionButtonHover: (NSInteger) row
270{
271    fMouseActionRow = row;
272    if (row >= 0)
273        [self setNeedsDisplayInRect: [self rectOfRow: row]];
274}
275
276- (void) mouseEntered: (NSEvent *) event
277{
278    NSDictionary * dict = (NSDictionary *)[event userData];
279   
280    NSNumber * row;
281    if ((row = [dict objectForKey: @"Row"]))
282    {
283        NSInteger rowVal = [row intValue];
284        NSString * type = [dict objectForKey: @"Type"];
285        if ([type isEqualToString: @"Action"])
286            fMouseActionRow = rowVal;
287        else if ([type isEqualToString: @"Control"])
288            fMouseControlRow = rowVal;
289        else
290            fMouseRevealRow = rowVal;
291       
292        [self setNeedsDisplayInRect: [self rectOfRow: rowVal]];
293    }
294}
295
296- (void) mouseExited: (NSEvent *) event
297{
298    NSDictionary * dict = (NSDictionary *)[event userData];
299   
300    NSNumber * row;
301    if ((row = [dict objectForKey: @"Row"]))
302    {
303        NSString * type = [dict objectForKey: @"Type"];
304        if ([type isEqualToString: @"Action"])
305            fMouseActionRow = -1;
306        else if ([type isEqualToString: @"Control"])
307            fMouseControlRow = -1;
308        else
309            fMouseRevealRow = -1;
310       
311        [self setNeedsDisplayInRect: [self rectOfRow: [row intValue]]];
312    }
313}
314
315- (void) outlineViewSelectionIsChanging: (NSNotification *) notification
316{
317    if (fSelectedValues)
318        [self selectValues: fSelectedValues];
319}
320
321- (void) outlineViewItemDidExpand: (NSNotification *) notification
322{
323    NSInteger value = [[[notification userInfo] objectForKey: @"NSObject"] groupIndex];
324    if (value < 0)
325        value = MAX_GROUP;
326   
327    if ([fCollapsedGroups containsIndex: value])
328    {
329        [fCollapsedGroups removeIndex: value];
330        [[NSNotificationCenter defaultCenter] postNotificationName: @"OutlineExpandCollapse" object: self];
331    }
332}
333
334- (void) outlineViewItemDidCollapse: (NSNotification *) notification
335{
336    NSInteger value = [[[notification userInfo] objectForKey: @"NSObject"] groupIndex];
337    if (value < 0)
338        value = MAX_GROUP;
339   
340    [fCollapsedGroups addIndex: value];
341    [[NSNotificationCenter defaultCenter] postNotificationName: @"OutlineExpandCollapse" object: self];
342}
343
344- (void) mouseDown: (NSEvent *) event
345{
346    NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
347    const NSInteger row = [self rowAtPoint: point];
348   
349    //check to toggle group status before anything else
350    if ([self pointInGroupStatusRect: point])
351    {
352        [fDefaults setBool: ![fDefaults boolForKey: @"DisplayGroupRowRatio"] forKey: @"DisplayGroupRowRatio"];
353        [self setGroupStatusColumns];
354       
355        return;
356    }
357   
358    const BOOL pushed = row != -1 && (fMouseActionRow == row || fMouseRevealRow == row || fMouseControlRow == row);
359   
360    //if pushing a button, don't change the selected rows
361    if (pushed)
362        fSelectedValues = [[self selectedValues] retain];
363   
364    [super mouseDown: event];
365   
366    [fSelectedValues release];
367    fSelectedValues = nil;
368   
369    //avoid weird behavior when showing menu by doing this after mouse down
370    if (row != -1 && fMouseActionRow == row)
371    {
372        fActionPushedRow = row;
373        [self setNeedsDisplayInRect: [self rectOfRow: row]]; //ensure button is pushed down
374       
375        [self displayTorrentMenuForEvent: event];
376       
377        fActionPushedRow = -1;
378        [self setNeedsDisplayInRect: [self rectOfRow: row]];
379    }
380    else if (!pushed && [event clickCount] == 2) //double click
381    {
382        id item = nil;
383        if (row != -1)
384            item = [self itemAtRow: row];
385       
386        if (!item || [item isKindOfClass: [Torrent class]])
387            [fController showInfo: nil];
388        else
389        {
390            if ([self isItemExpanded: item])
391                [self collapseItem: item];
392            else
393                [self expandItem: item];
394        }
395    }
396    else;
397}
398
399- (void) selectValues: (NSArray *) values
400{
401    NSMutableIndexSet * indexSet = [NSMutableIndexSet indexSet];
402   
403    for (id item in values)
404    {
405        if ([item isKindOfClass: [Torrent class]])
406        {
407            NSInteger index = [self rowForItem: item];
408            if (index != -1)
409                [indexSet addIndex: index];
410        }
411        else
412        {
413            NSInteger group = [item groupIndex];
414            for (NSInteger i = 0; i < [self numberOfRows]; i++)
415            {
416                if ([indexSet containsIndex: i])
417                    continue;
418               
419                id tableItem = [self itemAtRow: i];
420                if (![tableItem isKindOfClass: [Torrent class]] && group == [tableItem groupIndex])
421                {
422                    [indexSet addIndex: i];
423                    break;
424                }
425            }
426        }
427    }
428   
429    [self selectRowIndexes: indexSet byExtendingSelection: NO];
430}
431
432- (NSArray *) selectedValues
433{
434    NSIndexSet * selectedIndexes = [self selectedRowIndexes];
435    NSMutableArray * values = [NSMutableArray arrayWithCapacity: [selectedIndexes count]];
436   
437    for (NSUInteger i = [selectedIndexes firstIndex]; i != NSNotFound; i = [selectedIndexes indexGreaterThanIndex: i])
438        [values addObject: [self itemAtRow: i]];
439   
440    return values;
441}
442
443- (NSArray *) selectedTorrents
444{
445    NSIndexSet * selectedIndexes = [self selectedRowIndexes];
446    NSMutableArray * torrents = [NSMutableArray arrayWithCapacity: [selectedIndexes count]]; //take a shot at guessing capacity
447   
448    for (NSUInteger i = [selectedIndexes firstIndex]; i != NSNotFound; i = [selectedIndexes indexGreaterThanIndex: i])
449    {
450        id item = [self itemAtRow: i];
451        if ([item isKindOfClass: [Torrent class]])
452            [torrents addObject: item];
453        else
454        {
455            NSArray * groupTorrents = [item torrents];
456            [torrents addObjectsFromArray: groupTorrents];
457            i += [groupTorrents count];
458        }
459    }
460   
461    return torrents;
462}
463
464- (NSMenu *) menuForEvent: (NSEvent *) event
465{
466    NSInteger row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
467    if (row >= 0)
468    {
469        if (![self isRowSelected: row])
470            [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
471        return fContextRow;
472    }
473    else
474    {
475        [self deselectAll: self];
476        return fContextNoRow;
477    }
478}
479
480//make sure that the pause buttons become orange when holding down the option key
481- (void) flagsChanged: (NSEvent *) event
482{
483    [self display];
484    [super flagsChanged: event];
485}
486
487//option-command-f will focus the filter bar's search field
488- (void) keyDown: (NSEvent *) event
489{
490    const unichar firstChar = [[event charactersIgnoringModifiers] characterAtIndex: 0];
491   
492    if (firstChar == 'f' && [event modifierFlags] & NSAlternateKeyMask && [event modifierFlags] & NSCommandKeyMask)
493        [fController focusFilterField];
494    else
495    {
496        //handle quicklook
497        if (firstChar == ' ')
498            [[QuickLookController quickLook] toggleQuickLook];
499        else if (firstChar == NSRightArrowFunctionKey)
500            [[QuickLookController quickLook] pressRight];
501        else if (firstChar == NSLeftArrowFunctionKey)
502            [[QuickLookController quickLook] pressLeft];
503        else;
504       
505        [super keyDown: event];
506    }
507}
508
509- (NSRect) iconRectForRow: (NSInteger) row
510{
511    return [fTorrentCell iconRectForBounds: [self rectOfRow: row]];
512}
513
514- (void) paste: (id) sender
515{
516    NSURL * url;
517    if ((url = [NSURL URLFromPasteboard: [NSPasteboard generalPasteboard]]))
518        [fController openURL: url];
519}
520
521- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
522{
523    SEL action = [menuItem action];
524   
525    if (action == @selector(paste:))
526        return [[[NSPasteboard generalPasteboard] types] containsObject: NSURLPboardType];
527   
528    return YES;
529}
530
531- (void) toggleControlForTorrent: (Torrent *) torrent
532{
533    if ([torrent isActive])
534        [fController stopTorrents: [NSArray arrayWithObject: torrent]];
535    else
536    {
537        if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask)
538            [fController resumeTorrentsNoWait: [NSArray arrayWithObject: torrent]];
539        else if ([torrent waitingToStart])
540            [fController stopTorrents: [NSArray arrayWithObject: torrent]];
541        else
542            [fController resumeTorrents: [NSArray arrayWithObject: torrent]];
543    }
544}
545
546- (void) displayTorrentMenuForEvent: (NSEvent *) event
547{
548    NSInteger row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
549    if (row < 0)
550        return;
551   
552    NSInteger numberOfNonFileItems = [fActionMenu numberOfItems];
553   
554    //update file action menu
555    fMenuTorrent = [[self itemAtRow: row] retain];
556    [self createFileMenu: fActionMenu forFiles: [fMenuTorrent fileList]];
557   
558    //update global limit check
559    [fGlobalLimitItem setState: [fMenuTorrent usesGlobalSpeedLimit] ? NSOnState : NSOffState];
560   
561    //place menu below button
562    NSRect rect = [fTorrentCell iconRectForBounds: [self rectOfRow: row]];
563    NSPoint location = rect.origin;
564    location.y += rect.size.height + 5.0f;
565    location = [self convertPoint: location toView: nil];
566   
567    NSEvent * newEvent = [NSEvent mouseEventWithType: [event type] location: location
568        modifierFlags: [event modifierFlags] timestamp: [event timestamp] windowNumber: [event windowNumber]
569        context: [event context] eventNumber: [event eventNumber] clickCount: [event clickCount] pressure: [event pressure]];
570   
571    [NSMenu popUpContextMenu: fActionMenu withEvent: newEvent forView: self];
572   
573    for (NSInteger i = [fActionMenu numberOfItems]-1; i >= numberOfNonFileItems; i--)
574        [fActionMenu removeItemAtIndex: i];
575   
576    [fMenuTorrent release];
577    fMenuTorrent = nil;
578}
579
580- (void) menuNeedsUpdate: (NSMenu *) menu
581{
582    //this method seems to be called when it shouldn't be
583    if (!fMenuTorrent || ![menu supermenu])
584        return;
585   
586    if (menu == fUploadMenu || menu == fDownloadMenu)
587    {
588        NSMenuItem * item;
589        if ([menu numberOfItems] == 3)
590        {
591            const NSInteger speedLimitActionValue[] = { 0, 5, 10, 20, 30, 40, 50, 75, 100, 150, 200, 250, 500, 750, -1 };
592           
593            for (NSInteger i = 0; speedLimitActionValue[i] != -1; i++)
594            {
595                item = [[NSMenuItem alloc] initWithTitle: [NSString stringWithFormat: NSLocalizedString(@"%d KB/s",
596                        "Action menu -> upload/download limit"), speedLimitActionValue[i]] action: @selector(setQuickLimit:)
597                        keyEquivalent: @""];
598                [item setTarget: self];
599                [item setRepresentedObject: [NSNumber numberWithInt: speedLimitActionValue[i]]];
600                [menu addItem: item];
601                [item release];
602            }
603        }
604       
605        const BOOL upload = menu == fUploadMenu;
606        const BOOL limit = [fMenuTorrent usesSpeedLimit: upload];
607       
608        item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG];
609        [item setState: limit ? NSOnState : NSOffState];
610        [item setTitle: [NSString stringWithFormat: NSLocalizedString(@"Limit (%d KB/s)",
611                            "torrent action menu -> upload/download limit"), [fMenuTorrent speedLimit: upload]]];
612       
613        item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG];
614        [item setState: !limit ? NSOnState : NSOffState];
615    }
616    else if (menu == fRatioMenu)
617    {
618        NSMenuItem * item;
619        if ([menu numberOfItems] == 4)
620        {
621            const CGFloat ratioLimitActionValue[] = { 0.25f, 0.5f, 0.75f, 1.0f, 1.5f, 2.0f, 3.0f, -1.0f };
622           
623            for (NSInteger i = 0; ratioLimitActionValue[i] != -1.0f; i++)
624            {
625                item = [[NSMenuItem alloc] initWithTitle: [NSString localizedStringWithFormat: @"%.2f", ratioLimitActionValue[i]]
626                        action: @selector(setQuickRatio:) keyEquivalent: @""];
627                [item setTarget: self];
628                [item setRepresentedObject: [NSNumber numberWithFloat: ratioLimitActionValue[i]]];
629                [menu addItem: item];
630                [item release];
631            }
632        }
633       
634        const tr_ratiolimit mode = [fMenuTorrent ratioSetting];
635       
636        item = [menu itemWithTag: ACTION_MENU_LIMIT_TAG];
637        [item setState: mode == TR_RATIOLIMIT_SINGLE ? NSOnState : NSOffState];
638        [item setTitle: [NSString localizedStringWithFormat: NSLocalizedString(@"Stop at Ratio (%.2f)",
639            "torrent action menu -> ratio stop"), [fMenuTorrent ratioLimit]]];
640       
641        item = [menu itemWithTag: ACTION_MENU_UNLIMITED_TAG];
642        [item setState: mode == TR_RATIOLIMIT_UNLIMITED ? NSOnState : NSOffState];
643       
644        item = [menu itemWithTag: ACTION_MENU_GLOBAL_TAG];
645        [item setState: mode == TR_RATIOLIMIT_GLOBAL ? NSOnState : NSOffState];
646    }
647    else //assume the menu is part of the file list
648    {
649        if ([menu numberOfItems] > 0)
650            return;
651       
652        NSMenu * supermenu = [menu supermenu];
653        [self createFileMenu: menu forFiles: [(FileListNode *)[[supermenu itemAtIndex: [supermenu indexOfItemWithSubmenu: menu]]
654                                                representedObject] children]];
655    }
656}
657
658//alternating rows - first row after group row is white
659- (void) highlightSelectionInClipRect: (NSRect) clipRect
660{
661    NSColor * altColor = [[NSColor controlAlternatingRowBackgroundColors] objectAtIndex: 1];
662    [altColor set];
663   
664    NSRect visibleRect = clipRect;
665    NSRange rows = [self rowsInRect: visibleRect];
666    BOOL start = YES;
667   
668    if (rows.length > 0)
669    {
670        //determine what the first row color should be
671        if ([[self itemAtRow: rows.location] isKindOfClass: [Torrent class]])
672        {
673            for (NSInteger i = rows.location-1; i>=0; i--)
674            {
675                if (![[self itemAtRow: i] isKindOfClass: [Torrent class]])
676                    break;
677                start = !start;
678            }
679        }
680        else
681        {
682            rows.location++;
683            rows.length--;
684        }
685       
686        NSInteger i;
687        for (i = rows.location; i < NSMaxRange(rows); i++)
688        {
689            if (![[self itemAtRow: i] isKindOfClass: [Torrent class]])
690            {
691                start = YES;
692                continue;
693            }
694           
695            if (!start && ![self isRowSelected: i])
696                NSRectFill([self rectOfRow: i]);
697           
698            start = !start;
699        }
700       
701        CGFloat newY = NSMaxY([self rectOfRow: i-1]);
702        visibleRect.size.height -= newY - visibleRect.origin.y;
703        visibleRect.origin.y = newY;
704    }
705       
706    //remaining visible rows continue alternating
707    const CGFloat height = [self rowHeight] + [self intercellSpacing].height;
708    const NSInteger numberOfRects = ceil(visibleRect.size.height / height);
709   
710    visibleRect.size.height = height;
711    if (start)
712        visibleRect.origin.y += height;
713   
714    for (NSInteger i = start ? 1 : 0; i < numberOfRects; i += 2)
715    {
716        NSRectFill(visibleRect);
717        visibleRect.origin.y += 2.0 * height;
718    }
719   
720    [super highlightSelectionInClipRect: clipRect];
721}
722
723- (void) setQuickLimitMode: (id) sender
724{
725    const BOOL limit = [sender tag] == ACTION_MENU_LIMIT_TAG;
726    [fMenuTorrent setUseSpeedLimit: limit upload: [sender menu] == fUploadMenu];
727   
728    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
729}
730
731- (void) setQuickLimit: (id) sender
732{
733    const BOOL upload = [sender menu] == fUploadMenu;
734    [fMenuTorrent setUseSpeedLimit: YES upload: upload];
735    [fMenuTorrent setSpeedLimit: [[sender representedObject] intValue] upload: upload];
736   
737    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
738}
739
740- (void) setGlobalLimit: (id) sender
741{
742    [fMenuTorrent setUseGlobalSpeedLimit: [sender state] != NSOnState];
743   
744    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
745}
746
747- (void) setQuickRatioMode: (id) sender
748{
749    tr_ratiolimit mode;
750    switch ([sender tag])
751    {
752        case ACTION_MENU_UNLIMITED_TAG:
753            mode = TR_RATIOLIMIT_UNLIMITED;
754            break;
755        case ACTION_MENU_LIMIT_TAG:
756            mode = TR_RATIOLIMIT_SINGLE;
757            break;
758        case ACTION_MENU_GLOBAL_TAG:
759            mode = TR_RATIOLIMIT_GLOBAL;
760            break;
761        default:
762            return;
763    }
764   
765    [fMenuTorrent setRatioSetting: mode];
766   
767    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
768}
769
770- (void) setQuickRatio: (id) sender
771{
772    [fMenuTorrent setRatioSetting: TR_RATIOLIMIT_SINGLE];
773    [fMenuTorrent setRatioLimit: [[sender representedObject] floatValue]];
774   
775    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateOptions" object: nil];
776}
777
778- (void) checkFile: (id) sender
779{
780    NSIndexSet * indexSet = [(FileListNode *)[sender representedObject] indexes];
781    [fMenuTorrent setFileCheckState: [sender state] != NSOnState ? NSOnState : NSOffState forIndexes: indexSet];
782   
783    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil];
784}
785
786- (void) moveDataFile: (id) sender
787{
788    [fController moveDataFiles: [NSArray arrayWithObject: fMenuTorrent]];
789}
790
791- (void) togglePiecesBar
792{
793    //stop previous animation
794    if (fPiecesBarAnimation)
795        [fPiecesBarAnimation release];
796   
797    NSMutableArray * progressMarks = [NSMutableArray arrayWithCapacity: 16];
798    for (NSAnimationProgress i = 0.0625f; i <= 1.0f; i += 0.0625f)
799        [progressMarks addObject: [NSNumber numberWithFloat: i]];
800   
801    fPiecesBarAnimation = [[NSAnimation alloc] initWithDuration: TOGGLE_PROGRESS_SECONDS animationCurve: NSAnimationEaseIn];
802    [fPiecesBarAnimation setAnimationBlockingMode: NSAnimationNonblocking];
803    [fPiecesBarAnimation setProgressMarks: progressMarks];
804    [fPiecesBarAnimation setDelegate: self];
805   
806    [fPiecesBarAnimation startAnimation];
807}
808
809- (void) animationDidEnd: (NSAnimation *) animation
810{
811    if (animation == fPiecesBarAnimation)
812    {
813        [fPiecesBarAnimation release];
814        fPiecesBarAnimation = nil;
815    }
816}
817
818- (void) animation: (NSAnimation *) animation didReachProgressMark: (NSAnimationProgress) progress
819{
820    if (animation == fPiecesBarAnimation)
821    {
822        if ([fDefaults boolForKey: @"PiecesBar"])
823            fPiecesBarPercent = progress;
824        else
825            fPiecesBarPercent = 1.0f - progress;
826       
827        [self reloadData];
828    }
829}
830
831- (CGFloat) piecesBarPercent
832{
833    return fPiecesBarPercent;
834}
835
836@end
837
838@implementation TorrentTableView (Private)
839
840- (BOOL) pointInGroupStatusRect: (NSPoint) point
841{
842    NSInteger row = [self rowAtPoint: point];
843    if (row < 0 || [[self itemAtRow: row] isKindOfClass: [Torrent class]])
844        return NO;
845   
846    NSString * ident = [[[self tableColumns] objectAtIndex: [self columnAtPoint: point]] identifier];
847    return [ident isEqualToString: @"UL"] || [ident isEqualToString: @"UL Image"]
848            || [ident isEqualToString: @"DL"] || [ident isEqualToString: @"DL Image"];
849}
850
851- (void) setGroupStatusColumns
852{
853    BOOL ratio = [fDefaults boolForKey: @"DisplayGroupRowRatio"];
854   
855    [[self tableColumnWithIdentifier: @"DL"] setHidden: ratio];
856    [[self tableColumnWithIdentifier: @"DL Image"] setHidden: ratio];
857   
858    //change size of image column
859    NSTableColumn * ulImageTableColumn = [self tableColumnWithIdentifier: @"UL Image"];
860    CGFloat oldWidth = [ulImageTableColumn width], newWidth = ratio ? GROUP_RATIO_IMAGE_COLUMN_WIDTH : GROUP_SPEED_IMAGE_COLUMN_WIDTH;
861    if (oldWidth != newWidth)
862    {
863        [ulImageTableColumn setWidth: newWidth];
864       
865        NSTableColumn * groupTableColumn = [self tableColumnWithIdentifier: @"Group"];
866        [groupTableColumn setWidth: [groupTableColumn width] - (newWidth - oldWidth)];
867    }
868}
869
870- (void) createFileMenu: (NSMenu *) menu forFiles: (NSArray *) files
871{
872    for (FileListNode * node in files)
873    {
874        NSString * name = [node name];
875       
876        NSMenuItem * item = [[NSMenuItem alloc] initWithTitle: name action: @selector(checkFile:) keyEquivalent: @""];
877       
878        NSImage * icon;
879        if (![node isFolder])
880            icon = [[NSWorkspace sharedWorkspace] iconForFileType: [name pathExtension]];
881        else
882        {
883            NSMenu * itemMenu = [[NSMenu alloc] initWithTitle: name];
884            [itemMenu setAutoenablesItems: NO];
885            [item setSubmenu: itemMenu];
886            [itemMenu setDelegate: self];
887            [itemMenu release];
888           
889            icon = [[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')];
890        }
891       
892        [item setRepresentedObject: node];
893       
894        [icon setScalesWhenResized: YES];
895        [icon setSize: NSMakeSize(16.0, 16.0)];
896        [item setImage: icon];
897       
898        NSIndexSet * indexSet = [node indexes];
899        [item setState: [fMenuTorrent checkForFiles: indexSet]];
900        [item setEnabled: [fMenuTorrent canChangeDownloadCheckForFiles: indexSet]];
901       
902        [menu addItem: item];
903        [item release];
904    }
905}
906
907@end
Note: See TracBrowser for help on using the repository browser.