#! /usr/bin/perl
package vostok;

use strict;
use Gtk2 -init;
use Gtk2::GladeXML;
use Glib qw/TRUE FALSE/;
use base 'Class::Accessor::Fast';
__PACKAGE__->mk_accessors(qw(picture steg_text password pass_text));

sub run {
	my $self = shift->new();
	Gtk2->main;
}

sub new {
	return bless ({}, shift)->_init;
}

sub _init {
	my $self = shift;
	my $glade_file = __FILE__;
	print $glade_file;
	$glade_file =~ s/vostok.pm/vostok.glade/;
	my $gui = Gtk2::GladeXML->new($glade_file);
	$gui->signal_autoconnect_from_package($self);
	
	$self->picture($gui->get_widget('image_main'));
	$self->steg_text($gui->get_widget('textview1'));
	$self->steg_text->set_editable(TRUE);
	
	return $self;
}

sub on_appwin_delete_event {
	Gtk2->main_quit;
}

sub gtk_main_quit {
	Gtk2->main_quit;
}

sub on_appwin_open {
	my $self = shift;
	my $open_file_d = Gtk2::FileChooserDialog->new('open', undef, 'open', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok');
	my $filename;
	if ('ok' eq $open_file_d->run){
		$filename = $open_file_d->get_filename;
		
		my $pixbufloader = Gtk2::Gdk::PixbufLoader->new;
		my $raw_data = `cat $filename`;
		$pixbufloader->write($raw_data);
		$pixbufloader->close;
		my $pixbuf = $pixbufloader->get_pixbuf;
		my ($format, $width, $height) = $pixbuf->get_file_info ($filename);
		my $new_height = int($height/($width/300));
		my $scaled_pic = $pixbuf->scale_simple(300, $new_height, 'bilinear');
		#my $img = Gtk2::Image->new_from_pixbuf($scaled_pic);
		print "$width, $height, $new_height\n";
		#print "filename $filename";
		$self->picture->set_from_pixbuf ($scaled_pic);
		
		#stegofile
		#ask for pass
		my $pass = show_pass_dialog($open_file_d);
		my $status;
		my $buffer = Gtk2::TextBuffer->new(); #creating a text buffer
		$buffer->set_text(""); #delete text
		$self->steg_text->set_buffer($buffer);
		if ($pass){	$status = system('steghide info '.$filename.' -p '.$pass)}
		else {$status = system('steghide info '.$filename.' -p ""')};
		print "status: $status \n";
		unless ($status){
			#run decription
			system('steghide extract -sf '.$filename.' -xf output.txt -p '.$pass.' -f');
			my $text;
			open FILE, "<output.txt" or die $!;
				 while (<FILE>){
					$text .= $_;
				 }	
			close FILE;
			unlink("output.txt");
			print "$text";
			$buffer->set_text($text);
			$self->steg_text->set_buffer($buffer);
		}
		
	}
	$open_file_d->destroy;
	return
}

sub on_appwin_save {
	my $self = shift;
	my $save_file_d = Gtk2::FileChooserDialog->new('save', undef, 'save', 'gtk-cancel' => 'cancel', 'gtk-ok' => 'ok');
	my $filename;
	if ('ok' eq $save_file_d->run){
		$filename = $save_file_d->get_filename;
		#checking for overlap
		if (-f $filename) {
			my $overwrite = show_message_dialog($save_file_d, 'question','Overwrite existing file:'."<b>\n$filename</b>",'yes-no');
			if ($overwrite eq 'no'){$save_file_d->destroy; return;};
		}
		my $pixbuf = $self->picture->get_pixbuf;
		if ($pixbuf){
			$pixbuf->save("$filename", 'jpeg', quality => '100');
		}
		#save text
		my $buffer = $self->steg_text->get_buffer();
		
		my $string = $buffer->get_text($buffer->get_start_iter, $buffer->get_end_iter, FALSE);

		if ($string){
			#call if need to put a password
			my $pass = show_pass_dialog($save_file_d);
			unless ($pass){$pass = ""};
			
			open FILE, ">$filename.txt" or die $!;
			printf FILE $string;
			close FILE;
			system('steghide embed -cf '.$filename.' -ef '.$filename.'.txt -p '.$pass);
			unlink("$filename.txt");
		}
		
		
	}
	$save_file_d->destroy;
	return 0;
}

sub show_message_dialog {
	my ($self, $icon, $text, $button_type) = @_;
	my $dialog = Gtk2::MessageDialog->new_with_markup($self, [qw/modal destroy-with-parent/], $icon,$button_type, sprintf "$text");
	my $retval = $dialog->run;
	$dialog->destroy;
	return $retval;
}

sub show_pass_dialog {
	my ($self) = @_;
	#my $window = Gtk2::Window->new('toplevel');
	my ($password, $pass_text);
	my $pass_window = Gtk2::Dialog->new ('Enter your password',
                              undef,
                              [qw/modal destroy-with-parent/],
                              'gtk-ok'     => 'accept',
                              'gtk-cancel' => 'reject'); 
	#my $vbox = $window->vbox;                             
    my $entry = Gtk2::Entry->new();
    $entry->signal_connect (changed => sub {$pass_text = $entry->get_text;});
	
	$pass_window->vbox->add($entry);
	#$vbox->pack_start($pass_window,0,0,4);
	#$vbox->show_all();
    $entry->show;
                            
	if ('accept' eq $pass_window->run){
		$password = $pass_text;
	}
	$pass_window->destroy;
	return $password;
}

sub show_help_dialog {
	my ($self) = @_;
	my $dialog = Gtk2::Dialog->new ('Help',
                              undef,
                              [qw/modal destroy-with-parent/],
                              'gtk-ok'     => 'accept');
    my $text = Gtk2::Label->new("This is just a front end for steghide (an application for creating steganographic images. Copyright: tomoki tsuchiya. License: the same as perl license.");
    $dialog->vbox->add($text);
    $text->show;
                       
	$dialog->run;
	$dialog->destroy;
	return 0;
}

1;
