| 1 | # Copyright 2007, 2008, 2009, 2010, 2011, 2016, 2017 Kevin Ryde | ||
| 2 | |||
| 3 | # This file is part of Chart. | ||
| 4 | # | ||
| 5 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 6 | # terms of the GNU General Public License as published by the Free Software | ||
| 7 | # Foundation; either version 3, or (at your option) any later version. | ||
| 8 | # | ||
| 9 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 10 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 11 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 12 | # details. | ||
| 13 | # | ||
| 14 | # You should have received a copy of the GNU General Public License along | ||
| 15 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | |||
| 17 | package App::Chart::Gtk2::JobQueue; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Carp; | ||
| 22 | use Gtk2; | ||
| 23 | use Gtk2::Ex::TreeModelBits; | ||
| 24 | |||
| 25 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 26 | use App::Chart; | ||
| 27 | |||
| 28 | # uncomment this to run the ### lines | ||
| 29 | # use Smart::Comments; | ||
| 30 | |||
| 31 | use constant DEBUG => 0; | ||
| 32 | |||
| 33 | use Glib::Object::Subclass | ||
| 34 | 'Gtk2::ListStore'; | ||
| 35 | |||
| 36 | use Class::Singleton 1.03; # 1.03 for _new_instance() | ||
| 37 | use base 'Class::Singleton'; | ||
| 38 | *_new_instance = \&Glib::Object::new; | ||
| 39 | |||
| 40 | |||
| 41 | # Had some trouble during program exit with Job 'status-changed' emissions | ||
| 42 | # reaching our _do_job_status_changed() after we have been DESTROY'ed. This | ||
| 43 | # must be late in exit since the singleton keeps us alive globally. But | ||
| 44 | # it's not at global destruction, according to Devel::GlobalDestruction. | ||
| 45 | # | ||
| 46 | # In any case an explicit disconnect of $self->{'hook'} stops the emission. | ||
| 47 | # | ||
| 48 | # Normally DESTROY is wrong for Glib::Object subclasses since it's called | ||
| 49 | # variously when the object loses its last Perl reference but not last C | ||
| 50 | # reference, or something like that. But here as a global the last Perl | ||
| 51 | # reference means program exit. | ||
| 52 | # | ||
| 53 | sub DESTROY { | ||
| 54 | my ($self) = @_; | ||
| 55 | ### JobQueue DESTROY() ... | ||
| 56 | undef $self->{'hook'}; | ||
| 57 | $self->SUPER::DESTROY; | ||
| 58 | } | ||
| 59 | |||
| 60 | sub INIT_INSTANCE { | ||
| 61 | my ($self) = @_; | ||
| 62 | require App::Chart::Gtk2::Job; | ||
| 63 | $self->set_column_types ('App::Chart::Gtk2::Job'); | ||
| 64 | |||
| 65 | require App::Chart::Glib::Ex::EmissionHook; | ||
| 66 | $self->{'hook'} = App::Chart::Glib::Ex::EmissionHook->new | ||
| 67 | ('App::Chart::Gtk2::Job', | ||
| 68 | status_changed => \&_do_job_status_changed, | ||
| 69 | App::Chart::Glib::Ex::MoreUtils::ref_weak($self)); | ||
| 70 | |||
| 71 | # # Left connected until an emission notices the weakening so as to avoid | ||
| 72 | # # Glib warnings if we try to remove the hook if already removed during | ||
| 73 | # # "global destruction". | ||
| 74 | # App::Chart::Gtk2::Job->signal_add_emission_hook | ||
| 75 | # ('status_changed', \&_do_job_status_changed, | ||
| 76 | # App::Chart::Glib::Ex::MoreUtils::ref_weak($self)); | ||
| 77 | } | ||
| 78 | |||
| 79 | sub _do_job_status_changed { | ||
| 80 | my ($invocation_hint, $param_list, $ref_weak_self) = @_; | ||
| 81 | ### JobQueue _do_job_status_changed() ... | ||
| 82 | |||
| 83 | my ($job) = @$param_list; | ||
| 84 | my $self = $$ref_weak_self || return 0; # disconnect | ||
| 85 | return if $self->{'destroyed'}; | ||
| 86 | |||
| 87 | $self->foreach (sub { | ||
| 88 | my ($self, $path, $iter) = @_; | ||
| 89 | my $j = $self->get_value($iter,0); | ||
| 90 | if ($j == $job) { | ||
| 91 | $self->row_changed ($path, $iter); | ||
| 92 | } | ||
| 93 | }); | ||
| 94 | return 1; # stay connected | ||
| 95 | } | ||
| 96 | |||
| 97 | sub enqueue { | ||
| 98 | my ($self, $job) = @_; | ||
| 99 | ref $self or $self = $self->instance; | ||
| 100 | $job->isa('App::Chart::Gtk2::Job') or croak "JobQueue: not a job object: $job"; | ||
| 101 | |||
| 102 | my $pos = $self->iter_n_children(undef) - 1; | ||
| 103 | for ( ; $pos >= 0; $pos--) { | ||
| 104 | my $j = $self->get_value ($self->iter_nth_child(undef,$pos), 0); | ||
| 105 | if ($j->priority >= $job->priority) { | ||
| 106 | last; | ||
| 107 | } | ||
| 108 | } | ||
| 109 | $self->insert_with_values ($pos+1, 0 => $job); | ||
| 110 | $self->consider_run ($job); | ||
| 111 | } | ||
| 112 | |||
| 113 | sub consider_run { | ||
| 114 | my ($self, $new_job) = @_; | ||
| 115 | ref $self or $self = $self->instance; | ||
| 116 | my $job = List::Util::first { ! $_->get('done') && ! $_->get('subprocess') } | ||
| 117 | $self->all_jobs; | ||
| 118 | if (DEBUG) { print "JobQueue next to run ",$job//'undef',"\n"; } | ||
| 119 | |||
| 120 | if ($job) { | ||
| 121 | require App::Chart::Gtk2::Subprocess; | ||
| 122 | if (my $proc = App::Chart::Gtk2::Subprocess->find_idle) { | ||
| 123 | $proc->start_job ($job); | ||
| 124 | if ($new_job && $job == $new_job) { | ||
| 125 | $new_job = undef; | ||
| 126 | } | ||
| 127 | } | ||
| 128 | } | ||
| 129 | if ($new_job) { | ||
| 130 | $new_job->set (status => 'Waiting'); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | sub all_jobs { | ||
| 135 | my ($self, $job_class) = @_; | ||
| 136 | ref $self or $self = $self->instance; | ||
| 137 | my @jobs = Gtk2::Ex::TreeModelBits::column_contents ($self, 0); | ||
| 138 | if ($job_class) { @jobs = grep { $_->isa($job_class) } @jobs; } | ||
| 139 | return @jobs; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub remove_done { | ||
| 143 | my ($self) = @_; | ||
| 144 | ref $self or $self = $self->instance; | ||
| 145 | Gtk2::Ex::TreeModelBits::remove_matching_rows | ||
| 146 | ($self, sub { my ($self, $iter) = @_; | ||
| 147 | my $job = $self->get_value ($iter, 0); | ||
| 148 | return $job->get('done'); | ||
| 149 | }); | ||
| 150 | } | ||
| 151 | |||
| 152 | sub remove_job { | ||
| 153 | my ($self, $job) = @_; | ||
| 154 | ref $self or $self = $self->instance; | ||
| 155 | Gtk2::Ex::TreeModelBits::remove_matching_rows | ||
| 156 | ($self, sub { my ($self, $iter) = @_; | ||
| 157 | return $self->get_value($iter,0) == $job; | ||
| 158 | }); | ||
| 159 | } | ||
| 160 | |||
| 161 | 1; | ||
| 162 | __END__ |