App/Chart/Gtk2/JobsRunningDialog.pm untested

filter: Statementstmt 0.0% 0 / 73 Branchbran 0.0% 0 / 8 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 16 Podpod 0.0% 0 / 5 totaltotal 0.0% 0 / 120 CC 5 SCAR 17.9
CC
5
Cov
0%
_do_response
20.0
query_and_quit
6.0
INIT_INSTANCE
2.0
CRAP 6.0
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__