2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2007 Match Grun and the Claws Mail team
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
21 * Import LDIF address book data.
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtkwindow.h>
34 #include <gtk/gtksignal.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtktable.h>
38 #include <gtk/gtkbutton.h>
41 #include "addressbook.h"
42 #include "addressitem.h"
44 #include "stock_pixmap.h"
45 #include "prefs_common.h"
46 #include "manage_window.h"
52 #define IMPORTLDIF_GUESS_NAME "LDIF Import"
54 #define PAGE_FILE_INFO 0
55 #define PAGE_ATTRIBUTES 1
58 #define IMPORTLDIF_WIDTH 390
59 #define IMPORTLDIF_HEIGHT 300
61 #define FIELDS_N_COLS 4
62 #define FIELDS_COL_WIDTH_RESERVED 10
63 #define FIELDS_COL_WIDTH_SELECT 10
64 #define FIELDS_COL_WIDTH_FIELD 140
65 #define FIELDS_COL_WIDTH_ATTRIB 140
68 FIELD_COL_RESERVED = 0,
72 } ImpLdif_FieldColPos;
75 * LDIF dialog definition.
77 static struct _ImpLdif_Dlg {
82 GtkWidget *clist_field;
83 GtkWidget *entryField;
84 GtkWidget *entryAttrib;
85 GtkWidget *checkSelect;
89 GtkWidget *labelRecords;
92 GtkWidget *btnProceed;
103 static struct _AddressFileSelection _imp_ldif_file_selector_;
104 static AddressBookFile *_importedBook_;
105 static AddressIndex *_imp_addressIndex_;
106 static LdifFile *_ldifFile_ = NULL;
108 static GdkPixmap *markxpm;
109 static GdkBitmap *markxpmmask;
112 * Structure of error message table.
114 typedef struct _ErrMsgTableEntry ErrMsgTableEntry;
115 struct _ErrMsgTableEntry {
120 static gchar *_errMsgUnknown_ = N_( "Unknown" );
123 * Lookup table of error messages for general errors. Note that a NULL
124 * description signifies the end of the table.
126 static ErrMsgTableEntry _lutErrorsLDIF_[] = {
127 { MGU_SUCCESS, N_("Success") },
128 { MGU_BAD_ARGS, N_("Bad arguments") },
129 { MGU_NO_FILE, N_("File not specified") },
130 { MGU_OPEN_FILE, N_("Error opening file") },
131 { MGU_ERROR_READ, N_("Error reading file") },
132 { MGU_EOF, N_("End of file encountered") },
133 { MGU_OO_MEMORY, N_("Error allocating memory") },
134 { MGU_BAD_FORMAT, N_("Bad file format") },
135 { MGU_ERROR_WRITE, N_("Error writing to file") },
136 { MGU_OPEN_DIRECTORY, N_("Error opening directory") },
137 { MGU_NO_PATH, N_("No path specified") },
142 * Lookup message for specified error code.
143 * \param lut Lookup table.
144 * \param code Code to lookup.
145 * \return Description associated to code.
147 static gchar *imp_ldif_err2string( ErrMsgTableEntry lut[], gint code ) {
149 ErrMsgTableEntry entry;
152 for( i = 0; ; i++ ) {
154 if( entry.description == NULL ) break;
155 if( entry.code == code ) {
156 desc = entry.description;
161 desc = _errMsgUnknown_;
167 * Display message in status field.
168 * \param msg Message to display.
170 static void imp_ldif_status_show( gchar *msg ) {
171 if( impldif_dlg.statusbar != NULL ) {
172 gtk_statusbar_pop( GTK_STATUSBAR(impldif_dlg.statusbar),
173 impldif_dlg.status_cid );
176 GTK_STATUSBAR(impldif_dlg.statusbar),
177 impldif_dlg.status_cid, msg );
183 * Select and display status message appropriate for the page being displayed.
185 static void imp_ldif_message( void ) {
189 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
190 if( pageNum == PAGE_FILE_INFO ) {
191 sMsg = _( "Please specify address book name and file to import." );
193 else if( pageNum == PAGE_ATTRIBUTES ) {
194 sMsg = _( "Select and rename LDIF field names to import." );
196 else if( pageNum == PAGE_FINISH ) {
197 sMsg = _( "File imported." );
199 imp_ldif_status_show( sMsg );
203 * Update list with data for current row.
204 * \param clist List to update.
206 static void imp_ldif_update_row( GtkCMCList *clist ) {
208 gchar *text[ FIELDS_N_COLS ];
211 if( impldif_dlg.rowIndSelect < 0 ) return;
212 row = impldif_dlg.rowIndSelect;
214 rec = gtk_cmclist_get_row_data( clist, row );
215 text[ FIELD_COL_RESERVED ] = "";
216 text[ FIELD_COL_SELECT ] = "";
217 text[ FIELD_COL_FIELD ] = rec->tagName;
218 text[ FIELD_COL_ATTRIB ] = rec->userName;
220 gtk_cmclist_freeze( clist );
221 gtk_cmclist_remove( clist, row );
222 if( row == impldif_dlg.rowCount - 1 ) {
223 gtk_cmclist_append( clist, text );
226 gtk_cmclist_insert( clist, row, text );
228 if( rec->selected ) {
229 gtk_cmclist_set_pixmap(
230 clist, row, FIELD_COL_SELECT, markxpm, markxpmmask );
232 if( rec->reserved ) {
233 gtk_cmclist_set_pixmap(
234 clist, row, FIELD_COL_RESERVED, markxpm, markxpmmask );
237 gtk_cmclist_set_row_data( clist, row, rec );
238 gtk_cmclist_thaw( clist );
242 * Load list with LDIF fields read from file.
243 * \param ldf LDIF control data.
245 static void imp_ldif_load_fields( LdifFile *ldf ) {
246 GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
248 gchar *text[ FIELDS_N_COLS ];
250 impldif_dlg.rowIndSelect = -1;
251 impldif_dlg.rowCount = 0;
252 if( ! ldf->accessFlag ) return;
253 gtk_cmclist_clear( clist );
254 list = ldif_get_fieldlist( ldf );
257 Ldif_FieldRec *rec = node->data;
260 text[ FIELD_COL_RESERVED ] = "";
261 text[ FIELD_COL_SELECT ] = "";
262 text[ FIELD_COL_FIELD ] = rec->tagName;
263 text[ FIELD_COL_ATTRIB ] = rec->userName;
264 row = gtk_cmclist_append( clist, text );
265 gtk_cmclist_set_row_data( clist, row, rec );
266 if( rec->selected ) {
267 gtk_cmclist_set_pixmap( clist, row,
268 FIELD_COL_SELECT, markxpm, markxpmmask );
270 if( rec->reserved ) {
271 gtk_cmclist_set_pixmap( clist, row,
272 FIELD_COL_RESERVED, markxpm, markxpmmask );
274 impldif_dlg.rowCount++;
275 node = g_list_next( node );
279 ldif_set_accessed( ldf, FALSE );
283 * Callback function when list item is selected.
284 * \param clist List widget.
287 * \param event Event object.
288 * \param data User data.
290 static void imp_ldif_field_list_selected(
291 GtkCMCList *clist, gint row, gint column, GdkEvent *event,
294 Ldif_FieldRec *rec = gtk_cmclist_get_row_data( clist, row );
296 impldif_dlg.rowIndSelect = row;
297 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
299 /* Update widget contents */
301 GTK_LABEL(impldif_dlg.entryField), rec->tagName );
304 GTK_ENTRY(impldif_dlg.entryAttrib), rec->userName );
305 gtk_toggle_button_set_active(
306 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect),
309 /* Disable widgets for reserved fields */
310 gtk_widget_set_sensitive(
311 impldif_dlg.entryAttrib, ! rec->reserved );
312 gtk_widget_set_sensitive(
313 impldif_dlg.checkSelect, ! rec->reserved );
314 gtk_widget_set_sensitive(
315 impldif_dlg.btnModify, ! rec->reserved );
317 gtk_widget_grab_focus(impldif_dlg.entryAttrib);
321 * Callback function to toggle selected LDIF field.
322 * \param clist List to update.
323 * \param event Event object.
326 static gboolean imp_ldif_field_list_toggle(
327 GtkCMCList *clist, GdkEventButton *event, gpointer data )
330 gboolean toggle = FALSE;
332 if( ! event ) return FALSE;
333 if( impldif_dlg.rowIndSelect < 0 ) return FALSE;
334 if( event->button == 1 ) {
335 /* If single click in select column */
336 if( event->type == GDK_BUTTON_PRESS ) {
341 gtk_cmclist_get_selection_info( clist, x, y, &row, &col );
342 if( col != FIELD_COL_SELECT ) return FALSE;
343 if( row > impldif_dlg.rowCount ) return FALSE;
346 impldif_dlg.rowIndSelect = row;
350 /* If double click anywhere in row */
351 else if( event->type == GDK_2BUTTON_PRESS ) {
355 /* Toggle field selection */
357 rec = gtk_cmclist_get_row_data(
358 clist, impldif_dlg.rowIndSelect );
360 ldif_field_toggle( rec );
361 imp_ldif_update_row( clist );
368 * Callback function to update LDIF field data from input fields.
369 * \param widget Widget (button).
370 * \param data User data.
372 static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
373 GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
377 if( impldif_dlg.rowIndSelect < 0 ) return;
378 row = impldif_dlg.rowIndSelect;
379 rec = gtk_cmclist_get_row_data( clist, impldif_dlg.rowIndSelect );
381 ldif_field_set_name( rec, gtk_editable_get_chars(
382 GTK_EDITABLE(impldif_dlg.entryAttrib), 0, -1 ) );
383 ldif_field_set_selected( rec, gtk_toggle_button_get_active(
384 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect) ) );
385 imp_ldif_update_row( clist );
386 gtk_cmclist_select_row( clist, row, 0 );
387 gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
388 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
389 gtk_toggle_button_set_active(
390 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect), FALSE );
394 * Test whether we can move off fields page.
395 * \return <i>TRUE</i> if OK to move off page.
397 static gboolean imp_ldif_field_move() {
398 gboolean retVal = FALSE;
400 AddressBookFile *abf = NULL;
402 if( _importedBook_ ) {
403 addrbook_free_book( _importedBook_ );
406 abf = addrbook_create_book();
407 addrbook_set_path( abf, _imp_addressIndex_->filePath );
408 addrbook_set_name( abf, impldif_dlg.nameBook );
409 newFile = addrbook_guess_next_file( abf );
410 addrbook_set_file( abf, newFile );
413 /* Import data into file */
414 if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
415 addrbook_save_data( abf );
416 _importedBook_ = abf;
420 addrbook_free_book( abf );
427 * Test whether we can move off file page.
428 * \return <i>TRUE</i> if OK to move off page.
430 static gboolean imp_ldif_file_move() {
431 gboolean retVal = FALSE;
435 gboolean errFlag = FALSE;
437 sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryFile), 0, -1 );
438 g_strchug( sFile ); g_strchomp( sFile );
440 sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
441 g_strchug( sName ); g_strchomp( sName );
443 g_free( impldif_dlg.nameBook );
444 g_free( impldif_dlg.fileName );
445 impldif_dlg.nameBook = sName;
446 impldif_dlg.fileName = sFile;
448 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), sFile );
449 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), sName );
451 if( *sFile == '\0' ) {
452 sMsg = _( "Please select a file." );
453 gtk_widget_grab_focus(impldif_dlg.entryFile);
457 if( ! errFlag && *sName == '\0' ) {
458 sMsg = _( "Address book name must be supplied." );
459 gtk_widget_grab_focus(impldif_dlg.entryName);
464 /* Read attribute list */
465 ldif_set_file( _ldifFile_, sFile );
466 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
468 /* ldif_print_file( _ldifFile_, stdout ); */
469 imp_ldif_load_fields( _ldifFile_ );
473 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
476 imp_ldif_status_show( sMsg );
482 * Display finish page.
484 static void imp_ldif_finish_show() {
488 name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
489 gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
491 gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
492 gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
493 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
494 gtk_widget_hide( impldif_dlg.btnNext );
495 gtk_widget_show( impldif_dlg.btnProceed );
496 gtk_widget_set_sensitive( impldif_dlg.btnProceed, FALSE );
497 if( _ldifFile_->retVal == MGU_SUCCESS ) {
498 sMsg = _( "LDIF file imported successfully." );
501 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
503 imp_ldif_status_show( sMsg );
504 gtk_widget_grab_focus(impldif_dlg.btnCancel);
508 * Callback function to select previous page.
509 * \param widget Widget (button).
511 static void imp_ldif_prev( GtkWidget *widget ) {
514 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
515 if( pageNum == PAGE_ATTRIBUTES ) {
516 /* Goto file page stuff */
517 gtk_notebook_set_current_page(
518 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
519 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
520 gtk_widget_hide( impldif_dlg.btnProceed );
521 gtk_widget_show( impldif_dlg.btnNext );
527 * Callback function to select next page.
528 * \param widget Widget (button).
530 static void imp_ldif_next( GtkWidget *widget ) {
533 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
534 if( pageNum == PAGE_FILE_INFO ) {
535 /* Goto attributes stuff */
536 if( imp_ldif_file_move() ) {
537 gtk_notebook_set_current_page(
538 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
540 gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
541 gtk_widget_hide( impldif_dlg.btnNext );
542 gtk_widget_show( impldif_dlg.btnProceed );
543 gtk_widget_set_sensitive( impldif_dlg.btnProceed, TRUE );
546 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
547 _ldifFile_->dirtyFlag = TRUE;
550 else if( pageNum == PAGE_ATTRIBUTES ) {
551 /* Goto finish stuff */
552 if( imp_ldif_field_move() ) {
553 gtk_notebook_set_current_page(
554 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
555 gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
557 imp_ldif_finish_show();
563 * Callback function to cancel and close dialog.
564 * \param widget Widget (button).
565 * \param data User data.
567 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
570 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
571 if( pageNum != PAGE_FINISH ) {
572 impldif_dlg.cancelled = TRUE;
579 * Create LDIF file selection dialog.
580 * \param afs Address file selection data.
582 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
583 gchar *file = filesel_select_file_open(_("Select LDIF File"), NULL);
586 afs->cancelled = TRUE;
588 afs->cancelled = FALSE;
589 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), file );
595 * Callback function to display LDIF file selection dialog.
597 static void imp_ldif_file_select( void ) {
598 imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
602 * Callback function to handle dialog close event.
603 * \param widget Widget (dialog).
604 * \param event Event object.
605 * \param data User data.
607 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
608 imp_ldif_cancel( widget, data );
613 * Callback function to respond to dialog key press events.
614 * \param widget Widget.
615 * \param event Event object.
616 * \param data User data.
618 static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
619 if (event && event->keyval == GDK_Escape) {
620 imp_ldif_cancel( widget, data );
626 * Format notebook "file" page.
627 * \param pageNum Page (tab) number.
628 * \param pageLbl Page (tab) label.
630 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
634 GtkWidget *entryFile;
635 GtkWidget *entryName;
640 vbox = gtk_vbox_new(FALSE, 8);
641 gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
642 gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
644 label = gtk_label_new( pageLbl );
645 gtk_widget_show( label );
646 gtk_notebook_set_tab_label(
647 GTK_NOTEBOOK( impldif_dlg.notebook ),
648 gtk_notebook_get_nth_page(
649 GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
652 table = gtk_table_new(2, 3, FALSE);
653 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
654 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
655 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
656 gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
660 label = gtk_label_new(_("Address Book"));
661 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
663 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
665 entryName = gtk_entry_new();
666 gtk_table_attach(GTK_TABLE(table), entryName, 1, 2, top, (top + 1),
667 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
669 CLAWS_SET_TIP(entryName, _(
670 "Specify the name for the address book that will " \
671 "be created from the LDIF file data." ));
675 label = gtk_label_new(_("File Name"));
676 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
678 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
680 entryFile = gtk_entry_new();
681 gtk_table_attach(GTK_TABLE(table), entryFile, 1, 2, top, (top + 1),
682 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
684 CLAWS_SET_TIP(entryFile,
685 _( "The full file specification of the LDIF file to import." ));
687 btnFile = gtkut_get_browse_file_btn(_("B_rowse"));
688 gtk_table_attach(GTK_TABLE(table), btnFile, 2, 3, top, (top + 1),
691 CLAWS_SET_TIP(btnFile,
692 _( "Select the LDIF file to import." ));
694 gtk_widget_show_all(vbox);
697 g_signal_connect(G_OBJECT(btnFile), "clicked",
698 G_CALLBACK(imp_ldif_file_select), NULL);
700 impldif_dlg.entryFile = entryFile;
701 impldif_dlg.entryName = entryName;
706 * Format notebook fields page.
707 * \param pageNum Page (tab) number.
708 * \param pageLbl Page (tab) label.
710 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
716 GtkWidget *clist_swin;
717 GtkWidget *clist_field;
718 GtkWidget *entryField;
719 GtkWidget *entryAttrib;
720 GtkWidget *checkSelect;
721 GtkWidget *btnModify;
726 gchar *titles[ FIELDS_N_COLS ];
729 titles[ FIELD_COL_RESERVED ] = _("R");
730 titles[ FIELD_COL_SELECT ] = _("S");
731 titles[ FIELD_COL_FIELD ] = _("LDIF Field Name");
732 titles[ FIELD_COL_ATTRIB ] = _("Attribute Name");
734 vbox = gtk_vbox_new(FALSE, 8);
735 gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
736 gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
738 label = gtk_label_new( pageLbl );
739 gtk_widget_show( label );
740 gtk_notebook_set_tab_label(
741 GTK_NOTEBOOK( impldif_dlg.notebook ),
742 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
745 /* Upper area - Field list */
746 vboxt = gtk_vbox_new( FALSE, 4 );
747 gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
749 clist_swin = gtk_scrolled_window_new( NULL, NULL );
750 gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
751 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
752 GTK_POLICY_AUTOMATIC,
753 GTK_POLICY_AUTOMATIC);
755 clist_field = gtk_cmclist_new_with_titles( FIELDS_N_COLS, titles );
756 gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
757 gtk_cmclist_set_selection_mode(
758 GTK_CMCLIST(clist_field), GTK_SELECTION_BROWSE );
759 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
760 FIELD_COL_RESERVED, FIELDS_COL_WIDTH_RESERVED );
761 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
762 FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
763 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
764 FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
765 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
766 FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
768 /* Remove focus capability for column headers */
769 for( i = 0; i < FIELDS_N_COLS; i++ ) {
770 GTK_WIDGET_UNSET_FLAGS(
771 GTK_CMCLIST(clist_field)->column[i].button,
775 /* Lower area - Edit area */
776 vboxb = gtk_vbox_new( FALSE, 4 );
777 gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
779 /* Data entry area */
780 table = gtk_table_new( 3, 3, FALSE);
781 gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
782 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
783 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
787 label = gtk_label_new(_("LDIF Field"));
788 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
790 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
792 entryField = gtk_label_new( "" );
793 gtk_misc_set_alignment(GTK_MISC(entryField), 0.01, 0.5);
794 gtk_table_attach(GTK_TABLE(table), entryField, 1, 3, top, (top + 1),
795 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
799 label = gtk_label_new(_("Attribute"));
801 * Use an event box to attach some help in the form of a tooltip.
802 * Tried this for the clist but it looked bad.
804 eventBox = gtk_event_box_new();
805 gtk_container_add( GTK_CONTAINER(eventBox), label );
806 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
807 gtk_table_attach(GTK_TABLE(table), eventBox, 0, 1, top, (top + 1),
810 CLAWS_SET_TIP(eventBox, _(
811 "Choose the LDIF field that will be renamed or selected " \
812 "for import in the list above. Reserved fields (marked " \
813 "with a tick in the \"R\" column), are automatically " \
814 "imported and cannot be renamed. A single click in the " \
815 "Select (\"S\") column will select the field for import " \
816 "with a tick. A single click anywhere in the row will " \
817 "select that field for rename in the input area below " \
818 "the list. A double click anywhere in the row will also " \
819 "select the field for import."));
821 entryAttrib = gtk_entry_new();
822 gtk_table_attach(GTK_TABLE(table), entryAttrib, 1, 3, top, (top + 1),
823 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
825 CLAWS_SET_TIP(entryAttrib,
826 _( "The LDIF field can be renamed to the User Attribute name." ));
831 checkSelect = gtk_check_button_new_with_label( _( "Select for Import" ) );
832 gtk_table_attach(GTK_TABLE(table), checkSelect, 1, 2, top, (top + 1),
833 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
835 CLAWS_SET_TIP(checkSelect,
836 _( "Select the LDIF field for import into the address book." ));
838 btnModify = gtk_button_new_with_label( _(" Modify "));
839 gtk_table_attach(GTK_TABLE(table), btnModify, 2, 3, top, (top + 1),
842 CLAWS_SET_TIP(btnModify,
843 _( "This button will update the list above with the data supplied." ));
845 gtk_widget_show_all(vbox);
848 g_signal_connect( G_OBJECT(clist_field), "select_row",
849 G_CALLBACK(imp_ldif_field_list_selected), NULL );
850 g_signal_connect( G_OBJECT(clist_field), "button_press_event",
851 G_CALLBACK(imp_ldif_field_list_toggle), NULL );
852 g_signal_connect( G_OBJECT(btnModify), "clicked",
853 G_CALLBACK(imp_ldif_modify_pressed), NULL );
855 impldif_dlg.clist_field = clist_field;
856 impldif_dlg.entryField = entryField;
857 impldif_dlg.entryAttrib = entryAttrib;
858 impldif_dlg.checkSelect = checkSelect;
859 impldif_dlg.btnModify = btnModify;
863 * Format notebook finish page.
864 * \param pageNum Page (tab) number.
865 * \param pageLbl Page (tab) label.
867 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
871 GtkWidget *labelBook;
872 GtkWidget *labelFile;
873 GtkWidget *labelRecs;
876 vbox = gtk_vbox_new(FALSE, 8);
877 gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
878 gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
880 label = gtk_label_new( pageLbl );
881 gtk_widget_show( label );
882 gtk_notebook_set_tab_label(
883 GTK_NOTEBOOK( impldif_dlg.notebook ),
884 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
887 table = gtk_table_new(3, 2, FALSE);
888 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
889 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
890 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
891 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
895 label = gtk_label_new( _( "Address Book :" ) );
896 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
897 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
899 labelBook = gtk_label_new("");
900 gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
901 gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
905 label = gtk_label_new( _( "File Name :" ) );
906 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
907 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
909 labelFile = gtk_label_new("");
910 gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
911 gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
915 label = gtk_label_new( _("Records Imported :") );
916 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
917 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
919 labelRecs = gtk_label_new("");
920 gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
921 gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
923 impldif_dlg.labelBook = labelBook;
924 impldif_dlg.labelFile = labelFile;
925 impldif_dlg.labelRecords = labelRecs;
929 * Create main dialog decorations (excluding notebook pages).
931 static void imp_ldif_dialog_create() {
939 GtkWidget *btnProceed;
940 GtkWidget *btnCancel;
942 GtkWidget *statusbar;
944 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importldif");
945 gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
946 gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
947 gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
948 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
949 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
950 g_signal_connect(G_OBJECT(window), "delete_event",
951 G_CALLBACK(imp_ldif_delete_event),
953 g_signal_connect(G_OBJECT(window), "key_press_event",
954 G_CALLBACK(imp_ldif_key_pressed),
957 vbox = gtk_vbox_new(FALSE, 4);
958 gtk_widget_show(vbox);
959 gtk_container_add(GTK_CONTAINER(window), vbox);
961 vnbox = gtk_vbox_new(FALSE, 4);
962 gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
963 gtk_widget_show(vnbox);
964 gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
967 notebook = gtk_notebook_new();
968 gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
969 gtk_widget_show(notebook);
970 gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
971 gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
974 hsbox = gtk_hbox_new(FALSE, 0);
975 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
976 statusbar = gtk_statusbar_new();
977 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
980 gtkut_stock_button_set_create(&hbbox,
981 &btnCancel, GTK_STOCK_CANCEL,
982 &btnPrev, GTK_STOCK_GO_BACK,
983 &btnNext, GTK_STOCK_GO_FORWARD);
985 btnProceed = gtk_button_new_with_mnemonic(_("Proceed"));
986 gtk_button_set_image(GTK_BUTTON(btnProceed),
987 gtk_image_new_from_stock(GTK_STOCK_OK, GTK_ICON_SIZE_BUTTON));
988 GTK_WIDGET_SET_FLAGS(btnProceed, GTK_CAN_DEFAULT);
989 gtk_box_pack_start(GTK_BOX(hbbox), btnProceed, TRUE, TRUE, 0);
990 gtk_widget_hide(btnProceed);
992 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
993 gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
994 gtk_widget_grab_default(btnNext);
996 /* Button handlers */
997 g_signal_connect(G_OBJECT(btnPrev), "clicked",
998 G_CALLBACK(imp_ldif_prev), NULL);
999 g_signal_connect(G_OBJECT(btnNext), "clicked",
1000 G_CALLBACK(imp_ldif_next), NULL);
1001 g_signal_connect(G_OBJECT(btnProceed), "clicked",
1002 G_CALLBACK(imp_ldif_next), NULL);
1003 g_signal_connect(G_OBJECT(btnCancel), "clicked",
1004 G_CALLBACK(imp_ldif_cancel), NULL);
1006 gtk_widget_show_all(vbox);
1008 impldif_dlg.window = window;
1009 impldif_dlg.notebook = notebook;
1010 impldif_dlg.btnPrev = btnPrev;
1011 impldif_dlg.btnNext = btnNext;
1012 impldif_dlg.btnProceed = btnProceed;
1013 impldif_dlg.btnCancel = btnCancel;
1014 impldif_dlg.statusbar = statusbar;
1015 impldif_dlg.status_cid = gtk_statusbar_get_context_id(
1016 GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
1021 * Create import LDIF dialog.
1023 static void imp_ldif_create() {
1024 imp_ldif_dialog_create();
1025 imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
1026 imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
1027 imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
1028 gtk_widget_show_all( impldif_dlg.window );
1033 * \param addrIndex Address index.
1034 * \return Address book file of imported data, or <i>NULL</i> if import
1037 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
1038 _importedBook_ = NULL;
1039 _imp_addressIndex_ = addrIndex;
1041 if( ! impldif_dlg.window )
1044 gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
1046 gtk_widget_hide(impldif_dlg.btnProceed);
1047 gtk_widget_show(impldif_dlg.btnNext);
1049 impldif_dlg.cancelled = FALSE;
1050 gtk_widget_show(impldif_dlg.window);
1051 manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
1052 gtk_widget_grab_default(impldif_dlg.btnNext);
1054 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), IMPORTLDIF_GUESS_NAME );
1055 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), "" );
1056 gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
1057 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
1058 gtk_cmclist_clear( GTK_CMCLIST(impldif_dlg.clist_field) );
1059 gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
1060 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
1061 gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
1062 stock_pixmap_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
1063 &markxpm, &markxpmmask );
1065 gtk_widget_grab_focus(impldif_dlg.entryFile);
1067 impldif_dlg.rowIndSelect = -1;
1068 impldif_dlg.rowCount = 0;
1069 g_free( impldif_dlg.nameBook );
1070 g_free( impldif_dlg.fileName );
1071 impldif_dlg.nameBook = NULL;
1072 impldif_dlg.fileName = NULL;
1074 _ldifFile_ = ldif_create();
1076 gtk_widget_hide(impldif_dlg.window);
1077 ldif_free( _ldifFile_ );
1079 _imp_addressIndex_ = NULL;
1081 g_free( impldif_dlg.nameBook );
1082 g_free( impldif_dlg.fileName );
1083 impldif_dlg.nameBook = NULL;
1084 impldif_dlg.fileName = NULL;
1086 if( impldif_dlg.cancelled == TRUE ) return NULL;
1087 return _importedBook_;
1091 * ============================================================================
1093 * ============================================================================