On Windows, put Ethereal configuration files under the "Application
[obnox/wireshark/wip.git] / gtk / colors.c
1 /* colors.c
2  * Definitions for color structures and routines
3  *
4  * $Id: colors.c,v 1.14 2001/10/24 06:13:06 guy Exp $
5  *
6  * Ethereal - Network traffic analyzer
7  * By Gerald Combs <gerald@ethereal.com>
8  * Copyright 1998 Gerald Combs
9  * 
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.
14  * 
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.
19  * 
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., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
23  */
24
25 #ifdef HAVE_CONFIG_H
26 # include "config.h"
27 #endif
28
29 #include <gtk/gtk.h>
30
31 #include <stdlib.h>
32 #include <stdio.h>
33 #include <string.h>
34
35 #include <errno.h>
36
37 #ifdef HAVE_SYS_TYPES_H
38 #include <sys/types.h>
39 #endif
40
41 #include <epan/filesystem.h>
42
43 #include "gtk/main.h"
44 #include "packet.h"
45 #include "colors.h"
46 #include "file.h"
47 #include "dfilter/dfilter.h"
48 #include "simple_dialog.h"
49
50 extern capture_file cf;
51
52 static gboolean read_filters(colfilter *filter);
53
54 GSList *filter_list;
55
56 static GdkColormap*     sys_cmap;
57 static GdkColormap*     our_cmap = NULL;
58
59 GdkColor        WHITE = { 0, 65535, 65535, 65535 };
60 GdkColor        BLACK = { 0, 0, 0, 0 };
61
62 /* This structure is used to allow you to compile in default colors if
63  * you wish.  They can be later changed by a user.
64  */
65 #ifdef READ_DEFAULT_COLOR_LIST
66 struct _default_colors {
67         gchar* proto;
68         gchar* color; /* background only */
69 } default_colors[]  = {
70         {"arp", "green2"},
71         {"ip",  "light red"},
72         {"tcp",  "light blue"}
73 };
74 #endif
75
76 colfilter *
77 colfilter_new(void)
78 {
79   colfilter *filter;
80   gboolean got_white, got_black;
81 #ifdef READ_DEFAULT_COLOR_LIST
82   color_filter_t *colorf;
83   gint i;
84   GdkColor color;
85 #endif
86
87   filter = (colfilter *)g_malloc(sizeof(colfilter));
88   filter->num_of_filters = 0;
89
90   sys_cmap = gdk_colormap_get_system();
91
92   /* Allocate "constant" colors. */
93   got_white = get_color(&WHITE);
94   got_black = get_color(&BLACK);
95
96   /* Got milk? */
97   if (!got_white) {
98     if (!got_black)
99       simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate colors black or white.");
100     else
101       simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate color white.");
102   } else {
103     if (!got_black)
104       simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate color black.");
105   }
106
107 #ifdef READ_DEFAULT_COLOR_LIST
108   /* Now process defaults */
109   for (i = 0 ; i < sizeof default_colors/sizeof (struct _default_colors); i++){
110         gdk_color_parse(default_colors[i].color, &color);
111         
112         if( !get_color(&color)){
113                 /* oops */
114                 simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate color %s.",
115                     default_colors[i].color);
116         }
117
118         colorf = new_color_filter(filter, default_colors[i].proto,
119             default_colors[i].proto);
120         colorf->bg_color = color;
121
122         if (!dfilter_compile(default_colors[i].proto,
123             &colorf->c_colorfilter)) {
124                 simple_dialog(ESD_TYPE_WARN, NULL,
125                   "Cannot compile default color filter %s.\n%s",
126                   default_colors[i].proto, dfilter_error_msg);
127                 /* should reject this filter */
128         }
129         filter->num_of_filters++;
130   }
131 #endif
132   read_filters(filter);
133   return filter;
134 }
135
136 color_filter_t *
137 new_color_filter(colfilter *filters, gchar *name, gchar *filter_string)
138 {
139         color_filter_t *colorf;
140
141         colorf = (color_filter_t *)g_malloc(sizeof (color_filter_t));
142         colorf->filter_name = g_strdup(name);
143         colorf->filter_text = g_strdup(filter_string);
144         colorf->bg_color = WHITE;
145         colorf->fg_color = BLACK;
146         colorf->c_colorfilter = NULL;
147         colorf->edit_dialog = NULL;
148         filter_list = g_slist_append(filter_list, colorf);
149         return colorf;
150 }
151
152 void
153 delete_color_filter(color_filter_t *colorf)
154 {
155         if (colorf->filter_name != NULL)
156           g_free(colorf->filter_name);
157         if (colorf->filter_text != NULL)
158           g_free(colorf->filter_text);
159         if (colorf->c_colorfilter != NULL)
160           dfilter_free(colorf->c_colorfilter);
161         filter_list = g_slist_remove(filter_list, colorf);
162         g_free(colorf);
163 }
164
165 /*
166  * Get the pathname of the preferences file.
167  */
168 static const char *
169 get_colorfilter_file_path(void)
170 {
171   static gchar *cf_path = NULL;
172   static const char fname[] = "colorfilters";
173
174   if (cf_path == NULL) {
175     cf_path = (gchar *) g_malloc(strlen(get_persconffile_dir()) +
176       sizeof fname + 1);
177     sprintf(cf_path, "%s" G_DIR_SEPARATOR_S "%s", get_persconffile_dir(),
178       fname);
179   }
180   return cf_path;
181 }
182
183 static gboolean
184 read_filters(colfilter *filter)
185 {
186         /* TODO: Lots more syntax checking on the file */
187         /* I hate these fixed length names! TODO: make more dynamic */
188         /* XXX - buffer overflow possibility here */
189         gchar name[256],filter_exp[256], buf[1024];
190         guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
191         GdkColor fg_color, bg_color;
192         color_filter_t *colorf;
193         int i;
194         const gchar *path;
195         FILE *f;
196         dfilter_t *temp_dfilter;
197
198         /* decide what file to open (from dfilter code) */
199
200         /* should only be called by colors_init.
201          */
202         if(filter == NULL)
203                 return FALSE;
204         /* we have a clist */
205
206         path = get_colorfilter_file_path();
207         if ((f = fopen(path, "r")) == NULL) {
208           if (errno != ENOENT) {
209             simple_dialog(ESD_TYPE_CRIT, NULL,
210               "Could not open filter file\n\"%s\": %s.", path,
211               strerror(errno));
212           }
213           return FALSE;
214         }
215
216         i = 0;
217
218         do{
219           if(!fgets(buf,sizeof buf, f))
220                 break;
221                 
222           if(strspn( buf," \t") == (size_t)((strchr(buf,'*') - buf))){
223                 /* leading # comment */
224                 continue;
225           }
226
227           /* we get the @ delimiter.  It is not in any strings */
228           if(sscanf(buf," @%[^@]@%[^@]@[%hu,%hu,%hu][%hu,%hu,%hu]",
229                 name, filter_exp, &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 8){
230                 /* we got a filter */
231
232             if(!dfilter_compile(filter_exp, &temp_dfilter)) {
233                 simple_dialog(ESD_TYPE_CRIT, NULL,
234                  "Could not compile color filter %s from saved filters.\n%s",
235                  name, dfilter_error_msg);
236                 continue;
237             }
238             colorf = new_color_filter(filter, name, filter_exp);
239             colorf->c_colorfilter = temp_dfilter;
240             filter->num_of_filters++;
241             fg_color.red = fg_r;
242             fg_color.green = fg_g;
243             fg_color.blue = fg_b;
244             bg_color.red = bg_r;
245             bg_color.green = bg_g;
246             bg_color.blue = bg_b;
247             if( !get_color(&fg_color)){
248                 /* oops */
249                 simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate fg color specified"
250                   "in input file for %s.", name);
251
252                 i++;
253                 continue;
254             }
255             if( !get_color(&bg_color)){
256                 /* oops */
257                 simple_dialog(ESD_TYPE_WARN, NULL, "Could not allocate bg color specified"
258                   "in input file for %s.", name);
259                 i++;
260                 continue;
261             }
262
263             colorf->bg_color = bg_color;
264             colorf->fg_color = fg_color;
265             i++;
266           }    /* if sscanf */
267         } while( !feof(f));
268         return TRUE;
269 }
270
271 static void
272 write_filter(gpointer filter_arg, gpointer file_arg)
273 {
274         color_filter_t *colorf = filter_arg;
275         FILE *f = file_arg;
276
277         fprintf(f,"@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
278             colorf->filter_name,
279             colorf->filter_text,
280             colorf->bg_color.red,
281             colorf->bg_color.green,
282             colorf->bg_color.blue,
283             colorf->fg_color.red,
284             colorf->fg_color.green,
285             colorf->fg_color.blue);
286 }
287         
288 gboolean
289 write_filters(colfilter *filter)
290 {
291         gchar *pf_dir_path;
292         const gchar *path;
293         FILE *f;
294
295         /* Create the directory that holds personal configuration files,
296            if necessary.  */
297         if (create_persconffile_dir(&pf_dir_path) == -1) {
298           simple_dialog(ESD_TYPE_WARN, NULL,
299                 "Can't create directory\n\"%s\"\nfor color files: %s.",
300                 pf_dir_path, strerror(errno));
301           g_free(pf_dir_path);
302           return FALSE;
303         }
304
305         path = get_colorfilter_file_path();
306         if ((f = fopen(path, "w+")) == NULL) {
307           simple_dialog(ESD_TYPE_CRIT, NULL,
308                 "Could not open\n%s\nfor writing: %s.",
309                 path, strerror(errno));
310           return FALSE;
311         }
312         fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Ethereal\n");
313         g_slist_foreach(filter_list, write_filter, f);
314         fclose(f);
315         return TRUE;
316 }
317
318 gboolean
319 get_color (GdkColor *new_color)
320 {
321     GdkVisual *pv;
322
323     if (!our_cmap) {
324         if ( !gdk_colormap_alloc_color (sys_cmap, new_color, FALSE, TRUE)) {
325             pv = gdk_visual_get_best();
326             if ( !(our_cmap = gdk_colormap_new(pv, TRUE)))
327                 simple_dialog(ESD_TYPE_WARN, NULL, "Could not create new colormap");
328         } else
329             return (TRUE);
330     }
331     return ( gdk_colormap_alloc_color ( our_cmap, new_color, FALSE, TRUE) );
332 }