2 # Contest UI for the MO Submitter
3 # (c) 2007 Martin Mares <mj@ucw.cz>
9 defined $ENV{"MO_ROOT"} or die "Please set MO_ROOT to the contest root directory first.\n";
11 use lib $ENV{"MO_ROOT"} . "/lib/perl5";
12 use lib $ENV{"MO_ROOT"} . "/submit/lib/perl5";
21 my $conn = new MO::Submit;
23 ### GUI INITIALIZATION ###
26 sub start_refresh_timer($);
27 sub stop_refresh_timer();
28 my $force_refresh = 0;
33 my $busy_cursor = Gtk2::Gdk::Cursor->new('watch');
36 my $window = Gtk2::Window->new('toplevel');
37 $window->signal_connect("delete-event" => sub { Gtk2->main_quit });
38 $window->set_title($conn->{"Contest"} . " Submitter");
39 $window->set_wmclass("submitter", "Submitter");
40 $window->set_default_size(640, 480);
43 my $title_lab = Gtk2::Label->new;
44 $title_lab->set_markup("<span size='x-large'>" . $conn->{"Contest"} . "</span>");
47 my $b_submit = Gtk2::Button->new('Submit');
48 $b_submit->set_image(Gtk2::Image->new_from_stock('gtk-apply', 'button'));
49 $b_submit->signal_connect(clicked => sub { submit(0) });
50 $b_submit->set_sensitive(0);
52 my $b_check = Gtk2::Button->new('Check');
53 $b_check->set_image(Gtk2::Image->new_from_stock('gtk-find', 'button'));
54 $b_check->signal_connect(clicked => sub { submit(1) });
55 $b_check->set_sensitive(0);
57 my $b_refresh = Gtk2::Button->new('Refresh');
58 $b_refresh->set_image(Gtk2::Image->new_from_stock('gtk-refresh', 'button'));
59 $b_refresh->signal_connect(clicked => sub { start_refresh_timer(1) });
61 my $button_box = Gtk2::HBox->new;
62 $button_box->pack_start_defaults($b_submit);
63 $button_box->pack_start_defaults($b_check);
64 $button_box->pack_start_defaults($b_refresh);
65 $button_box->set_border_width(5);
68 my $task_store = Gtk2::ListStore->new('Glib::String', 'Glib::String');
70 my $task_view = Gtk2::TreeView->new($task_store);
71 my $task_renderer = Gtk2::CellRendererText->new;
72 my $task_col1 = Gtk2::TreeViewColumn->new_with_attributes("Task", $task_renderer, "text", 0);
73 $task_view->append_column($task_col1);
74 my $task_col2 = Gtk2::TreeViewColumn->new_with_attributes("Status", $task_renderer, "text", 1);
75 $task_view->append_column($task_col2);
76 $task_view->set_headers_visible(0);
78 my $task_scroll = Gtk2::ScrolledWindow->new;
79 $task_scroll->set_policy("automatic", "automatic");
80 $task_scroll->add($task_view);
81 $task_scroll->set_border_width(5);
83 my $task_frame = Gtk2::Frame->new("Tasks");
84 $task_frame->add($task_scroll);
87 my $task_sel = $task_view->get_selection;
88 $task_sel->set_mode('single');
89 $task_sel->signal_connect(changed => sub {
90 my $iter = $_[0]->get_selected;
92 $selected_task = $task_store->get($iter, 0);
93 $b_submit->set_sensitive(1);
94 $b_check->set_sensitive(1);
96 $selected_task = undef;
97 print "Deselected task\n";
98 $b_submit->set_sensitive(0);
99 $b_check->set_sensitive(0);
103 my $status_bar = Gtk2::Statusbar->new;
104 my $bar_ctx = $status_bar->get_context_id('xyzzy');
106 my $vbox = Gtk2::VBox->new;
107 $vbox->pack_start($title_lab, 0, 0, 10);
108 $vbox->pack_start($task_frame, 1, 1, 0);
109 $vbox->pack_start($button_box, 0, 0, 0);
110 $vbox->pack_start($status_bar, 0, 0, 0);
113 $window->signal_connect("expose-event" => sub { init_refresh(); return 0; });
121 print "GUI: ", $_[0], "\n" if $conn->{"Trace"};
126 defined $last_status_id and $status_bar->remove($bar_ctx, $last_status_id);
127 $last_status_id = $status_bar->push($bar_ctx, shift @_);
132 $window->window->set_cursor($busy_cursor);
133 $window->Gtk2::Gdk::flush;
138 $window->window->set_cursor(undef);
139 $window->Gtk2::Gdk::flush;
142 my $window_inited = 0;
145 if (!$window_inited) {
147 start_refresh_timer(1);
153 my $refresh_timer_id;
157 refresh(); # FIXME: If-modified-since version?
158 return 1; # We wish to re-run the timer
161 sub start_refresh_timer($) {
163 stop_refresh_timer();
165 $refresh_timer_id = Glib::Timeout->add($conn->{"RefreshTimer"}, \&timed_refresh);
168 sub stop_refresh_timer() {
169 if (defined $refresh_timer_id) {
170 Glib::Source->remove($refresh_timer_id);
171 $refresh_timer_id = undef;
175 my $task_status_object;
179 sub recalc_task_list() {
182 foreach my $t ($task_status_object->getarray("(T")) {
183 my $task = $t->get("T");
184 foreach my $p ($t->getarray("(P")) {
185 my $part = $p->get("P");
186 my $taskpart = ($task eq $part) ? $task : "$task/$part";
187 push @new_tp, $taskpart;
189 my $current_ver = $p->get("V");
190 foreach my $v ($p->getarray("(V")) {
191 if ($v->get("V") == $current_ver) {
192 my $time = strftime("%H:%M:%S", localtime $v->get("T"));
194 "$part." . $v->get("X") . ", " .
195 $v->get("L") . " bytes, " .
196 $v->get("S") . " $time)";
200 push @new_stat, $status;
204 if (join("\n", @new_tp) ne join("\n", @task_parts)) {
205 # The tasks have changed, repopulate the whole structure
208 foreach my $taskpart (@new_tp) {
209 my $iter = $task_store->append;
210 $task_store->set($iter,
215 # Update the task status
218 $task_store->foreach(sub {
219 my ($obj, $path, $iter) = @_;
220 if ($s[0] ne $ns[0]) {
221 $task_store->set($iter, 1, $ns[0]);
229 @task_parts = @new_tp;
230 @task_stat = @new_stat;
235 if (!$conn->is_connected || $force_refresh) {
236 busy("Connecting to server...");
237 if ($conn->connect) {
238 ready("Connected successfully");
240 ready($conn->{"error"});
243 if ($conn->is_connected) {
244 busy("Updating status...");
245 my $r = new Sherlock::Object("!" => "STATUS");
246 $r = $conn->request($r);
248 ready($conn->{"error"});
249 } elsif ($r->get("-")) {
252 $task_status_object = $r;
258 if (!$conn->is_connected && !$force_refresh) {
260 $conn->log("Retrying");
272 my $submitting_label;
276 my $submit_extension;
277 my %submit_fn_cache = ();
281 $subwin->destroy if $close;
282 start_refresh_timer(0);
283 ### FIXME: ... and refresh status
286 sub finish_submit() {
287 my $button = Gtk2::Button->new_from_stock('gtk-close');
288 $button->signal_connect(clicked => sub { end_submit(1) });
290 $bbutton_box = Gtk2::HButtonBox->new;
291 $bbutton_box->pack_start_defaults($button);
292 $subwin_vbox->pack_start($bbutton_box, 0, 0, 10);
296 $subwin->window->set_cursor(undef);
300 $status_label->set_markup("<span size='large'>Submitted OK</span>");
301 $submitting_label->set_markup("<span size='large'>The task has been successfully submitted.</span>");
306 sub submit_failed($) {
308 $status_label->set_markup("<span size='large'>Submit failed</span>");
309 $submitting_label->set_markup("<span size='large'>$msg</span>");
314 my ($task, $part) = split /\//, $selected_task;
315 defined $part or $part = $task;
317 if (defined $conn->{"History"}) {
318 busy("Submitting locally to " . $conn->{"History"});
319 my $err = $conn->local_submit($task, $part, $submit_extension, $submit_filename);
321 submit_failed("Recording to local history failed\n($err)");
326 if ($conn->is_connected) {
327 busy("Checking server status...");
328 my $r = new Sherlock::Object("!" => "STATUS"); ### FIXME: use a NOP command
329 $r = $conn->request($r);
331 if (!$conn->is_connected) {
332 busy("Reconnecting to server...");
333 if (!$conn->connect) {
334 ready($conn->{"error"});
335 submit_failed("Unable to connect to the server"); ### FIXME: Mention local submit
339 busy("Submitting...");
341 my $fh = new IO::File($submit_filename);
343 submit_failed("Unable to open $submit_filename\n($!)");
346 my $stat = File::stat::populate($fh->stat);
348 submit_failed("Unable to stat $submit_filename\n($!)");
351 my $size = $stat->size;
353 my $r = new Sherlock::Object("!" => "SUBMIT", "T" => $task, "P" => $part, "X" => $submit_extension, "S" => $size);
354 $r = $conn->request($r);
356 submit_failed("Connection to the server lost");
358 } elsif ($r->get("-")) {
359 submit_failed($r->get("-"));
363 $r = $conn->send_file($fh, $size);
365 submit_failed("Connection to the server lost");
367 } elsif ($r->get("-")) {
368 submit_failed($r->get("-"));
376 sub checks_failed($) {
379 $status_label->set_markup("<span size='large'>Check failed</span>");
381 my $text_buffer = Gtk2::TextBuffer->new;
382 $text_buffer->set_text($msg);
384 my $text_view = Gtk2::TextView->new_with_buffer($text_buffer);
385 $text_view->set_editable(0);
386 $text_view->set_cursor_visible(0);
388 my $text_scroll = Gtk2::ScrolledWindow->new;
389 $text_scroll->set_policy("automatic", "automatic");
390 $text_scroll->add($text_view);
392 my $text_frame = Gtk2::Frame->new("Checker log");
393 $text_frame->add($text_scroll);
395 $submitting_label->destroy;
396 $subwin_vbox->pack_start_defaults($text_frame);
403 $status_label->set_markup("<span size='large'>Checked successfully</span>");
404 $submitting_label->set_markup("<span size='large'>The task has passed the checks.</span>");
409 ### FIXME: Record to local history here
411 $status_label->set_markup("<span size='large'>Submitting</span>");
414 # Continue when everything is displayed
415 Glib::Idle->add(sub {
416 $window->Gtk2::Gdk::flush;
423 ($submit_extension) = ($submit_filename =~ /\.([^.]+)$/);
424 if (!$submit_extension) {
425 checks_failed("The filename does not have a valid extension");
428 if (!$conn->{"Checks"}) {
432 my $root = $conn->{"root"};
433 my ($task, $part) = split /\//, $selected_task;
434 defined $part or $part = "";
435 my $verdict = `$root/bin/check -s "$submit_filename" $task $part 2>&1`;
437 checks_failed($verdict);
444 $submit_filename = $chooser->get_filename;
445 $submit_fn_cache{$selected_task} = $submit_filename;
446 msg "Selected $submit_filename";
447 defined $submit_filename or return;
448 -f $submit_filename or return;
451 $bbutton_box->destroy;
453 $status_label->set_markup("<span size='large'>Checking</span>");
455 $submitting_label = Gtk2::Label->new("Please wait...");
456 $subwin_vbox->pack_start_defaults($submitting_label);
458 $subwin->window->set_cursor($busy_cursor);
460 # Continue when everything is displayed
461 Glib::Idle->add(sub {
462 $window->Gtk2::Gdk::flush;
469 $check_only = shift @_;
471 stop_refresh_timer();
473 $subwin = Gtk2::Window->new('toplevel');
474 $subwin->set_default_size(640, 480);
475 $subwin->set_modal(1);
476 $subwin->set_transient_for($window);
477 $subwin->set_destroy_with_parent(1);
478 $subwin->set_title("Submit task $selected_task");
479 $subwin->set_wmclass("submitter", "Submitter");
480 $subwin->signal_connect("delete-event" => sub { end_submit(0); return 0; });
482 my $bb_submit = Gtk2::Button->new($check_only ? 'Check' : 'Submit');
483 $bb_submit->set_image(Gtk2::Image->new_from_stock('gtk-apply', 'button'));
484 $bb_submit->signal_connect(clicked => \&do_submit);
486 my $bb_cancel = Gtk2::Button->new_from_stock('gtk-cancel');
487 $bb_cancel->signal_connect(clicked => sub { end_submit(1) });
489 $bbutton_box = Gtk2::HButtonBox->new;
490 $bbutton_box->pack_start_defaults($bb_submit);
491 $bbutton_box->pack_start_defaults($bb_cancel);
492 $bbutton_box->set_border_width(5);
494 my $subwin_label = Gtk2::Label->new;
495 $subwin_label->set_markup("<span size='x-large'>" . ($check_only ? "Checking" : "Submitting") . " $selected_task</span>");
497 $status_label = Gtk2::Label->new;
498 $status_label->set_markup("<span size='large'>Please select file to " . ($check_only ? "check" : "submit") . "</span>");
500 $chooser = Gtk2::FileChooserWidget->new("open");
501 $chooser->set_local_only(1);
502 $chooser->signal_connect("file-activated" => \&do_submit);
503 $chooser->set_filename($submit_fn_cache{$selected_task}) if defined $submit_fn_cache{$selected_task};
505 $subwin_vbox = Gtk2::VBox->new;
506 $subwin_vbox->pack_start($subwin_label, 0, 0, 10);
507 $subwin_vbox->pack_start($status_label, 0, 0, 10);
508 $subwin_vbox->pack_start_defaults($chooser);
509 $subwin_vbox->pack_start($bbutton_box, 0, 0, 0);
511 $subwin->add($subwin_vbox);