Remove some GTK+-only code.
[metze/wireshark/wip.git] / ui / preference_utils.c
1 /* preference_utils.c
2  * Routines for handling preferences
3  *
4  * Wireshark - Network traffic analyzer
5  * By Gerald Combs <gerald@wireshark.org>
6  * Copyright 1998 Gerald Combs
7  *
8  * SPDX-License-Identifier: GPL-2.0-or-later
9  */
10
11 #include "config.h"
12
13 #include <errno.h>
14
15 #include <epan/column.h>
16 #include <wsutil/filesystem.h>
17 #include <epan/prefs.h>
18 #include <epan/prefs-int.h>
19 #include <epan/packet.h>
20 #include <epan/decode_as.h>
21
22 #ifdef HAVE_LIBPCAP
23 #include "capture_opts.h"
24 #include "ui/capture_globals.h"
25 #endif
26
27 #include "ui/preference_utils.h"
28 #include "ui/simple_dialog.h"
29
30 #ifdef HAVE_LIBPCAP
31 gboolean auto_scroll_live;
32 #endif
33
34 /* Fill in capture options with values from the preferences */
35 void
36 prefs_to_capture_opts(void)
37 {
38 #ifdef HAVE_LIBPCAP
39     /* Set promiscuous mode from the preferences setting. */
40     /* the same applies to other preferences settings as well. */
41     global_capture_opts.default_options.promisc_mode = prefs.capture_prom_mode;
42     global_capture_opts.use_pcapng                   = prefs.capture_pcap_ng;
43     global_capture_opts.show_info                    = prefs.capture_show_info; /* GTK+ only? */
44     global_capture_opts.real_time_mode               = prefs.capture_real_time;
45     auto_scroll_live                                 = prefs.capture_auto_scroll;
46 #endif /* HAVE_LIBPCAP */
47 }
48
49 void
50 prefs_main_write(void)
51 {
52     int   err;
53     char *pf_dir_path;
54     char *pf_path;
55
56     /* Create the directory that holds personal configuration files, if
57        necessary.  */
58     if (create_persconffile_dir(&pf_dir_path) == -1) {
59         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
60                 "Can't create directory\n\"%s\"\nfor preferences file: %s.", pf_dir_path,
61                 g_strerror(errno));
62         g_free(pf_dir_path);
63     } else {
64         /* Write the preferencs out. */
65         err = write_prefs(&pf_path);
66         if (err != 0) {
67             simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
68                     "Can't open preferences file\n\"%s\": %s.", pf_path,
69                     g_strerror(err));
70             g_free(pf_path);
71         }
72     }
73 }
74
75 static unsigned int
76 prefs_store_ext_helper(const char * module_name, const char *pref_name, const char *pref_value)
77 {
78     module_t * module = NULL;
79     pref_t * pref = NULL;
80     unsigned int pref_changed = 0;
81
82     if ( ! prefs_is_registered_protocol(module_name))
83         return 0;
84
85     module = prefs_find_module(module_name);
86     if ( ! module )
87         return 0;
88
89     pref = prefs_find_preference(module, pref_name);
90
91     if (!pref)
92         return 0;
93
94     if (prefs_get_type(pref) == PREF_STRING )
95     {
96         pref_changed |= prefs_set_string_value(pref, pref_value, pref_stashed);
97         if ( ! pref_changed || prefs_get_string_value(pref, pref_stashed) != 0 )
98             pref_changed |= prefs_set_string_value(pref, pref_value, pref_current);
99     }
100
101     return pref_changed;
102 }
103
104 unsigned int
105 prefs_store_ext(const char * module_name, const char *pref_name, const char *pref_value)
106 {
107     unsigned int changed_flags = prefs_store_ext_helper(module_name, pref_name, pref_value);
108     if ( changed_flags )
109     {
110         prefs_main_write();
111         prefs_apply_all();
112         prefs_to_capture_opts();
113         return changed_flags;
114     }
115
116     return 0;
117 }
118
119 gboolean
120 prefs_store_ext_multiple(const char * module, GHashTable * pref_values)
121 {
122     gboolean pref_changed = FALSE;
123     GList * keys = NULL;
124
125     if ( ! prefs_is_registered_protocol(module))
126         return pref_changed;
127
128     keys = g_hash_table_get_keys(pref_values);
129     if ( ! keys )
130         return pref_changed;
131
132     while ( keys != NULL )
133     {
134         gchar * pref_name = (gchar *)keys->data;
135         gchar * pref_value = (gchar *) g_hash_table_lookup(pref_values, keys->data);
136
137         if ( pref_name && pref_value )
138         {
139             if ( prefs_store_ext_helper(module, pref_name, pref_value) )
140                 pref_changed = TRUE;
141         }
142         keys = g_list_next(keys);
143     }
144
145     if ( pref_changed )
146     {
147         prefs_main_write();
148         prefs_apply_all();
149         prefs_to_capture_opts();
150     }
151
152     return TRUE;
153 }
154
155 gint
156 column_prefs_add_custom(gint fmt, const gchar *title, const gchar *custom_fields, gint custom_occurrence)
157 {
158     GList *clp;
159     fmt_data *cfmt, *last_cfmt;
160     gint colnr;
161
162     cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
163     /*
164      * Because a single underscore is interpreted as a signal that the next character
165      * is going to be marked as accelerator for this header (i.e. is going to be
166      * shown underlined), escape it be inserting a second consecutive underscore.
167      */
168     cfmt->title = g_strdup(title);
169     cfmt->fmt = fmt;
170     cfmt->custom_fields = g_strdup(custom_fields);
171     cfmt->custom_occurrence = custom_occurrence;
172     cfmt->resolved = TRUE;
173
174     colnr = g_list_length(prefs.col_list);
175
176     if (custom_fields) {
177         cfmt->visible = TRUE;
178         clp = g_list_last(prefs.col_list);
179         last_cfmt = (fmt_data *) clp->data;
180         if (last_cfmt->fmt == COL_INFO) {
181             /* Last column is COL_INFO, add custom column before this */
182             colnr -= 1;
183             prefs.col_list = g_list_insert(prefs.col_list, cfmt, colnr);
184         } else {
185             prefs.col_list = g_list_append(prefs.col_list, cfmt);
186         }
187     } else {
188         cfmt->visible = FALSE;  /* Will be set to TRUE in visible_toggled() when added to list */
189         prefs.col_list = g_list_append(prefs.col_list, cfmt);
190     }
191
192     return colnr;
193 }
194
195 void
196 column_prefs_remove_link(GList *col_link)
197 {
198     fmt_data *cfmt;
199
200     if (!col_link || !col_link->data) return;
201
202     cfmt = (fmt_data *) col_link->data;
203
204     g_free(cfmt->title);
205     g_free(cfmt->custom_fields);
206     g_free(cfmt);
207     prefs.col_list = g_list_remove_link(prefs.col_list, col_link);
208 }
209
210 void
211 column_prefs_remove_nth(gint col)
212 {
213     column_prefs_remove_link(g_list_nth(prefs.col_list, col));
214 }
215
216 /*
217  * Editor modelines  -  https://www.wireshark.org/tools/modelines.html
218  *
219  * Local Variables:
220  * c-basic-offset: 4
221  * tab-width: 8
222  * indent-tabs-mode: nil
223  * End:
224  *
225  * ex: set shiftwidth=4 tabstop=8 expandtab:
226  * :indentSize=4:tabSize=8:noTabs=true:
227  */