source: trunk/macosx/TrackerTableView.m @ 9227

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

#2470 Pasting an address (or list of addresses) into the tracker list should add those trackers

  • Property svn:keywords set to Date Rev Author Id
File size: 6.2 KB
Line 
1/******************************************************************************
2 * $Id: TrackerTableView.m 9227 2009-10-01 01:15:37Z livings124 $
3 *
4 * Copyright (c) 2008-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 "TrackerTableView.h"
26#import "NSApplicationAdditions.h"
27#import "Torrent.h"
28#import "TrackerNode.h"
29
30@implementation TrackerTableView
31
32- (void) mouseDown: (NSEvent *) event
33{
34    [[self window] makeKeyWindow];
35    [super mouseDown: event];
36}
37
38- (void) setTorrent: (Torrent *) torrent
39{
40    fTorrent = torrent;
41}
42
43- (void) setTrackers: (NSArray *) trackers
44{
45    fTrackers = trackers;
46}
47
48- (void) copy: (id) sender
49{
50    NSMutableArray * addresses = [NSMutableArray arrayWithCapacity: [fTrackers count]];
51    NSIndexSet * indexes = [self selectedRowIndexes];
52    for (NSUInteger i = [indexes firstIndex]; i != NSNotFound; i = [indexes indexGreaterThanIndex: i])
53    {
54        id item = [fTrackers objectAtIndex: i];
55        if ([item isKindOfClass: [NSNumber class]])
56        {
57            for (++i; i < [fTrackers count] && ![[fTrackers objectAtIndex: i] isKindOfClass: [NSNumber class]]; ++i)
58                [addresses addObject: [(TrackerNode *)[fTrackers objectAtIndex: i] fullAnnounceAddress]];
59            --i;
60        }
61        else
62            [addresses addObject: [(TrackerNode *)item fullAnnounceAddress]];
63    }
64   
65    NSString * text = [addresses componentsJoinedByString: @"\n"];
66   
67    NSPasteboard * pb = [NSPasteboard generalPasteboard];
68    if ([NSApp isOnSnowLeopardOrBetter])
69    {
70        [pb clearContents];
71        [pb writeObjects: [NSArray arrayWithObject: text]];
72    }
73    else
74    {
75        [pb declareTypes: [NSArray arrayWithObject: NSStringPboardType] owner: nil];
76        [pb setString: text forType: NSStringPboardType];
77    }
78}
79
80- (void) paste: (id) sender
81{
82    if (!fTorrent)
83        return;
84   
85    if ([NSApp isOnSnowLeopardOrBetter])
86    {
87        NSArray * items = [[NSPasteboard generalPasteboard] readObjectsForClasses:
88                            [NSArray arrayWithObject: [NSString class]] options: nil];
89        NSAssert(items != nil, @"no string items to paste; should not be able to call this method");
90       
91        BOOL added = NO;
92        for (NSString * pbItem in items)
93        {
94            for (NSString * item in [pbItem componentsSeparatedByString: @"\n"])
95                if ([fTorrent addTrackerToNewTier: item])
96                    added = YES;
97        }
98       
99        //none added
100        if (!added)
101            NSBeep();
102    }
103}
104
105- (BOOL) validateMenuItem: (NSMenuItem *) menuItem
106{
107    const SEL action = [menuItem action];
108   
109    if (action == @selector(copy:))
110        return [self numberOfSelectedRows] > 0;
111   
112    if (action == @selector(paste:))
113        return [[NSPasteboard generalPasteboard] canReadObjectForClasses: [NSArray arrayWithObject: [NSString class]] options: nil];
114   
115    return YES;
116}
117
118//alternating rows - first row after group row is white
119- (void) highlightSelectionInClipRect: (NSRect) clipRect
120{
121    NSRect visibleRect = clipRect;
122    NSRange rows = [self rowsInRect: visibleRect];
123    BOOL start = YES;
124   
125    const CGFloat totalRowHeight = [self rowHeight] + [self intercellSpacing].height;
126   
127    NSRect gridRects[(NSInteger)(ceil(visibleRect.size.height / totalRowHeight / 2.0)) + 1]; //add one if partial rows at top and bottom
128    NSInteger rectNum = 0;
129   
130    if (rows.length > 0)
131    {
132        //determine what the first row color should be
133        if (![[fTrackers objectAtIndex: rows.location] isKindOfClass: [NSNumber class]])
134        {
135            for (NSInteger i = rows.location-1; i>=0; i--)
136            {
137                if ([[fTrackers objectAtIndex: i] isKindOfClass: [NSNumber class]])
138                    break;
139                start = !start;
140            }
141        }
142        else
143        {
144            rows.location++;
145            rows.length--;
146        }
147       
148        NSInteger i;
149        for (i = rows.location; i < NSMaxRange(rows); i++)
150        {
151            if ([[fTrackers objectAtIndex: i] isKindOfClass: [NSNumber class]])
152            {
153                start = YES;
154                continue;
155            }
156           
157            if (!start && ![self isRowSelected: i])
158                gridRects[rectNum++] = [self rectOfRow: i];
159           
160            start = !start;
161        }
162       
163        const CGFloat newY = NSMaxY([self rectOfRow: i-1]);
164        visibleRect.size.height -= newY - visibleRect.origin.y;
165        visibleRect.origin.y = newY;
166    }
167   
168    const NSInteger numberBlankRows = ceil(visibleRect.size.height / totalRowHeight);
169   
170    //remaining visible rows continue alternating
171    visibleRect.size.height = totalRowHeight;
172    if (start)
173        visibleRect.origin.y += totalRowHeight;
174   
175    for (NSInteger i = start ? 1 : 0; i < numberBlankRows; i += 2)
176    {
177        gridRects[rectNum++] = visibleRect;
178        visibleRect.origin.y += 2.0 * totalRowHeight;
179    }
180   
181    NSAssert([[NSColor controlAlternatingRowBackgroundColors] count] >= 2, @"There should be 2 alternating row colors");
182   
183    [[[NSColor controlAlternatingRowBackgroundColors] objectAtIndex: 1] set];
184    NSRectFillList(gridRects, rectNum);
185   
186    [super highlightSelectionInClipRect: clipRect];
187}
188
189@end
Note: See TracBrowser for help on using the repository browser.