CVS: sylpheedclaws/plugins/attachwarner/src attachwarner_prefs.c,NONE,1.1.2.1 attachwarner_prefs.h,NONE,1.1.2.1 Makefile.am,1.1.2.1,1.1.2.2 attachwarner.c,1.1.2.4,1.1.2.5

[email protected] Wed, 20 Dec 2006 18:12:16 +0000
Newsgroups gmane.mail.sylpheed.claws.cvs
Message-ID <[email protected]>
Update of /pack/anoncvs/sylpheedclaws/plugins/attachwarner/src
In directory sunsite.dk:/tmp/cvs-serv17046/src

Modified Files:
      Tag: gtk2
	Makefile.am attachwarner.c 
Added Files:
      Tag: gtk2
	attachwarner_prefs.c attachwarner_prefs.h 
Log Message:
2006-12-20 [colin]	0.2.2cvs2

	* src/Makefile.am
	* src/attachwarner.c
	* src/attachwarner_prefs.c
	* src/attachwarner_prefs.h
		Add basic prefs page allowing to set the
		regular expressions to match

--- NEW FILE: attachwarner_prefs.c ---
/*
 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail Team
 * Copyright (C) 2006 Ricardo Mones
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <gtk/gtk.h>
#include "defs.h"
#include "gettext.h"
#include "attachwarner_prefs.h"
#include "prefs_common.h"
#include "prefs_gtk.h"

#define PREFS_BLOCK_NAME "AttachWarner"

AttachWarnerPrefs attwarnerprefs;

struct AttachWarnerPrefsPage
{
	PrefsPage page;
	
	GtkWidget *regexp_text;
};

struct AttachWarnerPrefsPage attwarnerprefs_page;

static PrefParam param[] = {
	{"match_strings", N_("attach"), &attwarnerprefs.match_strings, P_STRING,
	 NULL, NULL, NULL},
	{NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
};

static void attwarner_prefs_create_widget_func(PrefsPage * _page,
					   GtkWindow * window,
					   gpointer data)
{
	struct AttachWarnerPrefsPage *page = (struct AttachWarnerPrefsPage *) _page;
	GtkWidget *vbox, *hbox;;
	GtkWidget *label;
	GtkWidget *scrolledwin;
	GtkTextBuffer *buffer;

	vbox = gtk_vbox_new(FALSE, 6);
	hbox = gtk_hbox_new(FALSE, 6);
	
	label = gtk_label_new(_("Warn when matching the following regular expressions:\n(one per line)"));
	gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
	gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 0);
	page->regexp_text = gtk_text_view_new();
	
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->regexp_text));
	gtk_text_buffer_set_text(buffer, attwarnerprefs.match_strings, -1);
	
	scrolledwin = gtk_scrolled_window_new(NULL, NULL);
	gtk_scrolled_window_set_policy
		(GTK_SCROLLED_WINDOW (scrolledwin),
		 GTK_POLICY_NEVER, GTK_POLICY_AUTOMATIC);
	gtk_scrolled_window_set_shadow_type
		(GTK_SCROLLED_WINDOW (scrolledwin), GTK_SHADOW_IN);

	gtk_container_add(GTK_CONTAINER(scrolledwin), page->regexp_text);
	gtk_widget_set_size_request(page->regexp_text, -1, 100);
	gtk_box_pack_start(GTK_BOX(vbox), scrolledwin, FALSE, FALSE, 0);
	
	gtk_box_pack_start(GTK_BOX(hbox), vbox, FALSE, FALSE, 6);
	gtk_widget_show_all(hbox);
	
	page->page.widget = hbox;

}

static void attwarner_prefs_destroy_widget_func(PrefsPage *_page)
{
}

static void attwarner_save_config(void)
{
	PrefFile *pfile;
	gchar *rcpath;

	debug_print("Saving AttachWarner Page\n");

	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
	pfile = prefs_write_open(rcpath);
	g_free(rcpath);
	if (!pfile || (prefs_set_block_label(pfile, PREFS_BLOCK_NAME) < 0))
		return;

	if (prefs_write_param(param, pfile->fp) < 0) {
		g_warning("Failed to write AttachWarner configuration to file\n");
		prefs_file_close_revert(pfile);
		return;
	}
	fprintf(pfile->fp, "\n");

	prefs_file_close(pfile);
}


static void attwarner_prefs_save_func(PrefsPage * _page)
{
	struct AttachWarnerPrefsPage *page = (struct AttachWarnerPrefsPage *) _page;
	GtkTextBuffer *buffer;
	GtkTextIter start, end;
	gchar *tmp;
	buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(page->regexp_text));
	
	g_free(attwarnerprefs.match_strings);
	
	gtk_text_buffer_get_start_iter(buffer, &start);
	gtk_text_buffer_get_end_iter(buffer, &end);
	
	tmp = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
	
	attwarnerprefs.match_strings = g_malloc(2*strlen(tmp)+1);
	pref_get_escaped_pref(attwarnerprefs.match_strings, tmp);
	attwarner_save_config();
	g_free(attwarnerprefs.match_strings);
	attwarnerprefs.match_strings = tmp;
}

void attachwarner_prefs_init(void)
{
	static gchar *path[3];
	gchar *rcpath;
	gchar *tmp;
	
	path[0] = _("Plugins");
	path[1] = _("Attach Warner");
	path[2] = NULL;

	prefs_set_default(param);
	rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
	prefs_read_config(param, PREFS_BLOCK_NAME, rcpath, NULL);
	g_free(rcpath);

	tmp = g_malloc(strlen(attwarnerprefs.match_strings)+1);
	pref_get_unescaped_pref(tmp, attwarnerprefs.match_strings);
	
	g_free(attwarnerprefs.match_strings);
	attwarnerprefs.match_strings = tmp;
	
	attwarnerprefs_page.page.path = path;
	attwarnerprefs_page.page.create_widget = attwarner_prefs_create_widget_func;
	attwarnerprefs_page.page.destroy_widget = attwarner_prefs_destroy_widget_func;
	attwarnerprefs_page.page.save_page = attwarner_prefs_save_func;

	prefs_gtk_register_page((PrefsPage *) &attwarnerprefs_page);
}

void attachwarner_prefs_done(void)
{
	prefs_gtk_unregister_page((PrefsPage *) &attwarnerprefs_page);

}

--- NEW FILE: attachwarner_prefs.h ---
/*
 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail Team
 * Copyright (C) 2006 Ricardo Mones
 *
 * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#ifndef __ATTACHWARNER_PREFS__
#define __ATTACHWARNER_PREFS__

#include <glib.h>

typedef struct _AttachWarnerPrefs AttachWarnerPrefs;

struct _AttachWarnerPrefs
{
	gchar		 *match_strings;
};

extern AttachWarnerPrefs attwarnerprefs;
void attachwarner_prefs_init(void);
void attachwarner_prefs_done(void);

#endif

Index: Makefile.am
===================================================================
RCS file: /pack/anoncvs/sylpheedclaws/plugins/attachwarner/src/Attic/Makefile.am,v
retrieving revision 1.1.2.1
retrieving revision 1.1.2.2
diff -u -d -r1.1.2.1 -r1.1.2.2
--- Makefile.am	2006/11/16 12:17:11	1.1.2.1
+++ Makefile.am	2006/12/20 18:12:11	1.1.2.2
@@ -15,5 +15,6 @@
 
 attachwarner_la_SOURCES = \
 	attachwarner.c attachwarner.h \
+	attachwarner_prefs.c attachwarner_prefs.h \
 	gettext.h
 

Index: attachwarner.c
===================================================================
RCS file: /pack/anoncvs/sylpheedclaws/plugins/attachwarner/src/Attic/attachwarner.c,v
retrieving revision 1.1.2.4
retrieving revision 1.1.2.5
diff -u -d -r1.1.2.4 -r1.1.2.5
--- attachwarner.c	2006/12/15 18:51:47	1.1.2.4
+++ attachwarner.c	2006/12/20 18:12:11	1.1.2.5
@@ -19,30 +19,77 @@
  */
 
 #include "attachwarner.h"
+#include "attachwarner_prefs.h"
+#include "codeconv.h"
 
 /** Identifier for the hook. */
 static guint hook_id;
 
-/** Matcher for finding the text. */
-static MatcherProp *matcher = NULL;
-
 /**
  * Creates the matcher.
  *
  * @return A newly allocated regexp matcher or null if no memory is available.
  */
+
+static gchar *build_complete_regexp(gchar **strings)
+{
+	int i = 0;
+	gchar *expr = NULL;
+	while (strings && strings[i] && *strings[i]) {
+		int old_len = expr ? strlen(expr):0;
+		int new_len = 0;
+		gchar *tmpstr = NULL;
+
+		if (g_utf8_validate(strings[i], -1, NULL))
+			tmpstr = g_strdup(strings[i]);
+		else
+			tmpstr = conv_codeset_strdup
+				(strings[i], conv_get_locale_charset_str_no_utf8(),
+				 CS_INTERNAL);
+
+		if (strstr(tmpstr, "\n"))
+			*(strstr(tmpstr, "\n")) = '\0';
+
+		new_len = strlen(tmpstr);
+
+		expr = g_realloc(expr, expr ?(old_len+strlen("|()")+new_len+1): 
+					     (strlen("()")+new_len+1));
+		
+		if (old_len) {
+			strcpy(expr+old_len, "|(");
+			strcpy(expr+old_len+2, tmpstr);
+			strcpy(expr+old_len+2+new_len, ")");
+		} else {
+			strcpy(expr+old_len, "(");
+			strcpy(expr+old_len+1, tmpstr);
+			strcpy(expr+old_len+1+new_len, ")");
+		}
+		g_free(tmpstr);
+		i++;
+	}
+	return expr;
+}
+
 MatcherProp * new_matcherprop(void)
 {
 	MatcherProp *m = NULL;
-	const gchar *expr = _("attach.*");
+	gchar **strings = g_strsplit(attwarnerprefs.match_strings, 
+				"\n", -1);
+	gchar *expr = NULL;
+	
+	expr = build_complete_regexp(strings);
 
-	debug_print("building matcherprop for expr (%s)\n", expr);
+	g_strfreev(strings);
+	
+	debug_print("building matcherprop for expr '%s'\n", expr);
 	m = matcherprop_new(MATCHCRITERIA_SUBJECT, NULL, MATCHTYPE_REGEXP, expr, 0);
 	if (m == NULL) {
 		/* print error message */
 		debug_print("failed to allocate memory for matcherprop\n");
 	}
 
+	g_free(expr);
+
 	return m;
 }
 
@@ -78,6 +125,13 @@
 	gchar *text = NULL;
 	gboolean mentioned = FALSE;
 
+	MatcherProp *matcher = new_matcherprop();
+
+	if (matcher == NULL) {
+		g_warning("couldn't allocate matcher");
+		return FALSE;
+	}
+
 	textview = GTK_TEXT_VIEW(compose->text);
         textbuffer = gtk_text_view_get_buffer(textview);
 	gtk_text_buffer_get_start_iter(textbuffer, &start);
@@ -85,12 +139,15 @@
 	text = gtk_text_buffer_get_text(textbuffer, &start, &end, FALSE);
 
 	debug_print("checking text for attachment mentions\n");
-	if (matcher != NULL && text != NULL) {
+	if (text != NULL) {
 		mentioned = matcherprop_string_match(matcher, text);
 		
 		g_free(text);
 	}	
 
+	if (matcher != NULL)
+		matcherprop_free(matcher);
+
 	return mentioned;
 }
 
@@ -172,12 +229,7 @@
 		return -1;
 	}
 
-	matcher = new_matcherprop();
-
-	if (matcher == NULL) {
-		*error = g_strdup(_("Failed to get memory for matcher"));
-		return -1;
-	}
+	attachwarner_prefs_init();
 
 	debug_print("Attachment warner plugin loaded\n");
 
@@ -191,8 +243,7 @@
 void plugin_done(void)
 {	
 	hooks_unregister_hook(COMPOSE_CHECK_BEFORE_SEND_HOOKLIST, hook_id);
-	matcherprop_free(matcher);
-
+	attachwarner_prefs_done();
 	debug_print("Attachment warner plugin unloaded\n");
 }