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



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 <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 void em_junk_get_name(CamelJunkPlugin *csp);
static gboolean em_junk_check_junk(CamelJunkPlugin *csp, CamelMimeMessage *m);
static void em_junk_report_junk(CamelJunkPlugin *csp, CamelMimeMessage *m);
static void em_junk_report_non_junk(CamelJunkPlugin *csp, CamelMimeMessage *m);
static void em_junk_commit_reports(CamelJunkPlugin *csp);
static void em_junk_init(CamelJunkPlugin *csp);

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

static 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,
	}
};

EMJunk *
em_junk_get_plugin()
{
	return &junk_plugin;
}

static void 
em_junk_init(CamelJunkPlugin *csp)
{
}

static void 
em_junk_get_name(CamelJunkPlugin *csp)
{

}

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

	if (item->hook->hook.plugin->enabled) {
		EMJunkHookTarget target = {
			 item, m
		};
		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->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->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->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)
{
	g_free (item->check_junk);
	g_free (item->report_junk);
	g_free (item->report_non_junk);
	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; 

	d(printf("  loading group item\n"));
	item = 	(struct _EMJunkHookItem * ) (&(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");

	item->hook = emjh;

	return item;
}

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;
	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);
		}
		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);

	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);
}
/* -*- 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.
 *
 */

#ifndef __EM_FORMAT_HOOK_H__
#define __EM_FORMAT_HOOK_H__

#include <glib-object.h>
#include "libedataserver/e-msgport.h"
#include "e-util/e-plugin.h"
#include <camel/camel-junk-plugin.h>
#include <camel/camel-mime-message.h>
#ifdef __cplusplus
extern "C" {
#pragma }
#endif /* __cplusplus */

typedef struct _EMJunkHookItem EMJunkHookItem;
typedef struct _EMJunkHookGroup EMJunkHookGroup;
typedef struct _EMJunkHook EMJunkHook;
typedef struct _EMJunkHookClass EMJunkHookClass;
typedef struct _EMJunk EMJunk;
typedef struct _EMJunkClass EMJunkClass;

typedef struct _EMJunkHookTarget EMJunkHookTarget;

typedef void (*EMJunkHookFunc)(struct _EPlugin *plugin, EMJunkHookTarget *data);

struct _EMJunkHookTarget {
	struct _EMJunkHookItem *item;
	CamelMimeMessage *m;
};

struct _EMJunkHookItem {
	CamelJunkPlugin csp;
	struct _EMJunkHook *hook; /* parent pointer */
	char *check_junk;
	char *report_junk;
	char *report_non_junk;
	char *commit_reports;
};

struct _EMJunkHookGroup {
	struct _EMJunkHook *hook; /* parent pointer */
	char *id;		/* target id */
	GSList *items;		/* items to consider */
};

struct _EMJunkHook {
	EPluginHook hook;
	GSList *groups;
};

struct _EMJunkHookClass {
	EPluginHookClass hook_class;

	/* which class to add matching items to */
	GHashTable *junk_classes;
};

GType em_junk_hook_get_type(void);
void em_junk_hook_register_type(GType type);

struct _EMJunk {
        CamelJunkPlugin csp;
};

struct _EMJunkClass {
	GObjectClass parent_class;
	/* which class to add matching items to */
	GHashTable *junk_classes;
};

GType emj_get_type(void);
void em_junk_hook_register_type(GType type);

struct _EMJunk * em_junk_get_plugin ();
#ifdef __cplusplus
}
#endif /* __cplusplus */

#endif /* __EM_JUNK_HOOK_H__ */
Index: ChangeLog
===================================================================
RCS file: /cvs/gnome/evolution-data-server/camel/ChangeLog,v
retrieving revision 1.2450
diff -u -p -r1.2450 ChangeLog
--- ChangeLog	9 Jun 2005 07:00:23 -0000	1.2450
+++ ChangeLog	23 Jun 2005 14:09:54 -0000
@@ -1,3 +1,8 @@
+2005-06-23  Vivek Jain <jvivek novell com>
+	
+	* camel-junk-plugin.[ch]: pass CamelJunkPlugin object in all the
+	 callbacks
+
 2005-06-09  Sarfraaz Ahmed <asarfraaz novell com>
 
 	* camel-store.c (camel_folder_info_build): Minor typo in debug message.
Index: camel-junk-plugin.h
===================================================================
RCS file: /cvs/gnome/evolution-data-server/camel/camel-junk-plugin.h,v
retrieving revision 1.3
diff -u -p -r1.3 camel-junk-plugin.h
--- camel-junk-plugin.h	25 Nov 2004 10:50:11 -0000	1.3
+++ camel-junk-plugin.h	23 Jun 2005 14:09:54 -0000
@@ -28,30 +28,32 @@
 typedef struct _CamelJunkPlugin CamelJunkPlugin;
 struct _CamelMimeMessage;
 
+
 struct _CamelJunkPlugin
 {
 	/* junk filter human readable name, translated */
-	const char * (*get_name) (void);
+	const char * (*get_name) (CamelJunkPlugin *csp);
 
 	/* should be set to 1 */
 	int api_version;
 
 	/* when called, it should return TRUE if message is identified as junk,
 	   FALSE otherwise */
-	int (*check_junk) (struct _CamelMimeMessage *message);
+	int (*check_junk) (CamelJunkPlugin *csp, struct _CamelMimeMessage *message);
 
 	/* called when user identified a message to be junk */
-	void (*report_junk) (struct _CamelMimeMessage *message);
+	void (*report_junk) (CamelJunkPlugin *csp, struct _CamelMimeMessage *message);
 
 	/* called when user identified a message not to be junk */
-	void (*report_notjunk) (struct _CamelMimeMessage *message);
+	void (*report_notjunk) (CamelJunkPlugin *csp, struct _CamelMimeMessage *message);
 
 	/* called after one or more junk/ham(s) reported */
-	void (*commit_reports) (void);
+	void (*commit_reports) (CamelJunkPlugin *csp);
 
 	/* called before all other calls to junk plugin for initialization */
-	void (*init) (void);
+	void (*init) (CamelJunkPlugin *csp);
 };
+
 
 const char * camel_junk_plugin_get_name (CamelJunkPlugin *csp);
 int camel_junk_plugin_check_junk (CamelJunkPlugin *csp, struct _CamelMimeMessage *message);
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	23 Jun 2005 14:09:54 -0000
@@ -35,7 +35,7 @@ camel_junk_plugin_get_name (CamelJunkPlu
 
 	d(fprintf (stderr, "camel_junk_plugin_get_namen"));
 
-	return (*csp->get_name) ();
+	return (*csp->get_name) (csp);
 }
 
 int
@@ -45,7 +45,7 @@ camel_junk_plugin_check_junk (CamelJunkP
 
 	d(fprintf (stderr, "camel_junk_plugin_check_junk\n"));
 
-	return (*csp->check_junk) (message);
+	return (*csp->check_junk) (csp, message);
 }
 
 void
@@ -54,7 +54,7 @@ camel_junk_plugin_report_junk (CamelJunk
 	d(fprintf (stderr, "camel_junk_plugin_report_junk\n"));
 
 	if (csp->report_junk)
-		(*csp->report_junk) (message);
+		(*csp->report_junk) (csp, message);
 }
 
 void
@@ -63,7 +63,7 @@ camel_junk_plugin_report_notjunk (CamelJ
 	d(fprintf (stderr, "camel_junk_plugin_report_notjunk\n"));
 
 	if (csp->report_notjunk)
-		(*csp->report_notjunk) (message);
+		(*csp->report_notjunk) (csp, message);
 }
 
 void
@@ -72,7 +72,7 @@ camel_junk_plugin_commit_reports (CamelJ
 	d(fprintf (stderr, "camel_junk_plugin_commit_reports\n"));
 
 	if (csp->commit_reports)
-		(*csp->commit_reports) ();
+		(*csp->commit_reports) (csp);
 }
 
 void
@@ -81,5 +81,5 @@ camel_junk_plugin_init (CamelJunkPlugin 
 	d(fprintf (stderr, "camel_junk_plugin_init\n"));
 
 	if (csp->init)
-		(*csp->init) ();
+		(*csp->init) (csp);
 }


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