Make it possible to "move" a filter around the temporary coloring rules
[metze/wireshark/wip.git] / color_filters.c
1 /* color_filters.c
2  * Routines for color filters
3  *
4  * $Id$
5  *
6  * Wireshark - Network traffic analyzer
7  * By Gerald Combs <gerald@wireshark.org>
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  * Updated 1 Dec 10 jjm
26  */
27  
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <glib.h>
33 #include <ctype.h>
34 #include <string.h>
35
36 #include <epan/filesystem.h>
37 #include "file_util.h"
38
39 #include <epan/packet.h>
40 #include "color.h"
41 #include "color_filters.h"
42 #include "file.h"
43 #include <epan/dfilter/dfilter.h>
44 #include "simple_dialog.h"
45 #include "ui_util.h"
46 #include <epan/prefs.h>
47
48 #define RED_COMPONENT(x)   (guint16) (((((x) >> 16) & 0xff) * 65535 / 255))
49 #define GREEN_COMPONENT(x) (guint16) (((((x) >>  8) & 0xff) * 65535 / 255))
50 #define BLUE_COMPONENT(x)  (guint16) ( (((x)        & 0xff) * 65535 / 255))
51
52 static gboolean read_users_filters(GSList **cfl);
53
54 /* the currently active filters */
55 static GSList *color_filter_list = NULL;
56
57 /* keep "old" deleted filters in this list until 
58  * the dissection no longer needs them (e.g. file is closed) */
59 static GSList *color_filter_deleted_list = NULL;
60 static GSList *color_filter_valid_list = NULL;
61
62 /* Color Filters can en-/disabled. */
63 gboolean filters_enabled = TRUE;
64
65
66 /* Create a new filter */
67 color_filter_t *
68 color_filter_new(const gchar *name,    /* The name of the filter to create */
69                  const gchar *filter_string, /* The string representing the filter */
70                  color_t *bg_color,    /* The background color */
71                  color_t *fg_color,    /* The foreground color */
72                  gboolean disabled)     /* Is the filter disabled? */
73 {
74         color_filter_t *colorf;
75
76         colorf = g_malloc(sizeof (color_filter_t));
77         colorf->filter_name = g_strdup(name);
78         colorf->filter_text = g_strdup(filter_string);
79         colorf->bg_color = *bg_color;
80         colorf->fg_color = *fg_color;
81         colorf->disabled = disabled;
82         colorf->c_colorfilter = NULL;
83         colorf->edit_dialog = NULL;
84         colorf->selected = FALSE;
85         return colorf;
86 }
87
88 /* Add ten empty (temporary) colorfilters for easy coloring */
89 void
90 color_filters_add_tmp(GSList **cfl)
91 {
92         gchar  *name = NULL;
93         guint32 i;
94         gchar** bg_colors;
95         gchar** fg_colors;
96         unsigned long int cval;
97         color_t bg_color, fg_color;
98         color_filter_t *colorf;
99
100         g_assert(strlen(prefs.gui_colorized_fg)==69);
101         g_assert(strlen(prefs.gui_colorized_bg)==69);
102         fg_colors = g_strsplit(prefs.gui_colorized_fg, ",", -1);
103         bg_colors = g_strsplit(prefs.gui_colorized_bg, ",", -1);
104
105         for ( i=1 ; i<=10 ; i++ ) {
106                 name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
107
108                 /* retrieve background and foreground colors */
109                 cval = strtoul(fg_colors[i-1], NULL, 16);
110                 initialize_color(&fg_color, RED_COMPONENT(cval),
111                                             GREEN_COMPONENT(cval),
112                                             BLUE_COMPONENT(cval) );
113                 cval = strtoul(bg_colors[i-1], NULL, 16);
114                 initialize_color(&bg_color, RED_COMPONENT(cval),
115                                             GREEN_COMPONENT(cval),
116                                             BLUE_COMPONENT(cval) );
117                 colorf = color_filter_new(name, NULL, &bg_color, &fg_color, TRUE);
118                 colorf->filter_text = g_strdup("frame");
119                 colorf->c_colorfilter = NULL;
120                 *cfl = g_slist_append(*cfl, colorf);
121
122                 g_free(name);
123         }
124
125         g_strfreev(fg_colors);
126         g_strfreev(bg_colors);
127
128         return;
129 }
130
131 static gint
132 color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
133 {
134         color_filter_t *colorf = (color_filter_t *)arg1;
135         gchar          *name   = (gchar *)arg2;
136
137         return (strstr(colorf->filter_name, name)==NULL) ? -1 : 0 ;
138 }
139
140
141 /* Set the filter off a temporary colorfilters and enable it */
142 void
143 color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
144 {
145         gchar  *name = NULL;
146         gchar  *tmpfilter = NULL;
147         GSList *cfl;
148         color_filter_t *colorf;
149         dfilter_t      *compiled_filter;
150         guint8 i;
151
152         /* Go through the tomporary filters and look for the same filter string.
153          * If found, clear it so that a filter can be "moved" up and down the list
154          */
155         for ( i=1 ; i<=10 ; i++ ) {
156                 /* If we need to reset the temporary filter (filter==NULL), don't look 
157                  * for other rules with the same filter string
158                  */
159                 if( i!=filt_nr && filter==NULL ) 
160                         continue;
161
162                 name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
163                 cfl = g_slist_find_custom(color_filter_list, (gpointer *) name, color_filters_find_by_name_cb);
164                 colorf = cfl->data;
165
166                 /* Only change the filter rule if this is the rule to change or if
167                  * a matching filter string has been found
168                  */
169                 if(colorf && ( (i==filt_nr) || (strstr(filter,colorf->filter_text)!=NULL) ) ) {
170                         /* set filter string to "frame" if we are resetting the rules
171                          * or if we found a matching filter string which need to be cleared
172                          */
173                         tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
174                         if (!dfilter_compile(tmpfilter, &compiled_filter)) {
175                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
176                                     "Could not compile color filter name: \"%s\""
177                                     " text: \"%s\".\n%s", name, filter, dfilter_error_msg);
178                         } else {
179                                 if (colorf->filter_text != NULL)
180                                         g_free(colorf->filter_text);
181                                 if (colorf->c_colorfilter != NULL)
182                                         dfilter_free(colorf->c_colorfilter);
183                                 colorf->filter_text = g_strdup(tmpfilter);
184                                 colorf->c_colorfilter = compiled_filter;
185                                 colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
186                         }
187                 }
188                 g_free(name);
189         }
190         return;
191 }
192
193 /* Reset the temporary colorfilters */
194 void
195 color_filters_reset_tmp()
196 {
197         guint8 i;
198
199         for ( i=1 ; i<=10 ; i++ ) {
200             color_filters_set_tmp(i, NULL, TRUE);
201         }
202         return;
203 }
204
205 /* delete the specified filter */
206 void
207 color_filter_delete(color_filter_t *colorf)
208 {
209         if (colorf->filter_name != NULL)
210                 g_free(colorf->filter_name);
211         if (colorf->filter_text != NULL)
212                 g_free(colorf->filter_text);
213         if (colorf->c_colorfilter != NULL)
214                 dfilter_free(colorf->c_colorfilter);
215         g_free(colorf);
216 }
217
218 /* delete the specified filter (called from g_slist_foreach) */
219 static void
220 color_filter_delete_cb(gpointer filter_arg, gpointer unused _U_)
221 {
222         color_filter_t *colorf = filter_arg;
223
224         color_filter_delete(colorf);
225 }
226
227 /* delete the specified list */
228 void
229 color_filter_list_delete(GSList **cfl)
230 {
231         g_slist_foreach(*cfl, color_filter_delete_cb, NULL);
232         g_slist_free(*cfl);
233         *cfl = NULL;
234 }
235
236 /* clone a single list entries from normal to edit list */
237 static color_filter_t *
238 color_filter_clone(color_filter_t *colorf)
239 {
240         color_filter_t *new_colorf;
241
242         new_colorf = g_malloc(sizeof (color_filter_t));
243         new_colorf->filter_name = g_strdup(colorf->filter_name);
244         new_colorf->filter_text = g_strdup(colorf->filter_text);
245         new_colorf->bg_color = colorf->bg_color;
246         new_colorf->fg_color = colorf->fg_color;
247         new_colorf->disabled = colorf->disabled;
248         new_colorf->c_colorfilter = NULL;
249         new_colorf->edit_dialog = NULL;
250         new_colorf->selected = FALSE;
251
252         return new_colorf;
253 }
254
255 static void
256 color_filter_list_clone_cb(gpointer filter_arg, gpointer cfl_arg)
257 {
258     gpointer *cfl = cfl_arg;
259     color_filter_t *new_colorf;
260
261     new_colorf = color_filter_clone(filter_arg);
262     *cfl = g_slist_append(*cfl, new_colorf);
263 }
264
265 /* clone the specified list */
266 static GSList *
267 color_filter_list_clone(GSList *cfl)
268 {
269         GSList *new_list = NULL;
270
271         g_slist_foreach(cfl, color_filter_list_clone_cb, &new_list);
272
273         return new_list;
274 }
275
276 /* Initialize the filter structures (reading from file) for general running, including app startup */
277 void
278 color_filters_init(void)
279 {
280         /* delete all currently existing filters */
281         color_filter_list_delete(&color_filter_list);
282
283         /* start the list with the temporary colorizing rules */
284         color_filters_add_tmp(&color_filter_list);
285
286         /* try to read the users filters */
287         if (!read_users_filters(&color_filter_list))
288                 /* if that failed, try to read the global filters */
289                 color_filters_read_globals(&color_filter_list);
290 }
291
292 void
293 color_filters_cleanup(void)
294 {
295         /* delete the previously deleted filters */
296         color_filter_list_delete(&color_filter_deleted_list);
297 }
298
299 static void
300 color_filters_clone_cb(gpointer filter_arg, gpointer user_data)
301 {
302         color_filter_t * new_colorf = color_filter_clone(filter_arg);
303         color_filter_add_cb (new_colorf, user_data);
304 }
305
306 void
307 color_filters_clone(gpointer user_data)
308 {
309     g_slist_foreach(color_filter_list, color_filters_clone_cb, user_data);
310 }
311
312
313 static void
314 color_filter_compile_cb(gpointer filter_arg, gpointer unused _U_)
315 {
316         color_filter_t *colorf = filter_arg;
317
318         g_assert(colorf->c_colorfilter == NULL);
319         if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
320                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
321                 "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
322                               colorf->filter_name, colorf->filter_text, dfilter_error_msg);
323                 /* this filter was compilable before, so this should never happen */
324                 /* except if the OK button of the parent window has been clicked */
325                 /* so don't use g_assert_not_reached() but check the filters again */
326         }
327 }
328
329 static void
330 color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
331 {
332         color_filter_t *colorf = filter_arg;
333
334         g_assert(colorf->c_colorfilter == NULL);
335         if (!dfilter_compile(colorf->filter_text, &colorf->c_colorfilter)) {
336                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
337                 "Removing color filter name: \"%s\" text: \"%s\".\n%s",
338                               colorf->filter_name, colorf->filter_text, dfilter_error_msg);
339                 /* Delete the color filter from the list of color filters. */
340                 color_filter_valid_list = g_slist_remove(color_filter_valid_list, colorf);
341                 color_filter_delete(colorf);
342         }
343 }
344
345 /* apply changes from the edit list */
346 void
347 color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
348 {
349         /* "move" old entries to the deleted list
350          * we must keep them until the dissection no longer needs them */
351         color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
352         color_filter_list = NULL;
353
354         /* clone all list entries from tmp/edit to normal list */
355         color_filter_valid_list = NULL;
356         color_filter_valid_list = color_filter_list_clone(tmp_cfl);
357         color_filter_valid_list = g_slist_concat(color_filter_valid_list, 
358                                                  color_filter_list_clone(edit_cfl) );
359
360         /* compile all filter */
361         g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
362
363         /* clone all list entries from tmp/edit to normal list */
364         color_filter_list = color_filter_list_clone(color_filter_valid_list);
365
366         /* compile all filter */
367         g_slist_foreach(color_filter_list, color_filter_compile_cb, NULL);
368 }
369
370 gboolean 
371 color_filters_used(void)
372 {
373         return color_filter_list != NULL && filters_enabled;
374 }
375
376 void
377 color_filters_enable(gboolean enable)
378 {
379         filters_enabled = enable;
380 }
381
382
383 /* prepare the epan_dissect_t for the filter */
384 static void
385 prime_edt(gpointer data, gpointer user_data)
386 {
387         color_filter_t  *colorf = data;
388         epan_dissect_t   *edt = user_data;
389
390         if (colorf->c_colorfilter != NULL)
391                 epan_dissect_prime_dfilter(edt, colorf->c_colorfilter);
392 }
393
394 /* Prime the epan_dissect_t with all the compiler
395  * color filters in 'color_filter_list'. */
396 void
397 color_filters_prime_edt(epan_dissect_t *edt)
398 {
399         g_slist_foreach(color_filter_list, prime_edt, edt);
400 }
401
402 /* Colorize a single packet of the packet list */
403 color_filter_t *
404 color_filters_colorize_packet(gint row, epan_dissect_t *edt)
405 {
406     GSList *curr;
407     color_filter_t *colorf;
408
409     /* If we have color filters, "search" for the matching one. */
410     if (color_filters_used()) {
411         curr = color_filter_list;
412
413         while(curr != NULL) {
414             colorf = curr->data;
415             if ( (!colorf->disabled) &&
416                  (colorf->c_colorfilter != NULL) &&
417                  dfilter_apply_edt(colorf->c_colorfilter, edt)) {
418                     /* this is the filter to use, apply it to the packet list */
419                     packet_list_set_colors(row, &(colorf->fg_color), &(colorf->bg_color));
420                     return colorf;
421             }
422             curr = g_slist_next(curr);
423         }
424     }
425
426     return NULL;
427 }
428
429 /* read filters from the given file */
430 /* XXX - Would it make more sense to use GStrings here instead of reallocing
431    our buffers? */
432 static gboolean
433 read_filters_file(FILE *f, gpointer user_data)
434 {
435 #define INIT_BUF_SIZE 128
436         gchar  *name = NULL;
437         gchar  *filter_exp = NULL;
438         guint32 name_len = INIT_BUF_SIZE;
439         guint32 filter_exp_len = INIT_BUF_SIZE;
440         guint32 i = 0;
441         gint32  c;
442         guint16 fg_r, fg_g, fg_b, bg_r, bg_g, bg_b;
443         gboolean disabled = FALSE;
444         gboolean skip_end_of_line = FALSE;
445
446         name = g_malloc(name_len + 1);
447         filter_exp = g_malloc(filter_exp_len + 1);
448
449         while (1) {
450
451                 if (skip_end_of_line) {
452                         do {
453                                 c = getc(f);
454                         } while (c != EOF && c != '\n');
455                         if (c == EOF)
456                                 break;
457                         disabled = FALSE;
458                         skip_end_of_line = FALSE;
459                 }
460
461                 while ((c = getc(f)) != EOF && isspace(c)) {
462                         if (c == '\n') {
463                                 continue;
464                         }
465                 }
466
467                 if (c == EOF)
468                         break;
469
470                 if (c == '!') {
471                         disabled = TRUE;
472                         continue;
473                 }
474
475                 /* skip # comments and invalid lines */
476                 if (c != '@') { 
477                         skip_end_of_line = TRUE;
478                         continue;
479                 }
480
481                 /* we get the @ delimiter.
482                  * Format is:
483                  * @name@filter expression@[background r,g,b][foreground r,g,b]
484                  */
485
486                 /* retrieve name */
487                 i = 0;
488                 while (1) {
489                         c = getc(f);
490                         if (c == EOF || c == '@')
491                                 break;
492                         if (i >= name_len) {
493                                 /* buffer isn't long enough; double its length.*/
494                                 name_len *= 2;
495                                 name = g_realloc(name, name_len + 1);
496                         }
497                         name[i++] = c;            
498                 }
499                 name[i] = '\0';
500
501                 if (c == EOF) {
502                         break;
503                 } else if (i == 0) {
504                         skip_end_of_line = TRUE;
505                         continue;
506                 }
507
508                 /* retrieve filter expression */
509                 i = 0;
510                 while (1) {
511                         c = getc(f);
512                         if (c == EOF || c == '@')
513                                 break;
514                         if (i >= filter_exp_len) {
515                                 /* buffer isn't long enough; double its length.*/
516                                 filter_exp_len *= 2;
517                                 filter_exp = g_realloc(filter_exp, filter_exp_len + 1);
518                         }
519                         filter_exp[i++] = c;
520                 }
521                 filter_exp[i] = '\0';
522
523                 if (c == EOF) {
524                         break;
525                 } else if (i == 0) {
526                         skip_end_of_line = TRUE;
527                         continue;
528                 }
529
530                 /* retrieve background and foreground colors */
531                 if (fscanf(f,"[%hu,%hu,%hu][%hu,%hu,%hu]",
532                         &bg_r, &bg_g, &bg_b, &fg_r, &fg_g, &fg_b) == 6) {
533
534                         /* we got a complete color filter */
535
536                         color_t bg_color, fg_color;
537                         color_filter_t *colorf;
538                         dfilter_t *temp_dfilter;
539
540                         if (!dfilter_compile(filter_exp, &temp_dfilter)) {
541                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
542                                 "Could not compile color filter %s from saved filters.\n%s",
543                                               name, dfilter_error_msg);
544                                 skip_end_of_line = TRUE;
545                                 continue;
546                         }
547
548                         if (!initialize_color(&fg_color, fg_r, fg_g, fg_b)) {
549                                 /* oops */
550                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
551                                     "Could not allocate foreground color "
552                                     "specified in input file for %s.", name);
553                                 dfilter_free(temp_dfilter);
554                                 skip_end_of_line = TRUE;
555                                 continue;
556                         }
557                         if (!initialize_color(&bg_color, bg_r, bg_g, bg_b)) {
558                                 /* oops */
559                                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
560                                     "Could not allocate background color "
561                                     "specified in input file for %s.", name);
562                                 dfilter_free(temp_dfilter);
563                                 skip_end_of_line = TRUE;
564                                 continue;
565                         }
566
567                         colorf = color_filter_new(name, filter_exp, &bg_color,
568                             &fg_color, disabled);
569                         if(user_data == &color_filter_list) {
570                                 GSList **cfl = (GSList **)user_data;
571
572                                 /* internal call */
573                                 colorf->c_colorfilter = temp_dfilter;
574                                 *cfl = g_slist_append(*cfl, colorf);
575                         } else {
576                                 /* external call */
577                                 /* just editing, don't need the compiled filter */
578                                 dfilter_free(temp_dfilter);
579                                 color_filter_add_cb (colorf, user_data);
580                         }
581                 }    /* if sscanf */
582
583                 skip_end_of_line = TRUE;
584         }
585
586         g_free(name);
587         g_free(filter_exp);
588         return TRUE;
589 }
590
591 /* read filters from the user's filter file */
592 static gboolean
593 read_users_filters(GSList **cfl)
594 {
595         gchar *path;
596         FILE *f;
597         gboolean ret;
598
599         /* decide what file to open (from dfilter code) */
600         path = get_persconffile_path("colorfilters", FALSE);
601         if ((f = eth_fopen(path, "r")) == NULL) {
602                 if (errno != ENOENT) {
603                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
604                             "Could not open filter file\n\"%s\": %s.", path,
605                             strerror(errno));
606                 }
607                 g_free(path);
608                 return FALSE;
609         }
610         g_free(path);
611         path = NULL;
612
613         ret = read_filters_file(f, cfl);
614         fclose(f);
615         return ret;
616 }
617
618 /* read filters from the filter file */
619 gboolean
620 color_filters_read_globals(gpointer user_data)
621 {
622         gchar *path;
623         FILE *f;
624         gboolean ret;
625
626         /* decide what file to open (from dfilter code) */
627         path = get_datafile_path("colorfilters");
628         if ((f = eth_fopen(path, "r")) == NULL) {
629                 if (errno != ENOENT) {
630                         simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
631                             "Could not open global filter file\n\"%s\": %s.", path,
632                             strerror(errno));
633                 }
634                 g_free(path);
635                 return FALSE;
636         }
637         g_free(path);
638         path = NULL;
639
640         ret = read_filters_file(f, user_data);
641         fclose(f);
642         return ret;
643 }
644
645 /* read filters from some other filter file (import) */
646 gboolean
647 color_filters_import(gchar *path, gpointer user_data)
648 {
649         FILE *f;
650         gboolean ret;
651
652         if ((f = eth_fopen(path, "r")) == NULL) {
653                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
654                     "Could not open\n%s\nfor reading: %s.",
655                     path, strerror(errno));
656                 return FALSE;
657         }
658
659         ret = read_filters_file(f, user_data);
660         fclose(f);
661         return ret;
662 }
663
664 struct write_filter_data
665 {
666   FILE * f;
667   gboolean only_selected;
668 };
669
670 /* save a single filter */
671 static void
672 write_filter(gpointer filter_arg, gpointer data_arg)
673 {
674         struct write_filter_data *data = data_arg;
675         color_filter_t *colorf = filter_arg;
676         FILE *f = data->f;
677
678         if ( (colorf->selected || !data->only_selected) &&
679              (strstr(colorf->filter_name,TEMP_COLOR_PREFIX)==NULL) ) {
680                 fprintf(f,"%s@%s@%s@[%d,%d,%d][%d,%d,%d]\n",
681                     colorf->disabled ? "!" : "",
682                     colorf->filter_name,
683                     colorf->filter_text,
684                     colorf->bg_color.red,
685                     colorf->bg_color.green,
686                     colorf->bg_color.blue,
687                     colorf->fg_color.red,
688                     colorf->fg_color.green,
689                     colorf->fg_color.blue);
690         }
691 }
692
693 /* save filters in a filter file */
694 static gboolean
695 write_filters_file(GSList *cfl, FILE *f, gboolean only_selected)
696 {
697         struct write_filter_data data;
698
699         data.f = f;
700         data.only_selected = only_selected;
701   
702         fprintf(f,"# DO NOT EDIT THIS FILE!  It was created by Wireshark\n");
703         g_slist_foreach(cfl, write_filter, &data);
704         return TRUE;
705 }
706
707 /* save filters in users filter file */
708 gboolean
709 color_filters_write(GSList *cfl)
710 {
711         gchar *pf_dir_path;
712         const gchar *path;
713         FILE *f;
714
715         /* Create the directory that holds personal configuration files,
716            if necessary.  */
717         if (create_persconffile_dir(&pf_dir_path) == -1) {
718                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
719                     "Can't create directory\n\"%s\"\nfor color files: %s.",
720                     pf_dir_path, strerror(errno));
721                 g_free(pf_dir_path);
722                 return FALSE;
723         }
724
725         path = get_persconffile_path("colorfilters", TRUE);
726         if ((f = eth_fopen(path, "w+")) == NULL) {
727                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
728                     "Could not open\n%s\nfor writing: %s.",
729                     path, strerror(errno));
730                 return FALSE;
731         }
732         write_filters_file(cfl, f, FALSE);
733         fclose(f);
734         return TRUE;
735 }
736
737 /* save filters in some other filter file (export) */
738 gboolean
739 color_filters_export(gchar *path, GSList *cfl, gboolean only_marked)
740 {
741         FILE *f;
742
743         if ((f = eth_fopen(path, "w+")) == NULL) {
744                 simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
745                     "Could not open\n%s\nfor writing: %s.",
746                     path, strerror(errno));
747                 return FALSE;
748         }
749         write_filters_file(cfl, f, only_marked);
750         fclose(f);
751         return TRUE;
752 }
753