ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/kgsueme/kgsueme/appwin.pl
Revision: 1.10
Committed: Sat Jun 28 16:44:56 2003 UTC (20 years, 11 months ago) by pcg
Content type: text/plain
Branch: MAIN
Changes since 1.9: +3 -2 lines
Log Message:
*** empty log message ***

File Contents

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