|
| 1 | +using Gtk; |
| 2 | + |
| 3 | +public class ProgressWindow : Window { |
| 4 | + |
| 5 | + private string file_name; |
| 6 | + private string[] files_to_be_updated; |
| 7 | + private int counter; |
| 8 | + private int file_counts; |
| 9 | + |
| 10 | + private ProgressBar progress; |
| 11 | + |
| 12 | + private Label action_label; |
| 13 | + private Label message_label; |
| 14 | + |
| 15 | + private Button btn_cancel; |
| 16 | + private Button btn_show_files; |
| 17 | + private Button btn_quit; |
| 18 | + |
| 19 | + private Pid child_pid; |
| 20 | + |
| 21 | + |
| 22 | + public ProgressWindow(string file_name, string[] files_to_be_updated) { |
| 23 | + var builder = new Builder(); |
| 24 | + try { |
| 25 | + builder.add_from_file ("ui/progress.ui"); |
| 26 | + } |
| 27 | + catch (Error e) { |
| 28 | + error ("Unable to load file: %s", e.message); |
| 29 | + } |
| 30 | + |
| 31 | + action_label = builder.get_object("action_label") as Label; |
| 32 | + action_label.set_vexpand(false); |
| 33 | + |
| 34 | + message_label = builder.get_object("message_label") as Label; |
| 35 | + progress = builder.get_object("progress_progressbar") as ProgressBar; |
| 36 | + |
| 37 | + this.title = "Updating " + file_name; |
| 38 | + this.window_position = WindowPosition.CENTER; |
| 39 | + |
| 40 | + /* Set app icon */ |
| 41 | + try { |
| 42 | + this.icon = new Gdk.Pixbuf.from_file("appimageupdate"); |
| 43 | + } catch (Error e) { |
| 44 | + stderr.printf ("Could not load application icon: %s\n", e.message); |
| 45 | + } |
| 46 | + |
| 47 | + var vbox = builder.get_object("main_box") as Box; |
| 48 | + add(vbox); |
| 49 | + |
| 50 | + btn_cancel = builder.get_object("btn_cancel") as Button; |
| 51 | + btn_cancel.clicked.connect(cancel); |
| 52 | + |
| 53 | + btn_quit = builder.get_object("btn_quit") as Button; |
| 54 | + btn_quit.clicked.connect(Gtk.main_quit); |
| 55 | + btn_quit.set_visible(false); |
| 56 | + |
| 57 | + btn_show_files = builder.get_object("btn_show_files") as Button; |
| 58 | + btn_show_files.clicked.connect(this.show_files); |
| 59 | + btn_show_files.set_visible(false); |
| 60 | + |
| 61 | + this.files_to_be_updated = files_to_be_updated; |
| 62 | + this.file_name = file_name; |
| 63 | + |
| 64 | + counter = 0; |
| 65 | + this.show_all(); |
| 66 | + |
| 67 | + update(); |
| 68 | + } |
| 69 | + |
| 70 | + private void update() { |
| 71 | + try { |
| 72 | + int extra_args = 0; |
| 73 | + |
| 74 | + string[] spawn_args = new string[8 + files_to_be_updated.length + extra_args]; |
| 75 | + spawn_args[0] = "appimageupdate"; |
| 76 | + spawn_args[1] = this.file_name; |
| 77 | + |
| 78 | + string[] spawn_env = Environ.get (); |
| 79 | + int standard_error; |
| 80 | + int standard_out; |
| 81 | + |
| 82 | + string a_string = string.joinv(" ", spawn_args); |
| 83 | + stdout.printf(a_string); |
| 84 | + |
| 85 | + Process.spawn_async_with_pipes ("/", |
| 86 | + spawn_args, |
| 87 | + spawn_env, |
| 88 | + SpawnFlags.SEARCH_PATH | SpawnFlags.DO_NOT_REAP_CHILD, |
| 89 | + null, |
| 90 | + out child_pid, |
| 91 | + null, |
| 92 | + out standard_out, |
| 93 | + out standard_error); |
| 94 | + |
| 95 | + // stdout: |
| 96 | + IOChannel output = new IOChannel.unix_new (standard_out); |
| 97 | + output.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => { |
| 98 | + return this.process_line (channel, condition, "stdout"); |
| 99 | + }); |
| 100 | + |
| 101 | + // stderr: |
| 102 | + IOChannel error = new IOChannel.unix_new (standard_error); |
| 103 | + error.add_watch (IOCondition.IN | IOCondition.HUP, (channel, condition) => { |
| 104 | + return this.process_error (channel, condition, "stderr"); |
| 105 | + }); |
| 106 | + |
| 107 | + ChildWatch.add (child_pid, (pid, status) => { |
| 108 | + // Triggered when the child indicated by child_pid exits |
| 109 | + Process.close_pid (pid); |
| 110 | + btn_cancel.set_label("Close"); |
| 111 | + btn_quit.set_visible(true); |
| 112 | + btn_show_files.set_visible(true); |
| 113 | + }); |
| 114 | + |
| 115 | + } catch (SpawnError e) { |
| 116 | + action_label.label = "Error: " + e.message; |
| 117 | + } |
| 118 | + |
| 119 | + } |
| 120 | + |
| 121 | + /* Handle output pipe */ |
| 122 | + private bool process_line (IOChannel channel, IOCondition condition, string stream_name) { |
| 123 | + if (condition == IOCondition.HUP) { |
| 124 | + // action_label.label = "Done"; |
| 125 | + return false; |
| 126 | + } |
| 127 | + |
| 128 | + try { |
| 129 | + string line; |
| 130 | + channel.read_line (out line, null, null); |
| 131 | + stdout.printf(line); // Be verbose |
| 132 | + file_counts = 12; // TODO: Get from command line output |
| 133 | + action_label.label = "Updating: " + line.substring(0, line.length - 1); |
| 134 | + counter++; |
| 135 | + progress.set_fraction( 1.0f * counter / file_counts); |
| 136 | + |
| 137 | + } catch (IOChannelError e) { |
| 138 | + stdout.printf ("%s: IOChannelError: %s\n", stream_name, e.message); |
| 139 | + return false; |
| 140 | + } catch (ConvertError e) { |
| 141 | + stdout.printf ("%s: ConvertError: %s\n", stream_name, e.message); |
| 142 | + return false; |
| 143 | + } |
| 144 | + |
| 145 | + return true; |
| 146 | + } |
| 147 | + |
| 148 | + private bool process_error(IOChannel channel, IOCondition condition, string stream_name) { |
| 149 | + if (condition == IOCondition.HUP) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + |
| 153 | + try { |
| 154 | + string line; |
| 155 | + channel.read_line (out line, null, null); |
| 156 | + stdout.printf(line); // Be verbose |
| 157 | + message_label.label = line.substring(0, line.length - 1); |
| 158 | + |
| 159 | + } catch (IOChannelError e) { |
| 160 | + stdout.printf ("%s: IOChannelError: %s\n", stream_name, e.message); |
| 161 | + return false; |
| 162 | + } catch (ConvertError e) { |
| 163 | + stdout.printf ("%s: ConvertError: %s\n", stream_name, e.message); |
| 164 | + return false; |
| 165 | + } |
| 166 | + |
| 167 | + return true; |
| 168 | + } |
| 169 | + |
| 170 | + private void show_files() { |
| 171 | + // To be implemented |
| 172 | + this.close(); |
| 173 | + } |
| 174 | + |
| 175 | + private void cancel() { |
| 176 | + Posix.kill(child_pid, Posix.SIGTERM); |
| 177 | + this.close(); |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments