1 |
package DC::MessageDistributor; |
2 |
use strict; |
3 |
no warnings; |
4 |
|
5 |
sub new { |
6 |
my $this = shift; |
7 |
my $class = ref($this) || $this; |
8 |
my $self = { @_ }; |
9 |
bless $self, $class; |
10 |
|
11 |
$self->{dockbar}->add_dock ( |
12 |
$self->{log} = DC::UI::ChatView->new ( |
13 |
expand => 1, |
14 |
can_close => 0, |
15 |
can_undock => 0, |
16 |
info => { |
17 |
id => "", |
18 |
title => "Log", |
19 |
tooltip => "<b>Server Log</b>. This text viewer contains all recent message sent by the server.", |
20 |
entry_tooltip => "<b>Command Entry</b>. If you enter something and press return/enter here, " |
21 |
. "the line you entered will be sent to the server as a command.", |
22 |
reply => '' |
23 |
} |
24 |
) |
25 |
); |
26 |
|
27 |
$self->{dockbar}->select_dockable ($self->{log}); |
28 |
|
29 |
return $self |
30 |
} |
31 |
|
32 |
# called by MAPWIDGET activate console event |
33 |
sub activate_console { |
34 |
# nop |
35 |
} |
36 |
|
37 |
# adding channel |
38 |
sub add_channel { |
39 |
my ($self, $chaninfo) = @_; |
40 |
|
41 |
$self->{info}->{$chaninfo->{id}} = $chaninfo; |
42 |
$self->touch_channel ($chaninfo->{id}); |
43 |
} |
44 |
|
45 |
# set max paragraphs |
46 |
sub set_max_par { |
47 |
my ($self, $par) = @_; |
48 |
for ($self->{log}, values %{$self->{chatview}}) { |
49 |
$_->set_max_par ($par); |
50 |
} |
51 |
} |
52 |
|
53 |
# set fontsize for all chatviews |
54 |
sub set_fontsize { |
55 |
my ($self, $s) = @_; |
56 |
|
57 |
for ($self->{log}, values %{$self->{chatview}}) { |
58 |
$_->set_fontsize ($s); |
59 |
} |
60 |
} |
61 |
|
62 |
# push message in |
63 |
sub message { |
64 |
my ($self, $para) = @_; |
65 |
my $id = $para->{type}; |
66 |
|
67 |
if (exists $self->{info}->{$id}) { |
68 |
unless (exists $self->{chatview}->{$id}) { |
69 |
$self->touch_channel ($id); |
70 |
} |
71 |
|
72 |
my $cv = $self->{chatview}->{$id}; |
73 |
|
74 |
unless ($cv) { |
75 |
warn "message couldn't be delivered to chatview with " |
76 |
."id '$id', sending it to main log."; |
77 |
$self->{log}->message ($para); |
78 |
return; |
79 |
} |
80 |
|
81 |
$cv->message ($para); |
82 |
|
83 |
} else { |
84 |
$self->{log}->message ($para); |
85 |
} |
86 |
} |
87 |
|
88 |
sub touch_channel { |
89 |
my ($self, $id) = @_; |
90 |
|
91 |
if (exists $self->{chatview}->{$id}) { |
92 |
$self->update_chat ($id); |
93 |
} else { |
94 |
$self->init_chat ($id); |
95 |
} |
96 |
} |
97 |
|
98 |
sub update_chat { |
99 |
my ($self, $id) = @_; |
100 |
|
101 |
$self->{chatview}->{$id}->update_info ($self->{info}->{$id}); |
102 |
} |
103 |
|
104 |
sub init_chat { |
105 |
my ($self, $id) = @_; |
106 |
|
107 |
my $chaninfo = $self->{info}->{$id}; |
108 |
my $dock = $self->{chatview}->{$id} = |
109 |
DC::UI::ChatView->new ( |
110 |
expand => 1, |
111 |
info => $chaninfo, |
112 |
); |
113 |
$dock->connect (close_dock => sub { |
114 |
delete $self->{chatview}->{$id}; |
115 |
0 |
116 |
}); |
117 |
$self->{dockbar}->add_dock ($dock); |
118 |
} |
119 |
|
120 |
1; |