Search in sources :

Example 6 with DiskManager

use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.

the class PiecesItem method refresh.

@Override
public void refresh(TableCell cell) {
    /* Notes:
		 * We store our image and imageBufer in DownloadManager using
		 * setData & getData.
		 */
    // Named infoObj so code can be copied easily to the other PiecesItem
    DownloadManager infoObj = (DownloadManager) cell.getDataSource();
    long lCompleted = (infoObj == null) ? 0 : infoObj.getStats().getCompleted();
    boolean bForce = infoObj != null && infoObj.getUserData("PiecesImage") == null;
    if (!cell.setSortValue(lCompleted) && cell.isValid() && !bForce) {
        return;
    }
    if (infoObj == null)
        return;
    // Compute bounds ...
    int newWidth = cell.getWidth();
    if (newWidth <= 0)
        return;
    int newHeight = cell.getHeight();
    int x0 = borderVerticalSize;
    int x1 = newWidth - 1 - borderVerticalSize;
    int y0 = completionHeight + borderHorizontalSize + borderSplit;
    int y1 = newHeight - 1 - borderHorizontalSize;
    int drawWidth = x1 - x0 + 1;
    if (drawWidth < 10 || y1 < 3)
        return;
    boolean bImageBufferValid = true;
    int[] imageBuffer = (int[]) infoObj.getUserData("PiecesImageBuffer");
    if (imageBuffer == null || imageBuffer.length != drawWidth) {
        imageBuffer = new int[drawWidth];
        bImageBufferValid = false;
    }
    Image image = (Image) infoObj.getUserData("PiecesImage");
    GC gcImage;
    boolean bImageChanged;
    Rectangle imageBounds;
    if (image == null || image.isDisposed()) {
        bImageChanged = true;
    } else {
        imageBounds = image.getBounds();
        bImageChanged = imageBounds.width != newWidth || imageBounds.height != newHeight;
    }
    if (bImageChanged) {
        if (image != null && !image.isDisposed()) {
            image.dispose();
        }
        image = new Image(Utils.getDisplay(), newWidth, newHeight);
        imageBounds = image.getBounds();
        bImageBufferValid = false;
        // draw border
        gcImage = new GC(image);
        gcImage.setForeground(Colors.grey);
        if (borderHorizontalSize > 0) {
            if (borderVerticalSize > 0) {
                gcImage.drawRectangle(0, 0, newWidth - 1, newHeight - 1);
            } else {
                gcImage.drawLine(0, 0, newWidth - 1, 0);
                gcImage.drawLine(0, newHeight - 1, newWidth - 1, newHeight - 1);
            }
        } else if (borderVerticalSize > 0) {
            gcImage.drawLine(0, 0, 0, newHeight - 1);
            gcImage.drawLine(newWidth - 1, 0, newWidth - 1, newHeight - 1);
        }
        if (borderSplit > 0) {
            gcImage.setForeground(Colors.white);
            gcImage.drawLine(x0, completionHeight + borderHorizontalSize, x1, completionHeight + borderHorizontalSize);
        }
    } else {
        gcImage = new GC(image);
    }
    DiskManager disk_manager = infoObj.getDiskManager();
    DiskManagerPiece[] pieces = disk_manager == null ? null : disk_manager.getPieces();
    int nbPieces = infoObj.getNbPieces();
    try {
        int nbComplete = 0;
        int a0;
        int a1 = 0;
        for (int i = 0; i < drawWidth; i++) {
            if (i == 0) {
                // always start out with one piece
                a0 = 0;
                a1 = nbPieces / drawWidth;
                if (a1 == 0)
                    a1 = 1;
            } else {
                // the last iteration, a1 will be nbPieces
                a0 = a1;
                a1 = ((i + 1) * nbPieces) / (drawWidth);
            }
            int index;
            if (a1 <= a0) {
                index = imageBuffer[i - 1];
            } else {
                int nbAvailable = 0;
                for (int j = a0; j < a1; j++) if (pieces != null && pieces[j].isDone())
                    nbAvailable++;
                nbComplete += nbAvailable;
                index = (nbAvailable * Colors.BLUES_DARKEST) / (a1 - a0);
            // System.out.println("i="+i+";nbAvailable="+nbAvailable+";nbComplete="+nbComplete+";nbPieces="+nbPieces+";a0="+a0+";a1="+a1);
            }
            if (!bImageBufferValid || imageBuffer[i] != index) {
                imageBuffer[i] = index;
                bImageChanged = true;
                gcImage.setForeground(index == INDEX_COLOR_NONEAVAIL ? Colors.red : Colors.blues[index]);
                gcImage.drawLine(i + x0, y0, i + x0, y1);
            }
        }
        // pieces can sometimes be 0 due to timing or bad torrent (well, there's a bug with a /0 error
        // so it can happen somehow :)
        int limit = nbPieces == 0 ? 0 : ((drawWidth * nbComplete) / nbPieces);
        if (limit < drawWidth) {
            gcImage.setBackground(Colors.blues[Colors.BLUES_LIGHTEST]);
            gcImage.fillRectangle(limit + x0, borderHorizontalSize, x1 - limit, completionHeight);
        }
        gcImage.setBackground(Colors.colorProgressBar);
        gcImage.fillRectangle(x0, borderHorizontalSize, limit, completionHeight);
    } catch (Exception e) {
        System.out.println("Error Drawing PiecesItem");
        Debug.printStackTrace(e);
    }
    gcImage.dispose();
    Image oldImage = null;
    Graphic graphic = cell.getGraphic();
    if (graphic instanceof UISWTGraphic) {
        oldImage = ((UISWTGraphic) graphic).getImage();
    }
    if (bImageChanged || image != oldImage || !cell.isValid()) {
        if (cell instanceof TableCellSWT) {
            ((TableCellSWT) cell).setGraphic(image);
        } else {
            cell.setGraphic(new UISWTGraphicImpl(image));
        }
        if (bImageChanged) {
            cell.invalidate();
        }
        infoObj.setUserData("PiecesImage", image);
        infoObj.setUserData("PiecesImageBuffer", imageBuffer);
    }
}
Also used : TableCellSWT(com.biglybt.ui.swt.views.table.TableCellSWT) Graphic(com.biglybt.pif.ui.Graphic) UISWTGraphic(com.biglybt.ui.swt.pif.UISWTGraphic) Rectangle(org.eclipse.swt.graphics.Rectangle) Image(org.eclipse.swt.graphics.Image) DiskManager(com.biglybt.core.disk.DiskManager) DownloadManager(com.biglybt.core.download.DownloadManager) UISWTGraphicImpl(com.biglybt.ui.swt.pifimpl.UISWTGraphicImpl) UISWTGraphic(com.biglybt.ui.swt.pif.UISWTGraphic) DiskManagerPiece(com.biglybt.core.disk.DiskManagerPiece) GC(org.eclipse.swt.graphics.GC)

Example 7 with DiskManager

use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.

the class DownloadManagerImpl method moveDataFilesSupport0.

void moveDataFilesSupport0(File new_parent_dir, String new_filename) throws DownloadManagerException {
    if (!canMoveDataFiles()) {
        throw new DownloadManagerException("canMoveDataFiles is false!");
    }
    if (new_filename != null) {
        new_filename = FileUtil.convertOSSpecificChars(new_filename, false);
    }
    // old file will be a "file" for simple torrents, a dir for non-simple
    File old_file = getSaveLocation();
    try {
        old_file = old_file.getCanonicalFile();
        if (new_parent_dir != null) {
            new_parent_dir = new_parent_dir.getCanonicalFile();
        }
    } catch (Throwable e) {
        Debug.printStackTrace(e);
        throw (new DownloadManagerException("Failed to get canonical paths", e));
    }
    final File current_save_location = old_file;
    File new_save_location = new File((new_parent_dir == null) ? old_file.getParentFile() : new_parent_dir, (new_filename == null) ? old_file.getName() : new_filename);
    if (current_save_location.equals(new_save_location)) {
        // null operation
        return;
    }
    DiskManager dm = getDiskManager();
    if (dm == null || dm.getFiles() == null) {
        if (!old_file.exists()) {
            // files not created yet
            FileUtil.mkdirs(new_save_location.getParentFile());
            setTorrentSaveDir(new_save_location.getParent().toString(), new_save_location.getName());
            return;
        }
        try {
            new_save_location = new_save_location.getCanonicalFile();
        } catch (Throwable e) {
            Debug.printStackTrace(e);
        }
        FileUtil.ProgressListener pl = new FileUtil.ProgressListener() {

            private long total_size;

            private long total_done;

            public void setTotalSize(long size) {
                total_size = size;
            }

            public void bytesDone(long num) {
                total_done += num;
                move_progress = (int) (Math.min(1000, (1000 * total_done) / total_size));
            }

            public void complete() {
                move_progress = 1000;
            }
        };
        try {
            move_progress = 0;
            if (old_file.equals(new_save_location)) {
            // nothing to do
            } else if (torrent.isSimpleTorrent()) {
                pl.setTotalSize(controller.getDiskManagerFileInfo()[0].getFile(true).length());
                if (controller.getDiskManagerFileInfo()[0].setLinkAtomic(new_save_location, pl)) {
                    setTorrentSaveDir(new_save_location.getParentFile().toString(), new_save_location.getName());
                } else {
                    throw new DownloadManagerException("rename operation failed");
                }
            /*
				  // Have to keep the file name in sync if we're renaming.
				  //if (controller.getDiskManagerFileInfo()[0].setLinkAtomic(new_save_location)) {
				  if ( FileUtil.renameFile( old_file, new_save_location )){
	
					  setTorrentSaveDir( new_save_location.getParentFile().toString(), new_save_location.getName());
	
				  }else{
	
					  throw( new DownloadManagerException( "rename operation failed" ));
				  }
				  //} else {throw new DownloadManagerException( "rename operation failed");}
				   */
            } else {
                if (FileUtil.isAncestorOf(old_file, new_save_location)) {
                    Logger.logTextResource(new LogAlert(this, LogAlert.REPEATABLE, LogAlert.AT_ERROR, "DiskManager.alert.movefilefails"), new String[] { old_file.toString(), "Target is sub-directory of files" });
                    throw (new DownloadManagerException("rename operation failed"));
                }
                long total_size = 0;
                // The files we move must be limited to those mentioned in the torrent.
                final HashSet files_to_move = new HashSet();
                // Required for the adding of parent directories logic.
                files_to_move.add(null);
                DiskManagerFileInfo[] info_files = controller.getDiskManagerFileInfo();
                for (int i = 0; i < info_files.length; i++) {
                    File f = info_files[i].getFile(true);
                    total_size += f.length();
                    try {
                        f = f.getCanonicalFile();
                    } catch (IOException ioe) {
                        f = f.getAbsoluteFile();
                    }
                    boolean added_entry = files_to_move.add(f);
                    /**
                     * Start adding all the parent directories to the
                     * files_to_move list. Doesn't matter if we include
                     * files which are outside of the file path, the
                     * renameFile call won't try to move those directories
                     * anyway.
                     */
                    while (added_entry) {
                        f = f.getParentFile();
                        added_entry = files_to_move.add(f);
                    }
                }
                FileFilter ff = new FileFilter() {

                    @Override
                    public boolean accept(File f) {
                        return files_to_move.contains(f);
                    }
                };
                pl.setTotalSize(total_size);
                if (FileUtil.renameFile(old_file, new_save_location, false, ff, pl)) {
                    setTorrentSaveDir(new_save_location.getParentFile().toString(), new_save_location.getName());
                } else {
                    throw (new DownloadManagerException("rename operation failed"));
                }
                if (current_save_location.isDirectory()) {
                    TorrentUtils.recursiveEmptyDirDelete(current_save_location, false);
                }
            }
        } finally {
            pl.complete();
            move_progress = -1;
        }
    } else {
        dm.moveDataFiles(new_save_location.getParentFile(), new_save_location.getName(), null);
    }
}
Also used : IOException(java.io.IOException) DiskManager(com.biglybt.core.disk.DiskManager) FileFilter(java.io.FileFilter) File(java.io.File)

Example 8 with DiskManager

use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.

the class DownloadManagerStatsImpl method getDownloadCompleted.

@Override
public int getDownloadCompleted(boolean bLive) {
    if (bLive) {
        DiskManager dm = download_manager.getDiskManager();
        if (dm != null) {
            int state = dm.getState();
            boolean transient_state = state == DiskManager.INITIALIZING || state == DiskManager.ALLOCATING || state == DiskManager.CHECKING;
            long total = dm.getTotalLength();
            long completed_download_bytes = total - dm.getRemaining();
            int computed_completion = (total == 0) ? 0 : (int) ((1000 * completed_download_bytes) / total);
            // use non-transient values to update the record of download completion
            if (!transient_state) {
                saved_completed_download_bytes = completed_download_bytes;
            }
            // return the transient completion level
            return computed_completion;
        }
    }
    long total = download_manager.getSize();
    int computed_completion = total == 0 ? 0 : (int) (1000 * getDownloadCompletedBytes() / total);
    return computed_completion;
}
Also used : DiskManager(com.biglybt.core.disk.DiskManager)

Example 9 with DiskManager

use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.

the class DownloadManagerStatsImpl method getRemainingExcludingDND.

@Override
public long getRemainingExcludingDND() {
    DiskManager dm = download_manager.getDiskManager();
    if (dm != null) {
        return dm.getRemainingExcludingDND();
    }
    long remaining = getRemaining();
    long rem = (remaining - (getSkippedFileSetSize() - saved_skipped_but_downloaded));
    if (rem < 0) {
        rem = 0;
    }
    return (rem);
}
Also used : DiskManager(com.biglybt.core.disk.DiskManager)

Example 10 with DiskManager

use of com.biglybt.core.disk.DiskManager in project BiglyBT by BiglySoftware.

the class DownloadManagerStatsImpl method getDiskManagerIfNotTransient.

private DiskManager getDiskManagerIfNotTransient() {
    DiskManager dm = download_manager.getDiskManager();
    if (dm == null) {
        return null;
    }
    int state = dm.getState();
    boolean transient_state = state == DiskManager.INITIALIZING || state == DiskManager.ALLOCATING || state == DiskManager.CHECKING;
    return transient_state ? null : dm;
}
Also used : DiskManager(com.biglybt.core.disk.DiskManager)

Aggregations

DiskManager (com.biglybt.core.disk.DiskManager)31 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)13 PEPeerManager (com.biglybt.core.peer.PEPeerManager)13 DownloadManager (com.biglybt.core.download.DownloadManager)12 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)7 PEPiece (com.biglybt.core.peer.PEPiece)6 LogEvent (com.biglybt.core.logging.LogEvent)5 PiecePicker (com.biglybt.core.peermanager.piecepicker.PiecePicker)4 TOTorrent (com.biglybt.core.torrent.TOTorrent)4 IOException (java.io.IOException)4 PEPeer (com.biglybt.core.peer.PEPeer)3 File (java.io.File)3 DMPieceList (com.biglybt.core.disk.impl.piecemapper.DMPieceList)2 DMPieceMapEntry (com.biglybt.core.disk.impl.piecemapper.DMPieceMapEntry)2 DownloadManagerStats (com.biglybt.core.download.DownloadManagerStats)2 TOTorrentFile (com.biglybt.core.torrent.TOTorrentFile)2 AERunnable (com.biglybt.core.util.AERunnable)2 Download (com.biglybt.pif.download.Download)2 MenuBuildUtils (com.biglybt.ui.swt.MenuBuildUtils)2 UISWTViewEvent (com.biglybt.ui.swt.pif.UISWTViewEvent)2