#!/usr/bin/perl -w
use strict;
use English;
use Tk;
use Tk::HList;

# TODO	- uswanie z ksizki adresowej

# $root - uchwyt gwnego okna Tk
# $adresy - lista adresow
# $font - uywany font
# $numer - aktualny numer
# $tresc - tre SMSa
# $status - linia statusu
# $lista - lista z ksiazki numerow
# $dodaj - nazwa pod jaka ma byc dodany numer
use vars '$root','$adresy','$font','$numer','$tresc','$status','$lista','$dodaj';

# Domylny font
$font = "-*-iso8859-2";
#$font = "-*-*-regular-r-*-iso8859-2";

# Utworzenie okienek i wejcie do gwnej ptli
&create_root;
MainLoop;

############################
# Utworzenie okna gwnego #
############################
sub create_root {
$root=MainWindow->new();
$root->title("Tk/Sms");
$root->minsize(qw(320 200));
# Gra okna - pola numeru, treci i przycisk wylij
my $up = $root->Frame();
$up->Label( -text => "Numer:", -font => $font)->
	pack( -padx => "1m",
		-pady => "1m",
		-side => "left");
$numer = $up->Entry( -width => "15", -font => $font);
$numer->pack( -padx => "1m",
	-pady => "1m",
	-side => "left");
$numer->bind( '<Return>', \&do_send);
$up->Button( -text => "Wylij", -font => $font,
	-command => \&do_send)->
        pack( -padx => "1m",
		-pady => "1m",
		-side => "left");
$up->Button( -text => "Koniec", -font => $font,
	-command => sub{ exit(0); })->
        pack( -padx => "1m",
		-pady => "1m",
		-side => "right",
		-anchor => "e");
$up->pack( -anchor => "w",
	-fill => "x");
$tresc = $root->Entry( -width => "100", -font => $font);
$tresc->bind( '<Return>', \&do_send);
$tresc->pack( -padx => "1m",
	-pady => "1m",
	-fill => "x",
	-side => "top");
$tresc->focus();
my $down = $root->Frame();
$lista = $down->Scrolled('HList', -font => $font,
	-separator => "/",
	-header => "1",
	-columns => "2",
	-scrollbars => "osoe",
	-height => "10",
	-width => "50",
	-command => \&do_fillnum,
	)->pack( -side => "left",
		-anchor => "w",
		-fill => "both",
		-expand => "y");
$lista->header("create", 0, -text => "Nazwa");
$lista->header("create", 1, -text => "Numer");
$down->Label( -text => "Dodaj jako:", -font => $font)->
	pack( -padx => "1m",
		-pady => "1m",
		-side => "left",
		);
$dodaj = $down->Entry( -width => "30", -font => $font);
$dodaj->bind( '<Return>', \&do_dodaj);
$dodaj->pack( -padx => "1m",
	-pady => "1m",
	-side => "left",
	);
$down->Button( -text => "Ok", -font => $font,
	-command => \&do_dodaj )->
	pack( -padx => "1m",
		-pady => "1m",
		-side => "left",
		);
$down->pack( -fill => "both",
		-expand => "both");
$status = $root->Label( -text => "Status:", -font => $font)->
	pack( -padx => "1m",
		-pady => "1m",
		-anchor => "w",
		-side => "bottom");
&updatelist();
}

# Wysanie wiadomoci
sub do_send {
my $bad = 0;
return if $numer->get() eq "";
return if $tresc->get() eq "";
$status->configure( -text => "Status: Wysyam");
$root->idletasks();
open (SMS, "sms_wr '" . $numer->get() . "' '" . $tresc->get(). "' |") or die "Bd sms: $!\n";
while (<SMS>) {
chop;
$status->configure( -text => "Status: $_");
$bad = 1;
}
$status->configure( -text => "Status:") if not $bad;
}

# Wypelnienie numeru z listy
sub do_fillnum {
my $num = shift;
$numer->delete(0,"end");
$num =~ s/\ .*$//;
$numer->insert(0,$num);
}

# Wypelnienie listy z smsaddr
sub updatelist {
$lista->delete("all");
open(SMSADDR, "smsaddr -l |") or die "Bd smsaddr: $!\n";
my @adr = sort <SMSADDR>;
foreach (@adr) {
chop;
(my $naz, my $num) = split /\t\t/;
$lista->add("$num $naz");
$lista->itemCreate("$num $naz", 0, -text => $naz);
$lista->itemCreate("$num $naz", 1, -text => $num);
}
}

# Dodawanie numeru do listy
sub do_dodaj {
my $bad=0;
return if $numer->get() eq "";
return if $dodaj->get() eq "";
$status->configure( -text => "Status: Dodaje do listy numerw");
$root->idletasks();
open (SMSADD, "smsaddr -a '" . $dodaj->get() . "' '" . $numer->get(). "' |") or die "Bd smsaddr: $!\n";
while (<SMSADD>) {
chop;
$status->configure( -text => "Status: $_");
$bad=1;
}
$status->configure( -text => "Status:") if not $bad;
&updatelist();
}
