nexmon – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /* preference_utils.h
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 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22  
23 #include "config.h"
24  
25 #include <errno.h>
26  
27  
28 #include <epan/column.h>
29 #include <wsutil/filesystem.h>
30 #include <epan/prefs.h>
31 #include <epan/prefs-int.h>
32  
33 #ifdef HAVE_LIBPCAP
34 #include "capture_opts.h"
35 #include "ui/capture_globals.h"
36 #endif
37  
38 #include "ui/preference_utils.h"
39 #include "ui/simple_dialog.h"
40  
41 guint
42 pref_stash(pref_t *pref, gpointer unused _U_)
43 {
44 switch (pref->type) {
45  
46 case PREF_UINT:
47 pref->stashed_val.uint = *pref->varp.uint;
48 break;
49  
50 case PREF_BOOL:
51 pref->stashed_val.boolval = *pref->varp.boolp;
52 break;
53  
54 case PREF_ENUM:
55 pref->stashed_val.enumval = *pref->varp.enump;
56 break;
57  
58 case PREF_STRING:
59 case PREF_FILENAME:
60 case PREF_DIRNAME:
61 g_free(pref->stashed_val.string);
62 pref->stashed_val.string = g_strdup(*pref->varp.string);
63 break;
64  
65 case PREF_RANGE:
66 g_free(pref->stashed_val.range);
67 pref->stashed_val.range = range_copy(*pref->varp.range);
68 break;
69  
70 case PREF_COLOR:
71 pref->stashed_val.color = *pref->varp.colorp;
72 break;
73  
74 case PREF_STATIC_TEXT:
75 case PREF_UAT:
76 case PREF_CUSTOM:
77 break;
78  
79 case PREF_OBSOLETE:
80 g_assert_not_reached();
81 break;
82 }
83 return 0;
84 }
85  
86 guint
87 pref_unstash(pref_t *pref, gpointer changed_p)
88 {
89 gboolean *pref_changed_p = (gboolean *)changed_p;
90  
91 /* Revert the preference to its saved value. */
92 switch (pref->type) {
93  
94 case PREF_UINT:
95 if (*pref->varp.uint != pref->stashed_val.uint) {
96 *pref_changed_p = TRUE;
97 *pref->varp.uint = pref->stashed_val.uint;
98 }
99 break;
100  
101 case PREF_BOOL:
102 if (*pref->varp.boolp != pref->stashed_val.boolval) {
103 *pref_changed_p = TRUE;
104 *pref->varp.boolp = pref->stashed_val.boolval;
105 }
106 break;
107  
108 case PREF_ENUM:
109 if (*pref->varp.enump != pref->stashed_val.enumval) {
110 *pref_changed_p = TRUE;
111 *pref->varp.enump = pref->stashed_val.enumval;
112 }
113 break;
114  
115 case PREF_STRING:
116 case PREF_FILENAME:
117 case PREF_DIRNAME:
118 if (strcmp(*pref->varp.string, pref->stashed_val.string) != 0) {
119 *pref_changed_p = TRUE;
120 g_free(*pref->varp.string);
121 *pref->varp.string = g_strdup(pref->stashed_val.string);
122 }
123 break;
124  
125 case PREF_RANGE:
126 if (!ranges_are_equal(*pref->varp.range, pref->stashed_val.range)) {
127 *pref_changed_p = TRUE;
128 g_free(*pref->varp.range);
129 *pref->varp.range = range_copy(pref->stashed_val.range);
130 }
131 break;
132  
133 case PREF_COLOR:
134 *pref->varp.colorp = pref->stashed_val.color;
135 break;
136  
137 case PREF_STATIC_TEXT:
138 case PREF_UAT:
139 case PREF_CUSTOM:
140 break;
141  
142 case PREF_OBSOLETE:
143 g_assert_not_reached();
144 break;
145 }
146 return 0;
147 }
148  
149 void
150 reset_stashed_pref(pref_t *pref) {
151 switch (pref->type) {
152  
153 case PREF_UINT:
154 pref->stashed_val.uint = pref->default_val.uint;
155 break;
156  
157 case PREF_BOOL:
158 pref->stashed_val.boolval = pref->default_val.boolval;
159 break;
160  
161 case PREF_ENUM:
162 pref->stashed_val.enumval = pref->default_val.enumval;
163 break;
164  
165 case PREF_STRING:
166 case PREF_FILENAME:
167 case PREF_DIRNAME:
168 g_free(pref->stashed_val.string);
169 pref->stashed_val.string = g_strdup(pref->default_val.string);
170 break;
171  
172 case PREF_RANGE:
173 g_free(pref->stashed_val.range);
174 pref->stashed_val.range = range_copy(pref->default_val.range);
175 break;
176  
177 case PREF_COLOR:
178 memcpy(&pref->stashed_val.color, &pref->default_val.color, sizeof(color_t));
179 break;
180  
181 case PREF_STATIC_TEXT:
182 case PREF_UAT:
183 case PREF_CUSTOM:
184 break;
185  
186 case PREF_OBSOLETE:
187 g_assert_not_reached();
188 break;
189 }
190 }
191  
192 guint
193 pref_clean_stash(pref_t *pref, gpointer unused _U_)
194 {
195 switch (pref->type) {
196  
197 case PREF_UINT:
198 break;
199  
200 case PREF_BOOL:
201 break;
202  
203 case PREF_ENUM:
204 break;
205  
206 case PREF_STRING:
207 case PREF_FILENAME:
208 case PREF_DIRNAME:
209 if (pref->stashed_val.string != NULL) {
210 g_free(pref->stashed_val.string);
211 pref->stashed_val.string = NULL;
212 }
213 break;
214  
215 case PREF_RANGE:
216 if (pref->stashed_val.range != NULL) {
217 g_free(pref->stashed_val.range);
218 pref->stashed_val.range = NULL;
219 }
220 break;
221  
222 case PREF_STATIC_TEXT:
223 case PREF_UAT:
224 case PREF_COLOR:
225 case PREF_CUSTOM:
226 break;
227  
228 case PREF_OBSOLETE:
229 g_assert_not_reached();
230 break;
231 }
232 return 0;
233 }
234  
235 /* Fill in capture options with values from the preferences */
236 void
237 prefs_to_capture_opts(void)
238 {
239 #ifdef HAVE_LIBPCAP
240 /* Set promiscuous mode from the preferences setting. */
241 /* the same applies to other preferences settings as well. */
242 global_capture_opts.default_options.promisc_mode = prefs.capture_prom_mode;
243 global_capture_opts.use_pcapng = prefs.capture_pcap_ng;
244 global_capture_opts.show_info = prefs.capture_show_info; /* GTK+ only */
245 global_capture_opts.real_time_mode = prefs.capture_real_time;
246 auto_scroll_live = prefs.capture_auto_scroll;
247 #endif /* HAVE_LIBPCAP */
248 }
249  
250 void
251 prefs_main_write(void)
252 {
253 int err;
254 char *pf_dir_path;
255 char *pf_path;
256  
257 /* Create the directory that holds personal configuration files, if
258 necessary. */
259 if (create_persconffile_dir(&pf_dir_path) == -1) {
260 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
261 "Can't create directory\n\"%s\"\nfor preferences file: %s.", pf_dir_path,
262 g_strerror(errno));
263 g_free(pf_dir_path);
264 } else {
265 /* Write the preferencs out. */
266 err = write_prefs(&pf_path);
267 if (err != 0) {
268 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
269 "Can't open preferences file\n\"%s\": %s.", pf_path,
270 g_strerror(err));
271 g_free(pf_path);
272 }
273 }
274 }
275  
276 static gboolean
277 prefs_store_ext_helper(const char * module_name, const char *pref_name, const char *pref_value)
278 {
279 module_t * module = NULL;
280 pref_t * pref = NULL;
281 gboolean pref_changed = TRUE;
282  
283 if ( ! prefs_is_registered_protocol(module_name))
284 return FALSE;
285  
286 module = prefs_find_module(module_name);
287 if ( ! module )
288 return FALSE;
289  
290 pref = prefs_find_preference(module, pref_name);
291  
292 if (!pref)
293 return FALSE;
294  
295 if ( pref->type == PREF_STRING )
296 {
297 g_free((void *)pref->stashed_val.string);
298 pref->stashed_val.string = (gchar *) g_strdup(pref_value);
299 /* unstash - taken from preferences_util */
300 if (strcmp(*pref->varp.string, pref->stashed_val.string) != 0)
301 {
302 pref_changed = TRUE;
303 g_free(*pref->varp.string);
304 *pref->varp.string = g_strdup(pref->stashed_val.string);
305 }
306 }
307  
308 return pref_changed;
309 }
310  
311 gboolean
312 prefs_store_ext(const char * module_name, const char *pref_name, const char *pref_value)
313 {
314 if ( prefs_store_ext_helper(module_name, pref_name, pref_value) )
315 {
316 prefs_main_write();
317 prefs_apply_all();
318 prefs_to_capture_opts();
319 return TRUE;
320 }
321  
322 return FALSE;
323 }
324  
325 gboolean
326 prefs_store_ext_multiple(const char * module, GHashTable * pref_values)
327 {
328 gboolean pref_changed = FALSE;
329 GList * keys = NULL;
330  
331 if ( ! prefs_is_registered_protocol(module))
332 return pref_changed;
333  
334 keys = g_hash_table_get_keys(pref_values);
335 if ( ! keys )
336 return pref_changed;
337  
338 while ( keys != NULL )
339 {
340 gchar * pref_name = (gchar *)keys->data;
341 gchar * pref_value = (gchar *) g_hash_table_lookup(pref_values, keys->data);
342  
343 if ( pref_name && pref_value )
344 {
345 if ( prefs_store_ext_helper(module, pref_name, pref_value) )
346 pref_changed = TRUE;
347 }
348 keys = g_list_next(keys);
349 }
350  
351 if ( pref_changed )
352 {
353 prefs_main_write();
354 prefs_apply_all();
355 prefs_to_capture_opts();
356 }
357  
358 return TRUE;
359 }
360  
361 gint
362 column_prefs_add_custom(gint fmt, const gchar *title, const gchar *custom_fields, gint custom_occurrence)
363 {
364 GList *clp;
365 fmt_data *cfmt, *last_cfmt;
366 gint colnr;
367  
368 cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
369 /*
370 * Because a single underscore is interpreted as a signal that the next character
371 * is going to be marked as accelerator for this header (i.e. is going to be
372 * shown underlined), escape it be inserting a second consecutive underscore.
373 */
374 cfmt->title = g_strdup(title);
375 cfmt->fmt = fmt;
376 cfmt->custom_fields = g_strdup(custom_fields);
377 cfmt->custom_occurrence = custom_occurrence;
378 cfmt->resolved = TRUE;
379  
380 colnr = g_list_length(prefs.col_list);
381  
382 if (custom_fields) {
383 cfmt->visible = TRUE;
384 clp = g_list_last(prefs.col_list);
385 last_cfmt = (fmt_data *) clp->data;
386 if (last_cfmt->fmt == COL_INFO) {
387 /* Last column is COL_INFO, add custom column before this */
388 colnr -= 1;
389 prefs.col_list = g_list_insert(prefs.col_list, cfmt, colnr);
390 } else {
391 prefs.col_list = g_list_append(prefs.col_list, cfmt);
392 }
393 } else {
394 cfmt->visible = FALSE; /* Will be set to TRUE in visible_toggled() when added to list */
395 prefs.col_list = g_list_append(prefs.col_list, cfmt);
396 }
397  
398 return colnr;
399 }
400  
401 void
402 column_prefs_remove_link(GList *col_link)
403 {
404 fmt_data *cfmt;
405  
406 if (!col_link || !col_link->data) return;
407  
408 cfmt = (fmt_data *) col_link->data;
409  
410 g_free(cfmt->title);
411 g_free(cfmt->custom_fields);
412 g_free(cfmt);
413 prefs.col_list = g_list_remove_link(prefs.col_list, col_link);
414 }
415  
416 void
417 column_prefs_remove_nth(gint col)
418 {
419 column_prefs_remove_link(g_list_nth(prefs.col_list, col));
420 }
421  
422 /*
423 * Editor modelines
424 *
425 * Local Variables:
426 * c-basic-offset: 2
427 * tab-width: 8
428 * indent-tabs-mode: nil
429 * End:
430 *
431 * ex: set shiftwidth=2 tabstop=8 expandtab:
432 * :indentSize=2:tabSize=8:noTabs=true:
433 */