2022-01-20 18:27:56 +00:00
|
|
|
// This file Copyright © 2006-2022 Transmission authors and contributors.
|
|
|
|
// It may be used under the MIT (SPDX: MIT) license.
|
|
|
|
// License text can be found in the licenses/ folder.
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2018-09-30 10:37:30 +00:00
|
|
|
#include <libtransmission/transmission.h>
|
|
|
|
#include <libtransmission/utils.h>
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "PiecesView.h"
|
2008-11-01 15:22:32 +00:00
|
|
|
#import "Torrent.h"
|
2007-09-16 01:02:06 +00:00
|
|
|
#import "InfoWindowController.h"
|
2010-04-23 16:59:14 +00:00
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
#define MAX_ACROSS 18
|
2009-09-18 02:18:22 +00:00
|
|
|
#define BETWEEN 1.0
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2008-06-10 22:54:30 +00:00
|
|
|
#define HIGH_PEERS 30
|
2008-06-10 19:56:53 +00:00
|
|
|
|
2009-10-18 02:12:43 +00:00
|
|
|
enum
|
|
|
|
{
|
|
|
|
PIECE_NONE,
|
|
|
|
PIECE_SOME,
|
|
|
|
PIECE_HIGH_PEERS,
|
|
|
|
PIECE_FINISHED,
|
|
|
|
PIECE_FLASHING
|
|
|
|
};
|
2008-06-10 19:56:53 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
@interface PiecesView ()
|
|
|
|
|
|
|
|
@property(nonatomic) int8_t* fPieces;
|
|
|
|
|
|
|
|
@property(nonatomic) NSColor* fGreenAvailabilityColor;
|
|
|
|
@property(nonatomic) NSColor* fBluePieceColor;
|
|
|
|
|
|
|
|
@property(nonatomic) NSInteger fNumPieces;
|
|
|
|
@property(nonatomic) NSInteger fAcross;
|
|
|
|
@property(nonatomic) NSInteger fWidth;
|
|
|
|
@property(nonatomic) NSInteger fExtraBorder;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2007-09-16 01:02:06 +00:00
|
|
|
@implementation PiecesView
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)awakeFromNib
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2008-06-17 17:17:15 +00:00
|
|
|
//store box colors
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fGreenAvailabilityColor = [NSColor colorWithCalibratedRed:0.0 green:1.0 blue:0.4 alpha:1.0];
|
|
|
|
self.fBluePieceColor = [NSColor colorWithCalibratedRed:0.0 green:0.4 blue:0.8 alpha:1.0];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-06-17 17:17:15 +00:00
|
|
|
//actually draw the box
|
2022-02-22 16:04:20 +00:00
|
|
|
self.torrent = nil;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)dealloc
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
tr_free(_fPieces);
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)setTorrent:(Torrent*)torrent
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2008-06-09 23:23:55 +00:00
|
|
|
[self clearView];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
_torrent = (torrent && !torrent.magnet) ? torrent : nil;
|
|
|
|
if (_torrent)
|
2011-03-05 03:19:26 +00:00
|
|
|
{
|
|
|
|
//determine relevant values
|
2022-02-22 16:04:20 +00:00
|
|
|
_fNumPieces = MIN(_torrent.pieceCount, MAX_ACROSS * MAX_ACROSS);
|
|
|
|
_fAcross = ceil(sqrt(_fNumPieces));
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
CGFloat const width = self.bounds.size.width;
|
2022-02-22 16:04:20 +00:00
|
|
|
_fWidth = (width - (_fAcross + 1) * BETWEEN) / _fAcross;
|
|
|
|
_fExtraBorder = (width - ((_fWidth + BETWEEN) * _fAcross + BETWEEN)) / 2;
|
2011-03-05 03:19:26 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* back = [[NSImage alloc] initWithSize:self.bounds.size];
|
2009-10-18 01:54:13 +00:00
|
|
|
[back lockFocus];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSGradient* gradient = [[NSGradient alloc] initWithStartingColor:[NSColor colorWithCalibratedWhite:0.0 alpha:0.4]
|
|
|
|
endingColor:[NSColor colorWithCalibratedWhite:0.2 alpha:0.4]];
|
|
|
|
[gradient drawInRect:self.bounds angle:90.0];
|
2009-10-18 01:54:13 +00:00
|
|
|
[back unlockFocus];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
self.image = back;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-06-09 23:23:55 +00:00
|
|
|
[self setNeedsDisplay];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)clearView
|
2008-06-09 21:20:01 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
tr_free(self.fPieces);
|
|
|
|
self.fPieces = NULL;
|
2008-06-09 21:20:01 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)updateView
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!self.torrent)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2007-09-16 01:02:06 +00:00
|
|
|
return;
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-06-09 21:20:01 +00:00
|
|
|
//determine if first time
|
2022-02-22 16:04:20 +00:00
|
|
|
BOOL const first = self.fPieces == NULL;
|
2009-09-18 02:18:22 +00:00
|
|
|
if (first)
|
2021-08-15 09:41:48 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fPieces = (int8_t*)tr_malloc(self.fNumPieces * sizeof(int8_t));
|
2021-08-15 09:41:48 +00:00
|
|
|
}
|
2007-09-16 01:02:06 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
int8_t* pieces = NULL;
|
|
|
|
float* piecesPercent = NULL;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
BOOL const showAvailability = [NSUserDefaults.standardUserDefaults boolForKey:@"PiecesViewShowAvailability"];
|
|
|
|
if (showAvailability)
|
2017-01-24 17:53:16 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
pieces = (int8_t*)tr_malloc(self.fNumPieces * sizeof(int8_t));
|
|
|
|
[self.torrent getAvailability:pieces size:self.fNumPieces];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
else
|
2017-01-24 17:53:16 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
piecesPercent = (float*)tr_malloc(self.fNumPieces * sizeof(float));
|
|
|
|
[self.torrent getAmountFinished:piecesPercent size:self.fNumPieces];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
NSImage* image = self.image;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
NSRect fillRects[self.fNumPieces];
|
|
|
|
NSColor* fillColors[self.fNumPieces];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-10-18 02:12:43 +00:00
|
|
|
NSInteger usedCount = 0;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-02-22 16:04:20 +00:00
|
|
|
for (NSInteger index = 0; index < self.fNumPieces; index++)
|
2009-10-18 02:23:10 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
NSColor* pieceColor = nil;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2022-04-21 14:28:38 +00:00
|
|
|
if (showAvailability ? pieces[index] == -1 : piecesPercent[index] == 1.0)
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (first || self.fPieces[index] != PIECE_FINISHED)
|
2008-06-09 22:45:31 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (!first && self.fPieces[index] != PIECE_FLASHING)
|
2008-06-10 20:15:00 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
pieceColor = NSColor.orangeColor;
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fPieces[index] = PIECE_FLASHING;
|
2008-06-10 21:13:33 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
else
|
2008-06-10 21:13:33 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
pieceColor = self.fBluePieceColor;
|
|
|
|
self.fPieces[index] = PIECE_FINISHED;
|
2008-06-10 20:15:00 +00:00
|
|
|
}
|
2008-06-10 21:33:13 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
2022-04-21 14:28:38 +00:00
|
|
|
else if (showAvailability ? pieces[index] == 0 : piecesPercent[index] == 0.0)
|
2009-10-18 02:23:10 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (first || self.fPieces[index] != PIECE_NONE)
|
2008-06-10 21:33:13 +00:00
|
|
|
{
|
2021-08-07 07:27:56 +00:00
|
|
|
pieceColor = NSColor.whiteColor;
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fPieces[index] = PIECE_NONE;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
2022-04-21 14:28:38 +00:00
|
|
|
else if (showAvailability && pieces[index] >= HIGH_PEERS)
|
2009-10-18 02:23:10 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (first || self.fPieces[index] != PIECE_HIGH_PEERS)
|
2008-06-09 22:45:31 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
pieceColor = self.fGreenAvailabilityColor;
|
|
|
|
self.fPieces[index] = PIECE_HIGH_PEERS;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
//always redraw "mixed"
|
2022-04-21 14:28:38 +00:00
|
|
|
CGFloat percent = showAvailability ? (CGFloat)pieces[index] / HIGH_PEERS : piecesPercent[index];
|
|
|
|
NSColor* fullColor = showAvailability ? self.fGreenAvailabilityColor : self.fBluePieceColor;
|
2021-08-15 09:41:48 +00:00
|
|
|
pieceColor = [NSColor.whiteColor blendedColorWithFraction:percent ofColor:fullColor];
|
2022-02-22 16:04:20 +00:00
|
|
|
self.fPieces[index] = PIECE_SOME;
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-10-18 02:23:10 +00:00
|
|
|
if (pieceColor)
|
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
NSInteger const across = index % self.fAcross;
|
|
|
|
NSInteger const down = index / self.fAcross;
|
2021-08-15 09:41:48 +00:00
|
|
|
fillRects[usedCount] = NSMakeRect(
|
2022-02-22 16:04:20 +00:00
|
|
|
across * (self.fWidth + BETWEEN) + BETWEEN + self.fExtraBorder,
|
|
|
|
image.size.width - (down + 1) * (self.fWidth + BETWEEN) - self.fExtraBorder,
|
|
|
|
self.fWidth,
|
|
|
|
self.fWidth);
|
2009-10-18 02:23:10 +00:00
|
|
|
fillColors[usedCount] = pieceColor;
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-10-18 02:23:10 +00:00
|
|
|
usedCount++;
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
2009-10-18 02:23:10 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2009-09-18 02:18:22 +00:00
|
|
|
if (usedCount > 0)
|
2008-06-10 21:46:34 +00:00
|
|
|
{
|
2009-09-17 23:33:50 +00:00
|
|
|
[image lockFocus];
|
2009-09-18 02:18:22 +00:00
|
|
|
NSRectFillListWithColors(fillRects, fillColors, usedCount);
|
2008-06-10 21:46:34 +00:00
|
|
|
[image unlockFocus];
|
|
|
|
[self setNeedsDisplay];
|
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2008-06-09 21:20:01 +00:00
|
|
|
tr_free(pieces);
|
|
|
|
tr_free(piecesPercent);
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (BOOL)acceptsFirstMouse:(NSEvent*)event
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
- (void)mouseDown:(NSEvent*)event
|
2007-09-16 01:02:06 +00:00
|
|
|
{
|
2022-02-22 16:04:20 +00:00
|
|
|
if (self.torrent)
|
2010-04-06 01:44:38 +00:00
|
|
|
{
|
2021-08-15 09:41:48 +00:00
|
|
|
BOOL const availability = ![NSUserDefaults.standardUserDefaults boolForKey:@"PiecesViewShowAvailability"];
|
|
|
|
[NSUserDefaults.standardUserDefaults setBool:availability forKey:@"PiecesViewShowAvailability"];
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-07 07:27:56 +00:00
|
|
|
[self sendAction:self.action to:self.target];
|
2010-04-06 01:44:38 +00:00
|
|
|
}
|
2017-01-24 17:53:16 +00:00
|
|
|
|
2021-08-15 09:41:48 +00:00
|
|
|
[super mouseDown:event];
|
2007-09-16 01:02:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|