App-Chart

 view release on metacpan or  search on metacpan

lib/App/Chart/Gtk2/Symlist.pm  view on Meta::CPAN

                  Glib::ParamSpec->string
                  ('name',
                   'name',
                   'The symlist name.',
                   '', # default
                   Glib::G_PARAM_READWRITE) ];

our %instances;  # key => symlist

my %key_to_class = (all        => 'App::Chart::Gtk2::Symlist::All',
                    favourites => 'App::Chart::Gtk2::Symlist::Favourites',
                    historical => 'App::Chart::Gtk2::Symlist::Historical',
                    alerts     => 'App::Chart::Gtk2::Symlist::Alerts');

use constant { COL_SYMBOL => 0,
               COL_NOTE   => 1 };

App::Chart::chart_dirbroadcast()->connect
  ('symlist-list-changed', \&_do_symlist_list_changed);

sub INIT_INSTANCE {
  my ($self) = @_;
  $self->{'key'} = '';
  require App::Chart::DBI;
  my $dbh = App::Chart::DBI->instance;
  $self->set_property (where   => { key => 'dummy' },
                       columns => [ 'symbol', 'note' ],
                       table   => 'symlist_content',
                       dbh     => $dbh);

  # class closure no good as of Perl-Gtk2 1.221, must connect to self
  $self->signal_connect (rows_reordered => \&_do_rows_reordered);

  App::Chart::chart_dirbroadcast()->connect_for_object
      ('symlist-content-inserted', \&_do_content_inserted, $self);
  App::Chart::chart_dirbroadcast()->connect_for_object
      ('symlist-content-deleted', \&_do_content_deleted, $self);
  App::Chart::chart_dirbroadcast()->connect_for_object
      ('symlist-content-reordered', \&_do_content_reordered, $self);
}

sub GET_PROPERTY {
  my ($self, $pspec) = @_;

  my $pname = $pspec->get_name;
  if ($pname eq 'name') {
    return $self->name;
  } else {
    return $self->{$pname};
  }
}

sub SET_PROPERTY {
  my ($self, $pspec, $newval) = @_;
  my $pname = $pspec->get_name;
  $self->{$pname} = $newval;

  if ($pname eq 'key') {
    my $key = $newval;
    $instances{$key} = $self;
    # Scalar::Util::weaken ($instances{$key});
    $self->set_property (where => { key => $key });
    delete $self->{'name'};
  }
}

sub FINALIZE_INSTANCE {
  my ($self) = @_;
  while (my ($key, $value) = each %instances) {
    if (! defined $value || $value == $self) {
      delete $instances{$key};
    }
  }
}

sub new_from_key {
  my ($class, $key) = @_;
  if (! defined $key) { return undef; }
  my $self = $class->new_from_key_maybe ($key);
  if (! $self) {
    carp 'No such symlist key ',$key;
  }
  return $self;
}
sub new_from_key_maybe {
  my ($class, $key) = @_;
  if (! defined $key) { return undef; }
  if (my $self = $instances{$key}) { return $self; }
  if (! defined App::Chart::Database::read_notes_single
      ('SELECT seq FROM symlist WHERE key=?', $key)) {
    return undef;
  }
  return _new_from_known_key ($key);
}

# sub new_from_pos {
#   my ($class, $seq) = @_;
#   my $key = App::Chart::Database::read_notes_single
#     ('SELECT key FROM symlist WHERE seq=?', $seq);
#   if (! $key) { croak "No symlist at position $seq"; }
#   return _new_from_known_key ($key);
# }

sub _new_from_known_key {
  my ($key) = @_;
  if (my $self = $instances{$key}) { return $self; }

  my $class = $key_to_class{$key} || 'App::Chart::Gtk2::Symlist::User';
  require Module::Load;
  Module::Load::load ($class);
  my $self = $class->new (key => $key);
  $instances{$key} = $self;
  #
  # can cause excess re-reading
  # Scalar::Util::weaken ($instances{$key});
  #
  return $self;
}

sub all_lists {
  my ($class) = @_;
  require App::Chart::DBI;
  my $dbh = App::Chart::DBI->instance;
  my $sth = $dbh->prepare_cached('SELECT key FROM symlist ORDER BY seq ASC');
  my $dbkeys = $dbh->selectcol_arrayref ($sth);
  $sth->finish();

  my @ret = map { _new_from_known_key($_) } @$dbkeys;
  my %got;
  @got{@$dbkeys} = 1;
  foreach my $key (keys %instances) {
    if (! exists $got{$key}) { unshift @ret, $instances{$key}; }
  }
  return @ret;
}

sub key {
  my ($self) = @_;
  return $self->{'key'} || croak;
}
sub can_edit           { return 0; }
sub can_delete_symlist { return 0; }

sub name {
  my ($self) = @_;
  if (! exists $self->{'name'}) {
    $self->{'name'}
      = App::Chart::Database::read_notes_single
        ('SELECT name FROM symlist WHERE key=?', $self->{'key'})
          || __('(No name)');
  }
  return $self->{'name'};
}

# default current symbols
sub interested_symbols {
  my ($self) = @_;
  return $self->symbols;
}

#------------------------------------------------------------------------------
# remote changes

# 'symlist-content-inserted' broadcast handler
sub _do_content_inserted {
  my ($self, $key, $seq) = @_;
  if ($self->key eq $key) {
    $self->reread;
  }
}
# 'symlist-content-deleted' broadcast handler
sub _do_content_deleted {
  my ($self, $key, $seq) = @_;
  if ($self->key eq $key) {
    $self->reread;



( run in 1.288 second using v1.01-cache-2.11-cpan-39bf76dae61 )