Search in sources :

Example 1 with ISelectedVuzeFileContent

use of com.biglybt.ui.selectedcontent.ISelectedVuzeFileContent in project BiglyBT by BiglySoftware.

the class TorrentUtil method calculateToolbarStates.

// XXX Don't think *View's need this call anymore.  ToolBarView does it fo them
public static Map<String, Long> calculateToolbarStates(ISelectedContent[] currentContent, String viewID_unused) {
    // System.out.println("calculateToolbarStates(" + currentContent.length + ", " + viewID_unused + " via " + Debug.getCompressedStackTrace());
    /*
		String[] TBKEYS = new String[] {
			"download",
			"play",
			"stream",
			"run",
			"top",
			"up",
			"down",
			"bottom",
			"start",
			"stop",
			"remove"
		};
		*/
    Map<String, Long> mapNewToolbarStates = new HashMap<>();
    String[] itemsNeedingSelection = {};
    String[] itemsNeedingRealDMSelection = { "remove", "top", "bottom", "transcode", "startstop" };
    String[] itemsRequiring1DMwithHash = { "details", "comment", "up", "down" };
    String[] itemsRequiring1DMSelection = {};
    int numSelection = currentContent.length;
    boolean hasSelection = numSelection > 0;
    boolean has1Selection = numSelection == 1;
    for (int i = 0; i < itemsNeedingSelection.length; i++) {
        String itemID = itemsNeedingSelection[i];
        mapNewToolbarStates.put(itemID, hasSelection ? UIToolBarItem.STATE_ENABLED : 0);
    }
    TableView tv = SelectedContentManager.getCurrentlySelectedTableView();
    // not sure why we assume that the existance of any table view
    boolean hasRealDM = tv != null;
    if (!hasRealDM && numSelection > 0) {
        hasRealDM = true;
        for (int i = 0; i < currentContent.length; i++) {
            ISelectedContent content = currentContent[i];
            DownloadManager dm = content.getDownloadManager();
            if (dm == null) {
                hasRealDM = false;
                break;
            }
        }
    }
    if (!hasRealDM) {
        MultipleDocumentInterfaceSWT mdi = UIFunctionsManagerSWT.getUIFunctionsSWT().getMDISWT();
        if (mdi != null) {
            MdiEntrySWT entry = mdi.getCurrentEntry();
            if (entry != null) {
                if (entry.getDataSource() instanceof DownloadManager) {
                    hasRealDM = true;
                } else if ((entry instanceof UIPluginView) && (((UIPluginView) entry).getDataSource() instanceof DownloadManager)) {
                    hasRealDM = true;
                }
            }
        }
    }
    boolean canStart = false;
    boolean canStop = false;
    boolean canRemoveFileInfo = false;
    boolean canRunFileInfo = false;
    boolean canCheckExist = false;
    boolean hasDM = false;
    boolean canRecheck = false;
    if (currentContent.length > 0 && hasRealDM) {
        // well, in fact, we can have hasRealDM set to true here (because tv isn't null) and actually not have a real dm.
        // fancy that - protect against null DownloadManagers...
        boolean canMoveUp = false;
        boolean canMoveDown = false;
        boolean canDownload = false;
        canCheckExist = true;
        GlobalManager gm = null;
        for (int i = 0; i < currentContent.length; i++) {
            ISelectedContent content = currentContent[i];
            DownloadManager dm = content.getDownloadManager();
            if (dm == null) {
                if (!canDownload && content.getDownloadInfo() != null) {
                    canDownload = true;
                }
                continue;
            }
            if (gm == null) {
                gm = dm.getGlobalManager();
            }
            int state = dm.getState();
            canCheckExist &= (state == DownloadManager.STATE_ERROR || state == DownloadManager.STATE_STOPPED || state == DownloadManager.STATE_QUEUED);
            int fileIndex = content.getFileIndex();
            if (fileIndex == -1) {
                if (!canMoveUp && gm.isMoveableUp(dm)) {
                    canMoveUp = true;
                }
                if (!canMoveDown && gm.isMoveableDown(dm)) {
                    canMoveDown = true;
                }
                hasDM = true;
                if (!canStart && ManagerUtils.isStartable(dm)) {
                    canStart = true;
                }
                if (!canStop && ManagerUtils.isStopable(dm)) {
                    canStop = true;
                }
            } else {
                DiskManagerFileInfoSet fileInfos = dm.getDiskManagerFileInfoSet();
                if (fileIndex < fileInfos.nbFiles()) {
                    DiskManagerFileInfo fileInfo = fileInfos.getFiles()[fileIndex];
                    if (!canStart && (fileInfo.isSkipped())) {
                        canStart = true;
                    }
                    if (!canStop && !fileInfo.isSkipped()) {
                        canStop = true;
                    }
                    if (!canRemoveFileInfo && !fileInfo.isSkipped()) {
                        int storageType = fileInfo.getStorageType();
                        if (storageType == DiskManagerFileInfo.ST_LINEAR || storageType == DiskManagerFileInfo.ST_COMPACT) {
                            canRemoveFileInfo = true;
                        }
                    }
                    if (!canRunFileInfo && fileInfo.getAccessMode() == DiskManagerFileInfo.READ && fileInfo.getDownloaded() == fileInfo.getLength() && FileUtil.existsWithTimeout(fileInfo.getFile(true))) {
                        canRunFileInfo = true;
                    }
                }
            }
            canRecheck = canRecheck || dm.canForceRecheck();
        }
        boolean canRemove = hasDM || canRemoveFileInfo;
        mapNewToolbarStates.put("remove", canRemove ? UIToolBarItem.STATE_ENABLED : 0);
        mapNewToolbarStates.put("download", canDownload ? UIToolBarItem.STATE_ENABLED : 0);
        if (currentContent.length == 1) {
            mapNewToolbarStates.put("up", canMoveUp ? UIToolBarItem.STATE_ENABLED : 0);
            mapNewToolbarStates.put("down", canMoveDown ? UIToolBarItem.STATE_ENABLED : 0);
        }
    }
    boolean canRun = has1Selection && ((hasDM && !canRunFileInfo) || (!hasDM && canRunFileInfo));
    if (canRun) {
        ISelectedContent content = currentContent[0];
        DownloadManager dm = content.getDownloadManager();
        if (dm == null) {
            canRun = false;
        } else {
            TOTorrent torrent = dm.getTorrent();
            if (torrent == null) {
                canRun = false;
            } else if (!dm.getAssumedComplete() && torrent.isSimpleTorrent()) {
                canRun = false;
            /*
									} else if (PlatformTorrentUtils.useEMP(torrent)
											&& PlatformTorrentUtils.embeddedPlayerAvail()
											&& PlayUtils.canProgressiveOrIsComplete(torrent)) {
										// play button enabled and not UMP.. don't need launch

										canRun = false;

									}
									*/
            }
        }
    }
    mapNewToolbarStates.put("run", canRun ? UIToolBarItem.STATE_ENABLED : 0);
    mapNewToolbarStates.put("start", canStart ? UIToolBarItem.STATE_ENABLED : 0);
    mapNewToolbarStates.put("stop", canStop ? UIToolBarItem.STATE_ENABLED : 0);
    mapNewToolbarStates.put("startstop", canStart || canStop ? UIToolBarItem.STATE_ENABLED : 0);
    for (int i = 0; i < itemsNeedingRealDMSelection.length; i++) {
        String itemID = itemsNeedingRealDMSelection[i];
        if (!mapNewToolbarStates.containsKey(itemID)) {
            mapNewToolbarStates.put(itemID, hasSelection && hasDM && hasRealDM ? UIToolBarItem.STATE_ENABLED : 0);
        }
    }
    for (int i = 0; i < itemsRequiring1DMSelection.length; i++) {
        String itemID = itemsRequiring1DMSelection[i];
        if (!mapNewToolbarStates.containsKey(itemID)) {
            mapNewToolbarStates.put(itemID, has1Selection && hasDM ? UIToolBarItem.STATE_ENABLED : 0);
        }
    }
    for (int i = 0; i < itemsRequiring1DMwithHash.length; i++) {
        String itemID = itemsRequiring1DMwithHash[i];
        if (!mapNewToolbarStates.containsKey(itemID)) {
            mapNewToolbarStates.put(itemID, hasDM ? UIToolBarItem.STATE_ENABLED : 0);
        }
    }
    mapNewToolbarStates.put("download", has1Selection && (!(currentContent[0] instanceof ISelectedVuzeFileContent)) && currentContent[0].getDownloadManager() == null && (currentContent[0].getHash() != null || currentContent[0].getDownloadInfo() != null) ? UIToolBarItem.STATE_ENABLED : 0);
    if (tv != null) {
        TableColumn tc = tv.getTableColumn(RankItem.COLUMN_ID);
        if (tc != null && !tc.isVisible()) {
            mapNewToolbarStates.put("up", 0L);
            mapNewToolbarStates.put("down", 0L);
        }
    }
    mapNewToolbarStates.put(TU_ITEM_RECHECK, canRecheck ? UIToolBarItem.STATE_ENABLED : 0);
    mapNewToolbarStates.put(TU_ITEM_CHECK_FILES, canCheckExist ? UIToolBarItem.STATE_ENABLED : 0);
    boolean ss = COConfigurationManager.getBooleanParameter("Show Side Bar");
    mapNewToolbarStates.put(TU_ITEM_SHOW_SIDEBAR, UIToolBarItem.STATE_ENABLED | (ss ? UIToolBarItem.STATE_DOWN : 0));
    return mapNewToolbarStates;
}
Also used : ISelectedVuzeFileContent(com.biglybt.ui.selectedcontent.ISelectedVuzeFileContent) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) ISelectedContent(com.biglybt.ui.selectedcontent.ISelectedContent) DiskManagerFileInfoSet(com.biglybt.core.disk.DiskManagerFileInfoSet) MdiEntrySWT(com.biglybt.ui.swt.mdi.MdiEntrySWT) DownloadManager(com.biglybt.core.download.DownloadManager) TableColumn(com.biglybt.pif.ui.tables.TableColumn) GlobalManager(com.biglybt.core.global.GlobalManager) TOTorrent(com.biglybt.core.torrent.TOTorrent) UIPluginView(com.biglybt.pif.ui.UIPluginView) MultipleDocumentInterfaceSWT(com.biglybt.ui.swt.mdi.MultipleDocumentInterfaceSWT) TableView(com.biglybt.ui.common.table.TableView)

Example 2 with ISelectedVuzeFileContent

use of com.biglybt.ui.selectedcontent.ISelectedVuzeFileContent 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)

Aggregations

DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)2 DownloadManager (com.biglybt.core.download.DownloadManager)2 ISelectedContent (com.biglybt.ui.selectedcontent.ISelectedContent)2 ISelectedVuzeFileContent (com.biglybt.ui.selectedcontent.ISelectedVuzeFileContent)2 MdiEntrySWT (com.biglybt.ui.swt.mdi.MdiEntrySWT)2 MultipleDocumentInterfaceSWT (com.biglybt.ui.swt.mdi.MultipleDocumentInterfaceSWT)2 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)1 DownloadManagerListener (com.biglybt.core.download.DownloadManagerListener)1 GlobalManager (com.biglybt.core.global.GlobalManager)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 AERunnableBoolean (com.biglybt.core.util.AERunnableBoolean)1 UIPluginView (com.biglybt.pif.ui.UIPluginView)1 UIPluginViewToolBarListener (com.biglybt.pif.ui.UIPluginViewToolBarListener)1 TableColumn (com.biglybt.pif.ui.tables.TableColumn)1 TableView (com.biglybt.ui.common.table.TableView)1 UIFunctionsSWT (com.biglybt.ui.swt.UIFunctionsSWT)1