| 1 | # Copyright 2009, 2010, 2011, 2013 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::JobsRunningDialog; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use List::Util; | ||
| 23 | use Locale::TextDomain ('App-Chart'); | ||
| 24 | |||
| 25 | use App::Chart::Database; | ||
| 26 | |||
| 27 | use Glib::Object::Subclass | ||
| 28 | 'Gtk2::MessageDialog'; | ||
| 29 | |||
| 30 | sub INIT_INSTANCE { | ||
| 31 | my ($self) = @_; | ||
| 32 | |||
| 33 | $self->set (message_type => 'question', | ||
| 34 | modal => 1, | ||
| 35 | title => __('Chart: Jobs Running Query'), | ||
| 36 | text => __('Job(s) still running, stop them ?'), | ||
| 37 | destroy_with_parent => 1); | ||
| 38 | $self->add_buttons ('gtk-ok' => 'ok', | ||
| 39 | 'gtk-cancel' => 'close'); | ||
| 40 | $self->signal_connect (response => \&_do_response); | ||
| 41 | |||
| 42 | require App::Chart::Gtk2::JobQueue; | ||
| 43 | my $model = Gtk2::TreeModelFilter->new (App::Chart::Gtk2::JobQueue->instance); | ||
| 44 | $model->set_visible_func | ||
| 45 | (sub { | ||
| 46 | my ($jobqueue, $iter) = @_; | ||
| 47 | return _job_is_queryable ($jobqueue->get($iter,0)); | ||
| 48 | }); | ||
| 49 | $model->get_iter_first || return; | ||
| 50 | |||
| 51 | my $vbox = $self->vbox; | ||
| 52 | my $treeview = Gtk2::TreeView->new_with_model ($model); | ||
| 53 | $treeview->set (headers_visible => 0); | ||
| 54 | $vbox->pack_start ($treeview, 0,0,0); | ||
| 55 | |||
| 56 | my $column = Gtk2::TreeViewColumn->new; | ||
| 57 | my $renderer = Gtk2::CellRendererText->new; | ||
| 58 | $renderer->set (xalign => 0, | ||
| 59 | ypad => 0); | ||
| 60 | $column->pack_start ($renderer, 1); | ||
| 61 | require App::Chart::Gtk2::DownloadDialog; | ||
| 62 | $column->set_cell_data_func | ||
| 63 | ($renderer, \&App::Chart::Gtk2::DownloadDialog::_job_cell_status); | ||
| 64 | $treeview->append_column ($column); | ||
| 65 | |||
| 66 | $vbox->show_all; | ||
| 67 | } | ||
| 68 | |||
| 69 | sub _job_is_queryable { | ||
| 70 | my ($job) = @_; | ||
| 71 | return ($job->is_stoppable | ||
| 72 | && ($job->isa('App::Chart::Gtk2::Job::Download') | ||
| 73 | # not a subclass at the moment ... | ||
| 74 | || $job->get('name') eq __('Vacuum database'))); | ||
| 75 | } | ||
| 76 | |||
| 77 | # 'response' signal handler | ||
| 78 | sub _do_response { | ||
| 79 | my ($self, $response) = @_; | ||
| 80 | |||
| 81 | if ($response eq 'ok') { | ||
| 82 | Gtk2::Ex::WidgetCursor->busy; | ||
| 83 | if (my $parent = $self->get_transient_for) { | ||
| 84 | $parent->destroy; | ||
| 85 | } else { | ||
| 86 | warn 'JobsRunningDialog: no parent to destroy'; | ||
| 87 | } | ||
| 88 | $self->destroy; | ||
| 89 | |||
| 90 | } elsif ($response eq 'close') { | ||
| 91 | # as per a keyboard close, defaults to raising 'delete-event', which | ||
| 92 | # in turn defaults to a destroy | ||
| 93 | $self->signal_emit ('close'); | ||
| 94 | } | ||
| 95 | } | ||
| 96 | |||
| 97 | sub popup { | ||
| 98 | my ($class, $parent) = @_; | ||
| 99 | |||
| 100 | # if "modal" is obeyed by the window manager then there won't be any other | ||
| 101 | # JobsRunningDialog's open, but it doesn't hurt to let dialog_popup search | ||
| 102 | require App::Chart::Gtk2::Ex::ToplevelBits; | ||
| 103 | return App::Chart::Gtk2::Ex::ToplevelBits::dialog_popup | ||
| 104 | (class => $class, | ||
| 105 | transient_for => $parent); | ||
| 106 | } | ||
| 107 | |||
| 108 | sub query_and_quit { | ||
| 109 | my ($class, $parent) = @_; | ||
| 110 | if (App::Chart::Gtk2::JobQueue->can('instance') | ||
| 111 | && (List::Util::first {_job_is_queryable($_)} | ||
| 112 | App::Chart::Gtk2::JobQueue->all_jobs)) { | ||
| 113 | $class->popup ($parent); | ||
| 114 | } else { | ||
| 115 | $parent->destroy; | ||
| 116 | } | ||
| 117 | } | ||
| 118 | |||
| 119 | 1; | ||
| 120 | __END__ | ||
| 121 | |||
| 122 | =for stopwords ok Intraday | ||
| 123 | |||
| 124 | =head1 NAME | ||
| 125 | |||
| 126 | App::Chart::Gtk2::JobsRunningDialog -- query user about running jobs before quitting | ||
| 127 | |||
| 128 | =for test_synopsis my ($main_window) | ||
| 129 | |||
| 130 | =head1 SYNOPSIS | ||
| 131 | |||
| 132 | use App::Chart::Gtk2::JobsRunningDialog; | ||
| 133 | App::Chart::Gtk2::JobsRunningDialog->query_and_quit ($main_window); | ||
| 134 | |||
| 135 | =head1 WIDGET HIERARCHY | ||
| 136 | |||
| 137 | C<App::Chart::Gtk2::JobsRunningDialog> is a subclass of C<Gtk2::MessageDialog>. | ||
| 138 | |||
| 139 | Gtk2::Widget | ||
| 140 | Gtk2::Container | ||
| 141 | Gtk2::Bin | ||
| 142 | Gtk2::Window | ||
| 143 | Gtk2::Dialog | ||
| 144 | Gtk2::MessageDialog | ||
| 145 | App::Chart::Gtk2::JobsRunningDialog | ||
| 146 | |||
| 147 | =head1 DESCRIPTION | ||
| 148 | |||
| 149 | A C<App::Chart::Gtk2::JobsRunningDialog> asks the user whether to kill running jobs | ||
| 150 | before quitting. Cancel means don't quit, ok means kill the jobs. | ||
| 151 | |||
| 152 | =head1 FUNCTIONS | ||
| 153 | |||
| 154 | =over 4 | ||
| 155 | |||
| 156 | =item C<< App::Chart::Gtk2::JobsRunningDialog->query_and_quit ($main_window) >> | ||
| 157 | |||
| 158 | C<$main_window> is a C<Gtk2::Window> which is the application main window. | ||
| 159 | If there's any running or pending Download or Vacuum jobs in the | ||
| 160 | C<App::Chart::Gtk2::JobQueue> then popup a JobsRunningDialog asking the user | ||
| 161 | whether to kill them and quit, or cancel to not quit. | ||
| 162 | |||
| 163 | If the user chooses to kill then C<< $main_window->destroy() >> is called. | ||
| 164 | C<$main_window> should be set up to exit the main loop if destroyed and the | ||
| 165 | jobs will be stopped or discarded in the usual way when garbage collected. | ||
| 166 | |||
| 167 | If there's no running or pending jobs then C<< $main_window->destroy() >> is | ||
| 168 | called immediately, without any dialog. Intraday image downloads and latest | ||
| 169 | quote downloads are ignored for quit query purposes since they're only for | ||
| 170 | immediate display, they're not user initiated database updates. | ||
| 171 | |||
| 172 | =back | ||
| 173 | |||
| 174 | =head1 SEE ALSO | ||
| 175 | |||
| 176 | L<App::Chart::Gtk2::Job>, L<App::Chart::Gtk2::JobQueue>, L<Gtk2::MessageDialog> | ||
| 177 | |||
| 178 | =head1 HOME PAGE | ||
| 179 | |||
| 180 | L<http://user42.tuxfamily.org/chart/index.html> | ||
| 181 | |||
| 182 | =head1 LICENCE | ||
| 183 | |||
| 184 | Copyright 2009, 2010, 2011, 2013 Kevin Ryde | ||
| 185 | |||
| 186 | Chart is free software; you can redistribute it and/or modify it under the | ||
| 187 | terms of the GNU General Public License as published by the Free Software | ||
| 188 | Foundation; either version 3, or (at your option) any later version. | ||
| 189 | |||
| 190 | Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 191 | WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 192 | FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 193 | details. | ||
| 194 | |||
| 195 | You should have received a copy of the GNU General Public License along with | ||
| 196 | Chart; see the file F<COPYING>. Failing that, see | ||
| 197 | L<http://www.gnu.org/licenses/>. | ||
| 198 | |||
| 199 | =cut |