1 |
package appwin; |
2 |
|
3 |
use KGS::Protocol::Client; |
4 |
|
5 |
use Scalar::Util; |
6 |
|
7 |
use base KGS::Listener; |
8 |
use base gtk::widget; |
9 |
|
10 |
my %context_id; |
11 |
|
12 |
# static method to request the userimage and call the cb when it's available. |
13 |
sub userpic { |
14 |
my ($name, $cb) = @_; |
15 |
$self->get_userpic ($name, $cb); |
16 |
} |
17 |
|
18 |
sub status { |
19 |
my ($type, $text) = @_; |
20 |
|
21 |
$self->{status}->pop($context_id{$type}) if $context_id{$type}; |
22 |
$self->{status}->push($context_id{$type} ||= $self->{status}->get_context_id($type), $text) if $text; |
23 |
} |
24 |
|
25 |
sub new { |
26 |
my $self = shift; |
27 |
$self = $self->SUPER::new(@_); |
28 |
|
29 |
$appwin::self = $self; # singleton |
30 |
Scalar::Util::weaken $appwin::self; |
31 |
|
32 |
$self->{conn} = new KGS::Protocol::Client; |
33 |
|
34 |
KGS::Listener::Debug->new->listen($self->{conn}, "any"); #d# debug only :) |
35 |
|
36 |
$self->listen($self->{conn}, qw(login userpic idle_warn)); |
37 |
|
38 |
$self->{roomlist} = new roomlist conn => $self->{conn}, appwin => $self; |
39 |
|
40 |
$self->{window} = new Gtk2::Window 'toplevel'; |
41 |
$self->{window}->set_title('kgsueme'); |
42 |
gtk::state $self->{window}, "main::window", undef, window_size => [400, 400]; |
43 |
$self->{window}->signal_connect(delete_event => sub { main_quit Gtk2; 1 }); |
44 |
|
45 |
$self->{window}->add(my $vbox = new Gtk2::VBox); |
46 |
|
47 |
$vbox->pack_start(($buttonbox = new Gtk2::HButtonBox), 0, 1, 0); |
48 |
$buttonbox->set_spacing(0); |
49 |
|
50 |
my $button = sub { |
51 |
$buttonbox->add(my $button = new Gtk2::Button $_[0]); |
52 |
signal_connect $button clicked => $_[1]; |
53 |
}; |
54 |
|
55 |
$button->("Login", sub { $self->login; }); |
56 |
$button->("Roomlist", sub { $self->{roomlist}->show; }); |
57 |
$button->("Save Config & Layout", \&util::save_config); |
58 |
$button->("Quit", sub { main_quit Gtk2 }); |
59 |
|
60 |
$vbox->pack_start((my $hbox = new Gtk2::HBox), 0, 1, 0); |
61 |
|
62 |
$hbox->add(new Gtk2::Label "Login"); |
63 |
|
64 |
$hbox->add($self->{login} = new_with_max_length Gtk2::Entry 12); |
65 |
$self->{login}->set_text($::config->{login}); |
66 |
|
67 |
if ($::HACK) { |
68 |
$self->{login}->signal_connect(activate => sub { |
69 |
$self->{conn}{name} = $self->{login}->get_text; |
70 |
}); |
71 |
} |
72 |
|
73 |
$hbox->add(new Gtk2::Label "Password"); |
74 |
$hbox->add($self->{password} = new Gtk2::Entry); |
75 |
$self->{password}->set_visibility(0); |
76 |
|
77 |
$self->{gamelist} = new gamelist conn => $self->{conn}; |
78 |
$vbox->pack_start ($self->{gamelist}->widget, 1, 1, 0); |
79 |
|
80 |
$appwin::gamelist = $self->{gamelist}; |
81 |
Scalar::Util::weaken $appwin::gamelist; |
82 |
|
83 |
$vbox->pack_start(($self->{status} = new Gtk2::Statusbar), 0, 1, 0); |
84 |
|
85 |
$self->{window}->show_all; |
86 |
|
87 |
$self; |
88 |
} |
89 |
|
90 |
sub login { |
91 |
my ($self) = @_; |
92 |
|
93 |
$self->{conn}->disconnect; |
94 |
|
95 |
# initialize new socket and connection |
96 |
#my $sock = new IO::Socket::INET PeerHost => "kgs.kiseido.com", PeerPort => "2379" |
97 |
my $sock = new IO::Socket::INET PeerHost => $ENV{KGSHOST} || "kgs.kiseido.com", PeerPort => "2379" |
98 |
or die; |
99 |
|
100 |
$sock->blocking(1); |
101 |
$self->{conn}->handshake($sock); |
102 |
$sock->blocking(0); |
103 |
|
104 |
my $input; $input = add_watch Glib::IO fileno $sock, [G_IO_IN, G_IO_ERR, G_IO_HUP], sub { |
105 |
# this is dorked |
106 |
my $buf; |
107 |
my $len = sysread $sock, $buf, 16384; |
108 |
if ($len) { |
109 |
$self->{conn}->feed_data($buf); |
110 |
} elsif (defined $len || (!$!{EINTR} and !$!{EAGAIN})) { |
111 |
warn "disconnected";#d# |
112 |
remove Glib::Source $input; |
113 |
$self->event_disconnect; |
114 |
} |
115 |
1; |
116 |
}, G_PRIORITY_HIGH; |
117 |
|
118 |
# now login |
119 |
$ENV{KGSUEME_CLIENTVER} = "1.4.1_01:Swing app:Sun Microsystems Inc."; # he asked for it...#d# |
120 |
$self->{conn}->login($ENV{KGSUEME_CLIENTVER} || "kgsueme $VERSION $^O", # allow users to overwrite |
121 |
$self->{login}->get_text, |
122 |
$self->{password}->get_text); |
123 |
} |
124 |
|
125 |
sub inject_idle_warn { |
126 |
my ($self, $msg) = @_; |
127 |
|
128 |
$self->send ("idle_reset"); |
129 |
} |
130 |
|
131 |
sub inject_login { |
132 |
my ($self, $msg) = @_; |
133 |
|
134 |
appwin::status("login", "logged in as '$self->{conn}{name}' with status '$msg->{result}' ('$msg->{reason}')"); |
135 |
$::config->{login} = $self->{conn}{name}; |
136 |
|
137 |
if ($msg->{success}) { |
138 |
# auto-join |
139 |
for (values %{$::config->{rooms}}) { |
140 |
$self->{roomlist}->join_room($_); |
141 |
} |
142 |
sound::play 3, "connect"; |
143 |
} elsif ($msg->{result} eq "user unknown") { |
144 |
sound::play 2, "user_unknown"; |
145 |
} else { |
146 |
sound::play 2, "warning"; |
147 |
} |
148 |
} |
149 |
|
150 |
my %userpic; |
151 |
my %userpic_cb; |
152 |
|
153 |
sub get_userpic { |
154 |
my ($self, $name, $cb) = @_; |
155 |
|
156 |
if (exists $userpic{$name}) { |
157 |
$cb->($userpic{$name}); |
158 |
} else { |
159 |
if (!exists $userpic_cb{$name}) { |
160 |
# after 10 seconds, flush callbacks |
161 |
$self->send (req_pic => name => $name); |
162 |
add Glib::Timeout 10000, sub { |
163 |
$_->() for @{delete $userpic_cb{$name} || []}; |
164 |
0; |
165 |
}; |
166 |
} |
167 |
push @{$userpic_cb{$name}}, $cb; |
168 |
} |
169 |
} |
170 |
|
171 |
sub inject_userpic { |
172 |
my ($self, $msg) = @_; |
173 |
|
174 |
$userpic{$msg->{name}} = $msg->{data}; |
175 |
$_->($userpic{$msg->{name}}) for @{delete $userpic_cb{$msg->{name}} || []}; |
176 |
} |
177 |
|
178 |
sub event_disconnect { } |
179 |
|
180 |
1; |
181 |
|