-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
bakery-gui.py
1652 lines (1446 loc) · 62.1 KB
/
bakery-gui.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
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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Copyright 2023 BredOS
#
# 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, 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 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/>.
#
# SPDX-License-Identifier: GPL-3.0-or-later
import faulthandler
from typing import Any
faulthandler.enable()
from sys import argv
import gi
from os import path
import threading
import locale
import gettext
import bakery
from bakery import (
dryrun,
kb_models,
kb_layouts,
kb_variants,
langs,
tz_list,
geoip,
validate_username,
validate_fullname,
validate_hostname,
check_efi,
check_partition_table,
list_drives,
get_partitions,
gen_new_partitions,
lp,
setup_translations,
debounce,
_,
uidc,
gidc,
lrun,
detect_install_device,
detect_install_source,
detect_session_configuration,
reboot,
upload_log,
log_path,
time_fn,
)
from time import sleep
from datetime import datetime
from babel import dates, numbers
from babel import Locale as bLocale
from pyrunning import (
LoggingHandler,
LogMessage,
Command,
LoggingLevel,
BatchJob,
Function,
)
from pytz import timezone
import config
gi.require_version("Gtk", "4.0")
gi.require_version("Adw", "1")
from gi.repository import Gtk, Adw, Gio, GLib # type: ignore
# py file path
script_dir = path.dirname(path.realpath(__file__))
class BakeryApp(Adw.Application):
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self.connect("activate", self.on_activate)
self.css_provider = self.load_css(script_dir + "/data/main.css")
def on_activate(self, app) -> None:
self.create_action("about", self.on_about_action)
def do_activate(self) -> None:
"""Called when the application is activated.
We raise the application's main window, creating it if
necessary.
"""
global win
win = self.props.active_window
if not win:
win = BakeryWindow(application=self)
self.win = win
self.add_custom_styling(self.win)
win.present()
def on_preferences_action(self, widget, _) -> None:
"""Callback for the app.preferences action."""
# Implement your preferences logic here
pass
def on_about_action(self, widget, py) -> None:
"""Callback for the app.about action."""
about = Adw.AboutWindow(
transient_for=self.props.active_window,
application_name=_("BredOS Installer"),
application_icon="org.bredos.bakery",
developer_name="BredOS",
debug_info=self.win.collect_data(show_pass=False),
version=config.installer_version,
developers=["Panda <[email protected]>", "bill88t <[email protected]>"],
designers=["Panda <[email protected]>", "DustyDaimler"],
documenters=["Panda <[email protected]>", "DroidMaster"],
translator_credits=_("translator-credits"),
copyright=_("Copyright The BredOS developers"),
comments=_("Bakery is a simple installer for BredOS"),
license_type=Gtk.License.GPL_3_0,
website="https://BredOS.org",
issue_url="https://github.com/BredOS/Bakery/issues",
support_url="https://discord.gg/jwhxuyKXaa",
)
translators = ["Bill88t", "Panda <[email protected]>"]
about.add_credit_section(_("Translated by"), translators)
about.add_acknowledgement_section(_("Special thanks to"), ["Shivanandvp"])
about.present()
def create_action(self, name, callback, shortcuts=None) -> None:
"""Add an application action.
Args:
name: the name of the action
callback: the function to be called when the action is
activated
shortcuts: an optional list of accelerators
"""
action = Gio.SimpleAction.new(name, None)
action.connect("activate", callback)
self.add_action(action)
if shortcuts:
self.set_accels_for_action(f"app.{name}", shortcuts)
def load_css(self, css_fn):
"""create a provider for custom styling"""
global css_provider
css_provider = None
if css_fn and path.exists(css_fn):
css_provider = Gtk.CssProvider()
try:
css_provider.load_from_path(css_fn)
except GLib.Error as e:
lp(f"Error loading CSS : {e} ", mode="error")
return None
lp(f"loading custom styling : {css_fn}", mode="debug")
return css_provider
def _add_widget_styling(self, widget):
if css_provider:
context = widget.get_style_context()
context.add_provider(css_provider, Gtk.STYLE_PROVIDER_PRIORITY_USER)
def add_custom_styling(self, widget):
self._add_widget_styling(widget)
# iterate children recursive
for child in widget:
self.add_custom_styling(child)
@Gtk.Template.from_file(script_dir + "/data/window.ui")
class BakeryWindow(Adw.ApplicationWindow):
__gtype_name__ = "BakeryWindow"
stack1 = Gtk.Template.Child()
stack1_sidebar = Gtk.Template.Child()
button_box = Gtk.Template.Child()
cancel_btn = Gtk.Template.Child()
back_btn = Gtk.Template.Child()
next_btn = Gtk.Template.Child()
main_stk = Gtk.Template.Child()
main_page = Gtk.Template.Child()
install_page = Gtk.Template.Child()
offline_install = Gtk.Template.Child()
# online_install = Gtk.Template.Child()
custom_install = Gtk.Template.Child()
install_cancel = Gtk.Template.Child()
install_confirm = Gtk.Template.Child()
err_dialog = Gtk.Template.Child()
log_dialog = Gtk.Template.Child()
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.set_deletable(False)
# go to the first page of main stack
self.main_stk.set_visible_child(self.main_page.get_child())
self.cancel_dialog = self.install_cancel
self.cancel_dialog.set_property("hide-on-close", True)
self.install_type = None
self.install_source = detect_install_source()
self.install_device = detect_install_device()
self.session_configuration = detect_session_configuration()
# self.online_install.connect("clicked", self.main_button_clicked)
self.offline_install.connect("clicked", self.main_button_clicked)
self.custom_install.connect("clicked", self.main_button_clicked)
self.cancel_dialog.connect("response", self.on_cancel_dialog_response)
self.next_btn.connect("clicked", self.on_next_clicked)
self.back_btn.connect("clicked", self.on_back_clicked)
self.cancel_btn.connect("clicked", self.on_cancel_clicked)
self.err_dialog.connect("response", self.on_err_dialog_response)
self.log_dialog.connect("response", self.on_log_dialog_response)
def on_err_dialog_response(self, dialog, resp) -> None:
if resp == "yes":
self.err_dialog.hide()
logurl = upload_log()
if logurl == "error":
self.log_dialog.set_heading(_("Log upload failed"))
self.log_dialog.set_body(
_("The log file could not be uploaded it can still be found at")
+ '<br><a href="file://'
+ log_path
+ '">'
+ log_path
+ "</a>"
)
else:
self.log_dialog.set_heading(_("Log uploaded"))
self.log_dialog.set_body(
_("The log file has been uploaded, you can find it at")
+ '\n<a href="'
+ logurl
+ '">'
+ logurl
+ "</a>"
+ '\n<a href="file://'
+ log_path
+ '">'
+ log_path
+ "</a>"
)
self.log_dialog.present()
if resp == "no":
self.err_dialog.hide()
exit(1)
def on_log_dialog_response(self, dialog, resp) -> None:
if resp == "ok":
self.log_dialog.hide()
exit(1)
@time_fn
@debounce(2)
def main_button_clicked(self, button) -> None:
if button == self.offline_install:
self.init_screens("offline")
# elif button == self.online_install:
# self.init_screens("online")
elif button == self.custom_install:
self.init_screens("custom")
def on_install_btn_clicked(self, button) -> None:
# connect the yes button to the install function
self.install_confirm.connect("response", self.on_install_dialog_response)
self.install_confirm.set_property("hide-on-close", True)
self.install_confirm.show()
@debounce(0.3)
def start_install(self) -> None:
self.current_page = self.pages.index("Install")
page_name = self.pages[self.current_page]
page_id = self.get_page_id(page_name)
self.stack1.set_visible_child_name(page_id)
self.button_box.set_visible(False)
self.install_thread = InstallThread(self, all_pages["Install"])
self.install_thread.start()
def on_install_dialog_response(self, dialog, resp) -> None:
if resp == "yes":
self.install_confirm.hide()
self.start_install()
else:
self.install_confirm.hide()
def on_done_clicked(self, button) -> None:
# quit the app
self.close()
reboot()
@debounce(0.3)
def on_next_clicked(self, button) -> None:
num_pages = len(self.pages)
if self.current_page < num_pages - 1:
self.current_page += 1
if self.current_page == self.pages.index("Summary"):
self.next_btn.disconnect_by_func(self.on_next_clicked)
self.next_btn.connect("clicked", self.on_install_btn_clicked)
self.update_buttons()
page_name = self.pages[self.current_page]
page_id = self.get_page_id(page_name)
self.stack1.set_visible_child_name(page_id)
else:
page_name = self.pages[self.current_page]
page_id = self.get_page_id(page_name)
self.stack1.set_visible_child_name(page_id)
self.update_buttons()
def on_back_clicked(self, button) -> None:
if self.current_page > 0:
self.current_page -= 1
page_name = self.pages[self.current_page]
page_id = self.get_page_id(page_name)
self.stack1.set_visible_child_name(page_id)
try:
self.next_btn.disconnect_by_func(self.on_install_btn_clicked)
self.next_btn.connect("clicked", self.on_next_clicked)
except:
pass
self.update_buttons()
def update_buttons(self) -> None:
num_pages = len(self.pages)
# if page is user page make it so user cant go forward
global user_event
global part_event
user_event = threading.Event()
part_event = threading.Event()
self.check_thread = CheckThread(all_pages["User"])
if self.install_source == "from_iso":
self.check_part_thread = CheckPartitioningThread(all_pages["Partitioning"])
if self.current_page == self.pages.index("User"):
self.next_btn.set_sensitive(False)
# start the thread to check when all fields are filled
self.next_btn.set_label(_("Next"))
self.check_thread.start()
elif self.current_page == self.pages.index("Partitioning"):
user_event.set()
self.next_btn.set_sensitive(False)
# start the thread to check when all fields are filled
self.next_btn.set_label(_("Next"))
self.check_part_thread.start()
elif self.current_page == self.pages.index("Summary"):
user_event.set()
all_pages["Summary"].page_shown()
# change the next button to install
self.next_btn.set_label(_("Install"))
self.next_btn.set_sensitive(self.current_page < num_pages - 1)
self.back_btn.set_sensitive(self.current_page > 0)
elif self.current_page == self.pages.index("Install"):
self.back_btn.set_sensitive(False)
self.next_btn.set_sensitive(False)
self.cancel_btn.set_sensitive(False)
else:
self.next_btn.set_label(_("Next"))
user_event.set()
part_event.set()
self.next_btn.set_sensitive(self.current_page < num_pages - 1)
self.back_btn.set_sensitive(self.current_page > 0)
else:
if self.current_page == self.pages.index("User"):
self.next_btn.set_sensitive(False)
# start the thread to check when all fields are filled
self.next_btn.set_label(_("Next"))
self.check_thread.start()
elif self.current_page == self.pages.index("Summary"):
user_event.set()
all_pages["Summary"].page_shown()
# change the next button to install
self.next_btn.set_label(_("Install"))
self.next_btn.set_sensitive(self.current_page < num_pages - 1)
self.back_btn.set_sensitive(self.current_page > 0)
elif self.current_page == self.pages.index("Install"):
self.back_btn.set_sensitive(False)
self.next_btn.set_sensitive(False)
self.cancel_btn.set_sensitive(False)
else:
self.next_btn.set_label(_("Next"))
user_event.set()
part_event.set()
self.next_btn.set_sensitive(self.current_page < num_pages - 1)
self.back_btn.set_sensitive(self.current_page > 0)
def on_cancel_clicked(self, button) -> None:
# connect the yes button to the delete_pages function
self.cancel_dialog.present()
def on_close_clicked(self, button) -> None:
# connect the yes button to the delete_pages function
self.cancel_dialog.present()
def on_cancel_dialog_response(self, dialog, resp) -> None:
if resp == "yes":
self.cancel_dialog.hide()
self.close()
else:
self.cancel_dialog.hide()
def delete_pages(self, dialog, resp) -> None:
if resp == "yes":
self.main_stk.set_visible_child(self.main_page.get_child())
# remove all pages from stack1
pages = [self.get_page_id(x) for x in self.pages]
for page_id in pages:
w = self.stack1.get_child_by_name(page_id)
self.stack1.remove(w)
self.cancel_dialog.hide()
def get_page_id(self, page_name) -> str:
return page_name
def collect_data(self, show_pass=False, *_) -> dict:
data = {}
if not self.install_type == None:
install_data = {}
install_data["type"] = self.install_type
install_data["source"] = self.install_source
install_data["device"] = self.install_device
data["install_type"] = install_data
data["session_configuration"] = self.session_configuration
data["root_password"] = False
data["layout"] = all_pages["Keyboard"].layout
data["locale"] = all_pages["Locale"].locale
data["timezone"] = all_pages["Timezone"].timezone
data["hostname"] = all_pages["User"].get_hostname()
data["user"] = all_pages["User"].collect_data()
if not show_pass:
data["user"]["password"] = "REDACTED"
installer = {}
installer["installer_version"] = config.installer_version
installer["ui"] = "gui"
installer["shown_pages"] = self.pages
data["installer"] = installer
data["packages"] = {}
if self.install_source == "from_iso":
data["packages"]["to_remove"] = config.iso_packages_to_remove
data["packages"]["de_packages"] = []
try:
data["partitions"] = all_pages["Partitioning"].collect_data()
except:
data["partitions"] = []
return data
else:
return {"install_type": "None"}
def set_list_text(list, string) -> None:
n = list.get_n_items()
if n > 0:
list.splice(0, n, [string])
else:
list.append(string)
def add_pages(self, stack, pages) -> None:
global all_pages, pages_dict
all_pages = {}
pages_dict = config.pages(_)
for page in pages:
page_ = globals()[pages_dict[page][0]](window=self)
all_pages[page] = page_
stack.add_titled(page_, page, pages_dict[page][1])
def init_screens(self, install_type) -> None:
if install_type == "online":
if self.install_source == "from_iso":
self.pages = config.online_pages_from_iso
else:
self.pages = config.online_pages_on_dev
self.add_pages(self.stack1, self.pages)
elif install_type == "offline":
if self.install_source == "from_iso":
self.pages = config.offline_pages_from_iso
else:
self.pages = config.offline_pages_on_dev
self.add_pages(self.stack1, self.pages)
bakery.logging_handler = LoggingHandler(
logger=bakery.logger,
logging_functions=[all_pages["Install"].console_logging],
)
self.install_type = install_type
self.current_page = 0
self.update_buttons()
self.main_stk.set_visible_child(self.install_page.get_child())
@Gtk.Template.from_file(script_dir + "/data/kb_screen.ui")
class kb_screen(Adw.Bin):
__gtype_name__ = "kb_screen"
event_controller = Gtk.EventControllerKey.new()
langs_list = Gtk.Template.Child() # GtkListBox
models_list = Gtk.Template.Child() # GtkDropDown
variant_dialog = Gtk.Template.Child()
variant_list = Gtk.Template.Child() # GtkListBox
select_variant_btn = Gtk.Template.Child()
def __init__(self, window, **kwargs) -> None:
super().__init__(**kwargs)
self.window = window
self.kb_prettylayouts = {k: v for k, v in sorted(kb_layouts(True).items())}
self.kb_prettymodels = kb_models(True)
self.kb_layouts = kb_layouts()
self.kb_models = kb_models()
self.layout = {"model": "pc105", "layout": None, "variant": None}
self.models_model = Gtk.StringList()
self.models_list.set_model(self.models_model)
for model in self.kb_models.keys():
self.models_model.append(self.kb_models[model])
# set pc105 as default
self.models_list.set_selected(list(self.kb_models.keys()).index("pc105"))
self.variant_dialog.set_transient_for(self.window)
self.variant_dialog.set_modal(self.window)
self.select_variant_btn.connect("clicked", self.confirm_selection)
self.models_list.connect("notify::selected-item", self.on_model_changed)
self.populate_layout_list()
def on_model_changed(self, dropdown, *_):
selected = dropdown.props.selected_item
if selected is not None:
self.layout["model"] = self.kb_prettymodels[selected.props.string]
def populate_layout_list(self) -> None:
for lang in self.kb_prettylayouts:
row = Gtk.ListBoxRow()
lang_label = Gtk.Label(label=lang)
row.set_child(lang_label)
self.langs_list.append(row)
self.langs_list.connect("row-activated", self.selected_lang)
self.last_selected_row = None
# preselect the American English layout
if lang == "American English":
self.langs_list.select_row(row)
self.last_selected_row = row
self.layout["layout"] = "us"
self.layout["variant"] = "normal"
self.change_kb_layout("us", "pc105", "normal")
def confirm_selection(self, *_) -> None:
self.variant_dialog.hide()
def show_dialog(self, *_) -> None:
self.variant_dialog.present()
def selected_lang(self, widget, row) -> None:
if row != self.last_selected_row:
self.last_selected_row = row
lang = row.get_child().get_label()
layouts = kb_variants(self.kb_prettylayouts[lang])
self.layout["layout"] = self.kb_prettylayouts[lang]
if not len(layouts):
self.layout["variant"] = "normal"
self.change_kb_layout(
self.layout["layout"], self.layout["model"], self.layout["variant"]
)
else:
# clear the listbox
self.variant_list.remove_all()
# add normal layout
newrow = Gtk.ListBoxRow()
# Language - Layout
layout_label = Gtk.Label(label=f"{lang} - normal")
newrow.set_child(layout_label)
self.variant_list.append(newrow)
self.variant_list.connect("row-activated", self.selected_layout)
self.last_selected_layout = None
# preselect the normal layout
self.variant_list.select_row(newrow)
self.selected_layout(None, newrow)
for layout_ in layouts:
newrow = Gtk.ListBoxRow()
# Language - Layout
layout_label = Gtk.Label(label=f"{lang} - {layout_}")
newrow.set_child(layout_label)
self.variant_list.append(newrow)
self.variant_list.connect("row-activated", self.selected_layout)
self.show_dialog()
def selected_layout(self, widget, row) -> None:
if row != self.last_selected_layout:
self.last_selected_layout = row
self.layout["variant"] = row.get_child().get_label().split(" - ")[1]
self.change_kb_layout(
self.layout["layout"], self.layout["model"], self.layout["variant"]
)
def change_kb_layout(self, lang, model, layout) -> None:
if layout == "normal":
layout = ""
lrun(["setxkbmap", "-model", model])
lrun(["setxkbmap", "-layout", lang])
# WARNING: Variant not set.
# lrun(["setxkbmap", "-variant", layout])
@Gtk.Template.from_file(script_dir + "/data/locale_screen.ui")
class locale_screen(Adw.Bin):
__gtype_name__ = "locale_screen"
langs_list = Gtk.Template.Child()
date_preview = Gtk.Template.Child()
currency_preview = Gtk.Template.Child()
locale_dialog = Gtk.Template.Child()
locales_list = Gtk.Template.Child()
select_locale_btn = Gtk.Template.Child()
def __init__(self, window, **kwargs) -> None:
super().__init__(**kwargs)
self.window = window
self.lang_data = {k: v for k, v in sorted(langs().items())}
self.locale_dialog.set_transient_for(self.window)
self.locale_dialog.set_modal(self.window)
self.populate_locales_list()
self.select_locale_btn.connect("clicked", self.hide_dialog)
def populate_locales_list(self) -> None:
for lang in self.lang_data:
row = Gtk.ListBoxRow()
lang_label = Gtk.Label(label=lang)
row.set_child(lang_label)
self.langs_list.append(row)
self.langs_list.connect("row-activated", self.selected_lang)
self.last_selected_row = None
if lang == "English":
self.langs_list.select_row(row)
self.last_selected_row = row
self.update_previews("en_US.UTF-8 UTF-8")
def selected_lang(self, widget, row) -> None:
if row != self.last_selected_row:
self.last_selected_row = row
lang = row.get_child().get_label()
if len(self.lang_data[lang]) == 1:
self.update_previews(self.lang_data[lang][0])
else:
# clear the listbox
self.locales_list.remove_all()
sr = langs()[lang]
sr.sort()
for locale in sr:
newrow = Gtk.ListBoxRow()
# Language - Layout
locale_label = Gtk.Label(label=locale)
newrow.set_child(locale_label)
self.locales_list.append(newrow)
self.locales_list.connect("row-activated", self.selected_locale)
self.show_dialog()
self.last_selected_locale = None
self.select_locale_btn.set_sensitive(False)
def selected_locale(self, widget, row) -> None:
if row != self.last_selected_locale:
self.last_selected_locale = row
self.update_previews(row.get_child().get_label())
self.select_locale_btn.set_sensitive(True)
def update_previews(self, selected_locale) -> None:
try:
the_locale, encoding = selected_locale.split(" ")
if not encoding == "UTF-8":
the_locale += "." + encoding
except ValueError:
the_locale = selected_locale
self.locale = selected_locale
locale_ = bLocale.parse(the_locale)
date = dates.format_date(date=datetime.utcnow(), format="full", locale=locale_)
time = dates.format_time(time=datetime.utcnow(), format="long", locale=locale_)
currency = numbers.get_territory_currencies(locale_.territory)[0]
currency_format = numbers.format_currency(1234.56, currency, locale=locale_)
number_format = numbers.format_decimal(1234567.89, locale=locale_)
self.date_preview.set_label(time + " - " + date)
self.currency_preview.set_label(number_format + " - " + currency_format)
def hide_dialog(self, stuff) -> None:
try:
# change the locale and update translations
try:
the_locale, encoding = self.locale.split(" ")
if not encoding == "UTF-8":
the_locale += "." + encoding
except ValueError:
the_locale = self.locale
win.queue_draw()
except Exception as e:
import traceback
lp(traceback.format_exc(), mode="error")
self.locale_dialog.hide()
def show_dialog(self, *_) -> None:
self.locales_list.unselect_all()
self.locale_dialog.present()
class CheckThread(threading.Thread):
def __init__(self, window):
threading.Thread.__init__(self)
self.window = window
def run(self):
while True:
if user_event.is_set():
break
if (
(self.window.get_username() is not None)
and (self.window.get_hostname() is not None)
and (self.window.get_password() is not None)
and (self.window.get_fullname() is not None)
and (self.window.validate_uid(self.window.uid_row) is not None)
):
win.next_btn.set_sensitive(True)
else:
win.next_btn.set_sensitive(False)
sleep(0.5)
class InstallThread(threading.Thread):
def __init__(self, window, install_window):
threading.Thread.__init__(self)
self.window = window
self.install_window = install_window
def run(self):
install_data = self.window.collect_data(show_pass=True)
lp(
"Starting install with data: "
+ str(self.window.collect_data(show_pass=False))
)
res = bakery.install(install_data)
if res == 0:
# Change to finish page
self.window.current_page = self.window.pages.index("Finish")
page_name = self.window.pages[self.window.current_page]
page_id = self.window.get_page_id(page_name)
self.window.stack1.set_visible_child_name(page_id)
self.window.button_box.set_visible(True)
self.window.cancel_btn.set_visible(False)
self.window.back_btn.set_visible(False)
self.window.next_btn.disconnect_by_func(self.window.on_install_btn_clicked)
self.window.next_btn.connect("clicked", self.window.on_done_clicked)
self.window.next_btn.set_label(_("Reboot"))
else:
GLib.timeout_add(500, self.window.err_dialog.present)
@Gtk.Template.from_file(script_dir + "/data/user_screen.ui")
class user_screen(Adw.Bin):
__gtype_name__ = "user_screen"
fullname_entry = Gtk.Template.Child()
user_entry = Gtk.Template.Child()
hostname_entry = Gtk.Template.Child()
pass_entry = Gtk.Template.Child()
confirm_pass_entry = Gtk.Template.Child()
user_info = Gtk.Template.Child()
uid_row = Gtk.Template.Child()
nopasswd = Gtk.Template.Child()
autologin = Gtk.Template.Child()
def __init__(self, window, **kwargs) -> None:
super().__init__(**kwargs)
self.window = window
self.pass_entry.connect("changed", self.on_confirm_pass_changed)
self.confirm_pass_entry.connect("changed", self.on_confirm_pass_changed)
self.user_entry.connect("changed", self.on_username_changed)
self.hostname_entry.connect("changed", self.on_hostname_changed)
self.fullname_entry.connect("changed", self.on_fullname_changed)
self.uid_row.connect("changed", self.validate_uid)
self.validate_uid(self.uid_row)
self.user_info_is_visible = False
def on_fullname_changed(self, entry):
fullname = entry.get_text()
a = validate_fullname(fullname)
if a == "":
self.user_info.set_visible(False)
self.user_info_is_visible = False
self.fullname_entry.get_style_context().remove_class("error")
else:
self.user_info.set_label(a)
self.fullname_entry.get_style_context().add_class("error")
if not self.user_info_is_visible:
self.user_info.set_visible(True)
self.user_info_is_visible = True
def validate_uid(self, spin_entry):
uid = int(spin_entry.get_value())
if not uidc(uid) and not gidc(uid):
spin_entry.get_style_context().remove_class("error")
return uid
else:
spin_entry.get_style_context().add_class("error")
return None
def on_hostname_changed(self, entry):
hostname = entry.get_text()
a = validate_hostname(hostname)
if a == "":
self.user_info.set_visible(False)
self.user_info_is_visible = False
self.hostname_entry.get_style_context().remove_class("error")
else:
self.user_info.set_label(a)
self.hostname_entry.get_style_context().add_class("error")
if not self.user_info_is_visible:
self.user_info.set_visible(True)
self.user_info_is_visible = True
def on_username_changed(self, entry):
username = entry.get_text()
a = validate_username(username)
if a == "":
self.user_info.set_visible(False)
self.user_info_is_visible = False
self.user_entry.get_style_context().remove_class("error")
else:
self.user_info.set_label(a)
self.user_entry.get_style_context().add_class("error")
if not self.user_info_is_visible:
self.user_info.set_visible(True)
self.user_info_is_visible = True
def on_confirm_pass_changed(self, entry):
pass_text = self.pass_entry.get_text()
confirm_pass_text = entry.get_text()
if not pass_text == confirm_pass_text:
self.confirm_pass_entry.get_style_context().add_class("error")
elif (
pass_text == confirm_pass_text
or not len(pass_text)
or not len(confirm_pass_text)
):
self.confirm_pass_entry.get_style_context().remove_class("error")
def get_fullname(self) -> str:
if self.fullname_entry.get_text() == "":
return None
else:
if validate_fullname(self.fullname_entry.get_text()) == "":
return self.fullname_entry.get_text()
else:
return None
def get_username(self) -> str:
if self.user_entry.get_text() == "":
return None
else:
if validate_username(self.user_entry.get_text()) == "":
return self.user_entry.get_text()
else:
return None
def get_hostname(self) -> str:
if self.hostname_entry.get_text() == "":
return None
else:
if validate_hostname(self.hostname_entry.get_text()) == "":
return self.hostname_entry.get_text()
else:
return None
def get_password(self) -> str:
if self.pass_entry.get_text() == "":
return None
else:
pass_text = self.pass_entry.get_text()
confirm_pass_text = self.confirm_pass_entry.get_text()
if pass_text == confirm_pass_text:
return pass_text
else:
return None
def collect_data(self) -> dict:
data = {}
data["fullname"] = self.get_fullname()
data["username"] = self.get_username()
data["password"] = self.get_password()
data["uid"] = self.validate_uid(self.uid_row)
data["gid"] = self.validate_uid(self.uid_row)
data["sudo_nopasswd"] = self.nopasswd.get_active()
data["autologin"] = self.autologin.get_active()
data["shell"] = "/bin/bash"
data["groups"] = ["wheel", "network", "video", "audio", "storage"]
return data
@Gtk.Template.from_file(script_dir + "/data/timezone_screen.ui")
class timezone_screen(Adw.Bin):
__gtype_name__ = "timezone_screen"
regions_list = Gtk.Template.Child()
zones_list = Gtk.Template.Child()
curr_time = Gtk.Template.Child()
preview_row = Gtk.Template.Child()
def __init__(self, window, **kwargs) -> None:
super().__init__(**kwargs)
self.window = window
self.tz_list = tz_list()
current_timezone = geoip()
self.timezone = {}
self.timezone["region"] = current_timezone["region"]
self.timezone["zone"] = current_timezone["zone"]
self.timezone["ntp"] = True
self.zones_list.connect("notify::selected-item", self.on_zone_changed)
self.zone_model = Gtk.StringList()
self.zones_list.set_model(self.zone_model)
self.regions_list.connect("notify::selected-item", self.on_region_changed)
self.region_model = Gtk.StringList()
self.regions_list.set_model(self.region_model)
for item in list(self.tz_list.keys()):
self.region_model.append(item)
self.change_regions_list(current_timezone["region"])
self.regions_list.set_selected(
list(self.tz_list.keys()).index(current_timezone["region"])
)
self.select_zone(current_timezone["region"], current_timezone["zone"])
def on_region_changed(self, dropdown, *_):
selected = dropdown.props.selected_item
if selected is not None:
self.timezone["region"] = selected.props.string
self.change_regions_list(selected.props.string)
def on_zone_changed(self, dropdown, *_):
selected = dropdown.props.selected_item
if selected is not None:
self.timezone["zone"] = selected.props.string
self.preview_timezone(self.timezone["region"], selected.props.string)
def change_regions_list(self, region) -> None:
self.zone_model = Gtk.StringList()
self.zones_list.set_model(self.zone_model)
for zone in self.tz_list[region]:
self.zone_model.append(zone)
def select_zone(self, region, zone) -> None:
# get the index of the zone in the list
index = self.tz_list[region].index(zone)
self.zones_list.set_selected(index)
def preview_timezone(self, region, zone) -> None:
# timezone expects region/zone
try:
tz = timezone(str(region + "/" + zone))
except:
tz = None
if tz is not None:
time = datetime.now(tz)
self.curr_time.set_label(time.strftime("%Y-%m-%d %H:%M:%S"))
self.preview_row.set_subtitle(_("Previewing time in ") + str(tz))
@Gtk.Template.from_file(script_dir + "/data/de_screen.ui")
class de_screen(Adw.Bin):
__gtype_name__ = "de_screen"
def __init__(self, window, **kwargs) -> None:
super().__init__(**kwargs)
self.window = window
@Gtk.Template.from_file(script_dir + "/data/summary_screen.ui")
class summary_screen(Adw.Bin):
__gtype_name__ = "summary_screen"