/* Monolith chat users pane widget. * - by Richard W.M. Jones * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Library General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library 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 * Library General Public License for more details. * * You should have received a copy of the GNU Library General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * * $Id: users_pane.c,v 1.6 2003/02/22 15:34:28 rich Exp $ */ #include "config.h" #include #include #ifdef HAVE_ASSERT_H #include #endif #ifdef HAVE_STRING_H #include #endif #include #include #include #include #include #include #include "chatroom.h" #include "lib.h" #include "users_pane.h" static void repaint (void *, ml_session, const char *, io_handle); struct ml_widget_operations users_pane_ops = { repaint: repaint }; struct users_pane { struct ml_widget_operations *ops; pool pool; /* Pool for allocations. */ ml_session session; /* Current session. */ const char *conninfo; /* Database connection. */ chatroom room; /* The chatroom. */ int ie_bug_workaround; /* Bug workaround for IE. */ }; users_pane new_users_pane (pool pool, ml_session session, const char *conninfo, chatroom room, int ie_bug_workaround) { users_pane w = pmalloc (pool, sizeof *w); w->ops = &users_pane_ops; w->pool = pool; w->session = session; w->conninfo = conninfo; w->room = room; w->ie_bug_workaround = ie_bug_workaround; return w; } static inline void get_users (chatroom room, pool thread_pool, int *nr_anon_rtn, vector *userids_rtn) { hash users = chatroom_users (room); const int zero = 0; int count; /* Get the number of anonymous users. */ if (hash_get (users, zero, count)) *nr_anon_rtn = count; else *nr_anon_rtn = 0; /* Get the list of other users. */ *userids_rtn = hash_keys_in_pool (users, thread_pool); } static void repaint (void *vw, ml_session session, const char *windowid, io_handle io) { users_pane w = (users_pane) vw; pool thread_pool = pth_get_pool (current_pth); int nr_anon; vector userids; int userid; char *username; db_handle dbh; st_handle sth; /* Get the current list of users. */ get_users (w->room, thread_pool, &nr_anon, &userids); /* Resolve userids to usernames and display. */ if (vector_size (userids) > 0) { dbh = get_db_handle (w->conninfo, DBI_THROW_ERRORS); sth = st_prepare_cached (dbh, "select userid, username from ml_users where userid in (@) " "order by 2", DBI_VECTOR_INT); st_execute (sth, userids); st_bind (sth, 0, userid, DBI_INT); st_bind (sth, 1, username, DBI_STRING); while (st_fetch (sth)) { io_fputs ("", io); ml_plaintext_print (io, username); io_fputs ("
", io); } put_db_handle (dbh); } /* Display number of anonymous users. */ io_fprintf (io, "%d anonymous", nr_anon); if (! w->ie_bug_workaround) { chat_fill_buffer (io, session, w->conninfo); /* Wait for a change. */ chatroom_wait_enter_leave_event (w->room, session); /* Send some javascript to force the browser to reload. */ io_fputs ("\n", io); } }