1 | package room; |
1 | package room; |
2 | |
2 | |
|
|
3 | use KGS::Constants; |
|
|
4 | |
3 | use base KGS::Listener::Room; |
5 | use base KGS::Listener::Room; |
4 | use base gtk::widget; |
6 | |
|
|
7 | use Glib::Object::Subclass |
|
|
8 | Gtk2::Frame; |
5 | |
9 | |
6 | sub new { |
10 | sub new { |
7 | my $self = shift; |
11 | my ($self, %arg) = @_; |
8 | $self = $self->SUPER::new(@_); |
12 | $self = $self->Glib::Object::new; |
|
|
13 | $self->{$_} = delete $arg{$_} for keys %arg; |
9 | |
14 | |
10 | $self->listen($self->{conn}, qw(msg_room:)); |
15 | $self->signal_connect (destroy => sub { |
|
|
16 | delete $::config->{rooms}{$self->{channel}}; |
|
|
17 | delete $self->{app}{room}{$self->{channel}}; |
|
|
18 | (remove Glib::Source delete $self->{gameupdate}) if $self->{gameupdate}; |
|
|
19 | $self->unlisten; |
|
|
20 | %{$_[0]} = (); |
|
|
21 | }); |
11 | |
22 | |
12 | $self->{window} = new Gtk2::Window 'toplevel'; |
23 | $self->listen ($self->{conn}, qw(msg_room:)); |
13 | $self->{window}->set_title("KGS Room $self->{name}"); |
|
|
14 | gtk::state $self->{window}, "room::window", $self->{name}, window_size => [600, 400]; |
|
|
15 | |
24 | |
16 | $self->{window}->signal_connect(delete_event => sub { $self->part; 1 }); |
25 | $self->signal_connect (delete_event => sub { $self->part; 1 }); |
17 | |
26 | |
18 | $self->{window}->add(my $hpane = new Gtk2::HPaned); |
27 | $self->add (my $hbox = new Gtk2::HBox); |
19 | $hpane->set(position_set => 1); |
|
|
20 | gtk::state $hpane, "room::hpane", $self->{name}, position => 200; |
|
|
21 | |
28 | |
22 | $hpane->pack1((my $vbox = new Gtk2::VBox), 1, 1); |
29 | $hbox->pack_start ((my $vbox = new Gtk2::VBox), 1, 1, 0); |
23 | |
30 | |
|
|
31 | $vbox->add ($self->{chat} = new chat); |
|
|
32 | |
|
|
33 | $self->{chat}->signal_connect(command => sub { |
|
|
34 | my ($chat, $cmd, $arg) = @_; |
|
|
35 | $self->{app}->do_command ($chat, $cmd, $arg, userlist => $self->{userlist}, room => $self); |
|
|
36 | }); |
|
|
37 | |
|
|
38 | $hbox->pack_start ((my $vbox = new Gtk2::VBox), 0, 1, 0); |
|
|
39 | |
|
|
40 | $vbox->pack_start ((my $button = new_with_label Gtk2::Button "Close"), 0, 1, 0); |
|
|
41 | $button->signal_connect (clicked => sub { $self->part }); |
|
|
42 | |
|
|
43 | $vbox->pack_start ((my $button = new_with_label Gtk2::Button "New Game"), 0, 1, 0); |
|
|
44 | $button->signal_connect (clicked => sub { $self->new_game }); |
|
|
45 | |
24 | $vbox->add(my $sw = new Gtk2::ScrolledWindow); |
46 | $vbox->pack_start ((my $sw = new Gtk2::ScrolledWindow), 1, 1, 0); |
25 | $sw->set_policy("automatic", "always"); |
47 | $sw->set_policy("automatic", "always"); |
26 | |
48 | |
27 | $sw->add(($self->{text} = new gtk::text)->widget); |
|
|
28 | |
|
|
29 | $vbox->pack_start(($self->{entry} = new Gtk2::Entry), 0, 1, 0); |
|
|
30 | $self->{entry}->signal_connect(activate => sub { |
|
|
31 | my $text = $self->{entry}->get_text; |
|
|
32 | $self->say($text) if $text =~ /\S/; |
|
|
33 | $self->{entry}->set_text(""); |
|
|
34 | }); |
|
|
35 | |
|
|
36 | $hpane->pack2((my $sw = new Gtk2::ScrolledWindow), 0, 1); |
|
|
37 | $sw->set_policy("automatic", "always"); |
|
|
38 | |
|
|
39 | $sw->add(($self->{userlist} = new gtk::userlist)->widget); |
49 | $sw->add ($self->{userlist} = new userlist); |
40 | |
50 | |
41 | $self; |
51 | $self; |
42 | } |
52 | } |
43 | |
53 | |
44 | sub join { |
54 | sub FINALIZE_INSTANCE { print "FIN room\n" } # never called MEMLEAK #d#TODO# |
45 | my ($self) = @_; |
|
|
46 | $self->SUPER::join; |
|
|
47 | |
|
|
48 | $self->{window}->show_all; |
|
|
49 | } |
|
|
50 | |
55 | |
51 | sub part { |
56 | sub part { |
52 | my ($self) = @_; |
57 | my ($self) = @_; |
53 | $self->SUPER::part; |
58 | $self->SUPER::part; |
54 | |
59 | |
55 | delete $::config->{rooms}{$self->{channel}}; |
|
|
56 | $self->{window}->hide_all; |
60 | $self->hide_all; |
57 | } |
61 | } |
58 | |
62 | |
59 | sub inject_msg_room { |
63 | sub inject_msg_room { |
60 | my ($self, $msg) = @_; |
64 | my ($self, $msg) = @_; |
61 | |
65 | |
62 | $self->{text}->append_text("\n$msg->{name}: $msg->{message}"); |
66 | # secret typoe ;-) |
|
|
67 | $self->{chat}->append_text("\n<header><user>" . (util::toxml $msg->{name}) |
|
|
68 | . "</user>: </header>" . (util::toxml $msg->{message})); |
63 | } |
69 | } |
64 | |
70 | |
65 | sub event_update_users { |
71 | sub event_update_users { |
66 | my ($self) = @_; |
72 | my ($self, $add, $update, $remove) = @_; |
67 | |
73 | |
68 | $self->{userlist}->update ($self->{users}); |
74 | $self->{userlist}->update ($add, $update, $remove); |
69 | } |
75 | } |
70 | |
76 | |
71 | sub event_update_games { |
77 | sub event_update_games { |
72 | my ($self) = @_; |
78 | my ($self, $add, $update, $remove) = @_; |
73 | |
79 | |
74 | $appwin::gamelist->update ($self); |
80 | $self->{app}{gamelist}->update ($self, $add, $update, $remove); |
75 | } |
81 | } |
76 | |
82 | |
77 | sub event_join { |
83 | sub event_join { |
78 | my ($self) = @_; |
84 | my ($self) = @_; |
79 | $self->SUPER::event_join; |
85 | $self->SUPER::event_join; |
80 | |
86 | |
81 | $::config->{rooms}{$self->{channel}} = { channel => $self->{channel}, name => $self->{name} }; |
87 | $::config->{rooms}{$self->{channel}} = { channel => $self->{channel}, name => $self->{name} }; |
|
|
88 | |
|
|
89 | # mysteriously enough, we have to request game updates manually |
|
|
90 | $self->{gameupdate} ||= add Glib::Timeout INTERVAL_GAMEUPDATES * 1000, sub { |
|
|
91 | $self->req_games; |
|
|
92 | 1; |
|
|
93 | }; |
|
|
94 | |
|
|
95 | $self->show_all; |
|
|
96 | } |
|
|
97 | |
|
|
98 | sub event_part { |
|
|
99 | my ($self) = @_; |
|
|
100 | |
|
|
101 | $self->SUPER::event_part; |
|
|
102 | $self->destroy; |
82 | } |
103 | } |
83 | |
104 | |
84 | sub event_update_roominfo { |
105 | sub event_update_roominfo { |
85 | my ($self) = @_; |
106 | my ($self) = @_; |
86 | |
107 | |
87 | $self->{text}->append_text("\n$self->{owner}: $self->{description}\n"); |
108 | $self->{chat}->append_text("\n<user>" . (util::toxml $self->{owner}) . "</user>\n" |
|
|
109 | . "<description>" . (util::toxml $self->{description}) . "</description>\n"); |
|
|
110 | } |
|
|
111 | |
|
|
112 | sub new_game { |
|
|
113 | my ($self) = @_; |
|
|
114 | |
|
|
115 | $self->{new_game} ||= new game conn => $self->{conn}, app => $self; |
|
|
116 | $self->{new_game}->show_all; |
88 | } |
117 | } |
89 | |
118 | |
90 | 1; |
119 | 1; |
91 | |
120 | |