Debian Codegraph @

pynicotine/gtkgui/dialogs/fileproperties.py

raw
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 pynicotine.core import core
20 from pynicotine.gtkgui.widgets import ui
21 from pynicotine.gtkgui.widgets.dialogs import Dialog
22 from pynicotine.slskmessages import FileListMessage
23 from pynicotine.utils import human_length
24 from pynicotine.utils import human_size
25 from pynicotine.utils import human_speed
26 from pynicotine.utils import humanize
27
28
29 class FileProperties(Dialog):
30
31 def __init__(self, application):
32
33 self.properties = {}
34 self.total_size = 0
35 self.total_length = 0
36 self.current_index = 0
37
38 (
39 self.container,
40 self.country_row,
41 self.country_value_label,
42 self.folder_value_label,
43 self.length_row,
44 self.length_value_label,
45 self.name_value_label,
46 self.next_button,
47 self.path_row,
48 self.path_value_label,
49 self.previous_button,
50 self.quality_row,
51 self.quality_value_label,
52 self.queue_row,
53 self.queue_value_label,
54 self.size_value_label,
55 self.speed_row,
56 self.speed_value_label,
57 self.username_value_label
58 ) = ui.load(scope=self, path="dialogs/fileproperties.ui")
59
60 super().__init__(
61 parent=application.window,
62 content_box=self.container,
63 buttons_start=(self.previous_button, self.next_button),
64 default_button=self.next_button,
65 title=_("File Properties"),
66 width=600
67 )
68
69 def update_title(self):
70
71 index = self.current_index + 1
72 total_files = len(self.properties)
73 total_size = human_size(self.total_size)
74
75 if self.total_length:
76 self.set_title(_("File Properties (%(num)i of %(total)i / %(size)s / %(length)s)") % {
77 "num": index, "total": total_files, "size": total_size,
78 "length": human_length(self.total_length)
79 })
80 return
81
82 self.set_title(_("File Properties (%(num)i of %(total)i / %(size)s)") % {
83 "num": index, "total": total_files, "size": total_size})
84
85 def update_current_file(self):
86 """Updates the UI with properties for the selected file."""
87
88 properties = self.properties[self.current_index]
89
90 for button in (self.previous_button, self.next_button):
91 button.set_visible(len(self.properties) > 1)
92
93 size = properties["size"]
94 h_size = human_size(size)
95
96 self.name_value_label.set_text(properties["basename"])
97 self.folder_value_label.set_text(properties["virtual_folder_path"])
98 self.size_value_label.set_text(f"{h_size} ({size} B)") # Don't humanize exact size for easier use in filter
99 self.username_value_label.set_text(properties["user"])
100
101 real_folder_path = properties.get("real_folder_path", "")
102 h_quality, _bitrate, h_length, _length = FileListMessage.parse_audio_quality_length(
103 size, properties.get("file_attributes"), always_show_bitrate=True)
104 queue_position = properties.get("queue_position", 0)
105 speed = properties.get("speed", 0)
106 country_code = properties.get("country_code")
107 country_name = core.network_filter.COUNTRIES.get(country_code)
108 country = f"{country_name} ({country_code})" if country_name else ""
109
110 self.path_value_label.set_text(real_folder_path)
111 self.path_row.set_visible(bool(real_folder_path))
112
113 self.quality_value_label.set_text(h_quality)
114 self.quality_row.set_visible(bool(h_quality))
115
116 self.length_value_label.set_text(h_length)
117 self.length_row.set_visible(bool(h_length))
118
119 self.queue_value_label.set_text(humanize(queue_position))
120 self.queue_row.set_visible(bool(queue_position))
121
122 self.speed_value_label.set_text(human_speed(speed))
123 self.speed_row.set_visible(bool(speed))
124
125 self.country_value_label.set_text(country)
126 self.country_row.set_visible(bool(country))
127
128 self.update_title()
129
130 def update_properties(self, properties, total_size=0, total_length=0):
131
132 self.properties = properties
133 self.total_size = total_size
134 self.total_length = total_length
135 self.current_index = 0
136
137 self.update_current_file()
138
139 def on_previous(self, *_args):
140
141 self.current_index -= 1
142
143 if self.current_index < 0:
144 self.current_index = len(self.properties) - 1
145
146 self.update_current_file()
147
148 def on_next(self, *_args):
149
150 self.current_index += 1
151
152 if self.current_index >= len(self.properties):
153 self.current_index = 0
154
155 self.update_current_file()