carve out the (currently disabled) welcome page into it's own file to slightly reduce...
[obnox/wireshark/wip.git] / color_filters.c
index 18a02422c300bd15c51eb70ccba60b31e934494a..445c36d1cfa6b3ed4b5901e4629d86d7d72e8830 100644 (file)
@@ -62,6 +62,10 @@ static GSList *color_filter_valid_list = NULL;
 /* Color Filters can en-/disabled. */
 gboolean filters_enabled = TRUE;
 
+/* Remember if there are temporary coloring filters set to
+ * add sensitivity to the "Reset Coloring 1-10" menu item
+ */
+gboolean tmp_colors_set = FALSE;
 
 /* Create a new filter */
 color_filter_t *
@@ -90,7 +94,6 @@ void
 color_filters_add_tmp(GSList **cfl)
 {
        gchar  *name = NULL;
-        gchar  *stockstr = NULL;
        guint32 i;
         gchar** bg_colors;
         gchar** fg_colors;
@@ -141,33 +144,70 @@ color_filters_find_by_name_cb(gconstpointer arg1, gconstpointer arg2)
 
 /* Set the filter off a temporary colorfilters and enable it */
 void
-color_filters_set_tmp(guint8 filt_nr, gchar *filter)
+color_filters_set_tmp(guint8 filt_nr, gchar *filter, gboolean disabled)
 {
        gchar  *name = NULL;
+       gchar  *tmpfilter = NULL;
         GSList *cfl;
         color_filter_t *colorf;
         dfilter_t      *compiled_filter;
+       guint8 i;
+
+        /* Go through the tomporary filters and look for the same filter string.
+         * If found, clear it so that a filter can be "moved" up and down the list
+         */
+        for ( i=1 ; i<=10 ; i++ ) {
+                /* If we need to reset the temporary filter (filter==NULL), don't look 
+                 * for other rules with the same filter string
+                 */
+                if( i!=filt_nr && filter==NULL ) 
+                        continue;
 
-        name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,filt_nr);
-        cfl = g_slist_find_custom(color_filter_list, (gpointer *) name, color_filters_find_by_name_cb);
-        colorf = cfl->data;
-
-        if(colorf) {
-                if (!dfilter_compile(filter, &compiled_filter)) {
-                        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
-                                "Could not compile color filter name: \"%s\" text: \"%s\".\n%s",
-                                name, filter, dfilter_error_msg);
-                } else {
-                        if (colorf->filter_text != NULL)
-                                g_free(colorf->filter_text);
-                        if (colorf->c_colorfilter != NULL)
-                                dfilter_free(colorf->c_colorfilter);
-                        colorf->filter_text = g_strdup(filter);
-                        colorf->c_colorfilter = compiled_filter;
-                        colorf->disabled = FALSE;
+                name = g_strdup_printf("%s%02d",TEMP_COLOR_PREFIX,i);
+                cfl = g_slist_find_custom(color_filter_list, (gpointer *) name, color_filters_find_by_name_cb);
+                colorf = cfl->data;
+
+                /* Only change the filter rule if this is the rule to change or if
+                 * a matching filter string has been found
+                 */
+                if(colorf && ( (i==filt_nr) || (strstr(filter,colorf->filter_text)!=NULL) ) ) {
+                        /* set filter string to "frame" if we are resetting the rules
+                         * or if we found a matching filter string which need to be cleared
+                         */
+                        tmpfilter = ( (filter==NULL) || (i!=filt_nr) ) ? "frame" : filter;
+                        if (!dfilter_compile(tmpfilter, &compiled_filter)) {
+                                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
+                                    "Could not compile color filter name: \"%s\""
+                                    " text: \"%s\".\n%s", name, filter, dfilter_error_msg);
+                        } else {
+                                if (colorf->filter_text != NULL)
+                                        g_free(colorf->filter_text);
+                                if (colorf->c_colorfilter != NULL)
+                                        dfilter_free(colorf->c_colorfilter);
+                                colorf->filter_text = g_strdup(tmpfilter);
+                                colorf->c_colorfilter = compiled_filter;
+                                colorf->disabled = ((i!=filt_nr) ? TRUE : disabled);
+                                /* Remember that there are now temporary coloring filters set */
+                                if( filter )
+                                        tmp_colors_set = TRUE;
+                        }
                 }
+                g_free(name);
         }
-        g_free(name);
+        return;
+}
+
+/* Reset the temporary colorfilters */
+void
+color_filters_reset_tmp()
+{
+       guint8 i;
+
+        for ( i=1 ; i<=10 ; i++ ) {
+            color_filters_set_tmp(i, NULL, TRUE);
+        }
+        /* Remember that there are now *no* temporary coloring filters set */
+        tmp_colors_set = FALSE;
        return;
 }
 
@@ -258,6 +298,23 @@ color_filters_init(void)
                color_filters_read_globals(&color_filter_list);
 }
 
+void
+color_filters_reload(void)
+{
+        /* "move" old entries to the deleted list
+         * we must keep them until the dissection no longer needs them */
+        color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
+        color_filter_list = NULL;
+
+        /* start the list with the temporary colorizing rules */
+        color_filters_add_tmp(&color_filter_list);
+
+       /* try to read the users filters */
+       if (!read_users_filters(&color_filter_list))
+               /* if that failed, try to read the global filters */
+               color_filters_read_globals(&color_filter_list);
+}
+
 void
 color_filters_cleanup(void)
 {
@@ -313,21 +370,23 @@ color_filter_validate_cb(gpointer filter_arg, gpointer unused _U_)
 
 /* apply changes from the edit list */
 void
-color_filters_apply(GSList *cfl)
+color_filters_apply(GSList *tmp_cfl, GSList *edit_cfl)
 {
         /* "move" old entries to the deleted list
          * we must keep them until the dissection no longer needs them */
         color_filter_deleted_list = g_slist_concat(color_filter_deleted_list, color_filter_list);
         color_filter_list = NULL;
 
-        /* clone all list entries from edit to normal list */
+        /* clone all list entries from tmp/edit to normal list */
         color_filter_valid_list = NULL;
-        color_filter_valid_list = color_filter_list_clone(cfl);
+        color_filter_valid_list = color_filter_list_clone(tmp_cfl);
+        color_filter_valid_list = g_slist_concat(color_filter_valid_list, 
+                                                 color_filter_list_clone(edit_cfl) );
 
         /* compile all filter */
         g_slist_foreach(color_filter_valid_list, color_filter_validate_cb, NULL);
 
-        /* clone all list entries from edit to normal list */
+        /* clone all list entries from tmp/edit to normal list */
         color_filter_list = color_filter_list_clone(color_filter_valid_list);
 
         /* compile all filter */
@@ -340,6 +399,12 @@ color_filters_used(void)
         return color_filter_list != NULL && filters_enabled;
 }
 
+gboolean 
+tmp_color_filters_used(void)
+{
+        return tmp_colors_set;
+}
+
 void
 color_filters_enable(gboolean enable)
 {
@@ -564,7 +629,7 @@ read_users_filters(GSList **cfl)
        gboolean ret;
 
        /* decide what file to open (from dfilter code) */
-       path = get_persconffile_path("colorfilters", FALSE);
+       path = get_persconffile_path("colorfilters", TRUE, FALSE);
        if ((f = eth_fopen(path, "r")) == NULL) {
                if (errno != ENOENT) {
                        simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
@@ -676,7 +741,7 @@ gboolean
 color_filters_write(GSList *cfl)
 {
        gchar *pf_dir_path;
-       const gchar *path;
+       gchar *path;
        FILE *f;
 
        /* Create the directory that holds personal configuration files,
@@ -689,13 +754,15 @@ color_filters_write(GSList *cfl)
                return FALSE;
        }
 
-       path = get_persconffile_path("colorfilters", TRUE);
+       path = get_persconffile_path("colorfilters", TRUE, TRUE);
        if ((f = eth_fopen(path, "w+")) == NULL) {
                simple_dialog(ESD_TYPE_ERROR, ESD_BTN_OK,
                    "Could not open\n%s\nfor writing: %s.",
                    path, strerror(errno));
+               g_free(path);
                return FALSE;
        }
+       g_free(path);
        write_filters_file(cfl, f, FALSE);
        fclose(f);
        return TRUE;