-
-
Notifications
You must be signed in to change notification settings - Fork 255
/
installer.py
216 lines (176 loc) · 7.16 KB
/
installer.py
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
# installer.py
#
# Copyright 2022 brombinmirko <send@mirko.pm>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, in version 3 of the License.
#
# 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import urllib.request
from gi.repository import Gtk, GLib, Gio, GdkPixbuf, Adw
from bottles.backend.utils.threading import RunAsync
from bottles.frontend.utils.gtk import GtkUtils
@Gtk.Template(resource_path="/com/usebottles/bottles/local-resource-entry.ui")
class LocalResourceEntry(Adw.ActionRow):
__gtype_name__ = "LocalResourceEntry"
# region Widgets
btn_path = Gtk.Template.Child()
# endregion
def __init__(self, parent, resource, **kwargs):
super().__init__(**kwargs)
# common variables and references
self.parent = parent
self.resource = resource
self.set_title(resource)
# connect signals
self.btn_path.connect("clicked", self.__choose_path)
def __choose_path(self, *_args):
"""
Open the file chooser dialog and set the path to the
selected file
"""
def set_path(_dialog, response):
if response != Gtk.ResponseType.ACCEPT:
return
path = dialog.get_file().get_path()
self.parent.add_resource(self.resource, path)
self.set_subtitle(path)
dialog = Gtk.FileChooserNative.new(
title=_("Select Resource File"),
action=Gtk.FileChooserAction.OPEN,
parent=self.parent,
)
dialog.set_modal(True)
dialog.connect("response", set_path)
dialog.show()
@Gtk.Template(resource_path="/com/usebottles/bottles/dialog-installer.ui")
class InstallerDialog(Adw.Window):
__gtype_name__ = "InstallerDialog"
__sections = {}
__steps = 0
__current_step = 0
__local_resources = []
__final_resources = {}
# region widgets
stack = Gtk.Template.Child()
window_title = Gtk.Template.Child()
btn_install = Gtk.Template.Child()
btn_proceed = Gtk.Template.Child()
btn_close = Gtk.Template.Child()
status_init = Gtk.Template.Child()
status_installed = Gtk.Template.Child()
status_error = Gtk.Template.Child()
progressbar = Gtk.Template.Child()
group_resources = Gtk.Template.Child()
install_status_page = Gtk.Template.Child()
img_icon = Gtk.Template.Child()
img_icon_install = Gtk.Template.Child()
style_provider = Gtk.CssProvider()
# endregion
def __init__(self, window, config, installer, **kwargs):
super().__init__(**kwargs)
self.set_transient_for(window)
self.window = window
self.manager = window.manager
self.config = config
self.installer = installer
self.__steps_phrases = {
"deps": _("Installing Windows dependencies…"),
"params": _("Configuring the bottle…"),
"steps": _("Processing installer steps…"),
"exe": _("Installing the {}…".format(installer[1].get("Name"))),
"checks": _("Performing final checks…"),
}
self.status_init.set_title(installer[1].get("Name"))
self.install_status_page.set_title(
_("Installing {0}…").format(installer[1].get("Name"))
)
self.status_installed.set_description(
_("{0} is now available in the programs view.").format(
installer[1].get("Name")
)
)
self.__set_icon()
self.btn_install.connect("clicked", self.__check_resources)
self.btn_proceed.connect("clicked", self.__install)
self.btn_close.connect("clicked", self.__close)
def __set_icon(self):
try:
url = self.manager.installer_manager.get_icon_url(self.installer[0])
if url is None:
self.img_icon.set_visible(False)
self.img_icon_install.set_visible(False)
return
with urllib.request.urlopen(url) as res:
stream = Gio.MemoryInputStream.new_from_data(res.read(), None)
pixbuf = GdkPixbuf.Pixbuf.new_from_stream(stream, None)
self.img_icon.set_pixel_size(78)
self.img_icon.set_from_pixbuf(pixbuf)
self.img_icon_install.set_pixel_size(78)
self.img_icon_install.set_from_pixbuf(pixbuf)
except:
self.img_icon.set_visible(False)
self.img_icon_install.set_visible(False)
def __check_resources(self, *_args):
self.__local_resources = self.manager.installer_manager.has_local_resources(
self.installer
)
if len(self.__local_resources) == 0:
self.__install()
return
for resource in self.__local_resources:
_entry = LocalResourceEntry(self, resource)
GLib.idle_add(self.group_resources.add, _entry)
self.btn_proceed.set_visible(True)
self.stack.set_visible_child_name("page_resources")
def __install(self, *_args):
self.set_deletable(False)
self.stack.set_visible_child_name("page_install")
@GtkUtils.run_in_main_loop
def set_status(result, error=False):
if result.ok:
return self.__installed()
_err = result.data.get("message", _("Installer failed with unknown error"))
self.__error(_err)
self.set_steps(self.manager.installer_manager.count_steps(self.installer))
RunAsync(
task_func=self.manager.installer_manager.install,
callback=set_status,
config=self.config,
installer=self.installer,
step_fn=self.next_step,
local_resources=self.__final_resources,
)
def __installed(self):
self.set_deletable(False)
self.stack.set_visible_child_name("page_installed")
self.window.page_details.view_bottle.update_programs()
self.window.page_details.go_back_sidebar()
def __error(self, error):
self.set_deletable(True)
self.status_error.set_description(error)
self.stack.set_visible_child_name("page_error")
def next_step(self):
"""Next step"""
phrase = self.__steps_phrases[self.__sections[self.__current_step]]
self.progressbar.set_text(phrase)
self.__current_step += 1
self.progressbar.set_fraction(self.__current_step * (1 / self.__steps))
def set_steps(self, steps):
"""Set steps"""
self.__steps = steps["total"]
self.__sections = steps["sections"]
def add_resource(self, resource, path):
self.__final_resources[resource] = path
if len(self.__local_resources) == len(self.__final_resources):
self.btn_proceed.set_sensitive(True)
def __close(self, *_args):
self.destroy()