| 1 | # Copyright 2008, 2009, 2010, 2011, 2014 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 | |||
| 18 | package App::Chart::Gtk2::SymlistListModel; | ||
| 19 | use 5.010; | ||
| 20 | use strict; | ||
| 21 | use warnings; | ||
| 22 | use Glib; | ||
| 23 | use Gtk2; | ||
| 24 | use Carp; | ||
| 25 | |||
| 26 | use App::Chart::Database; | ||
| 27 | use App::Chart::Gtk2::Symlist; | ||
| 28 | |||
| 29 | # uncomment this to run the ### lines | ||
| 30 | #use Smart::Comments; | ||
| 31 | |||
| 32 | use constant DEBUG => 0; | ||
| 33 | |||
| 34 | use App::Chart::Gtk2::Ex::ListStoreDBISeq; | ||
| 35 | use Glib::Object::Subclass | ||
| 36 | 'App::Chart::Gtk2::Ex::ListStoreDBISeq', | ||
| 37 | signals => { row_changed => \&_do_row_changed, | ||
| 38 | row_inserted => \&_do_row_inserted, | ||
| 39 | row_deleted => \&_do_row_deleted }; | ||
| 40 | |||
| 41 | use constant { COL_KEY => 0, | ||
| 42 | COL_NAME => 1, | ||
| 43 | COL_CONDITION => 2, | ||
| 44 | NUM_COLUMNS => 3 | ||
| 45 | }; | ||
| 46 | |||
| 47 | use Exporter; | ||
| 48 | our @ISA; | ||
| 49 | unshift @ISA, 'Exporter'; | ||
| 50 | our @EXPORT_OK = qw(COL_KEY COL_NAME COL_CONDITION); | ||
| 51 | |||
| 52 | use base 'Class::WeakSingleton'; | ||
| 53 | *_new_instance = \&Glib::Object::new; | ||
| 54 | |||
| 55 | sub INIT_INSTANCE { | ||
| 56 | my ($self) = @_; | ||
| 57 | |||
| 58 | require App::Chart::DBI; | ||
| 59 | my $dbh = App::Chart::DBI->instance; | ||
| 60 | $self->set_property (columns => [ 'key', 'name', 'condition' ], | ||
| 61 | table => 'symlist', | ||
| 62 | dbh => $dbh); | ||
| 63 | |||
| 64 | $self->signal_connect (rows_reordered => \&_do_rows_reordered); | ||
| 65 | |||
| 66 | App::Chart::chart_dirbroadcast()->connect_for_object | ||
| 67 | ('symlist-list-inserted', \&_do_symlist_inserted, $self); | ||
| 68 | App::Chart::chart_dirbroadcast()->connect_for_object | ||
| 69 | ('symlist-list-deleted', \&_do_symlist_deleted, $self); | ||
| 70 | } | ||
| 71 | |||
| 72 | #------------------------------------------------------------------------------ | ||
| 73 | # remote changes | ||
| 74 | |||
| 75 | # 'symlist-list-inserted' broadcast handler | ||
| 76 | sub _do_symlist_inserted { | ||
| 77 | my ($self, $seq, $key) = @_; | ||
| 78 | if ($self->{'reading_database'}) { | ||
| 79 | # this is a broadcast resulting from an insert on the ListStore, it's | ||
| 80 | # only for everyone else (locally and remotely), not a ListStore/DB sync | ||
| 81 | ### Symlist-List symlist-list-inserted handler while reading_database ... | ||
| 82 | return; | ||
| 83 | } | ||
| 84 | # someone external has changed the database | ||
| 85 | # insert a row to make the liststore hopefully like the database except | ||
| 86 | # for this wone new row, and check with reread | ||
| 87 | { local $self->{'reading_database'} = 1; | ||
| 88 | $self->insert_with_values ($seq, COL_KEY, $key); | ||
| 89 | } | ||
| 90 | $self->reread; | ||
| 91 | } | ||
| 92 | |||
| 93 | # 'symlist-list-deleted' broadcast handler | ||
| 94 | sub _do_symlist_deleted { | ||
| 95 | my ($self, $seq) = @_; | ||
| 96 | if ($self->{'reading_database'}) { | ||
| 97 | # this is a broadcast resulting from a delete on the ListStore, so it's | ||
| 98 | # only for everyone else (locally and remotely), not a ListStore/DB sync | ||
| 99 | ### Symlist-List symlist-list-deleted handler while reading_database ... | ||
| 100 | return; | ||
| 101 | } | ||
| 102 | |||
| 103 | # someone external has changed the database | ||
| 104 | # try to make the liststore look like the database, then reread to be sure | ||
| 105 | { local $self->{'reading_database'} = 1; | ||
| 106 | if (my $iter = $self->iter_nth_child (undef, $seq)) { | ||
| 107 | $self->remove ($iter); | ||
| 108 | } | ||
| 109 | } | ||
| 110 | $self->reread; | ||
| 111 | } | ||
| 112 | |||
| 113 | #------------------------------------------------------------------------------ | ||
| 114 | # local changes applied to database | ||
| 115 | |||
| 116 | sub remove { | ||
| 117 | my ($self, $iter) = @_; | ||
| 118 | my $key = $self->get($iter,COL_KEY); | ||
| 119 | delete $App::Chart::Gtk2::Symlist::instances{$key}; | ||
| 120 | return $self->SUPER::remove ($iter); | ||
| 121 | } | ||
| 122 | |||
| 123 | # 'row-changed' class closure | ||
| 124 | sub _do_row_changed { | ||
| 125 | my ($self, $path, $iter) = @_; | ||
| 126 | $self->signal_chain_from_overridden ($path, $iter); | ||
| 127 | |||
| 128 | if ($self->{'reading_database'}) { return; } | ||
| 129 | |||
| 130 | my ($seq) = $path->get_indices; | ||
| 131 | my $key = $self->get_value($iter,COL_KEY); | ||
| 132 | if (DEBUG) { | ||
| 133 | my $name = $self->get_value($iter,COL_NAME); | ||
| 134 | my $condition = $self->get_value($iter,COL_CONDITION); | ||
| 135 | print "Symlist List database change seq=$seq", | ||
| 136 | " to key=",defined $key ? "'$key'" : 'undef', | ||
| 137 | " name=",defined $name ? "'$name'" : 'undef', | ||
| 138 | " cond=",defined $condition ? "'$condition'" : 'undef',"\n"; | ||
| 139 | } | ||
| 140 | local $self->{'reading_database'} = 1; | ||
| 141 | App::Chart::chart_dirbroadcast()->send ('symlist-list-changed', $seq,$key); | ||
| 142 | } | ||
| 143 | |||
| 144 | # 'row-deleted' class closure | ||
| 145 | sub _do_row_deleted { | ||
| 146 | my ($self, $path) = @_; | ||
| 147 | $self->signal_chain_from_overridden ($path); | ||
| 148 | if ($self->{'reading_database'}) { return; } | ||
| 149 | |||
| 150 | my ($seq) = $path->get_indices; | ||
| 151 | ### Symlist-List database delete seq: $seq | ||
| 152 | # database_delete ($self, $seq); | ||
| 153 | local $self->{'reading_database'} = 1; | ||
| 154 | App::Chart::chart_dirbroadcast()->send ('symlist-list-deleted', $seq); | ||
| 155 | } | ||
| 156 | |||
| 157 | # 'row-inserted' class closure | ||
| 158 | sub _do_row_inserted { | ||
| 159 | my ($self, $path, $iter) = @_; | ||
| 160 | $self->signal_chain_from_overridden ($path, $iter); | ||
| 161 | if ($self->{'reading_database'}) { | ||
| 162 | ### Symlist-List row-inserted while reading_database | ||
| 163 | return; | ||
| 164 | } | ||
| 165 | |||
| 166 | my ($seq) = $path->get_indices; | ||
| 167 | my $key = $self->get_value($iter,0) // ''; | ||
| 168 | if (DEBUG) { | ||
| 169 | my $name = $self->get_value($iter,1) // 'undef'; | ||
| 170 | print "Symlist List database insert at seq=$seq", | ||
| 171 | " key=$key name=$name\n"; | ||
| 172 | } | ||
| 173 | local $self->{'reading_database'} = 1; | ||
| 174 | App::Chart::chart_dirbroadcast()->send ('symlist-list-inserted',$seq,$key); | ||
| 175 | } | ||
| 176 | |||
| 177 | # 'rows-reordered' connected on self | ||
| 178 | sub _do_rows_reordered { | ||
| 179 | my ($self, $path, $iter, $aref) = @_; | ||
| 180 | if ($self->{'reading_database'}) { return; } | ||
| 181 | |||
| 182 | ### Symlist-List database reorder: "@$aref" | ||
| 183 | local $self->{'reading_database'} = 1; | ||
| 184 | App::Chart::chart_dirbroadcast()->send ('symlist-list-reordered'); | ||
| 185 | } | ||
| 186 | |||
| 187 | |||
| 188 | #------------------------------------------------------------------------------ | ||
| 189 | # contents | ||
| 190 | |||
| 191 | sub length { | ||
| 192 | my ($self) = @_; | ||
| 193 | return $self->iter_n_children(undef); | ||
| 194 | } | ||
| 195 | |||
| 196 | |||
| 197 | #------------------------------------------------------------------------------ | ||
| 198 | |||
| 199 | sub key_to_pos { | ||
| 200 | my ($self, $key) = @_; | ||
| 201 | my $iter = $self->key_to_iter ($key); | ||
| 202 | my ($ret) = $self->get_path($iter)->get_indices; | ||
| 203 | return $ret; | ||
| 204 | |||
| 205 | # return App::Chart::Database::read_notes_single | ||
| 206 | # ('SELECT seq FROM symlist WHERE key=?', $key); | ||
| 207 | } | ||
| 208 | |||
| 209 | sub key_to_iter { | ||
| 210 | my ($self, $key) = @_; | ||
| 211 | my $ret; | ||
| 212 | $self->foreach | ||
| 213 | (sub { | ||
| 214 | my ($self, $path, $iter) = @_; | ||
| 215 | my $this_key = $self->get_value($iter, COL_KEY); | ||
| 216 | if (defined $this_key && $this_key eq $key) { | ||
| 217 | $ret = $iter->copy; | ||
| 218 | return 1; # stop iterating | ||
| 219 | } | ||
| 220 | return 0; # continue; | ||
| 221 | }); | ||
| 222 | return $ret; | ||
| 223 | } | ||
| 224 | |||
| 225 | 1; | ||
| 226 | __END__ |