[Commits] Makefile.am 1.2 1.3 ldifexport_extension.c 1.3 1.4 wizard.c NONE 1.1 wizard.h NONE 1.1
miras at claws-mail.org
miras at claws-mail.org
Tue Dec 13 15:29:44 CET 2011
Update of /home/claws-mail/contacts/extensions/export
In directory claws-mail:/tmp/cvs-serv25154/extensions/export
Modified Files:
Makefile.am ldifexport_extension.c
Added Files:
wizard.c wizard.h
Log Message:
2011-12-13 [mir] 0.6.0cvs44
* extensions/export/Makefile.am
* extensions/export/ldifexport_extension.c
* extensions/export/wizard.c
* extensions/export/wizard.h
* src/gtk-utils.c
Completed implementation of LDIF export.
Compatibility tested and found working
100% against Jxplorer-3.2.2rc1.
--- NEW FILE: wizard.c ---
(This appears to be a binary file; contents omitted.)
--- NEW FILE: wizard.h ---
(This appears to be a binary file; contents omitted.)
Index: Makefile.am
===================================================================
RCS file: /home/claws-mail/contacts/extensions/export/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 13 Dec 2011 14:29:42 -0000 1.3
@@ -20,7 +20,9 @@
-DG_LOG_DOMAIN=\"Claws-Contacts\"
ldifexport_extension_la_SOURCES = \
- ldifexport_extension.c
+ ldifexport_extension.c \
+ wizard.h \
+ wizard.c
ldifexport_extension_la_LIBADD= \
@GLIB_LIBS@ \
Index: ldifexport_extension.c
===================================================================
RCS file: /home/claws-mail/contacts/extensions/export/ldifexport_extension.c,v
retrieving revision 1.3
retrieving revision 1.4
diff -u -d -r1.3 -r1.4
--- ldifexport_extension.c 9 Dec 2011 23:01:21 -0000 1.3
+++ ldifexport_extension.c 13 Dec 2011 14:29:42 -0000 1.4
@@ -40,48 +40,277 @@
#include "extension.h"
#include "utils.h"
#include "gtk-utils.h"
+#include "plugin.h"
+#include "plugin-loader.h"
+#include "callbacks.h"
+#include "wizard.h"
#define NAME "LDIF Export extension"
static guint my_id;
+static const gchar* ObjectClass[] = {
+ "objectClass: person",
+ "objectClass: organizationalPerson",
+ "objectClass: inetOrgPerson",
+ NULL
+};
+static const gchar* LDIF_Version = "version: 1";
+static Plugin* plugin;
+static AddressBook* abook;
+static GtkTreeView* view;
+GtkWidget* action;
+MainWindow* Main = NULL;
-static void my_cb(GtkWidget* widget, gpointer data) {
- const MainWindow* mainwindow = (const MainWindow*) data;
+static const gchar* native2ldap(const gchar* attr) {
+ if (strcasecmp(attr, "email") == 0)
+ return "mail";
+ else if (strcasecmp(attr, "first-name") == 0)
+ return "givenName";
+ else if (strcasecmp(attr, "last-name") == 0)
+ return "sn";
+ else if (strcasecmp(attr, "nick-name") == 0)
+ return "displayName";
+ else if (strcasecmp(attr, "image") == 0)
+ return "jpegPhoto";
+ else
+ return attr;
+}
+
+static gchar* get_dn(Contact* contact, ExportData* data) {
+ gchar *tmp = NULL, *dn = NULL;
- show_message(mainwindow->window, GTK_UTIL_MESSAGE_INFO, "test");
+ extract_data(contact->data, "dn", (gpointer) &tmp);
+ if (! tmp) {
+ extract_data(contact->data, "DN", (gpointer) &tmp);
+ if (tmp)
+ g_hash_table_remove(contact->data, "DN");
+ }
+ else
+ g_hash_table_remove(contact->data, "dn");
+
+ if (tmp && data->use_dn)
+ dn = g_strdup_printf("dn: %s\n", tmp);
+ else {
+ g_free(tmp);
+ tmp = NULL;
+ if (strcmp("display name", data->rdn) == 0) {
+ extract_data(contact->data, "nick-name", (gpointer) &tmp);
+ if (tmp) {
+ dn = g_strconcat("dn: displayName=", tmp, ",", data->suffix, "\n", NULL);
+ g_free(tmp);
+ tmp = NULL;
+ }
+ else {
+ return dn;
+ }
+ }
+ else if (strcmp("cn", data->rdn) == 0) {
+ extract_data(contact->data, "cn", (gpointer) &tmp);
+ if (tmp) {
+ dn = g_strconcat("dn: cn=", tmp, ",", data->suffix, "\n", NULL);
+ g_free(tmp);
+ tmp = NULL;
+ }
+ else {
+ return dn;
+ }
+ }
+ else {
+ if (contact->emails && contact->emails->data) {
+ Email* e = (Email *) contact->emails->data;
+ if (e->email) {
+ dn = g_strconcat("dn: mail=", e->email, ",",
+ data->suffix, "\n", NULL);
+ }
+ }
+ }
+ }
+ g_free(tmp);
+
+ return dn;
+}
+
+static gchar* contact_as_ldif(Contact* contact, ExportData* data) {
+ gchar* ldif = NULL;
+ GString* text;
+ GHashTableIter iter;
+ gpointer key, value;
+ gchar* attrib;
+ Contact* tmp;
+ GSList* cur;
+ const gchar** class = ObjectClass;
+
+ tmp = contact_copy(contact);
+ text = g_string_new("");
+
+ while (*class)
+ g_string_append_printf(text, "%s\n", *class++);
+
+ ldif = get_dn(tmp, data);
+ if (ldif) {
+ text = g_string_append(text, ldif);
+ g_free(ldif);
+ }
+ else {
+ contact_free(tmp);
+ g_string_free(text, TRUE);
+ return NULL;
+ }
+ /* remove internal key (uid) */
+ g_hash_table_remove(tmp->data, "uid");
+
+ g_hash_table_iter_init(&iter, tmp->data);
+ while (g_hash_table_iter_next(&iter, &key, &value)) {
+ get_data((AttribDef *) value, (gpointer) &attrib);
+ if (attrib && strlen(attrib) > 0) {
+ debug_print("%s -> %s\n", native2ldap(key), attrib);
+ if (strcasecmp("image", key) == 0)
+ g_string_append_printf(text, "%s:: %s\n",
+ native2ldap(key), attrib);
+ else
+ g_string_append_printf(text, "%s: %s\n",
+ native2ldap(key), attrib);
+ }
+ g_free(attrib);
+ }
+ cur = tmp->emails;
+ while (cur) {
+ Email* e = (Email *) cur->data;
+ if (e->email && strlen(e->email) > 0)
+ g_string_append_printf(text, "mail: %s\n", e->email);
+ cur = cur->next;
+ }
+
+ contact_free(tmp);
+
+ return g_string_free(text, FALSE);
+}
+
+static gchar* get_contacts_as_ldif(MainWindow* win, ExportData* data) {
+ GString* text = g_string_new("");
+ gchar* ldif;
+ GList* list;
+
+ view = GTK_TREE_VIEW(win->abook_list);
+ plugin = get_selected_plugin(view);
+ abook = get_selected_address_book(view);
+
+ for (list = abook->contacts; list; list = g_list_next(list)) {
+ Contact* contact = (Contact *) list->data;
+ ldif = contact_as_ldif(contact, data);
+ if (ldif) {
+ text = g_string_append(text, ldif);
+ g_free(ldif);
+ text = g_string_append(text, "\n");
+ }
+ }
+
+ return g_string_free(text, FALSE);
+}
+
+static void make_ldif(ExportData* data, gpointer user_data) {
+ const gchar* name;
+ GString* export;
+ gchar* text = NULL;
+ GError* error = NULL;
+ MainWindow* Main = (MainWindow *) user_data;
+ GtkTreeIter* iter = NULL;
+ GtkTreeSelection* row;
+
+ if (! data)
+ return;
+
+ debug_print("LDIF Export\n");
+ if (data->filename) {
+ name = gtk_widget_get_name(action);
+ if (strcmp("toolsmenu", name) == 0) {
+ /* get abook, get file name for write, get contact list,
+ * convert to ldif, save text to file*/
+ 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) {
+ export_data_free(&data);
+ return;
+ }
+ row = gtk_tree_view_get_selection(view);
+ gtk_tree_selection_select_iter(row, iter);
+ g_free(iter);
+ }
+ }
+
+ text = get_contacts_as_ldif(Main, data);
+ export = g_string_new(LDIF_Version);
+ export = g_string_append(export, "\n");
+ export = g_string_append(export, text);
+ g_free(text);
+
+ text = g_string_free(export, FALSE);
+ if (! g_file_set_contents(data->filename, text, -1, &error)) {
+ show_message(Main->window, GTK_UTIL_MESSAGE_ERROR, error->message);
+ g_clear_error(&error);
+ }
+ else {
+ show_message(Main->window, GTK_UTIL_MESSAGE_INFO,
+ _("Data successfully exported as LDIF to file\n%s"),
+ data->filename);
+ }
+ export_data_free(&data);
+ g_free(text);
+ }
+}
+
+static void ldif_export(GtkWidget* widget, gpointer data) {
+ Main = (MainWindow *) data;
+/* GtkTreeView* view;
+ GtkTreeIter* iter = NULL;
+ GtkTreeSelection* row;
+ AddressBook* abook;*/
+
+ action = widget;
+ debug_print("LDIF Export\n");
+/* view = GTK_TREE_VIEW(win->abook_list);
+ abook = get_selected_address_book(view);
+ if (abook == NULL) {
+ iter = set_selection_combobox(win->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);
+ }*/
+ show_export_wizard(Main, make_ldif);
}
static void setup(const MainWindow* mainwindow, gpointer object) {
GtkWidget *menu;
MenuItem* menu_item;
-/* //root = gtk_image_menu_item_new_with_mnemonic("_Test1");
- menu = gtk_image_menu_item_new_with_mnemonic("_Zest2");
+ menu = gtk_image_menu_item_new_with_mnemonic("_LDIF");
+ gtk_widget_set_name(menu, "toolsmenu");
g_signal_connect(menu, "activate",
- G_CALLBACK(my_cb), mainwindow);
+ G_CALLBACK(ldif_export), mainwindow);
menu_item = menu_item_new();
menu_item->menu = CONTACTS_MAIN_MENU;
- menu_item->parent = "file";
- menu_item->sublabel = "_Test1";
+ menu_item->parent = "tools";
+ menu_item->sublabel = "_Export";
menu_item->submenu = TRUE;
add_menu_item(GTK_IMAGE_MENU_ITEM(menu), menu_item);
-*/
- menu = gtk_image_menu_item_new_with_mnemonic("_Zest2");
- g_signal_connect(menu, "activate",
- G_CALLBACK(my_cb), mainwindow);
- menu_item = menu_item_new();
- menu_item->menu = CONTACTS_MAIN_MENU;
- menu_item->parent = "_Menu";
- menu_item->submenu = FALSE;
- add_menu_item(GTK_IMAGE_MENU_ITEM(menu), menu_item);
- menu = gtk_image_menu_item_new_with_mnemonic("_Yest1");
+ menu = gtk_image_menu_item_new_with_mnemonic("_LDIF");
+ gtk_widget_set_name(menu, "contextmenu");
g_signal_connect(menu, "activate",
- G_CALLBACK(my_cb), mainwindow);
+ G_CALLBACK(ldif_export), mainwindow);
menu_item = menu_item_new();
- menu_item->menu = CONTACTS_MAIN_MENU;
- menu_item->parent = "_Menu";
- menu_item->sublabel = "_Test1";
+ menu_item->menu = CONTACTS_ADDRESSBOOK_MENU;
+ menu_item->sublabel = "_Export";
menu_item->submenu = TRUE;
add_menu_item(GTK_IMAGE_MENU_ITEM(menu), menu_item);
@@ -96,9 +325,15 @@
*/
gint extension_init(guint id) {
my_id = id;
- gchar* error;
+ gchar* error = NULL;
register_hook_function(my_id, EXTENSION_AFTER_INIT_HOOK, setup, &error);
+
+ if (error) {
+ show_message(NULL, GTK_UTIL_MESSAGE_ERROR, error);
+ return 1;
+ }
+
return 0;
}
More information about the Commits
mailing list