Search in sources :

Example 1 with DownloadManagerListener

use of com.biglybt.core.download.DownloadManagerListener in project BiglyBT by BiglySoftware.

the class FilesViewMenuUtil method recheckFiles.

public static void recheckFiles(List<DiskManagerFileInfo> file_list) {
    if (file_list == null || file_list.size() == 0) {
        return;
    }
    Map<DownloadManager, ArrayList<DiskManagerFileInfo>> mapDMtoDMFI = new IdentityHashMap<>();
    for (DiskManagerFileInfo file : file_list) {
        DownloadManager dm = file.getDownloadManager();
        ArrayList<DiskManagerFileInfo> listFileInfos = mapDMtoDMFI.get(dm);
        if (listFileInfos == null) {
            listFileInfos = new ArrayList<>(1);
            mapDMtoDMFI.put(dm, listFileInfos);
        }
        listFileInfos.add(file);
    }
    for (DownloadManager dm : mapDMtoDMFI.keySet()) {
        ArrayList<DiskManagerFileInfo> list = mapDMtoDMFI.get(dm);
        DiskManagerFileInfo[] fileInfos = list.toArray(new DiskManagerFileInfo[0]);
        boolean was_force = dm.isForceStart();
        boolean was_paused = dm.isPaused();
        int state = dm.getState();
        boolean was_stopped = (state == DownloadManager.STATE_STOPPED || state == DownloadManager.STATE_STOPPING || state == DownloadManager.STATE_ERROR);
        if (!was_stopped) {
            dm.pause(true);
        }
        for (DiskManagerFileInfo file : fileInfos) {
            file.recheck();
        }
        // to actually do a file-level recheck the download need to be started after requesting the
        // recheck(s)
        DownloadManagerListener l = new DownloadManagerAdapter() {

            boolean is_ready;

            @Override
            public void stateChanged(DownloadManager manager, int state) {
                if (state == DownloadManager.STATE_ERROR) {
                    if (!was_force) {
                        dm.setForceStart(false);
                    }
                    dm.removeListener(this);
                } else if (state == DownloadManager.STATE_READY) {
                    is_ready = true;
                } else if (is_ready && (state == DownloadManager.STATE_DOWNLOADING || state == DownloadManager.STATE_SEEDING || state == DownloadManager.STATE_QUEUED)) {
                    dm.removeListener(this);
                    if (!was_force) {
                        dm.setForceStart(false);
                    }
                    if (was_paused) {
                        ManagerUtils.asyncPause(dm);
                    } else if (was_stopped) {
                        ManagerUtils.asyncStop(dm, DownloadManager.STATE_STOPPED);
                    }
                } else if (is_ready && state == DownloadManager.STATE_STOPPED) {
                    // shouldn't get here but just in case
                    dm.removeListener(this);
                }
            }
        };
        dm.addListener(l);
        if (was_paused) {
            dm.resume();
        } else {
            dm.stopIt(DownloadManager.STATE_QUEUED, false, false);
        }
        // gotta get it running
        dm.setForceStart(true);
    }
}
Also used : DownloadManagerListener(com.biglybt.core.download.DownloadManagerListener) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) IdentityHashMap(java.util.IdentityHashMap) ArrayList(java.util.ArrayList) DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerAdapter(com.biglybt.core.download.impl.DownloadManagerAdapter)

Example 2 with DownloadManagerListener

use of com.biglybt.core.download.DownloadManagerListener in project BiglyBT by BiglySoftware.

the class ToolBarView method _refreshCoreToolBarItems.

public void _refreshCoreToolBarItems() {
    if (DEBUG && !isVisible()) {
        Debug.out("Trying to refresh core toolbar items when toolbar is not visible " + this + getMainSkinObject());
    }
    UIFunctionsSWT uiFunctionsSWT = UIFunctionsManagerSWT.getUIFunctionsSWT();
    MultipleDocumentInterfaceSWT mdi = uiFunctionsSWT != null ? uiFunctionsSWT.getMDISWT() : null;
    if (mdi != null) {
        UIToolBarItem[] allToolBarItems = tbm.getAllToolBarItems();
        MdiEntrySWT entry = mdi.getCurrentEntry();
        Map<String, Long> mapStates = new HashMap<>();
        if (entry != null) {
            UIToolBarEnablerBase[] enablers = entry.getToolbarEnablers();
            for (UIToolBarEnablerBase enabler : enablers) {
                if (enabler instanceof UIPluginViewToolBarListener) {
                    try {
                        ((UIPluginViewToolBarListener) enabler).refreshToolBarItems(mapStates);
                    } catch (Throwable e) {
                        // don't trust them plugins
                        Debug.out(e);
                    }
                }
            }
        }
        ISelectedContent[] currentContent = SelectedContentManager.getCurrentlySelectedContent();
        // System.out.println("_refreshCoreToolBarItems(" + currentContent.length + ", " + entry + " via " + Debug.getCompressedStackTrace());
        boolean allFiles = currentContent.length > 0;
        synchronized (dm_listener_map) {
            Map<DownloadManager, DownloadManagerListener> copy = new IdentityHashMap<>(dm_listener_map);
            for (ISelectedContent content : currentContent) {
                DownloadManager dm = content.getDownloadManager();
                if (dm != null) {
                    if (content.getFileIndex() == -1) {
                        allFiles = false;
                    }
                    copy.remove(dm);
                    if (!dm_listener_map.containsKey(dm)) {
                        DownloadManagerListener l = new DownloadManagerListener() {

                            @Override
                            public void stateChanged(DownloadManager manager, int state) {
                                refreshCoreToolBarItems();
                            }

                            @Override
                            public void downloadComplete(DownloadManager manager) {
                                refreshCoreToolBarItems();
                            }

                            @Override
                            public void completionChanged(DownloadManager manager, boolean bCompleted) {
                                refreshCoreToolBarItems();
                            }

                            @Override
                            public void positionChanged(DownloadManager download, int oldPosition, int newPosition) {
                                refreshCoreToolBarItems();
                            }

                            @Override
                            public void filePriorityChanged(DownloadManager download, DiskManagerFileInfo file) {
                                refreshCoreToolBarItems();
                            }
                        };
                        dm.addListener(l, false);
                        dm_listener_map.put(dm, l);
                    // System.out.println( "Added " + dm.getDisplayName() + " - size=" + dm_listener_map.size());
                    }
                }
            }
            for (Map.Entry<DownloadManager, DownloadManagerListener> e : copy.entrySet()) {
                DownloadManager dm = e.getKey();
                dm.removeListener(e.getValue());
                dm_listener_map.remove(dm);
            // System.out.println( "Removed " + dm.getDisplayName() + " - size=" + dm_listener_map.size());
            }
        }
        boolean has1Selection = currentContent.length == 1;
        boolean can_play = false;
        boolean can_stream = false;
        boolean stream_permitted = false;
        if (has1Selection) {
            if (!(currentContent[0] instanceof ISelectedVuzeFileContent)) {
                can_play = PlayUtils.canPlayDS(currentContent[0], currentContent[0].getFileIndex(), false);
                can_stream = PlayUtils.canStreamDS(currentContent[0], currentContent[0].getFileIndex(), false);
                if (can_stream) {
                    stream_permitted = PlayUtils.isStreamPermitted();
                }
            }
        }
        if (mapStates.containsKey("play")) {
            can_play |= (mapStates.get("play") & UIToolBarItem.STATE_ENABLED) > 0;
        }
        if (mapStates.containsKey("stream")) {
            can_stream |= (mapStates.get("stream") & UIToolBarItem.STATE_ENABLED) > 0;
        }
        mapStates.put("play", can_play | can_stream ? UIToolBarItem.STATE_ENABLED : 0);
        UIToolBarItem pitem = tbm.getToolBarItem("play");
        if (pitem != null) {
            if (can_stream) {
                pitem.setImageID(stream_permitted ? "image.button.stream" : "image.button.pstream");
                pitem.setTextID(stream_permitted ? "iconBar.stream" : "iconBar.pstream");
            } else {
                pitem.setImageID("image.button.play");
                pitem.setTextID("iconBar.play");
            }
        }
        UIToolBarItem startItem = tbm.getToolBarItem("start");
        if (startItem != null) {
            startItem.setTextID(allFiles ? "iconBar.startFiles" : "iconBar.start");
        }
        UIToolBarItem stopItem = tbm.getToolBarItem("stop");
        if (stopItem != null) {
            stopItem.setTextID(allFiles ? "iconBar.stopFiles" : "iconBar.stop");
        }
        UIToolBarItem ssItem = tbm.getToolBarItem("startstop");
        if (ssItem != null) {
            boolean shouldStopGroup = false;
            // if no selected content set then use the 'start' key to determine the start/stop
            // toolbar state (required for archived downloads)
            // alternative solution would be for the view to start updating the current selected
            // content which is a little painful
            boolean use_other_states = false;
            if (currentContent.length == 0) {
                use_other_states = true;
            } else {
                Boolean test = TorrentUtil.shouldStopGroupTest(currentContent);
                if (test == null) {
                    // no dms or files in the selected content so revert to using the existence of other
                    // keys
                    use_other_states = true;
                } else {
                    shouldStopGroup = test;
                }
            }
            if (use_other_states) {
                if ((mapStates.containsKey("start") && (mapStates.get("start") & UIToolBarItem.STATE_ENABLED) > 0) && (!mapStates.containsKey("stop") || (mapStates.get("stop") & UIToolBarItem.STATE_ENABLED) == 0)) {
                    shouldStopGroup = false;
                } else {
                    shouldStopGroup = true;
                }
            }
            if (allFiles) {
                ssItem.setTextID(shouldStopGroup ? "iconBar.stopFiles" : "iconBar.startFiles");
            } else {
                ssItem.setTextID(shouldStopGroup ? "iconBar.stop" : "iconBar.start");
            }
            ssItem.setImageID("image.toolbar.startstop." + (shouldStopGroup ? "stop" : "start"));
            if (currentContent.length == 0 && !mapStates.containsKey("startstop")) {
                boolean can_stop = mapStates.containsKey("stop") && (mapStates.get("stop") & UIToolBarItem.STATE_ENABLED) > 0;
                boolean can_start = mapStates.containsKey("start") && (mapStates.get("start") & UIToolBarItem.STATE_ENABLED) > 0;
                if (can_start && can_stop) {
                    can_stop = false;
                }
                if (can_start || can_stop) {
                    ssItem.setTextID(can_stop ? "iconBar.stop" : "iconBar.start");
                    ssItem.setImageID("image.toolbar.startstop." + (can_stop ? "stop" : "start"));
                    mapStates.put("startstop", UIToolBarItem.STATE_ENABLED);
                }
            }
        }
        Map<String, Long> fallBackStates = TorrentUtil.calculateToolbarStates(currentContent, null);
        for (String key : fallBackStates.keySet()) {
            if (!mapStates.containsKey(key)) {
                mapStates.put(key, fallBackStates.get(key));
            }
        }
        final String[] TBKEYS = new String[] { "play", "run", "top", "up", "down", "bottom", "start", "stop", "startstop", "remove" };
        for (String key : TBKEYS) {
            if (!mapStates.containsKey(key)) {
                mapStates.put(key, 0L);
            }
        }
        for (int i = 0; i < allToolBarItems.length; i++) {
            UIToolBarItem toolBarItem = allToolBarItems[i];
            Long state = mapStates.get(toolBarItem.getID());
            if (state != null) {
                toolBarItem.setState(state);
            }
        }
    }
}
Also used : DownloadManagerListener(com.biglybt.core.download.DownloadManagerListener) DownloadManager(com.biglybt.core.download.DownloadManager) MultipleDocumentInterfaceSWT(com.biglybt.ui.swt.mdi.MultipleDocumentInterfaceSWT) AERunnableBoolean(com.biglybt.core.util.AERunnableBoolean) ISelectedVuzeFileContent(com.biglybt.ui.selectedcontent.ISelectedVuzeFileContent) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) ISelectedContent(com.biglybt.ui.selectedcontent.ISelectedContent) UIFunctionsSWT(com.biglybt.ui.swt.UIFunctionsSWT) MdiEntrySWT(com.biglybt.ui.swt.mdi.MdiEntrySWT) UIPluginViewToolBarListener(com.biglybt.pif.ui.UIPluginViewToolBarListener)

Example 3 with DownloadManagerListener

use of com.biglybt.core.download.DownloadManagerListener in project BiglyBT by BiglySoftware.

the class ManagerUtils method asyncPauseForPeriod.

public static void asyncPauseForPeriod(final List<DownloadManager> dms, final int seconds) {
    CoreWaiterSWT.waitForCore(TriggerInThread.NEW_THREAD, new CoreRunningListener() {

        @Override
        public void coreRunning(Core core) {
            final List<DownloadManager> paused = new ArrayList<>();
            final DownloadManagerListener listener = new DownloadManagerAdapter() {

                @Override
                public void stateChanged(DownloadManager manager, int state) {
                    synchronized (paused) {
                        if (!paused.remove(manager)) {
                            return;
                        }
                    }
                    manager.removeListener(this);
                }
            };
            final long target_time = SystemTime.getOffsetTime(seconds * 1000);
            String time_str = new SimpleDateFormat("HH:mm:ss").format(new Date(target_time));
            String reason = MessageText.getString("label.resuming.at", new String[] { time_str });
            for (DownloadManager dm : dms) {
                if (!dm.isPaused() && !isPauseable(dm)) {
                    continue;
                }
                if (dm.pause(false, target_time)) {
                    dm.setStopReason(reason);
                    synchronized (paused) {
                        paused.add(dm);
                    }
                    dm.addListener(listener, false);
                }
            }
            if (paused.size() > 0) {
                SimpleTimer.addEvent("ManagerUtils.resumer", target_time, new TimerEventPerformer() {

                    @Override
                    public void perform(TimerEvent event) {
                        List<DownloadManager> to_resume = new ArrayList<>();
                        synchronized (paused) {
                            to_resume.addAll(paused);
                            paused.clear();
                        }
                        for (DownloadManager dm : to_resume) {
                            dm.removeListener(listener);
                            try {
                                if (dm.getAutoResumeTime() == target_time) {
                                    dm.resume();
                                }
                            } catch (Throwable e) {
                                Debug.out(e);
                            }
                        }
                    }
                });
            }
        }
    });
}
Also used : DownloadManagerListener(com.biglybt.core.download.DownloadManagerListener) DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerAdapter(com.biglybt.core.download.impl.DownloadManagerAdapter) CoreRunningListener(com.biglybt.core.CoreRunningListener) SWTSkinObjectList(com.biglybt.ui.swt.skin.SWTSkinObjectList) List(java.util.List) SimpleDateFormat(java.text.SimpleDateFormat) Core(com.biglybt.core.Core)

Aggregations

DownloadManager (com.biglybt.core.download.DownloadManager)3 DownloadManagerListener (com.biglybt.core.download.DownloadManagerListener)3 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)2 DownloadManagerAdapter (com.biglybt.core.download.impl.DownloadManagerAdapter)2 Core (com.biglybt.core.Core)1 CoreRunningListener (com.biglybt.core.CoreRunningListener)1 AERunnableBoolean (com.biglybt.core.util.AERunnableBoolean)1 UIPluginViewToolBarListener (com.biglybt.pif.ui.UIPluginViewToolBarListener)1 ISelectedContent (com.biglybt.ui.selectedcontent.ISelectedContent)1 ISelectedVuzeFileContent (com.biglybt.ui.selectedcontent.ISelectedVuzeFileContent)1 UIFunctionsSWT (com.biglybt.ui.swt.UIFunctionsSWT)1 MdiEntrySWT (com.biglybt.ui.swt.mdi.MdiEntrySWT)1 MultipleDocumentInterfaceSWT (com.biglybt.ui.swt.mdi.MultipleDocumentInterfaceSWT)1 SWTSkinObjectList (com.biglybt.ui.swt.skin.SWTSkinObjectList)1 SimpleDateFormat (java.text.SimpleDateFormat)1 ArrayList (java.util.ArrayList)1 IdentityHashMap (java.util.IdentityHashMap)1 List (java.util.List)1