| 1 |
# COPYRIGHT (C) 2022-2023 Nicotine+ Contributors |
| 2 |
# |
| 3 |
# GNU GENERAL PUBLIC LICENSE |
| 4 |
# Version 3, 29 June 2007 |
| 5 |
# |
| 6 |
# This program is free software: you can redistribute it and/or modify |
| 7 |
# it under the terms of the GNU General Public License as published by |
| 8 |
# the Free Software Foundation, either version 3 of the License, or |
| 9 |
# (at your option) any later version. |
| 10 |
# |
| 11 |
# This program is distributed in the hope that it will be useful, |
| 12 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
# GNU General Public License for more details. |
| 15 |
# |
| 16 |
# You should have received a copy of the GNU General Public License |
| 17 |
# along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
|
| 19 |
from pynicotine.config import config |
| 20 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 21 |
from pynicotine.gtkgui.widgets import ui |
| 22 |
from pynicotine.gtkgui.widgets.popover import Popover |
| 23 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 24 |
|
| 25 |
|
| 26 |
class TransferSpeeds(Popover): |
| 27 |
|
| 28 |
def __init__(self, window, transfer_type): |
| 29 |
|
| 30 |
self.transfer_type = transfer_type |
| 31 |
|
| 32 |
( |
| 33 |
self.alt_speed_spinner, |
| 34 |
self.container, |
| 35 |
self.speed_spinner, |
| 36 |
self.use_alt_limit_radio, |
| 37 |
self.use_limit_radio, |
| 38 |
self.use_unlimited_speed_radio |
| 39 |
) = ui.load(scope=self, path=f"popovers/{transfer_type}speeds.ui") |
| 40 |
|
| 41 |
super().__init__( |
| 42 |
window=window, |
| 43 |
content_box=self.container, |
| 44 |
show_callback=self.on_show |
| 45 |
) |
| 46 |
|
| 47 |
def set_menu_button(self, menu_button): |
| 48 |
|
| 49 |
super().set_menu_button(menu_button) |
| 50 |
|
| 51 |
if menu_button is not None and GTK_API_VERSION >= 4: |
| 52 |
inner_button = next(iter(menu_button)) |
| 53 |
add_css_class(widget=inner_button, css_class="flat") |
| 54 |
|
| 55 |
@staticmethod |
| 56 |
def update_transfer_limits(): |
| 57 |
raise NotImplementedError |
| 58 |
|
| 59 |
def on_active_limit_toggled(self, *_args): |
| 60 |
|
| 61 |
use_limit_config_key = f"use_{self.transfer_type}_speed_limit" |
| 62 |
prev_active_limit = config.sections["transfers"][use_limit_config_key] |
| 63 |
|
| 64 |
if self.use_limit_radio.get_active(): |
| 65 |
config.sections["transfers"][use_limit_config_key] = "primary" |
| 66 |
|
| 67 |
elif self.use_alt_limit_radio.get_active(): |
| 68 |
config.sections["transfers"][use_limit_config_key] = "alternative" |
| 69 |
|
| 70 |
else: |
| 71 |
config.sections["transfers"][use_limit_config_key] = "unlimited" |
| 72 |
|
| 73 |
if prev_active_limit != config.sections["transfers"][use_limit_config_key]: |
| 74 |
self.update_transfer_limits() |
| 75 |
|
| 76 |
def on_limit_changed(self, *_args): |
| 77 |
|
| 78 |
speed_limit = self.speed_spinner.get_value_as_int() |
| 79 |
|
| 80 |
if speed_limit == config.sections["transfers"][f"{self.transfer_type}limit"]: |
| 81 |
return |
| 82 |
|
| 83 |
config.sections["transfers"][f"{self.transfer_type}limit"] = speed_limit |
| 84 |
self.update_transfer_limits() |
| 85 |
|
| 86 |
def on_alt_limit_changed(self, *_args): |
| 87 |
|
| 88 |
alt_speed_limit = self.alt_speed_spinner.get_value_as_int() |
| 89 |
|
| 90 |
if alt_speed_limit == config.sections["transfers"][f"{self.transfer_type}limitalt"]: |
| 91 |
return |
| 92 |
|
| 93 |
config.sections["transfers"][f"{self.transfer_type}limitalt"] = alt_speed_limit |
| 94 |
self.update_transfer_limits() |
| 95 |
|
| 96 |
def on_show(self, *_args): |
| 97 |
|
| 98 |
self.alt_speed_spinner.set_value(config.sections["transfers"][f"{self.transfer_type}limitalt"]) |
| 99 |
self.speed_spinner.set_value(config.sections["transfers"][f"{self.transfer_type}limit"]) |
| 100 |
|
| 101 |
use_speed_limit = config.sections["transfers"][f"use_{self.transfer_type}_speed_limit"] |
| 102 |
|
| 103 |
if use_speed_limit == "primary": |
| 104 |
self.use_limit_radio.set_active(True) |
| 105 |
|
| 106 |
elif use_speed_limit == "alternative": |
| 107 |
self.use_alt_limit_radio.set_active(True) |
| 108 |
|
| 109 |
else: |
| 110 |
self.use_unlimited_speed_radio.set_active(True) |