Skip to content

Commit

Permalink
Let the user configure network cache size via Network Options
Browse files Browse the repository at this point in the history
  • Loading branch information
zas committed Apr 9, 2024
1 parent 0a36d1e commit ff238e2
Show file tree
Hide file tree
Showing 5 changed files with 146 additions and 53 deletions.
3 changes: 2 additions & 1 deletion picard/const/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@
USER_PLUGIN_DIR = appdirs.plugin_folder()

# Network Cache default settings
CACHE_SIZE_IN_BYTES = 100*1000*1000
CACHE_SIZE_DISPLAY_UNIT = 1000*1000
CACHE_SIZE_IN_BYTES = 100*CACHE_SIZE_DISPLAY_UNIT

# AcoustID client API key
ACOUSTID_KEY = 'v8pQ6oyB'
Expand Down
26 changes: 26 additions & 0 deletions picard/ui/options/network.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
TextOption,
get_config,
)
from picard.const import (
CACHE_SIZE_DISPLAY_UNIT,
CACHE_SIZE_IN_BYTES,
)

from picard.ui.options import (
OptionsPage,
Expand Down Expand Up @@ -56,6 +60,7 @@ class NetworkOptionsPage(OptionsPage):
IntOption('setting', 'browser_integration_port', 8000),
BoolOption('setting', 'browser_integration_localhost_only', True),
IntOption('setting', 'network_transfer_timeout_seconds', 30),
IntOption('setting', 'network_cache_size_bytes', CACHE_SIZE_IN_BYTES),
]

def __init__(self, parent=None):
Expand All @@ -79,6 +84,7 @@ def load(self):
self.ui.browser_integration_port.setValue(config.setting['browser_integration_port'])
self.ui.browser_integration_localhost_only.setChecked(
config.setting['browser_integration_localhost_only'])
self.cachesize2display(config)

def save(self):
config = get_config()
Expand All @@ -100,6 +106,26 @@ def save(self):
config.setting['browser_integration_localhost_only'] = \
self.ui.browser_integration_localhost_only.isChecked()
self.tagger.update_browser_integration()
self.display2cachesize(config)

def display2cachesize(self, config):
try:
cache_size = int(self.ui.network_cache_size.text())
except ValueError:
return
if cache_size > 0:
config.setting['network_cache_size_bytes'] = int(cache_size * CACHE_SIZE_DISPLAY_UNIT)

def cachesize2display(self, config):
try:
cache_size = int(config.setting['network_cache_size_bytes'])
except ValueError:
cache_size = -1

value = int(cache_size / CACHE_SIZE_DISPLAY_UNIT)
if cache_size <= 0:
value = CACHE_SIZE_IN_BYTES
self.ui.network_cache_size.setText(str(value))


register_options_page(NetworkOptionsPage)
71 changes: 49 additions & 22 deletions picard/ui/ui_options_network.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Form implementation generated from reading ui file 'ui/options_network.ui'
#
# Created by: PyQt6 UI code generator 6.3.1
# Created by: PyQt6 UI code generator 6.5.3
#
# WARNING: Any manual changes made to this file will be lost when pyuic6 is
# run again. Do not edit this file unless you know what you are doing.
Expand All @@ -12,12 +12,12 @@
class Ui_NetworkOptionsPage(object):
def setupUi(self, NetworkOptionsPage):
NetworkOptionsPage.setObjectName("NetworkOptionsPage")
NetworkOptionsPage.resize(316, 371)
NetworkOptionsPage.resize(316, 491)
self.vboxlayout = QtWidgets.QVBoxLayout(NetworkOptionsPage)
self.vboxlayout.setContentsMargins(9, 9, 9, 9)
self.vboxlayout.setSpacing(6)
self.vboxlayout.setObjectName("vboxlayout")
self.web_proxy = QtWidgets.QGroupBox(NetworkOptionsPage)
self.web_proxy = QtWidgets.QGroupBox(parent=NetworkOptionsPage)
self.web_proxy.setCheckable(True)
self.web_proxy.setObjectName("web_proxy")
self.gridlayout = QtWidgets.QGridLayout(self.web_proxy)
Expand All @@ -26,85 +26,110 @@ def setupUi(self, NetworkOptionsPage):
self.gridlayout.setObjectName("gridlayout")
self.horizontalLayout_2 = QtWidgets.QHBoxLayout()
self.horizontalLayout_2.setObjectName("horizontalLayout_2")
self.proxy_type_http = QtWidgets.QRadioButton(self.web_proxy)
self.proxy_type_http = QtWidgets.QRadioButton(parent=self.web_proxy)
self.proxy_type_http.setChecked(True)
self.proxy_type_http.setObjectName("proxy_type_http")
self.horizontalLayout_2.addWidget(self.proxy_type_http)
self.proxy_type_socks = QtWidgets.QRadioButton(self.web_proxy)
self.proxy_type_socks = QtWidgets.QRadioButton(parent=self.web_proxy)
self.proxy_type_socks.setObjectName("proxy_type_socks")
self.horizontalLayout_2.addWidget(self.proxy_type_socks)
spacerItem = QtWidgets.QSpacerItem(40, 20, QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Minimum)
self.horizontalLayout_2.addItem(spacerItem)
self.gridlayout.addLayout(self.horizontalLayout_2, 0, 0, 1, 1)
self.server_host = QtWidgets.QLineEdit(self.web_proxy)
self.server_host = QtWidgets.QLineEdit(parent=self.web_proxy)
self.server_host.setObjectName("server_host")
self.gridlayout.addWidget(self.server_host, 5, 0, 1, 1)
self.username = QtWidgets.QLineEdit(self.web_proxy)
self.username = QtWidgets.QLineEdit(parent=self.web_proxy)
self.username.setObjectName("username")
self.gridlayout.addWidget(self.username, 7, 0, 1, 2)
self.password = QtWidgets.QLineEdit(self.web_proxy)
self.password = QtWidgets.QLineEdit(parent=self.web_proxy)
self.password.setEchoMode(QtWidgets.QLineEdit.EchoMode.Password)
self.password.setObjectName("password")
self.gridlayout.addWidget(self.password, 9, 0, 1, 2)
self.server_port = QtWidgets.QSpinBox(self.web_proxy)
self.server_port = QtWidgets.QSpinBox(parent=self.web_proxy)
self.server_port.setMinimum(1)
self.server_port.setMaximum(65535)
self.server_port.setProperty("value", 80)
self.server_port.setObjectName("server_port")
self.gridlayout.addWidget(self.server_port, 5, 1, 1, 1)
self.label_6 = QtWidgets.QLabel(self.web_proxy)
self.label_6 = QtWidgets.QLabel(parent=self.web_proxy)
self.label_6.setObjectName("label_6")
self.gridlayout.addWidget(self.label_6, 6, 0, 1, 2)
self.label_7 = QtWidgets.QLabel(self.web_proxy)
self.label_7 = QtWidgets.QLabel(parent=self.web_proxy)
self.label_7.setObjectName("label_7")
self.gridlayout.addWidget(self.label_7, 4, 1, 1, 1)
self.label_5 = QtWidgets.QLabel(self.web_proxy)
self.label_5 = QtWidgets.QLabel(parent=self.web_proxy)
self.label_5.setObjectName("label_5")
self.gridlayout.addWidget(self.label_5, 8, 0, 1, 2)
self.label = QtWidgets.QLabel(self.web_proxy)
self.label = QtWidgets.QLabel(parent=self.web_proxy)
self.label.setObjectName("label")
self.gridlayout.addWidget(self.label, 4, 0, 1, 1)
self.vboxlayout.addWidget(self.web_proxy)
self.networkopts = QtWidgets.QGroupBox(parent=NetworkOptionsPage)
self.networkopts.setObjectName("networkopts")
self.verticalLayout_5 = QtWidgets.QVBoxLayout(self.networkopts)
self.verticalLayout_5.setObjectName("verticalLayout_5")
self.horizontalLayout_3 = QtWidgets.QHBoxLayout()
self.horizontalLayout_3.setObjectName("horizontalLayout_3")
self.label_3 = QtWidgets.QLabel(NetworkOptionsPage)
self.label_3 = QtWidgets.QLabel(parent=self.networkopts)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_3.sizePolicy().hasHeightForWidth())
self.label_3.setSizePolicy(sizePolicy)
self.label_3.setObjectName("label_3")
self.horizontalLayout_3.addWidget(self.label_3)
self.transfer_timeout = QtWidgets.QSpinBox(NetworkOptionsPage)
self.transfer_timeout = QtWidgets.QSpinBox(parent=self.networkopts)
self.transfer_timeout.setMaximum(900)
self.transfer_timeout.setProperty("value", 30)
self.transfer_timeout.setObjectName("transfer_timeout")
self.horizontalLayout_3.addWidget(self.transfer_timeout)
self.vboxlayout.addLayout(self.horizontalLayout_3)
self.browser_integration = QtWidgets.QGroupBox(NetworkOptionsPage)
self.verticalLayout_5.addLayout(self.horizontalLayout_3)
self.horizontalLayout = QtWidgets.QHBoxLayout()
self.horizontalLayout.setObjectName("horizontalLayout")
self.label_cache_size = QtWidgets.QLabel(parent=self.networkopts)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_cache_size.sizePolicy().hasHeightForWidth())
self.label_cache_size.setSizePolicy(sizePolicy)
self.label_cache_size.setObjectName("label_cache_size")
self.horizontalLayout.addWidget(self.label_cache_size, 0, QtCore.Qt.AlignmentFlag.AlignLeft|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.network_cache_size = QtWidgets.QLineEdit(parent=self.networkopts)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Minimum, QtWidgets.QSizePolicy.Policy.Fixed)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.network_cache_size.sizePolicy().hasHeightForWidth())
self.network_cache_size.setSizePolicy(sizePolicy)
self.network_cache_size.setAlignment(QtCore.Qt.AlignmentFlag.AlignRight|QtCore.Qt.AlignmentFlag.AlignTrailing|QtCore.Qt.AlignmentFlag.AlignVCenter)
self.network_cache_size.setObjectName("network_cache_size")
self.horizontalLayout.addWidget(self.network_cache_size)
self.verticalLayout_5.addLayout(self.horizontalLayout)
self.vboxlayout.addWidget(self.networkopts, 0, QtCore.Qt.AlignmentFlag.AlignVCenter)
self.browser_integration = QtWidgets.QGroupBox(parent=NetworkOptionsPage)
self.browser_integration.setCheckable(True)
self.browser_integration.setChecked(True)
self.browser_integration.setObjectName("browser_integration")
self.verticalLayout_2 = QtWidgets.QVBoxLayout(self.browser_integration)
self.verticalLayout_2.setObjectName("verticalLayout_2")
self.horizontalLayout_4 = QtWidgets.QHBoxLayout()
self.horizontalLayout_4.setObjectName("horizontalLayout_4")
self.label_2 = QtWidgets.QLabel(self.browser_integration)
self.label_2 = QtWidgets.QLabel(parent=self.browser_integration)
sizePolicy = QtWidgets.QSizePolicy(QtWidgets.QSizePolicy.Policy.Expanding, QtWidgets.QSizePolicy.Policy.Preferred)
sizePolicy.setHorizontalStretch(0)
sizePolicy.setVerticalStretch(0)
sizePolicy.setHeightForWidth(self.label_2.sizePolicy().hasHeightForWidth())
self.label_2.setSizePolicy(sizePolicy)
self.label_2.setObjectName("label_2")
self.horizontalLayout_4.addWidget(self.label_2)
self.browser_integration_port = QtWidgets.QSpinBox(self.browser_integration)
self.browser_integration_port = QtWidgets.QSpinBox(parent=self.browser_integration)
self.browser_integration_port.setMinimum(1)
self.browser_integration_port.setMaximum(65535)
self.browser_integration_port.setProperty("value", 8000)
self.browser_integration_port.setObjectName("browser_integration_port")
self.horizontalLayout_4.addWidget(self.browser_integration_port)
self.verticalLayout_2.addLayout(self.horizontalLayout_4)
self.browser_integration_localhost_only = QtWidgets.QCheckBox(self.browser_integration)
self.browser_integration_localhost_only = QtWidgets.QCheckBox(parent=self.browser_integration)
self.browser_integration_localhost_only.setChecked(False)
self.browser_integration_localhost_only.setObjectName("browser_integration_localhost_only")
self.verticalLayout_2.addWidget(self.browser_integration_localhost_only)
Expand All @@ -123,8 +148,8 @@ def setupUi(self, NetworkOptionsPage):
NetworkOptionsPage.setTabOrder(self.server_host, self.server_port)
NetworkOptionsPage.setTabOrder(self.server_port, self.username)
NetworkOptionsPage.setTabOrder(self.username, self.password)
NetworkOptionsPage.setTabOrder(self.password, self.transfer_timeout)
NetworkOptionsPage.setTabOrder(self.transfer_timeout, self.browser_integration)
NetworkOptionsPage.setTabOrder(self.password, self.network_cache_size)
NetworkOptionsPage.setTabOrder(self.network_cache_size, self.browser_integration)
NetworkOptionsPage.setTabOrder(self.browser_integration, self.browser_integration_port)
NetworkOptionsPage.setTabOrder(self.browser_integration_port, self.browser_integration_localhost_only)

Expand All @@ -137,7 +162,9 @@ def retranslateUi(self, NetworkOptionsPage):
self.label_7.setText(_("Port:"))
self.label_5.setText(_("Password:"))
self.label.setText(_("Server address:"))
self.networkopts.setTitle(_("Network options"))
self.label_3.setText(_("Request timeout in seconds:"))
self.label_cache_size.setText(_("Cache size (MB):"))
self.browser_integration.setTitle(_("Browser Integration"))
self.label_2.setText(_("Default listening port:"))
self.browser_integration_localhost_only.setText(_("Listen only on localhost"))
4 changes: 2 additions & 2 deletions picard/webservice/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,9 @@ def __init__(self, parent=None):
self.manager = QtNetwork.QNetworkAccessManager()
self.manager.sslErrors.connect(self.ssl_errors)
self.oauth_manager = OAuthManager(self)
self.set_cache()
self.setup_proxy()
config = get_config()
self.set_cache(cache_size_in_bytes=config.setting['network_cache_size_bytes'])
self.setup_proxy()
self.set_transfer_timeout(config.setting['network_transfer_timeout_seconds'])
self.manager.finished.connect(self._process_reply)
self._request_methods = {
Expand Down
95 changes: 67 additions & 28 deletions ui/options_network.ui
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<x>0</x>
<y>0</y>
<width>316</width>
<height>371</height>
<height>491</height>
</rect>
</property>
<layout class="QVBoxLayout">
Expand Down Expand Up @@ -150,32 +150,71 @@
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Request timeout in seconds:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="transfer_timeout">
<property name="maximum">
<number>900</number>
</property>
<property name="value">
<number>30</number>
</property>
</widget>
</item>
</layout>
<item alignment="Qt::AlignVCenter">
<widget class="QGroupBox" name="networkopts">
<property name="title">
<string>Network options</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_3">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Request timeout in seconds:</string>
</property>
</widget>
</item>
<item>
<widget class="QSpinBox" name="transfer_timeout">
<property name="maximum">
<number>900</number>
</property>
<property name="value">
<number>30</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout">
<item alignment="Qt::AlignLeft|Qt::AlignVCenter">
<widget class="QLabel" name="label_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Cache size (MB):</string>
</property>
</widget>
</item>
<item>
<widget class="QLineEdit" name="network_cache_size">
<property name="sizePolicy">
<sizepolicy hsizetype="Minimum" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
</layout>
</item>
</layout>
</widget>
</item>
<item>
<widget class="QGroupBox" name="browser_integration">
Expand Down Expand Up @@ -255,7 +294,7 @@
<tabstop>server_port</tabstop>
<tabstop>username</tabstop>
<tabstop>password</tabstop>
<tabstop>transfer_timeout</tabstop>
<tabstop>network_cache_size</tabstop>
<tabstop>browser_integration</tabstop>
<tabstop>browser_integration_port</tabstop>
<tabstop>browser_integration_localhost_only</tabstop>
Expand Down

0 comments on commit ff238e2

Please sign in to comment.