Fix build (automake) error: WIRESHARK_CUSTOM_HEADERS is ng but WIRESHARK_CUSTOM_HDRS...
[obnox/wireshark/wip.git] / gtk / cfilter_combo_utils.c
1 /* cfilter_combo_utils.c
2  * Capture filter combo box routines
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 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <stdio.h>
30 #include <string.h>
31
32 #include <gtk/gtk.h>
33
34 #include "gtk/main.h"
35 #include "gtk/gtkglobals.h"
36 #include "gtk/cfilter_combo_utils.h"
37 #include "gtk/recent.h"
38
39
40 /* XXX: use a preference for this setting! */
41 static guint cfilter_combo_max_recent = 20;
42
43 static gboolean
44 cfilter_combo_add(gchar *s) {
45   GList     *li;
46   GList     *fl = g_object_get_data(G_OBJECT(top_level), E_CFILTER_FL_KEY);
47
48   li = g_list_first(fl);
49   while (li) {
50     /* If the filter is already in the list, remove the old one and
51      * append the new one at the latest position (at g_list_append() below) */
52     if (li->data && strcmp(s, li->data) == 0) {
53       fl = g_list_remove(fl, li->data);
54       break;
55     }
56     li = li->next;
57   }
58   fl = g_list_append(fl, s);
59   g_object_set_data(G_OBJECT(top_level), E_CFILTER_FL_KEY, fl);
60   return TRUE;
61 }
62
63
64 /* write all non empty capture filters (until maximum count)
65  * of the combo box GList to the user's recent file */
66 void
67  cfilter_combo_recent_write_all(FILE *rf) {
68    GList     *cfilter_list = g_object_get_data(G_OBJECT(top_level), E_CFILTER_FL_KEY);
69    GList     *li;
70    guint      max_count = 0;
71
72    /* write all non empty capture filter strings to the recent file (until max count) */
73    li = g_list_first(cfilter_list);
74    while (li && (max_count++ <= cfilter_combo_max_recent) ) {
75      if (li->data && strlen(li->data)) {
76        fprintf (rf, RECENT_KEY_CAPTURE_FILTER ": %s\n", (char *)li->data);
77      }
78      li = li->next;
79    }
80 }
81
82 /* add a capture filter coming from the user's recent file to the cfilter combo box */
83 gboolean
84  cfilter_combo_add_recent(gchar *s) {
85    gchar *dup;
86
87    if (s) {
88      dup = g_strdup(s);
89      if (!cfilter_combo_add(dup)) {
90        g_free(dup);
91        return FALSE;
92      }
93    }
94    return TRUE;
95 }