1#############################################################################
2#
3#  This program is free software; you can redistribute it and/or modify
4#  it under the terms of the GNU General Public License as published by
5#  the Free Software Foundation; either version 2 of the License, or
6#  (at your option) any later version.
7#
8#  This program is distributed in the hope that it will be useful,
9#  but WITHOUT ANY WARRANTY; without even the implied warranty of
10#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11#  GNU General Public License for more details.
12#
13#  You should have received a copy of the GNU General Public License
14#  along with this program; if not, write to the Free Software
15#  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
16#
17#  Jabber
18#  Copyright (C) 1998-1999 The Jabber Team http://jabber.org/
19#
20##############################################################################
21
22package CLI::Roster;
23
24use strict;
25use vars qw($VERSION);
26
27use base qw(Jarl::Roster);
28
29
30$VERSION = "0.1";
31
32sub new {
33  my $proto = shift;
34  my $self = { };
35
36  $self->{ROSTER} = {};
37
38  bless($self, $proto);
39
40  my (%args) = @_;
41
42  $self->{varsOnlineOnly} = 0;
43
44  $self->{varsUpdateMode} =
45    $self->ParseArg(\%args,"-updatemode","roster");
46
47  $self->DrawMode(1);
48
49  return $self;
50}
51
52
53##############################################################################
54#
55# ParseArg - Helper function to extract the specified argument from the
56#            function call.
57#
58##############################################################################
59sub ParseArg {
60  my $self = shift;
61  my ($args,$arg,$default) = @_;
62  return (exists($args->{$arg}) ? delete($args->{$arg}) : $default);
63}
64
65
66sub Draw {
67  my $self = shift;
68
69  return unless ($self->DrawMode() == 1);
70
71  $main::TabBar->Print("="x($main::GUI{maxX}-1),"\n");
72  $main::TabBar->Print("Roster:\n");
73
74  foreach my $group ($self->GetGroups()) {
75    next if ($group eq '__roster__:none');
76    $self->DrawItem($self->CreateTag($group),"group",$group,0)
77      unless (($self->{varsOnlineOnly} == 1) &&
78              ($self->ActiveGroup($group) == 0));
79
80    if ($self->{groups}->{$group}->{'__roster__:status'} == 1) {
81      foreach my $jid (sort {uc($self->{jids}->{$a}->{name}) cmp uc($self->{jids}->{$b}->{name})} keys(%{$self->{groups}->{$group}})) {
82	next if ($jid eq '__roster__:status');
83	next if (($self->{varsOnlineOnly} == 1) && !defined($self->Resource($jid)));
84	$self->DrawItem($self->CreateTag($group,$jid),"jid",$self->{jids}->{$jid}->{name},1);
85	if ($self->{groups}->{$group}->{$jid} == 1) {
86	  foreach my $resource (sort {$self->{resources}->{$jid}->{$b}->{priority} <=> $self->{resources}->{$jid}->{$a}->{priority}} keys(%{$self->{resources}->{$jid}})) {
87	    $self->DrawItem($self->CreateTag($group,$jid,$resource),"resource",$resource,2);
88	  }
89	}
90      }
91    }
92  }
93
94  foreach my $jid (sort {uc($self->{jids}->{$a}->{name}) cmp uc($self->{jids}->{$b}->{name})} keys(%{$self->{groups}->{'__roster__:none'}})) {
95    next if ($jid eq '__roster__:status');
96    next if (($self->{varsOnlineOnly} == 1) && !defined($self->Resource($jid)));
97    $self->DrawItem($self->CreateTag('__roster__:none',$jid),"jid",$self->{jids}->{$jid}->{name},0);
98    if ($self->{jids}->{$jid}->{status} == 1) {
99      foreach my $resource (sort {$self->{resources}->{$jid}->{$b}->{priority} <=> $self->{resources}->{$jid}->{$a}->{priority}} keys(%{$self->{resources}->{$jid}})) {
100	$self->DrawItem($self->CreateTag('__roster__:none',$jid,$resource),"resource",$resource,1);
101      }
102    }
103  }
104
105  if (!defined($self->{varsSelectedTag})) {
106    $self->{varsSelectedJID} = undef;
107    $self->{varsSelectedTag} = undef;
108  }
109
110  $main::TabBar->Print("="x($main::GUI{maxX}-1),"\n");
111}
112
113
114sub DrawItem {
115  my $self = shift;
116  my ($tag,$type,$text,$indentLevel) = @_;
117
118  my ($group) = ($tag =~ /group-([^(:::)]+)/);
119  $group =~ s/\&bar;/\|/g;
120  my ($jid) = ($tag =~ /jid-([^(:::)]+)/);
121  $jid =~ s/\&bar;/\|/g;
122  my ($resource) = ($tag =~ /res-([^(:::)]+)/);
123  $resource =~ s/\&bar;/\|/g;
124
125  my $icon;
126
127  if ($type eq "group") {
128    $icon = ($self->{groups}->{$group}->{'__roster__:status'} == 1) ? "-" : "+";
129  }
130
131  if ($type eq "resource") {
132    $icon = "%Res";
133  }
134
135  if ($type eq "jid") {
136
137    my $resource = $self->Resource($jid);
138
139    if (defined($resource)) {
140      my $presence =
141        (($self->{resources}->{$jid}->{$resource}->{show} ne "") ?
142	 $self->{resources}->{$jid}->{$resource}->{show} :
143	 $self->{resources}->{$jid}->{$resource}->{type}
144	);
145
146      $icon = &main::jarlRosterIF_PresenceIcon($self,$presence,$jid);
147    } else {
148      $icon = &main::jarlRosterIF_PresenceIcon($self,"unavailable",$jid);
149    }
150  }
151
152  my $balloon = "";
153  $balloon = " - ".${$self->{balloons}->{$tag}}
154    if exists($self->{balloons}->{$tag});
155  $main::TabBar->Print(sprintf("  %s %s\n",$icon,$text)) if ($type eq "group");
156  $main::TabBar->Print(sprintf("  %s%-6s %s\n","       "x$indentLevel,$icon,$text.$balloon)) if ($type ne "group");
157}
158
159
160##############################################################################
161#
162# RegisterGroup - stub for children to have code for when a group is added.
163#
164##############################################################################
165sub RegisterGroup {
166  my $self = shift;
167  my ($group) = @_;
168}
169
170
171##############################################################################
172#
173# RegisterJID - stub for children to have code for when a JID is added.
174#
175##############################################################################
176sub RegisterJID {
177  my $self = shift;
178  my ($group,$jid,$resource) = @_;
179
180  my $tag = $self->CreateTag($group,$jid,$resource);
181
182  $self->RegisterBalloon($tag,\$self->{jids}->{$jid}->{balloon});
183}
184
185
1861;
187