using button compatibility macros
[obnox/wireshark/wip.git] / gtk / ldap_stat.c
1 /* ldap_stat.c
2  * ldap_stat   2003 Ronnie Sahlberg
3  *
4  * $Id: ldap_stat.c,v 1.6 2004/01/10 16:27:42 ulfl Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  *
10  * This program is free software; you can redistribute it and/or
11  * modify it under the terms of the GNU General Public License
12  * as published by the Free Software Foundation; either version 2
13  * of the License, or (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <stdio.h>
30
31 #ifdef HAVE_SYS_TYPES_H
32 # include <sys/types.h>
33 #endif
34
35 #include <gtk/gtk.h>
36 #include <string.h>
37 #include "../epan/packet_info.h"
38 #include "../epan/epan.h"
39 #include "menu.h"
40 #include "../tap.h"
41 #include "../epan/value_string.h"
42 #include "../packet-ldap.h"
43 #include "../register.h"
44 #include "../timestats.h"
45 #include "compat_macros.h"
46 #include "../simple_dialog.h"
47 #include "dlg_utils.h"
48 #include "../file.h"
49 #include "../globals.h"
50 #include "filter_prefs.h"
51 #include "service_response_time_table.h"
52
53 extern GtkWidget   *main_display_filter_widget;
54
55 /* used to keep track of the statistics for an entire program interface */
56 typedef struct _ldapstat_t {
57         GtkWidget *win;
58         srt_stat_table ldap_srt_table;
59 } ldapstat_t;
60
61 static void
62 ldapstat_set_title(ldapstat_t *ldap)
63 {
64         char            *title;
65
66         title = g_strdup_printf("LDAP Service Response Time statistics: %s",
67             cf_get_display_name(&cfile));
68         gtk_window_set_title(GTK_WINDOW(ldap->win), title);
69         g_free(title);
70 }
71
72 static void
73 ldapstat_reset(void *pldap)
74 {
75         ldapstat_t *ldap=(ldapstat_t *)pldap;
76
77         reset_srt_table_data(&ldap->ldap_srt_table);
78         ldapstat_set_title(ldap);
79 }
80
81 static int
82 ldapstat_packet(void *pldap, packet_info *pinfo, epan_dissect_t *edt _U_, void *psi)
83 {
84         ldap_call_response_t *ldap=(ldap_call_response_t *)psi;
85         ldapstat_t *fs=(ldapstat_t *)pldap;
86
87         /* we are only interested in reply packets */
88         if(ldap->is_request){
89                 return 0;
90         }
91         /* if we havnt seen the request, just ignore it */
92         if(!ldap->req_frame){ 
93                 return 0;
94         }
95
96         /* only use the commands we know how to handle */
97         switch(ldap->protocolOpTag){
98         case LDAP_REQ_BIND:
99         case LDAP_REQ_SEARCH:
100         case LDAP_REQ_MODIFY:
101         case LDAP_REQ_ADD:
102         case LDAP_REQ_DELETE:
103         case LDAP_REQ_MODRDN:
104         case LDAP_REQ_COMPARE:
105         case LDAP_REQ_EXTENDED:
106                 break;
107         default:
108                 return 0;
109         }
110
111         add_srt_table_data(&fs->ldap_srt_table, ldap->protocolOpTag, &ldap->req_time, pinfo);
112
113         return 1;
114 }
115
116
117
118 static void
119 ldapstat_draw(void *pldap)
120 {
121         ldapstat_t *ldap=(ldapstat_t *)pldap;
122
123         draw_srt_table_data(&ldap->ldap_srt_table);
124 }
125
126
127 void protect_thread_critical_region(void);
128 void unprotect_thread_critical_region(void);
129 static void
130 win_destroy_cb(GtkWindow *win _U_, gpointer data)
131 {
132         ldapstat_t *ldap=(ldapstat_t *)data;
133
134         protect_thread_critical_region();
135         remove_tap_listener(ldap);
136         unprotect_thread_critical_region();
137
138         free_srt_table_data(&ldap->ldap_srt_table);
139         g_free(ldap);
140 }
141
142
143 static void
144 gtk_ldapstat_init(char *optarg)
145 {
146         ldapstat_t *ldap;
147         char *filter=NULL;
148         GtkWidget *label;
149         char filter_string[256];
150         GString *error_string;
151         GtkWidget *vbox;
152
153         if(!strncmp(optarg,"ldap,srt,",9)){
154                 filter=optarg+9;
155         } else {
156                 filter=NULL;
157         }
158
159         ldap=g_malloc(sizeof(ldapstat_t));
160
161         ldap->win=gtk_window_new(GTK_WINDOW_TOPLEVEL);
162         gtk_window_set_default_size(GTK_WINDOW(ldap->win), 550, 400);
163         ldapstat_set_title(ldap);
164         SIGNAL_CONNECT(ldap->win, "destroy", win_destroy_cb, ldap);
165
166         vbox=gtk_vbox_new(FALSE, 0);
167         gtk_container_add(GTK_CONTAINER(ldap->win), vbox);
168         gtk_container_set_border_width(GTK_CONTAINER(vbox), 10);
169         gtk_widget_show(vbox);
170
171         label=gtk_label_new("LDAP Service Response Time statistics");
172         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
173         gtk_widget_show(label);
174
175         snprintf(filter_string,255,"Filter:%s",filter?filter:"");
176         label=gtk_label_new(filter_string);
177         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
178         gtk_widget_show(label);
179
180
181         label=gtk_label_new("LDAP Commands");
182         gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
183         gtk_widget_show(label);
184
185         /* We must display TOP LEVEL Widget before calling init_srt_table() */
186         gtk_widget_show(ldap->win);
187
188         init_srt_table(&ldap->ldap_srt_table, 24, vbox, NULL);
189         init_srt_table_row(&ldap->ldap_srt_table, 0, "Bind");
190         init_srt_table_row(&ldap->ldap_srt_table, 1, "<unknown>");
191         init_srt_table_row(&ldap->ldap_srt_table, 2, "<unknown>");
192         init_srt_table_row(&ldap->ldap_srt_table, 3, "Search");
193         init_srt_table_row(&ldap->ldap_srt_table, 4, "<unknown>");
194         init_srt_table_row(&ldap->ldap_srt_table, 5, "<unknown>");
195         init_srt_table_row(&ldap->ldap_srt_table, 6, "Modify");
196         init_srt_table_row(&ldap->ldap_srt_table, 7, "<unknown>");
197         init_srt_table_row(&ldap->ldap_srt_table, 8, "Add");
198         init_srt_table_row(&ldap->ldap_srt_table, 9, "<unknown>");
199         init_srt_table_row(&ldap->ldap_srt_table, 10, "Delete");
200         init_srt_table_row(&ldap->ldap_srt_table, 11, "<unknown>");
201         init_srt_table_row(&ldap->ldap_srt_table, 12, "Modrdn");
202         init_srt_table_row(&ldap->ldap_srt_table, 13, "<unknown>");
203         init_srt_table_row(&ldap->ldap_srt_table, 14, "Compare");
204         init_srt_table_row(&ldap->ldap_srt_table, 15, "<unknown>");
205         init_srt_table_row(&ldap->ldap_srt_table, 16, "<unknown>");
206         init_srt_table_row(&ldap->ldap_srt_table, 17, "<unknown>");
207         init_srt_table_row(&ldap->ldap_srt_table, 18, "<unknown>");
208         init_srt_table_row(&ldap->ldap_srt_table, 19, "<unknown>");
209         init_srt_table_row(&ldap->ldap_srt_table, 20, "<unknown>");
210         init_srt_table_row(&ldap->ldap_srt_table, 21, "<unknown>");
211         init_srt_table_row(&ldap->ldap_srt_table, 22, "<unknown>");
212         init_srt_table_row(&ldap->ldap_srt_table, 23, "Extended");
213
214
215         error_string=register_tap_listener("ldap", ldap, filter, ldapstat_reset, ldapstat_packet, ldapstat_draw);
216         if(error_string){
217                 simple_dialog(ESD_TYPE_WARN, NULL, error_string->str);
218                 g_string_free(error_string, TRUE);
219                 g_free(ldap);
220                 return;
221         }
222
223         gtk_widget_show_all(ldap->win);
224         redissect_packets(&cfile);
225 }
226
227
228
229 static GtkWidget *dlg=NULL;
230 static GtkWidget *filter_entry;
231
232 static void
233 dlg_destroy_cb(void)
234 {
235         dlg=NULL;
236 }
237
238 static void
239 dlg_cancel_cb(GtkWidget *cancel_bt _U_, gpointer parent_w)
240 {
241         gtk_widget_destroy(GTK_WIDGET(parent_w));
242 }
243
244 static void
245 ldapstat_start_button_clicked(GtkWidget *item _U_, gpointer data _U_)
246 {
247         GString *str;
248         char *filter;
249
250         str = g_string_new("ldap,srt");
251         filter=(char *)gtk_entry_get_text(GTK_ENTRY(filter_entry));
252         if(filter[0]!=0){
253                 g_string_sprintfa(str,",%s", filter);
254         }
255         gtk_ldapstat_init(str->str);
256         g_string_free(str, TRUE);
257 }
258
259 static void
260 gtk_ldapstat_cb(GtkWidget *w _U_, gpointer d _U_)
261 {
262         GtkWidget *dlg_box;
263         GtkWidget *filter_box, *filter_bt;
264         GtkWidget *bbox, *start_button, *cancel_button;
265         const char *filter;
266         static construct_args_t args = {
267           "Service Response Time Statistics Filter",
268           TRUE,
269           FALSE
270         };
271
272         /* if the window is already open, bring it to front */
273         if(dlg){
274                 gdk_window_raise(dlg->window);
275                 return;
276         }
277
278         dlg=dlg_window_new("Ethereal: Compute LDAP Service Response Time statistics");
279         SIGNAL_CONNECT(dlg, "destroy", dlg_destroy_cb, NULL);
280
281         dlg_box=gtk_vbox_new(FALSE, 10);
282         gtk_container_border_width(GTK_CONTAINER(dlg_box), 10);
283         gtk_container_add(GTK_CONTAINER(dlg), dlg_box);
284         gtk_widget_show(dlg_box);
285
286         /* Filter box */
287         filter_box=gtk_hbox_new(FALSE, 3);
288
289         /* Filter button */
290         filter_bt=gtk_button_new_with_label("Filter:");
291         SIGNAL_CONNECT(filter_bt, "clicked", display_filter_construct_cb, &args);
292         gtk_box_pack_start(GTK_BOX(filter_box), filter_bt, FALSE, FALSE, 0);
293         gtk_widget_show(filter_bt);
294
295         /* Filter entry */
296         filter_entry=gtk_entry_new();
297         WIDGET_SET_SIZE(filter_entry, 300, -2);
298
299         /* filter prefs dialog */
300         OBJECT_SET_DATA(filter_bt, E_FILT_TE_PTR_KEY, filter_entry);
301         /* filter prefs dialog */
302
303         gtk_box_pack_start(GTK_BOX(filter_box), filter_entry, TRUE, TRUE, 0);
304         filter=gtk_entry_get_text(GTK_ENTRY(main_display_filter_widget));
305         if(filter){
306                 gtk_entry_set_text(GTK_ENTRY(filter_entry), filter);
307         }
308         gtk_widget_show(filter_entry);
309
310         gtk_box_pack_start(GTK_BOX(dlg_box), filter_box, TRUE, TRUE, 0);
311         gtk_widget_show(filter_box);
312
313         /* button box */
314         bbox=gtk_hbutton_box_new();
315         gtk_button_box_set_layout (GTK_BUTTON_BOX (bbox), GTK_BUTTONBOX_DEFAULT_STYLE);
316         gtk_button_box_set_spacing(GTK_BUTTON_BOX(bbox), 5);
317         gtk_box_pack_start(GTK_BOX(dlg_box), bbox, FALSE, FALSE, 0);
318         gtk_widget_show(bbox);
319
320         /* the start button */
321         start_button=gtk_button_new_with_label("Create Stat");
322         SIGNAL_CONNECT_OBJECT(start_button, "clicked",
323                               ldapstat_start_button_clicked, NULL);
324         gtk_box_pack_start(GTK_BOX(bbox), start_button, TRUE, TRUE, 0);
325         GTK_WIDGET_SET_FLAGS(start_button, GTK_CAN_DEFAULT);
326         gtk_widget_grab_default(start_button);
327         gtk_widget_show(start_button);
328
329         cancel_button=BUTTON_NEW_FROM_STOCK(GTK_STOCK_CANCEL);
330         SIGNAL_CONNECT(cancel_button, "clicked", dlg_cancel_cb, dlg);
331         GTK_WIDGET_SET_FLAGS(cancel_button, GTK_CAN_DEFAULT);
332         gtk_box_pack_start(GTK_BOX(bbox), cancel_button, TRUE, TRUE, 0);
333         gtk_widget_show(cancel_button);
334
335         /* Catch the "activate" signal on the filter text entry, so that
336            if the user types Return there, we act as if the "Create Stat"
337            button had been selected, as happens if Return is typed if some
338            widget that *doesn't* handle the Return key has the input
339            focus. */
340         dlg_set_activate(filter_entry, start_button);
341
342         /* Catch the "key_press_event" signal in the window, so that we can
343            catch the ESC key being pressed and act as if the "Cancel" button
344            had been selected. */
345         dlg_set_cancel(dlg, cancel_button);
346
347         /* Give the initial focus to the "Filter" entry box. */
348         gtk_widget_grab_focus(filter_entry);
349
350         gtk_widget_show_all(dlg);
351 }
352
353 void
354 register_tap_listener_gtkldapstat(void)
355 {
356         register_ethereal_tap("ldap,srt", gtk_ldapstat_init);
357 }
358
359 void
360 register_tap_menu_gtkldapstat(void)
361 {
362         register_tap_menu_item("_Statistics/Service Response Time/LDAP...",
363             gtk_ldapstat_cb, NULL, NULL, NULL);
364 }