| 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 | ▶ | use 5.010; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 19 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 20 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 21 | 0 | ▶ | use Gtk2; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 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__ | ||