package Filer::ListStore;

use strict;
use warnings;

use Filer::Constants;
use Filer::FileInfo;

use Glib::Object::Subclass
	Glib::Object::,
	interfaces => [ Gtk2::TreeModel:: ],
;

sub INIT_INSTANCE {
	my $self = shift;
	$self->{stamp} = sprintf '%d', rand (1<<31);
}

sub FINALIZE_INSTANCE {
	my $self = shift;
}

sub GET_FLAGS {
	return [qw/list-only iters-persist/]
}

sub GET_N_COLUMNS {
	return 7;
}

sub GET_COLUMN_TYPE {
	print @_, "\n";
}

sub GET_ITER {
	my ($self, $path) = @_; #warn "GET_ITER\n";
	die "no path" unless $path;

	my $n = $path->get_indices;
	return undef if $n >= @{$self->{array}} || $n < 0;

	return [ $self->{stamp}, $n, $Filer::FilePane::Files[ $self->{array}[$n] ] , undef ];
}

sub GET_PATH {
	my ($self, $iter) = @_; #warn "GET_PATH\n";
	die "no iter" unless $iter;

	my $path = Gtk2::TreePath->new;
	$path->append_index ($iter->[1]);
	return $path;
}

sub GET_VALUE {
	my ($self,$iter,$column) = @_; #warn "GET_VALUE\n";
	
}

sub ITER_NEXT {
	my ($self,$iter) = @_;
	my $n = $iter->[1];

	return undef unless ++$n < @{$self->{array}};
	return [ $self->{stamp}, $n, $Filer::FilePane::Files[ $self->{array}[$n] ], undef ];
}

sub ITER_CHILDREN
{	my ($self, $parent) = @_; #warn "GET_CHILDREN\n";
	# this is a list, nodes have no children
	return undef if $parent;

	# parent == NULL is a special case; we need to return the first top-level row
 	# No rows => no first row
	return undef unless @{ $self->{array} };

	# Set iter to first item in list
	return [ $self->{stamp}, 0, $::Songs[ $self->{array}[0] ],undef ];
}

sub ITER_HAS_CHILD {
	return $FALSE;
}

sub ITER_N_CHILDREN {
	my ($self, $iter) = @_; #warn "ITER_N_CHILDREN\n";

	return ($iter? 0 : scalar @{$self->{array}});
}

sub ITER_NTH_CHILD {
	my ($self,$parent,$n) = @_; #warn "ITER_NTH_CHILD\n";

	return undef if $parent;

	return undef if $n >= @{$self->{array}};

	return [ $self->{stamp}, $n,  $Filer::FilePane::Files[ $self->{array}[$n] ] , undef ];
}

sub ITER_PARENT {
	return $FALSE;
}

1;
