[evolution-patches] Re: New hook for junk plugin (improved)



hi,

I am attaching the modified "em-junk-hook.c" and  patches to
mail-session.c and camel-junk-plugin.c

Now I am assigning NULL to junk_plugin member of session while init.
The plugin is supplied in the "construct" of the "item". If "item" is
not constructed, there is no plugin to load.

file camel-junk-plugin.c is modified to handle the cases when there is
no plugin at all.
Please let me know if it looks ok.

Thanks,
Vivek Jain



On Fri, 2005-06-24 at 14:33 +0530, Vivek Jain wrote:
> Hi,
> 
> Now I am passing the CamelJunkPlugin object to the callbacks and
> subclassing it into the EMJunkHookItem, so that I can get the "item"
> when methods are invoked.
> 
> The camel/camel-junk-plugin.[ch] files have to be patched for this.
> I have attached the improved em-junk-hook.[ch] files and a patch to
> camel.
> 
> Please let me know if this looks ok.
> 
> Thanks,
> Vivek Jain
> 
> 
> On Thu, 2005-06-23 at 21:11 +0530, Vivek Jain wrote:
> > hi,
> > 
> > I have created a hook that wraps up camelJunkPlugin and allows its
> > methods to be exposed to the plugins. Using this hook one can write a
> > plugin to use its own method for spam handling. As an example I have
> > moved the spamassasin junk mail handling to a plugin which used this new
> > hook.
> > 
> > I have added two files em-format-hook.c em-format-hook.h in the /mail
> > and removed em-junk-filter.[ch] from here. I have moved em-junk-filter.c
> > to the newly written sa-junk-plugin.
> > 
> > I am attaching the following:
> > 
> > 1. Patch for mail/
> > 2. two new files
> > 3. sa-junk-plugin
> > 
> > 
> > Thanks,
> > Vivek Jain
> > 
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  Authors: Vivek Jain <jvivek novell com>
 *
 *  Copyright 2004 Novell, Inc. (www.novell.com)
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Street #330, Boston, MA 02111-1307, USA.
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <string.h>
#include <stdlib.h>
#include <glib.h>
#include "em-junk-hook.h"
#include "mail-session.h"
#include <e-util/e-icon-factory.h>
#include <camel/camel-junk-plugin.h>
#include <glib/gi18n.h>

static GHashTable *emjh_types;
static GObjectClass *parent_class = NULL;

static void *emjh_parent_class;
static GObjectClass *emj_parent;
#define emjh ((EMJunkHook *)eph)

#define d(x)

static const EPluginHookTargetKey emjh_flag_map[] = {
	{ 0 }
};



/* ********************************************************************** */

/* Mail junk plugin */

/*
  <hook class="org.gnome.evolution.mail.junk:1.0">
  <group id="EMJunk">
     <item check_junk="sa_check_junk"
     	   report_junk="sa_report_junk"
	   report_non_junk="sa_report_non_junk"
	   commit_reports = "sa_commit_reports"/>
  </group>
  </hook>

*/

	

static void 
em_junk_init(CamelJunkPlugin *csp)
{
}

static const char *
em_junk_get_name (CamelJunkPlugin *csp)
{
	struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

	if (item->hook && item->hook->hook.plugin->enabled) {
		return (item->hook->hook.plugin->name);
	}
	else
		return 	_("Default: No plugin");	
}

static gboolean 
em_junk_check_junk(CamelJunkPlugin *csp, CamelMimeMessage *m)
{
	struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

	if (item->hook && item->hook->hook.plugin->enabled) {
		EMJunkHookTarget target = {
			 item, m
		};
		return (gboolean)(e_plugin_invoke(item->hook->hook.plugin, item->check_junk, &target));
	}

	return FALSE;
}

static void 
em_junk_report_junk(CamelJunkPlugin *csp, CamelMimeMessage *m)
{
	struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

	if (item->hook && item->hook->hook.plugin->enabled) {
		EMJunkHookTarget target = {
			 item, m
		};

		e_plugin_invoke(item->hook->hook.plugin, item->report_junk, &target);
	}
}

static void 
em_junk_report_non_junk(CamelJunkPlugin *csp, CamelMimeMessage *m)
{
	struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

	if (item->hook && item->hook->hook.plugin->enabled) {
		EMJunkHookTarget target = {
			item, m
		};
		e_plugin_invoke(item->hook->hook.plugin, item->report_non_junk, &target);
	}
}

static void 
em_junk_commit_reports(CamelJunkPlugin *csp)
{
	struct _EMJunkHookItem *item = (EMJunkHookItem *)csp;

	if (item->hook && item->hook->hook.plugin->enabled) {
		EMJunkHookTarget target = {
			 item, NULL
		};

		e_plugin_invoke(item->hook->hook.plugin, item->commit_reports, &target);
	}
}

static void 
emj_dispose (GObject *object)
{
	if (parent_class->dispose)
		parent_class->dispose (object);
}

static void 
emj_finalize (GObject *object)
{
	if (parent_class->finalize)
		parent_class->finalize (object);
}

static void
emjh_free_item(EMJunkHookItem *item)
{
	if (!item)
		return;

	if (item->check_junk)
		g_free (item->check_junk);
	if (item->report_junk)
		g_free (item->report_junk);
	if (item->report_non_junk)
		g_free (item->report_non_junk);
	if (item->commit_reports)
		g_free (item->commit_reports);
	g_free(item);
}

static void
emjh_free_group(EMJunkHookGroup *group)
{
	g_slist_foreach(group->items, (GFunc)emjh_free_item, NULL);
	g_slist_free(group->items);

	g_free(group->id);
	g_free(group);
}

static struct _EMJunkHookItem *
emjh_construct_item(EPluginHook *eph, EMJunkHookGroup *group, xmlNodePtr root)
{
	struct _EMJunkHookItem *item; 
	EMJunk junk_plugin = {
		{
			em_junk_get_name,
			1,
			em_junk_check_junk,
			em_junk_report_junk,
			em_junk_report_non_junk,
			em_junk_commit_reports,
			em_junk_init,
		}
	};

	d(printf("  loading group item\n"));
	item = g_malloc0(sizeof(*item));
	item->csp =  junk_plugin.csp;
	item->check_junk = e_plugin_xml_prop(root, "check_junk");
	item->report_junk = e_plugin_xml_prop(root, "report_junk");
	item->report_non_junk = e_plugin_xml_prop(root, "report_non_junk");
	item->commit_reports = e_plugin_xml_prop(root, "commit_reports");
	/* assign the plugin to the session*/
	session->junk_plugin = CAMEL_JUNK_PLUGIN (&(item->csp));
	camel_junk_plugin_init (session->junk_plugin);
	item->hook = emjh;
	if (item->check_junk == NULL || item->report_junk == NULL || item->report_non_junk == NULL || item->commit_reports == NULL)
		goto error;
	
	return item;
error:
	printf ("ERROR");
	emjh_free_item (item);
	return NULL;
}

static struct _EMJunkHookGroup *
emjh_construct_group(EPluginHook *eph, xmlNodePtr root)
{
	struct _EMJunkHookGroup *group;
	xmlNodePtr node;

	d(printf(" loading group\n"));
	group = g_malloc0(sizeof(*group));

	group->id = e_plugin_xml_prop(root, "id");
	if (group->id == NULL)
		goto error;

	node = root->children;
	
	/* We'll processs only  the first item from xml file*/
	while (node) {
		if (0 == strcmp(node->name, "item")) {
			struct _EMJunkHookItem *item;

			item = emjh_construct_item(eph, group, node);
			if (item)
				group->items = g_slist_append(group->items, item);
			break;
		}
		node = node->next;
	}

	return group;
error:
	emjh_free_group(group);
	return NULL;
}

static int
emjh_construct(EPluginHook *eph, EPlugin *ep, xmlNodePtr root)
{
	xmlNodePtr node;

	d(printf("loading junk hook\n"));

	if (((EPluginHookClass *)emjh_parent_class)->construct(eph, ep, root) == -1)
		return -1;

	node = root->children;
	while (node) {
		if (strcmp(node->name, "group") == 0) {
			struct _EMJunkHookGroup *group;

			group = emjh_construct_group(eph, node);
			if (group) {
				emjh->groups = g_slist_append(emjh->groups, group);
			}
		}
		node = node->next;
	}

	eph->plugin = ep;

	return 0;
}

/*XXX: don't think we need here*/
static void
emjh_enable(EPluginHook *eph, int state)
{
	GSList *g;
	
	g = emjh->groups;
	if (emjh_types == NULL)
		return;

}

static void
emjh_finalise(GObject *o)
{
	EPluginHook *eph = (EPluginHook *)o;

	g_slist_foreach(emjh->groups, (GFunc)emjh_free_group, NULL);
	g_slist_free(emjh->groups);

	((GObjectClass *)emjh_parent_class)->finalize(o);
}

static void
emjh_class_init(EPluginHookClass *klass)
{
	((GObjectClass *)klass)->finalize = emjh_finalise;
	klass->construct = emjh_construct;
	klass->enable = emjh_enable;
	klass->id = "org.gnome.evolution.mail.junk:1.0";
}

GType
em_junk_hook_get_type(void)
{
	static GType type = 0;
	
	if (!type) {
		static const GTypeInfo info = {
			sizeof(EMJunkHookClass), NULL, NULL, (GClassInitFunc) emjh_class_init, NULL, NULL,
			sizeof(EMJunkHook), 0, (GInstanceInitFunc) NULL,
		};

		emjh_parent_class = g_type_class_ref(e_plugin_hook_get_type());
		type = g_type_register_static(e_plugin_hook_get_type(), "EMJunkHook", &info, 0);
	}
	
	return type;
}

static void
emj_class_init (EMJunkClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	parent_class = g_type_class_peek_parent (klass);
	object_class->dispose = emj_dispose;
	object_class->finalize = emj_finalize;
}

GType
emj_get_type(void)
{
	static GType type = 0;
	
	if (!type) {
		static const GTypeInfo info = {
			sizeof(EMJunkClass), NULL, NULL, (GClassInitFunc) emj_class_init, NULL, NULL,
			sizeof(EMJunk), 0, (GInstanceInitFunc) NULL,
		};

		emj_parent = g_type_class_ref(G_TYPE_OBJECT);
		type = g_type_register_static(G_TYPE_OBJECT, "EMJunk", &info, 0);
	}
	
	return type;
}

void em_junk_hook_register_type(GType type)
{
	EMJunk *klass;

	if (emjh_types == NULL)
		emjh_types = g_hash_table_new(g_str_hash, g_str_equal);

	d(printf("registering junk plugin type '%s'\n", g_type_name(type)));

	klass = g_type_class_ref(type);
	g_hash_table_insert(emjh_types, (void *)g_type_name(type), klass);
}
Index: mail-session.c
===================================================================
RCS file: /cvs/gnome/evolution/mail/mail-session.c,v
retrieving revision 1.105
diff -u -p -r1.105 mail-session.c
--- mail-session.c	24 Jun 2005 17:38:12 -0000	1.105
+++ mail-session.c	4 Jul 2005 10:56:29 -0000
@@ -54,7 +54,6 @@
 #include "mail-ops.h"
 #include <libedataserverui/e-passwords.h>
 #include "libedataserver/e-msgport.h"
-#include "em-junk-filter.h"
 #include "e-util/e-error.h"
 
 #define d(x)
@@ -653,10 +652,7 @@ mail_session_init (const char *base_dire
 	session_check_junk_notify_id = gconf_client_notify_add (gconf, "/apps/evolution/mail/junk",
 								(GConfClientNotifyFunc) mail_session_check_junk_notify,
 								session, NULL, NULL);
-	session->junk_plugin = CAMEL_JUNK_PLUGIN (em_junk_filter_get_plugin ());
-	if (session->junk_plugin)
-		camel_junk_plugin_init (session->junk_plugin);
-
+	session->junk_plugin = NULL;
 	/* The shell will tell us to go online. */
 	camel_session_set_online ((CamelSession *) session, FALSE);
 	
Index: camel-junk-plugin.c
===================================================================
RCS file: /cvs/gnome/evolution-data-server/camel/camel-junk-plugin.c,v
retrieving revision 1.4
diff -u -p -r1.4 camel-junk-plugin.c
--- camel-junk-plugin.c	15 Feb 2005 11:12:51 -0000	1.4
+++ camel-junk-plugin.c	4 Jul 2005 10:58:28 -0000
@@ -31,21 +31,23 @@
 const char *
 camel_junk_plugin_get_name (CamelJunkPlugin *csp)
 {
+	g_return_val_if_fail (csp != NULL, NULL);
 	g_return_val_if_fail (csp->get_name != NULL, NULL);
 
 	d(fprintf (stderr, "camel_junk_plugin_get_namen"));
 
-	return (*csp->get_name) ();
+	return csp->get_name (csp);
 }
 
 int
 camel_junk_plugin_check_junk (CamelJunkPlugin *csp, CamelMimeMessage *message)
 {
+	g_return_val_if_fail (csp != NULL, FALSE);
 	g_return_val_if_fail (csp->check_junk != NULL, FALSE);
 
 	d(fprintf (stderr, "camel_junk_plugin_check_junk\n"));
 
-	return (*csp->check_junk) (message);
+	return csp->check_junk (csp, message);
 }
 
 void
@@ -53,8 +55,8 @@ camel_junk_plugin_report_junk (CamelJunk
 {
 	d(fprintf (stderr, "camel_junk_plugin_report_junk\n"));
 
-	if (csp->report_junk)
-		(*csp->report_junk) (message);
+	if (csp && csp->report_junk)
+		csp->report_junk (csp, message);
 }
 
 void
@@ -62,8 +64,8 @@ camel_junk_plugin_report_notjunk (CamelJ
 {
 	d(fprintf (stderr, "camel_junk_plugin_report_notjunk\n"));
 
-	if (csp->report_notjunk)
-		(*csp->report_notjunk) (message);
+	if (csp && csp->report_notjunk)
+		csp->report_notjunk (csp, message);
 }
 
 void
@@ -71,8 +73,8 @@ camel_junk_plugin_commit_reports (CamelJ
 {
 	d(fprintf (stderr, "camel_junk_plugin_commit_reports\n"));
 
-	if (csp->commit_reports)
-		(*csp->commit_reports) ();
+	if (csp && csp->commit_reports)
+		csp->commit_reports (csp);
 }
 
 void
@@ -80,6 +82,6 @@ camel_junk_plugin_init (CamelJunkPlugin 
 {
 	d(fprintf (stderr, "camel_junk_plugin_init\n"));
 
-	if (csp->init)
-		(*csp->init) ();
+	if (csp && csp->init)
+		csp->init (csp);
 }


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