1 /* Monolith user directory widget.
2 * - by Richard W.M. Jones <rich@annexia.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Library General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Library General Public License for more details.
14 * You should have received a copy of the GNU Library General Public
15 * License along with this library; if not, write to the Free
16 * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 * $Id: ml_user_directory.c,v 1.2 2003/02/22 15:34:33 rich Exp $
33 #include <pthr_iolib.h>
36 #include "ml_widget.h"
37 #include "ml_button.h"
38 #include "ml_window.h"
40 #include "ml_form_select.h"
41 #include "ml_form_submit.h"
42 #include "ml_table_layout.h"
43 #include "ml_user_directory.h"
45 static void repaint (void *, ml_session, const char *, io_handle);
47 struct ml_widget_operations user_directory_ops =
52 struct ml_user_directory
54 struct ml_widget_operations *ops;
55 pool pool; /* Pool for allocations. */
56 ml_session session; /* Current session. */
57 const char *conninfo; /* Database connection. */
58 ml_form form; /* Form (can be NULL). */
59 int userid; /* Currently selected userid (0 = none). */
60 ml_widget top; /* This is our top-level widget. */
61 ml_form_select select; /* Selection widget. */
62 vector users; /* List of all users (vector of int). */
65 static void make_select (pool, ml_session, const char *,
67 ml_form_select *select_rtn,
71 new_ml_user_directory (pool pool, ml_session session, const char *conninfo,
72 ml_form form, int userid)
74 ml_user_directory w = pmalloc (pool, sizeof *w);
76 w->ops = &user_directory_ops;
79 w->conninfo = conninfo;
82 /* Create the table widget. */
83 if (form) /* We are in an existing form. */
86 make_select (pool, session, conninfo, form, userid,
87 &w->select, &w->users);
90 else /* We are a standalone widget. */
93 ml_form_submit submit;
95 w->form = new_ml_form (w->pool);
96 tbl = new_ml_table_layout (pool, 1, 2);
97 make_select (pool, session, conninfo, w->form, userid,
98 &w->select, &w->users);
99 ml_table_layout_pack (tbl, w->select, 0, 0);
100 submit = new_ml_form_submit (pool, w->form, "Go");
101 ml_table_layout_pack (tbl, submit, 0, 1);
102 ml_form_pack (w->form, tbl);
111 make_select (pool pool, ml_session session, const char *conninfo,
112 ml_form form, int selected_userid,
113 ml_form_select *select, vector *users)
118 const char *email, *given_name, *family_name;
120 *select = new_ml_form_select (pool, form);
121 *users = new_vector (pool, int);
123 dbh = get_db_handle (conninfo, DBI_THROW_ERRORS);
125 /* Pull out the list of users, and details. */
126 sth = st_prepare_cached
128 "select p.userid, u.email, u.given_name, u.family_name "
129 "from ml_userdir_prefs p, ml_users u "
130 "order by u.given_name, u.family_name, u.email");
133 st_bind (sth, 0, userid, DBI_INT);
134 st_bind (sth, 1, email, DBI_STRING);
135 st_bind (sth, 2, given_name, DBI_STRING);
136 st_bind (sth, 3, family_name, DBI_STRING);
138 while (st_fetch (sth))
140 vector_push_back (*users, userid);
141 ml_form_select_push_back
143 psprintf (pool, "%s %s <%s>", given_name, family_name, email));
145 if (userid == selected_userid)
146 ml_form_select_set_selection (*select,
147 ml_form_select_size (*select) - 1);
154 repaint (void *vw, ml_session session, const char *windowid, io_handle io)
156 ml_user_directory w = (ml_user_directory) vw;
158 ml_widget_repaint (w->top, session, windowid, io);