| 1 | # update entries if database changes | ||
| 2 | |||
| 3 | |||
| 4 | |||
| 5 | # Copyright 2008, 2009, 2010, 2011 Kevin Ryde | ||
| 6 | |||
| 7 | # This file is part of Chart. | ||
| 8 | # | ||
| 9 | # Chart is free software; you can redistribute it and/or modify it under the | ||
| 10 | # terms of the GNU General Public License as published by the Free Software | ||
| 11 | # Foundation; either version 3, or (at your option) any later version. | ||
| 12 | # | ||
| 13 | # Chart is distributed in the hope that it will be useful, but WITHOUT ANY | ||
| 14 | # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS | ||
| 15 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more | ||
| 16 | # details. | ||
| 17 | # | ||
| 18 | # You should have received a copy of the GNU General Public License along | ||
| 19 | # with Chart. If not, see <http://www.gnu.org/licenses/>. | ||
| 20 | |||
| 21 | package App::Chart::Gtk2::PreferencesDialog; | ||
| 22 | use 5.010; | ||
| 23 | use strict; | ||
| 24 | use warnings; | ||
| 25 | use Gtk2; | ||
| 26 | use Gtk2::Ex::Units; | ||
| 27 | use Locale::TextDomain ('App-Chart'); | ||
| 28 | |||
| 29 | use App::Chart; | ||
| 30 | use App::Chart::Database; | ||
| 31 | use App::Chart::DBI; | ||
| 32 | |||
| 33 | # use App::Chart::Gtk2::Ex::ToplevelSingleton hide_on_delete => 1; | ||
| 34 | # use base 'App::Chart::Gtk2::Ex::ToplevelSingleton'; | ||
| 35 | # sub popup { | ||
| 36 | # my ($class, $parent) = @_; | ||
| 37 | # my $self = $class->instance; | ||
| 38 | # $self->present; | ||
| 39 | # return $self; | ||
| 40 | # } | ||
| 41 | |||
| 42 | use Glib::Object::Subclass | ||
| 43 | 'Gtk2::Dialog', | ||
| 44 | signals => { show => \&_do_show }, | ||
| 45 | properties => []; | ||
| 46 | |||
| 47 | |||
| 48 | my @preferences = ( { key => 'lme-username', | ||
| 49 | name => __('LME Username'), | ||
| 50 | type => 'string', | ||
| 51 | default => '' }, | ||
| 52 | { key => 'lme-password', | ||
| 53 | name => __('LME Password'), | ||
| 54 | type => 'string', | ||
| 55 | default => '' }, | ||
| 56 | ); | ||
| 57 | |||
| 58 | use constant RESPONSE_SAVE => 0; | ||
| 59 | |||
| 60 | sub INIT_INSTANCE { | ||
| 61 | my ($self) = @_; | ||
| 62 | |||
| 63 | $self->set_title (__('Chart: Preferences')); | ||
| 64 | $self->add_buttons ('gtk-save' => RESPONSE_SAVE, | ||
| 65 | 'gtk-ok' => 'ok', | ||
| 66 | 'gtk-cancel' => 'cancel', | ||
| 67 | 'gtk-help' => 'help'); | ||
| 68 | $self->signal_connect (response => \&_do_response); | ||
| 69 | |||
| 70 | my $vbox = $self->vbox; | ||
| 71 | |||
| 72 | my @elements; | ||
| 73 | $self->{'elements'} = \@elements; | ||
| 74 | |||
| 75 | foreach my $pref (@preferences) { | ||
| 76 | my $key = $pref->{'key'}; | ||
| 77 | my $name = $pref->{'name'}; | ||
| 78 | my $type = $pref->{'type'}; | ||
| 79 | |||
| 80 | my $hbox = Gtk2::HBox->new (0, 0); | ||
| 81 | $hbox->{'key'} = $key; | ||
| 82 | $hbox->{'type'} = $type; | ||
| 83 | push @elements, $hbox; | ||
| 84 | $vbox->pack_start ($hbox, 0,0, | ||
| 85 | 0.25 * Gtk2::Ex::Units::line_height($hbox)); | ||
| 86 | |||
| 87 | my $label = Gtk2::Label->new ($name . " "); | ||
| 88 | $label->set (xalign => 0); # left | ||
| 89 | $hbox->pack_start ($label, 0,0,0); | ||
| 90 | |||
| 91 | if ($type eq 'string') { | ||
| 92 | my $entry = $hbox->{'entry'} = Gtk2::Entry->new (); | ||
| 93 | $hbox->pack_start ($entry, 1,1,0); | ||
| 94 | $entry->signal_connect | ||
| 95 | (changed => sub { $self->set_response_sensitive (RESPONSE_SAVE, 1); }); | ||
| 96 | |||
| 97 | } elsif ($type eq 'boolean') { | ||
| 98 | my $checkbox = $hbox->{'checkbox'} = Gtk2::CheckButton->new; | ||
| 99 | $hbox->pack_start ($checkbox, 0,0,0); | ||
| 100 | $checkbox->signal_connect | ||
| 101 | (toggled => sub { $self->set_response_sensitive (RESPONSE_SAVE, 1); }); | ||
| 102 | } | ||
| 103 | } | ||
| 104 | $vbox->show_all; | ||
| 105 | } | ||
| 106 | |||
| 107 | # 'show' class closure | ||
| 108 | sub _do_show { | ||
| 109 | my ($self) = @_; | ||
| 110 | $self->load; | ||
| 111 | return shift->signal_chain_from_overridden(@_); | ||
| 112 | } | ||
| 113 | |||
| 114 | # 'response' signal handler | ||
| 115 | sub _do_response { | ||
| 116 | my ($self, $response) = @_; | ||
| 117 | |||
| 118 | if ($response eq RESPONSE_SAVE) { | ||
| 119 | $self->save; | ||
| 120 | |||
| 121 | } elsif ($response eq 'ok') { | ||
| 122 | $self->save; | ||
| 123 | $self->hide; | ||
| 124 | |||
| 125 | } elsif ($response eq 'cancel') { | ||
| 126 | $self->hide; | ||
| 127 | |||
| 128 | } elsif ($response eq 'help') { | ||
| 129 | require App::Chart::Manual; | ||
| 130 | App::Chart::Manual->open(__p('manual-node','Preferences'), $self); | ||
| 131 | } | ||
| 132 | } | ||
| 133 | |||
| 134 | sub load { | ||
| 135 | my ($self) = @_; | ||
| 136 | my $dbh = App::Chart::DBI->instance; | ||
| 137 | my $aref; | ||
| 138 | my $sth = $dbh->prepare_cached ('SELECT key, value FROM preference'); | ||
| 139 | $aref = $dbh->selectall_arrayref ($sth); | ||
| 140 | $sth->finish; | ||
| 141 | _set_widget_values ($self, $aref); | ||
| 142 | $self->set_response_sensitive (RESPONSE_SAVE, 0); | ||
| 143 | } | ||
| 144 | |||
| 145 | # aref is an array reference of pairs [ ['key','value'],['key','value'],... ] | ||
| 146 | sub _set_widget_values { | ||
| 147 | my ($self, $aref) = @_; | ||
| 148 | |||
| 149 | PAIR: foreach my $pair (@$aref) { | ||
| 150 | my $key = $pair->[0]; | ||
| 151 | my $value = $pair->[1]; | ||
| 152 | |||
| 153 | foreach my $element (@{$self->{'elements'}}) { | ||
| 154 | if ($element->{'key'} ne $key) { next; } | ||
| 155 | my $type = $element->{'type'}; | ||
| 156 | if ($type eq 'string') { | ||
| 157 | $element->{'entry'}->set_text ($value); | ||
| 158 | } elsif ($type eq 'boolean') { | ||
| 159 | $element->{'checkbox'}->set_active ($value); | ||
| 160 | } else { | ||
| 161 | die "oops, unknown type ". $element->{'type'}; | ||
| 162 | } | ||
| 163 | next PAIR; | ||
| 164 | } | ||
| 165 | } | ||
| 166 | } | ||
| 167 | |||
| 168 | sub save { | ||
| 169 | my ($self) = @_; | ||
| 170 | my @pairs = _get_widget_values ($self); | ||
| 171 | my $dbh = App::Chart::DBI->instance; | ||
| 172 | App::Chart::Database::call_with_transaction | ||
| 173 | ($dbh, sub { | ||
| 174 | foreach my $pair (@pairs) { | ||
| 175 | my $key = $pair->[0]; | ||
| 176 | my $value = $pair->[1]; | ||
| 177 | $dbh->do ('INSERT OR REPLACE INTO preference (key, value) | ||
| 178 | VALUES (?,?)', {}, $key, $value); | ||
| 179 | } | ||
| 180 | }); | ||
| 181 | $self->set_response_sensitive (RESPONSE_SAVE, 0); | ||
| 182 | } | ||
| 183 | |||
| 184 | # return a list of pairs ['key','value'],['key','value'],... | ||
| 185 | sub _get_widget_values { | ||
| 186 | my ($self) = @_; | ||
| 187 | my @ret; | ||
| 188 | foreach my $element (@{$self->{'elements'}}) { | ||
| 189 | my $key = $element->{'key'}; | ||
| 190 | my $value; | ||
| 191 | my $type = $element->{'type'}; | ||
| 192 | if ($type eq 'string') { | ||
| 193 | $value = $element->{'entry'}->get_text; | ||
| 194 | } elsif ($type eq 'boolean') { | ||
| 195 | $value = ($element->{'checkbox'}->get_active ? 1 : 0); | ||
| 196 | } else { | ||
| 197 | die "oops, unknown type ". $element->{'type'}; | ||
| 198 | } | ||
| 199 | push @ret, [ $key, $value ]; | ||
| 200 | } | ||
| 201 | return @ret; | ||
| 202 | } | ||
| 203 | |||
| 204 | |||
| 205 | 1; | ||
| 206 | __END__ |