/* Monolith discussion panel 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: ml_discussion_panel.c,v 1.4 2003/02/22 15:34:29 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 #include #include #include "ml_discussion.h" #include "ml_discussion_panel.h" static void repaint (void *, ml_session, const char *, io_handle); struct ml_widget_operations discussion_panel_ops = { repaint: repaint }; struct ml_discussion_panel { struct ml_widget_operations *ops; pool pool; /* Pool for allocations. */ ml_session session; /* Current session. */ const char *conninfo; /* Database connection. */ ml_select_layout w; /* The actual select layout. */ }; static void do_refresh (ml_session, void *vw); ml_discussion_panel new_ml_discussion_panel (pool pool, ml_session session, const char *conninfo) { ml_discussion_panel w = pmalloc (pool, sizeof *w); ml_multicol_layout tbl; ml_button refresh_button; w->ops = &discussion_panel_ops; w->pool = pool; w->session = session; w->conninfo = conninfo; /* Create the layout. */ w->w = new_ml_select_layout (pool, session); ml_widget_set_property (w->w, "class", "ml_discussion_panel"); /* Create the action buttons. */ tbl = new_ml_multicol_layout (pool, 1); ml_widget_set_property (tbl, "class", "ml_discussion_panel_actions"); refresh_button = new_ml_button (pool, "Refresh"); ml_button_set_callback (refresh_button, do_refresh, session, w); ml_multicol_layout_pack (tbl, refresh_button); /* Put the action buttons into the layout. */ ml_widget_set_property (w->w, "select_layout.top", tbl); /* Refresh selected newsgroups. */ do_refresh (session, w); return w; } /* XXX When called from new_ml_discussion_panel, this is OK, but when called * from the button, it's a bit brutal, because it creates new copies of * every widget. This is not necessary, and we could definitely be more * clever here. */ static void do_refresh (ml_session session, void *vw) { ml_discussion_panel w = (ml_discussion_panel) vw; db_handle dbh; st_handle sth; int resid, unread; const char *resname; ml_select_layout_clear (w->w); /* Pull out the list of newsgroups. */ /* XXX Access control. */ dbh = get_db_handle (w->conninfo, DBI_THROW_ERRORS); sth = st_prepare_cached (dbh, "select g.resid, r.name, ml_discussion_unread (?, g.resid) " " from ml_discussion_group g, ml_resources r " " where g.resid = r.resid " " order by 2", DBI_INT); st_execute (sth, ml_session_userid (session)); st_bind (sth, 0, resid, DBI_INT); st_bind (sth, 1, resname, DBI_STRING); st_bind (sth, 2, unread, DBI_INT); while (st_fetch (sth)) { char *name; ml_widget ng; /* Create the name of this group. */ name = psprintf (w->pool, "%s (%d)", resname, unread); /* Create the newsgroup widget. */ /* XXX Avoid the extra DB query somehow? */ ng = new_ml_discussion (w->pool, session, w->conninfo, resname); /* Pack into the layout. */ ml_select_layout_pack (w->w, name, ng); } put_db_handle (dbh); } static void repaint (void *vw, ml_session session, const char *windowid, io_handle io) { ml_discussion_panel w = (ml_discussion_panel) vw; if (w->w) ml_widget_repaint (w->w, session, windowid, io); }