2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2012 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.
26 #include "claws-features.h"
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
37 #include "addressbook.h"
38 #include "addressitem.h"
40 #include "stock_pixmap.h"
41 #include "prefs_common.h"
42 #include "manage_window.h"
48 #define IMPORTLDIF_GUESS_NAME "LDIF Import"
50 #define PAGE_FILE_INFO 0
51 #define PAGE_ATTRIBUTES 1
54 #define IMPORTLDIF_WIDTH 390
55 #define IMPORTLDIF_HEIGHT 300
57 #define FIELDS_N_COLS 4
58 #define FIELDS_COL_WIDTH_RESERVED 10
59 #define FIELDS_COL_WIDTH_SELECT 10
60 #define FIELDS_COL_WIDTH_FIELD 140
61 #define FIELDS_COL_WIDTH_ATTRIB 140
64 FIELD_COL_RESERVED = 0,
68 } ImpLdif_FieldColPos;
71 * LDIF dialog definition.
73 static struct _ImpLdif_Dlg {
78 GtkWidget *clist_field;
79 GtkWidget *entryField;
80 GtkWidget *entryAttrib;
81 GtkWidget *checkSelect;
85 GtkWidget *labelRecords;
88 GtkWidget *btnProceed;
99 static struct _AddressFileSelection _imp_ldif_file_selector_;
100 static AddressBookFile *_importedBook_;
101 static AddressIndex *_imp_addressIndex_;
102 static LdifFile *_ldifFile_ = NULL;
104 static GdkPixbuf *markxpm;
107 * Structure of error message table.
109 typedef struct _ErrMsgTableEntry ErrMsgTableEntry;
110 struct _ErrMsgTableEntry {
115 static gchar *_errMsgUnknown_ = N_( "Unknown" );
118 * Lookup table of error messages for general errors. Note that a NULL
119 * description signifies the end of the table.
121 static ErrMsgTableEntry _lutErrorsLDIF_[] = {
122 { MGU_SUCCESS, N_("Success") },
123 { MGU_BAD_ARGS, N_("Bad arguments") },
124 { MGU_NO_FILE, N_("File not specified") },
125 { MGU_OPEN_FILE, N_("Error opening file") },
126 { MGU_ERROR_READ, N_("Error reading file") },
127 { MGU_EOF, N_("End of file encountered") },
128 { MGU_OO_MEMORY, N_("Error allocating memory") },
129 { MGU_BAD_FORMAT, N_("Bad file format") },
130 { MGU_ERROR_WRITE, N_("Error writing to file") },
131 { MGU_OPEN_DIRECTORY, N_("Error opening directory") },
132 { MGU_NO_PATH, N_("No path specified") },
137 * Lookup message for specified error code.
138 * \param lut Lookup table.
139 * \param code Code to lookup.
140 * \return Description associated to code.
142 static gchar *imp_ldif_err2string( ErrMsgTableEntry lut[], gint code ) {
144 ErrMsgTableEntry entry;
147 for( i = 0; ; i++ ) {
149 if( entry.description == NULL ) break;
150 if( entry.code == code ) {
151 desc = entry.description;
156 desc = _errMsgUnknown_;
162 * Display message in status field.
163 * \param msg Message to display.
165 static void imp_ldif_status_show( gchar *msg ) {
166 if( impldif_dlg.statusbar != NULL ) {
167 gtk_statusbar_pop( GTK_STATUSBAR(impldif_dlg.statusbar),
168 impldif_dlg.status_cid );
171 GTK_STATUSBAR(impldif_dlg.statusbar),
172 impldif_dlg.status_cid, msg );
178 * Select and display status message appropriate for the page being displayed.
180 static void imp_ldif_message( void ) {
184 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
185 if( pageNum == PAGE_FILE_INFO ) {
186 sMsg = _( "Please specify address book name and file to import." );
188 else if( pageNum == PAGE_ATTRIBUTES ) {
189 sMsg = _( "Select and rename LDIF field names to import." );
191 else if( pageNum == PAGE_FINISH ) {
192 sMsg = _( "File imported." );
194 imp_ldif_status_show( sMsg );
198 * Update list with data for current row.
199 * \param clist List to update.
201 static void imp_ldif_update_row( GtkCMCList *clist ) {
203 gchar *text[ FIELDS_N_COLS ];
206 if( impldif_dlg.rowIndSelect < 0 ) return;
207 row = impldif_dlg.rowIndSelect;
209 rec = gtk_cmclist_get_row_data( clist, row );
213 text[ FIELD_COL_RESERVED ] = "";
214 text[ FIELD_COL_SELECT ] = "";
215 text[ FIELD_COL_FIELD ] = rec->tagName;
216 text[ FIELD_COL_ATTRIB ] = rec->userName;
218 gtk_cmclist_freeze( clist );
219 gtk_cmclist_remove( clist, row );
220 if( row == impldif_dlg.rowCount - 1 ) {
221 gtk_cmclist_append( clist, text );
224 gtk_cmclist_insert( clist, row, text );
226 if( rec->selected ) {
227 gtk_cmclist_set_pixbuf(
228 clist, row, FIELD_COL_SELECT, markxpm );
230 if( rec->reserved ) {
231 gtk_cmclist_set_pixbuf(
232 clist, row, FIELD_COL_RESERVED, markxpm );
235 gtk_cmclist_set_row_data( clist, row, rec );
236 gtk_cmclist_thaw( clist );
240 * Load list with LDIF fields read from file.
241 * \param ldf LDIF control data.
243 static void imp_ldif_load_fields( LdifFile *ldf ) {
244 GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
246 gchar *text[ FIELDS_N_COLS ];
248 impldif_dlg.rowIndSelect = -1;
249 impldif_dlg.rowCount = 0;
250 if( ! ldf->accessFlag ) return;
251 gtk_cmclist_clear( clist );
252 list = ldif_get_fieldlist( ldf );
255 Ldif_FieldRec *rec = node->data;
258 text[ FIELD_COL_RESERVED ] = "";
259 text[ FIELD_COL_SELECT ] = "";
260 text[ FIELD_COL_FIELD ] = rec->tagName;
261 text[ FIELD_COL_ATTRIB ] = rec->userName;
262 row = gtk_cmclist_append( clist, text );
263 gtk_cmclist_set_row_data( clist, row, rec );
264 if( rec->selected ) {
265 gtk_cmclist_set_pixbuf( clist, row,
266 FIELD_COL_SELECT, markxpm );
268 if( rec->reserved ) {
269 gtk_cmclist_set_pixbuf( clist, row,
270 FIELD_COL_RESERVED, markxpm );
272 impldif_dlg.rowCount++;
273 node = g_list_next( node );
277 ldif_set_accessed( ldf, FALSE );
281 * Callback function when list item is selected.
282 * \param clist List widget.
285 * \param event Event object.
286 * \param data User data.
288 static void imp_ldif_field_list_selected(
289 GtkCMCList *clist, gint row, gint column, GdkEvent *event,
292 Ldif_FieldRec *rec = gtk_cmclist_get_row_data( clist, row );
294 impldif_dlg.rowIndSelect = row;
295 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
297 /* Update widget contents */
299 GTK_LABEL(impldif_dlg.entryField), rec->tagName );
302 GTK_ENTRY(impldif_dlg.entryAttrib), rec->userName );
303 gtk_toggle_button_set_active(
304 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect),
307 /* Disable widgets for reserved fields */
308 gtk_widget_set_sensitive(
309 impldif_dlg.entryAttrib, ! rec->reserved );
310 gtk_widget_set_sensitive(
311 impldif_dlg.checkSelect, ! rec->reserved );
312 gtk_widget_set_sensitive(
313 impldif_dlg.btnModify, ! rec->reserved );
315 gtk_widget_grab_focus(impldif_dlg.entryAttrib);
319 * Callback function to toggle selected LDIF field.
320 * \param clist List to update.
321 * \param event Event object.
324 static gboolean imp_ldif_field_list_toggle(
325 GtkCMCList *clist, GdkEventButton *event, gpointer data )
328 gboolean toggle = FALSE;
330 if( ! event ) return FALSE;
331 if( impldif_dlg.rowIndSelect < 0 ) return FALSE;
332 if( event->button == 1 ) {
333 /* If single click in select column */
334 if( event->type == GDK_BUTTON_PRESS ) {
339 if (!gtk_cmclist_get_selection_info( clist, x, y, &row, &col ))
341 if( col != FIELD_COL_SELECT ) return FALSE;
342 if( row > impldif_dlg.rowCount ) return FALSE;
345 impldif_dlg.rowIndSelect = row;
349 /* If double click anywhere in row */
350 else if( event->type == GDK_2BUTTON_PRESS ) {
354 /* Toggle field selection */
356 rec = gtk_cmclist_get_row_data(
357 clist, impldif_dlg.rowIndSelect );
359 ldif_field_toggle( rec );
360 imp_ldif_update_row( clist );
367 * Callback function to update LDIF field data from input fields.
368 * \param widget Widget (button).
369 * \param data User data.
371 static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
372 GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
376 if( impldif_dlg.rowIndSelect < 0 ) return;
377 row = impldif_dlg.rowIndSelect;
378 rec = gtk_cmclist_get_row_data( clist, impldif_dlg.rowIndSelect );
380 ldif_field_set_name( rec, gtk_editable_get_chars(
381 GTK_EDITABLE(impldif_dlg.entryAttrib), 0, -1 ) );
382 ldif_field_set_selected( rec, gtk_toggle_button_get_active(
383 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect) ) );
384 imp_ldif_update_row( clist );
385 gtk_cmclist_select_row( clist, row, 0 );
386 gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
387 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
388 gtk_toggle_button_set_active(
389 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect), FALSE );
393 * Test whether we can move off fields page.
394 * \return <i>TRUE</i> if OK to move off page.
396 static gboolean imp_ldif_field_move() {
397 gboolean retVal = FALSE;
399 AddressBookFile *abf = NULL;
401 if( _importedBook_ ) {
402 addrbook_free_book( _importedBook_ );
405 abf = addrbook_create_book();
406 addrbook_set_path( abf, _imp_addressIndex_->filePath );
407 addrbook_set_name( abf, impldif_dlg.nameBook );
408 newFile = addrbook_guess_next_file( abf );
409 addrbook_set_file( abf, newFile );
412 /* Import data into file */
413 if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
414 addrbook_save_data( abf );
415 _importedBook_ = abf;
419 addrbook_free_book( abf );
426 * Test whether we can move off file page.
427 * \return <i>TRUE</i> if OK to move off page.
429 static gboolean imp_ldif_file_move() {
430 gboolean retVal = FALSE;
434 gboolean errFlag = FALSE;
436 sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryFile), 0, -1 );
437 g_strchug( sFile ); g_strchomp( sFile );
439 sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
440 g_strchug( sName ); g_strchomp( sName );
442 g_free( impldif_dlg.nameBook );
443 g_free( impldif_dlg.fileName );
444 impldif_dlg.nameBook = sName;
445 impldif_dlg.fileName = sFile;
447 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), sFile );
448 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), sName );
450 if( *sFile == '\0' ) {
451 sMsg = _( "Please select a file." );
452 gtk_widget_grab_focus(impldif_dlg.entryFile);
456 if( ! errFlag && *sName == '\0' ) {
457 sMsg = _( "Address book name must be supplied." );
458 gtk_widget_grab_focus(impldif_dlg.entryName);
463 /* Read attribute list */
464 ldif_set_file( _ldifFile_, sFile );
465 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
467 /* ldif_print_file( _ldifFile_, stdout ); */
468 imp_ldif_load_fields( _ldifFile_ );
472 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
475 imp_ldif_status_show( sMsg );
481 * Display finish page.
483 static void imp_ldif_finish_show() {
487 name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
488 gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
490 gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
491 gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
492 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
493 gtk_widget_hide( impldif_dlg.btnNext );
494 gtk_widget_show( impldif_dlg.btnProceed );
495 gtk_widget_set_sensitive( impldif_dlg.btnProceed, FALSE );
496 if( _ldifFile_->retVal == MGU_SUCCESS ) {
497 sMsg = _( "LDIF file imported successfully." );
500 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
502 imp_ldif_status_show( sMsg );
503 gtk_widget_grab_focus(impldif_dlg.btnCancel);
507 * Callback function to select previous page.
508 * \param widget Widget (button).
510 static void imp_ldif_prev( GtkWidget *widget ) {
513 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
514 if( pageNum == PAGE_ATTRIBUTES ) {
515 /* Goto file page stuff */
516 gtk_notebook_set_current_page(
517 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
518 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
519 gtk_widget_hide( impldif_dlg.btnProceed );
520 gtk_widget_show( impldif_dlg.btnNext );
526 * Callback function to select next page.
527 * \param widget Widget (button).
529 static void imp_ldif_next( GtkWidget *widget ) {
532 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
533 if( pageNum == PAGE_FILE_INFO ) {
534 /* Goto attributes stuff */
535 if( imp_ldif_file_move() ) {
536 gtk_notebook_set_current_page(
537 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
539 gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
540 gtk_widget_hide( impldif_dlg.btnNext );
541 gtk_widget_show( impldif_dlg.btnProceed );
542 gtk_widget_set_sensitive( impldif_dlg.btnProceed, TRUE );
545 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
546 _ldifFile_->dirtyFlag = TRUE;
549 else if( pageNum == PAGE_ATTRIBUTES ) {
550 /* Goto finish stuff */
551 if( imp_ldif_field_move() ) {
552 gtk_notebook_set_current_page(
553 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
554 gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
556 imp_ldif_finish_show();
562 * Callback function to cancel and close dialog.
563 * \param widget Widget (button).
564 * \param data User data.
566 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
569 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
570 if( pageNum != PAGE_FINISH ) {
571 impldif_dlg.cancelled = TRUE;
578 * Create LDIF file selection dialog.
579 * \param afs Address file selection data.
581 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
582 gchar *file = filesel_select_file_open(_("Select LDIF File"), NULL);
585 afs->cancelled = TRUE;
587 afs->cancelled = FALSE;
588 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), file );
594 * Callback function to display LDIF file selection dialog.
596 static void imp_ldif_file_select( void ) {
597 imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
601 * Callback function to handle dialog close event.
602 * \param widget Widget (dialog).
603 * \param event Event object.
604 * \param data User data.
606 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
607 imp_ldif_cancel( widget, data );
612 * Callback function to respond to dialog key press events.
613 * \param widget Widget.
614 * \param event Event object.
615 * \param data User data.
617 static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
618 if (event && event->keyval == GDK_KEY_Escape) {
619 imp_ldif_cancel( widget, data );
625 * Format notebook "file" page.
626 * \param pageNum Page (tab) number.
627 * \param pageLbl Page (tab) label.
629 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
633 GtkWidget *entryFile;
634 GtkWidget *entryName;
638 vbox = gtk_vbox_new(FALSE, 8);
639 gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
640 gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
642 label = gtk_label_new( pageLbl );
643 gtk_widget_show( label );
644 gtk_notebook_set_tab_label(
645 GTK_NOTEBOOK( impldif_dlg.notebook ),
646 gtk_notebook_get_nth_page(
647 GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
650 table = gtk_table_new(2, 3, FALSE);
651 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
652 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
653 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
654 gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
658 label = gtk_label_new(_("Address Book"));
659 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
661 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
663 entryName = gtk_entry_new();
664 gtk_table_attach(GTK_TABLE(table), entryName, 1, 2, top, (top + 1),
665 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
667 CLAWS_SET_TIP(entryName, _(
668 "Specify the name for the address book that will " \
669 "be created from the LDIF file data." ));
673 label = gtk_label_new(_("File Name"));
674 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
676 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
678 entryFile = gtk_entry_new();
679 gtk_table_attach(GTK_TABLE(table), entryFile, 1, 2, top, (top + 1),
680 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
682 CLAWS_SET_TIP(entryFile,
683 _( "The full file specification of the LDIF file to import." ));
685 btnFile = gtkut_get_browse_file_btn(_("B_rowse"));
686 gtk_table_attach(GTK_TABLE(table), btnFile, 2, 3, top, (top + 1),
689 CLAWS_SET_TIP(btnFile,
690 _( "Select the LDIF file to import." ));
692 gtk_widget_show_all(vbox);
695 g_signal_connect(G_OBJECT(btnFile), "clicked",
696 G_CALLBACK(imp_ldif_file_select), NULL);
698 impldif_dlg.entryFile = entryFile;
699 impldif_dlg.entryName = entryName;
704 * Format notebook fields page.
705 * \param pageNum Page (tab) number.
706 * \param pageLbl Page (tab) label.
708 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
714 GtkWidget *clist_swin;
715 GtkWidget *clist_field;
716 GtkWidget *entryField;
717 GtkWidget *entryAttrib;
718 GtkWidget *checkSelect;
719 GtkWidget *btnModify;
723 gchar *titles[ FIELDS_N_COLS ];
726 titles[ FIELD_COL_RESERVED ] = _("R");
727 titles[ FIELD_COL_SELECT ] = _("S");
728 titles[ FIELD_COL_FIELD ] = _("LDIF Field Name");
729 titles[ FIELD_COL_ATTRIB ] = _("Attribute Name");
731 vbox = gtk_vbox_new(FALSE, 8);
732 gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
733 gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
735 label = gtk_label_new( pageLbl );
736 gtk_widget_show( label );
737 gtk_notebook_set_tab_label(
738 GTK_NOTEBOOK( impldif_dlg.notebook ),
739 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
742 /* Upper area - Field list */
743 vboxt = gtk_vbox_new( FALSE, 4 );
744 gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
746 clist_swin = gtk_scrolled_window_new( NULL, NULL );
747 gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
748 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
749 GTK_POLICY_AUTOMATIC,
750 GTK_POLICY_AUTOMATIC);
752 clist_field = gtk_cmclist_new_with_titles( FIELDS_N_COLS, titles );
753 gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
754 gtk_cmclist_set_selection_mode(
755 GTK_CMCLIST(clist_field), GTK_SELECTION_BROWSE );
756 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
757 FIELD_COL_RESERVED, FIELDS_COL_WIDTH_RESERVED );
758 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
759 FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
760 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
761 FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
762 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
763 FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
765 /* Remove focus capability for column headers */
766 for( i = 0; i < FIELDS_N_COLS; i++ ) {
767 gtkut_widget_set_can_focus(
768 GTK_CMCLIST(clist_field)->column[i].button,
772 /* Lower area - Edit area */
773 vboxb = gtk_vbox_new( FALSE, 4 );
774 gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
776 /* Data entry area */
777 table = gtk_table_new( 3, 3, FALSE);
778 gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
779 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
780 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
784 label = gtk_label_new(_("LDIF Field"));
785 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
787 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
789 entryField = gtk_label_new( "" );
790 gtk_misc_set_alignment(GTK_MISC(entryField), 0.01, 0.5);
791 gtk_table_attach(GTK_TABLE(table), entryField, 1, 3, top, (top + 1),
792 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
796 label = gtk_label_new(_("Attribute"));
798 * Use an event box to attach some help in the form of a tooltip.
799 * Tried this for the clist but it looked bad.
801 eventBox = gtk_event_box_new();
802 gtk_container_add( GTK_CONTAINER(eventBox), label );
803 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
804 gtk_table_attach(GTK_TABLE(table), eventBox, 0, 1, top, (top + 1),
807 CLAWS_SET_TIP(eventBox, _(
808 "Choose the LDIF field that will be renamed or selected " \
809 "for import in the list above. Reserved fields (marked " \
810 "with a tick in the \"R\" column), are automatically " \
811 "imported and cannot be renamed. A single click in the " \
812 "Select (\"S\") column will select the field for import " \
813 "with a tick. A single click anywhere in the row will " \
814 "select that field for rename in the input area below " \
815 "the list. A double click anywhere in the row will also " \
816 "select the field for import."));
818 entryAttrib = gtk_entry_new();
819 gtk_table_attach(GTK_TABLE(table), entryAttrib, 1, 3, top, (top + 1),
820 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
822 CLAWS_SET_TIP(entryAttrib,
823 _( "The LDIF field can be renamed to the User Attribute name." ));
828 checkSelect = gtk_check_button_new_with_label( _( "Select for Import" ) );
829 gtk_table_attach(GTK_TABLE(table), checkSelect, 1, 2, top, (top + 1),
830 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
832 CLAWS_SET_TIP(checkSelect,
833 _( "Select the LDIF field for import into the address book." ));
835 btnModify = gtk_button_new_with_label( _(" Modify "));
836 gtk_table_attach(GTK_TABLE(table), btnModify, 2, 3, top, (top + 1),
839 CLAWS_SET_TIP(btnModify,
840 _( "This button will update the list above with the data supplied." ));
842 gtk_widget_show_all(vbox);
845 g_signal_connect( G_OBJECT(clist_field), "select_row",
846 G_CALLBACK(imp_ldif_field_list_selected), NULL );
847 g_signal_connect( G_OBJECT(clist_field), "button_press_event",
848 G_CALLBACK(imp_ldif_field_list_toggle), NULL );
849 g_signal_connect( G_OBJECT(btnModify), "clicked",
850 G_CALLBACK(imp_ldif_modify_pressed), NULL );
852 impldif_dlg.clist_field = clist_field;
853 impldif_dlg.entryField = entryField;
854 impldif_dlg.entryAttrib = entryAttrib;
855 impldif_dlg.checkSelect = checkSelect;
856 impldif_dlg.btnModify = btnModify;
860 * Format notebook finish page.
861 * \param pageNum Page (tab) number.
862 * \param pageLbl Page (tab) label.
864 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
868 GtkWidget *labelBook;
869 GtkWidget *labelFile;
870 GtkWidget *labelRecs;
873 vbox = gtk_vbox_new(FALSE, 8);
874 gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
875 gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
877 label = gtk_label_new( pageLbl );
878 gtk_widget_show( label );
879 gtk_notebook_set_tab_label(
880 GTK_NOTEBOOK( impldif_dlg.notebook ),
881 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
884 table = gtk_table_new(3, 2, FALSE);
885 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
886 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
887 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
888 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
892 label = gtk_label_new( _( "Address Book :" ) );
893 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
894 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
896 labelBook = gtk_label_new("");
897 gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
898 gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
902 label = gtk_label_new( _( "File Name :" ) );
903 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
904 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
906 labelFile = gtk_label_new("");
907 gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
908 gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
912 label = gtk_label_new( _("Records Imported :") );
913 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
914 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
916 labelRecs = gtk_label_new("");
917 gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
918 gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
920 impldif_dlg.labelBook = labelBook;
921 impldif_dlg.labelFile = labelFile;
922 impldif_dlg.labelRecords = labelRecs;
926 * Create main dialog decorations (excluding notebook pages).
928 static void imp_ldif_dialog_create() {
936 GtkWidget *btnProceed;
937 GtkWidget *btnCancel;
939 GtkWidget *statusbar;
941 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importldif");
942 gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
943 gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
944 gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
945 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
946 g_signal_connect(G_OBJECT(window), "delete_event",
947 G_CALLBACK(imp_ldif_delete_event),
949 g_signal_connect(G_OBJECT(window), "key_press_event",
950 G_CALLBACK(imp_ldif_key_pressed),
953 vbox = gtk_vbox_new(FALSE, 4);
954 gtk_widget_show(vbox);
955 gtk_container_add(GTK_CONTAINER(window), vbox);
957 vnbox = gtk_vbox_new(FALSE, 4);
958 gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
959 gtk_widget_show(vnbox);
960 gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
963 notebook = gtk_notebook_new();
964 gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
965 gtk_widget_show(notebook);
966 gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
967 gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
970 hsbox = gtk_hbox_new(FALSE, 0);
971 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
972 statusbar = gtk_statusbar_new();
973 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
976 gtkut_stock_button_set_create(&hbbox,
977 &btnCancel, GTK_STOCK_CANCEL,
978 &btnPrev, GTK_STOCK_GO_BACK,
979 &btnNext, GTK_STOCK_GO_FORWARD);
981 btnProceed = gtk_button_new_with_mnemonic(_("Proceed"));
982 gtk_button_set_image(GTK_BUTTON(btnProceed),
983 gtk_image_new_from_stock(GTK_STOCK_OK, GTK_ICON_SIZE_BUTTON));
984 gtkut_widget_set_can_default(btnProceed, TRUE);
985 gtk_box_pack_start(GTK_BOX(hbbox), btnProceed, TRUE, TRUE, 0);
986 gtk_widget_hide(btnProceed);
988 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
989 gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
990 gtk_widget_grab_default(btnNext);
992 /* Button handlers */
993 g_signal_connect(G_OBJECT(btnPrev), "clicked",
994 G_CALLBACK(imp_ldif_prev), NULL);
995 g_signal_connect(G_OBJECT(btnNext), "clicked",
996 G_CALLBACK(imp_ldif_next), NULL);
997 g_signal_connect(G_OBJECT(btnProceed), "clicked",
998 G_CALLBACK(imp_ldif_next), NULL);
999 g_signal_connect(G_OBJECT(btnCancel), "clicked",
1000 G_CALLBACK(imp_ldif_cancel), NULL);
1002 gtk_widget_show_all(vbox);
1004 impldif_dlg.window = window;
1005 impldif_dlg.notebook = notebook;
1006 impldif_dlg.btnPrev = btnPrev;
1007 impldif_dlg.btnNext = btnNext;
1008 impldif_dlg.btnProceed = btnProceed;
1009 impldif_dlg.btnCancel = btnCancel;
1010 impldif_dlg.statusbar = statusbar;
1011 impldif_dlg.status_cid = gtk_statusbar_get_context_id(
1012 GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
1017 * Create import LDIF dialog.
1019 static void imp_ldif_create() {
1020 imp_ldif_dialog_create();
1021 imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
1022 imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
1023 imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
1024 gtk_widget_show_all( impldif_dlg.window );
1029 * \param addrIndex Address index.
1030 * \return Address book file of imported data, or <i>NULL</i> if import
1033 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
1034 _importedBook_ = NULL;
1035 _imp_addressIndex_ = addrIndex;
1037 if( ! impldif_dlg.window )
1040 gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
1042 gtk_widget_hide(impldif_dlg.btnProceed);
1043 gtk_widget_show(impldif_dlg.btnNext);
1045 impldif_dlg.cancelled = FALSE;
1046 gtk_widget_show(impldif_dlg.window);
1047 manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
1048 gtk_widget_grab_default(impldif_dlg.btnNext);
1049 gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), TRUE);
1051 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), IMPORTLDIF_GUESS_NAME );
1052 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), "" );
1053 gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
1054 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
1055 gtk_cmclist_clear( GTK_CMCLIST(impldif_dlg.clist_field) );
1056 gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
1057 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
1058 gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
1059 stock_pixbuf_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
1062 gtk_widget_grab_focus(impldif_dlg.entryFile);
1064 impldif_dlg.rowIndSelect = -1;
1065 impldif_dlg.rowCount = 0;
1066 g_free( impldif_dlg.nameBook );
1067 g_free( impldif_dlg.fileName );
1068 impldif_dlg.nameBook = NULL;
1069 impldif_dlg.fileName = NULL;
1071 _ldifFile_ = ldif_create();
1073 gtk_widget_hide(impldif_dlg.window);
1074 gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), FALSE);
1075 ldif_free( _ldifFile_ );
1077 _imp_addressIndex_ = NULL;
1079 g_free( impldif_dlg.nameBook );
1080 g_free( impldif_dlg.fileName );
1081 impldif_dlg.nameBook = NULL;
1082 impldif_dlg.fileName = NULL;
1084 if( impldif_dlg.cancelled == TRUE ) return NULL;
1085 return _importedBook_;
1089 * ============================================================================
1091 * ============================================================================