#!/usr/bin/perl -w
#
# This is the basis of an application with signal handlers
#
# You can safely edit this file, any changes that you make will be preserved
# and this file will not be overwritten by the next run of Glade::PerlGenerate
#
# Skeleton subs of any missing signal handlers can be copied from
# /home/ivan/gtkdbi/src/GtkdbiSIGS.pm
#
#==============================================================================
#=== This is the 'window1' class                              
#==============================================================================
package window1;
require 5.000; use strict 'vars', 'refs', 'subs';
# UI class 'window1' (version 0.01)
# 
# Copyright (c) Date Tue Oct 23 01:49:12 PDT 2001
# Author Ivan Kohler,Chess room,888-670-SISD,215-996-0824 <ivan\@rootwood.haze.st>
#
## Unspecified copying policy, please contact the author\n#  Ivan Kohler,Chess room,888-670-SISD,215-996-0824 <ivan\@rootwood.haze.st>
#
#==============================================================================
# This perl source file was automatically generated by 
# Glade::PerlGenerate version 0.59 - Wed Jun 20 14:48:25 BST 2001
# Copyright (c) Author Dermot Musgrove <dermot.musgrove\@virgin.net>
#
# from Glade file /home/ivan/gtkdbi/gtkdbi.glade
# Tue Oct 23 01:53:38 PDT 2001
#==============================================================================

BEGIN {
    use src::gtkdbiUI;
} # End of sub BEGIN

use vars qw($ick_global_window1 $dbh $schema);

sub app_run {
    my ($class) = @_;
    #$class->load_translations('Gtkdbi');
    $class->load_translations('gtkdbi');
    # You can use the line below to load a test .mo file before it is installed in 
    # the normal place (eg /usr/local/share/locale/fr/LC_MESSAGES/Gtkdbi.mo)
#    $class->load_translations('Gtkdbi', 'test', undef, '/home/ivan/gtkdbi/ppo/Gtkdbi.mo');
    Gtk->init;
    my $window = $class->new;
    $ick_global_window1 = $window;
    $window->TOPLEVEL->show;

    # Put any extra UI initialisation (eg signal_connect) calls here

    # Now let Gtk handle signals
    Gtk->main;

    $window->TOPLEVEL->destroy;

    return $window;

} # End of sub app_run

#===============================================================================
#=== Below are the default signal handlers for 'window1' class
#===============================================================================
sub about_Form {
    my ($class) = @_;
    my $gtkversion = 
        Gtk->major_version.".".
        Gtk->minor_version.".".
        Gtk->micro_version;
    my $name = $0;
    my $message = 
        __PACKAGE__." ("._("version")." $VERSION - $DATE)\n".
        _("Written by")." $AUTHOR \n\n".
        _("No description")." \n\n".
        "Gtk ".     _("version").": $gtkversion\n".
        "Gtk-Perl "._("version").": $Gtk::VERSION\n".
        _("run from file").": $name";
    __PACKAGE__->message_box($message, _("About")." \u".__PACKAGE__, [_('Dismiss'), _('Quit Program')], 1,
        "$Glade::PerlRun::pixmaps_directory/glade2perl_logo.xpm", 'left' );
} # End of sub about_Form

sub destroy_Form {
    my ($class, $data, $object, $instance) = @_;
    Gtk->main_quit; 
} # End of sub destroy_Form

sub toplevel_hide    { shift->get_toplevel->hide    }
sub toplevel_close   { shift->get_toplevel->close   }
sub toplevel_destroy { shift->get_toplevel->destroy }

#==============================================================================
#=== Below are the signal handlers for 'window1' class 
#==============================================================================
#sub on_file1_activate {
#    my ($class, $data, $object, $instance, $event) = @_;
#    my $me = __PACKAGE__."->on_file1_activate";
#    # Get ref to hash of all widgets on our form
#    my $form = $__PACKAGE__::all_forms->{$instance};
#
#    # REPLACE the line below with the actions to be taken when __PACKAGE__."->on_file1_activate." is called
#    __PACKAGE__->show_skeleton_message($me, \@_, __PACKAGE__, "$Glade::PerlRun::pixmaps_directory/glade2perl_logo.xpm");
#
#} # End of sub on_file1_activate
#
#sub on_help1_activate {
#    my ($class, $data, $object, $instance, $event) = @_;
#    my $me = __PACKAGE__."->on_help1_activate";
#    # Get ref to hash of all widgets on our form
#    my $form = $__PACKAGE__::all_forms->{$instance};
#
#    # REPLACE the line below with the actions to be taken when __PACKAGE__."->on_help1_activate." is called
#    __PACKAGE__->show_skeleton_message($me, \@_, __PACKAGE__, "$Glade::PerlRun::pixmaps_directory/glade2perl_logo.xpm");
#
#} # End of sub on_help1_activate


use vars qw($quit_dialog);
#$quit_dialog = quit_dialog->new;

sub on_quit1_activate {
    my ($class, $data, $object, $instance, $event) = @_;
#    my $me = __PACKAGE__."->on_quit1_activate";
    # Get ref to hash of all widgets on our form
#    my $form = $__PACKAGE__::all_forms->{$instance};

    $quit_dialog ||= quit_dialog->new;

    #should disable window1 too

    $quit_dialog->TOPLEVEL->show;

}

{
  package quit_dialog;

  sub on_quit_no_clicked {
    my ($class, $data, $object, $instance, $event) = @_;
    $class->get_toplevel->hide;
  }

  sub on_quit_yes_clicked {
    my ($class, $data, $object, $instance, $event) = @_;
    #exit;
    Gtk->main_quit;
  }
}

use vars qw($about_dialog);

sub on_about1_activate {
    my ($class, $data, $object, $instance, $event) = @_;

    $about_dialog ||= about_dialog->new;

    #should disable window1 too

    $about_dialog->TOPLEVEL->show;

}

{
  package about_dialog;

  sub on_about_button_clicked {
    my ($class, $data, $object, $instance, $event) = @_;
    $class->get_toplevel->hide;
  }

}

use vars qw($connect_dialog);

sub on_connect1_activate {
    my ($class, $data, $object, $instance, $event) = @_;

    $connect_dialog ||= connect_dialog->new;

    $connect_dialog->read_sessions;

    #should disable window1 too

    $connect_dialog->TOPLEVEL->show;

}

{
  package connect_dialog;

  use vars qw( $datasource $username $password ); #shouldn't be globals...
#  $datasource = '';
#  $username = '';
#  $password = '';

  use vars qw( %session_username %session_password ); #ick more

  use subs qw( save_session );

  sub on_abortconnect_button_clicked {
    my ($class, $data, $object, $instance, $event) = @_;
    $class->get_toplevel->hide;
  }

  sub on_connect_button_clicked {
    my ($class, $data, $object, $instance, $event) = @_;

    # dbh shouldn't be global.  alas.  window1 should pass a ref to connect dialog or something.
    use DBI;
    use DBIx::DBSchema 0.19;
    $@ = '';
    if ( $window1::dbh =
           eval { DBI->connect($datasource, $username, $password,
                               { RaiseError => 0, PrintError => 0 },
                              )
                }
    ) {

      save_session($datasource, $username, $password)
        unless exists $session_username{$datasource};

      $class->get_toplevel->hide;

      #shouldn't be global
      $window1::ick_global_window1->TOPLEVEL->set_title("gtkdbi - $datasource");
      $window1::ick_global_window1->FORM->{'entry1'}->set_text("Connected to $datasource");

      #$window1::schema = new_odbc DBIx::DBSchema $window1::dbh;
      #DBI::type_info Pg driver returned no results for type 23 at /usr/local/share/perl/5.6.1/DBIx/DBSchema/Table.pm line 172.
      $window1::schema = new_native DBIx::DBSchema $window1::dbh;

#      warn $window1::schema->pretty_print;

      foreach my $table ( $window1::schema->tables ) {
        #make a 
        warn $table;

        #maybe this should go in a sub around resultview
        my $sth = $window1::dbh->prepare("SELECT * FROM $table") or do {
          warn "should pop up an error message ". $window1::dbh->errstr;
          next;
        };
        $sth->execute or do {
          warn "should pop up an error message ". $sth->errstr;
          next;
        };

        my $rows = $sth->fetchall_arrayref({});
        
        #my $view = resultview->new( $table, [ $schema->table($table)->columns ], [] );
        #my($window, $label) = resultview->new( $table, [ $window1::schema->table($table)->columns ], [] );
        my($window, $label) = resultview->new( $table, [ $window1::schema->table($table)->columns ], $rows );

        $window1::ick_global_window1->FORM->{'notebook1'}->append_page($window, $label);

        #		$forms->{'window1'}{'notebook1'}->append_page($forms->{'window1'}{'scrolledwindow1'}, $widgets->{'tablelabel1'} );
#		$widgets->{'tablelabel1'}->show;
#		$forms->{'window1'}{'tablelabel1'} = $widgets->{'tablelabel1'};
#		$forms->{'window1'}{'tablelabel1'}->set_alignment(0.5, 0.5 );
#	    $forms->{'window1'}{'vbox1'}->set_child_packing($forms->{'window1'}{'notebook1'}, 1, 1, 0, 'start' );


      }

    } else {
      warn "should pop up an error message $DBI::errstr $@";
    }

  }

  sub on_datasource_entry_changed {
    my ($class, $data, $object, $instance, $event) = @_;
    $datasource = $class->get_text;
  }

  sub on_username_entry_changed {
    my ($class, $data, $object, $instance, $event) = @_;
    $username = $class->get_text;
  }

  sub on_password_entry_changed {
    my ($class, $data, $object, $instance, $event) = @_;
    $password = $class->get_text;
  }

  sub on_savesession_button_clicked {
    my ($class, $data, $object, $instance, $event) = @_;

    save_session($datasource, $username, $password);

  }

  #sub, not method
  sub save_session {
    my($datasource, $username, $password) = @_;
    #this is very lame and embarassing but it works for now.
    open(CONF, ">>$ENV{HOME}/.gtkdbi") or do {
      warn "should pop up an error message: can't open $ENV{HOME}/.gtkdbi: $!";
      #error dialog
      return;
    };
    print CONF "$datasource\t$username\t$password\n";
    close CONF;
  }

  #method
  sub read_sessions {
    my $self = shift;
    #this is very lame and embarassing but it works for now.
    open(CONF, "<$ENV{HOME}/.gtkdbi") or return;
    while (<CONF>) {
      chomp;
      my($datasource, $username, $password) = split(/\t/, $_);
      $session_username{$datasource} = $username;
      $session_password{$datasource} = $password;
      my $menuitem = new Gtk::MenuItem($datasource);
      my $class = ref($self);
      $menuitem->signal_connect('activate', "$class\::on_session_optionmenu_changed", $datasource );
      $self->FORM->{'session_optionmenu_menu'}->append( $menuitem );
      $menuitem->show;
    }
    close CONF;
  }

  sub on_session_optionmenu_changed {
    my( $self, $data ) = @_;

    # via $window1:: ???? !  this is getting worse and worse.
    $window1::connect_dialog->FORM->{'datasource_entry'}->set_text($data);
    $window1::connect_dialog->FORM->{'username_entry'}->set_text($session_username{$data});
    $window1::connect_dialog->FORM->{'password_entry'}->set_text($session_password{$data});
  }

}

#################################3

package resultview;

sub new {
  my ( $self, $labeltext, $columns, $rows ) = @_;

  my $widgets;
  my $forms;

  #ganked from a glade-constructed gtkdbiUI.pm

		# Construct a GtkScrolledWindow 'scrolledwindow1'
		$widgets->{'scrolledwindow1'} = new Gtk::ScrolledWindow( undef, undef);
		$widgets->{'scrolledwindow1'}->set_policy('always', 'always' );
		$widgets->{'scrolledwindow1'}->border_width(0 );
		$widgets->{'scrolledwindow1'}->hscrollbar->set_update_policy('continuous' );
		$widgets->{'scrolledwindow1'}->vscrollbar->set_update_policy('continuous' );
		$widgets->{'scrolledwindow1'}->show;
		$forms->{'window1'}{'scrolledwindow1'} = $widgets->{'scrolledwindow1'};
		    #
		    # Construct a GtkViewport 'viewport1'
		    $widgets->{'viewport1'} = new Gtk::Viewport(new Gtk::Adjustment( 0.0, 0.0, 101.0, 0.1, 1.0, 1.0), new Gtk::Adjustment( 0.0, 0.0, 101.0, 0.1, 1.0, 1.0) );
		    $widgets->{'viewport1'}->set_shadow_type('in' );
		    $forms->{'window1'}{'scrolledwindow1'}->add_with_viewport($widgets->{'viewport1'} );
		    $widgets->{'viewport1'}->show;
		    $forms->{'window1'}{'viewport1'} = $widgets->{'viewport1'};
			#
			# Construct a GtkHBox 'hbox1'
			$widgets->{'hbox1'} = new Gtk::HBox(0, 0 );
			$forms->{'window1'}{'viewport1'}->add($widgets->{'hbox1'} );
			$widgets->{'hbox1'}->show;
			$forms->{'window1'}{'hbox1'} = $widgets->{'hbox1'};

			foreach my $column ( @$columns ) {
			    #
			    # Construct a GtkVBox 'vbox2'
			    #$widgets->{'vbox2'} = new Gtk::VBox(0, 0 );
			    my $vbox = new Gtk::VBox(0, 0 );
			    #$forms->{'window1'}{'hbox1'}->add($widgets->{'vbox2'} );
			    $forms->{'window1'}{'hbox1'}->add($vbox);
			    #$widgets->{'vbox2'}->show;
			    $vbox->show;
			    #$forms->{'window1'}{'vbox2'} = $widgets->{'vbox2'};
				#
				# Construct a GtkButton 'button1'
				#$widgets->{'button1'} = new Gtk::Button(_('button1'));
				my $button = new Gtk::Button($column);
				#$forms->{'window1'}{'vbox2'}->add($widgets->{'button1'} );
				$vbox->add($button );
				#$widgets->{'button1'}->show;
				$button->show;
				#$forms->{'window1'}{'button1'} = $widgets->{'button1'};
				#$forms->{'window1'}{'button1'}->can_focus(1 );
				$button->can_focus(1 );
				#$forms->{'window1'}{'vbox2'}->set_child_packing($forms->{'window1'}{'button1'}, 0, 0, 0, 'start' );
				$vbox->set_child_packing($button, 0, 0, 0, 'start' );
				#
				# Construct a GtkEntry 'entry2'
				foreach my $row ( @$rows ) {
				    #$widgets->{'entry2'} = new Gtk::Entry;
				    my $entry = new Gtk::Entry;
				    #$forms->{'window1'}{'vbox2'}->add($widgets->{'entry2'} );
				    $vbox->add($entry );
				    #$widgets->{'entry2'}->show;
				    $entry->show;
				    #$forms->{'window1'}{'entry2'} = $widgets->{'entry2'};
				    $entry->can_focus(1 );
				    #$entry->set_text(_('') );
				    $entry->set_text($row->{$column});
				    $entry->set_max_length(0 );
				    $entry->set_visibility(1 );
				    $entry->set_editable(1 );

				    #$forms->{'window1'}{'vbox2'}->set_child_packing($forms->{'window1'}{'entry2'}, 0, 0, 0, 'start' );
				    $vbox->set_child_packing($entry, 0, 0, 0, 'start' );
				}

			        #$forms->{'window1'}{'hbox1'}->set_child_packing($forms->{'window1'}{'vbox2'}, 1, 1, 0, 'start' );
			        $forms->{'window1'}{'hbox1'}->set_child_packing($vbox, 1, 1, 0, 'start' );
			}

#			    #
#			    # Construct a GtkVBox 'vbox3'
#			    $widgets->{'vbox3'} = new Gtk::VBox(0, 0 );
#			    $forms->{'window1'}{'hbox1'}->add($widgets->{'vbox3'} );
#			    $widgets->{'vbox3'}->show;
#			    $forms->{'window1'}{'vbox3'} = $widgets->{'vbox3'};
#				#
#				# Construct a GtkButton 'button2'
#				$widgets->{'button2'} = new Gtk::Button(_('button2'));
#				$forms->{'window1'}{'vbox3'}->add($widgets->{'button2'} );
#				$widgets->{'button2'}->show;
#				$forms->{'window1'}{'button2'} = $widgets->{'button2'};
#				$forms->{'window1'}{'button2'}->can_focus(1 );
#				$forms->{'window1'}{'vbox3'}->set_child_packing($forms->{'window1'}{'button2'}, 0, 0, 0, 'start' );
#				#
#				# Construct a GtkEntry 'entry3'
#				$widgets->{'entry3'} = new Gtk::Entry;
#				$forms->{'window1'}{'vbox3'}->add($widgets->{'entry3'} );
#				$widgets->{'entry3'}->show;
#				$forms->{'window1'}{'entry3'} = $widgets->{'entry3'};
#				$forms->{'window1'}{'entry3'}->can_focus(1 );
#				$forms->{'window1'}{'entry3'}->set_text(_('') );
#				$forms->{'window1'}{'entry3'}->set_max_length(0 );
#				$forms->{'window1'}{'entry3'}->set_visibility(1 );
#				$forms->{'window1'}{'entry3'}->set_editable(1 );
#				$forms->{'window1'}{'vbox3'}->set_child_packing($forms->{'window1'}{'entry3'}, 0, 0, 0, 'start' );
#			    $forms->{'window1'}{'hbox1'}->set_child_packing($forms->{'window1'}{'vbox3'}, 1, 1, 0, 'start' );

		#
		# Construct a GtkLabel 'tablelabel1'
		#$widgets->{'tablelabel1'} = new Gtk::Label(_('tabletable1'));
		my $label = new Gtk::Label($labeltext);
		#$widgets->{'tablelabel1'}->set_justify('center' );
		$label->set_justify('center' );
		#$widgets->{'tablelabel1'}->set_line_wrap(0 );
		$label->set_line_wrap(0 );

  ( $widgets->{'scrolledwindow1'}, $label );

#		$forms->{'window1'}{'notebook1'}->append_page($forms->{'window1'}{'scrolledwindow1'}, $widgets->{'tablelabel1'} );
#		$widgets->{'tablelabel1'}->show;
#		$forms->{'window1'}{'tablelabel1'} = $widgets->{'tablelabel1'};
#		$forms->{'window1'}{'tablelabel1'}->set_alignment(0.5, 0.5 );
#	    $forms->{'window1'}{'vbox1'}->set_child_packing($forms->{'window1'}{'notebook1'}, 1, 1, 0, 'start' );


}

1;

__END__

#===============================================================================
#==== Documentation
#===============================================================================
=pod

=head1 NAME

gtkdbi - version 0.01 Tue Oct 23 01:49:12 PDT 2001

No description

=head1 SYNOPSIS

 use gtkdbi;

 To construct the window object and show it call
 
 Gtk->init;
 my $window = window1->new;
 $window->TOPLEVEL->show;
 Gtk->main;
 
 OR use the shorthand for the above calls
 
 window1->app_run;

=head1 DESCRIPTION

Unfortunately, the author has not yet written any documentation :-(

=head1 AUTHOR

Ivan Kohler,Chess room,888-670-SISD,215-996-0824 <ivan\@rootwood.haze.st>

=cut
