Debian Codegraph @

pynicotine/gtkgui/widgets/ui.py

raw
1 # COPYRIGHT (C) 2020-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 import os
20
21 from gi.repository import Gtk
22
23 from pynicotine.gtkgui.application import GTK_API_VERSION
24 from pynicotine.gtkgui.application import GTK_GUI_FOLDER_PATH
25 from pynicotine.gtkgui.application import GTK_MINOR_VERSION
26 from pynicotine.utils import encode_path
27
28
29 # UI Builder #
30
31
32 ui_data = {}
33
34
35 def load(scope, path):
36
37 if path not in ui_data:
38 with open(encode_path(os.path.join(GTK_GUI_FOLDER_PATH, "ui", path)), encoding="utf-8") as file_handle:
39 ui_content = file_handle.read()
40
41 # Translate UI strings using Python's gettext
42 start_tag = ' translatable="yes">'
43 end_tag = "</property>"
44 start_tag_len = len(start_tag)
45 tag_start_pos = ui_content.find(start_tag)
46
47 while tag_start_pos > -1:
48 string_start_pos = (tag_start_pos + start_tag_len)
49 string_end_pos = ui_content.find(end_tag, string_start_pos)
50
51 original_string = ui_content[string_start_pos:string_end_pos]
52 translated_string = _(original_string)
53
54 if original_string != translated_string:
55 ui_content = ui_content[:string_start_pos] + translated_string + ui_content[string_end_pos:]
56
57 # Find next translatable string
58 new_string_end_pos = (string_end_pos + (len(translated_string) - len(original_string)))
59 tag_start_pos = ui_content.find(start_tag, new_string_end_pos)
60
61 # GTK 4 replacements
62 if GTK_API_VERSION >= 4:
63 ui_content = (
64 ui_content
65 .replace("GtkRadioButton", "GtkCheckButton")
66 .replace('"can-focus"', '"focusable"'))
67
68 if GTK_MINOR_VERSION >= 10:
69 ui_content = (
70 ui_content
71 .replace("GtkColorButton", "GtkColorDialogButton")
72 .replace("GtkFontButton", "GtkFontDialogButton"))
73
74 ui_data[path] = ui_content
75
76 if GTK_API_VERSION >= 4:
77 builder = Gtk.Builder(scope)
78 builder.add_from_string(ui_data[path])
79 Gtk.Buildable.get_name = Gtk.Buildable.get_buildable_id # pylint: disable=no-member
80 else:
81 builder = Gtk.Builder()
82 builder.add_from_string(ui_data[path])
83 builder.connect_signals(scope) # pylint: disable=no-member
84
85 widgets = builder.get_objects()
86
87 for obj in list(widgets):
88 try:
89 obj_name = Gtk.Buildable.get_name(obj)
90 if not obj_name.startswith("_"):
91 continue
92
93 except TypeError:
94 pass
95
96 widgets.remove(obj)
97
98 widgets.sort(key=Gtk.Buildable.get_name)
99 return widgets