| 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::IntradaySave; | ||
| 18 | use 5.010; | ||
| 19 | use strict; | ||
| 20 | use warnings; | ||
| 21 | use Gtk2; | ||
| 22 | use Locale::TextDomain ('App-Chart'); | ||
| 23 | |||
| 24 | use Gtk2::Ex::Units; | ||
| 25 | use App::Chart::Glib::Ex::MoreUtils; | ||
| 26 | use App::Chart; | ||
| 27 | |||
| 28 | # uncomment this to run the ### lines | ||
| 29 | #use Smart::Comments; | ||
| 30 | |||
| 31 | use Glib::Object::Subclass | ||
| 32 | 'Gtk2::FileChooserDialog', | ||
| 33 | signals => { delete_event => \&Gtk2::Widget::hide_on_delete }, | ||
| 34 | properties => [Glib::ParamSpec->string | ||
| 35 | ('symbol', | ||
| 36 | __('Symbol'), | ||
| 37 | 'The symbol of the stock or commodity to be shown', | ||
| 38 | '', # default | ||
| 39 | Glib::G_PARAM_READWRITE), | ||
| 40 | |||
| 41 | Glib::ParamSpec->string | ||
| 42 | ('mode', | ||
| 43 | 'mode', | ||
| 44 | 'The graph mode, such as 1 day or 5 days', | ||
| 45 | '', # default | ||
| 46 | Glib::G_PARAM_READWRITE)]; | ||
| 47 | |||
| 48 | use constant { RESPONSE_REFRESH => 0, | ||
| 49 | RESPONSE_CROSS => 1 }; | ||
| 50 | |||
| 51 | |||
| 52 | sub new { | ||
| 53 | my $class = shift; | ||
| 54 | # pending support for object "constructor" thingie | ||
| 55 | $class->SUPER::new (action => 'save', @_); | ||
| 56 | } | ||
| 57 | |||
| 58 | sub INIT_INSTANCE { | ||
| 59 | my ($self) = @_; | ||
| 60 | $self->{'symbol'} = ''; # defaults | ||
| 61 | $self->{'mode'} = ''; | ||
| 62 | |||
| 63 | $self->set_title (__('Chart: Save Intraday Image')); | ||
| 64 | $self->add_buttons ('gtk-save' => 'accept', | ||
| 65 | 'gtk-cancel' => 'cancel', | ||
| 66 | # 'gtk-help' => 'help' | ||
| 67 | ); | ||
| 68 | |||
| 69 | # connect to self instead of a class handler since as of Gtk2-Perl 1.200 a | ||
| 70 | # Gtk2::Dialog class handler for 'response' is called with response IDs as | ||
| 71 | # numbers, not enum strings like 'accept' | ||
| 72 | # | ||
| 73 | # this is an "after" to allow a user's signals to be called first on | ||
| 74 | # 'close' or 'delete-event', since we're going to $self->destroy on those | ||
| 75 | # | ||
| 76 | $self->signal_connect_after (response => \&_do_response); | ||
| 77 | |||
| 78 | my $vbox = $self->vbox; | ||
| 79 | { | ||
| 80 | my $label = Gtk2::Label->new (__('Save intraday image to file') | ||
| 81 | . "\n\nCaution: This is a bit rough yet."); | ||
| 82 | $label->show; | ||
| 83 | $vbox->pack_start ($label, 0,0,0); | ||
| 84 | $vbox->reorder_child ($label, 0); # at the top of the dialog | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | sub SET_PROPERTY { | ||
| 89 | my ($self, $pspec, $newval) = @_; | ||
| 90 | my $pname = $pspec->get_name; | ||
| 91 | $self->{$pname} = $newval; # per default GET_PROPERTY | ||
| 92 | |||
| 93 | if ($pname eq 'symbol') { | ||
| 94 | my $symbol = $newval; | ||
| 95 | my $entry = $self->{'entry'}; | ||
| 96 | $entry->set_text ($symbol); | ||
| 97 | Gtk2::Ex::EntryBits::select_region_noclip ($entry, 0, -1); | ||
| 98 | } | ||
| 99 | _update_title_label ($self); | ||
| 100 | _update_job_status ($self); | ||
| 101 | $self->refresh_old; | ||
| 102 | } | ||
| 103 | |||
| 104 | sub _do_response { | ||
| 105 | my ($self, $response) = @_; | ||
| 106 | ### IntradaySave response: $response | ||
| 107 | |||
| 108 | if ($response eq 'accept') { | ||
| 109 | $self->save; | ||
| 110 | |||
| 111 | } elsif ($response eq 'cancel') { | ||
| 112 | # raise 'close' as per a keyboard Esc to close, which defaults to | ||
| 113 | # raising 'delete-event', which is setup as a hide() above | ||
| 114 | $self->signal_emit ('close'); | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | sub popup { | ||
| 119 | my ($class, $intradaydialog) = @_; | ||
| 120 | require App::Chart::Gtk2::Ex::ToplevelBits; | ||
| 121 | return App::Chart::Gtk2::Ex::ToplevelBits::popup | ||
| 122 | ($class, | ||
| 123 | properties => { transient_for => $intradaydialog }, | ||
| 124 | screen => $intradaydialog); | ||
| 125 | } | ||
| 126 | |||
| 127 | sub save { | ||
| 128 | my ($self) = @_; | ||
| 129 | my $intradaydialog = $self->get_transient_for; | ||
| 130 | my ($symbol, $mode) = $intradaydialog->get('symbol','mode'); | ||
| 131 | |||
| 132 | my $dbh = App::Chart::DBI->instance; | ||
| 133 | my $sth = $dbh->prepare_cached | ||
| 134 | ('SELECT image, error FROM intraday_image WHERE symbol=? AND mode=?'); | ||
| 135 | my ($blob, $error) = $dbh->selectrow_array | ||
| 136 | ($sth, undef, $self->{'symbol'}, $self->{'mode'}); | ||
| 137 | $sth->finish(); | ||
| 138 | if (defined $blob) { | ||
| 139 | my $filename = $self->get_filename; | ||
| 140 | open my $fh, '>', $filename or die; | ||
| 141 | binmode ($fh) or die; | ||
| 142 | print $fh $blob or die; | ||
| 143 | close $fh or die; | ||
| 144 | } else { | ||
| 145 | my $msg = Gtk2::MessageDialog->new ($self, | ||
| 146 | ['modal','destroy-with-parent'], | ||
| 147 | 'error', | ||
| 148 | 'ok', | ||
| 149 | "No image to save: %s", | ||
| 150 | $error||__('(No data)')); | ||
| 151 | $msg->signal_connect (response => sub { | ||
| 152 | my ($msg) = @_; | ||
| 153 | $msg->destroy; | ||
| 154 | }); | ||
| 155 | $msg->present; | ||
| 156 | } | ||
| 157 | } | ||
| 158 | |||
| 159 | 1; | ||
| 160 | __END__ |