Debian Codegraph @

pynicotine/gtkgui/dialogs/statistics.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 import time
20
21 from pynicotine.core import core
22 from pynicotine.events import events
23 from pynicotine.gtkgui.widgets import ui
24 from pynicotine.gtkgui.widgets.dialogs import Dialog
25 from pynicotine.gtkgui.widgets.dialogs import OptionDialog
26 from pynicotine.utils import human_size
27 from pynicotine.utils import humanize
28
29
30 class Statistics(Dialog):
31
32 def __init__(self, application):
33
34 (
35 self.completed_downloads_session_label,
36 self.completed_downloads_total_label,
37 self.completed_uploads_session_label,
38 self.completed_uploads_total_label,
39 self.container,
40 self.current_session_label,
41 self.downloaded_size_session_label,
42 self.downloaded_size_total_label,
43 self.reset_button,
44 self.since_timestamp_total_label,
45 self.uploaded_size_session_label,
46 self.uploaded_size_total_label
47 ) = ui.load(scope=self, path="dialogs/statistics.ui")
48
49 self.stat_id_labels = {
50 "completed_downloads": {
51 "session": self.completed_downloads_session_label,
52 "total": self.completed_downloads_total_label
53 },
54 "completed_uploads": {
55 "session": self.completed_uploads_session_label,
56 "total": self.completed_uploads_total_label
57 },
58 "downloaded_size": {
59 "session": self.downloaded_size_session_label,
60 "total": self.downloaded_size_total_label
61 },
62 "uploaded_size": {
63 "session": self.uploaded_size_session_label,
64 "total": self.uploaded_size_total_label
65 },
66 "since_timestamp": {
67 "total": self.since_timestamp_total_label
68 }
69 }
70
71 super().__init__(
72 parent=application.window,
73 content_box=self.container,
74 show_callback=self.on_show,
75 title=_("Transfer Statistics"),
76 width=425
77 )
78
79 events.connect("update-stat", self.update_stat)
80
81 def update_stat(self, stat_id, session_value, total_value):
82
83 current_stat_id_labels = self.stat_id_labels.get(stat_id)
84
85 if not current_stat_id_labels:
86 return
87
88 if not self.widget.get_visible():
89 return
90
91 if stat_id in {"downloaded_size", "uploaded_size"}:
92 session_value = human_size(session_value)
93 total_value = human_size(total_value)
94
95 elif stat_id == "since_timestamp":
96 session_value = None
97 total_value = (_("Total Since %(date)s") % {
98 "date": time.strftime("%x", time.localtime(total_value))} if total_value > 0 else None)
99
100 else:
101 session_value = humanize(session_value)
102 total_value = humanize(total_value)
103
104 if session_value is not None:
105 session_label = current_stat_id_labels["session"]
106
107 if session_label.get_text() != session_value:
108 session_label.set_text(session_value)
109
110 if total_value is not None:
111 total_label = current_stat_id_labels["total"]
112
113 if total_label.get_text() != total_value:
114 total_label.set_text(total_value)
115
116 def on_reset_statistics_response(self, *_args):
117 core.statistics.reset_stats()
118
119 def on_reset_statistics(self, *_args):
120
121 OptionDialog(
122 parent=self,
123 title=_("Reset Transfer Statistics?"),
124 message=_("Do you really want to reset transfer statistics?"),
125 destructive_response_id="ok",
126 callback=self.on_reset_statistics_response
127 ).present()
128
129 def on_close(self, *_args):
130 self.close()
131
132 def on_show(self, *_args):
133 core.statistics.update_stats()