Refactor range preference.
[metze/wireshark/wip.git] / epan / prefs-int.h
1 /* prefs-int.h
2  * Definitions for implementation of preference handling routines;
3  * used by "friends" of the preferences type.
4  *
5  * Wireshark - Network traffic analyzer
6  * By Gerald Combs <gerald@wireshark.org>
7  * Copyright 1998 Gerald Combs
8  *
9  * This program is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU General Public License
11  * as published by the Free Software Foundation; either version 2
12  * of the License, or (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22  */
23
24 #ifndef __PREFS_INT_H__
25 #define __PREFS_INT_H__
26
27 #ifdef __cplusplus
28 extern "C" {
29 #endif /* __cplusplus */
30
31 #include <stdio.h>
32 #include "ws_symbol_export.h"
33 #include <epan/wmem/wmem.h>
34
35 /**
36  *@file
37  */
38
39 struct pref_module {
40     const char *name;           /**< name of module */
41     const char *title;          /**< title of module (displayed in preferences list) */
42     const char *description;    /**< Description of module (displayed in preferences notebook) */
43     void (*apply_cb)(void);     /**< routine to call when preferences applied */
44     GList *prefs;               /**< list of its preferences */
45     struct pref_module *parent; /**< parent module */
46     wmem_tree_t *submodules;    /**< list of its submodules */
47     int numprefs;               /**< number of non-obsolete preferences */
48     gboolean prefs_changed;     /**< if TRUE, a preference has changed since we last checked */
49     gboolean obsolete;          /**< if TRUE, this is a module that used to
50                                  * exist but no longer does
51                                  */
52     gboolean use_gui;           /**< Determines whether or not the module will use the generic
53                                   * GUI interface/APIs with the preference value or if its own
54                                   * independent GUI will be provided.  This allows all preferences
55                                   * to have a common API for reading/writing, but not require them to
56                                   * use simple GUI controls to change the options.  In general, the "general"
57                                   * Wireshark preferences should have this set to FALSE, while the protocol
58                                   * modules will have this set to TRUE */
59 };
60
61 typedef struct {
62     module_t *module;
63     FILE     *pf;
64 } write_pref_arg_t;
65
66 /**
67  * Module used for protocol preferences.
68  * With MSVC and a libwireshark.dll, we need a special declaration.
69  */
70 WS_DLL_PUBLIC module_t *protocols_module;
71
72 typedef void (*pref_custom_free_cb) (pref_t* pref);
73 typedef void (*pref_custom_reset_cb) (pref_t* pref);
74 typedef prefs_set_pref_e (*pref_custom_set_cb) (pref_t* pref, const gchar* value, gboolean* changed);
75 /* typedef void (*pref_custom_write_cb) (pref_t* pref, write_pref_arg_t* arg); Deprecated. */
76 /* pref_custom_type_name_cb should return NULL for internal / hidden preferences. */
77 typedef const char * (*pref_custom_type_name_cb) (void);
78 typedef char * (*pref_custom_type_description_cb) (void);
79 typedef gboolean (*pref_custom_is_default_cb) (pref_t* pref);
80 typedef char * (*pref_custom_to_str_cb) (pref_t* pref, gboolean default_val);
81
82 /** Structure to hold callbacks for PREF_CUSTOM type */
83 struct pref_custom_cbs {
84     pref_custom_free_cb free_cb;
85     pref_custom_reset_cb reset_cb;
86     pref_custom_set_cb set_cb;
87     /* pref_custom_write_cb write_cb; Deprecated. */
88     pref_custom_type_name_cb type_name_cb;
89     pref_custom_type_description_cb type_description_cb;
90     pref_custom_is_default_cb is_default_cb;
91     pref_custom_to_str_cb to_str_cb;
92 };
93
94 /**
95  * PREF_OBSOLETE is used for preferences that a module used to support
96  * but no longer supports; we give different error messages for them.
97  */
98 #define PREF_UINT        (1u << 0)
99 #define PREF_BOOL        (1u << 1)
100 #define PREF_ENUM        (1u << 2)
101 #define PREF_STRING      (1u << 3)
102 #define PREF_RANGE       (1u << 4)
103 #define PREF_STATIC_TEXT (1u << 5)
104 #define PREF_UAT         (1u << 6)
105 #define PREF_FILENAME    (1u << 7)
106 #define PREF_COLOR       (1u << 8) /* XXX - These are only supported for "internal" (non-protocol) */
107 #define PREF_CUSTOM      (1u << 9) /* use and not as a generic protocol preference */
108 #define PREF_OBSOLETE    (1u << 10)
109 #define PREF_DIRNAME     (1u << 11)
110 #define PREF_DECODE_AS_UINT  (1u << 12)     /* XXX - These are only supported for "internal" (non-protocol) */
111 #define PREF_DECODE_AS_RANGE (1u << 13) /* use and not as a generic protocol preference */
112
113 typedef enum {
114         GUI_ALL,
115         GUI_GTK,
116         GUI_QT
117 } gui_type_t;
118
119 /** Struct to hold preference data */
120 struct preference {
121     const char *name;                /**< name of preference */
122     const char *title;               /**< title to use in GUI */
123     const char *description;         /**< human-readable description of preference */
124     int ordinal;                     /**< ordinal number of this preference */
125     int type;                        /**< type of that preference */
126     gui_type_t gui;                  /**< type of the GUI (QT, GTK or both) the preference is registered for */
127     union {                          /* The Qt preference code assumes that these will all be pointers (and unique) */
128         guint *uint;
129         gboolean *boolp;
130         gint *enump;
131         char **string;
132         range_t **range;
133         struct epan_uat* uat;
134         color_t *colorp;
135         GList** list;
136     } varp;                          /**< pointer to variable storing the value */
137     union {
138         guint uint;
139         gboolean boolval;
140         gint enumval;
141         char *string;
142         range_t *range;
143         color_t color;
144         GList* list;
145     } stashed_val;                     /**< original value, when editing from the GUI */
146     union {
147         guint uint;
148         gboolean boolval;
149         gint enumval;
150         char *string;
151         range_t *range;
152         color_t color;
153         GList* list;
154     } default_val;                   /**< the default value of the preference */
155     union {
156       guint base;                    /**< input/output base, for PREF_UINT */
157       guint32 max_value;             /**< maximum value of a range */
158       struct {
159         const enum_val_t *enumvals;  /**< list of name & values */
160         gboolean radio_buttons;      /**< TRUE if it should be shown as
161                                           radio buttons rather than as an
162                                           option menu or combo box in
163                                           the preferences tab */
164       } enum_info;                   /**< for PREF_ENUM */
165     } info;                          /**< display/text file information */
166     struct pref_custom_cbs custom_cbs;   /**< for PREF_CUSTOM */
167     void    *control;                /**< handle for GUI control for this preference. GTK+ only? */
168 };
169
170 /* read_prefs_file: read in a generic config file and do a callback to */
171 /* pref_set_pair_fct() for every key/value pair found */
172 /**
173  * Given a string of the form "<pref name>:<pref value>", as might appear
174  * as an argument to a "-o" option, parse it and set the preference in
175  * question.
176  * @return an indication of whether it succeeded or failed
177  * in some fashion.
178  */
179 typedef prefs_set_pref_e (*pref_set_pair_cb) (gchar *key, const gchar *value, void *private_data, gboolean return_range_errors);
180
181 /** Set the value of a string-like preference. */
182 WS_DLL_PUBLIC
183 void
184 prefs_set_string_like_value(pref_t *pref, const gchar *value, gboolean *changed);
185
186 /** Set the value of a range preference.  Return FALSE on error, TRUE otherwise. */
187 WS_DLL_PUBLIC
188 gboolean
189 prefs_set_range_value(pref_t *pref, const gchar *value, gboolean *changed);
190
191 WS_DLL_PUBLIC
192 gboolean
193 prefs_set_stashed_range_value(pref_t *pref, const gchar *value);
194
195 WS_DLL_PUBLIC
196 gboolean
197 prefs_set_stashed_range(pref_t *pref, range_t *value);
198
199 WS_DLL_PUBLIC
200 range_t *
201 prefs_get_stashed_range(pref_t *pref);
202
203 /** Add a range value of a range preference. */
204 WS_DLL_PUBLIC
205 void
206 prefs_range_add_value(pref_t *pref, guint32 val);
207
208 /** Remove a range value of a range preference. */
209 WS_DLL_PUBLIC
210 void
211 prefs_range_remove_value(pref_t *pref, guint32 val);
212
213 /** Set the value of an enum preference. */
214 WS_DLL_PUBLIC
215 void
216 prefs_set_enum_value(pref_t *pref, const gchar *value, gboolean *changed);
217
218 /** read the preferences file (or similar) and call the callback
219  * function to set each key/value pair found
220  */
221 WS_DLL_PUBLIC
222 int
223 read_prefs_file(const char *pf_path, FILE *pf, pref_set_pair_cb pref_set_pair_fct, void *private_data);
224
225 WS_DLL_PUBLIC
226 gboolean
227 prefs_pref_is_default(pref_t *pref);
228
229 /** "Stash" a preference.
230  * Copy a preference to its stashed value. Can be called from prefs_pref_foreach().
231  *
232  * @param pref A preference.
233  * @param unused unused
234  */
235 WS_DLL_PUBLIC
236 guint pref_stash(pref_t *pref, gpointer unused _U_);
237
238 typedef struct pref_unstash_data
239 {
240     /* Used to set prefs_changed member to TRUE if the preference
241        differs from its stashed values. Also used by "decode as" types
242        to look up dissector short name */
243     module_t *module;
244     /* Qt uses stashed values to then "applies" them
245       during unstash.  Use this flag for that behavior */
246     gboolean handle_decode_as;
247 } pref_unstash_data_t;
248
249 /** "Unstash" a preference.
250  * Set a preference to its stashed value. Can be called from prefs_pref_foreach().
251  *
252  * @param pref A preference.
253  * @param unstash_data_p A pointer to a pref_unstash_data_t structure.
254  *
255  * @return Always returns 0.
256  */
257 WS_DLL_PUBLIC
258 guint pref_unstash(pref_t *pref, gpointer unstash_data_p);
259
260 /** Clean up a stashed preference.
261  * Can be called from prefs_pref_foreach().
262  *
263  * @param pref A preference.
264  * @param unused unused
265  *
266  * @return Always returns 0.
267  */
268 WS_DLL_PUBLIC
269 guint pref_clean_stash(pref_t *pref, gpointer unused _U_);
270
271 /** Set a stashed preference to its default value.
272  *
273  *@param pref A preference.
274  */
275 WS_DLL_PUBLIC
276 void reset_stashed_pref(pref_t *pref);
277
278 /** Convert a string list preference to a preference string.
279  *
280  * Given a GList of gchar pointers, create a quoted, comma-separated
281  * string. Should be used with prefs_get_string_list() and
282  * prefs_clear_string_list().
283  *
284  * @param sl String list.
285  * @return Quoted, joined, and wrapped string. May be empty.
286  */
287 WS_DLL_PUBLIC
288 char *
289 join_string_list(GList *sl);
290
291 #ifdef __cplusplus
292 }
293 #endif /* __cplusplus */
294
295 #endif /* prefs-int.h */