|
| 1 | + |
| 2 | +import gi |
| 3 | +gi.require_version('Gtk', '4.0') |
| 4 | +gi.require_version('Adw', '1') |
| 5 | +from gi.repository import Gtk, Adw, GLib |
| 6 | +import subprocess |
| 7 | +import gettext |
| 8 | +import threading |
| 9 | + |
| 10 | +DOMAIN = 'biglinux-settings' |
| 11 | +_ = gettext.gettext |
| 12 | + |
| 13 | +class ChatAIDialog(Adw.Window): |
| 14 | + def __init__(self, parent_window, script_path, on_close_callback=None): |
| 15 | + super().__init__(modal=True, transient_for=parent_window) |
| 16 | + self.set_default_size(500, 300) |
| 17 | + self.set_title(_("ChatAI Installation")) |
| 18 | + |
| 19 | + self.script_path = script_path |
| 20 | + self.on_close_callback = on_close_callback |
| 21 | + self.is_running = False |
| 22 | + |
| 23 | + # Main content box |
| 24 | + self.main_box = Gtk.Box(orientation=Gtk.Orientation.VERTICAL, spacing=20, margin_top=20, margin_bottom=20, margin_start=20, margin_end=20) |
| 25 | + self.set_content(self.main_box) |
| 26 | + |
| 27 | + # Header |
| 28 | + self.status_label = Gtk.Label(label=_("Installing ChatAI...")) |
| 29 | + self.status_label.add_css_class("title-3") |
| 30 | + self.main_box.append(self.status_label) |
| 31 | + |
| 32 | + # Progress Bar |
| 33 | + self.progress_bar = Gtk.ProgressBar() |
| 34 | + self.progress_bar.set_hexpand(True) |
| 35 | + self.progress_bar.set_show_text(True) |
| 36 | + self.progress_bar.pulse() # Indeterminate since we don't have exact progress |
| 37 | + self.main_box.append(self.progress_bar) |
| 38 | + |
| 39 | + # Detail Log (Expander) |
| 40 | + self.log_expander = Gtk.Expander(label=_("Show Details")) |
| 41 | + self.log_view = Gtk.TextView() |
| 42 | + self.log_view.set_editable(False) |
| 43 | + self.log_view.set_wrap_mode(Gtk.WrapMode.WORD) |
| 44 | + self.log_view.set_monospace(True) |
| 45 | + scrolled = Gtk.ScrolledWindow() |
| 46 | + scrolled.set_child(self.log_view) |
| 47 | + scrolled.set_min_content_height(150) |
| 48 | + self.log_expander.set_child(scrolled) |
| 49 | + self.main_box.append(self.log_expander) |
| 50 | + |
| 51 | + # Buttons |
| 52 | + self.button_box = Gtk.Box(orientation=Gtk.Orientation.HORIZONTAL, spacing=10, halign=Gtk.Align.CENTER) |
| 53 | + self.main_box.append(self.button_box) |
| 54 | + |
| 55 | + self.close_button = Gtk.Button(label=_("Close")) |
| 56 | + self.close_button.connect("clicked", self.on_close) |
| 57 | + self.close_button.set_visible(False) |
| 58 | + self.close_button.add_css_class("suggested-action") |
| 59 | + self.button_box.append(self.close_button) |
| 60 | + |
| 61 | + # Start process |
| 62 | + self.start_install() |
| 63 | + |
| 64 | + def append_log(self, text): |
| 65 | + buffer = self.log_view.get_buffer() |
| 66 | + end_iter = buffer.get_end_iter() |
| 67 | + buffer.insert(end_iter, text + "\n") |
| 68 | + # Scroll to bottom |
| 69 | + adj = self.log_expander.get_child().get_vadjustment() |
| 70 | + adj.set_value(adj.get_upper()) |
| 71 | + |
| 72 | + def update_status(self, text): |
| 73 | + self.status_label.set_label(text) |
| 74 | + |
| 75 | + def start_install(self): |
| 76 | + self.is_running = True |
| 77 | + threading.Thread(target=self.install_thread).start() |
| 78 | + |
| 79 | + def install_thread(self): |
| 80 | + cmd = [self.script_path, "install"] |
| 81 | + |
| 82 | + # We need to read stdout line by line |
| 83 | + process = subprocess.Popen(cmd, stdout=subprocess.PIPE, stderr=subprocess.STDOUT, text=True, bufsize=1) |
| 84 | + |
| 85 | + while True: |
| 86 | + line = process.stdout.readline() |
| 87 | + if not line and process.poll() is not None: |
| 88 | + break |
| 89 | + if line: |
| 90 | + text = line.strip() |
| 91 | + GLib.idle_add(self.append_log, text) |
| 92 | + GLib.idle_add(self.progress_bar.pulse) |
| 93 | + |
| 94 | + rc = process.poll() |
| 95 | + GLib.idle_add(self.on_install_finished, rc) |
| 96 | + |
| 97 | + def on_install_finished(self, return_code): |
| 98 | + self.is_running = False |
| 99 | + if return_code == 0: |
| 100 | + self.update_status(_("Installation Complete!")) |
| 101 | + self.progress_bar.set_fraction(1.0) |
| 102 | + self.close_button.set_visible(True) |
| 103 | + if self.on_close_callback: |
| 104 | + self.on_close_callback(True) |
| 105 | + else: |
| 106 | + self.update_status(_("Installation Failed.")) |
| 107 | + self.close_button.set_visible(True) |
| 108 | + if self.on_close_callback: |
| 109 | + self.on_close_callback(False) |
| 110 | + |
| 111 | + def on_close(self, btn): |
| 112 | + self.close() |
0 commit comments