use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.
the class FileInfoView method getPieceNumber.
private int getPieceNumber(int x, int y) {
Rectangle bounds = fileInfoCanvas.getClientArea();
if (bounds.width <= 0 || bounds.height <= 0) {
return (-1);
}
if (file == null) {
return (-1);
}
DownloadManager download_manager = file.getDownloadManager();
if (download_manager == null) {
return (-1);
}
DiskManager disk_manager = download_manager.getDiskManager();
PEPeerManager peer_manager = download_manager.getPeerManager();
if (disk_manager == null || peer_manager == null) {
return (-1);
}
int first_piece = file.getFirstPieceNumber();
int num_pieces = file.getNbPieces();
int iNumCols = bounds.width / BLOCK_SIZE;
int x_block = x / BLOCK_SIZE;
int y_block = y / BLOCK_SIZE;
int piece_number = y_block * iNumCols + x_block + first_piece;
if (piece_number >= first_piece && piece_number < first_piece + num_pieces) {
return (piece_number);
} else {
return (-1);
}
}
use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.
the class FileInfoView method showPieceDetails.
protected void showPieceDetails(int x, int y) {
int piece_number = getPieceNumber(x, y);
if (piece_number >= 0) {
DownloadManager download_manager = file.getDownloadManager();
if (download_manager == null) {
topLabel.setText("");
return;
}
DiskManager disk_manager = download_manager.getDiskManager();
PEPeerManager peer_manager = download_manager.getPeerManager();
if (disk_manager == null || peer_manager == null) {
topLabel.setText("");
return;
}
DiskManagerPiece[] dm_pieces = disk_manager.getPieces();
PEPiece[] pe_pieces = peer_manager.getPieces();
DiskManagerPiece dm_piece = dm_pieces[piece_number];
PEPiece pe_piece = pe_pieces[piece_number];
String text = "Piece " + piece_number + ": " + dm_piece.getString();
if (pe_piece != null) {
text += ", active: " + pe_piece.getString();
} else {
if (dm_piece.isNeeded() && !dm_piece.isDone()) {
text += ", inactive: " + peer_manager.getPiecePicker().getPieceString(piece_number);
}
}
topLabel.setText(text);
} else {
topLabel.setText("");
}
}
use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.
the class PeerInfoView method refreshInfoCanvas.
/**
* Constructs and image representing the download state of _all_
* the pieces in the torrent. Particularily slow when there's lots of pieces,
* and also wasteful since only a fraction of them ever get painted at
* any given time.
*
* TODO: Construct image for visible area only or something
*/
private void refreshInfoCanvas() {
refreshInfoCanvasQueued = false;
if (peerInfoComposite == null || peerInfoComposite.isDisposed() || !peerInfoComposite.isVisible()) {
return;
}
peerInfoCanvas.layout(true);
Rectangle bounds = peerInfoCanvas.getClientArea();
if (bounds.width <= 0 || bounds.height <= 0)
return;
if (img != null && !img.isDisposed()) {
img.dispose();
img = null;
}
if (peer == null || peer.getPeerState() != PEPeer.TRANSFERING) {
GC gc = new GC(peerInfoCanvas);
gc.fillRectangle(bounds);
gc.dispose();
return;
}
BitFlags peerHavePieces = peer.getAvailable();
if (peerHavePieces == null) {
GC gc = new GC(peerInfoCanvas);
gc.fillRectangle(bounds);
gc.dispose();
return;
}
DiskManagerPiece[] dm_pieces = null;
PEPeerManager pm = peer.getManager();
DiskManager dm = pm.getDiskManager();
dm_pieces = dm.getPieces();
int iNumCols = bounds.width / BLOCK_SIZE;
int iNeededHeight = (((dm.getNbPieces() - 1) / iNumCols) + 1) * BLOCK_SIZE;
if (sc.getMinHeight() != iNeededHeight) {
sc.setMinHeight(iNeededHeight);
sc.layout(true, true);
bounds = peerInfoCanvas.getClientArea();
}
img = new Image(peerInfoCanvas.getDisplay(), bounds.width, iNeededHeight);
GC gcImg = new GC(img);
try {
// use advanced capabilities for faster drawText
gcImg.setAdvanced(true);
gcImg.setBackground(peerInfoCanvas.getBackground());
gcImg.fillRectangle(0, 0, bounds.width, iNeededHeight);
int[] availability = pm.getAvailability();
int iNextDLPieceID = -1;
int iDLPieceID = -1;
int[] ourRequestedPieces = peer.getOutgoingRequestedPieceNumbers();
if (ourRequestedPieces != null) {
if (!peer.isChokingMe()) {
if (ourRequestedPieces.length > 0) {
iDLPieceID = ourRequestedPieces[0];
if (ourRequestedPieces.length > 1)
iNextDLPieceID = ourRequestedPieces[1];
}
} else {
if (ourRequestedPieces.length > 0)
iNextDLPieceID = ourRequestedPieces[0];
}
// if (iNextDLPieceID == -1) {
// iNextDLPieceID = peer.getNextPieceNumberGuess();
// }
}
int[] peerRequestedPieces = peer.getIncomingRequestedPieceNumbers();
if (peerRequestedPieces == null)
peerRequestedPieces = new int[0];
int peerNextRequestedPiece = -1;
if (peerRequestedPieces.length > 0)
peerNextRequestedPiece = peerRequestedPieces[0];
Arrays.sort(peerRequestedPieces);
int iRow = 0;
int iCol = 0;
for (int i = 0; i < peerHavePieces.flags.length; i++) {
int colorIndex;
boolean done = (dm_pieces == null) ? false : dm_pieces[i].isDone();
int iXPos = iCol * BLOCK_SIZE;
int iYPos = iRow * BLOCK_SIZE;
if (done) {
if (peerHavePieces.flags[i])
colorIndex = BLOCKCOLOR_AVAIL_HAVE;
else
colorIndex = BLOCKCOLOR_NOAVAIL_HAVE;
gcImg.setBackground(blockColors[colorIndex]);
gcImg.fillRectangle(iXPos, iYPos, BLOCK_FILLSIZE, BLOCK_FILLSIZE);
} else {
// !done
boolean partiallyDone = (dm_pieces == null) ? false : dm_pieces[i].getNbWritten() > 0;
int x = iXPos;
int width = BLOCK_FILLSIZE;
if (partiallyDone) {
if (peerHavePieces.flags[i])
colorIndex = BLOCKCOLOR_AVAIL_HAVE;
else
colorIndex = BLOCKCOLOR_NOAVAIL_HAVE;
gcImg.setBackground(blockColors[colorIndex]);
// partiallyDone false when dm_pieces null
@SuppressWarnings("null") int iNewWidth = (int) (((float) dm_pieces[i].getNbWritten() / dm_pieces[i].getNbBlocks()) * width);
if (iNewWidth >= width)
iNewWidth = width - 1;
else if (iNewWidth <= 0)
iNewWidth = 1;
gcImg.fillRectangle(x, iYPos, iNewWidth, BLOCK_FILLSIZE);
width -= iNewWidth;
x += iNewWidth;
}
if (peerHavePieces.flags[i])
colorIndex = BLOCKCOLOR_AVAIL_NOHAVE;
else
colorIndex = BLOCKCOLOR_NOAVAIL_NOHAVE;
gcImg.setBackground(blockColors[colorIndex]);
gcImg.fillRectangle(x, iYPos, width, BLOCK_FILLSIZE);
}
// Down Arrow inside box for "dowloading" piece
if (i == iDLPieceID) {
gcImg.setBackground(blockColors[BLOCKCOLOR_TRANSFER]);
gcImg.fillPolygon(new int[] { iXPos, iYPos, iXPos + BLOCK_FILLSIZE, iYPos, iXPos + (BLOCK_FILLSIZE / 2), iYPos + BLOCK_FILLSIZE });
}
// Small Down Arrow inside box for next download piece
if (i == iNextDLPieceID) {
gcImg.setBackground(blockColors[BLOCKCOLOR_NEXT]);
gcImg.fillPolygon(new int[] { iXPos + 2, iYPos + 2, iXPos + BLOCK_FILLSIZE - 1, iYPos + 2, iXPos + (BLOCK_FILLSIZE / 2), iYPos + BLOCK_FILLSIZE - 1 });
}
// Up Arrow in uploading piece
if (i == peerNextRequestedPiece) {
gcImg.setBackground(blockColors[BLOCKCOLOR_TRANSFER]);
gcImg.fillPolygon(new int[] { iXPos, iYPos + BLOCK_FILLSIZE, iXPos + BLOCK_FILLSIZE, iYPos + BLOCK_FILLSIZE, iXPos + (BLOCK_FILLSIZE / 2), iYPos });
} else if (Arrays.binarySearch(peerRequestedPieces, i) >= 0) {
// Small Up Arrow each upload request
gcImg.setBackground(blockColors[BLOCKCOLOR_NEXT]);
gcImg.fillPolygon(new int[] { iXPos + 1, iYPos + BLOCK_FILLSIZE - 2, iXPos + BLOCK_FILLSIZE - 2, iYPos + BLOCK_FILLSIZE - 2, iXPos + (BLOCK_FILLSIZE / 2), iYPos + 2 });
}
if (availability != null && availability[i] < 10) {
gcImg.setFont(font);
String sNumber = String.valueOf(availability[i]);
Point size = gcImg.stringExtent(sNumber);
int x = iXPos + (BLOCK_FILLSIZE / 2) - (size.x / 2);
int y = iYPos + (BLOCK_FILLSIZE / 2) - (size.y / 2);
gcImg.setForeground(blockColors[BLOCKCOLOR_AVAILCOUNT]);
gcImg.drawText(sNumber, x, y, true);
}
iCol++;
if (iCol >= iNumCols) {
iCol = 0;
iRow++;
}
}
} catch (Exception e) {
Logger.log(new LogEvent(LogIDs.GUI, "drawing piece map", e));
} finally {
gcImg.dispose();
}
peerInfoCanvas.redraw();
}
use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.
the class PieceInfoView method createPeerInfoPanel.
private void createPeerInfoPanel(Composite parent) {
GridLayout layout;
GridData gridData;
// Peer Info section contains
// - Peer's Block display
// - Peer's Datarate
pieceInfoComposite = new Composite(parent, SWT.NONE);
layout = new GridLayout();
layout.numColumns = 2;
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
layout.marginHeight = 0;
layout.marginWidth = 0;
pieceInfoComposite.setLayout(layout);
gridData = new GridData(GridData.FILL, GridData.FILL, true, true);
pieceInfoComposite.setLayoutData(gridData);
imageLabel = new Label(pieceInfoComposite, SWT.NULL);
gridData = new GridData();
imageLabel.setLayoutData(gridData);
topLabel = new Label(pieceInfoComposite, SWT.NULL);
topLabel.setBackground(Colors.white);
gridData = new GridData(SWT.FILL, SWT.DEFAULT, false, false);
topLabel.setLayoutData(gridData);
sc = new ScrolledComposite(pieceInfoComposite, SWT.V_SCROLL);
sc.setExpandHorizontal(true);
sc.setExpandVertical(true);
layout = new GridLayout();
layout.horizontalSpacing = 0;
layout.verticalSpacing = 0;
layout.marginHeight = 0;
layout.marginWidth = 0;
sc.setLayout(layout);
gridData = new GridData(GridData.FILL, GridData.FILL, true, true, 2, 1);
sc.setLayoutData(gridData);
sc.getVerticalBar().setIncrement(BLOCK_SIZE);
pieceInfoCanvas = new Canvas(sc, SWT.NO_REDRAW_RESIZE | SWT.NO_BACKGROUND);
gridData = new GridData(GridData.FILL, SWT.DEFAULT, true, false);
pieceInfoCanvas.setLayoutData(gridData);
pieceInfoCanvas.addPaintListener(new PaintListener() {
@Override
public void paintControl(PaintEvent e) {
if (e.width <= 0 || e.height <= 0)
return;
try {
Rectangle bounds = (img == null) ? null : img.getBounds();
if (bounds == null || dlm == null || dlm.getPeerManager() == null) {
e.gc.fillRectangle(e.x, e.y, e.width, e.height);
} else {
if (e.x + e.width > bounds.width)
e.gc.fillRectangle(bounds.width, e.y, e.x + e.width - bounds.width + 1, e.height);
if (e.y + e.height > bounds.height)
e.gc.fillRectangle(e.x, bounds.height, e.width, e.y + e.height - bounds.height + 1);
int width = Math.min(e.width, bounds.width - e.x);
int height = Math.min(e.height, bounds.height - e.y);
e.gc.drawImage(img, e.x, e.y, width, height, e.x, e.y, width, height);
}
} catch (Exception ex) {
}
}
});
pieceInfoCanvas.addListener(SWT.KeyDown, new DoNothingListener());
pieceInfoCanvas.addListener(SWT.Resize, new Listener() {
@Override
public void handleEvent(Event e) {
synchronized (PieceInfoView.this) {
if (alreadyFilling) {
return;
}
alreadyFilling = true;
}
// wrap in asyncexec because sc.setMinWidth (called later) doesn't work
// too well inside a resize (the canvas won't size isn't always updated)
Utils.execSWTThreadLater(0, new AERunnable() {
@Override
public void runSupport() {
if (img != null) {
int iOldColCount = img.getBounds().width / BLOCK_SIZE;
int iNewColCount = pieceInfoCanvas.getClientArea().width / BLOCK_SIZE;
if (iOldColCount != iNewColCount)
refreshInfoCanvas();
}
synchronized (PieceInfoView.this) {
alreadyFilling = false;
}
}
});
}
});
sc.setContent(pieceInfoCanvas);
pieceInfoCanvas.addMouseMoveListener(new MouseMoveListener() {
@Override
public void mouseMove(MouseEvent event) {
int piece_number = getPieceNumber(event.x, event.y);
if (piece_number != selectedPiece) {
selectedPieceShowFilePending = -1;
}
}
});
pieceInfoCanvas.addMouseTrackListener(new MouseTrackAdapter() {
@Override
public void mouseHover(MouseEvent event) {
int piece_number = getPieceNumber(event.x, event.y);
if (piece_number >= 0) {
selectedPiece = piece_number;
selectedPieceShowFilePending = piece_number;
SimpleTimer.addEvent("ShowFile", SystemTime.getOffsetTime(1000), new TimerEventPerformer() {
@Override
public void perform(TimerEvent event) {
Utils.execSWTThread(new Runnable() {
@Override
public void run() {
if (selectedPieceShowFilePending == piece_number) {
selectedPieceShowFile = true;
refreshInfoCanvas();
}
}
});
}
});
refreshInfoCanvas();
DiskManager disk_manager = dlm.getDiskManager();
PEPeerManager pm = dlm.getPeerManager();
DiskManagerPiece dm_piece = disk_manager.getPiece(piece_number);
PEPiece pm_piece = pm.getPiece(piece_number);
String text = "Piece " + piece_number + ": " + dm_piece.getString();
if (pm_piece != null) {
text += ", active: " + pm_piece.getString();
} else {
if (dm_piece.isNeeded() && !dm_piece.isDone()) {
text += ", inactive: " + pm.getPiecePicker().getPieceString(piece_number);
}
}
text += " - ";
DMPieceList l = disk_manager.getPieceList(piece_number);
for (int i = 0; i < l.size(); i++) {
DMPieceMapEntry entry = l.get(i);
DiskManagerFileInfo info = entry.getFile();
text += (i == 0 ? "" : "; ") + info.getFile(true).getName();
}
topLabelRHS = text;
} else {
topLabelRHS = "";
}
updateTopLabel();
}
@Override
public void mouseExit(MouseEvent e) {
selectedPiece = -1;
selectedPieceShowFile = false;
refreshInfoCanvas();
}
});
final Menu menu = new Menu(pieceInfoCanvas.getShell(), SWT.POP_UP);
pieceInfoCanvas.setMenu(menu);
pieceInfoCanvas.addListener(SWT.MenuDetect, new Listener() {
@Override
public void handleEvent(Event event) {
Point pt = pieceInfoCanvas.toControl(event.x, event.y);
int piece_number = getPieceNumber(pt.x, pt.y);
menu.setData("pieceNumber", piece_number);
}
});
MenuBuildUtils.addMaintenanceListenerForMenu(menu, new MenuBuildUtils.MenuBuilder() {
@Override
public void buildMenu(Menu menu, MenuEvent event) {
Integer pn = (Integer) menu.getData("pieceNumber");
if (pn != null && pn != -1) {
DownloadManager download_manager = dlm;
if (download_manager == null) {
return;
}
DiskManager disk_manager = download_manager.getDiskManager();
PEPeerManager peer_manager = download_manager.getPeerManager();
if (disk_manager == null || peer_manager == null) {
return;
}
final PiecePicker picker = peer_manager.getPiecePicker();
DiskManagerPiece[] dm_pieces = disk_manager.getPieces();
PEPiece[] pe_pieces = peer_manager.getPieces();
final int piece_number = pn;
final DiskManagerPiece dm_piece = dm_pieces[piece_number];
final PEPiece pm_piece = pe_pieces[piece_number];
final MenuItem force_piece = new MenuItem(menu, SWT.CHECK);
Messages.setLanguageText(force_piece, "label.force.piece");
boolean done = dm_piece.isDone();
force_piece.setEnabled(!done);
if (!done) {
force_piece.setSelection(picker.isForcePiece(piece_number));
force_piece.addSelectionListener(new SelectionListenerForcePiece(picker, piece_number, force_piece));
}
final MenuItem reset_piece = new MenuItem(menu, SWT.PUSH);
Messages.setLanguageText(reset_piece, "label.reset.piece");
boolean can_reset = dm_piece.isDone() || dm_piece.getNbWritten() > 0;
reset_piece.setEnabled(can_reset);
reset_piece.addSelectionListener(new SelectionListenerResetPiece(dm_piece, pm_piece));
new MenuItem(menu, SWT.SEPARATOR);
final MenuItem seq_asc = new MenuItem(menu, SWT.PUSH);
Messages.setLanguageText(seq_asc, "label.seq.asc.from", new String[] { String.valueOf(piece_number) });
seq_asc.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, false);
picker.setReverseBlockOrder(false);
picker.setSequentialAscendingFrom(piece_number);
}
});
final MenuItem seq_desc = new MenuItem(menu, SWT.PUSH);
Messages.setLanguageText(seq_desc, "label.seq.desc.from", new String[] { String.valueOf(piece_number) });
seq_desc.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, false);
picker.setReverseBlockOrder(true);
picker.setSequentialDescendingFrom(piece_number);
}
});
final MenuItem seq_clear = new MenuItem(menu, SWT.PUSH);
Messages.setLanguageText(seq_clear, "label.seq.clear", new String[] { String.valueOf(piece_number) });
seq_clear.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
download_manager.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, false);
picker.setReverseBlockOrder(false);
picker.clearSequential();
}
});
}
}
});
Legend.createLegendComposite(pieceInfoComposite, blockColors, new String[] { "PiecesView.BlockView.Have", "PiecesView.BlockView.NoHave", "PeersView.BlockView.Transfer", "PeersView.BlockView.NextRequest", "PeersView.BlockView.AvailCount", "PeersView.BlockView.ShowFile" }, new GridData(SWT.FILL, SWT.DEFAULT, true, false, 2, 1));
int iFontPixelsHeight = 10;
int iFontPointHeight = (iFontPixelsHeight * 72) / Utils.getDPIRaw(pieceInfoCanvas.getDisplay()).y;
Font f = pieceInfoCanvas.getFont();
FontData[] fontData = f.getFontData();
fontData[0].setHeight(iFontPointHeight);
font = new Font(pieceInfoCanvas.getDisplay(), fontData);
}
use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.
the class RemainingPiecesItem method refresh.
@Override
public void refresh(TableCell cell) {
DiskManagerFileInfo fileInfo = (DiskManagerFileInfo) cell.getDataSource();
// dm may be null if this is a skeleton file view
DiskManager dm = fileInfo == null ? null : fileInfo.getDiskManager();
int remaining = 0;
if (fileInfo != null && dm != null) {
if (fileInfo instanceof FilesView.FilesViewTreeNode && !((FilesView.FilesViewTreeNode) fileInfo).isLeaf()) {
remaining = -1;
} else {
int start = fileInfo.getFirstPieceNumber();
int end = start + fileInfo.getNbPieces();
DiskManagerPiece[] pieces = dm.getPieces();
for (int i = start; i < end; i++) {
if (!pieces[i].isDone())
remaining++;
}
}
} else {
// unknown
remaining = -1;
}
if (!cell.setSortValue(remaining) && cell.isValid()) {
return;
}
cell.setText("" + (remaining < 0 ? "" : ("" + remaining)));
}
Aggregations