Search in sources :

Example 36 with DiskManagerFileInfo

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

the class GlobalManagerFileMerger method syncFileSets.

void syncFileSets() {
    List<DownloadManager> dms = gm.getDownloadManagers();
    synchronized (dm_map) {
        boolean changed = false;
        Set<HashWrapper> existing_dm_hashes = new HashSet<>(dm_map.keySet());
        if (enabled) {
            for (DownloadManager dm : dms) {
                /* 
					 * not sure why we were ignoring shares, one might share a local file in order to help
					 * out a 'normal' download
					 * 
					if ( !dm.isPersistent()){

						continue;
					}
					*/
                DownloadManagerState state = dm.getDownloadState();
                if (state.getFlag(DownloadManagerState.FLAG_LOW_NOISE) || state.getFlag(DownloadManagerState.FLAG_METADATA_DOWNLOAD)) {
                    continue;
                }
                if (enabled_extended || !dm.isDownloadComplete(false)) {
                    TOTorrent torrent = dm.getTorrent();
                    if (torrent != null) {
                        try {
                            HashWrapper hw = torrent.getHashWrapper();
                            if (dm_map.containsKey(hw)) {
                                existing_dm_hashes.remove(hw);
                            } else {
                                dm_map.put(hw, dm);
                                changed = true;
                            }
                        } catch (Throwable e) {
                        }
                    }
                }
            }
        }
        if (existing_dm_hashes.size() > 0) {
            changed = true;
            for (HashWrapper hw : existing_dm_hashes) {
                dm_map.remove(hw);
            }
        }
        if (changed) {
            List<Set<DiskManagerFileInfo>> interesting = new LinkedList<>();
            Map<Long, Set<DiskManagerFileInfo>> size_map = new HashMap<>();
            for (DownloadManager dm : dm_map.values()) {
                TOTorrent torrent = dm.getTorrent();
                if (torrent == null) {
                    continue;
                }
                DiskManagerFileInfo[] files = dm.getDiskManagerFileInfoSet().getFiles();
                for (DiskManagerFileInfo file : files) {
                    if (file.getNbPieces() < MIN_PIECES) {
                        continue;
                    }
                    long len = file.getLength();
                    Set<DiskManagerFileInfo> set = size_map.get(len);
                    if (set == null) {
                        set = new HashSet<>();
                        size_map.put(len, set);
                    }
                    boolean same_dm = false;
                    for (DiskManagerFileInfo existing : set) {
                        if (existing.getDownloadManager() == dm) {
                            same_dm = true;
                            break;
                        }
                    }
                    if (!same_dm) {
                        set.add(file);
                        if (set.size() == 2) {
                            interesting.add(set);
                        }
                    }
                }
            }
            // remove sets consisting of only completed files
            Iterator<Set<DiskManagerFileInfo>> interesting_it = interesting.iterator();
            while (interesting_it.hasNext()) {
                Set<DiskManagerFileInfo> set = interesting_it.next();
                boolean all_done = true;
                for (DiskManagerFileInfo file : set) {
                    if (file.getDownloaded() != file.getLength()) {
                        all_done = false;
                        break;
                    }
                }
                if (all_done) {
                    interesting_it.remove();
                }
            }
            List<SameSizeFiles> sames_copy = new LinkedList<>(sames);
            for (Set<DiskManagerFileInfo> set : interesting) {
                boolean found = false;
                Iterator<SameSizeFiles> sames_it = sames_copy.iterator();
                while (sames_it.hasNext()) {
                    SameSizeFiles same = sames_it.next();
                    if (same.sameAs(set)) {
                        found = true;
                        sames_it.remove();
                        break;
                    }
                }
                if (!found) {
                    sames.add(new SameSizeFiles(set));
                }
            }
            for (SameSizeFiles dead : sames_copy) {
                dead.destroy();
                sames.remove(dead);
            }
            if (sames.size() > 0) {
                if (timer_event == null) {
                    timer_event = SimpleTimer.addPeriodicEvent("GMFM:sync", TIMER_PERIOD, new TimerEventPerformer() {

                        private int tick_count = 0;

                        @Override
                        public void perform(TimerEvent event) {
                            tick_count++;
                            synchronized (dm_map) {
                                for (SameSizeFiles s : sames) {
                                    s.sync(tick_count);
                                }
                            }
                        }
                    });
                }
            } else {
                if (timer_event != null) {
                    timer_event.cancel();
                    timer_event = null;
                }
            }
        }
    }
}
Also used : DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) TOTorrent(com.biglybt.core.torrent.TOTorrent)

Example 37 with DiskManagerFileInfo

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

the class Priority method execute.

@Override
public void execute(String commandName, ConsoleInput console, CommandLine commandLine) {
    String tnumstr, fnumstr, newpriostr;
    int tnumber;
    DiskManagerFileInfo[] files;
    String[] sections;
    List args = commandLine.getArgList();
    LinkedList fs, fe;
    DownloadManager dm;
    if (args.isEmpty()) {
        console.out.println("Torrent # required!");
        return;
    } else {
        tnumstr = (String) args.remove(0);
    }
    if (args.isEmpty()) {
        console.out.println("File # required!");
        return;
    } else {
        fnumstr = (String) args.remove(0);
    }
    if ((console.torrents == null) || console.torrents.isEmpty()) {
        console.out.println("> Command 'prio': No torrents in list (try 'show torrents' first).");
        return;
    }
    try {
        tnumber = Integer.parseInt(tnumstr);
        if ((tnumber == 0) || (tnumber > console.torrents.size())) {
            console.out.println("> Command 'prio': Torrent #" + tnumber + " unknown.");
            return;
        }
        dm = (DownloadManager) console.torrents.get(tnumber - 1);
        files = dm.getDiskManagerFileInfo();
    } catch (Exception e) {
        e.printStackTrace();
        console.out.println("> Command 'prio': Torrent # '" + tnumstr + "' unknown.");
        return;
    }
    if (args.isEmpty()) {
        console.out.println("> Command 'prio': missing parameter for new priority");
        return;
    } else {
        newpriostr = (String) args.remove(0);
    }
    if (newpriostr.equalsIgnoreCase("normal")) {
        newprio = NORMAL;
    } else if (newpriostr.equalsIgnoreCase("high")) {
        newprio = HIGH;
    } else if (newpriostr.equalsIgnoreCase("dnd")) {
        newprio = DONOTDOWNLOAD;
    } else if (newpriostr.equalsIgnoreCase("del")) {
        newprio = DELETE;
    } else {
        console.out.println("> Command 'prio': unknown priority " + newpriostr);
        return;
    }
    if (fnumstr.equalsIgnoreCase("all")) {
        sections = new String[1];
        sections[0] = "1-" + files.length;
    } else
        sections = fnumstr.split(",");
    fs = new LinkedList();
    fe = new LinkedList();
    int dash, start, end;
    for (int i = 0; i < sections.length; i++) {
        try {
            if ((dash = sections[i].indexOf('-')) != -1) {
                start = Integer.parseInt(sections[i].substring(0, dash));
                end = Integer.parseInt(sections[i].substring(dash + 1));
            } else
                start = end = Integer.parseInt(sections[i]);
            if ((start == 0) || (end > files.length)) {
                console.out.println("> Command 'prio': Invalid file range " + sections[i]);
                return;
            }
            if (start > end) {
                console.out.println("> Command 'prio': Invalid file range '" + sections[i] + "'");
            }
            // -1 compensates for 0-based offsets
            fs.add(new Integer(start - 1));
            fe.add(new Integer(end - 1));
        } catch (Exception e) {
            console.out.println("> Command 'prio': File # '" + sections[i] + "' unknown.");
            return;
        }
    }
    // console.out.println("DM was " + dm.getState());
    if ((newprio == DELETE) && (dm.getState() != DownloadManager.STATE_STOPPED)) {
        try {
            dm.stopIt(DownloadManager.STATE_STOPPED, false, false);
        } catch (Exception e) {
            console.out.println("Failed to stop torrent " + tnumber);
            return;
        }
    }
    // console.out.println("DM is " + dm.getState());
    int nummod = 0;
    while (fs.size() > 0) {
        start = ((Integer) fs.removeFirst()).intValue();
        end = ((Integer) fe.removeFirst()).intValue();
        for (int i = start; i <= end; i++) {
            nummod++;
            // console.out.println("Setting priority for file " + i + " to " + newprio);
            if (newprio == NORMAL) {
                files[i].setPriority(0);
                files[i].setSkipped(false);
            } else if (newprio == HIGH) {
                files[i].setPriority(1);
                files[i].setSkipped(false);
            } else if (newprio == DONOTDOWNLOAD) {
                files[i].setPriority(0);
                files[i].setSkipped(true);
            } else if (newprio == DELETE) {
                int st = files[i].getStorageType();
                int target_st = -1;
                if (st == DiskManagerFileInfo.ST_LINEAR) {
                    target_st = DiskManagerFileInfo.ST_COMPACT;
                } else if (st == DiskManagerFileInfo.ST_REORDER) {
                    target_st = DiskManagerFileInfo.ST_REORDER_COMPACT;
                }
                if (target_st != -1 && files[i].setStorageType(target_st)) {
                    files[i].setPriority(0);
                    files[i].setSkipped(true);
                } else {
                    console.out.println("> Command 'prio': Failed to delete file " + (i + 1));
                    nummod--;
                }
            }
        }
    }
    if ((newprio == DELETE) && (dm.getState() == DownloadManager.STATE_STOPPED)) {
        try {
            dm.stopIt(DownloadManager.STATE_QUEUED, false, false);
        } catch (Exception e) {
            console.out.println("Failed to restart torrent " + tnumber);
            return;
        }
    }
    // console.out.println("DM is again " + dm.getState());
    console.out.println(nummod + " file(s) priority set to " + priostr[newprio - 1]);
}
Also used : DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) List(java.util.List) LinkedList(java.util.LinkedList) DownloadManager(com.biglybt.core.download.DownloadManager) LinkedList(java.util.LinkedList)

Example 38 with DiskManagerFileInfo

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

the class ColumnUnopened method cellMouseTrigger.

// @see com.biglybt.pif.ui.tables.TableCellMouseListener#cellMouseTrigger(com.biglybt.pif.ui.tables.TableCellMouseEvent)
@Override
public void cellMouseTrigger(TableCellMouseEvent event) {
    if (event.eventType == TableRowMouseEvent.EVENT_MOUSEUP && event.button == 1) {
        Object ds = event.cell.getDataSource();
        boolean hasBeenOpened;
        if (ds instanceof DownloadManager) {
            DownloadManager dm = (DownloadManager) event.cell.getDataSource();
            boolean complete = dm.getAssumedComplete();
            if (!complete)
                return;
            hasBeenOpened = !PlatformTorrentUtils.getHasBeenOpened(dm);
            PlatformTorrentUtils.setHasBeenOpened(dm, hasBeenOpened);
        } else {
            DiskManagerFileInfo file = (DiskManagerFileInfo) ds;
            boolean complete = file.getLength() == file.getDownloaded();
            if (!complete)
                return;
            DownloadManager dm = file.getDownloadManager();
            int ff = dm.getDownloadState().getFileFlags(file.getIndex());
            ff ^= DownloadManagerState.FILE_FLAG_NOT_NEW;
            hasBeenOpened = (ff & DownloadManagerState.FILE_FLAG_NOT_NEW) != 0;
            dm.getDownloadState().setFileFlags(file.getIndex(), ff);
        }
        event.cell.setGraphic(hasBeenOpened ? graphicUnCheck : graphicCheck);
        event.cell.invalidate();
    }
}
Also used : DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) DownloadManager(com.biglybt.core.download.DownloadManager)

Example 39 with DiskManagerFileInfo

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

the class ColumnProgressETA method cellPaint.

// @see com.biglybt.ui.swt.views.table.TableCellSWTPaintListener#cellPaint(org.eclipse.swt.graphics.GC, com.biglybt.ui.swt.views.table.TableCellSWT)
@Override
public void cellPaint(GC gc, TableCellSWT cell) {
    Object ds = cell.getDataSource();
    if (ds instanceof DiskManagerFileInfo) {
        TableRowCore row = cell.getTableRowCore();
        if (row != null) {
            fileProgress.fillInfoProgressETA(row, gc, (DiskManagerFileInfo) ds, cell.getBounds());
        }
        return;
    }
    if (!(ds instanceof DownloadManager)) {
        return;
    }
    DownloadManager dm = (DownloadManager) ds;
    String tooltip = null;
    if (dm.getState() == DownloadManager.STATE_QUEUED) {
        tooltip = MessageText.getString("ManagerItem.queued.tooltip");
    }
    int percentDone = getPercentDone(ds);
    long eta = showETA ? getETA(cell) : 0;
    // Compute bounds ...
    int newWidth = cell.getWidth();
    if (newWidth <= 0) {
        return;
    }
    int newHeight = cell.getHeight();
    Color fgFirst = gc.getForeground();
    final Color fgOriginal = fgFirst;
    Rectangle cellBounds = cell.getBounds();
    int xStart = cellBounds.x;
    int yStart = cellBounds.y;
    int xRelProgressFillStart = borderWidth;
    int yRelProgressFillStart = borderWidth;
    int xRelProgressFillEnd = newWidth - xRelProgressFillStart - borderWidth;
    int yRelProgressFillEnd = yRelProgressFillStart + 13;
    boolean showSecondLine = yRelProgressFillEnd + 10 < newHeight;
    if (xRelProgressFillEnd < 10) {
        return;
    }
    String sStatusLine = null;
    // Draw Progress bar
    // ImageLoader imageLoader = ImageLoader.getInstance();
    Rectangle boundsImgBG;
    if (!ImageLoader.isRealImage(imgBGTorrent)) {
        boundsImgBG = new Rectangle(0, 0, 0, 13);
    } else {
        boundsImgBG = imgBGTorrent.getBounds();
    }
    if (fontText == null) {
        fontText = FontUtils.getFontWithHeight(gc.getFont(), gc, boundsImgBG.height - 3);
    }
    if (!showSecondLine) {
        yRelProgressFillStart = (cellBounds.height / 2) - ((boundsImgBG.height) / 2);
    }
    yRelProgressFillEnd = yRelProgressFillStart + boundsImgBG.height;
    int progressWidth = newWidth - 1;
    gc.setForeground(cBorder);
    gc.drawRectangle(xStart + xRelProgressFillStart - 1, yStart + yRelProgressFillStart - 1, progressWidth + 1, boundsImgBG.height + 1);
    int pctWidth = (int) (percentDone * (progressWidth) / 1000);
    gc.setBackground(percentDone == 1000 || dm.isDownloadComplete(false) ? cBGcd : cBGdl);
    gc.fillRectangle(xStart + xRelProgressFillStart, yStart + yRelProgressFillStart, pctWidth, boundsImgBG.height);
    if (progressWidth > pctWidth) {
        gc.setBackground(Colors.white);
        gc.fillRectangle(xStart + xRelProgressFillStart + pctWidth, yStart + yRelProgressFillStart, progressWidth - pctWidth, boundsImgBG.height);
    }
    if (boundsImgBG.width > 0) {
        gc.drawImage(imgBGTorrent, 0, 0, boundsImgBG.width, boundsImgBG.height, xStart + xRelProgressFillStart, yStart + yRelProgressFillStart, progressWidth, boundsImgBG.height);
    }
    if (sStatusLine == null) {
        if (dm.isUnauthorisedOnTracker()) {
            sStatusLine = dm.getTrackerStatus();
        // fgFirst = Colors.colorError;	pftt, no colours allowed apparently
        } else {
            if (showETA && eta > 0) {
                String sETA = ViewUtils.formatETA(eta, progress_eta_absolute, cdf.getDateFormat());
                sStatusLine = MessageText.getString("MyTorrents.column.ColumnProgressETA.2ndLine", new String[] { sETA });
            } else {
                sStatusLine = DisplayFormatters.formatDownloadStatus(dm).toUpperCase();
            }
        }
        int cursor_id;
        if (sStatusLine != null && !sStatusLine.contains("http://")) {
            dm.setUserData(CLICK_KEY, null);
            cursor_id = SWT.CURSOR_ARROW;
        } else {
            dm.setUserData(CLICK_KEY, sStatusLine);
            cursor_id = SWT.CURSOR_HAND;
            if (!cell.getTableRow().isSelected()) {
                fgFirst = Colors.blue;
            }
        }
        ((TableCellSWT) cell).setCursorID(cursor_id);
    }
    gc.setTextAntialias(SWT.ON);
    gc.setFont(fontText);
    if (showSecondLine && sStatusLine != null) {
        gc.setForeground(fgFirst);
        boolean fit = GCStringPrinter.printString(gc, sStatusLine, new Rectangle(cellBounds.x, yStart + yRelProgressFillEnd, cellBounds.width, newHeight - yRelProgressFillEnd), true, false, SWT.CENTER);
        if (!fit) {
            if (tooltip == null) {
                tooltip = sStatusLine;
            } else {
                tooltip = sStatusLine + ": " + tooltip;
            }
        }
        gc.setForeground(fgOriginal);
    }
    String sSpeed = "";
    if (showSpeed) {
        long lSpeed = getSpeed(ds);
        if (lSpeed > 0) {
            sSpeed = " (" + DisplayFormatters.formatByteCountToKiBEtcPerSec(lSpeed, true) + ")";
        }
    }
    String sPercent = DisplayFormatters.formatPercentFromThousands(percentDone);
    Rectangle area = new Rectangle(xStart + xRelProgressFillStart + 3, yStart + yRelProgressFillStart, xRelProgressFillEnd - xRelProgressFillStart - 6, yRelProgressFillEnd - yRelProgressFillStart);
    GCStringPrinter sp = new GCStringPrinter(gc, sPercent + sSpeed, area, true, false, SWT.LEFT);
    if (cTextDrop != null) {
        area.x++;
        area.y++;
        gc.setForeground(cTextDrop);
        sp.printString();
        area.x--;
        area.y--;
    }
    gc.setForeground(cText);
    sp.printString();
    Point pctExtent = sp.getCalculatedSize();
    area.width -= (pctExtent.x + 3);
    area.x += (pctExtent.x + 3);
    if (!showSecondLine && sStatusLine != null) {
        boolean fit = GCStringPrinter.printString(gc, sStatusLine, area.intersection(cellBounds), true, false, SWT.RIGHT);
        if (!fit) {
            if (tooltip == null) {
                tooltip = sStatusLine;
            } else {
                tooltip = sStatusLine + ": " + tooltip;
            }
        }
    }
    cell.setToolTip(tooltip);
    gc.setFont(null);
}
Also used : DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) GCStringPrinter(com.biglybt.ui.swt.shells.GCStringPrinter) TableCellSWT(com.biglybt.ui.swt.views.table.TableCellSWT) DownloadManager(com.biglybt.core.download.DownloadManager) EnhancedDownloadManager(com.biglybt.core.download.EnhancedDownloadManager) TableRowCore(com.biglybt.ui.common.table.TableRowCore)

Example 40 with DiskManagerFileInfo

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

the class ColumnProgressETA method cellMouseTrigger.

@Override
public void cellMouseTrigger(TableCellMouseEvent event) {
    Object ds = event.cell.getDataSource();
    if (ds instanceof DiskManagerFileInfo) {
        fileProgress.fileInfoMouseTrigger(event);
        return;
    }
    DownloadManager dm = (DownloadManager) ds;
    if (dm == null) {
        return;
    }
    String clickable = (String) dm.getUserData(CLICK_KEY);
    if (clickable == null) {
        return;
    }
    event.skipCoreFunctionality = true;
    if (event.eventType == TableCellMouseEvent.EVENT_MOUSEUP) {
        String url = UrlUtils.getURL(clickable);
        if (url != null) {
            Utils.launch(url);
        }
    }
}
Also used : DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) DownloadManager(com.biglybt.core.download.DownloadManager) EnhancedDownloadManager(com.biglybt.core.download.EnhancedDownloadManager)

Aggregations

DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)107 DownloadManager (com.biglybt.core.download.DownloadManager)54 File (java.io.File)16 TOTorrent (com.biglybt.core.torrent.TOTorrent)15 PEPeerManager (com.biglybt.core.peer.PEPeerManager)9 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)8 TableRowCore (com.biglybt.ui.common.table.TableRowCore)8 DiskManager (com.biglybt.core.disk.DiskManager)7 IOException (java.io.IOException)7 Point (org.eclipse.swt.graphics.Point)7 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)6 MessageBoxShell (com.biglybt.ui.swt.shells.MessageBoxShell)6 FilesView (com.biglybt.ui.swt.views.FilesView)6 List (java.util.List)6 Image (org.eclipse.swt.graphics.Image)6 DiskManagerPiece (com.biglybt.core.disk.DiskManagerPiece)5 PEPeer (com.biglybt.core.peer.PEPeer)5 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)5 UIFunctions (com.biglybt.ui.UIFunctions)5 ISelectedContent (com.biglybt.ui.selectedcontent.ISelectedContent)5