1 |
pcg |
1.1 |
package appwin; |
2 |
|
|
|
3 |
|
|
use base KGS::Listener; |
4 |
|
|
|
5 |
|
|
my %context_id; |
6 |
|
|
|
7 |
|
|
sub status { |
8 |
|
|
my ($type, $text) = @_; |
9 |
|
|
|
10 |
|
|
$self->{status}->pop($context_id{$type}) if $context_id{$type}; |
11 |
|
|
$self->{status}->push($context_id{$type} ||= $self->{status}->get_context_id($type), $text) if $text; |
12 |
|
|
} |
13 |
|
|
|
14 |
|
|
sub new { |
15 |
|
|
my $self = shift; |
16 |
|
|
$self = $self->SUPER::new(@_); |
17 |
|
|
|
18 |
|
|
$appwin::self = $self; # singleton |
19 |
|
|
|
20 |
|
|
$self->{conn} = new KGS::Protocol; |
21 |
|
|
|
22 |
|
|
KGS::Listener::Debug->new->listen($self->{conn}, "any"); #d# debug only :) |
23 |
|
|
|
24 |
|
|
$self->listen($self->{conn}, "login"); |
25 |
|
|
|
26 |
|
|
$self->{roomlist} = new roomlist conn => $self->{conn}; |
27 |
|
|
|
28 |
|
|
$self->{window} = new Gtk2::Window 'toplevel'; |
29 |
|
|
$self->{window}->set_title('kgsueme'); |
30 |
|
|
gtk::state $self->{window}, "main::window", undef, window_size => [400, 100]; |
31 |
|
|
$self->{window}->signal_connect(delete_event => sub { main_quit Gtk2; 1 }); |
32 |
|
|
|
33 |
|
|
$self->{window}->add(my $vbox = new Gtk2::VBox); |
34 |
|
|
|
35 |
|
|
$vbox->pack_start(($buttonbox = new Gtk2::HButtonBox), 0, 1, 0); |
36 |
|
|
$buttonbox->set_spacing(0); |
37 |
|
|
|
38 |
|
|
my $button = sub { |
39 |
|
|
$buttonbox->add(my $button = new Gtk2::Button $_[0]); |
40 |
|
|
signal_connect $button clicked => $_[1]; |
41 |
|
|
}; |
42 |
|
|
|
43 |
|
|
$button->("Login", sub { $self->login; }); |
44 |
|
|
$button->("Roomlist", sub { $self->{roomlist}->show; }); |
45 |
pcg |
1.2 |
$button->("Save Config & Layout", \&util::save_config); |
46 |
pcg |
1.1 |
$button->("Quit", sub { main_quit Gtk2 }); |
47 |
|
|
|
48 |
|
|
$vbox->pack_start((my $hbox = new Gtk2::HBox), 0, 1, 0); |
49 |
|
|
|
50 |
|
|
$hbox->add(new Gtk2::Label "Login"); |
51 |
|
|
|
52 |
|
|
$hbox->add($self->{login} = new_with_max_length Gtk2::Entry 12); |
53 |
|
|
$self->{login}->set_text($::config->{login}); |
54 |
|
|
|
55 |
|
|
if ($::HACK) { |
56 |
|
|
$self->{login}->signal_connect(activate => sub { |
57 |
|
|
$self->{conn}{name} = $self->{login}->get_text; |
58 |
|
|
}); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
$hbox->add(new Gtk2::Label "Password"); |
62 |
|
|
$hbox->add($self->{password} = new Gtk2::Entry); |
63 |
|
|
$self->{password}->set_visibility(0); |
64 |
|
|
|
65 |
|
|
$vbox->pack_start(($self->{status} = new Gtk2::Statusbar), 0, 1, 0); |
66 |
|
|
|
67 |
|
|
$self->{window}->show_all; |
68 |
|
|
|
69 |
|
|
$self; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
sub login { |
73 |
|
|
my ($self) = @_; |
74 |
|
|
|
75 |
|
|
$self->{conn}->disconnect; |
76 |
|
|
|
77 |
|
|
# initialize new socket and connection |
78 |
|
|
my $sock = new IO::Socket::INET PeerHost => "kgs.kiseido.com", PeerPort => "2379" |
79 |
|
|
or die; |
80 |
|
|
|
81 |
|
|
$sock->blocking(1); |
82 |
|
|
$self->{conn}->handshake($sock); |
83 |
|
|
$sock->blocking(0); |
84 |
|
|
|
85 |
|
|
my $input; $input = add_watch Glib::IO fileno $sock, [G_IO_IN, G_IO_ERR, G_IO_HUP], sub { |
86 |
|
|
# this is dorked |
87 |
|
|
my $buf; |
88 |
|
|
if (0 >= sysread $sock, $buf, 16384 |
89 |
|
|
and !$!{EINTR} and !$!{EAGAIN}) { |
90 |
|
|
remove Glib::Source $input; |
91 |
|
|
$self->event_disconnect; |
92 |
|
|
} |
93 |
|
|
$self->{conn}->feed_data($buf); |
94 |
|
|
1; |
95 |
|
|
}, G_PRIORITY_HIGH; |
96 |
|
|
|
97 |
|
|
# now login |
98 |
|
|
$self->{conn}->login("kgsueme $VERSION $^O", $self->{login}->get_text, $self->{password}->get_text); |
99 |
|
|
} |
100 |
|
|
|
101 |
|
|
sub inject_login { |
102 |
|
|
my ($self, $msg) = @_; |
103 |
|
|
|
104 |
|
|
appwin::status("login", "logged in as '$self->{conn}{name}' with status '$msg->{result}'"); |
105 |
|
|
$::config->{login} = $self->{conn}{name}; |
106 |
|
|
|
107 |
|
|
if ($msg->{success}) { |
108 |
|
|
for (keys %{$::config->{rooms}}) { |
109 |
|
|
$self->{roomlist}->join_room($_); |
110 |
|
|
} |
111 |
|
|
sound::play 3, "connect"; |
112 |
|
|
} elsif ($msg->{result} eq "user unknown") { |
113 |
|
|
sound::play 2, "user_unknown"; |
114 |
|
|
} else { |
115 |
|
|
sound::play 2, "warning"; |
116 |
|
|
} |
117 |
|
|
} |
118 |
|
|
|
119 |
|
|
sub event_disconnect { } |
120 |
|
|
|
121 |
|
|
1; |
122 |
|
|
|