aboutsummaryrefslogtreecommitdiffstats
path: root/ui/preference_utils.c
blob: ca0a70c7a33e40b5b392cadb4bf58e2c6f161659 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
/* preference_utils.h
 * Routines for handling preferences
 *
 * Wireshark - Network traffic analyzer
 * By Gerald Combs <gerald@wireshark.org>
 * Copyright 1998 Gerald Combs
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "config.h"

#include <errno.h>


#include <epan/column.h>
#include <wsutil/filesystem.h>
#include <epan/prefs.h>
#include <epan/prefs-int.h>

#ifdef HAVE_LIBPCAP
#include "capture_opts.h"
#include "ui/capture_globals.h"
#endif

#include "ui/preference_utils.h"
#include "ui/simple_dialog.h"

guint
pref_stash(pref_t *pref, gpointer unused _U_)
{
  switch (pref->type) {

  case PREF_UINT:
    pref->stashed_val.uint = *pref->varp.uint;
    break;

  case PREF_BOOL:
    pref->stashed_val.boolval = *pref->varp.boolp;
    break;

  case PREF_ENUM:
    pref->stashed_val.enumval = *pref->varp.enump;
    break;

  case PREF_STRING:
  case PREF_FILENAME:
  case PREF_DIRNAME:
    g_free(pref->stashed_val.string);
    pref->stashed_val.string = g_strdup(*pref->varp.string);
    break;

  case PREF_RANGE:
    g_free(pref->stashed_val.range);
    pref->stashed_val.range = range_copy(*pref->varp.range);
    break;

  case PREF_COLOR:
    pref->stashed_val.color = *pref->varp.colorp;
    break;

  case PREF_STATIC_TEXT:
  case PREF_UAT:
  case PREF_CUSTOM:
    break;

  case PREF_OBSOLETE:
    g_assert_not_reached();
    break;
  }
  return 0;
}

guint
pref_unstash(pref_t *pref, gpointer changed_p)
{
  gboolean *pref_changed_p = (gboolean *)changed_p;

  /* Revert the preference to its saved value. */
  switch (pref->type) {

  case PREF_UINT:
    if (*pref->varp.uint != pref->stashed_val.uint) {
      *pref_changed_p = TRUE;
      *pref->varp.uint = pref->stashed_val.uint;
    }
    break;

  case PREF_BOOL:
    if (*pref->varp.boolp != pref->stashed_val.boolval) {
      *pref_changed_p = TRUE;
      *pref->varp.boolp = pref->stashed_val.boolval;
    }
    break;

  case PREF_ENUM:
    if (*pref->varp.enump != pref->stashed_val.enumval) {
      *pref_changed_p = TRUE;
      *pref->varp.enump = pref->stashed_val.enumval;
    }
    break;

  case PREF_STRING:
  case PREF_FILENAME:
  case PREF_DIRNAME:
    if (strcmp(*pref->varp.string, pref->stashed_val.string) != 0) {
      *pref_changed_p = TRUE;
      g_free(*pref->varp.string);
      *pref->varp.string = g_strdup(pref->stashed_val.string);
    }
    break;

  case PREF_RANGE:
    if (!ranges_are_equal(*pref->varp.range, pref->stashed_val.range)) {
      *pref_changed_p = TRUE;
      g_free(*pref->varp.range);
      *pref->varp.range = range_copy(pref->stashed_val.range);
    }
    break;

  case PREF_COLOR:
    *pref->varp.colorp = pref->stashed_val.color;
    break;

  case PREF_STATIC_TEXT:
  case PREF_UAT:
  case PREF_CUSTOM:
    break;

  case PREF_OBSOLETE:
    g_assert_not_reached();
    break;
  }
  return 0;
}

void
reset_stashed_pref(pref_t *pref) {
  switch (pref->type) {

  case PREF_UINT:
    pref->stashed_val.uint = pref->default_val.uint;
    break;

  case PREF_BOOL:
    pref->stashed_val.boolval = pref->default_val.boolval;
    break;

  case PREF_ENUM:
    pref->stashed_val.enumval = pref->default_val.enumval;
    break;

  case PREF_STRING:
  case PREF_FILENAME:
  case PREF_DIRNAME:
    g_free(pref->stashed_val.string);
    pref->stashed_val.string = g_strdup(pref->default_val.string);
    break;

  case PREF_RANGE:
    g_free(pref->stashed_val.range);
    pref->stashed_val.range = range_copy(pref->default_val.range);
    break;

  case PREF_COLOR:
    memcpy(&pref->stashed_val.color, &pref->default_val.color, sizeof(color_t));
    break;

  case PREF_STATIC_TEXT:
  case PREF_UAT:
  case PREF_CUSTOM:
    break;

  case PREF_OBSOLETE:
    g_assert_not_reached();
    break;
  }
}

guint
pref_clean_stash(pref_t *pref, gpointer unused _U_)
{
  switch (pref->type) {

  case PREF_UINT:
    break;

  case PREF_BOOL:
    break;

  case PREF_ENUM:
    break;

  case PREF_STRING:
  case PREF_FILENAME:
  case PREF_DIRNAME:
    if (pref->stashed_val.string != NULL) {
      g_free(pref->stashed_val.string);
      pref->stashed_val.string = NULL;
    }
    break;

  case PREF_RANGE:
    if (pref->stashed_val.range != NULL) {
      g_free(pref->stashed_val.range);
      pref->stashed_val.range = NULL;
    }
    break;

  case PREF_STATIC_TEXT:
  case PREF_UAT:
  case PREF_COLOR:
  case PREF_CUSTOM:
    break;

  case PREF_OBSOLETE:
    g_assert_not_reached();
    break;
  }
  return 0;
}

/* Fill in capture options with values from the preferences */
void
prefs_to_capture_opts(void)
{
#ifdef HAVE_LIBPCAP
  /* Set promiscuous mode from the preferences setting. */
  /* the same applies to other preferences settings as well. */
    global_capture_opts.default_options.promisc_mode = prefs.capture_prom_mode;
    global_capture_opts.use_pcapng                   = prefs.capture_pcap_ng;
    global_capture_opts.show_info                    = prefs.capture_show_info; /* GTK+ only */
    global_capture_opts.real_time_mode               = prefs.capture_real_time;
    auto_scroll_live                                 = prefs.capture_auto_scroll;
#endif /* HAVE_LIBPCAP */
}

void
prefs_main_write(void)
{
  int   err;
  char *pf_dir_path;
  char *pf_path;

  /* Create the directory that holds personal configuration files, if
     necessary.  */
  if (create_persconffile_dir(&pf_dir_path) == -1) {
    simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                  "Can't create directory\n\"%s\"\nfor preferences file: %s.", pf_dir_path,
                  g_strerror(errno));
    g_free(pf_dir_path);
  } else {
    /* Write the preferencs out. */
    err = write_prefs(&pf_path);
    if (err != 0) {
      simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "Can't open preferences file\n\"%s\": %s.", pf_path,
                    g_strerror(err));
      g_free(pf_path);
    }
  }
}

static gboolean
prefs_store_ext_helper(const char * module_name, const char *pref_name, const char *pref_value)
{
  module_t * module = NULL;
  pref_t * pref = NULL;
  gboolean pref_changed = TRUE;

  if ( ! prefs_is_registered_protocol(module_name))
    return FALSE;

  module = prefs_find_module(module_name);
  if ( ! module )
    return FALSE;

  pref = prefs_find_preference(module, pref_name);

  if (!pref)
    return FALSE;

  if ( pref->type == PREF_STRING )
  {
    g_free((void *)pref->stashed_val.string);
    pref->stashed_val.string = (gchar *) g_strdup(pref_value);
    /* unstash - taken from preferences_util */
    if (strcmp(*pref->varp.string, pref->stashed_val.string) != 0)
    {
      pref_changed = TRUE;
      g_free(*pref->varp.string);
      *pref->varp.string = g_strdup(pref->stashed_val.string);
    }
  }

  return pref_changed;
}

gboolean
prefs_store_ext(const char * module_name, const char *pref_name, const char *pref_value)
{
  if ( prefs_store_ext_helper(module_name, pref_name, pref_value) )
  {
    prefs_main_write();
    prefs_apply_all();
    prefs_to_capture_opts();
    return TRUE;
  }

  return FALSE;
}

gboolean
prefs_store_ext_multiple(const char * module, GHashTable * pref_values)
{
  gboolean pref_changed = FALSE;
  GList * keys = NULL;

  if ( ! prefs_is_registered_protocol(module))
    return pref_changed;

  keys = g_hash_table_get_keys(pref_values);
  if ( ! keys )
    return pref_changed;

  while ( keys != NULL )
  {
    gchar * pref_name = (gchar *)keys->data;
    gchar * pref_value = (gchar *) g_hash_table_lookup(pref_values, keys->data);

    if ( pref_name && pref_value )
    {
      if ( prefs_store_ext_helper(module, pref_name, pref_value) )
        pref_changed = TRUE;
    }
    keys = g_list_next(keys);
  }

  if ( pref_changed )
  {
    prefs_main_write();
    prefs_apply_all();
    prefs_to_capture_opts();
  }

  return TRUE;
}

gint
column_prefs_add_custom(gint fmt, const gchar *title, const gchar *custom_fields, gint custom_occurrence)
{
    GList *clp;
    fmt_data *cfmt, *last_cfmt;
    gint colnr;

    cfmt = (fmt_data *) g_malloc(sizeof(fmt_data));
    /*
     * Because a single underscore is interpreted as a signal that the next character
     * is going to be marked as accelerator for this header (i.e. is going to be
     * shown underlined), escape it be inserting a second consecutive underscore.
     */
    cfmt->title = g_strdup(title);
    cfmt->fmt = fmt;
    cfmt->custom_fields = g_strdup(custom_fields);
    cfmt->custom_occurrence = custom_occurrence;
    cfmt->resolved = TRUE;

    colnr = g_list_length(prefs.col_list);

    if (custom_fields) {
        cfmt->visible = TRUE;
        clp = g_list_last(prefs.col_list);
        last_cfmt = (fmt_data *) clp->data;
        if (last_cfmt->fmt == COL_INFO) {
            /* Last column is COL_INFO, add custom column before this */
            colnr -= 1;
            prefs.col_list = g_list_insert(prefs.col_list, cfmt, colnr);
        } else {
            prefs.col_list = g_list_append(prefs.col_list, cfmt);
        }
    } else {
        cfmt->visible = FALSE;  /* Will be set to TRUE in visible_toggled() when added to list */
        prefs.col_list = g_list_append(prefs.col_list, cfmt);
    }

    return colnr;
}

void
column_prefs_remove_link(GList *col_link)
{
    fmt_data *cfmt;

    if (!col_link || !col_link->data) return;

    cfmt = (fmt_data *) col_link->data;

    g_free(cfmt->title);
    g_free(cfmt->custom_fields);
    g_free(cfmt);
    prefs.col_list = g_list_remove_link(prefs.col_list, col_link);
}

void
column_prefs_remove_nth(gint col)
{
    column_prefs_remove_link(g_list_nth(prefs.col_list, col));
}

/*
 * Editor modelines
 *
 * Local Variables:
 * c-basic-offset: 2
 * tab-width: 8
 * indent-tabs-mode: nil
 * End:
 *
 * ex: set shiftwidth=2 tabstop=8 expandtab:
 * :indentSize=2:tabSize=8:noTabs=true:
 */