34ec6d05ac3dbc21a40c6e9ed5a3de6356f89874
[obnox/wireshark/wip.git] / gtk / main_filter_toolbar.c
1 /* main_filter_toolbar.c
2  * The filter toolbar
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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 /*
26  * This file implements the "filter" toolbar for Wireshark.
27  */
28
29 #ifdef HAVE_CONFIG_H
30 #include "config.h"
31 #endif
32
33 #include <stdio.h>
34 #include <string.h>
35
36 #include <gtk/gtk.h>
37
38 #include "filter_dlg.h"
39 #include "filter_autocomplete.h"
40
41 #include "epan/prefs.h"
42
43 #include "keys.h"
44 #include "gtkglobals.h"
45 #include "stock_icons.h"
46 #include "recent.h"
47
48 #include "main.h"
49 #include "menus.h"
50 #include "main_toolbar.h"
51 #include "main_filter_toolbar.h"
52
53 GtkWidget   *main_display_filter_widget=NULL;
54
55 /* Run the current display filter on the current packet set, and
56    redisplay. */
57 static void
58 filter_activate_cb(GtkWidget *w _U_, gpointer data)
59 {
60   const char *s;
61 #ifdef NEW_FILTER_COMBO_BOX
62 #if GTK_CHECK_VERSION(2,6,0)
63 #else
64         GtkTreeIter   iter;
65         GtkTreeModel *model;
66 #endif
67   /* Since 2.6 */
68   #if GTK_CHECK_VERSION(2,6,0)
69   s = gtk_combo_box_get_active_text(GTK_COMBO_BOX(filter_cm))
70   #else
71   gtk_combo_box_get_active_iter(GTK_COMBO_BOX(filter_cm), &iter);
72   model = gtk_combo_box_get_model(GTK_COMBO_BOX(filter_cm));
73   gtk_tree_model_get(model, &iter, 0, &s, -1);
74   #endif
75
76 #else
77   s = gtk_entry_get_text(GTK_ENTRY(data));
78 #endif
79   main_filter_packets(&cfile, s, FALSE);
80 }
81
82 /* redisplay with no display filter */
83 static void
84 filter_reset_cb(GtkWidget *w, gpointer data _U_)
85 {
86   GtkWidget *filter_te = NULL;
87
88   if ((filter_te = g_object_get_data(G_OBJECT(w), E_DFILTER_TE_KEY))) {
89     gtk_entry_set_text(GTK_ENTRY(filter_te), "");
90   }
91   main_filter_packets(&cfile, NULL, FALSE);
92 }
93
94 /*#define NEW_FILTER_COMBO_BOX 1*/
95
96 GtkWidget *filter_toolbar_new()
97 {
98     GtkWidget     *filter_cm;
99         GtkWidget     *filter_te;
100     GtkWidget     *filter_tb;
101     GList         *dfilter_list = NULL;
102     GtkTooltips   *tooltips;
103     GtkToolItem   *filter_bt, *filter_add_expr_bt, *filter_reset;
104     GtkToolItem   *filter_apply, *item;
105
106
107     /* Display filter construct dialog has an Apply button, and "OK" not
108        only sets our text widget, it activates it (i.e., it causes us to
109        filter the capture). */
110     static construct_args_t args = {
111         "Wireshark: Display Filter",
112         TRUE,
113         TRUE,
114         FALSE
115     };
116
117     tooltips = gtk_tooltips_new();
118
119     /* filter toolbar */
120     filter_tb = gtk_toolbar_new();
121     gtk_toolbar_set_orientation(GTK_TOOLBAR(filter_tb),
122                                 GTK_ORIENTATION_HORIZONTAL);
123
124     g_object_set_data(G_OBJECT(top_level), E_TB_FILTER_KEY, filter_tb);
125     gtk_widget_show(filter_tb);
126
127     /* Create the "Filter:" button */
128     filter_bt = gtk_tool_button_new_from_stock (WIRESHARK_STOCK_DISPLAY_FILTER_ENTRY);
129     g_signal_connect(filter_bt, "clicked", G_CALLBACK(display_filter_construct_cb), &args);
130     gtk_widget_show(GTK_WIDGET (filter_bt));
131     g_object_set_data(G_OBJECT(top_level), E_FILT_BT_PTR_KEY, filter_bt);
132
133     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
134                        filter_bt,
135                        -1);
136     gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), GTK_WIDGET(filter_bt),
137                          "Open the \"Display Filter\" dialog, to edit/apply filters",
138                          "Private");
139
140     /* Create the filter combobox */
141 #ifdef NEW_FILTER_COMBO_BOX
142         filter_cm = gtk_combo_box_entry_new_text ();
143         filter_te = gtk_bin_get_child(GTK_BIN(filter_cm));
144 #else
145     filter_cm = gtk_combo_new();
146     dfilter_list = NULL;
147     gtk_combo_disable_activate(GTK_COMBO(filter_cm));
148     gtk_combo_set_case_sensitive(GTK_COMBO(filter_cm), TRUE);
149     g_object_set_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY, dfilter_list);
150     filter_te = GTK_COMBO(filter_cm)->entry;
151 #endif
152     main_display_filter_widget=filter_te;
153     g_object_set_data(G_OBJECT(filter_bt), E_FILT_TE_PTR_KEY, filter_te);
154     g_object_set_data(G_OBJECT(filter_te), E_DFILTER_CM_KEY, filter_cm);
155     g_object_set_data(G_OBJECT(top_level), E_DFILTER_CM_KEY, filter_cm);
156     g_signal_connect(filter_te, "activate", G_CALLBACK(filter_activate_cb), filter_te);
157     g_signal_connect(filter_te, "changed", G_CALLBACK(filter_te_syntax_check_cb), NULL);
158     g_object_set_data(G_OBJECT(filter_tb), E_FILT_AUTOCOMP_PTR_KEY, NULL);
159     g_object_set_data(G_OBJECT(filter_te), E_FILT_FIELD_USE_STATUSBAR_KEY, "");
160     g_signal_connect(filter_te, "key-press-event", G_CALLBACK (filter_string_te_key_pressed_cb), NULL);
161     g_signal_connect(filter_tb, "key-press-event", G_CALLBACK (filter_parent_dlg_key_pressed_cb), NULL);
162
163         gtk_widget_set_size_request(filter_cm, 400, -1);
164     gtk_widget_show(filter_cm);
165     item = gtk_tool_item_new ();
166     gtk_container_add (GTK_CONTAINER (item), filter_cm);
167     gtk_widget_show (GTK_WIDGET (item));
168
169     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
170                        item,
171                        -1);
172
173     /* setting a tooltip for a combobox will do nothing, so add it to the corresponding text entry */
174 #ifdef NEW_FILTER_COMBO_BOX
175     gtk_tooltips_set_tip(tooltips, filter_cm,
176 #else
177     gtk_tooltips_set_tip(tooltips, filter_te,
178 #endif
179         "Enter a display filter, or choose one of your recently used filters. "
180         "The background color of this field is changed by a continuous syntax check "
181         "(green is valid, red is invalid, yellow may have unexpected results).",
182         NULL);
183
184     /* Create the "Add Expression..." button, to pop up a dialog
185        for constructing filter comparison expressions. */
186     filter_add_expr_bt = gtk_tool_button_new_from_stock(WIRESHARK_STOCK_ADD_EXPRESSION);
187 #ifdef NEW_FILTER_COMBO_BOX
188 #else
189     g_object_set_data(G_OBJECT(filter_tb), E_FILT_FILTER_TE_KEY, filter_te);
190     g_signal_connect(filter_add_expr_bt, "clicked", G_CALLBACK(filter_add_expr_bt_cb), filter_tb);
191 #endif
192     gtk_widget_show(GTK_WIDGET(filter_add_expr_bt));
193
194     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
195                        filter_add_expr_bt,
196                        -1);
197
198     gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), GTK_WIDGET(filter_add_expr_bt),
199                          "Add an expression to this filter string", 
200                          "Private");
201
202     /* Create the "Clear" button */
203     filter_reset = gtk_tool_button_new_from_stock(WIRESHARK_STOCK_CLEAR_EXPRESSION);
204     g_object_set_data(G_OBJECT(filter_reset), E_DFILTER_TE_KEY, filter_te);
205     g_signal_connect(filter_reset, "clicked", G_CALLBACK(filter_reset_cb), NULL);
206     gtk_widget_show(GTK_WIDGET(filter_reset));
207     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
208                        filter_reset,
209                        -1);
210
211     gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), GTK_WIDGET(filter_reset),
212                          "Clear this filter string and update the display", 
213                          "Private");
214
215     /* Create the "Apply" button */
216     filter_apply = gtk_tool_button_new_from_stock(WIRESHARK_STOCK_APPLY_EXPRESSION);
217     g_object_set_data(G_OBJECT(filter_apply), E_DFILTER_CM_KEY, filter_cm);
218     g_signal_connect(filter_apply, "clicked", G_CALLBACK(filter_activate_cb), filter_te);
219     gtk_widget_show(GTK_WIDGET(filter_apply));
220
221     gtk_toolbar_insert(GTK_TOOLBAR(filter_tb),
222                        filter_apply,
223                        -1);
224
225     gtk_tooltips_set_tip(GTK_TOOLTIPS(tooltips), GTK_WIDGET(filter_apply),
226                          "Apply this filter string to the display",
227                          "Private");
228
229     /* Sets the text entry widget pointer as the E_DILTER_TE_KEY data
230      * of any widget that ends up calling a callback which needs
231      * that text entry pointer */
232     set_menu_object_data("/File/Open...", E_DFILTER_TE_KEY, filter_te);
233     set_menu_object_data("/Edit/Copy/As Filter", E_DFILTER_TE_KEY,
234                          filter_te);
235     set_menu_object_data("/Analyze/Display Filters...", E_FILT_TE_PTR_KEY,
236                          filter_te);
237     set_menu_object_data("/Analyze/Follow TCP Stream", E_DFILTER_TE_KEY,
238                          filter_te);
239     set_menu_object_data("/Analyze/Follow UDP Stream", E_DFILTER_TE_KEY,
240                          filter_te);
241     set_menu_object_data("/Analyze/Follow SSL Stream", E_DFILTER_TE_KEY,
242                          filter_te);
243     set_menu_object_data("/Analyze/Apply as Filter/Selected", E_DFILTER_TE_KEY,
244                          filter_te);
245     set_menu_object_data("/Analyze/Apply as Filter/Not Selected", E_DFILTER_TE_KEY,
246                          filter_te);
247     set_menu_object_data("/Analyze/Apply as Filter/... and Selected", E_DFILTER_TE_KEY,
248                          filter_te);
249     set_menu_object_data("/Analyze/Apply as Filter/... or Selected", E_DFILTER_TE_KEY,
250                          filter_te);
251     set_menu_object_data("/Analyze/Apply as Filter/... and not Selected", E_DFILTER_TE_KEY,
252                          filter_te);
253     set_menu_object_data("/Analyze/Apply as Filter/... or not Selected", E_DFILTER_TE_KEY,
254                          filter_te);
255     set_menu_object_data("/Analyze/Prepare a Filter/Selected", E_DFILTER_TE_KEY,
256                          filter_te);
257     set_menu_object_data("/Analyze/Prepare a Filter/Not Selected", E_DFILTER_TE_KEY,
258                          filter_te);
259     set_menu_object_data("/Analyze/Prepare a Filter/... and Selected", E_DFILTER_TE_KEY,
260                          filter_te);
261     set_menu_object_data("/Analyze/Prepare a Filter/... or Selected", E_DFILTER_TE_KEY,
262                          filter_te);
263     set_menu_object_data("/Analyze/Prepare a Filter/... and not Selected", E_DFILTER_TE_KEY,
264                          filter_te);
265     set_menu_object_data("/Analyze/Prepare a Filter/... or not Selected", E_DFILTER_TE_KEY,
266                          filter_te);
267     set_menu_object_data("/Conversation Filter/Ethernet", E_DFILTER_TE_KEY,
268                          filter_te);
269     set_menu_object_data("/Conversation Filter/IP", E_DFILTER_TE_KEY,
270                          filter_te);
271     set_menu_object_data("/Conversation Filter/TCP", E_DFILTER_TE_KEY,
272                          filter_te);
273     set_menu_object_data("/Conversation Filter/UDP", E_DFILTER_TE_KEY,
274                          filter_te);
275     set_menu_object_data("/Conversation Filter/PN-CBA Server", E_DFILTER_TE_KEY,
276                          filter_te);
277     set_toolbar_object_data(E_DFILTER_TE_KEY, filter_te);
278     g_object_set_data(G_OBJECT(popup_menu_object), E_DFILTER_TE_KEY, filter_te);
279
280     /* make current preferences effective */
281     toolbar_redraw_all();
282
283     return filter_tb;
284 }
285
286 static gint
287 dfilter_entry_match(gconstpointer a, gconstpointer b)
288 {
289     const char *s1 = a;
290     const char *s2 = b;
291
292     return strcmp(s1, s2);
293 }
294 static gboolean
295 dfilter_entry_match_new(GtkWidget *filter_cm, char *s)
296 {
297         GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX(filter_cm));
298         GtkTreeIter   iter;
299         GValue value = { 0, };
300         const char *filter_str;
301
302         if (!gtk_tree_model_get_iter_first (model, &iter))
303                 return FALSE;
304         do{
305                 gtk_tree_model_get_value (model, &iter, 0, &value);
306                 filter_str = g_value_get_string (&value);
307                 if(filter_str){
308                         if(strcmp(s, filter_str) == 0){
309                                 g_value_unset (&value);
310                                 return TRUE;
311                         }
312                 }
313         }while (gtk_tree_model_iter_next (model, &iter));
314
315         return FALSE;
316 }
317
318 /* add a display filter to the combo box */
319 /* Note: a new filter string will not replace an old identical one */
320 static gboolean
321 dfilter_combo_add(GtkWidget *filter_cm, char *s) {
322 #ifdef NEW_FILTER_COMBO_BOX
323         if(!dfilter_entry_match_new(filter_cm,s))
324                 gtk_combo_box_append_text(GTK_COMBO_BOX(filter_cm), s);
325 #else
326     GList     *dfilter_list = g_object_get_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY);
327
328     /* GtkCombos don't let us get at their list contents easily, so we maintain
329        our own filter list, and feed it to gtk_combo_set_popdown_strings when
330        a new filter is added. */
331     if (s && strlen(s) > 0 &&
332         g_list_length(dfilter_list) < prefs.gui_recent_df_entries_max &&
333         g_list_find_custom(dfilter_list, s, dfilter_entry_match) == NULL) {
334
335         dfilter_list = g_list_append(dfilter_list, s);
336         s = NULL;
337         g_object_set_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY, dfilter_list);
338         gtk_combo_set_popdown_strings(GTK_COMBO(filter_cm), dfilter_list);
339         gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(filter_cm)->entry), g_list_first(dfilter_list)->data);
340     }
341 #endif
342     g_free(s);
343
344     return TRUE;
345 }
346
347
348 /* write all non empty display filters (until maximum count)
349  * of the combo box GList to the user's recent file */
350 void
351 dfilter_recent_combo_write_all(FILE *rf) {
352     GtkWidget *filter_cm = g_object_get_data(G_OBJECT(top_level), E_DFILTER_CM_KEY);
353 #ifdef NEW_FILTER_COMBO_BOX
354         GtkTreeModel *model = gtk_combo_box_get_model (GTK_COMBO_BOX(filter_cm));
355         GtkTreeIter   iter;
356         GValue value = { 0, };
357         const char *filter_str;
358         guint      max_count = 0;
359
360         if (!gtk_tree_model_get_iter_first (model, &iter))
361                 return;
362         do{
363                 gtk_tree_model_get_value (model, &iter, 0, &value);
364                 filter_str = g_value_get_string (&value);
365                 if(filter_str)
366                         fprintf (rf, RECENT_KEY_DISPLAY_FILTER ": %s\n", filter_str);
367                 g_value_unset (&value);
368
369         }while (gtk_tree_model_iter_next (model, &iter)&& (max_count++ < prefs.gui_recent_df_entries_max));
370
371 #else
372     GList     *dfilter_list = g_object_get_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY);
373     GList     *li;
374     guint      max_count = 0;
375
376     /* write all non empty display filter strings to the recent file (until max count) */
377     li = g_list_first(dfilter_list);
378     while ( li && (max_count++ < prefs.gui_recent_df_entries_max) ) {
379         if (strlen(li->data)) {
380             fprintf (rf, RECENT_KEY_DISPLAY_FILTER ": %s\n", (char *)li->data);
381         }
382         li = li->next;
383     }
384 #endif
385 }
386
387 /* empty the combobox entry field */
388 void
389 dfilter_combo_add_empty(void) {
390     GtkWidget *filter_cm = g_object_get_data(G_OBJECT(top_level), E_DFILTER_CM_KEY);
391
392     gtk_entry_set_text(GTK_ENTRY(GTK_COMBO(filter_cm)->entry), "");
393 }
394
395
396 /* add a display filter coming from the user's recent file to the dfilter combo box */
397 gboolean
398 dfilter_combo_add_recent(gchar *s) {
399     GtkWidget *filter_cm = g_object_get_data(G_OBJECT(top_level), E_DFILTER_CM_KEY);
400     char      *dup;
401
402     dup = g_strdup(s);
403
404     return dfilter_combo_add(filter_cm, dup);
405 }
406
407 /* call cf_filter_packets() and add this filter string to the recent filter list */
408 gboolean
409 main_filter_packets(capture_file *cf, const gchar *dftext, gboolean force)
410 {
411     GtkCombo  *filter_cm = g_object_get_data(G_OBJECT(top_level), E_DFILTER_CM_KEY);
412     GList     *dfilter_list = g_object_get_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY);
413     gboolean   free_filter = TRUE;
414     char      *s;
415     cf_status_t cf_status;
416
417     s = g_strdup(dftext);
418
419     cf_status = cf_filter_packets(cf, s, force);
420     if (!s)
421         return (cf_status == CF_OK);
422
423     /* GtkCombos don't let us get at their list contents easily, so we maintain
424        our own filter list, and feed it to gtk_combo_set_popdown_strings when
425        a new filter is added. */
426     if (cf_status == CF_OK && strlen(s) > 0) {
427         GList *li;
428
429         while ((li = g_list_find_custom(dfilter_list, s, dfilter_entry_match)) != NULL)
430             /* Delete old/duplicate entry now. We'll re-add it later */
431             dfilter_list = g_list_delete_link(dfilter_list, li);
432
433         /* trim list size first */
434         while (g_list_length(dfilter_list) >= prefs.gui_recent_df_entries_max)
435             dfilter_list = g_list_delete_link(dfilter_list, g_list_last(dfilter_list));
436
437         free_filter = FALSE;
438         /* Push the filter to the front of the list */
439         dfilter_list = g_list_prepend(dfilter_list, s);
440         g_object_set_data(G_OBJECT(filter_cm), E_DFILTER_FL_KEY, dfilter_list);
441         gtk_combo_set_popdown_strings(filter_cm, dfilter_list);
442         gtk_entry_set_text(GTK_ENTRY(filter_cm->entry), g_list_first(dfilter_list)->data);
443     }
444
445     if (free_filter)
446         g_free(s);
447
448     return (cf_status == CF_OK);
449 }
450
451