| 1 |
# COPYRIGHT (C) 2020-2024 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 gi.repository import GObject |
| 20 |
from gi.repository import Gtk |
| 21 |
|
| 22 |
from pynicotine.config import config |
| 23 |
from pynicotine.core import core |
| 24 |
from pynicotine.gtkgui.application import GTK_API_VERSION |
| 25 |
from pynicotine.gtkgui.widgets.combobox import ComboBox |
| 26 |
from pynicotine.gtkgui.widgets.dialogs import Dialog |
| 27 |
from pynicotine.gtkgui.widgets.dialogs import EntryDialog |
| 28 |
from pynicotine.gtkgui.widgets.filechooser import FileChooserButton |
| 29 |
from pynicotine.gtkgui.widgets.textview import TextView |
| 30 |
from pynicotine.gtkgui.widgets.theme import add_css_class |
| 31 |
|
| 32 |
|
| 33 |
class PluginSettings(Dialog): |
| 34 |
|
| 35 |
def __init__(self, application): |
| 36 |
|
| 37 |
self.application = application |
| 38 |
self.plugin_id = None |
| 39 |
self.plugin_settings = None |
| 40 |
self.option_widgets = {} |
| 41 |
|
| 42 |
cancel_button = Gtk.Button(label=_("_Cancel"), use_underline=True, visible=True) |
| 43 |
cancel_button.connect("clicked", self.on_cancel) |
| 44 |
|
| 45 |
ok_button = Gtk.Button(label=_("_Apply"), use_underline=True, visible=True) |
| 46 |
ok_button.connect("clicked", self.on_ok) |
| 47 |
add_css_class(ok_button, "suggested-action") |
| 48 |
|
| 49 |
self.primary_container = Gtk.Box( |
| 50 |
orientation=Gtk.Orientation.VERTICAL, width_request=340, visible=True, |
| 51 |
margin_top=14, margin_bottom=14, margin_start=18, margin_end=18, spacing=12 |
| 52 |
) |
| 53 |
self.scrolled_window = Gtk.ScrolledWindow( |
| 54 |
child=self.primary_container, hexpand=True, vexpand=True, min_content_height=300, |
| 55 |
hscrollbar_policy=Gtk.PolicyType.NEVER, vscrollbar_policy=Gtk.PolicyType.AUTOMATIC, visible=True |
| 56 |
) |
| 57 |
|
| 58 |
super().__init__( |
| 59 |
parent=application.preferences, |
| 60 |
content_box=self.scrolled_window, |
| 61 |
buttons_start=(cancel_button,), |
| 62 |
buttons_end=(ok_button,), |
| 63 |
default_button=ok_button, |
| 64 |
close_callback=self.on_close, |
| 65 |
width=600, |
| 66 |
height=425, |
| 67 |
show_title_buttons=False |
| 68 |
) |
| 69 |
|
| 70 |
def destroy(self): |
| 71 |
self.__dict__.clear() |
| 72 |
|
| 73 |
@staticmethod |
| 74 |
def _generate_label(text): |
| 75 |
return Gtk.Label(label=text, hexpand=True, wrap=True, xalign=0, visible=bool(text)) |
| 76 |
|
| 77 |
def _generate_widget_container(self, description, child_widget=None, homogeneous=False, |
| 78 |
orientation=Gtk.Orientation.HORIZONTAL): |
| 79 |
|
| 80 |
container = Gtk.Box(homogeneous=homogeneous, orientation=orientation, spacing=12, visible=True) |
| 81 |
label = self._generate_label(description) |
| 82 |
|
| 83 |
if GTK_API_VERSION >= 4: |
| 84 |
container.append(label) # pylint: disable=no-member |
| 85 |
self.primary_container.append(container) # pylint: disable=no-member |
| 86 |
|
| 87 |
if child_widget: |
| 88 |
container.append(child_widget) # pylint: disable=no-member |
| 89 |
else: |
| 90 |
container.add(label) # pylint: disable=no-member |
| 91 |
self.primary_container.add(container) # pylint: disable=no-member |
| 92 |
|
| 93 |
if child_widget: |
| 94 |
container.add(child_widget) # pylint: disable=no-member |
| 95 |
|
| 96 |
return label |
| 97 |
|
| 98 |
def _add_numerical_option(self, option_name, option_value, description, minimum, maximum, stepsize, decimals): |
| 99 |
|
| 100 |
self.option_widgets[option_name] = button = Gtk.SpinButton( |
| 101 |
adjustment=Gtk.Adjustment( |
| 102 |
value=0, lower=minimum, upper=maximum, step_increment=stepsize, page_increment=10, |
| 103 |
page_size=0 |
| 104 |
), |
| 105 |
climb_rate=1, digits=decimals, valign=Gtk.Align.CENTER, visible=True |
| 106 |
) |
| 107 |
|
| 108 |
label = self._generate_widget_container(description, button) |
| 109 |
label.set_mnemonic_widget(button) |
| 110 |
self.application.preferences.set_widget(button, option_value) |
| 111 |
|
| 112 |
def _add_boolean_option(self, option_name, option_value, description): |
| 113 |
|
| 114 |
self.option_widgets[option_name] = button = Gtk.Switch( |
| 115 |
receives_default=True, valign=Gtk.Align.CENTER, visible=True |
| 116 |
) |
| 117 |
|
| 118 |
label = self._generate_widget_container(description, button) |
| 119 |
label.set_mnemonic_widget(button) |
| 120 |
|
| 121 |
self.application.preferences.set_widget(button, option_value) |
| 122 |
|
| 123 |
def _add_radio_option(self, option_name, option_value, description, items): |
| 124 |
|
| 125 |
box = Gtk.Box(spacing=6, orientation=Gtk.Orientation.VERTICAL, visible=True) |
| 126 |
label = self._generate_widget_container(description, box) |
| 127 |
|
| 128 |
last_radio = None |
| 129 |
group_radios = [] |
| 130 |
|
| 131 |
for option_label in items: |
| 132 |
widget_class = Gtk.CheckButton if GTK_API_VERSION >= 4 else Gtk.RadioButton |
| 133 |
radio = widget_class(group=last_radio, label=option_label, receives_default=True, visible=True) |
| 134 |
|
| 135 |
if not last_radio: |
| 136 |
self.option_widgets[option_name] = radio |
| 137 |
|
| 138 |
last_radio = radio |
| 139 |
group_radios.append(radio) |
| 140 |
|
| 141 |
if GTK_API_VERSION >= 4: |
| 142 |
box.append(radio) # pylint: disable=no-member |
| 143 |
else: |
| 144 |
box.add(radio) # pylint: disable=no-member |
| 145 |
|
| 146 |
label.set_mnemonic_widget(self.option_widgets[option_name]) |
| 147 |
self.option_widgets[option_name].group_radios = group_radios |
| 148 |
self.application.preferences.set_widget(self.option_widgets[option_name], option_value) |
| 149 |
|
| 150 |
def _add_dropdown_option(self, option_name, option_value, description, items): |
| 151 |
|
| 152 |
label = self._generate_widget_container(description, homogeneous=True) |
| 153 |
self.option_widgets[option_name] = combobox = ComboBox( |
| 154 |
container=label.get_parent(), label=label, items=items |
| 155 |
) |
| 156 |
self.application.preferences.set_widget(combobox, option_value) |
| 157 |
|
| 158 |
def _add_entry_option(self, option_name, option_value, description): |
| 159 |
|
| 160 |
self.option_widgets[option_name] = entry = Gtk.Entry(hexpand=True, valign=Gtk.Align.CENTER, |
| 161 |
visible=True) |
| 162 |
label = self._generate_widget_container(description, entry, homogeneous=True) |
| 163 |
label.set_mnemonic_widget(entry) |
| 164 |
|
| 165 |
self.application.preferences.set_widget(entry, option_value) |
| 166 |
|
| 167 |
def _add_textview_option(self, option_name, option_value, description): |
| 168 |
|
| 169 |
box = Gtk.Box(visible=True) |
| 170 |
scrolled_window = Gtk.ScrolledWindow(hexpand=True, vexpand=True, min_content_height=125, |
| 171 |
visible=True) |
| 172 |
frame_container = Gtk.Frame(child=box, visible=True) |
| 173 |
|
| 174 |
if GTK_API_VERSION >= 4: |
| 175 |
box.append(scrolled_window) # pylint: disable=no-member |
| 176 |
else: |
| 177 |
box.add(scrolled_window) # pylint: disable=no-member |
| 178 |
|
| 179 |
self.option_widgets[option_name] = textview = TextView(scrolled_window) |
| 180 |
label = self._generate_widget_container(description, frame_container, orientation=Gtk.Orientation.VERTICAL) |
| 181 |
label.set_mnemonic_widget(textview.widget) |
| 182 |
self.application.preferences.set_widget(textview, option_value) |
| 183 |
|
| 184 |
def _add_list_option(self, option_name, option_value, description): |
| 185 |
|
| 186 |
scrolled_window = Gtk.ScrolledWindow( |
| 187 |
hexpand=True, vexpand=True, min_content_height=125, |
| 188 |
hscrollbar_policy=Gtk.PolicyType.AUTOMATIC, vscrollbar_policy=Gtk.PolicyType.AUTOMATIC, visible=True |
| 189 |
) |
| 190 |
container = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, visible=True) |
| 191 |
frame_container = Gtk.Frame(child=container, margin_top=6, visible=True) |
| 192 |
add_css_class(scrolled_window, "border-bottom") |
| 193 |
|
| 194 |
from pynicotine.gtkgui.widgets.treeview import TreeView |
| 195 |
self.option_widgets[option_name] = treeview = TreeView( |
| 196 |
self.application.window, parent=scrolled_window, activate_row_callback=self.on_row_activated, |
| 197 |
delete_accelerator_callback=self.on_delete_accelerator, |
| 198 |
columns={ |
| 199 |
# Visible columns |
| 200 |
"description": { |
| 201 |
"column_type": "text", |
| 202 |
"title": description |
| 203 |
}, |
| 204 |
|
| 205 |
# Hidden data columns |
| 206 |
"id_data": { |
| 207 |
"data_type": GObject.TYPE_INT, |
| 208 |
"default_sort_type": "ascending", |
| 209 |
"iterator_key": True |
| 210 |
} |
| 211 |
} |
| 212 |
) |
| 213 |
rows = [[item, index] for index, item in enumerate(option_value)] |
| 214 |
treeview.description = description |
| 215 |
treeview.row_id = len(rows) |
| 216 |
self.application.preferences.set_widget(treeview, rows) |
| 217 |
|
| 218 |
button_container = Gtk.Box(margin_end=6, margin_bottom=6, margin_start=6, margin_top=6, |
| 219 |
spacing=6, visible=True) |
| 220 |
|
| 221 |
for icon_name, label_text, callback in ( |
| 222 |
("list-add-symbolic", _("Add…"), self.on_add), |
| 223 |
("document-edit-symbolic", _("Edit…"), self.on_edit), |
| 224 |
("list-remove-symbolic", _("Remove"), self.on_remove) |
| 225 |
): |
| 226 |
button = Gtk.Button(visible=True) |
| 227 |
label_container = Gtk.Box(spacing=6, visible=True) |
| 228 |
icon = Gtk.Image(icon_name=icon_name, visible=True) |
| 229 |
label = Gtk.Label(label=label_text, mnemonic_widget=button, visible=True) |
| 230 |
|
| 231 |
button.connect("clicked", callback, treeview) |
| 232 |
add_css_class(button, "flat") |
| 233 |
|
| 234 |
if GTK_API_VERSION >= 4: |
| 235 |
button.set_child(label_container) # pylint: disable=no-member |
| 236 |
label_container.append(icon) # pylint: disable=no-member |
| 237 |
label_container.append(label) # pylint: disable=no-member |
| 238 |
button_container.append(button) # pylint: disable=no-member |
| 239 |
else: |
| 240 |
button.add(label_container) # pylint: disable=no-member |
| 241 |
label_container.add(icon) # pylint: disable=no-member |
| 242 |
label_container.add(label) # pylint: disable=no-member |
| 243 |
button_container.add(button) # pylint: disable=no-member |
| 244 |
|
| 245 |
if GTK_API_VERSION >= 4: |
| 246 |
self.primary_container.append(frame_container) # pylint: disable=no-member |
| 247 |
|
| 248 |
container.append(scrolled_window) # pylint: disable=no-member |
| 249 |
container.append(button_container) # pylint: disable=no-member |
| 250 |
else: |
| 251 |
self.primary_container.add(frame_container) # pylint: disable=no-member |
| 252 |
|
| 253 |
container.add(scrolled_window) # pylint: disable=no-member |
| 254 |
container.add(button_container) # pylint: disable=no-member |
| 255 |
|
| 256 |
def _add_file_option(self, option_name, option_value, description, file_chooser_type): |
| 257 |
|
| 258 |
container = Gtk.Box(visible=True) |
| 259 |
label = self._generate_widget_container(description, container, homogeneous=True) |
| 260 |
|
| 261 |
self.option_widgets[option_name] = FileChooserButton( |
| 262 |
container, window=self, label=label, chooser_type=file_chooser_type, |
| 263 |
show_open_external_button=not self.application.isolated_mode |
| 264 |
) |
| 265 |
self.application.preferences.set_widget(self.option_widgets[option_name], option_value) |
| 266 |
|
| 267 |
def _add_options(self): |
| 268 |
|
| 269 |
self.option_widgets.clear() |
| 270 |
|
| 271 |
for child in list(self.primary_container): |
| 272 |
self.primary_container.remove(child) |
| 273 |
|
| 274 |
for option_name, data in self.plugin_settings.items(): |
| 275 |
option_type = data.get("type") |
| 276 |
|
| 277 |
if not option_type: |
| 278 |
continue |
| 279 |
|
| 280 |
description = data.get("description", "") |
| 281 |
option_value = config.sections["plugins"][self.plugin_id.lower()][option_name] |
| 282 |
|
| 283 |
if option_type in {"integer", "int", "float"}: |
| 284 |
self._add_numerical_option( |
| 285 |
option_name, option_value, description, minimum=data.get("minimum", 0), |
| 286 |
maximum=data.get("maximum", 99999), stepsize=data.get("stepsize", 1), |
| 287 |
decimals=(0 if option_type in {"integer", "int"} else 2) |
| 288 |
) |
| 289 |
|
| 290 |
elif option_type == "bool": |
| 291 |
self._add_boolean_option(option_name, option_value, description) |
| 292 |
|
| 293 |
elif option_type == "radio": |
| 294 |
self._add_radio_option( |
| 295 |
option_name, option_value, description, items=data.get("options", [])) |
| 296 |
|
| 297 |
elif option_type == "dropdown": |
| 298 |
self._add_dropdown_option( |
| 299 |
option_name, option_value, description, items=data.get("options", [])) |
| 300 |
|
| 301 |
elif option_type in {"str", "string"}: |
| 302 |
self._add_entry_option(option_name, option_value, description) |
| 303 |
|
| 304 |
elif option_type == "textview": |
| 305 |
self._add_textview_option(option_name, option_value, description) |
| 306 |
|
| 307 |
elif option_type == "list string": |
| 308 |
self._add_list_option(option_name, option_value, description) |
| 309 |
|
| 310 |
elif option_type == "file": |
| 311 |
self._add_file_option( |
| 312 |
option_name, option_value, description, file_chooser_type=data.get("chooser")) |
| 313 |
|
| 314 |
@staticmethod |
| 315 |
def _get_widget_data(widget): |
| 316 |
|
| 317 |
if isinstance(widget, Gtk.SpinButton): |
| 318 |
if widget.get_digits() > 0: |
| 319 |
return widget.get_value() |
| 320 |
|
| 321 |
return widget.get_value_as_int() |
| 322 |
|
| 323 |
if isinstance(widget, Gtk.Entry): |
| 324 |
return widget.get_text() |
| 325 |
|
| 326 |
if isinstance(widget, TextView): |
| 327 |
return widget.get_text() |
| 328 |
|
| 329 |
if isinstance(widget, Gtk.Switch): |
| 330 |
return widget.get_active() |
| 331 |
|
| 332 |
if isinstance(widget, Gtk.CheckButton): |
| 333 |
try: |
| 334 |
# Radio button |
| 335 |
for radio in widget.group_radios: |
| 336 |
if radio.get_active(): |
| 337 |
return widget.group_radios.index(radio) |
| 338 |
|
| 339 |
return 0 |
| 340 |
|
| 341 |
except (AttributeError, TypeError): |
| 342 |
# Regular check button |
| 343 |
return widget.get_active() |
| 344 |
|
| 345 |
if isinstance(widget, ComboBox): |
| 346 |
return widget.get_selected_id() |
| 347 |
|
| 348 |
from pynicotine.gtkgui.widgets.treeview import TreeView |
| 349 |
if isinstance(widget, TreeView): |
| 350 |
return [ |
| 351 |
widget.get_row_value(iterator, "description") |
| 352 |
for row_id, iterator in sorted(widget.iterators.items()) |
| 353 |
] |
| 354 |
|
| 355 |
if isinstance(widget, FileChooserButton): |
| 356 |
return widget.get_path(dynamic=False) |
| 357 |
|
| 358 |
return None |
| 359 |
|
| 360 |
def update_settings(self, plugin_id, plugin_settings): |
| 361 |
|
| 362 |
self.plugin_id = plugin_id |
| 363 |
self.plugin_settings = plugin_settings |
| 364 |
plugin_name = core.pluginhandler.get_plugin_info(plugin_id).get("Name", plugin_id) |
| 365 |
|
| 366 |
self.set_title(_("%s Settings") % plugin_name) |
| 367 |
self._add_options() |
| 368 |
|
| 369 |
def on_add_response(self, window, _response_id, treeview): |
| 370 |
|
| 371 |
value = window.get_entry_value() |
| 372 |
|
| 373 |
if not value: |
| 374 |
return |
| 375 |
|
| 376 |
treeview.row_id += 1 |
| 377 |
treeview.add_row([value, treeview.row_id]) |
| 378 |
|
| 379 |
def on_add(self, _button, treeview): |
| 380 |
|
| 381 |
EntryDialog( |
| 382 |
parent=self, |
| 383 |
title=_("Add Item"), |
| 384 |
message=treeview.description, |
| 385 |
action_button_label=_("_Add"), |
| 386 |
callback=self.on_add_response, |
| 387 |
callback_data=treeview |
| 388 |
).present() |
| 389 |
|
| 390 |
def on_edit_response(self, window, _response_id, data): |
| 391 |
|
| 392 |
value = window.get_entry_value() |
| 393 |
|
| 394 |
if not value: |
| 395 |
return |
| 396 |
|
| 397 |
treeview, row_id = data |
| 398 |
iterator = treeview.iterators[row_id] |
| 399 |
|
| 400 |
treeview.remove_row(iterator) |
| 401 |
treeview.add_row([value, row_id]) |
| 402 |
|
| 403 |
def on_edit(self, _button=None, treeview=None): |
| 404 |
|
| 405 |
for iterator in treeview.get_selected_rows(): |
| 406 |
value = treeview.get_row_value(iterator, "description") or "" |
| 407 |
row_id = treeview.get_row_value(iterator, "id_data") |
| 408 |
|
| 409 |
EntryDialog( |
| 410 |
parent=self, |
| 411 |
title=_("Edit Item"), |
| 412 |
message=treeview.description, |
| 413 |
action_button_label=_("_Edit"), |
| 414 |
callback=self.on_edit_response, |
| 415 |
callback_data=(treeview, row_id), |
| 416 |
default=value |
| 417 |
).present() |
| 418 |
return |
| 419 |
|
| 420 |
def on_remove(self, _button=None, treeview=None): |
| 421 |
for iterator in reversed(list(treeview.get_selected_rows())): |
| 422 |
row_id = treeview.get_row_value(iterator, "id_data") |
| 423 |
orig_iterator = treeview.iterators[row_id] |
| 424 |
|
| 425 |
treeview.remove_row(orig_iterator) |
| 426 |
|
| 427 |
def on_row_activated(self, treeview, *_args): |
| 428 |
self.on_edit(treeview=treeview) |
| 429 |
|
| 430 |
def on_delete_accelerator(self, treeview): |
| 431 |
self.on_remove(treeview=treeview) |
| 432 |
|
| 433 |
def on_cancel(self, *_args): |
| 434 |
self.close() |
| 435 |
|
| 436 |
def on_ok(self, *_args): |
| 437 |
|
| 438 |
plugin = core.pluginhandler.enabled_plugins[self.plugin_id] |
| 439 |
|
| 440 |
for name in self.plugin_settings: |
| 441 |
value = self._get_widget_data(self.option_widgets[name]) |
| 442 |
|
| 443 |
if value is not None: |
| 444 |
plugin.settings[name] = value |
| 445 |
|
| 446 |
self.close() |
| 447 |
|
| 448 |
def on_close(self, *_args): |
| 449 |
self.scrolled_window.get_vadjustment().set_value(0) |