ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/Net-XMPP2/samples/devcl/DevCL/Main.pm
Revision: 1.1
Committed: Wed Jul 11 13:34:55 2007 UTC (19 years, 2 months ago) by elmex
Branch: MAIN
Log Message:
added development client example

File Contents

# User Rev Content
1 elmex 1.1 package DevCL::Main;
2     use strict;
3     use Gtk2;
4     use Net::XMPP2::Util qw/dump_twig_xml/;
5     use POSIX qw/strftime/;
6     use DevCL::Browser;
7    
8     sub new {
9     my $this = shift;
10     my $class = ref($this) || $this;
11     my $self = { @_ };
12     bless $self, $class
13     }
14    
15     sub start {
16     my ($self) = @_;
17    
18     my $w = Gtk2::Window->new ('toplevel');
19     $w->add (my $vb = Gtk2::VBox->new);
20     $w->set_default_size (500, 600);
21    
22     $vb->pack_start (my $menu = Gtk2::MenuBar->new, 0, 1, 0);
23     $self->_populate_menu ($menu);
24     $vb->pack_start (my $vp = Gtk2::VPaned->new, 1, 1, 0);
25     $vp->add1 (my $sw = $self->{log_recv_sb} = Gtk2::ScrolledWindow->new);
26     $sw->set_policy ('automatic', 'automatic');
27     $sw->add (my $log = $self->{log_recv} = Gtk2::TextView->new);
28     $log->set_wrap_mode ('word');
29     $vp->add2 (my $sw2 = $self->{log_send_sb} = Gtk2::ScrolledWindow->new);
30     $sw2->set_policy ('automatic', 'automatic');
31     $sw2->add (my $log2 = $self->{log_send} = Gtk2::TextView->new);
32     $log2->set_wrap_mode ('word');
33     $vb->pack_start (my $sb = $self->{sb} = Gtk2::Statusbar->new, 0, 1, 0);
34    
35     _prep_text_view ($log);
36     _prep_text_view ($log2);
37    
38     $w->signal_connect (destroy => sub { ::end () });
39    
40     $self->attach;
41     $w->show_all;
42     }
43    
44     sub _populate_menu {
45     my ($self, $menu) = @_;
46     $menu->append (my $browser = Gtk2::MenuItem->new ('Browser'));
47     $browser->signal_connect (activate => sub {
48     $self->start_browser;
49     });
50     }
51    
52     sub _ts {
53     strftime ("%T %F %z", localtime (time))
54     }
55     sub _append_text_view {
56     my ($tv, $txt) = @_;
57     my $buf = $tv->get_buffer;
58     $buf->insert ($buf->get_end_iter, $txt);
59     }
60    
61     sub _prep_text_view {
62     my ($tv) = @_;
63     my $b = $tv->get_buffer ();
64     my $em = $b->create_mark ('end', $b->get_end_iter, 0);
65     $b->signal_connect (insert_text => sub {
66     $tv->scroll_to_mark ($em, 0, 1, 0, 1);
67     });
68     }
69    
70     sub attach {
71     my ($self) = @_;
72    
73     $::CLIENT->reg_cb (
74     debug_recv => sub {
75     my ($cl, $acc, $data) = @_;
76     _append_text_view ($self->{log_recv}, _ts . ":\n");
77     _append_text_view ($self->{log_recv}, dump_twig_xml ($data));
78     1
79     },
80     debug_send => sub {
81     my ($cl, $acc, $data) = @_;
82     _append_text_view ($self->{log_send}, _ts . ":\n");
83     _append_text_view ($self->{log_send}, dump_twig_xml ($data));
84     1
85     },
86     );
87     }
88    
89     sub start_browser {
90     my ($self) = @_;
91     return if $self->{browser};
92    
93     $self->{browser} = DevCL::Browser->new (
94     on_destroy => sub { delete $self->{browser} },
95     );
96    
97     $self->{browser}->start;
98     }
99    
100     1