[Commits] Makefile.am 1.2 1.3 ldifimport_extension.c 1.1 1.2 ldifimport_parser.c NONE 1.1 ldifimport_parser.h NONE 1.1

miras at claws-mail.org miras at claws-mail.org
Sat Dec 17 03:55:15 CET 2011


Update of /home/claws-mail/contacts/extensions/import
In directory claws-mail:/tmp/cvs-serv17694/extensions/import

Modified Files:
	Makefile.am ldifimport_extension.c 
Added Files:
	ldifimport_parser.c ldifimport_parser.h 
Log Message:
2011-12-17 [mir]	0.6.0cvs50

	* extensions/import/Makefile.am
	* extensions/import/ldifimport_extension.c
	* extensions/import/ldifimport_parser.c
	* extensions/import/ldifimport_parser.h
	    Completed the LDIF import extension. 

Index: Makefile.am
===================================================================
RCS file: /home/claws-mail/contacts/extensions/import/Makefile.am,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- Makefile.am	3 Dec 2011 21:18:38 -0000	1.2
+++ Makefile.am	17 Dec 2011 02:55:13 -0000	1.3
@@ -20,7 +20,9 @@
 		-DG_LOG_DOMAIN=\"Claws-Contacts\"
 
 ldifimport_extension_la_SOURCES = \
-		    ldifimport_extension.c
+		    ldifimport_extension.c \
+			ldifimport_parser.h \
+			ldifimport_parser.c
 
 ldifimport_extension_la_LIBADD= \
 		   @GLIB_LIBS@ \

Index: ldifimport_extension.c
===================================================================
RCS file: /home/claws-mail/contacts/extensions/import/ldifimport_extension.c,v
retrieving revision 1.1
retrieving revision 1.2
diff -u -d -r1.1 -r1.2
--- ldifimport_extension.c	30 Nov 2011 23:35:12 -0000	1.1
+++ ldifimport_extension.c	17 Dec 2011 02:55:13 -0000	1.2
@@ -0,0 +1,229 @@
+/*
+ * $Id$
+ */
+/* vim:et:ts=4:sw=4:et:sts=4:ai:set list listchars=tab\:»·,trail\:·: */
+
+/*
+ * Claws-contacts is a proposed new design for the address book feature
+ * in Claws Mail. The goal for this new design was to create a
+ * solution more suitable for the term lightweight and to be more
+ * maintainable than the present implementation.
+ *
+ * More lightweight is achieved by design, in that sence that the whole
+ * structure is based on a plugable design.
+ *
+ * Claws Mail is Copyright (C) 1999-2011 by the Claws Mail Team and
+ * Claws-contacts is Copyright (C) 2011 by Michael Rasmussen.
+ *
+ * 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 3 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, see <http://www.gnu.org/licenses/>.
+ * 
+ */
+#ifdef HAVE_CONFIG_H
+#       include <config.h>
+#endif
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+
+#include "extension.h"
+#include "utils.h"
+#include "gtk-utils.h"
+#include "plugin.h"
+#include "plugin-loader.h"
+#include "callbacks.h"
+#include "ldifimport_parser.h"
+
+#define NAME "LDIF importer extension"
+
+static guint my_id;
+
+static void ldif_import(GtkWidget* widget, gpointer data) {
+	MainWindow* Main = (MainWindow*) data;
+	LdifParser* parser;
+	GtkTreeView* view;
+	AddressBook* abook;
+	Plugin* plugin;
+	Contact* contact;
+	Record* record;
+	GtkTreeIter* iter = NULL;
+	GtkTreeSelection* row;
+	GSList *attribs, *cur;
+	gchar* value;
+	gchar* err;
+	
+	view = GTK_TREE_VIEW(Main->abook_list);
+	abook = get_selected_address_book(view);
+	if (abook == NULL) {
+		iter = set_selection_combobox(Main->window,
+						_("[New Contact] Choose address book"),
+						gtk_tree_view_get_model(view),
+						BOOK_NAME_COLUMN);
+		if (! iter) {
+			return;
+		}
+		row = gtk_tree_view_get_selection(view);
+		gtk_tree_selection_select_iter(row, iter);
+		g_free(iter);
+	}
+	plugin = get_selected_plugin(view);
+	
+	parser = ldif_parser_new();
+	gint fd = open("/home/mir/Projekter/c/claws/claws_rw/new-claws/contacts/test.ldif", O_RDONLY);
+	ldif_parser_set_fd(parser, fd);
+	attribs = plugin->attrib_list();
+
+	cur = attribs;
+	while (cur) {
+		AttribDef* attr = (AttribDef *) cur->data;
+		const gchar* key = native2ldap(attr->attrib_name);
+		ldif_parser_add_symbol(parser, key);
+		cur = cur->next;
+	}
+	gslist_free(&attribs, attrib_def_free);
+	ldif_parser_parse(parser);
+	close(fd);
+	if (ldif_parser_get_errors(parser) > 0) {
+		show_message(Main->window, GTK_UTIL_MESSAGE_WARNING, "%s:%d:%d [%d] %s",
+			parser->input_name, parser->error_line, parser->error_place,
+			parser->error_code, parser->error_msg);
+	}
+	cur = ldif_parser_get_records(parser);
+	while (cur) {
+		if (debug_get_mode()) {
+			ldif_parser_record_dump(cur->data);
+		}
+		record = (Record *) cur->data;
+		contact = contact_new();
+		for (attribs = record->tokens; attribs; attribs = g_slist_next(attribs)) {
+			Token* token = (Token *) attribs->data;
+			value = NULL;
+			if (token->type == LDIF_PARSER_BVALS) {
+				value = g_base64_encode(
+					(const guchar *) token->token_bvals.bv_val,
+					token->token_bvals.bv_len);
+			}
+			else {
+				value = g_strdup(token->value);
+			}
+			debug_print("Attribute: %s - Value: %s\n", ldap2native(token->name), value);
+			if (strcasecmp("mail", token->name) == 0) {
+				/* add email */
+				Email* email = g_new0(Email, 1);
+				email->email = g_strdup(value);
+				contact->emails = g_slist_prepend(contact->emails, email);
+			}
+			else {
+				/* some other attribute */
+				swap_data(contact->data, ldap2native(token->name), value);
+			}
+			g_free(value);
+		}
+		err = NULL;
+		plugin->set_contact(abook, contact, &err);
+		if (err) {
+			show_message(Main->window, GTK_UTIL_MESSAGE_WARNING, "%s", err);
+			g_free(err);
+			err = NULL;
+			contact_free(contact);
+		}
+		else {
+			abook->contacts = g_list_prepend(abook->contacts, contact);
+			abook->dirty = TRUE;
+			list_view_append_contact(GTK_TREE_VIEW(Main->contact_list), contact);
+			while (gtk_events_pending())
+				gtk_main_iteration();
+		}
+		cur = cur->next;
+	}
+	gslist_free(&cur, ldif_parser_record_free);
+
+	ldif_parser_destroy(parser);
+}
+
+static void setup(const MainWindow* mainwindow, gpointer object) {
+	GtkWidget *menu;
+	MenuItem* menu_item;
+	
+	menu = gtk_image_menu_item_new_with_mnemonic("_LDIF");
+	gtk_widget_set_name(menu, "toolsmenu");
+    g_signal_connect(menu, "activate",
+            G_CALLBACK(ldif_import), (gpointer) mainwindow);
+    menu_item = menu_item_new();
+    menu_item->menu = CONTACTS_MAIN_MENU;
+    menu_item->parent = "tools";
+	menu_item->sublabel = "_Import";
+    menu_item->submenu = TRUE;
+	add_menu_item(GTK_IMAGE_MENU_ITEM(menu), menu_item);
+
+	menu = gtk_image_menu_item_new_with_mnemonic("_LDIF");
+	gtk_widget_set_name(menu, "contextmenu");
+    g_signal_connect(menu, "activate",
+            G_CALLBACK(ldif_import), (gpointer) mainwindow);
+    menu_item = menu_item_new();
+    menu_item->menu = CONTACTS_ADDRESSBOOK_MENU;
+    menu_item->sublabel = "_Import";
+    menu_item->submenu = TRUE;
+	add_menu_item(GTK_IMAGE_MENU_ITEM(menu), menu_item);
+}
+
+/**
+ * The main application will call this function after loading the
+ * extension providing a uniq id for the extension which is to be
+ * used for further references
+ * @param id uniq id provided by main application
+ * @return 0 if success 1 otherwise
+ */
+gint extension_init(guint id) {
+	my_id = id;
+	gchar* error = NULL;
+	
+	register_hook_function(my_id, EXTENSION_AFTER_INIT_HOOK, setup, &error);
+	return 0;
+}
+
+/**
+ * Called by main application when the extension should be unloaded
+ * @return TRUE if success FALSE otherwise
+ */
+gboolean extension_done(void) {
+	return TRUE;
+}
+
+/**
+ * Called by main application to ensure extension license is compatible
+ * @return license
+ */
+const gchar* extension_license(void) {
+	return "GPL3+";
+}
+
+/**
+ * Called by main application to get name of extension
+ * @return name
+ */
+const gchar* extension_name(void) {
+	return NAME;
+}
+
+/**
+ * Called by main application to get extension's describtion
+ * @return description
+ */
+const gchar* extension_describtion(void) {
+	return _("Import file in LDIF format into an address book");
+}

--- NEW FILE: ldifimport_parser.h ---
(This appears to be a binary file; contents omitted.)

--- NEW FILE: ldifimport_parser.c ---
(This appears to be a binary file; contents omitted.)



More information about the Commits mailing list