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