[balsa] More user friendly view of filters' conditions.



commit 6187c29e9d560f2d4598770dd8be6538550b44f8
Author: Pawel Salek <pawsa0 gmail com>
Date:   Mon Aug 29 22:42:45 2011 +0200

    More user friendly view of filters' conditions.
    
    * libbalsa/filter-funcs.c: implement libbalsa_condition_to_string_user
    * libbalsa/filter.h: add libbalsa_condition_to_string_user() proto.
    * src/filter-edit-callbacks.c: use it to provide more user-friendly
      filter view.

 ChangeLog                   |    7 +++
 libbalsa/filter-funcs.c     |  110 ++++++++++++++++++++++++++++++++++++++++++-
 libbalsa/filter.h           |    1 +
 src/filter-edit-callbacks.c |   12 ++++-
 4 files changed, 127 insertions(+), 3 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index c64b753..7d0ff0c 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2011-08-29  Pawel Salek
+
+	* libbalsa/filter-funcs.c: implement libbalsa_condition_to_string_user
+	* libbalsa/filter.h: add libbalsa_condition_to_string_user() proto.
+	* src/filter-edit-callbacks.c: use it to provide more user-friendly
+	filter view.
+
 2011-06-15  Peter Bloomfield
 
 	* src/main-window.c: use GDK_1 instead of new GDK_KEY_1 in alt-n
diff --git a/libbalsa/filter-funcs.c b/libbalsa/filter-funcs.c
index 1418c11..ff1e252 100644
--- a/libbalsa/filter-funcs.c
+++ b/libbalsa/filter-funcs.c
@@ -27,7 +27,8 @@
 #if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
 # include "config.h"
 #endif                          /* HAVE_CONFIG_H */
-#include "filter-funcs.h"
+
+#include <glib/gi18n.h>
 
 #include <ctype.h>
 #include <stdlib.h>
@@ -35,6 +36,7 @@
 #include <time.h>
 #include "missing.h"
 
+#include "filter-funcs.h"
 #include "filter-private.h"
 
 /* Conditions */
@@ -371,6 +373,112 @@ libbalsa_condition_to_string(LibBalsaCondition *cond)
     return g_string_free(res, FALSE);
 }
 
+static void
+append_header_names(LibBalsaCondition *cond, GString *res)
+{
+    static const struct {
+        unsigned header;
+        const gchar *header_name;
+    } header_name_map[] = {
+        { CONDITION_MATCH_TO, N_("To") },
+        { CONDITION_MATCH_FROM, N_("From") },
+        { CONDITION_MATCH_SUBJECT, N_("Subject") },
+        { CONDITION_MATCH_CC, N_("Cc") },
+        { CONDITION_MATCH_BODY, N_("Body") }
+    };
+
+    unsigned i;
+    if (CONDITION_CHKMATCH(cond,CONDITION_MATCH_US_HEAD)) {
+        g_string_append_printf(res, _("Header:%s"),
+                               cond->match.string.user_header);
+    }
+    for (i=0; i<ELEMENTS(header_name_map); ++i) {
+        if (CONDITION_CHKMATCH(cond, header_name_map[i].header)) {
+            if (res->len>0) {
+                g_string_append_printf(res, _(",%s"),
+                                       _(header_name_map[i].header_name));
+            } else {
+                res = g_string_append(res, _(header_name_map[i].header_name));
+            }
+        }
+    }
+}
+
+static void
+append_flag_names(LibBalsaCondition *cond, GString *res)
+{
+    static const struct {
+        LibBalsaMessageFlag flag;
+        const gchar *flag_name;
+    } flag_name_map[] = {
+        { LIBBALSA_MESSAGE_FLAG_NEW, N_("New") },
+        { LIBBALSA_MESSAGE_FLAG_DELETED, N_("Deleted") },
+        { LIBBALSA_MESSAGE_FLAG_REPLIED, N_("Replied") },
+        { LIBBALSA_MESSAGE_FLAG_FLAGGED, N_("Flagged") },
+    };
+    unsigned i;
+    gsize len = res->len;
+    for (i=0; i<ELEMENTS(flag_name_map); ++i) {
+        if (cond->match.flags & flag_name_map[i].flag) {
+            if (res->len == len) {
+                res = g_string_append(res, _(flag_name_map[i].flag_name));
+            } else {
+                g_string_printf(res, _(",%s"),
+                                _(flag_name_map[i].flag_name));
+            }
+        }
+    }
+}
+
+gchar*
+libbalsa_condition_to_string_user(LibBalsaCondition *cond)
+{
+    GDate date;
+    char str[80];
+    GString *res = g_string_new("");
+
+    if(cond->negate)
+        g_string_append(res, _("Not "));
+
+    switch(cond->type) {
+    case CONDITION_STRING:
+        append_header_names(cond, res);
+        g_string_append_c(res, ' ');
+        append_quoted_string(res, cond->match.string.string);
+	break;
+    case CONDITION_REGEX:
+#if 0
+        /* FIXME! */
+#endif        
+	break;
+    case CONDITION_DATE:
+	if (cond->match.date.date_low) {
+	    g_date_set_time_t(&date, cond->match.date.date_low);
+	    g_date_strftime(str, sizeof(str), _("From %Y-%m-%d"), &date);
+	} else str[0]='\0';
+        append_quoted_string(res, str);
+        g_string_append_c(res, ' ');
+	if (cond->match.date.date_high) {
+	    g_date_set_time_t(&date, cond->match.date.date_high);
+	    g_date_strftime(str, sizeof(str), _("To %Y-%m-%d"), &date);
+	} else str[0]='\0';
+        append_quoted_string(res, str);
+	break;
+    case CONDITION_FLAG:
+        append_flag_names(cond, res);
+        break;
+    case CONDITION_AND:
+        g_string_append(res, _("And"));
+        break;
+    case CONDITION_OR:
+        g_string_append(res, _("Or"));
+	break;
+    case CONDITION_NONE:
+        break;
+    }
+    return g_string_free(res, FALSE);
+}
+
 /*
  * condition_delete_regex()
  *
diff --git a/libbalsa/filter.h b/libbalsa/filter.h
index 3013023..36b809f 100644
--- a/libbalsa/filter.h
+++ b/libbalsa/filter.h
@@ -107,6 +107,7 @@ struct _LibBalsaCondition {
 
 LibBalsaCondition* libbalsa_condition_new_from_string(gchar **string);
 gchar*             libbalsa_condition_to_string(LibBalsaCondition *cond);
+gchar*             libbalsa_condition_to_string_user(LibBalsaCondition *cond);
 
 LibBalsaCondition* libbalsa_condition_new_flag_enum(gboolean negated,
                                                     LibBalsaMessageFlag flgs);
diff --git a/src/filter-edit-callbacks.c b/src/filter-edit-callbacks.c
index 79ff018..e77155d 100644
--- a/src/filter-edit-callbacks.c
+++ b/src/filter-edit-callbacks.c
@@ -362,13 +362,17 @@ update_condition_list_label(void)
     GtkTreeModel *model;
     GtkTreeIter iter;
     LibBalsaCondition *cond;
+    gchar *filter_description;
 
     if (!gtk_tree_selection_get_selected(selection, &model, &iter))
         return;
 
     gtk_tree_model_get(model, &iter, 1, &cond, -1);
+
+    filter_description = libbalsa_condition_to_string_user(cond);
     gtk_list_store_set(GTK_LIST_STORE(model), &iter,
-                       0, _(fe_search_type[cond->type - 1].text), -1);
+                       0, filter_description, -1);
+    g_free(filter_description);
 }                      /* end fe_update_condition_list_label */
 
 static ConditionMatchType
@@ -2015,7 +2019,9 @@ static void
 fill_condition_list(GtkTreeModel *model, LibBalsaCondition *condition,
 		    ConditionMatchType type)
 {
+    gchar *filter_description;
     GtkTreeIter iter;
+
     if (!condition)
 	return;
     if (condition->type == CONDITION_OR
@@ -2031,10 +2037,12 @@ fill_condition_list(GtkTreeModel *model, LibBalsaCondition *condition,
     }
 
     gtk_list_store_prepend(GTK_LIST_STORE(model), &iter);
+    filter_description = libbalsa_condition_to_string_user(condition);
     gtk_list_store_set(GTK_LIST_STORE(model), &iter,
-                       0, _(fe_search_type[condition->type-1].text),
+                       0, filter_description,
                        1, libbalsa_condition_ref(condition),
                        -1);
+    g_free(filter_description);
 }
 
 void



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]