File Coverage

blib/lib/App/Chart/Gtk2/DeleteDialog.pm
Criterion Covered Total %
statement 9 11 81.8
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 13 15 86.6


line stmt bran cond sub pod time code
1             # Copyright 2007, 2008, 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::DeleteDialog;
18 1     1   424 use 5.010;
  1         3  
19 1     1   5 use strict;
  1         1  
  1         17  
20 1     1   4 use warnings;
  1         1  
  1         20  
21 1     1   137 use Gtk2;
  0            
  0            
22             use Glib;
23             use Locale::TextDomain ('App-Chart');
24              
25             use App::Chart::Database;
26             use App::Chart::Gtk2::GUI;
27              
28             use Glib::Object::Subclass
29             'Gtk2::MessageDialog',
30             properties => [Glib::ParamSpec->string
31             ('symbol',
32             __('Symbol'),
33             'The stock or commodity symbol to ask about deleting',
34             '', # default
35             Glib::G_PARAM_READWRITE)];
36              
37             sub INIT_INSTANCE {
38             my ($self) = @_;
39              
40             $self->set (message_type => 'question',
41             modal => 1,
42             title => __('Chart: Delete Symbol'));
43             $self->add_buttons ('gtk-ok' => 'ok',
44             'gtk-cancel' => 'close');
45             $self->signal_connect (response => \&_do_response);
46             my $vbox = $self->vbox;
47              
48             my $notes_check = $self->{'notes_check'}
49             = Gtk2::CheckButton->new_with_label(__('And delete your annotations too'));
50             $notes_check->set_active (1);
51             $vbox->pack_start ($notes_check, 0,0,0);
52              
53             my $notes_none = $self->{'notes_none'}
54             = Gtk2::Label->new (__('(No annotations, just downloaded data.)'));
55             $notes_none->set_alignment (0, 0.5);
56             $vbox->pack_start ($notes_none, 0,0,0);
57             }
58              
59             sub SET_PROPERTY {
60             my ($self, $pspec, $newval) = @_;
61             my $pname = $pspec->get_name;
62             $self->{$pname} = $newval; # per default GET_PROPERTY
63              
64             if ($pname eq 'symbol') {
65             my $symbol = $newval;
66             $self->set (text => "\n" . __x('Delete symbol {symbol} ?',
67             symbol => $symbol));
68             if ($symbol && symbol_any_notes ($symbol)) {
69             $self->{'notes_check'}->show;
70             $self->{'notes_none'}->hide;
71             } else {
72             $self->{'notes_check'}->hide;
73             $self->{'notes_none'}->show;
74             }
75             }
76             }
77              
78             # 'response' signal handler
79             sub _do_response {
80             my ($self, $response) = @_;
81             if ($response eq 'ok') {
82             my $symbol = $self->get('symbol');
83             if (defined $symbol) {
84             # deleting is normally quite fast, but can be noticeable if the system
85             # is a bit loaded or there's a lot of daily data
86             require Gtk2::Ex::WidgetCursor;
87             Gtk2::Ex::WidgetCursor->busy;
88              
89             my $notes_too = $self->{'notes_check'}->get_active;
90             App::Chart::Database->delete_symbol ($symbol, $notes_too);
91             }
92             }
93             $self->destroy;
94             }
95              
96             # return true if $symbol has any notes in the database
97             sub symbol_any_notes {
98             my ($symbol) = @_;
99             my $nbh = App::Chart::DBI->instance;
100             my $sth = $nbh->prepare_cached
101             ('SELECT symbol FROM annotation WHERE symbol=? UNION ALL
102             SELECT symbol FROM line WHERE symbol=? UNION ALL
103             SELECT symbol FROM alert WHERE symbol=?
104             LIMIT 1');
105             my $row = $nbh->selectrow_arrayref ($sth, undef, $symbol, $symbol, $symbol);
106             $sth->finish;
107             return (defined $row);
108             }
109              
110             sub popup {
111             my ($class, $symbol, $parent) = @_;
112              
113             # supposed to be insensitive when no symbol, but check in case
114             if (! defined $symbol || $symbol eq '') {
115             return;
116             }
117              
118             # if "modal" is obeyed by the window manager then there won't be any other
119             # delete dialogs open, but it doesn't hurt to let popup() search
120             require App::Chart::Gtk2::Ex::ToplevelBits;
121             return App::Chart::Gtk2::Ex::ToplevelBits::popup
122             ($class,
123             transient_for => $parent,
124             properties => { symbol => $symbol });
125             }
126              
127             1;
128             __END__
129              
130             =for stopwords popup Eg
131              
132             =head1 NAME
133              
134             App::Chart::Gtk2::DeleteDialog -- query user to delete symbol from database
135              
136             =for test_synopsis my ($symbol, $parent_window)
137              
138             =head1 SYNOPSIS
139              
140             use App::Chart::Gtk2::DeleteDialog;
141             App::Chart::Gtk2::DeleteDialog->popup ($symbol, $parent_window);
142              
143             =head1 WIDGET HIERARCHY
144              
145             C<App::Chart::Gtk2::DeleteDialog> is a subclass of C<Gtk2::MessageDialog>.
146              
147             Gtk2::Widget
148             Gtk2::Container
149             Gtk2::Bin
150             Gtk2::Window
151             Gtk2::Dialog
152             Gtk2::MessageDialog
153             App::Chart::Gtk2::DeleteDialog
154              
155             =head1 DESCRIPTION
156              
157             A C<App::Chart::Gtk2::DeleteDialog> asks the user whether to delete a given symbol
158             from the database and if the answer is yes then it does so.
159              
160             =head1 FUNCTIONS
161              
162             =over 4
163              
164             =item C<< $dialog = App::Chart::Gtk2::DeleteDialog->popup ($symbol, $parent_window) >>
165              
166             Create and popup a dialog asking the user whether to delete C<$symbol>. The
167             dialog is created modal, and transient for the given C<$parent_window>. Eg.
168              
169             App::Chart::Gtk2::DeleteDialog->popup ('FOO.ZZ', $toplevel);
170              
171             The return value is the dialog created, but usually that can be ignored --
172             when the user answers it the dialog is destroyed.
173              
174             =item C<< $dialog = App::Chart::Gtk2::DeleteDialog->new (key=>value,...) >>
175              
176             Create and return a C<App::Chart::Gtk2::DeleteDialog>. Optional key/value
177             pairs set initial properties as per C<< Glib::Object->new() >>. The dialog
178             is not displayed (but can be with C<show> in the usual way).
179              
180             =back
181              
182             =head1 PROPERTIES
183              
184             =over 4
185              
186             =item C<symbol> (string)
187              
188             The stock symbol to ask the user about and delete. This can be changed
189             while the dialog is open (and the question text updates accordingly), but
190             doing so is likely to confuse the user.
191              
192             =back
193              
194             =head1 SEE ALSO
195              
196             L<App::Chart::Database>, L<Gtk2::MessageDialog>
197              
198             =head1 HOME PAGE
199              
200             L<http://user42.tuxfamily.org/chart/index.html>
201              
202             =head1 LICENCE
203              
204             Copyright 2007, 2008, 2009, 2010, 2011, 2013 Kevin Ryde
205              
206             Chart is free software; you can redistribute it and/or modify it under the
207             terms of the GNU General Public License as published by the Free Software
208             Foundation; either version 3, or (at your option) any later version.
209              
210             Chart is distributed in the hope that it will be useful, but WITHOUT ANY
211             WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
212             FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
213             details.
214              
215             You should have received a copy of the GNU General Public License along with
216             Chart; see the file F<COPYING>. Failing that, see
217             L<http://www.gnu.org/licenses/>.
218              
219             =cut