source: trunk/macosx/TorrentTableView.m @ 2810

Last change on this file since 2810 was 2810, checked in by livings124, 14 years ago

allow folders of files to be enabled/disabled from the action button

  • Property svn:keywords set to Date Rev Author Id
File size: 17.6 KB
Line 
1/******************************************************************************
2 * $Id: TorrentTableView.m 2810 2007-08-16 23:05:59Z livings124 $
3 *
4 * Copyright (c) 2005-2007 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 "Controller.h"
28#import "Torrent.h"
29
30#define PADDING 3.0
31
32#define BUTTON_TO_TOP_REGULAR 33.0
33#define BUTTON_TO_TOP_SMALL 20.0
34
35#define ACTION_BUTTON_TO_TOP 47.0
36
37@interface TorrentTableView (Private)
38
39- (NSRect) pauseRectForRow: (int) row;
40- (NSRect) revealRectForRow: (int) row;
41- (NSRect) actionRectForRow: (int) row;
42
43- (BOOL) pointInIconRect: (NSPoint) point;
44- (BOOL) pointInMinimalStatusRect: (NSPoint) point;
45
46- (BOOL) pointInPauseRect: (NSPoint) point;
47- (BOOL) pointInRevealRect: (NSPoint) point;
48- (BOOL) pointInActionRect: (NSPoint) point;
49
50@end
51
52@implementation TorrentTableView
53
54- (id) initWithCoder: (NSCoder *) decoder
55{
56    if ((self = [super initWithCoder: decoder]))
57    {
58        fResumeOnIcon = [NSImage imageNamed: @"ResumeOn.png"];
59        fResumeOffIcon = [NSImage imageNamed: @"ResumeOff.png"];
60        fPauseOnIcon = [NSImage imageNamed: @"PauseOn.png"];
61        fPauseOffIcon = [NSImage imageNamed: @"PauseOff.png"];
62        fResumeNoWaitOnIcon = [NSImage imageNamed: @"ResumeNoWaitOn.png"];
63        fResumeNoWaitOffIcon = [NSImage imageNamed: @"ResumeNoWaitOff.png"];
64       
65        fRevealOnIcon = [NSImage imageNamed: @"RevealOn.png"];
66        fRevealOffIcon = [NSImage imageNamed: @"RevealOff.png"];
67       
68        fActionOnIcon = [NSImage imageNamed: @"ActionOn.png"];
69        fActionOffIcon = [NSImage imageNamed: @"ActionOff.png"];
70       
71        fClickPoint = NSZeroPoint;
72        fClickIn = NO;
73       
74        fKeyStrokes = [[NSMutableArray alloc] init];
75       
76        fSmallStatusAttributes = [[NSDictionary alloc] initWithObjectsAndKeys:
77                                        [NSFont messageFontOfSize: 9.0], NSFontAttributeName, nil];
78       
79        fDefaults = [NSUserDefaults standardUserDefaults];
80       
81        [self setDelegate: self];
82    }
83   
84    return self;
85}
86   
87- (void) awakeFromNib
88{
89    [fContextRow setTitle: @"Context"];
90    [fContextNoRow setTitle: @"Context"];
91}
92
93- (void) dealloc
94{
95    [fKeyStrokes release];
96    [fSmallStatusAttributes release];
97   
98    [fMenuTorrent release];
99   
100    [super dealloc];
101}
102
103- (void) setTorrents: (NSArray *) torrents
104{
105    fTorrents = torrents;
106}
107
108- (void) tableView: (NSTableView *) tableView willDisplayCell: (id) cell
109        forTableColumn: (NSTableColumn *) tableColumn row: (int) row
110{
111    [cell setRepresentedObject: [fTorrents objectAtIndex: row]];
112}
113
114- (void) mouseDown: (NSEvent *) event
115{
116    fClickPoint = [self convertPoint: [event locationInWindow] fromView: nil];
117
118    if ([self pointInActionRect: fClickPoint])
119    {
120        int row = [self rowAtPoint: fClickPoint];
121        [self setNeedsDisplayInRect: [self rectOfRow: row]]; //ensure button is pushed down
122       
123        [self displayTorrentMenuForEvent: event];
124       
125        fClickPoint = NSZeroPoint;
126        [self setNeedsDisplayInRect: [self rectOfRow: row]];
127    }
128    else if ([self pointInPauseRect: fClickPoint] || [self pointInRevealRect: fClickPoint])
129    {
130        fClickIn = YES;
131        [self setNeedsDisplayInRect: [self rectOfRow: [self rowAtPoint: fClickPoint]]];
132    }
133    else
134    {
135        if ([event modifierFlags] & NSAlternateKeyMask)
136        {
137            [fDefaults setBool: ![fDefaults boolForKey: @"UseAdvancedBar"] forKey: @"UseAdvancedBar"];
138            fClickPoint = NSZeroPoint;
139            [self reloadData];
140        }
141        else
142        {
143            if ([self pointInMinimalStatusRect: fClickPoint])
144            {
145                [fDefaults setBool: ![fDefaults boolForKey: @"SmallStatusRegular"] forKey: @"SmallStatusRegular"];
146                fClickPoint = NSZeroPoint;
147                [self reloadData];
148            }
149
150            [super mouseDown: event];
151        }
152    }
153}
154
155- (void) mouseUp: (NSEvent *) event
156{
157    NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
158    int row = [self rowAtPoint: point], oldRow = [self rowAtPoint: fClickPoint];
159    BOOL sameRow = row == oldRow;
160   
161    if (sameRow && [self pointInPauseRect: point] && [self pointInPauseRect: fClickPoint])
162    {
163        Torrent * torrent = [fTorrents objectAtIndex: row];
164
165        if ([torrent isActive])
166            [fController stopTorrents: [NSArray arrayWithObject: torrent]];
167        else if ([torrent isPaused])
168        {
169            if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask)
170                [fController resumeTorrentsNoWait: [NSArray arrayWithObject: torrent]];
171            else if ([torrent waitingToStart])
172                [fController stopTorrents: [NSArray arrayWithObject: torrent]];
173            else
174                [fController resumeTorrents: [NSArray arrayWithObject: torrent]];
175        }
176        else;
177    }
178    else if (sameRow && [self pointInRevealRect: point] && [self pointInRevealRect: fClickPoint])
179        [[fTorrents objectAtIndex: row] revealData];
180    else if ([event clickCount] == 2 && !NSEqualPoints(fClickPoint, NSZeroPoint))
181    {
182        if ([self pointInIconRect: point])
183            [[fTorrents objectAtIndex: row] revealData];
184        else if (![self pointInActionRect: point])
185            [fController showInfo: nil];
186        else;
187    }
188    else;
189   
190    [super mouseUp: event];
191
192    fClickPoint = NSZeroPoint;
193    fClickIn = NO;
194    [self setNeedsDisplayInRect: [self rectOfRow: oldRow]];
195}
196
197- (void) mouseDragged: (NSEvent *) event
198{
199    if (NSEqualPoints(fClickPoint, NSZeroPoint))
200    {
201        [super mouseDragged: event];
202        return;
203    }
204   
205    NSPoint point = [self convertPoint: [event locationInWindow] fromView: nil];
206    int oldRow = [self rowAtPoint: fClickPoint];
207   
208    BOOL inRect;
209    if ([self pointInRevealRect: fClickPoint])
210        inRect = oldRow == [self rowAtPoint: point] && [self pointInRevealRect: point];
211    else if ([self pointInPauseRect: fClickPoint])
212        inRect = oldRow == [self rowAtPoint: point] && [self pointInPauseRect: point];
213    else
214    {
215        [super mouseDragged: event];
216        return;
217    }
218   
219    if (inRect != fClickIn)
220    {
221        fClickIn = inRect;
222        [self setNeedsDisplayInRect: [self rectOfRow: oldRow]];
223    }
224}
225
226- (NSMenu *) menuForEvent: (NSEvent *) event
227{
228    int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
229   
230    if (row >= 0)
231    {
232        if (![self isRowSelected: row])
233            [self selectRowIndexes: [NSIndexSet indexSetWithIndex: row] byExtendingSelection: NO];
234               
235        return fContextRow;
236    }
237    else
238    {
239        [self deselectAll: self];
240        return fContextNoRow;
241    }
242}
243
244- (void) keyDown: (NSEvent *) event
245{
246    unichar newChar = [[event characters] characterAtIndex: 0];
247    if (newChar == ' ' || [[NSCharacterSet alphanumericCharacterSet] characterIsMember: newChar]
248        || [[NSCharacterSet symbolCharacterSet] characterIsMember: newChar]
249        || [[NSCharacterSet punctuationCharacterSet] characterIsMember: newChar])
250    {
251        if ([fKeyStrokes count] > 0 && [event timestamp] - [[fKeyStrokes lastObject] timestamp] > 1.0)
252            [fKeyStrokes removeAllObjects];
253        [fKeyStrokes addObject: event];
254   
255        [self interpretKeyEvents: fKeyStrokes];
256    }
257    else
258    {
259        if ([fKeyStrokes count] > 0)
260            [fKeyStrokes removeAllObjects];
261       
262        [super keyDown: event];
263    }
264}
265
266- (void) flagsChanged: (NSEvent *) event
267{
268    [self display];
269    [super flagsChanged: event];
270}
271
272- (void) insertText: (NSString *) text
273{
274    //sort torrents by name before finding closest match
275    NSSortDescriptor * nameDescriptor = [[[NSSortDescriptor alloc] initWithKey: @"name" ascending: YES
276                                            selector: @selector(caseInsensitiveCompare:)] autorelease];
277    NSArray * descriptors = [[NSArray alloc] initWithObjects: nameDescriptor, nil];
278
279    NSArray * tempTorrents = [fTorrents sortedArrayUsingDescriptors: descriptors];
280    [descriptors release];
281   
282    //select torrent closest to text that isn't before text alphabetically
283    int row;
284    NSEnumerator * enumerator = [tempTorrents objectEnumerator];
285    Torrent * torrent;
286    while ((torrent = [enumerator nextObject]))
287        if ([[torrent name] caseInsensitiveCompare: text] != NSOrderedAscending)
288        {
289            row = [fTorrents indexOfObject: torrent];
290            break;
291        }
292   
293    //select last torrent alphabetically if no match found
294    if (!torrent)
295        row = [fTorrents indexOfObject: [tempTorrents lastObject]];
296   
297    [self selectRow: row byExtendingSelection: NO];
298    [self scrollRowToVisible: row];
299}
300
301- (void) displayTorrentMenuForEvent: (NSEvent *) event
302{
303    int row = [self rowAtPoint: [self convertPoint: [event locationInWindow] fromView: nil]];
304    if (row < 0)
305        return;
306   
307    fMenuTorrent = [[fTorrents objectAtIndex: row] retain];
308    NSMenu * torrentMenu = [fMenuTorrent torrentMenu];
309    [torrentMenu setDelegate: self];
310   
311    NSRect rect = [self actionRectForRow: row];
312    NSPoint location = rect.origin;
313    location.y += rect.size.height + 5.0;
314    location = [self convertPoint: location toView: nil];
315   
316    NSEvent * newEvent = [NSEvent mouseEventWithType: [event type] location: location
317        modifierFlags: [event modifierFlags] timestamp: [event timestamp] windowNumber: [event windowNumber]
318        context: [event context] eventNumber: [event eventNumber] clickCount: [event clickCount] pressure: [event pressure]];
319   
320    [NSMenu popUpContextMenu: torrentMenu withEvent: newEvent forView: self];
321   
322    [fMenuTorrent release];
323    fMenuTorrent = nil;
324}
325
326- (void) menuNeedsUpdate: (NSMenu *) menu
327{
328    //this method seems to be called when it shouldn't be
329    if (!fMenuTorrent)
330        return;
331   
332    BOOL create = [menu numberOfItems] <= 0, folder;
333   
334    NSMenu * supermenu = [menu supermenu];
335    NSArray * items;
336    if (supermenu)
337        items = [[[supermenu itemAtIndex: [supermenu indexOfItemWithSubmenu: menu]] representedObject] objectForKey: @"Children"];
338    else
339        items = [fMenuTorrent fileList];
340    NSEnumerator * enumerator = [items objectEnumerator];
341    NSDictionary * dict;
342    NSMenuItem * item;
343    while ((dict = [enumerator nextObject]))
344    {
345        NSString * name = [dict objectForKey: @"Name"];
346       
347        folder = [[dict objectForKey: @"IsFolder"] boolValue];
348       
349        if (create)
350        {
351            item = [[NSMenuItem alloc] initWithTitle: name action: NULL keyEquivalent: @""];
352           
353            NSImage * icon;
354            if (!folder)
355            {
356                icon = [[dict objectForKey: @"Icon"] copy];
357                [icon setFlipped: NO];
358            }
359            else
360            {
361                NSMenu * itemMenu = [[NSMenu alloc] initWithTitle: name];
362                [itemMenu setAutoenablesItems: NO];
363                [item setSubmenu: itemMenu];
364                [itemMenu setDelegate: self];
365                [itemMenu release];
366               
367                icon = [[[NSWorkspace sharedWorkspace] iconForFileType: NSFileTypeForHFSTypeCode('fldr')] copy];
368            }
369           
370            [item setRepresentedObject: dict];
371           
372            [icon setScalesWhenResized: YES];
373            [icon setSize: NSMakeSize(16.0, 16.0)];
374            [item setImage: icon];
375            [icon release];
376           
377            [item setAction: @selector(checkFile:)];
378           
379            [menu addItem: item];
380            [item release];
381        }
382        else
383            item = [menu itemWithTitle: name];
384       
385        NSIndexSet * indexSet = [dict objectForKey: @"Indexes"];
386        [item setState: [fMenuTorrent checkForFiles: indexSet]];
387        [item setEnabled: [fMenuTorrent canChangeDownloadCheckForFiles: indexSet]];
388    }
389}
390
391- (void) checkFile: (id) sender
392{
393    NSIndexSet * indexes = [[sender representedObject] objectForKey: @"Indexes"];
394    [fMenuTorrent setFileCheckState: [sender state] != NSOnState ? NSOnState : NSOffState forIndexes: indexes];
395   
396    [[NSNotificationCenter defaultCenter] postNotificationName: @"UpdateStats" object: nil];
397}
398
399- (void) drawRow: (int) row clipRect: (NSRect) rect
400{
401    [super drawRow: row clipRect: rect];
402   
403    Torrent * torrent = [fTorrents objectAtIndex: row];
404   
405    //pause/resume icon
406    NSImage * pauseImage = nil;
407    NSRect pauseRect  = [self pauseRectForRow: row];
408    if ([torrent isActive])
409    {
410        if (![torrent isChecking])
411            pauseImage = fClickIn && NSPointInRect(fClickPoint, pauseRect) ? fPauseOnIcon : fPauseOffIcon;
412    }
413    else if ([torrent isPaused])
414    {
415        BOOL inPauseRect = fClickIn && NSPointInRect(fClickPoint, pauseRect);
416        if ([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask && [fDefaults boolForKey: @"Queue"])
417            pauseImage = inPauseRect ? fResumeNoWaitOnIcon : fResumeNoWaitOffIcon;
418        else if ([torrent waitingToStart])
419            pauseImage = inPauseRect ? fPauseOnIcon : fPauseOffIcon;
420        else
421            pauseImage = inPauseRect ? fResumeOnIcon : fResumeOffIcon;
422    }
423    else;
424   
425    if (pauseImage)
426        [pauseImage compositeToPoint: NSMakePoint(pauseRect.origin.x, NSMaxY(pauseRect)) operation: NSCompositeSourceOver];
427   
428    //reveal icon
429    NSRect revealRect = [self revealRectForRow: row];
430    NSImage * revealImage = fClickIn && NSPointInRect(fClickPoint, revealRect) ? fRevealOnIcon : fRevealOffIcon;
431    [revealImage compositeToPoint: NSMakePoint(revealRect.origin.x, NSMaxY(revealRect)) operation: NSCompositeSourceOver];
432   
433    //action icon
434    if (![fDefaults boolForKey: @"SmallView"])
435    {
436        NSRect actionRect = [self actionRectForRow: row];
437        NSImage * actionImage = NSPointInRect(fClickPoint, actionRect) ? fActionOnIcon : fActionOffIcon;
438        [actionImage compositeToPoint: NSMakePoint(actionRect.origin.x, NSMaxY(actionRect)) operation: NSCompositeSourceOver];
439    }
440}
441
442@end
443
444@implementation TorrentTableView (Private)
445
446- (NSRect) pauseRectForRow: (int) row
447{
448    if (row < 0)
449        return NSZeroRect;
450   
451    NSRect cellRect = [self frameOfCellAtColumn: 0 row: row];
452   
453    float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
454   
455    return NSMakeRect(NSMaxX(cellRect) - PADDING - BUTTON_WIDTH - PADDING - BUTTON_WIDTH,
456                        cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
457}
458
459- (NSRect) revealRectForRow: (int) row
460{
461    if (row < 0)
462        return NSZeroRect;
463   
464    NSRect cellRect = [self frameOfCellAtColumn: 0 row: row];
465   
466    float buttonToTop = [fDefaults boolForKey: @"SmallView"] ? BUTTON_TO_TOP_SMALL : BUTTON_TO_TOP_REGULAR;
467   
468    return NSMakeRect(NSMaxX(cellRect) - PADDING - BUTTON_WIDTH,
469                        cellRect.origin.y + buttonToTop, BUTTON_WIDTH, BUTTON_WIDTH);
470}
471
472- (NSRect) actionRectForRow: (int) row
473{
474    if (row < 0)
475        return NSZeroRect;
476   
477    TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell];
478    NSRect cellRect = [self frameOfCellAtColumn: 0 row: row],
479            iconRect = [cell iconRectForBounds: cellRect];
480   
481    if ([fDefaults boolForKey: @"SmallView"])
482        return iconRect;
483    else
484        return NSMakeRect(iconRect.origin.x + (iconRect.size.width - ACTION_BUTTON_WIDTH) * 0.5,
485                        cellRect.origin.y + ACTION_BUTTON_TO_TOP, ACTION_BUTTON_WIDTH, ACTION_BUTTON_HEIGHT);
486}
487
488- (BOOL) pointInIconRect: (NSPoint) point
489{
490    int row = [self rowAtPoint: point];
491    if (row < 0)
492        return NO;
493   
494    TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell];
495    return NSPointInRect(point, [cell iconRectForBounds: [self frameOfCellAtColumn: 0 row: row]]);
496}
497
498- (BOOL) pointInMinimalStatusRect: (NSPoint) point
499{
500    int row = [self rowAtPoint: point];
501    if (row < 0 || ![fDefaults boolForKey: @"SmallView"])
502        return NO;
503   
504    TorrentCell * cell = [[self tableColumnWithIdentifier: @"Torrent"] dataCell];
505    [cell setRepresentedObject: [fTorrents objectAtIndex: row]];
506    return NSPointInRect(point, [cell minimalStatusRectForBounds: [self frameOfCellAtColumn: 0 row: row]]);
507}
508
509- (BOOL) pointInPauseRect: (NSPoint) point
510{
511    int row = [self rowAtPoint: point];
512    if (row < 0)
513        return NO;
514   
515    return NSPointInRect(point, [self pauseRectForRow: row]);
516}
517
518- (BOOL) pointInRevealRect: (NSPoint) point
519{
520    int row = [self rowAtPoint: point];
521    if (row < 0)
522        return NO;
523   
524    return NSPointInRect(point, [self revealRectForRow: row]);
525}
526
527- (BOOL) pointInActionRect: (NSPoint) point
528{
529    int row = [self rowAtPoint: point];
530    if (row < 0)
531        return NO;
532   
533    return NSPointInRect(point, [self actionRectForRow: row]);
534}
535
536@end
Note: See TracBrowser for help on using the repository browser.