summaryrefslogtreecommitdiff
path: root/src/chat/gcol.cpp
blob: 099f11e7944101f456b316c8d04ce082d5e0f06b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*:*
 *: File: ./src/chat/gcol.cpp
 *: 
 *: yChat; Homepage: www.yChat.org; Version 0.7.9.5-RELEASE
 *: 
 *: Copyright (C) 2003 Paul C. Buetow, Volker Richter
 *: Copyright (C) 2004 Paul C. Buetow
 *: Copyright (C) 2005 EXA Digital Solutions GbR
 *: 
 *: This program is free software; you can redistribute it and/or
 *: modify it under the terms of the GNU General Public License
 *: as published by the Free Software Foundation; either version 2
 *: of the License, or (at your option) any later version.
 *: 
 *: This program is distributed in the hope that it will be useful,
 *: but WITHOUT ANY WARRANTY; without even the implied warranty of
 *: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *: GNU General Public License for more details.
 *: 
 *: You should have received a copy of the GNU General Public License
 *: along with this program; if not, write to the Free Software
 *: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *:*/

#ifndef GCOL_CPP
#define GCOL_CPP

#include "gcol.h"

using namespace std;

gcol::gcol()
{
  pthread_mutex_init( &mut_vec_rooms  , NULL);

  p_map_users = new shashmap<user*>;
  wrap::system_message( GARBAGE );

#ifdef NCURSES

  print_garbage();
#endif
}

gcol::~gcol()
{
  remove_garbage();
  pthread_mutex_destroy( &mut_vec_rooms );
  delete p_map_users;
}

void
gcol::add_room_to_garbage( room* p_room )
{
  pthread_mutex_lock  ( &mut_vec_rooms );
  vec_rooms.push_back( p_room );
  pthread_mutex_unlock( &mut_vec_rooms );
  wrap::system_message( GARROOM + p_room->get_name() );
#ifdef NCURSES

  print_garbage();
#endif
}

void
gcol::add_user_to_garbage( user* p_user )
{
  p_user->s_mess_delete();
  p_map_users->add_elem( p_user, tool::to_lower(p_user->get_name()) );
  wrap::system_message( GARUSER + p_user->get_name() );
  p_user->destroy_session();

#ifdef NCURSES

  print_garbage();
#endif
}

bool
gcol::remove_garbage()
{
  bool b_empty;

  pthread_mutex_lock  ( &mut_vec_rooms );
  b_empty  = ( vec_rooms.empty() && p_map_users->size() == 0);
  pthread_mutex_unlock( &mut_vec_rooms );

  if ( b_empty )
    return false;

  wrap::system_message( GARBACT );

  pthread_mutex_lock  ( &mut_vec_rooms );
  for ( vector<room*>::iterator iter = vec_rooms.begin();
        iter != vec_rooms.end(); iter++ )
  {
    wrap::system_message( REMROOM + (*iter)->get_name() );
    delete *iter;
  }
  vec_rooms.clear();
  pthread_mutex_unlock( &mut_vec_rooms );


  p_map_users->run_func( delete_users_ );
  p_map_users->clear();

#ifdef NCURSES

  print_garbage();
#endif

  return true;
}

room*
gcol::get_room_from_garbage()
{
  pthread_mutex_lock  ( &mut_vec_rooms );

  if ( vec_rooms.empty() )
  {
    pthread_mutex_unlock( &mut_vec_rooms );
    return NULL;
  }


  room* p_room = vec_rooms.back();
  vec_rooms.pop_back();
  pthread_mutex_unlock( &mut_vec_rooms );

#ifdef NCURSES

  print_garbage();
#endif

  return p_room;
}

room*
gcol::get_room_from_garbage_or_new( string s_room )
{
  room* p_room = get_room_from_garbage();
  if ( p_room == NULL )
    return new room( s_room );
  p_room->set_name( s_room );
  return p_room;
}

user*
gcol::get_user_from_garbage( string s_user )
{

  user* p_user = p_map_users->get_elem( tool::to_lower(s_user) );
  ;

  if ( p_user != NULL )
  {
    p_map_users->del_elem( tool::to_lower(s_user) );
    p_user->set_name( s_user );
    p_user->set_online( true );
    p_user->set_invisible( false );
    p_user->renew_timeout();
    wrap::system_message(GARUSE2 + p_user->get_name() );
#ifdef NCURSES

    print_garbage();
#endif

  }

  return p_user;
}

void
gcol::delete_users_( user *user_obj )
{
  wrap::system_message( REMUSER + user_obj->get_name() );
  user_obj->clean();
  delete user_obj;
}

void
gcol::lock_mutex()
{
  pthread_mutex_lock  ( &mut_vec_rooms );
}

void
gcol::unlock_mutex()
{
  pthread_mutex_unlock ( &mut_vec_rooms );
}

#ifdef NCURSES
void
gcol::print_garbage( )
{
  if ( wrap::NCUR->is_ready() )
  {
    pthread_mutex_lock  ( &mut_vec_rooms );
    mvprintw( NCUR_GARBAGE_X,NCUR_GARBAGE_Y, "Garbage: %d ", p_map_users->size() + vec_rooms.size() );
    pthread_mutex_unlock( &mut_vec_rooms );
    refresh();
  }
}

#endif

#endif