aboutsummaryrefslogtreecommitdiffstats
path: root/filter/filter-option.c
diff options
context:
space:
mode:
authorMilan Crha <mcrha@redhat.com>2008-12-06 03:20:28 +0800
committerMilan Crha <mcrha@src.gnome.org>2008-12-06 03:20:28 +0800
commit7b221ebc07a7867c3528b113e82f25a01816a015 (patch)
tree975f1efd25565d3fd930c935fabeb10ec5b866da /filter/filter-option.c
parent888965285486dbe274238c1b6062ed89986676a8 (diff)
downloadgsoc2013-evolution-7b221ebc07a7867c3528b113e82f25a01816a015.tar.gz
gsoc2013-evolution-7b221ebc07a7867c3528b113e82f25a01816a015.tar.zst
gsoc2013-evolution-7b221ebc07a7867c3528b113e82f25a01816a015.zip
** Fix for bug #332629
2008-12-05 Milan Crha <mcrha@redhat.com> ** Fix for bug #332629 * filter/filter-option.h: (struct _filter_option), (struct _FilterOption), (filter_option_add): * filter/filter-option.c: (filter_option_init), (filter_option_finalise), (filter_option_add), (xml_create), (get_widget), (clone): Be able to define optionlist with dynamically created list of options. * filter/filter-label.c: (fill_options): Adapt. * addressbook/gui/widgets/addresstypes.xml: * calendar/gui/caltypes.xml: * calendar/gui/memotypes.xml: * calendar/gui/tasktypes.xml: Use dynamically created list of categories in the option's widget. svn path=/trunk/; revision=36836
Diffstat (limited to 'filter/filter-option.c')
-rw-r--r--filter/filter-option.c102
1 files changed, 98 insertions, 4 deletions
diff --git a/filter/filter-option.c b/filter/filter-option.c
index 1685fe06ce..e0827d4475 100644
--- a/filter/filter-option.c
+++ b/filter/filter-option.c
@@ -29,6 +29,7 @@
#include <gtk/gtk.h>
#include <glib/gi18n.h>
+#include <dlfcn.h>
#include "filter-option.h"
#include "filter-part.h"
@@ -102,6 +103,7 @@ static void
filter_option_init (FilterOption *fo)
{
fo->type = "option";
+ fo->dynamic_func = NULL;
}
static void
@@ -120,6 +122,7 @@ filter_option_finalise (GObject *obj)
g_list_foreach (fo->options, (GFunc)free_option, NULL);
g_list_free (fo->options);
+ g_free (fo->dynamic_func);
G_OBJECT_CLASS (parent_class)->finalize (obj);
}
@@ -164,7 +167,7 @@ filter_option_set_current (FilterOption *option, const char *name)
/* used by implementers to add additional options */
struct _filter_option *
-filter_option_add(FilterOption *fo, const char *value, const char *title, const char *code)
+filter_option_add(FilterOption *fo, const char *value, const char *title, const char *code, gboolean is_dynamic)
{
struct _filter_option *op;
@@ -175,6 +178,7 @@ filter_option_add(FilterOption *fo, const char *value, const char *title, const
op->title = g_strdup(title);
op->value = g_strdup(value);
op->code = g_strdup(code);
+ op->is_dynamic = is_dynamic;
fo->options = g_list_append(fo->options, op);
if (fo->current == NULL)
@@ -253,10 +257,34 @@ xml_create (FilterElement *fe, xmlNodePtr node)
work = work->next;
}
- filter_option_add (fo, value, title, code);
+ filter_option_add (fo, value, title, code, FALSE);
xmlFree (value);
g_free (title);
g_free (code);
+ } else if (g_str_equal ((char *)n->name, "dynamic")) {
+ if (fo->dynamic_func) {
+ g_warning ("Only one 'dynamic' node is acceptable in the optionlist '%s'", fe->name);
+ } else {
+ /* Expecting only one <dynamic func="cb" /> in the option list,
+ The 'cb' should be of this prototype:
+ GSList *cb (void);
+ returning GSList of struct _filter_option, all newly allocated, because it'll
+ be freed with g_free and g_slist_free. 'is_dynamic' member is ignored here.
+ */
+ xmlChar *fn;
+
+ fn = xmlGetProp (n, (const unsigned char *)"func");
+ if (fn && *fn) {
+ fo->dynamic_func = g_strdup ((const char *)fn);
+
+ /* to remember where to place them */
+ filter_option_add (fo, "fake_dynamic", "fake_dynamic", NULL, TRUE);
+ } else {
+ g_warning ("Missing 'func' attribute within '%s' node in optionlist '%s'", n->name, fe->name);
+ }
+
+ xmlFree (fn);
+ }
} else if (n->type == XML_ELEMENT_NODE) {
g_warning ("Unknown xml node within optionlist: %s\n", n->name);
}
@@ -315,11 +343,75 @@ get_widget (FilterElement *fe)
GtkWidget *omenu;
GtkWidget *item;
GtkWidget *first = NULL;
- GList *l = fo->options;
+ GList *l;
struct _filter_option *op;
int index = 0, current = 0;
+ if (fo->dynamic_func) {
+ /* it is dynamically filled, thus remove all dynamics and put there the fresh ones */
+ GList *old_ops;
+ struct _filter_option *old_cur;
+ void *module;
+ GSList *(*get_func)(void);
+
+ old_ops = fo->options;
+ old_cur = fo->current;
+ l = old_ops;
+
+ /* start with an empty list */
+ fo->current = NULL;
+ fo->options = NULL;
+
+ for (l = fo->options; l; l = l->next) {
+ op = l->data;
+
+ if (op->is_dynamic) {
+ break;
+ } else {
+ filter_option_add (fo, op->value, op->title, op->code, FALSE);
+ }
+ }
+
+ module = dlopen (NULL, RTLD_LAZY);
+
+ get_func = dlsym (module, fo->dynamic_func);
+ if (get_func) {
+ GSList *items, *i;
+
+ items = get_func ();
+ for (i = items; i; i = i->next) {
+ op = i->data;
+
+ if (op) {
+ filter_option_add (fo, op->value, op->title, op->code, TRUE);
+ free_option (op, NULL);
+ }
+ }
+
+ g_slist_free (items);
+ } else
+ g_warning ("optionlist dynamic fill function '%s' not found", fo->dynamic_func);
+
+ dlclose (module);
+
+ /* maybe some static left after those dynamic, add them too */
+ for (; l; l = l->next) {
+ op = l->data;
+
+ if (!op->is_dynamic)
+ filter_option_add (fo, op->value, op->title, op->code, FALSE);
+ }
+
+ if (old_cur)
+ filter_option_set_current (fo, old_cur->value);
+
+ /* free old list */
+ g_list_foreach (old_ops, (GFunc)free_option, NULL);
+ g_list_free (old_ops);
+ }
+
menu = gtk_menu_new ();
+ l = fo->options;
while (l) {
op = l->data;
item = gtk_menu_item_new_with_label (_(op->title));
@@ -382,12 +474,14 @@ clone (FilterElement *fe)
l = fo->options;
while (l) {
op = l->data;
- newop = filter_option_add (new, op->value, op->title, op->code);
+ newop = filter_option_add (new, op->value, op->title, op->code, op->is_dynamic);
if (fo->current == op)
new->current = newop;
l = l->next;
}
+ new->dynamic_func = g_strdup (fo->dynamic_func);
+
d(printf ("cloning option code %p, current = %p\n", new, new->current));
return (FilterElement *) new;