Search in sources :

Example 1 with SpeedLimitHandler

use of com.biglybt.core.speedmanager.SpeedLimitHandler in project BiglyBT by BiglySoftware.

the class TorrentUtil method addSpeedLimitsMenu.

protected static void addSpeedLimitsMenu(DownloadManager[] dms, Menu menu) {
    Core core = CoreFactory.getSingleton();
    Shell menu_shell = menu.getShell();
    final SpeedLimitHandler slh = SpeedLimitHandler.getSingleton(core);
    boolean all_have_limit = true;
    Set<String> common_profiles = new HashSet<>();
    final List<byte[]> dm_hashes = new ArrayList<>();
    for (int i = 0; i < dms.length; i++) {
        DownloadManager dm = dms[i];
        int maxul = dm.getStats().getUploadRateLimitBytesPerSecond();
        int maxdl = dm.getStats().getDownloadRateLimitBytesPerSecond();
        if (maxul == 0 && maxdl == 0) {
            all_have_limit = false;
        }
        TOTorrent t = dm.getTorrent();
        if (t == null) {
            common_profiles.clear();
        } else {
            try {
                byte[] hash = t.getHash();
                dm_hashes.add(hash);
                List<String> profs = slh.getProfilesForDownload(hash);
                if (i == 0) {
                    common_profiles.addAll(profs);
                } else {
                    common_profiles.retainAll(profs);
                }
            } catch (TOTorrentException e) {
                Debug.out(e);
                common_profiles.clear();
            }
        }
    }
    java.util.List<String> profiles = slh.getProfileNames();
    // add to profile
    final Menu add_to_prof_menu = new Menu(menu_shell, SWT.DROP_DOWN);
    MenuItem add_to_prof_item = new MenuItem(menu, SWT.CASCADE);
    add_to_prof_item.setMenu(add_to_prof_menu);
    Messages.setLanguageText(add_to_prof_item, "MyTorrentsView.menu.sl_add_to_prof");
    if (!all_have_limit) {
        add_to_prof_item.setEnabled(false);
    } else {
        for (final String p : profiles) {
            MenuItem addItem = new MenuItem(add_to_prof_menu, SWT.PUSH);
            addItem.setText(p);
            addItem.addListener(SWT.Selection, new Listener() {

                @Override
                public void handleEvent(Event arg0) {
                    slh.addDownloadsToProfile(p, dm_hashes);
                    MenuFactory.showText("MainWindow.menu.speed_limits.info.title", MessageText.getString("MainWindow.menu.speed_limits.info.prof", new String[] { p }), slh.getProfile(p));
                }
            });
        }
    }
    // remove from profile
    final Menu remove_from_prof_menu = new Menu(menu_shell, SWT.DROP_DOWN);
    MenuItem remove_from_prof_item = new MenuItem(menu, SWT.CASCADE);
    remove_from_prof_item.setMenu(remove_from_prof_menu);
    Messages.setLanguageText(remove_from_prof_item, "MyTorrentsView.menu.sl_remove_from_prof");
    if (common_profiles.isEmpty()) {
        remove_from_prof_item.setEnabled(false);
    } else {
        for (final String p : common_profiles) {
            MenuItem addItem = new MenuItem(remove_from_prof_menu, SWT.PUSH);
            addItem.setText(p);
            addItem.addListener(SWT.Selection, new Listener() {

                @Override
                public void handleEvent(Event arg0) {
                    slh.removeDownloadsFromProfile(p, dm_hashes);
                    MenuFactory.showText("MainWindow.menu.speed_limits.info.title", MessageText.getString("MainWindow.menu.speed_limits.info.prof", new String[] { p }), slh.getProfile(p));
                }
            });
        }
    }
}
Also used : UIManagerListener(com.biglybt.pif.ui.UIManagerListener) UserPrompterResultListener(com.biglybt.ui.UserPrompterResultListener) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener) WebSeedsEditorListener(com.biglybt.ui.swt.maketorrent.WebSeedsEditorListener) UIToolBarActivationListener(com.biglybt.pif.ui.toolbar.UIToolBarActivationListener) TrackerEditorListener(com.biglybt.ui.swt.maketorrent.TrackerEditorListener) SpeedLimitHandler(com.biglybt.core.speedmanager.SpeedLimitHandler) DownloadManager(com.biglybt.core.download.DownloadManager) TOTorrentException(com.biglybt.core.torrent.TOTorrentException) MessageBoxShell(com.biglybt.ui.swt.shells.MessageBoxShell) java.util(java.util) com.biglybt.core.util(com.biglybt.core.util) TOTorrent(com.biglybt.core.torrent.TOTorrent) MenuEvent(org.eclipse.swt.events.MenuEvent) SelectionEvent(org.eclipse.swt.events.SelectionEvent) Core(com.biglybt.core.Core) TableColumnCore(com.biglybt.ui.common.table.TableColumnCore)

Example 2 with SpeedLimitHandler

use of com.biglybt.core.speedmanager.SpeedLimitHandler in project BiglyBT by BiglySoftware.

the class MenuFactory method addSpeedLimitsToMenu.

public static MenuItem addSpeedLimitsToMenu(final Menu menuParent) {
    MenuItem speedLimitsMenuItem = createTopLevelMenuItem(menuParent, MENU_ID_SPEED_LIMITS);
    MenuBuildUtils.addMaintenanceListenerForMenu(speedLimitsMenuItem.getMenu(), new MenuBuildUtils.MenuBuilder() {

        @Override
        public void buildMenu(Menu menu, MenuEvent menuEvent) {
            if (CoreFactory.isCoreRunning()) {
                Core core = CoreFactory.getSingleton();
                final SpeedLimitHandler slh = SpeedLimitHandler.getSingleton(core);
                MenuItem viewCurrentItem = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(viewCurrentItem, "MainWindow.menu.speed_limits.view_current");
                viewCurrentItem.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event arg0) {
                        showText("MainWindow.menu.speed_limits.info.title", "MainWindow.menu.speed_limits.info.curr", slh.getCurrent());
                    }
                });
                java.util.List<String> profiles = slh.getProfileNames();
                Menu profiles_menu = new Menu(Utils.findAnyShell(), SWT.DROP_DOWN);
                MenuItem profiles_item = new MenuItem(menu, SWT.CASCADE);
                profiles_item.setMenu(profiles_menu);
                Messages.setLanguageText(profiles_item, "MainWindow.menu.speed_limits.profiles");
                if (profiles.size() == 0) {
                    profiles_item.setEnabled(false);
                } else {
                    for (final String p : profiles) {
                        Menu profile_menu = new Menu(Utils.findAnyShell(), SWT.DROP_DOWN);
                        MenuItem profile_item = new MenuItem(profiles_menu, SWT.CASCADE);
                        profile_item.setMenu(profile_menu);
                        profile_item.setText(p);
                        MenuItem loadItem = new MenuItem(profile_menu, SWT.PUSH);
                        Messages.setLanguageText(loadItem, "MainWindow.menu.speed_limits.load");
                        loadItem.addListener(SWT.Selection, new Listener() {

                            @Override
                            public void handleEvent(Event arg0) {
                                showText("MainWindow.menu.speed_limits.info.title", MessageText.getString("MainWindow.menu.speed_limits.info.prof", new String[] { p }), slh.loadProfile(p));
                            }
                        });
                        MenuItem viewItem = new MenuItem(profile_menu, SWT.PUSH);
                        Messages.setLanguageText(viewItem, "MainWindow.menu.speed_limits.view");
                        viewItem.addListener(SWT.Selection, new Listener() {

                            @Override
                            public void handleEvent(Event arg0) {
                                showText("MainWindow.menu.speed_limits.info.title", MessageText.getString("MainWindow.menu.speed_limits.info.prof", new String[] { p }), slh.getProfile(p));
                            }
                        });
                        addSeparatorMenuItem(profile_menu);
                        MenuItem deleteItem = new MenuItem(profile_menu, SWT.PUSH);
                        Messages.setLanguageText(deleteItem, "MainWindow.menu.speed_limits.delete");
                        deleteItem.addListener(SWT.Selection, new Listener() {

                            @Override
                            public void handleEvent(Event arg0) {
                                slh.deleteProfile(p);
                            }
                        });
                    }
                }
                MenuItem saveItem = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(saveItem, "MainWindow.menu.speed_limits.save_current");
                saveItem.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event arg0) {
                        UISWTInputReceiver entry = new SimpleTextEntryWindow();
                        entry.allowEmptyInput(false);
                        entry.setLocalisedTitle(MessageText.getString("MainWindow.menu.speed_limits.profile"));
                        entry.prompt(new UIInputReceiverListener() {

                            @Override
                            public void UIInputReceiverClosed(UIInputReceiver entry) {
                                if (!entry.hasSubmittedInput()) {
                                    return;
                                }
                                String input = entry.getSubmittedInput().trim();
                                if (input.length() > 0) {
                                    showText("MainWindow.menu.speed_limits.info.title", MessageText.getString("MainWindow.menu.speed_limits.info.prof", new String[] { input }), slh.saveProfile(input));
                                }
                            }
                        });
                    }
                });
                addSeparatorMenuItem(menu);
                MenuItem resetItem = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(resetItem, "MainWindow.menu.speed_limits.reset");
                resetItem.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event arg0) {
                        showText("MainWindow.menu.speed_limits.info.title", "MainWindow.menu.speed_limits.info.curr", slh.reset());
                    }
                });
                addSeparatorMenuItem(menu);
                MenuItem scheduleItem = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(scheduleItem, "MainWindow.menu.speed_limits.schedule");
                scheduleItem.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event arg0) {
                        Utils.execSWTThreadLater(1, new Runnable() {

                            @Override
                            public void run() {
                                java.util.List<String> lines = slh.getSchedule();
                                StringBuilder text = new StringBuilder(80 * lines.size());
                                for (String s : lines) {
                                    if (text.length() > 0) {
                                        text.append("\n");
                                    }
                                    text.append(s);
                                }
                                final TextViewerWindow viewer = new TextViewerWindow("MainWindow.menu.speed_limits.schedule.title", "MainWindow.menu.speed_limits.schedule.msg", text.toString(), false);
                                viewer.setEditable(true);
                                viewer.addListener(new TextViewerWindow.TextViewerWindowListener() {

                                    @Override
                                    public void closed() {
                                        if (viewer.getOKPressed()) {
                                            String text = viewer.getText();
                                            String[] lines = text.split("\n");
                                            java.util.List<String> updated_lines = new ArrayList<>(Arrays.asList(lines));
                                            java.util.List<String> result = slh.setSchedule(updated_lines);
                                            if (result != null && result.size() > 0) {
                                                showText("MainWindow.menu.speed_limits.schedule.title", "MainWindow.menu.speed_limits.schedule.err", result);
                                            }
                                        }
                                    }
                                });
                            }
                        });
                    }
                });
                addSeparatorMenuItem(menu);
                MenuItem helpItem = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(helpItem, "MainWindow.menu.speed_limits.wiki");
                helpItem.addListener(SWT.Selection, new Listener() {

                    @Override
                    public void handleEvent(Event arg0) {
                        Utils.launch(MessageText.getString("MainWindow.menu.speed_limits.wiki.url"));
                    }
                });
            }
        }
    });
    return (speedLimitsMenuItem);
}
Also used : UpdateCheckInstanceListener(com.biglybt.pif.update.UpdateCheckInstanceListener) ParameterListener(com.biglybt.core.config.ParameterListener) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener) CoreRunningListener(com.biglybt.core.CoreRunningListener) SpeedLimitHandler(com.biglybt.core.speedmanager.SpeedLimitHandler) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) Core(com.biglybt.core.Core) UISWTInputReceiver(com.biglybt.ui.swt.pif.UISWTInputReceiver) com.biglybt.core.util(com.biglybt.core.util) UIInputReceiver(com.biglybt.pif.ui.UIInputReceiver) UIInputReceiverListener(com.biglybt.pif.ui.UIInputReceiverListener)

Example 3 with SpeedLimitHandler

use of com.biglybt.core.speedmanager.SpeedLimitHandler in project BiglyBT by BiglySoftware.

the class TorrentMenuFancy method buildTorrentCustomMenu_Control.

public void buildTorrentCustomMenu_Control(final Composite cParent, final DownloadManager[] dms) {
    final int userMode = COConfigurationManager.getIntParameter("User Mode");
    boolean start = false;
    boolean stop = false;
    boolean pause = false;
    boolean recheck = false;
    boolean barsOpened = true;
    boolean bChangeDir = hasSelection;
    for (int i = 0; i < dms.length; i++) {
        DownloadManager dm = dms[i];
        if (barsOpened && !DownloadBar.getManager().isOpen(dm)) {
            barsOpened = false;
        }
        stop = stop || ManagerUtils.isStopable(dm);
        start = start || ManagerUtils.isStartable(dm);
        pause = pause || ManagerUtils.isPauseable(dm);
        recheck = recheck || dm.canForceRecheck();
        boolean stopped = ManagerUtils.isStopped(dm);
        int state = dm.getState();
        bChangeDir &= (state == DownloadManager.STATE_ERROR || state == DownloadManager.STATE_STOPPED || state == DownloadManager.STATE_QUEUED);
        /**
         * Only perform a test on disk if:
         *    1) We are currently set to allow the "Change Data Directory" option, and
         *    2) We've only got one item selected - otherwise, we may potentially end up checking massive
         *       amounts of files across multiple torrents before we generate a menu.
         */
        if (bChangeDir && dms.length == 1) {
            bChangeDir = dm.isDataAlreadyAllocated();
            if (bChangeDir && state == DownloadManager.STATE_ERROR) {
                // filesExist is way too slow!
                bChangeDir = !dm.filesExist(true);
            } else {
                DiskManagerFileInfo[] files = dm.getDiskManagerFileInfoSet().getFiles();
                bChangeDir = false;
                for (DiskManagerFileInfo info : files) {
                    if (info.isSkipped()) {
                        continue;
                    }
                    bChangeDir = !info.getFile(true).exists();
                    break;
                }
            }
        }
    }
    Composite cQuickCommands = new Composite(cParent, SWT.NONE);
    // cQuickCommands.setBackground(ColorCache.getRandomColor());
    RowLayout rowLayout = new RowLayout(SWT.HORIZONTAL);
    rowLayout.justify = true;
    rowLayout.marginLeft = 0;
    rowLayout.marginRight = 0;
    cQuickCommands.setLayout(rowLayout);
    GridData gd = new GridData();
    gd.grabExcessHorizontalSpace = true;
    gd.horizontalAlignment = SWT.FILL;
    Utils.setLayoutData(cQuickCommands, gd);
    // Queue
    createActionButton(dms, cQuickCommands, "MyTorrentsView.menu.queue", "start", start, new ListenerGetOffSWT() {

        @Override
        void handleEventOffSWT(Event event) {
            TorrentUtil.queueDataSources(dms, true);
        }
    });
    // Force Start
    if (userMode > 0) {
        boolean forceStart = false;
        boolean forceStartEnabled = false;
        for (int i = 0; i < dms.length; i++) {
            DownloadManager dm = dms[i];
            forceStartEnabled = forceStartEnabled || ManagerUtils.isForceStartable(dm);
            forceStart = forceStart || dm.isForceStart();
        }
        final boolean newForceStart = !forceStart;
        createActionButton(dms, cQuickCommands, "MyTorrentsView.menu.forceStart", "forcestart", forceStartEnabled, forceStart ? SWT.BORDER : SWT.PUSH, new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager dm) {
                if (ManagerUtils.isForceStartable(dm)) {
                    dm.setForceStart(newForceStart);
                }
            }
        });
    }
    // Pause
    if (userMode > 0) {
        createActionButton(dms, cQuickCommands, "v3.MainWindow.button.pause", "pause", pause, new ListenerGetOffSWT() {

            @Override
            public void handleEventOffSWT(Event event) {
                TorrentUtil.pauseDataSources(dms);
            }
        });
    }
    // Stop
    createActionButton(dms, cQuickCommands, "MyTorrentsView.menu.stop", "stop", stop, new ListenerGetOffSWT() {

        @Override
        public void handleEventOffSWT(Event event) {
            TorrentUtil.stopDataSources(dms);
        }
    });
    // Force Recheck
    createActionButton(dms, cQuickCommands, "MyTorrentsView.menu.recheck", "recheck", recheck, new ListenerDMTask(dms) {

        @Override
        public void run(DownloadManager dm) {
            if (dm.canForceRecheck()) {
                dm.forceRecheck();
            }
        }
    });
    // Delete
    createActionButton(dms, cQuickCommands, "menu.delete.options", "delete", hasSelection, new Listener() {

        @Override
        public void handleEvent(Event event) {
            TorrentUtil.removeDownloads(dms, null, true);
        }
    });
    if (bChangeDir) {
        createRow(cParent, "MyTorrentsView.menu.changeDirectory", null, new Listener() {

            @Override
            public void handleEvent(Event e) {
                TorrentUtil.changeDirSelectedTorrents(dms, parentShell);
            }
        });
    }
    // Open Details
    if (hasSelection) {
        createRow(cParent, "MyTorrentsView.menu.showdetails", "details", new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager dm) {
                UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
                if (uiFunctions != null) {
                    uiFunctions.getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_TORRENT_DETAILS, dm);
                }
            }
        });
    }
    // Open Bar
    if (hasSelection) {
        FancyRowInfo row = createRow(cParent, "MyTorrentsView.menu.showdownloadbar", "downloadBar", new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager dm) {
                if (DownloadBar.getManager().isOpen(dm)) {
                    DownloadBar.close(dm);
                } else {
                    DownloadBar.open(dm, parentShell);
                }
            }
        });
        row.setSelection(barsOpened);
    }
    if (hasSelection) {
        FancyRowInfo rowSpeedDL = createRow(cParent, "MyTorrentsView.menu.downSpeedLimit", "image.torrentspeed.down", false, new Listener() {

            @Override
            public void handleEvent(Event e) {
                Event event = new Event();
                event.type = SWT.MouseUp;
                event.widget = e.widget;
                event.stateMask = e.stateMask;
                event.button = e.button;
                e.display.post(event);
                Core core = CoreFactory.getSingleton();
                SelectableSpeedMenu.invokeSlider((Control) event.widget, core, dms, false, shell);
                if (e.display.getActiveShell() != shell) {
                    if (!shell.isDisposed()) {
                        shell.dispose();
                    }
                    return;
                }
                FancyRowInfo rowInfo = findRowInfo(event.widget);
                if (rowInfo != null) {
                    updateRowSpeed(rowInfo, false);
                }
            }
        });
        rowSpeedDL.keepMenu = true;
        updateRowSpeed(rowSpeedDL, false);
    }
    if (hasSelection) {
        FancyRowInfo rowSpeedUL = createRow(cParent, "MyTorrentsView.menu.upSpeedLimit", "image.torrentspeed.up", false, new Listener() {

            @Override
            public void handleEvent(Event e) {
                Event event = new Event();
                event.type = SWT.MouseUp;
                event.widget = e.widget;
                event.stateMask = e.stateMask;
                event.button = e.button;
                e.display.post(event);
                Core core = CoreFactory.getSingleton();
                SelectableSpeedMenu.invokeSlider((Control) e.widget, core, dms, true, shell);
                if (e.display.getActiveShell() != shell) {
                    if (!shell.isDisposed()) {
                        shell.dispose();
                    }
                    return;
                }
                FancyRowInfo rowInfo = findRowInfo(event.widget);
                if (rowInfo != null) {
                    updateRowSpeed(rowInfo, true);
                }
            }
        });
        rowSpeedUL.keepMenu = true;
        updateRowSpeed(rowSpeedUL, true);
    }
    if (hasSelection && userMode > 0) {
        createMenuRow(cParent, "MyTorrentsView.menu.tracker", null, new FancyMenuRowInfoListener() {

            @Override
            public void buildMenu(Menu menu) {
                boolean changeUrl = hasSelection;
                boolean manualUpdate = true;
                boolean allStopped = true;
                boolean canMove = true;
                int userMode = COConfigurationManager.getIntParameter("User Mode");
                final boolean use_open_containing_folder = COConfigurationManager.getBooleanParameter("MyTorrentsView.menu.show_parent_folder_enabled");
                for (DownloadManager dm : dms) {
                    boolean stopped = ManagerUtils.isStopped(dm);
                    allStopped &= stopped;
                    canMove = canMove && dm.canMoveDataFiles();
                    if (userMode < 2) {
                        TRTrackerAnnouncer trackerClient = dm.getTrackerClient();
                        if (trackerClient != null) {
                            boolean update_state = ((SystemTime.getCurrentTime() / 1000 - trackerClient.getLastUpdateTime() >= TRTrackerAnnouncer.REFRESH_MINIMUM_SECS));
                            manualUpdate = manualUpdate & update_state;
                        }
                    }
                }
                TorrentUtil.addTrackerTorrentMenu(menu, dms, changeUrl, manualUpdate, allStopped, use_open_containing_folder, canMove);
            }
        });
    }
    if (hasSelection) {
        Core core = CoreFactory.getSingleton();
        SpeedLimitHandler slh = SpeedLimitHandler.getSingleton(core);
        if (slh.hasAnyProfiles()) {
            createMenuRow(cParent, IMenuConstants.MENU_ID_SPEED_LIMITS, null, new FancyMenuRowInfoListener() {

                @Override
                public void buildMenu(Menu menu) {
                    TorrentUtil.addSpeedLimitsMenu(dms, menu);
                }
            });
        }
    }
    if (userMode > 0 && hasSelection) {
        boolean can_pause = false;
        for (int i = 0; i < dms.length; i++) {
            DownloadManager dm = dms[i];
            if (ManagerUtils.isPauseable(dm)) {
                can_pause = true;
                break;
            }
        }
        if (can_pause) {
            createRow(detailArea, "MainWindow.menu.transfers.pausetransfersfor", null, new Listener() {

                @Override
                public void handleEvent(Event event) {
                    TorrentUtil.pauseDownloadsFor(dms);
                }
            });
        }
    }
    if (userMode > 0 && dms.length > 1) {
        createRow(cParent, "label.options.and.info", null, new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager[] dms) {
                UIFunctions uiFunctions = UIFunctionsManager.getUIFunctions();
                if (uiFunctions != null) {
                    uiFunctions.getMDI().showEntryByID(MultipleDocumentInterface.SIDEBAR_SECTION_TORRENT_OPTIONS, dms);
                }
            }
        });
    }
    if (userMode > 0) {
        createMenuRow(cParent, "MyTorrentsView.menu.peersource", null, new FancyMenuRowInfoListener() {

            @Override
            public void buildMenu(Menu menu) {
                TorrentUtil.addPeerSourceSubMenu(dms, menu);
            }
        });
    }
    // Sequential download
    {
        boolean allSeq = true;
        boolean AllNonSeq = true;
        for (int j = 0; j < dms.length; j++) {
            DownloadManager dm = dms[j];
            boolean seq = dm.getDownloadState().getFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD);
            if (seq) {
                AllNonSeq = false;
            } else {
                allSeq = false;
            }
        }
        boolean bChecked;
        if (allSeq) {
            bChecked = true;
        } else if (AllNonSeq) {
            bChecked = false;
        } else {
            bChecked = false;
        }
        final boolean newSeq = !bChecked;
        FancyRowInfo row = createRow(cParent, "menu.sequential.download", null, new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager dm) {
                dm.getDownloadState().setFlag(DownloadManagerState.FLAG_SEQUENTIAL_DOWNLOAD, newSeq);
            }
        });
        row.setSelection(bChecked);
    }
    // IP Filter Enable
    if (userMode > 0 && IpFilterManagerFactory.getSingleton().getIPFilter().isEnabled()) {
        boolean allEnabled = true;
        boolean allDisabled = true;
        for (int j = 0; j < dms.length; j++) {
            DownloadManager dm = dms[j];
            boolean filterDisabled = dm.getDownloadState().getFlag(DownloadManagerState.FLAG_DISABLE_IP_FILTER);
            if (filterDisabled) {
                allEnabled = false;
            } else {
                allDisabled = false;
            }
        }
        boolean bChecked;
        if (allEnabled) {
            bChecked = true;
        } else if (allDisabled) {
            bChecked = false;
        } else {
            bChecked = false;
        }
        final boolean newDisable = bChecked;
        FancyRowInfo row = createRow(cParent, "MyTorrentsView.menu.ipf_enable", null, new ListenerDMTask(dms) {

            @Override
            public void run(DownloadManager dm) {
                dm.getDownloadState().setFlag(DownloadManagerState.FLAG_DISABLE_IP_FILTER, newDisable);
            }
        });
        row.setSelection(bChecked);
    }
    if (userMode > 1) {
        createMenuRow(cParent, "MyTorrentsView.menu.networks", null, new FancyMenuRowInfoListener() {

            @Override
            public void buildMenu(Menu menu) {
                TorrentUtil.addNetworksSubMenu(dms, menu);
            }
        });
    }
    // Advanced menu with stuff I don't know where to put
    if (userMode > 0) {
        createMenuRow(cParent, "MyTorrentsView.menu.advancedmenu", null, new FancyMenuRowInfoListener() {

            @Override
            public void buildMenu(Menu menu) {
                boolean allStopped = true;
                boolean allScanSelected = true;
                boolean allScanNotSelected = true;
                boolean fileMove = true;
                boolean allResumeIncomplete = true;
                boolean hasClearableLinks = false;
                boolean hasRevertableFiles = false;
                for (DownloadManager dm : dms) {
                    boolean stopped = ManagerUtils.isStopped(dm);
                    allStopped &= stopped;
                    fileMove = fileMove && dm.canMoveDataFiles();
                    boolean scan = dm.getDownloadState().getFlag(DownloadManagerState.FLAG_SCAN_INCOMPLETE_PIECES);
                    // include DND files in incomplete stat, since a recheck may
                    // find those files have been completed
                    boolean incomplete = !dm.isDownloadComplete(true);
                    allScanSelected = incomplete && allScanSelected && scan;
                    allScanNotSelected = incomplete && allScanNotSelected && !scan;
                    DownloadManagerState dms = dm.getDownloadState();
                    if (dms.isResumeDataComplete()) {
                        allResumeIncomplete = false;
                    }
                    if (stopped && !hasClearableLinks) {
                        if (dm.getDiskManagerFileInfoSet().nbFiles() > 1) {
                            if (dms.getFileLinks().hasLinks()) {
                                hasClearableLinks = true;
                            }
                        }
                    }
                    if (dm.getDownloadState().getFileLinks().size() > 0) {
                        hasRevertableFiles = true;
                    }
                }
                boolean fileRescan = allScanSelected || allScanNotSelected;
                final MenuItem itemFileRescan = new MenuItem(menu, SWT.CHECK);
                Messages.setLanguageText(itemFileRescan, "MyTorrentsView.menu.rescanfile");
                itemFileRescan.addListener(SWT.Selection, new ListenerDMTask(dms) {

                    @Override
                    public void run(DownloadManager dm) {
                        dm.getDownloadState().setFlag(DownloadManagerState.FLAG_SCAN_INCOMPLETE_PIECES, itemFileRescan.getSelection());
                    }
                });
                itemFileRescan.setSelection(allScanSelected);
                itemFileRescan.setEnabled(fileRescan);
                // revert
                final MenuItem itemRevertFiles = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(itemRevertFiles, "MyTorrentsView.menu.revertfiles");
                itemRevertFiles.addListener(SWT.Selection, new ListenerDMTask(dms) {

                    @Override
                    public void run(DownloadManager[] dms) {
                        FilesViewMenuUtil.revertFiles(tv, dms);
                    }
                });
                itemRevertFiles.setEnabled(hasRevertableFiles);
                // clear links
                final MenuItem itemClearLinks = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(itemClearLinks, "FilesView.menu.clear.links");
                itemClearLinks.addListener(SWT.Selection, new ListenerDMTask(dms) {

                    @Override
                    public void run(DownloadManager dm) {
                        if (ManagerUtils.isStopped(dm) && dm.getDownloadState().getFileLinks().hasLinks()) {
                            DiskManagerFileInfoSet fis = dm.getDiskManagerFileInfoSet();
                            if (fis.nbFiles() > 1) {
                                DiskManagerFileInfo[] files = fis.getFiles();
                                for (DiskManagerFileInfo file_info : files) {
                                    File file_link = file_info.getFile(true);
                                    File file_nolink = file_info.getFile(false);
                                    if (!file_nolink.getAbsolutePath().equals(file_link.getAbsolutePath())) {
                                        file_info.setLink(null);
                                    }
                                }
                            }
                        }
                    }
                });
                itemClearLinks.setEnabled(hasClearableLinks);
                // clear allocation
                MenuItem itemFileClearAlloc = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(itemFileClearAlloc, "MyTorrentsView.menu.clear_alloc_data");
                itemFileClearAlloc.addListener(SWT.Selection, new ListenerDMTask(dms) {

                    @Override
                    public void run(DownloadManager dm) {
                        dm.setDataAlreadyAllocated(false);
                    }
                });
                itemFileClearAlloc.setEnabled(allStopped);
                // clear resume
                MenuItem itemFileClearResume = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(itemFileClearResume, "MyTorrentsView.menu.clear_resume_data");
                itemFileClearResume.addListener(SWT.Selection, new ListenerDMTask(dms) {

                    @Override
                    public void run(DownloadManager dm) {
                        dm.getDownloadState().clearResumeData();
                    }
                });
                itemFileClearResume.setEnabled(allStopped);
                // set resume complete
                MenuItem itemFileSetResumeComplete = new MenuItem(menu, SWT.PUSH);
                Messages.setLanguageText(itemFileSetResumeComplete, "MyTorrentsView.menu.set.resume.complete");
                itemFileSetResumeComplete.addListener(SWT.Selection, new ListenerDMTask(dms) {

                    @Override
                    public void run(DownloadManager dm) {
                        TorrentUtils.setResumeDataCompletelyValid(dm.getDownloadState());
                    }
                });
                itemFileSetResumeComplete.setEnabled(allStopped && allResumeIncomplete);
                if (userMode > 1 && isSeedingView) {
                    boolean canSetSuperSeed = false;
                    boolean superSeedAllYes = true;
                    boolean superSeedAllNo = true;
                    for (DownloadManager dm : dms) {
                        PEPeerManager pm = dm.getPeerManager();
                        if (pm != null) {
                            if (pm.canToggleSuperSeedMode()) {
                                canSetSuperSeed = true;
                            }
                            if (pm.isSuperSeedMode()) {
                                superSeedAllYes = false;
                            } else {
                                superSeedAllNo = false;
                            }
                        } else {
                            superSeedAllYes = false;
                            superSeedAllNo = false;
                        }
                    }
                    final MenuItem itemSuperSeed = new MenuItem(menu, SWT.CHECK);
                    Messages.setLanguageText(itemSuperSeed, "ManagerItem.superseeding");
                    boolean enabled = canSetSuperSeed && (superSeedAllNo || superSeedAllYes);
                    itemSuperSeed.setEnabled(enabled);
                    final boolean selected = superSeedAllNo;
                    if (enabled) {
                        itemSuperSeed.setSelection(selected);
                        itemSuperSeed.addListener(SWT.Selection, new ListenerDMTask(dms) {

                            @Override
                            public void run(DownloadManager dm) {
                                PEPeerManager pm = dm.getPeerManager();
                                if (pm != null) {
                                    if (pm.isSuperSeedMode() == selected && pm.canToggleSuperSeedMode()) {
                                        pm.setSuperSeedMode(!selected);
                                    }
                                }
                            }
                        });
                    }
                }
            }
        });
    }
}
Also used : TableSelectedRowsListener(com.biglybt.ui.swt.views.table.TableSelectedRowsListener) SpeedLimitHandler(com.biglybt.core.speedmanager.SpeedLimitHandler) DownloadManager(com.biglybt.core.download.DownloadManager) DownloadManagerState(com.biglybt.core.download.DownloadManagerState) UIFunctions(com.biglybt.ui.UIFunctions) TableRowCore(com.biglybt.ui.common.table.TableRowCore) TableCellCore(com.biglybt.ui.common.table.TableCellCore) Core(com.biglybt.core.Core) TableColumnCore(com.biglybt.ui.common.table.TableColumnCore) DiskManagerFileInfo(com.biglybt.core.disk.DiskManagerFileInfo) DiskManagerFileInfoSet(com.biglybt.core.disk.DiskManagerFileInfoSet) MenuItem(com.biglybt.pif.ui.menus.MenuItem) Point(org.eclipse.swt.graphics.Point) TRTrackerAnnouncer(com.biglybt.core.tracker.client.TRTrackerAnnouncer) PEPeerManager(com.biglybt.core.peer.PEPeerManager) File(java.io.File)

Aggregations

Core (com.biglybt.core.Core)3 SpeedLimitHandler (com.biglybt.core.speedmanager.SpeedLimitHandler)3 DownloadManager (com.biglybt.core.download.DownloadManager)2 com.biglybt.core.util (com.biglybt.core.util)2 UIInputReceiverListener (com.biglybt.pif.ui.UIInputReceiverListener)2 TableColumnCore (com.biglybt.ui.common.table.TableColumnCore)2 CoreRunningListener (com.biglybt.core.CoreRunningListener)1 ParameterListener (com.biglybt.core.config.ParameterListener)1 DiskManagerFileInfo (com.biglybt.core.disk.DiskManagerFileInfo)1 DiskManagerFileInfoSet (com.biglybt.core.disk.DiskManagerFileInfoSet)1 DownloadManagerState (com.biglybt.core.download.DownloadManagerState)1 PEPeerManager (com.biglybt.core.peer.PEPeerManager)1 TOTorrent (com.biglybt.core.torrent.TOTorrent)1 TOTorrentException (com.biglybt.core.torrent.TOTorrentException)1 TRTrackerAnnouncer (com.biglybt.core.tracker.client.TRTrackerAnnouncer)1 UIInputReceiver (com.biglybt.pif.ui.UIInputReceiver)1 UIManagerListener (com.biglybt.pif.ui.UIManagerListener)1 MenuItem (com.biglybt.pif.ui.menus.MenuItem)1 UIToolBarActivationListener (com.biglybt.pif.ui.toolbar.UIToolBarActivationListener)1 UpdateCheckInstanceListener (com.biglybt.pif.update.UpdateCheckInstanceListener)1