1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# window.py
#
# Copyright 2024 Etersoft
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
#
# SPDX-License-Identifier: AGPL-3.0-or-later
from gi.repository import Gtk, Adw, GObject
import gettext
from .widgets.applicationrow import ApplicationRow
from .widgets.logdialog import LogDialog
from .widgets.errordialog import ErrorDialog
from .widgets.flagsdialog import FlagsDialog
gettext.textdomain('eepm-play-gui')
_ = gettext.gettext
from .tools.appsmanager import ApplicationManager
@Gtk.Template(resource_path='/ru/eepm/PlayGUI/window.ui')
class EepmPlayGuiWindow(Adw.ApplicationWindow):
__gtype_name__ = 'EepmPlayGuiWindow'
# noinspection PyArgumentList
is_loading = GObject.Property(type=bool, default=True)
search_entry = Gtk.Template.Child()
search_bar = Gtk.Template.Child()
search_toggle_button = Gtk.Template.Child()
main_stack = Gtk.Template.Child()
search_dropdown = Gtk.Template.Child()
choice_listbox = Gtk.Template.Child()
reset_button = Gtk.Template.Child()
apply_button = Gtk.Template.Child()
flags_button = Gtk.Template.Child()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_title("EPM PLAY")
self.rows = None
self.reset_button.connect("clicked", lambda _: self.update_ui())
self.apply_button.connect("activated", self.on_apply_clicked)
self.errordialog = ErrorDialog()
self.flagsdialog = FlagsDialog()
self.flags_button.connect("clicked", lambda _: self.flagsdialog.present(self))
self.flagsdialog.add_flags(
[
{
"flag": "--force",
"name": "Force",
"description": _("Sometimes it helps to get the latest version of the program. (Not recommended)"),
},
{
"flag": "--ipfs",
"name": "IPFS",
"description": _("It helps to get resources that are unavailable from your network"),
},
{
"flag": "--auto",
"name": "Auto",
"description": _("The user will not be asked any questions"),
},
]
)
self.connect("notify::is-loading", self.on_is_loading_changed)
self.search_bar.connect_entry(self.search_entry)
self.search_entry.connect("search-changed", self.on_search_changed)
self.search_dropdown.connect("notify::selected", self.on_filter_changed)
self.choice_listbox.set_filter_func(self.listbox_filter_func)
self.logdialog = LogDialog(win=self)
self.update_ui()
def on_is_loading_changed (self, obj, _pspec):
self.main_stack.props.visible_child_name = "loading" if self.props.is_loading else "main"
def on_applications_loaded(self, applications, error=None):
if error:
print(f"Error: {error}")
else:
self.applications = applications
def update_ui(self):
self.installed_apps = None
self.applications = None
self.props.is_loading = True
self.update_button_status()
ApplicationManager.get_available_applications(self.on_applications_loaded)
ApplicationManager.get_installed_applications(self.on_installed_apps_loaded)
def on_installed_apps_loaded(self, installed_apps, error=None):
if error:
print(f"Error: {error}")
else:
self.installed_apps = installed_apps
self.clear_choice_listbox()
self.rows = {}
for app in self.applications:
self.add_application_row(app)
self.choice_listbox.invalidate_filter()
self.props.is_loading = False
def clear_choice_listbox(self):
self.choice_listbox.remove_all()
def add_application_row(self, app):
"""Adds an application row to the listbox."""
row = ApplicationRow(
app=app,
is_installed=app['name'] in self.installed_apps,
on_toggle=self.on_checkbox_toggled
)
self.choice_listbox.append(row)
self.rows[app['name']] = row
def on_checkbox_toggled(self, app_name, active):
print(f"{app_name} {'установлен' if active else 'снят'}")
self.update_button_status()
def update_button_status(self):
"""Update the button status based on the current selection."""
to_install, to_remove = self.get_install_remove_lists()
button_states = {
(True, True): (_("Remove and install applications"), "suggested-action"),
(True, False): (_("Install applications"), "suggested-action"),
(False, True): (_("Remove applications"), "destructive-action"),
(False, False): (_("Update applications"), "suggested-action"),
}
title, css_class = button_states[(bool(to_install), bool(to_remove))]
self.apply_button.set_title(title)
# Remove all previous CSS classes in a loop
for css in ["suggested-action", "destructive-action"]:
self.apply_button.remove_css_class(css)
# Add the new CSS class
self.apply_button.add_css_class(css_class)
def on_search_changed(self, search_entry):
self.choice_listbox.invalidate_filter() # Обновление фильтра при изменении поиска
def on_filter_changed(self, dropdown, _pspec):
self.choice_listbox.invalidate_filter() # Обновление фильтра при изменении фильтра
def listbox_filter_func(self, row):
"""Функция фильтрации для GtkListBox, которая проверяет текст и состояние фильтра."""
search_text = self.search_entry.get_text().lower()
filter_option = self.search_dropdown.get_selected()
# Получение заголовка и подзаголовка строки
title = row.get_title().lower() if row.get_title() else ''
subtitle = row.get_subtitle().lower() if row.get_subtitle() else ''
# Проверка текста поиска (по имени или описанию)
matches_search = search_text in title or search_text in subtitle
# Проверка по фильтру: установлено/не установлено в системе
app_name = row.get_title() if row.get_title() else ''
is_installed = app_name in self.installed_apps
is_changed = row.is_changed()
if filter_option == 1: # Installed
return matches_search and is_installed
elif filter_option == 2: # Uninstalled
return matches_search and not is_installed
elif filter_option == 3: # Changed
return matches_search and is_changed
else:
return matches_search # All
def on_logdialog_closed(self, error=None):
if error:
self.errordialog.present_error(self, error)
self.update_ui()
def on_apply_clicked(self, button):
self.props.is_loading = True # Show loading message before command execution
to_install, to_remove = self.get_install_remove_lists()
full_command = self.build_commands(to_install, to_remove)
if full_command:
pkexec_command = f'pkexec sh -c "{full_command}"'
self.logdialog.run(pkexec_command, on_done=self.on_logdialog_closed)
else:
self.logdialog.run(self.flagsdialog.apply_flags(
"epm play",
"pkexec epm play --update all",
ignored_flags=[
"--force",
]
),
on_done=self.on_logdialog_closed
)
def get_install_remove_lists(self):
if not (self.installed_apps and self.rows):
return [], []
to_install = [
app_name for app_name, row in self.rows.items()
if row.checkbox.get_active() and app_name not in self.installed_apps
]
to_remove = [
app_name for app_name, row in self.rows.items()
if not row.checkbox.get_active() and app_name in self.installed_apps
]
return to_install, to_remove
def build_commands(self, to_install, to_remove):
commands = []
if to_install:
commands.append(self.flagsdialog.apply_flags("epm play", f"epm play {' '.join(to_install)}"))
if to_remove:
commands.append(
self.flagsdialog.apply_flags(
"epm play",
f"epm play --remove {' '.join(to_remove)}",
ignored_flags=[
"--force", "--ipfs"
]
)
)
if not commands:
return None
full_command = " && ".join(commands)
return full_command