MSWSP: WIP: dissect_CPMSetBindings()
[wireshark-wip.git] / ui / preference_utils.c
blobefcc160a35326f565eb056db1f25c5d784d2607c
1 /* preference_utils.h
2 * Routines for handling preferences
4 * $Id$
6 * Wireshark - Network traffic analyzer
7 * By Gerald Combs <gerald@wireshark.org>
8 * Copyright 1998 Gerald Combs
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 #include "config.h"
27 #include <errno.h>
29 #include <glib.h>
31 #include <epan/column-info.h>
32 #include <epan/column.h>
33 #include <epan/filesystem.h>
34 #include <epan/prefs.h>
35 #include <epan/prefs-int.h>
37 #ifdef HAVE_LIBPCAP
38 #include "capture_opts.h"
39 #include "ui/capture_globals.h"
40 #endif
42 #include "ui/preference_utils.h"
43 #include "ui/simple_dialog.h"
45 guint
46 pref_stash(pref_t *pref, gpointer unused _U_)
48 switch (pref->type) {
50 case PREF_UINT:
51 pref->stashed_val.uint = *pref->varp.uint;
52 break;
54 case PREF_BOOL:
55 pref->stashed_val.boolval = *pref->varp.boolp;
56 break;
58 case PREF_ENUM:
59 pref->stashed_val.enumval = *pref->varp.enump;
60 break;
62 case PREF_STRING:
63 case PREF_FILENAME:
64 case PREF_DIRNAME:
65 g_free(pref->stashed_val.string);
66 pref->stashed_val.string = g_strdup(*pref->varp.string);
67 break;
69 case PREF_RANGE:
70 g_free(pref->stashed_val.range);
71 pref->stashed_val.range = range_copy(*pref->varp.range);
72 break;
74 case PREF_COLOR:
75 pref->stashed_val.color = *pref->varp.colorp;
76 break;
78 case PREF_STATIC_TEXT:
79 case PREF_UAT:
80 case PREF_CUSTOM:
81 break;
83 case PREF_OBSOLETE:
84 g_assert_not_reached();
85 break;
87 return 0;
90 guint
91 pref_unstash(pref_t *pref, gpointer changed_p)
93 gboolean *pref_changed_p = (gboolean *)changed_p;
95 /* Revert the preference to its saved value. */
96 switch (pref->type) {
98 case PREF_UINT:
99 if (*pref->varp.uint != pref->stashed_val.uint) {
100 *pref_changed_p = TRUE;
101 *pref->varp.uint = pref->stashed_val.uint;
103 break;
105 case PREF_BOOL:
106 if (*pref->varp.boolp != pref->stashed_val.boolval) {
107 *pref_changed_p = TRUE;
108 *pref->varp.boolp = pref->stashed_val.boolval;
110 break;
112 case PREF_ENUM:
113 if (*pref->varp.enump != pref->stashed_val.enumval) {
114 *pref_changed_p = TRUE;
115 *pref->varp.enump = pref->stashed_val.enumval;
117 break;
119 case PREF_STRING:
120 case PREF_FILENAME:
121 case PREF_DIRNAME:
122 if (strcmp(*pref->varp.string, pref->stashed_val.string) != 0) {
123 *pref_changed_p = TRUE;
124 g_free((void *)*pref->varp.string);
125 *pref->varp.string = g_strdup(pref->stashed_val.string);
127 break;
129 case PREF_RANGE:
130 if (!ranges_are_equal(*pref->varp.range, pref->stashed_val.range)) {
131 *pref_changed_p = TRUE;
132 g_free(*pref->varp.range);
133 *pref->varp.range = range_copy(pref->stashed_val.range);
135 break;
137 case PREF_COLOR:
138 *pref->varp.colorp = pref->stashed_val.color;
139 break;
141 case PREF_STATIC_TEXT:
142 case PREF_UAT:
143 case PREF_CUSTOM:
144 break;
146 case PREF_OBSOLETE:
147 g_assert_not_reached();
148 break;
150 return 0;
153 void
154 reset_stashed_pref(pref_t *pref) {
155 switch (pref->type) {
157 case PREF_UINT:
158 pref->stashed_val.uint = pref->default_val.uint;
159 break;
161 case PREF_BOOL:
162 pref->stashed_val.boolval = pref->default_val.boolval;
163 break;
165 case PREF_ENUM:
166 pref->stashed_val.enumval = pref->default_val.enumval;
167 break;
169 case PREF_STRING:
170 case PREF_FILENAME:
171 case PREF_DIRNAME:
172 g_free(pref->stashed_val.string);
173 pref->stashed_val.string = g_strdup(pref->default_val.string);
174 break;
176 case PREF_RANGE:
177 g_free(pref->stashed_val.range);
178 pref->stashed_val.range = range_copy(pref->default_val.range);
179 break;
181 case PREF_COLOR:
182 memcpy(&pref->stashed_val.color, &pref->default_val.color, sizeof(color_t));
183 break;
185 case PREF_STATIC_TEXT:
186 case PREF_UAT:
187 case PREF_CUSTOM:
188 break;
190 case PREF_OBSOLETE:
191 g_assert_not_reached();
192 break;
196 guint
197 pref_clean_stash(pref_t *pref, gpointer unused _U_)
199 switch (pref->type) {
201 case PREF_UINT:
202 break;
204 case PREF_BOOL:
205 break;
207 case PREF_ENUM:
208 break;
210 case PREF_STRING:
211 case PREF_FILENAME:
212 case PREF_DIRNAME:
213 if (pref->stashed_val.string != NULL) {
214 g_free(pref->stashed_val.string);
215 pref->stashed_val.string = NULL;
217 break;
219 case PREF_RANGE:
220 if (pref->stashed_val.range != NULL) {
221 g_free(pref->stashed_val.range);
222 pref->stashed_val.range = NULL;
224 break;
226 case PREF_STATIC_TEXT:
227 case PREF_UAT:
228 case PREF_COLOR:
229 case PREF_CUSTOM:
230 break;
232 case PREF_OBSOLETE:
233 g_assert_not_reached();
234 break;
236 return 0;
239 /* Fill in capture options with values from the preferences */
240 void
241 prefs_to_capture_opts(void)
243 #ifdef HAVE_LIBPCAP
244 /* Set promiscuous mode from the preferences setting. */
245 /* the same applies to other preferences settings as well. */
246 global_capture_opts.default_options.promisc_mode = prefs.capture_prom_mode;
247 global_capture_opts.use_pcapng = prefs.capture_pcap_ng;
248 global_capture_opts.show_info = prefs.capture_show_info;
249 global_capture_opts.real_time_mode = prefs.capture_real_time;
250 auto_scroll_live = prefs.capture_auto_scroll;
251 #endif /* HAVE_LIBPCAP */
254 void
255 prefs_main_write(void)
257 int err;
258 char *pf_dir_path;
259 char *pf_path;
261 /* Create the directory that holds personal configuration files, if
262 necessary. */
263 if (create_persconffile_dir(&pf_dir_path) == -1) {
264 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
265 "Can't create directory\n\"%s\"\nfor preferences file: %s.", pf_dir_path,
266 g_strerror(errno));
267 g_free(pf_dir_path);
268 } else {
269 /* Write the preferencs out. */
270 err = write_prefs(&pf_path);
271 if (err != 0) {
272 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
273 "Can't open preferences file\n\"%s\": %s.", pf_path,
274 g_strerror(err));
275 g_free(pf_path);
280 void
281 column_prefs_add_custom(gint fmt, const gchar *title, const gchar *custom_field, gint custom_occurrence)
283 GList *clp;
284 fmt_data *cfmt, *last_cfmt;
286 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
288 * Because a single underscore is interpreted as a signal that the next character
289 * is going to be marked as accelerator for this header (i.e. is going to be
290 * shown underlined), escape it be inserting a second consecutive underscore.
292 cfmt->title = g_strdup(title);
293 cfmt->fmt = fmt;
294 cfmt->custom_field = g_strdup(custom_field);
295 cfmt->custom_occurrence = custom_occurrence;
296 cfmt->resolved = TRUE;
298 if (custom_field) {
299 cfmt->visible = TRUE;
300 clp = g_list_last(prefs.col_list);
301 last_cfmt = (fmt_data *) clp->data;
302 if (last_cfmt->fmt == COL_INFO) {
303 /* Last column is COL_INFO, add custom column before this */
304 prefs.col_list = g_list_insert(prefs.col_list, cfmt, g_list_length(prefs.col_list)-1);
305 } else {
306 prefs.col_list = g_list_append(prefs.col_list, cfmt);
308 } else {
309 cfmt->visible = FALSE; /* Will be set to TRUE in visible_toggled() when added to list */
310 prefs.col_list = g_list_append(prefs.col_list, cfmt);
314 void
315 column_prefs_remove_link(GList *col_link)
317 fmt_data *cfmt;
319 if (!col_link || !col_link->data) return;
321 cfmt = (fmt_data *) col_link->data;
323 g_free(cfmt->title);
324 g_free(cfmt->custom_field);
325 g_free(cfmt);
326 prefs.col_list = g_list_remove_link(prefs.col_list, col_link);
329 void
330 column_prefs_remove_nth(gint col)
332 column_prefs_remove_link(g_list_nth(prefs.col_list, col));
336 * Editor modelines
338 * Local Variables:
339 * c-basic-offset: 2
340 * tab-width: 8
341 * indent-tabs-mode: nil
342 * End:
344 * ex: set shiftwidth=2 tabstop=8 expandtab:
345 * :indentSize=2:tabSize=8:noTabs=true: