App/Chart/Gtk2/Symlist.pm untested

filter: Statementstmt 0.0% 0 / 366 Branchbran 0.0% 0 / 128 Conditioncond 0.0% 0 / 47 MC/DCmcdc 0.0% 0 / 28 Subroutinesub 0.0% 0 / 60 Podpod 0.0% 0 / 41 totaltotal 0.0% 0 / 670 CC 65 SCAR 37.6
CC
65
Cov
0%
next_plain
210.0
previous
132.0
ROW_DRAGGABLE
30.0
CRAP 42.7
1 # Copyright 2007, 2008, 2009, 2010, 2011, 2015 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::Symlist;
18 use 5.008;
19 use strict;
20 use warnings;
21 use Carp 'carp','croak';
22 use Gtk2;
23 use Scalar::Util;
24 use Locale::TextDomain ('App-Chart');
25  
26 use Gtk2::Ex::TreeModelBits;
27 use App::Chart;
28  
29 # uncomment this to run the ### lines
30 #use Smart::Comments;
31  
32 use constant DEBUG => 0;
33  
34 use base 'App::Chart::Gtk2::Ex::ListStore::DragByCopy';
35  
36 use App::Chart::Gtk2::Ex::ListStoreDBISeq;
37 use Glib::Object::Subclass
38 'App::Chart::Gtk2::Ex::ListStoreDBISeq',
39 interfaces => [ 'Gtk2::TreeDragSource',
40 'Gtk2::TreeDragDest' ],
41 signals => { row_changed => \&_do_row_changed,
42 row_inserted => \&_do_row_inserted,
43 row_deleted => \&_do_row_deleted },
44  
45 properties => [ Glib::ParamSpec->string
46 ('key',
47 'key',
48 'The symlist database key.',
49 '',
50 Glib::G_PARAM_READWRITE),
51  
52 Glib::ParamSpec->string
53 ('name',
54 'name',
55 'The symlist name.',
56 '', # default
57 Glib::G_PARAM_READWRITE) ];
58  
59 our %instances; # key => symlist
60  
61 my %key_to_class = (all => 'App::Chart::Gtk2::Symlist::All',
62 favourites => 'App::Chart::Gtk2::Symlist::Favourites',
63 historical => 'App::Chart::Gtk2::Symlist::Historical',
64 alerts => 'App::Chart::Gtk2::Symlist::Alerts');
65  
66 use constant { COL_SYMBOL => 0,
67 COL_NOTE => 1 };
68  
69 App::Chart::chart_dirbroadcast()->connect
70 ('symlist-list-changed', \&_do_symlist_list_changed);
71  
72 sub INIT_INSTANCE {
73 my ($self) = @_;
74 $self->{'key'} = '';
75 require App::Chart::DBI;
76 my $dbh = App::Chart::DBI->instance;
77 $self->set_property (where => { key => 'dummy' },
78 columns => [ 'symbol', 'note' ],
79 table => 'symlist_content',
80 dbh => $dbh);
81  
82 # class closure no good as of Perl-Gtk2 1.221, must connect to self
83 $self->signal_connect (rows_reordered => \&_do_rows_reordered);
84  
85 App::Chart::chart_dirbroadcast()->connect_for_object
86 ('symlist-content-inserted', \&_do_content_inserted, $self);
87 App::Chart::chart_dirbroadcast()->connect_for_object
88 ('symlist-content-deleted', \&_do_content_deleted, $self);
89 App::Chart::chart_dirbroadcast()->connect_for_object
90 ('symlist-content-reordered', \&_do_content_reordered, $self);
91 }
92  
93 sub GET_PROPERTY {
94 my ($self, $pspec) = @_;
95  
96 my $pname = $pspec->get_name;
97 if ($pname eq 'name') {
98 return $self->name;
99 } else {
100 return $self->{$pname};
101 }
102 }
103  
104 sub SET_PROPERTY {
105 my ($self, $pspec, $newval) = @_;
106 my $pname = $pspec->get_name;
107 $self->{$pname} = $newval;
108  
109 if ($pname eq 'key') {
110 my $key = $newval;
111 $instances{$key} = $self;
112 # Scalar::Util::weaken ($instances{$key});
113 $self->set_property (where => { key => $key });
114 delete $self->{'name'};
115 }
116 }
117  
118 sub FINALIZE_INSTANCE {
119 my ($self) = @_;
120 while (my ($key, $value) = each %instances) {
121 if (! defined $value || $value == $self) {
122 delete $instances{$key};
123 }
124 }
125 }
126  
127 sub new_from_key {
128 my ($class, $key) = @_;
129 if (! defined $key) { return undef; }
130 my $self = $class->new_from_key_maybe ($key);
131 if (! $self) {
132 carp 'No such symlist key ',$key;
133 }
134 return $self;
135 }
136 sub new_from_key_maybe {
137 my ($class, $key) = @_;
138 if (! defined $key) { return undef; }
139 if (my $self = $instances{$key}) { return $self; }
140 if (! defined App::Chart::Database::read_notes_single
141 ('SELECT seq FROM symlist WHERE key=?', $key)) {
142 return undef;
143 }
144 return _new_from_known_key ($key);
145 }
146  
147 # sub new_from_pos {
148 # my ($class, $seq) = @_;
149 # my $key = App::Chart::Database::read_notes_single
150 # ('SELECT key FROM symlist WHERE seq=?', $seq);
151 # if (! $key) { croak "No symlist at position $seq"; }
152 # return _new_from_known_key ($key);
153 # }
154  
155 sub _new_from_known_key {
156 my ($key) = @_;
157 if (my $self = $instances{$key}) { return $self; }
158  
159 my $class = $key_to_class{$key} || 'App::Chart::Gtk2::Symlist::User';
160 require Module::Load;
161 Module::Load::load ($class);
162 my $self = $class->new (key => $key);
163 $instances{$key} = $self;
164 #
165 # can cause excess re-reading
166 # Scalar::Util::weaken ($instances{$key});
167 #
168 return $self;
169 }
170  
171 sub all_lists {
172 my ($class) = @_;
173 require App::Chart::DBI;
174 my $dbh = App::Chart::DBI->instance;
175 my $sth = $dbh->prepare_cached('SELECT key FROM symlist ORDER BY seq ASC');
176 my $dbkeys = $dbh->selectcol_arrayref ($sth);
177 $sth->finish();
178  
179 my @ret = map { _new_from_known_key($_) } @$dbkeys;
180 my %got;
181 @got{@$dbkeys} = 1;
182 foreach my $key (keys %instances) {
183 if (! exists $got{$key}) { unshift @ret, $instances{$key}; }
184 }
185 return @ret;
186 }
187  
188 sub key {
189 my ($self) = @_;
190 return $self->{'key'} || croak;
191 }
192 sub can_edit { return 0; }
193 sub can_delete_symlist { return 0; }
194  
195 sub name {
196 my ($self) = @_;
197 if (! exists $self->{'name'}) {
198 $self->{'name'}
199 = App::Chart::Database::read_notes_single
200 ('SELECT name FROM symlist WHERE key=?', $self->{'key'})
201 || __('(No name)');
202 }
203 return $self->{'name'};
204 }
205  
206 # default current symbols
207 sub interested_symbols {
208 my ($self) = @_;
209 return $self->symbols;
210 }
211  
212 #------------------------------------------------------------------------------
213 # remote changes
214  
215 # 'symlist-content-inserted' broadcast handler
216 sub _do_content_inserted {
217 my ($self, $key, $seq) = @_;
218 if ($self->key eq $key) {
219 $self->reread;
220 }
221 }
222 # 'symlist-content-deleted' broadcast handler
223 sub _do_content_deleted {
224 my ($self, $key, $seq) = @_;
225 if ($self->key eq $key) {
226 $self->reread;
227 }
228 }
229  
230 # 'symlist-content-reordered' broadcast handler
231 sub _do_content_reordered {
232 my ($self, $key) = @_;
233 if ($self->key eq $key) {
234 $self->reread;
235 }
236 }
237  
238 # 'symlist-list-changed' broadcast handler
239 sub _do_symlist_list_changed {
240 my ($seq, $key) = @_;
241 if (DEBUG) { print "Symlist list changed '$key'\n"; }
242 defined $key or return;
243 my $symlist = $instances{$key} || return;
244 if (DEBUG) { print " notify $symlist 'name'\n"; }
245 delete $symlist->{'name'}; # refetch
246 $symlist->notify('name');
247 }
248  
249 #------------------------------------------------------------------------------
250 # local changes applied to database
251 #
252 # signal_chain_from_overridden() is done to update the database before
253 # dirbroadcast()->send is sent, because the local broadcast handlers are
254 # likely to use ->hash, ->symbol_listref, etc, which will do a ->reread of
255 # the database contents.
256 #
257  
258 # 'row-changed' class closure
259 sub _do_row_changed {
260 my ($self, $path, $iter) = @_;
261 delete $self->{'symbol_hash'};
262 delete $self->{'symbol_list'};
263  
264 $self->signal_chain_from_overridden ($path, $iter);
265  
266 if (! $self->{'reading_database'}) {
267 my $key = $self->{'key'};
268 my ($seq) = $path->get_indices;
269 my $symbol = $self->get_value($iter,0);
270 local $self->{'reading_database'} = 1;
271 App::Chart::chart_dirbroadcast()->send ('symlist-content-changed',$key,$seq,$symbol);
272 }
273 }
274  
275 # 'row-deleted' class closure
276 sub _do_row_deleted {
277 my ($self, $path) = @_;
278 delete $self->{'symbol_hash'};
279 delete $self->{'symbol_list'};
280  
281 $self->signal_chain_from_overridden ($path);
282  
283 if (! $self->{'reading_database'}) {
284 my $key = $self->{'key'};
285 my ($seq) = $path->get_indices;
286 ### Symlist database: "$key delete $seq"
287 local $self->{'reading_database'} = 1;
288 App::Chart::chart_dirbroadcast()->send ('symlist-content-deleted',
289 $key, $seq);
290 }
291 }
292  
293 # 'row-inserted' class closure
294 sub _do_row_inserted {
295 my ($self, $path, $iter) = @_;
296 my $symbol = $self->get_value ($iter, COL_SYMBOL);
297  
298 delete $self->{'symbol_list'};
299 if (my $hash = $self->{'symbol_hash'}) {
300 if (defined $symbol) {
301 $hash->{$symbol} = 1;
302 }
303 }
304  
305 $self->signal_chain_from_overridden ($path, $iter);
306  
307 if (! $self->{'reading_database'}) {
308 my $key = $self->{'key'};
309 my ($seq) = $path->get_indices;
310 ### Symlist database: "$key insert $symbol at $seq"
311 App::Chart::chart_dirbroadcast()->send ('symlist-content-inserted',
312 $key, $seq, $symbol);
313 }
314 }
315  
316 # 'rows-reordered' connected on self
317 sub _do_rows_reordered {
318 my ($self, $path, $iter, $aref) = @_;
319 delete $self->{'symbol_hash'};
320 delete $self->{'symbol_list'};
321 if (! $self->{'reading_database'}) {
322 my $key = $self->{'key'};
323 if (DEBUG) { print "Symlist database '$key' reorder ",
324 join(' ',@$aref),"\n"; }
325 App::Chart::chart_dirbroadcast()->send ('symlist-content-reordered', $key);
326 }
327 }
328  
329  
330 #------------------------------------------------------------------------------
331 # contents
332  
333 sub hash {
334 my ($self) = @_;
335 return ($self->{'symbol_hash'} ||= do {
336 my %hash;
337 my $s = $self->symbol_listref;
338 @hash{@$s} = (0 .. $#$s);
339 \%hash;
340 });
341 }
342 sub contains_symbol {
343 my ($self, $symbol) = @_;
344 my $hash = $self->hash;
345 return exists $hash->{$symbol};
346 }
347  
348 sub symbols {
349 my ($self) = @_;
350 ### Symlist symbols: "$self"
351 return @{$self->symbol_listref};
352 }
353 sub symbol_listref {
354 my ($self) = @_;
355 return ($self->{'symbol_list'} ||= do {
356 require Gtk2::Ex::TreeModelBits;
357 [ Gtk2::Ex::TreeModelBits::column_contents ($self,COL_SYMBOL) ]
358 });
359 }
360  
361 sub is_empty {
362 my ($self) = @_;
363 return $self->length == 0;
364 }
365 sub length {
366 my ($self) = @_;
367 return $self->iter_n_children(undef);
368 }
369  
370 #------------------------------------------------------------------------------
371 # helpers
372  
373 sub append_or_elevate {
374 my ($self, $symbol, $note) = @_;
375 if (my $iter = $self->find_symbol_iter ($symbol)) {
376 $self->move_before ($iter, $self->get_iter_first);
377 return 'elevated';
378 } else {
379 $self->append_symbol ($symbol, $note);
380 return 'appended';
381 }
382 }
383 sub append_symbol {
384 my ($self, $symbol, $note) = @_;
385 $self->insert_symbol_at_pos ($symbol, $self->length, $note);
386 }
387 sub insert_symbol_at_pos {
388 my ($self, $symbol, $seq, $note) = @_;
389 if (DEBUG) { print "Symlist insert symbol=$symbol seq=$seq\n"; }
390 # if (! $self->can_edit) {
391 # croak 'Cannot edit symlist "'.$self->{'key'}.'"';
392 # }
393 $self->insert_with_values ($seq, 0=>$symbol, 1=>$note);
394 }
395  
396 sub delete_symbol {
397 my ($self, $symbol) = @_;
398 if (DEBUG) { print "Symlist ",$self->{'key'},
399 " delete_symbol $symbol\n"; }
400 # if (! $self->can_edit) {
401 # croak 'Cannot edit symlist "'.$self->{'key'}.'"';
402 # }
403  
404 # loop in case multiple copies in the list
405 while (my $iter = $self->find_symbol_iter ($symbol)) {
406 $self->remove ($iter);
407 }
408 }
409  
410 # return integer pos or undef
411 sub find_symbol_pos {
412 my ($self, $target_symbol) = @_;
413 my $path = $self->find_symbol_path ($target_symbol);
414 if (! $path) { return undef; }
415 my ($seq) = $path->get_indices;
416 return $seq;
417 }
418  
419 # return Gtk2::TreePath or undef
420 sub find_symbol_path {
421 my ($self, $target_symbol) = @_;
422 my $iter = $self->find_symbol_iter ($target_symbol);
423 return $iter && $self->get_path($iter);
424 }
425  
426 # return Gtk2::TreeIter or undef
427 sub find_symbol_iter {
428 my ($self, $target_symbol) = @_;
429 my $hash = $self->hash;
430 if (! exists $hash->{$target_symbol}) {
431 return undef;
432 }
433  
434 my $ret;
435 $self->foreach (sub {
436 my ($self, $path, $iter) = @_;
437 my $symbol = $self->get_value($iter,0);
438 if ($symbol eq $target_symbol) {
439 $ret = $iter->copy;
440 return 1; # stop iterating
441 } else {
442 return 0; # keep iterating
443 }
444 });
445 return $ret;
446 }
447  
448 #------------------------------------------------------------------------------
449  
450 sub symlist_length {
451 my ($key) = @_;
452 my $last = App::Chart::Database::read_notes_single
453 ('SELECT seq FROM symlist_content WHERE key=? ORDER BY seq DESC LIMIT 1',
454 $key);
455 if (defined $last) {
456 return $last + 1;
457 } else {
458 return 0;
459 }
460 }
461  
462 sub next {
463 my ($symbol, $symlist) = @_;
464 do {
465 ($symbol, $symlist) = next_plain ($symbol, $symlist);
466 if (DEBUG) { print "got $symbol, $symlist\n"; }
467 } while (defined $symbol
468 && ! App::Chart::Database->symbol_exists ($symbol));
469 return ($symbol, $symlist);
470 }
471  
472 sub next_plain {
473 my ($symbol, $symlist) = @_;
474  
475 if (DEBUG) { print "next from ",$symbol||'undef',
476 " ",$symlist||'undef',"\n"; }
477 if ($symbol) {
478 my $seq = App::Chart::Database::read_notes_single
479 ('SELECT seq FROM symlist_content WHERE key=? AND symbol=?',
480 $symlist->key, $symbol);
481 if (DEBUG) { print " is seq ",defined $seq ? $seq : 'undef',"\n"; }
482 if (! defined $seq) {
483 $seq = App::Chart::Database::read_notes_single
484 ('SELECT seq FROM symlist_content WHERE key=? AND symbol>=?
485 ORDER BY symbol ASC LIMIT 1',
486 $symlist->key, $symbol);
487 }
488 if (defined $seq) {
489 $seq++;
490 if (DEBUG) { print " to seq $seq\n"; }
491 $symbol = App::Chart::Database::read_notes_single
492 ('SELECT symbol FROM symlist_content WHERE key=? AND seq=?',
493 $symlist->key, $seq);
494 if (defined $symbol) { return ($symbol, $symlist); }
495 if (DEBUG) { print " not found\n"; }
496 }
497 }
498  
499 if (DEBUG) { print " next symlist from ",$symlist||'undef',"\n"; }
500 my $symlist_num = -1;
501 if ($symlist) {
502 $symlist_num = App::Chart::Database::read_notes_single
503 ('SELECT seq FROM symlist WHERE key=?', $symlist->key);
504 if (! defined $symlist_num) { $symlist_num = -1; }
505 }
506 require App::Chart::DBI;
507 my $dbh = App::Chart::DBI->instance;
508 my $sth = $dbh->prepare_cached
509 ('SELECT symlist_content.symbol, symlist.key
510 FROM symlist,symlist_content
511 WHERE symlist.seq > ?
512 AND symlist.key = symlist_content.key
513 AND symlist_content.seq = 0
514 ORDER BY symlist.seq');
515 ($symbol, $symlist) = $dbh->selectrow_array($sth, undef, $symlist_num);
516 if (defined $symlist) { $symlist = _new_from_known_key ($symlist); }
517 return ($symbol, $symlist);
518 }
519  
520 sub previous {
521 my ($symbol, $symlist) = @_;
522 if (DEBUG) { print "previous from $symbol $symlist\n"; }
523 if (! $symbol || ! $symlist) { return (undef, undef); }
524  
525 AGAIN:
526 {
527 my $seq = App::Chart::Database::read_notes_single
528 ('SELECT seq FROM symlist_content WHERE key=? AND symbol=?',
529 $symlist->key, $symbol);
530 if (! defined $seq) {
531 $seq = App::Chart::Database::read_notes_single
532 ('SELECT seq FROM symlist_content WHERE key=? AND symbol<=?
533 ORDER BY symbol ASC LIMIT 1',
534 $symlist->key, $symbol);
535 }
536  
537 if (defined $seq && $seq > 0) {
538 $seq--;
539 $symbol = App::Chart::Database::read_notes_single
540 ('SELECT symbol FROM symlist_content WHERE key=? AND seq=?',
541 $symlist->key, $seq);
542 if (defined $symbol) { goto FOUND; }
543 }
544 }
545  
546 my $symlist_num = App::Chart::Database::read_notes_single
547 ('SELECT seq FROM symlist WHERE key=?', $symlist->key);
548 if (! defined $symlist_num) { return (undef, undef); }
549  
550 for (;;) {
551 $symlist_num--;
552 if ($symlist_num < 0) { return (undef, undef); }
553  
554 my $key = App::Chart::Database::read_notes_single
555 ('SELECT key FROM symlist WHERE seq=?', $symlist_num);
556 if (! $key) { return (undef, undef); }
557 $symlist = _new_from_known_key ($key);
558  
559 $symbol = App::Chart::Database::read_notes_single
560 ('SELECT symbol FROM symlist_content WHERE key=? ORDER BY seq DESC LIMIT 1',
561 $symlist->key);
562 if ($symbol) { last; }
563 }
564  
565 FOUND:
566 if (! App::Chart::Database->symbol_exists ($symbol)) {
567 goto AGAIN;
568 }
569 return ($symbol, $symlist);
570 }
571  
572  
573 #------------------------------------------------------------------------------
574 # drag source
575 #
576  
577 # gtk_tree_drag_source_row_draggable ($self, $src_path)
578 sub ROW_DRAGGABLE {
579 my ($self, $src_path) = @_;
580 if (DEBUG) { print "Symlist ROW_DRAGGABLE path=",$src_path->to_string,"\n";
581 print " ",$self->can_edit?"yes":"no", ", can_edit\n"; }
582 $self->can_edit or do {
583 if (DEBUG) { print " no, cannot edit\n"; }
584 return undef;
585 };
586 if (DEBUG) { print " super:\n"; }
587 return $self->SUPER::ROW_DRAGGABLE ($src_path);
588 }
589  
590 #------------------------------------------------------------------------------
591 # drag dest
592  
593 # gtk_tree_drag_dest_row_drop_possible
594 #
595 sub ROW_DROP_POSSIBLE {
596 my ($self, $dst_path, $sel) = @_;
597 ### Symlist ROW_DROP_POSSIBLE
598 ### to path: $dst_path->to_string
599 ### type: $sel->type->name
600  
601 $self->can_edit or do {
602 ### no, cannot edit
603 return 0;
604 };
605 if ($dst_path->get_depth != 1) {
606 ### no, dest path depth: $dst_path->get_depth
607 return 0;
608 }
609  
610 if (defined (my $str = $sel->get_text)) {
611 ### yes, can drop text
612 return 1;
613 }
614  
615 if (my ($src_model, $src_path) = $sel->get_row_drag_data) {
616 if ($src_model->isa('App::Chart::Gtk2::Symlist')) {
617 ### yes, source model is a Symlist
618 return 1;
619 }
620 }
621  
622 ### no, not text or symlist row
623 return 0;
624 }
625  
626 # gtk_tree_drag_dest_drag_data_received
627 #
628 sub DRAG_DATA_RECEIVED {
629 my ($self, $dst_path, $sel) = @_;
630 ### Symlist DRAG_DATA_RECEIVED
631 ### to path: $dst_path->to_string
632 ### type: $sel->type->name
633 ### src model: (($sel->get_row_drag_data)[0])
634 ### src path : (($sel->type->name eq 'GTK_TREE_MODEL_ROW') && ($sel->get_row_drag_data)[1]->to_string)
635  
636 ### get_text: $sel->get_text
637 if (defined (my $str = $sel->get_text)) {
638 if ($dst_path->get_depth != 1) {
639 ### no, dest path depth: $dst_path->get_depth
640 return 0;
641 }
642 my ($dst_index) = $dst_path->get_indices;
643 eval { $self->insert_with_values ($dst_index, COL_SYMBOL, $str); 1 }
644 or do {
645 ### no, error from insert_with_values(): $@
646 return 0;
647 };
648 ### yes, dropped text
649 return 1;
650 }
651  
652 ### go to SUPER
653 return $self->SUPER::DRAG_DATA_RECEIVED ($dst_path, $sel);
654 }
655  
656  
657 1;
658 __END__