--- kgsueme/kgsueme/room.pl 2003/07/22 21:24:50 1.14 +++ kgsueme/kgsueme/room.pl 2004/06/08 17:58:48 1.32 @@ -3,63 +3,72 @@ use KGS::Constants; use base KGS::Listener::Room; -use base gtk::widget; + +use Glib::Object::Subclass + Gtk2::Frame; sub new { - my $self = shift; - $self = $self->SUPER::new(@_); + my ($self, %arg) = @_; + + $self = $self->Glib::Object::new; + $self->{$_} = delete $arg{$_} for keys %arg; - $self->listen($self->{conn}, qw(msg_room:)); + $self->signal_connect (destroy => sub { + delete $::config->{rooms}{$self->{channel}}; + delete $self->{app}{room}{$self->{channel}}; + (remove Glib::Source delete $self->{gameupdate}) if $self->{gameupdate}; + $self->unlisten; + %{$_[0]} = (); + }); - $self->{window} = new Gtk2::Window 'toplevel'; - $self->{window}->set_title("KGS Room $self->{name}"); - gtk::state $self->{window}, "room::window", $self->{name}, window_size => [600, 400]; + $self->listen ($self->{conn}, qw(msg_room:)); - $self->{window}->signal_connect(delete_event => sub { $self->part; 1 }); + $self->signal_connect (delete_event => sub { $self->part; 1 }); - $self->{window}->add($self->{hpane} = new Gtk2::HPaned); - $self->{hpane}->set(position_set => 1); - gtk::state $self->{hpane}, "room::hpane", $self->{name}, position => 200; + $self->add (my $vbox = new Gtk2::VBox); - $self->{hpane}->pack1((my $vbox = new Gtk2::VBox), 1, 1); + $vbox->pack_start((my $hbox = new Gtk2::HBox), 0, 1, 0); + + $hbox->pack_start ((my $button = new_with_label Gtk2::Button "Leave"), 0, 1, 0); + $button->signal_connect (clicked => sub { $self->part }); - $vbox->add(($self->{text} = new gtk::text)->widget); + $hbox->pack_start ((my $button = new_with_label Gtk2::Button "New Game"), 0, 1, 0); + $button->signal_connect (clicked => sub { $self->new_game }); + + $vbox->pack_start ((my $hpane = new Gtk2::HPaned), 1, 1, 0); + gtk::state $hpane, "room::hpane", undef, position => 500; - $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0); - $self->{entry}->signal_connect(activate => sub { - my $text = $self->{entry}->get_text; - $self->say($text) if $text =~ /\S/; - $self->{entry}->set_text(""); + $hpane->pack1 (($self->{chat} = new chat app => $self->{app}), 1, 0); + + $self->{chat}->signal_connect (command => sub { + my ($chat, $cmd, $arg) = @_; + $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, room => $self); }); - $self->{hpane}->pack2((my $sw = new Gtk2::ScrolledWindow), 0, 1); - $sw->set_policy("automatic", "always"); + $hpane->pack2 ((my $sw = new Gtk2::ScrolledWindow), 1, 0); + + $sw->set_policy ("automatic", "always"); - $sw->add(($self->{userlist} = new userlist)->widget); + $sw->add ($self->{userlist} = new userlist); $self; } -sub join { - my ($self) = @_; - $self->SUPER::join; - - $self->{window}->show_all; -} +sub FINALIZE_INSTANCE { print "FIN room\n" } # never called MEMLEAK #d#TODO# sub part { my ($self) = @_; - $self->SUPER::part; - $self->destroy; # yeaha + $self->hide; + $self->SUPER::part; } sub inject_msg_room { my ($self, $msg) = @_; # secret typoe ;-) - $self->{text}->append_text("\n
" . (util::toxml $msg->{name}) - . ":
" . (util::toxml $msg->{message})); + $self->{chat}->append_text ("\n" . (util::toxml $msg->{name}) + . ": " . (util::toxml $msg->{message})); } sub event_update_users { @@ -72,6 +81,20 @@ my ($self, $add, $update, $remove) = @_; $self->{app}{gamelist}->update ($self, $add, $update, $remove); + + # try to identify any new games assigned to us. stupid protocol + # first updates the game, joins you and THEN tells you that + # which of the games you asked for this is. + + for (@$add) { + if (($_->{black}{name} eq $self->{conn}{name} + || $_->{white}{name} eq $self->{conn}{name} + || $_->{owner}{name} eq $self->{conn}{name}) + && (my $game = shift @{$self->{new_game}})) { + $game->inject_upd_game ({ game => $_ }); + $game->set_channel ($game->{channel}); + } + } } sub event_join { @@ -85,32 +108,39 @@ $self->req_games; 1; }; + + $self->show_all; } sub event_part { my ($self) = @_; - delete $::config->{rooms}{$self->{channel}}; - delete $self->{app}{roomlist}{room}{$self->{channel}}; - (remove Glib::Source delete $self->{gameupdate}) if $self->{gameupdate}; - $self->unlisten; - $self->SUPER::event_part; + $self->destroy; +} + +sub event_quit { + my ($self) = @_; + + $self->SUPER::event_quit; + $self->destroy; } sub event_update_roominfo { my ($self) = @_; - $self->{text}->append_text("\n" . (util::toxml $self->{owner}) . "\n" + $self->{chat}->append_text("\n" . (util::toxml $self->{owner}) . "\n" . "" . (util::toxml $self->{description}) . "\n"); } -sub destroy { +sub new_game { my ($self) = @_; - $self->event_part; + my $game = new game conn => $self->{conn}, app => $self->{app}, roomid => $self->{channel}; + $game->new_game_challenge; + $game->show_all; - $self->SUPER::destroy; + push @{$self->{new_game}}, $game; } 1;