App/Chart/Gtk2/DownloadDialog.pm untested

filter: Statementstmt 0.0% 0 / 305 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 31 MC/DCmcdc 0.0% 0 / 18 Subroutinesub 0.0% 0 / 48 Podpod 0.0% 0 / 21 totaltotal 0.0% 0 / 457 CC 18 SCAR 26.2
CC
18
Cov
0%
_do_response
72.0
_do_start_button
30.0
_do_job_row_changed
12.0
CRAP 13.7
1 # Copyright 2007, 2008, 2009, 2010, 2011 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::DownloadDialog;
18 use 5.010;
19 use strict;
20 use warnings;
21 use Glib::Ex::SignalIds;
22 use Gtk2 1.220;
23 use Gtk2::Ex::Units;
24 use Locale::TextDomain ('App-Chart');
25 use Regexp::Common 'whitespace';
26  
27 use App::Chart::Glib::Ex::MoreUtils;
28 use App::Chart;
29 use App::Chart::Gtk2::GUI;
30 use App::Chart::Gtk2::Job;
31 use App::Chart::Gtk2::JobQueue;
32 use App::Chart::Gtk2::Subprocess;
33  
34 # uncomment this to run the ### lines
35 #use Smart::Comments;
36  
37  
38 use base 'App::Chart::Gtk2::Ex::ToplevelSingleton';
39 use Glib::Object::Subclass
40 'Gtk2::Dialog',
41 signals => { destroy => \&_do_destroy };
42  
43 use constant { RESPONSE_START => 0,
44 RESPONSE_STOP => 1,
45 RESPONSE_CLEAR => 2 };
46  
47 use constant { WHEN_UPDATE => 0,
48 WHEN_BACKTO => 1 };
49  
50 sub popup {
51 my ($class, $symbol, $parent) = @_;
52 require App::Chart::Gtk2::Ex::ToplevelBits;
53 my $dialog = App::Chart::Gtk2::Ex::ToplevelBits::popup
54 ($class,
55 hide_on_delete => 1,
56 screen => $parent);
57 if (defined $symbol) {
58 $dialog->{'entry'}->set_text($symbol);
59 }
60 return $dialog;
61 }
62 sub popup_update {
63 my ($class, $symbol, $parent) = @_;
64 my $dialog = $class->popup ($symbol, $parent);
65 $dialog->start ($symbol, undef);
66 $dialog->{'hide_on_success'} = 1;
67 return $dialog;
68 }
69  
70 sub INIT_INSTANCE {
71 my ($self) = @_;
72  
73 $self->set_title (__('Chart: Download'));
74 $self->add_buttons (__('_Start') => RESPONSE_START,
75 __('_Stop') => RESPONSE_STOP,
76 'gtk-clear' => RESPONSE_CLEAR,
77 'gtk-close' => 'close',
78 'gtk-help' => 'help');
79 $self->signal_connect (response => \&_do_response);
80 my $vbox = $self->vbox;
81  
82 my $renderer = Gtk2::CellRendererText->new;
83 $renderer->set (xalign => 0,
84 ypad => 0);
85  
86 { my $label = Gtk2::Label->new (__('Subprocesses'));
87 $label->set (xalign => 0);
88 $vbox->pack_start ($label, 0,0,0);
89 }
90  
91 my $proc_scrolled = Gtk2::ScrolledWindow->new;
92 $proc_scrolled->set (hscrollbar_policy => 'automatic',
93 vscrollbar_policy => 'automatic');
94 $vbox->pack_start ($proc_scrolled, 1,1,0);
95  
96 {
97 require App::Chart::Gtk2::Subprocess;
98 my $treeview = $self->{'proc_treeview'}
99 = Gtk2::TreeView->new_with_model ($App::Chart::Gtk2::Subprocess::store);
100 $treeview->set (headers_visible => 0,
101 reorderable => 1);
102 $proc_scrolled->add ($treeview);
103 $treeview->add_events ('button-press-mask');
104 $treeview->signal_connect
105 (button_press_event => \&_do_proc_treeview_button_press, $self);
106  
107 {
108 my $column = Gtk2::TreeViewColumn->new;
109 $column->pack_start ($renderer, 1);
110 $column->set_cell_data_func ($renderer, \&_proc_cell_status);
111 $treeview->append_column ($column);
112 }
113 }
114  
115 { my $label = Gtk2::Label->new (__('Jobs'));
116 $label->set (xalign => 0);
117 $vbox->pack_start ($label, 0,0,0);
118 }
119  
120 my $model = $self->{'model'} = App::Chart::Gtk2::JobQueue->instance;
121 $self->{'model_ids'} = Glib::Ex::SignalIds->new
122 ($model,
123 $model->signal_connect (row_changed => \&_do_job_row_changed, $self));
124  
125 my $jobs_scrolled = Gtk2::ScrolledWindow->new;
126 $jobs_scrolled->set (hscrollbar_policy => 'automatic',
127 vscrollbar_policy => 'automatic');
128 $vbox->pack_start ($jobs_scrolled, 1,1,0);
129  
130 my $treeview = $self->{'jobs_treeview'}
131 = Gtk2::TreeView->new_with_model ($model);
132 $treeview->set (headers_visible => 0,
133 reorderable => 1);
134 $jobs_scrolled->add ($treeview);
135 $treeview->add_events ('button-press-mask');
136 $treeview->signal_connect
137 (button_press_event => \&_do_jobs_treeview_button_press);
138  
139 my $selection = $treeview->get_selection();
140 $selection->signal_connect (changed => \&_do_selection_changed, $self);
141 $selection->set_mode ('single');
142  
143 {
144 my $column = Gtk2::TreeViewColumn->new;
145 $column->pack_start ($renderer, 1);
146 $column->set_cell_data_func ($renderer, \&_job_cell_status);
147 $treeview->append_column ($column);
148 }
149  
150 { my $label = Gtk2::Label->new(__('Messages'));
151 $label->set (xalign => 0);
152 $vbox->pack_start ($label, 0,0,0);
153 }
154 my $textbuf = $self->{'textbuf'} = Gtk2::TextBuffer->new();
155 $textbuf->signal_connect ('changed', \&_do_textbuf_changed, $self);
156  
157 require Gtk2::Ex::TextView::FollowAppend;
158 my $textview = $self->{'textview'}
159 = Gtk2::Ex::TextView::FollowAppend->new_with_buffer ($textbuf);
160 $textview->set (wrap_mode => 'char',
161 editable => 0);
162  
163 my $messages_scrolled = Gtk2::ScrolledWindow->new();
164 $messages_scrolled->add($textview);
165 $messages_scrolled->set_policy('never', 'always');
166 $vbox->pack_start ($messages_scrolled, 1, 1, 0);
167  
168 # During perl "global destruction" can have App::Chart::Gtk2::Job already
169 # destroyed enough that it has disconnected the message emission hook
170 # itself, leading to an unsightly Glib warning if attempting
171 # signal_remove_emission_hook() in our 'destroy' class closure. So
172 # instead leave it connected, with a weakened ref, and let it return 0 to
173 # disconnect itself on the next emission (if any).
174 #
175 # App::Chart::Gtk2::Job->signal_add_emission_hook
176 # (message => \&_do_job_message, App::Chart::Glib::Ex::MoreUtils::ref_weak ($self));
177 #
178 require App::Chart::Glib::Ex::EmissionHook;
179 $self->{'hook'} = App::Chart::Glib::Ex::EmissionHook->new
180 ('App::Chart::Gtk2::Job',
181 message => \&_do_job_message,
182 App::Chart::Glib::Ex::MoreUtils::ref_weak($self));
183  
184 my $hbox = Gtk2::HBox->new (0, 0);
185 $hbox->pack_start (Gtk2::Label->new (__('What:')), 0,0,0);
186 $vbox->pack_start ($hbox, 0,0,0);
187  
188 my $what_model = $self->{'what_model'}
189 = Gtk2::ListStore->new ('Glib::String', 'Glib::Scalar');
190 $what_model->set ($what_model->append, 0, __('One symbol'), 1, undef);
191 $what_model->set ($what_model->append, 0, __('Favourites'), 1, 'favourites');
192 $what_model->set ($what_model->append, 0, __('All'), 1, 'all');
193  
194 my $what_combobox = $self->{'what_combobox'}
195 = Gtk2::ComboBox->new_with_model ($what_model);
196 my $what_renderer = Gtk2::CellRendererText->new;
197 $what_combobox->pack_start ($what_renderer, 1);
198 $what_combobox->set_attributes ($what_renderer, text => 0);
199 $hbox->pack_start ($what_combobox, 0,0,0);
200  
201 my $entry = $self->{'entry'} = Gtk2::Entry->new;
202 $hbox->pack_start ($entry, 1,1, 0.5 * Gtk2::Ex::Units::em($entry));
203 $what_combobox->signal_connect ('changed', \&_do_what_changed, $self);
204 $what_combobox->set_active (1);
205  
206 $hbox->pack_start (Gtk2::Label->new (' ' . __('When:')), 0,0,0);
207  
208 my $when_model = $self->{'when_model'}
209 = Gtk2::ListStore->new ('Glib::String', 'Glib::Scalar');
210 $when_model->insert_with_values (WHEN_UPDATE, 0 => __('Update'));
211 $when_model->insert_with_values (WHEN_BACKTO, 0 => __('Backto'));
212  
213 my $when_combobox = $self->{'when_combobox'}
214 = Gtk2::ComboBox->new_with_model ($when_model);
215 my $when_renderer = Gtk2::CellRendererText->new;
216 $when_combobox->pack_start ($when_renderer, 1);
217 $when_combobox->set_attributes ($when_renderer, text => 0);
218 $hbox->pack_start ($when_combobox, 0,0,0);
219 $when_combobox->set_active (WHEN_UPDATE);
220 $when_combobox->signal_connect ('changed', \&_do_when_changed, $self);
221  
222 require Date::Calc;
223 my ($today_year, undef, undef) = Date::Calc::Today();
224 my $when_adj = Gtk2::Adjustment->new ($today_year-5, # initial
225 1800, $today_year+1, # min,max
226 1,10,
227 0); # page_size
228 my $when_spin = $self->{'when_spin'}
229 = Gtk2::SpinButton->new ($when_adj, 10, 0);
230 $hbox->pack_start ($when_spin, 0,0,0);
231 $when_spin->set_sensitive (0);
232  
233 _update_stop_sensitive ($self);
234 _update_clear_sensitive ($self);
235  
236 $vbox->show_all;
237  
238 # with sensible jobs view, message text and entry sizes
239 # FIXME: the initial proportions don't come out with the plain VBox packing
240 Gtk2::Ex::Units::set_default_size_with_subsizes
241 ($self,
242 [$proc_scrolled, -1, '3 lines'],
243 [$jobs_scrolled, -1, '4 lines'],
244 [$messages_scrolled, '60 ems', '10 lines'],
245 [$entry, '10 ems', -1]);
246 }
247  
248 # 'destroy' class closure
249 # this can be called more than once!
250 sub _do_destroy {
251 my ($self) = @_;
252 ### DownloadDialog _do_destroy()
253  
254 delete $self->{'model_ids'};
255  
256 # break circular references
257 delete $self->{'textview'};
258 delete $self->{'textbuf'};
259  
260 return shift->signal_chain_from_overridden(@_);
261 }
262  
263 sub entry_symbol {
264 my ($self) = @_;
265 my $entry = $self->{'entry'};
266 return App::Chart::collapse_whitespace ($entry->get_text());
267 }
268  
269 sub _do_textbuf_changed {
270 my ($textbuf, $self) = @_;
271 _update_clear_sensitive ($self);
272 }
273  
274 sub _do_what_changed {
275 my ($combobox, $self) = @_;
276 my $idx = $self->{'what_combobox'}->get_active;
277 $self->{'entry'}->set_sensitive ($idx == 0);
278 }
279  
280 sub _do_when_changed {
281 my ($combobox, $self) = @_;
282 my $idx = $self->{'when_combobox'}->get_active;
283 $self->{'when_spin'}->set_sensitive ($idx == WHEN_BACKTO);
284 }
285  
286 # 'message' emission hook on App::Chart::Gtk2::Job
287 sub _do_job_message {
288 my ($invocation_hint, $parameters, $ref_weak_self) = @_;
289 my $self = $$ref_weak_self || return 0; # disconnect
290 my ($job, $str) = @$parameters;
291 $self->message ($str);
292 return 1; # stay connected
293 }
294  
295 sub message {
296 my ($self, $str) = @_;
297 my $textbuf = $self->{'textview'}->get_buffer;
298 $textbuf->insert ($textbuf->get_end_iter, $str);
299 }
300  
301 sub start {
302 my ($self, $what, $when) = @_;
303 $self->{'hide_on_success'} = undef;
304  
305 my $queue = App::Chart::Gtk2::JobQueue->instance;
306 $queue->remove_done;
307 App::Chart::Gtk2::Subprocess->remove_done;
308  
309 require App::Chart::Gtk2::Job::Download;
310 my $job = App::Chart::Gtk2::Job::Download->start ($what, $when);
311  
312 # if this job is the only one then select it
313 if ($queue->iter_n_children(undef) == 1) {
314 my $treeview = $self->{'jobs_treeview'};
315 my $selection = $treeview->get_selection;
316 $selection->select_path (Gtk2::TreePath->new_from_indices(0));
317 }
318 return $job;
319 }
320  
321 sub _do_start_button {
322 my ($self) = @_;
323  
324 my $what = $self->{'what_combobox'}->get_active;
325 my $type;
326 if ($what == 0) {
327 $what = $self->{'entry'}->get_text;
328 $what =~ s/$RE{ws}{crop}//g; # leading and trailing whitespace
329 if ($what eq '') {
330 my $textbuf = $self->{'textbuf'};
331 $textbuf->insert ($textbuf->get_end_iter, "No symbol entered.\n");
332 $self->{'entry'}->grab_focus;
333 return;
334 }
335 $type = $what;
336 } elsif ($what == 1) {
337 $type = __('Favourites');
338 $what = '--favourites';
339 } else {
340 $type = __('All');
341 $what = '--all';
342 }
343  
344 my $when_index = $self->{'when_combobox'}->get_active;
345 my $when = ($when_index == WHEN_BACKTO
346 ? $self->{'when_spin'}->get_value
347 : undef);
348  
349 $self->start ($what, $when);
350 }
351  
352 # 'response' signal handler
353 sub _do_response {
354 my ($self, $response) = @_;
355 ### DownloadDialog _do_response(): $response
356  
357 if ($response eq RESPONSE_START) {
358 $self->_do_start_button;
359  
360 } elsif ($response eq RESPONSE_STOP) {
361 my $treeview = $self->{'jobs_treeview'};
362 my $selection = $treeview->get_selection;
363 my ($model, $iter) = $selection->get_selected;
364 my $job = $model->get_value ($iter, 0);
365 $job->stop;
366  
367 } elsif ($response eq RESPONSE_CLEAR) {
368 my $textbuf = $self->{'textbuf'};
369 $textbuf->delete ($textbuf->get_start_iter, $textbuf->get_end_iter);
370 if (App::Chart::Gtk2::JobQueue->can('remove_done')) { # if loaded
371 App::Chart::Gtk2::JobQueue->remove_done;
372 }
373 if (App::Chart::Gtk2::Subprocess->can('remove_done')) { # if loaded
374 App::Chart::Gtk2::Subprocess->remove_done;
375 }
376  
377 } elsif ($response eq 'close') {
378 # as per a keyboard close, defaults to raising 'delete-event', which in
379 # turn defaults to a destroy
380 $self->signal_emit ('close');
381  
382 } elsif ($response eq 'help') {
383 require App::Chart::Manual;
384 App::Chart::Manual->open(__p('manual-node','Download'), $self);
385 }
386 }
387  
388 sub _update_stop_sensitive {
389 my ($self) = @_;
390 my $treeview = $self->{'jobs_treeview'};
391 my $selection = $treeview->get_selection;
392 my ($model, $iter) = $selection->get_selected;
393 my $job = $iter && $model->get_value ($iter, 0);
394 my $sensitive = $job && $job->is_stoppable;
395 $self->set_response_sensitive (RESPONSE_STOP, $sensitive);
396 }
397 sub _update_clear_sensitive {
398 my ($self) = @_;
399  
400 my $anything_to_clear = do {
401 my $textbuf = $self->{'textbuf'};
402 $textbuf->get_char_count != 0
403 } || do {
404 App::Chart::Gtk2::JobQueue->can('remove_done') # if loaded
405 && List::Util::first {$_->get('done')} App::Chart::Gtk2::JobQueue->all_jobs;
406 } || do {
407 App::Chart::Gtk2::Subprocess->can('remove_done') # if loaded
408 && List::Util::first {! $_->pid} App::Chart::Gtk2::Subprocess->all_subprocesses;
409 };
410 $self->set_response_sensitive (RESPONSE_CLEAR, $anything_to_clear);
411 }
412  
413 # 'button-press-event' on the treeview
414 sub _do_proc_treeview_button_press {
415 my ($treeview, $event, $self) = @_;
416 if ($event->button == 3) {
417 require App::Chart::Gtk2::SubprocessStopMenu;
418 App::Chart::Gtk2::SubprocessStopMenu->popup_from_treeview ($event, $treeview);
419 }
420 return Gtk2::EVENT_PROPAGATE;
421 }
422  
423 # 'changed' on the treeview selection
424 sub _do_selection_changed {
425 my ($selection, $self) = @_;
426 _update_stop_sensitive ($self);
427 }
428  
429 # 'row-changed' on the JobQueue model
430 sub _do_job_row_changed {
431 my ($model, $path, $iter, $self) = @_;
432 _update_stop_sensitive ($self);
433  
434 if (my $hide = $self->{'hide_on_success'}) {
435 my $job = $model->get_value ($iter, 0);
436 if ($job == $hide && $job->status eq __('Done')) {
437 $self->hide;
438 }
439 }
440 }
441  
442 # 'set_cell_data_func' to display a cell in the job model row
443 sub _job_cell_status {
444 my ($treecolumn, $renderer, $model, $iter) = @_;
445 my $job = $model->get_value ($iter, 0);
446 my $str = join (' - ', $job->type // '', $job->status // ());
447 $renderer->set (text => $str);
448 }
449  
450 # 'set_cell_data_func' to display a cell in the subprocess model row
451 sub _proc_cell_status {
452 my ($treecolumn, $renderer, $model, $iter) = @_;
453 my $proc = $model->get_value ($iter, 0);
454 my $str = $proc->status;
455 $renderer->set (text => $str);
456 }
457  
458 # 'button-press-event' on the treeview
459 sub _do_jobs_treeview_button_press {
460 my ($treeview, $event) = @_;
461 if ($event->button == 3) {
462 require App::Chart::Gtk2::JobStopMenu;
463 my $self = $treeview->get_toplevel;
464 my $menu = ($self->{'job_menu'} ||= App::Chart::Gtk2::JobStopMenu->new);
465 $menu->popup_from_treeview ($event, $treeview);
466 }
467 return Gtk2::EVENT_PROPAGATE;
468 }
469  
470  
471 #------------------------------------------------------------------------------
472 # generic helpers
473  
474  
475 1;
476 __END__