Search in sources :

Example 1 with SpeedScaleShell

use of com.biglybt.ui.swt.shells.SpeedScaleShell in project BiglyBT by BiglySoftware.

the class SelectableSpeedMenu method invokeSlider.

/**
 * @param cClickedFrom
 * @since 3.0.1.7
 */
public static void invokeSlider(Control cClickedFrom, Core core, boolean isUpSpeed) {
    final String prefix = MessageText.getString(isUpSpeed ? "GeneralView.label.maxuploadspeed" : "GeneralView.label.maxdownloadspeed");
    GlobalManager gm = core.getGlobalManager();
    final String configAutoKey = TransferSpeedValidator.getActiveAutoUploadParameter(gm);
    boolean auto = COConfigurationManager.getBooleanParameter(configAutoKey);
    final String configKey = isUpSpeed ? TransferSpeedValidator.getActiveUploadParameter(gm) : "Max Download Speed KBs";
    int maxBandwidth = COConfigurationManager.getIntParameter(configKey);
    final boolean unlim = (maxBandwidth == 0);
    if (unlim && !isUpSpeed) {
        GlobalManagerStats stats = gm.getStats();
        int dataReceive = stats.getDataReceiveRate();
        if (dataReceive >= 1024) {
            maxBandwidth = dataReceive / 1024;
        }
    }
    SpeedScaleShell speedScale = new SpeedScaleShell() {

        @Override
        public String getStringValue(int value, String sValue) {
            if (sValue != null) {
                return prefix + ": " + sValue;
            }
            if (value == 0) {
                return MessageText.getString("MyTorrentsView.menu.setSpeed.unlimited");
            }
            if (value == -1) {
                return MessageText.getString("ConfigView.auto");
            }
            return prefix + ": " + DisplayFormatters.formatByteCountToKiBEtcPerSec(getValue() * 1024, true);
        }
    };
    int max = unlim ? (isUpSpeed ? 100 : 800) : maxBandwidth * 5;
    if (max < 50) {
        max = 50;
    }
    speedScale.setMaxValue(max);
    speedScale.setMaxTextValue(9999999);
    final String config_prefix = "config.ui.speed.partitions.manual." + (isUpSpeed ? "upload" : "download") + ".";
    int lastValue = COConfigurationManager.getIntParameter(config_prefix + "last", -10);
    Integer[] speed_limits;
    if (COConfigurationManager.getBooleanParameter(config_prefix + "enabled", false)) {
        speed_limits = parseSpeedPartitionString(COConfigurationManager.getStringParameter(config_prefix + "values", ""));
    } else {
        speed_limits = getGenericSpeedList(6, maxBandwidth);
    }
    if (speed_limits != null) {
        for (int i = 0; i < speed_limits.length; i++) {
            int value = speed_limits[i].intValue();
            if (value > 0) {
                speedScale.addOption(DisplayFormatters.formatByteCountToKiBEtcPerSec(value * 1024, true), value);
                if (value == lastValue) {
                    lastValue = -10;
                }
            }
        }
    }
    speedScale.addOption(MessageText.getString("MyTorrentsView.menu.setSpeed.unlimited"), 0);
    speedScale.addOption(MessageText.getString("ConfigView.auto"), -1);
    if (lastValue > 0) {
        speedScale.addOption(DisplayFormatters.formatByteCountToKiBEtcPerSec(lastValue * 1024, true), lastValue);
    }
    if (speedScale.open(cClickedFrom, auto ? -1 : maxBandwidth, true)) {
        int value = speedScale.getValue();
        if (!speedScale.wasMenuChosen() || lastValue == value) {
            COConfigurationManager.setParameter(config_prefix + "last", maxBandwidth);
        }
        if (value >= 0) {
            if (auto) {
                COConfigurationManager.setParameter(configAutoKey, false);
            }
            COConfigurationManager.setParameter(configKey, value);
            COConfigurationManager.save();
        } else {
            // autospeed
            COConfigurationManager.setParameter(configAutoKey, true);
            COConfigurationManager.save();
        }
    }
}
Also used : SpeedScaleShell(com.biglybt.ui.swt.shells.SpeedScaleShell) GlobalManager(com.biglybt.core.global.GlobalManager) GlobalManagerStats(com.biglybt.core.global.GlobalManagerStats)

Example 2 with SpeedScaleShell

use of com.biglybt.ui.swt.shells.SpeedScaleShell in project BiglyBT by BiglySoftware.

the class SelectableSpeedMenu method invokeSlider.

public static void invokeSlider(Control cClickedFrom, Core core, DownloadManager[] dms, boolean isUpSpeed, Shell parentShell) {
    final String prefix = MessageText.getString(isUpSpeed ? "GeneralView.label.maxuploadspeed" : "GeneralView.label.maxdownloadspeed");
    final int kInB = DisplayFormatters.getKinB();
    int maxBandwidth_k = 0;
    boolean allDisabled = true;
    for (DownloadManager dm : dms) {
        int bandwidth = (isUpSpeed ? dm.getStats().getUploadRateLimitBytesPerSecond() : dm.getStats().getDownloadRateLimitBytesPerSecond());
        int bw_k = bandwidth / kInB;
        if (bw_k > maxBandwidth_k || bandwidth == 0) {
            maxBandwidth_k = bw_k;
            allDisabled = false;
        } else if (bandwidth != -1) {
            allDisabled = false;
        }
    }
    boolean unlim = maxBandwidth_k == 0;
    final int num_entries = dms.length;
    SpeedScaleShell speedScale = new SpeedScaleShell() {

        @Override
        public String getStringValue(int value, String sValue) {
            if (sValue != null) {
                return prefix + ": " + sValue;
            }
            if (value == 0) {
                return MessageText.getString("MyTorrentsView.menu.setSpeed.unlimited");
            } else if (value == -1) {
                return MessageText.getString("ConfigView.auto");
            } else if (value == -2) {
                return MessageText.getString("label.disabled");
            }
            String speed = DisplayFormatters.formatByteCountToKiBEtcPerSec(value * kInB, true);
            if (num_entries > 1) {
                speed = MessageText.getString("MyTorrentsView.menu.setSpeed.multi", new String[] { DisplayFormatters.formatByteCountToKiBEtcPerSec(value * kInB * num_entries), String.valueOf(num_entries), speed });
            }
            return prefix + ": " + speed;
        }
    };
    int max = unlim ? (isUpSpeed ? 100 : 800) : maxBandwidth_k * 5;
    if (max < 50) {
        max = 50;
    }
    speedScale.setMaxValue(max);
    speedScale.setMaxTextValue(9999999);
    speedScale.setParentShell(parentShell);
    final String config_prefix = "config.ui.speed.partitions.manual." + (isUpSpeed ? "upload" : "download") + ".";
    int lastValue = COConfigurationManager.getIntParameter(config_prefix + "last", -10);
    Integer[] speed_limits;
    if (COConfigurationManager.getBooleanParameter(config_prefix + "enabled", false)) {
        speed_limits = parseSpeedPartitionString(COConfigurationManager.getStringParameter(config_prefix + "values", ""));
    } else {
        speed_limits = getGenericSpeedList(6, maxBandwidth_k);
    }
    if (speed_limits != null) {
        for (int i = 0; i < speed_limits.length; i++) {
            int value = speed_limits[i].intValue();
            if (value > 0) {
                int total = value * num_entries;
                String speed = DisplayFormatters.formatByteCountToKiBEtcPerSec(total * kInB, true);
                if (num_entries > 1) {
                    speed = MessageText.getString("MyTorrentsView.menu.setSpeed.multi", new String[] { speed, String.valueOf(num_entries), DisplayFormatters.formatByteCountToKiBEtcPerSec(value * kInB) });
                }
                speedScale.addOption(speed, value);
                if (value == lastValue) {
                    lastValue = -10;
                }
            }
        }
    }
    speedScale.addOption(MessageText.getString("MyTorrentsView.menu.setSpeed.unlimited"), 0);
    speedScale.addOption(MessageText.getString("label.disabled"), -2);
    if (lastValue > 0) {
        speedScale.addOption(DisplayFormatters.formatByteCountToKiBEtcPerSec(lastValue * kInB, true), lastValue);
    }
    if (speedScale.open(cClickedFrom, allDisabled ? -2 : maxBandwidth_k, true)) {
        int value = speedScale.getValue();
        if (!speedScale.wasMenuChosen() || lastValue == value) {
            COConfigurationManager.setParameter(config_prefix + "last", maxBandwidth_k);
        }
        if (value >= 0) {
            for (DownloadManager dm : dms) {
                if (isUpSpeed) {
                    dm.getStats().setUploadRateLimitBytesPerSecond(value * kInB);
                } else {
                    dm.getStats().setDownloadRateLimitBytesPerSecond(value * kInB);
                }
            }
        } else if (value == -2) {
            for (DownloadManager dm : dms) {
                if (isUpSpeed) {
                    dm.getStats().setUploadRateLimitBytesPerSecond(-1);
                } else {
                    dm.getStats().setDownloadRateLimitBytesPerSecond(-1);
                }
            }
        }
    }
}
Also used : SpeedScaleShell(com.biglybt.ui.swt.shells.SpeedScaleShell) DownloadManager(com.biglybt.core.download.DownloadManager)

Aggregations

SpeedScaleShell (com.biglybt.ui.swt.shells.SpeedScaleShell)2 DownloadManager (com.biglybt.core.download.DownloadManager)1 GlobalManager (com.biglybt.core.global.GlobalManager)1 GlobalManagerStats (com.biglybt.core.global.GlobalManagerStats)1