2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2009 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>
36 #include "addressbook.h"
37 #include "addressitem.h"
39 #include "stock_pixmap.h"
40 #include "prefs_common.h"
41 #include "manage_window.h"
47 #define IMPORTLDIF_GUESS_NAME "LDIF Import"
49 #define PAGE_FILE_INFO 0
50 #define PAGE_ATTRIBUTES 1
53 #define IMPORTLDIF_WIDTH 390
54 #define IMPORTLDIF_HEIGHT 300
56 #define FIELDS_N_COLS 4
57 #define FIELDS_COL_WIDTH_RESERVED 10
58 #define FIELDS_COL_WIDTH_SELECT 10
59 #define FIELDS_COL_WIDTH_FIELD 140
60 #define FIELDS_COL_WIDTH_ATTRIB 140
63 FIELD_COL_RESERVED = 0,
67 } ImpLdif_FieldColPos;
70 * LDIF dialog definition.
72 static struct _ImpLdif_Dlg {
77 GtkWidget *clist_field;
78 GtkWidget *entryField;
79 GtkWidget *entryAttrib;
80 GtkWidget *checkSelect;
84 GtkWidget *labelRecords;
87 GtkWidget *btnProceed;
98 static struct _AddressFileSelection _imp_ldif_file_selector_;
99 static AddressBookFile *_importedBook_;
100 static AddressIndex *_imp_addressIndex_;
101 static LdifFile *_ldifFile_ = NULL;
103 static GdkPixbuf *markxpm;
106 * Structure of error message table.
108 typedef struct _ErrMsgTableEntry ErrMsgTableEntry;
109 struct _ErrMsgTableEntry {
114 static gchar *_errMsgUnknown_ = N_( "Unknown" );
117 * Lookup table of error messages for general errors. Note that a NULL
118 * description signifies the end of the table.
120 static ErrMsgTableEntry _lutErrorsLDIF_[] = {
121 { MGU_SUCCESS, N_("Success") },
122 { MGU_BAD_ARGS, N_("Bad arguments") },
123 { MGU_NO_FILE, N_("File not specified") },
124 { MGU_OPEN_FILE, N_("Error opening file") },
125 { MGU_ERROR_READ, N_("Error reading file") },
126 { MGU_EOF, N_("End of file encountered") },
127 { MGU_OO_MEMORY, N_("Error allocating memory") },
128 { MGU_BAD_FORMAT, N_("Bad file format") },
129 { MGU_ERROR_WRITE, N_("Error writing to file") },
130 { MGU_OPEN_DIRECTORY, N_("Error opening directory") },
131 { MGU_NO_PATH, N_("No path specified") },
136 * Lookup message for specified error code.
137 * \param lut Lookup table.
138 * \param code Code to lookup.
139 * \return Description associated to code.
141 static gchar *imp_ldif_err2string( ErrMsgTableEntry lut[], gint code ) {
143 ErrMsgTableEntry entry;
146 for( i = 0; ; i++ ) {
148 if( entry.description == NULL ) break;
149 if( entry.code == code ) {
150 desc = entry.description;
155 desc = _errMsgUnknown_;
161 * Display message in status field.
162 * \param msg Message to display.
164 static void imp_ldif_status_show( gchar *msg ) {
165 if( impldif_dlg.statusbar != NULL ) {
166 gtk_statusbar_pop( GTK_STATUSBAR(impldif_dlg.statusbar),
167 impldif_dlg.status_cid );
170 GTK_STATUSBAR(impldif_dlg.statusbar),
171 impldif_dlg.status_cid, msg );
177 * Select and display status message appropriate for the page being displayed.
179 static void imp_ldif_message( void ) {
183 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
184 if( pageNum == PAGE_FILE_INFO ) {
185 sMsg = _( "Please specify address book name and file to import." );
187 else if( pageNum == PAGE_ATTRIBUTES ) {
188 sMsg = _( "Select and rename LDIF field names to import." );
190 else if( pageNum == PAGE_FINISH ) {
191 sMsg = _( "File imported." );
193 imp_ldif_status_show( sMsg );
197 * Update list with data for current row.
198 * \param clist List to update.
200 static void imp_ldif_update_row( GtkCMCList *clist ) {
202 gchar *text[ FIELDS_N_COLS ];
205 if( impldif_dlg.rowIndSelect < 0 ) return;
206 row = impldif_dlg.rowIndSelect;
208 rec = gtk_cmclist_get_row_data( clist, row );
209 text[ FIELD_COL_RESERVED ] = "";
210 text[ FIELD_COL_SELECT ] = "";
211 text[ FIELD_COL_FIELD ] = rec->tagName;
212 text[ FIELD_COL_ATTRIB ] = rec->userName;
214 gtk_cmclist_freeze( clist );
215 gtk_cmclist_remove( clist, row );
216 if( row == impldif_dlg.rowCount - 1 ) {
217 gtk_cmclist_append( clist, text );
220 gtk_cmclist_insert( clist, row, text );
222 if( rec->selected ) {
223 gtk_cmclist_set_pixbuf(
224 clist, row, FIELD_COL_SELECT, markxpm );
226 if( rec->reserved ) {
227 gtk_cmclist_set_pixbuf(
228 clist, row, FIELD_COL_RESERVED, markxpm );
231 gtk_cmclist_set_row_data( clist, row, rec );
232 gtk_cmclist_thaw( clist );
236 * Load list with LDIF fields read from file.
237 * \param ldf LDIF control data.
239 static void imp_ldif_load_fields( LdifFile *ldf ) {
240 GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
242 gchar *text[ FIELDS_N_COLS ];
244 impldif_dlg.rowIndSelect = -1;
245 impldif_dlg.rowCount = 0;
246 if( ! ldf->accessFlag ) return;
247 gtk_cmclist_clear( clist );
248 list = ldif_get_fieldlist( ldf );
251 Ldif_FieldRec *rec = node->data;
254 text[ FIELD_COL_RESERVED ] = "";
255 text[ FIELD_COL_SELECT ] = "";
256 text[ FIELD_COL_FIELD ] = rec->tagName;
257 text[ FIELD_COL_ATTRIB ] = rec->userName;
258 row = gtk_cmclist_append( clist, text );
259 gtk_cmclist_set_row_data( clist, row, rec );
260 if( rec->selected ) {
261 gtk_cmclist_set_pixbuf( clist, row,
262 FIELD_COL_SELECT, markxpm );
264 if( rec->reserved ) {
265 gtk_cmclist_set_pixbuf( clist, row,
266 FIELD_COL_RESERVED, markxpm );
268 impldif_dlg.rowCount++;
269 node = g_list_next( node );
273 ldif_set_accessed( ldf, FALSE );
277 * Callback function when list item is selected.
278 * \param clist List widget.
281 * \param event Event object.
282 * \param data User data.
284 static void imp_ldif_field_list_selected(
285 GtkCMCList *clist, gint row, gint column, GdkEvent *event,
288 Ldif_FieldRec *rec = gtk_cmclist_get_row_data( clist, row );
290 impldif_dlg.rowIndSelect = row;
291 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
293 /* Update widget contents */
295 GTK_LABEL(impldif_dlg.entryField), rec->tagName );
298 GTK_ENTRY(impldif_dlg.entryAttrib), rec->userName );
299 gtk_toggle_button_set_active(
300 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect),
303 /* Disable widgets for reserved fields */
304 gtk_widget_set_sensitive(
305 impldif_dlg.entryAttrib, ! rec->reserved );
306 gtk_widget_set_sensitive(
307 impldif_dlg.checkSelect, ! rec->reserved );
308 gtk_widget_set_sensitive(
309 impldif_dlg.btnModify, ! rec->reserved );
311 gtk_widget_grab_focus(impldif_dlg.entryAttrib);
315 * Callback function to toggle selected LDIF field.
316 * \param clist List to update.
317 * \param event Event object.
320 static gboolean imp_ldif_field_list_toggle(
321 GtkCMCList *clist, GdkEventButton *event, gpointer data )
324 gboolean toggle = FALSE;
326 if( ! event ) return FALSE;
327 if( impldif_dlg.rowIndSelect < 0 ) return FALSE;
328 if( event->button == 1 ) {
329 /* If single click in select column */
330 if( event->type == GDK_BUTTON_PRESS ) {
335 gtk_cmclist_get_selection_info( clist, x, y, &row, &col );
336 if( col != FIELD_COL_SELECT ) return FALSE;
337 if( row > impldif_dlg.rowCount ) return FALSE;
340 impldif_dlg.rowIndSelect = row;
344 /* If double click anywhere in row */
345 else if( event->type == GDK_2BUTTON_PRESS ) {
349 /* Toggle field selection */
351 rec = gtk_cmclist_get_row_data(
352 clist, impldif_dlg.rowIndSelect );
354 ldif_field_toggle( rec );
355 imp_ldif_update_row( clist );
362 * Callback function to update LDIF field data from input fields.
363 * \param widget Widget (button).
364 * \param data User data.
366 static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
367 GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
371 if( impldif_dlg.rowIndSelect < 0 ) return;
372 row = impldif_dlg.rowIndSelect;
373 rec = gtk_cmclist_get_row_data( clist, impldif_dlg.rowIndSelect );
375 ldif_field_set_name( rec, gtk_editable_get_chars(
376 GTK_EDITABLE(impldif_dlg.entryAttrib), 0, -1 ) );
377 ldif_field_set_selected( rec, gtk_toggle_button_get_active(
378 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect) ) );
379 imp_ldif_update_row( clist );
380 gtk_cmclist_select_row( clist, row, 0 );
381 gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
382 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
383 gtk_toggle_button_set_active(
384 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect), FALSE );
388 * Test whether we can move off fields page.
389 * \return <i>TRUE</i> if OK to move off page.
391 static gboolean imp_ldif_field_move() {
392 gboolean retVal = FALSE;
394 AddressBookFile *abf = NULL;
396 if( _importedBook_ ) {
397 addrbook_free_book( _importedBook_ );
400 abf = addrbook_create_book();
401 addrbook_set_path( abf, _imp_addressIndex_->filePath );
402 addrbook_set_name( abf, impldif_dlg.nameBook );
403 newFile = addrbook_guess_next_file( abf );
404 addrbook_set_file( abf, newFile );
407 /* Import data into file */
408 if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
409 addrbook_save_data( abf );
410 _importedBook_ = abf;
414 addrbook_free_book( abf );
421 * Test whether we can move off file page.
422 * \return <i>TRUE</i> if OK to move off page.
424 static gboolean imp_ldif_file_move() {
425 gboolean retVal = FALSE;
429 gboolean errFlag = FALSE;
431 sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryFile), 0, -1 );
432 g_strchug( sFile ); g_strchomp( sFile );
434 sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
435 g_strchug( sName ); g_strchomp( sName );
437 g_free( impldif_dlg.nameBook );
438 g_free( impldif_dlg.fileName );
439 impldif_dlg.nameBook = sName;
440 impldif_dlg.fileName = sFile;
442 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), sFile );
443 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), sName );
445 if( *sFile == '\0' ) {
446 sMsg = _( "Please select a file." );
447 gtk_widget_grab_focus(impldif_dlg.entryFile);
451 if( ! errFlag && *sName == '\0' ) {
452 sMsg = _( "Address book name must be supplied." );
453 gtk_widget_grab_focus(impldif_dlg.entryName);
458 /* Read attribute list */
459 ldif_set_file( _ldifFile_, sFile );
460 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
462 /* ldif_print_file( _ldifFile_, stdout ); */
463 imp_ldif_load_fields( _ldifFile_ );
467 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
470 imp_ldif_status_show( sMsg );
476 * Display finish page.
478 static void imp_ldif_finish_show() {
482 name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
483 gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
485 gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
486 gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
487 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
488 gtk_widget_hide( impldif_dlg.btnNext );
489 gtk_widget_show( impldif_dlg.btnProceed );
490 gtk_widget_set_sensitive( impldif_dlg.btnProceed, FALSE );
491 if( _ldifFile_->retVal == MGU_SUCCESS ) {
492 sMsg = _( "LDIF file imported successfully." );
495 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
497 imp_ldif_status_show( sMsg );
498 gtk_widget_grab_focus(impldif_dlg.btnCancel);
502 * Callback function to select previous page.
503 * \param widget Widget (button).
505 static void imp_ldif_prev( GtkWidget *widget ) {
508 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
509 if( pageNum == PAGE_ATTRIBUTES ) {
510 /* Goto file page stuff */
511 gtk_notebook_set_current_page(
512 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
513 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
514 gtk_widget_hide( impldif_dlg.btnProceed );
515 gtk_widget_show( impldif_dlg.btnNext );
521 * Callback function to select next page.
522 * \param widget Widget (button).
524 static void imp_ldif_next( GtkWidget *widget ) {
527 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
528 if( pageNum == PAGE_FILE_INFO ) {
529 /* Goto attributes stuff */
530 if( imp_ldif_file_move() ) {
531 gtk_notebook_set_current_page(
532 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
534 gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
535 gtk_widget_hide( impldif_dlg.btnNext );
536 gtk_widget_show( impldif_dlg.btnProceed );
537 gtk_widget_set_sensitive( impldif_dlg.btnProceed, TRUE );
540 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
541 _ldifFile_->dirtyFlag = TRUE;
544 else if( pageNum == PAGE_ATTRIBUTES ) {
545 /* Goto finish stuff */
546 if( imp_ldif_field_move() ) {
547 gtk_notebook_set_current_page(
548 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
549 gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
551 imp_ldif_finish_show();
557 * Callback function to cancel and close dialog.
558 * \param widget Widget (button).
559 * \param data User data.
561 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
564 pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
565 if( pageNum != PAGE_FINISH ) {
566 impldif_dlg.cancelled = TRUE;
573 * Create LDIF file selection dialog.
574 * \param afs Address file selection data.
576 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
577 gchar *file = filesel_select_file_open(_("Select LDIF File"), NULL);
580 afs->cancelled = TRUE;
582 afs->cancelled = FALSE;
583 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), file );
589 * Callback function to display LDIF file selection dialog.
591 static void imp_ldif_file_select( void ) {
592 imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
596 * Callback function to handle dialog close event.
597 * \param widget Widget (dialog).
598 * \param event Event object.
599 * \param data User data.
601 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
602 imp_ldif_cancel( widget, data );
607 * Callback function to respond to dialog key press events.
608 * \param widget Widget.
609 * \param event Event object.
610 * \param data User data.
612 static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
613 if (event && event->keyval == GDK_Escape) {
614 imp_ldif_cancel( widget, data );
620 * Format notebook "file" page.
621 * \param pageNum Page (tab) number.
622 * \param pageLbl Page (tab) label.
624 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
628 GtkWidget *entryFile;
629 GtkWidget *entryName;
634 vbox = gtk_vbox_new(FALSE, 8);
635 gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
636 gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
638 label = gtk_label_new( pageLbl );
639 gtk_widget_show( label );
640 gtk_notebook_set_tab_label(
641 GTK_NOTEBOOK( impldif_dlg.notebook ),
642 gtk_notebook_get_nth_page(
643 GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
646 table = gtk_table_new(2, 3, FALSE);
647 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
648 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
649 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
650 gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
654 label = gtk_label_new(_("Address Book"));
655 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
657 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
659 entryName = gtk_entry_new();
660 gtk_table_attach(GTK_TABLE(table), entryName, 1, 2, top, (top + 1),
661 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
663 CLAWS_SET_TIP(entryName, _(
664 "Specify the name for the address book that will " \
665 "be created from the LDIF file data." ));
669 label = gtk_label_new(_("File Name"));
670 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
672 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
674 entryFile = gtk_entry_new();
675 gtk_table_attach(GTK_TABLE(table), entryFile, 1, 2, top, (top + 1),
676 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
678 CLAWS_SET_TIP(entryFile,
679 _( "The full file specification of the LDIF file to import." ));
681 btnFile = gtkut_get_browse_file_btn(_("B_rowse"));
682 gtk_table_attach(GTK_TABLE(table), btnFile, 2, 3, top, (top + 1),
685 CLAWS_SET_TIP(btnFile,
686 _( "Select the LDIF file to import." ));
688 gtk_widget_show_all(vbox);
691 g_signal_connect(G_OBJECT(btnFile), "clicked",
692 G_CALLBACK(imp_ldif_file_select), NULL);
694 impldif_dlg.entryFile = entryFile;
695 impldif_dlg.entryName = entryName;
700 * Format notebook fields page.
701 * \param pageNum Page (tab) number.
702 * \param pageLbl Page (tab) label.
704 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
710 GtkWidget *clist_swin;
711 GtkWidget *clist_field;
712 GtkWidget *entryField;
713 GtkWidget *entryAttrib;
714 GtkWidget *checkSelect;
715 GtkWidget *btnModify;
720 gchar *titles[ FIELDS_N_COLS ];
723 titles[ FIELD_COL_RESERVED ] = _("R");
724 titles[ FIELD_COL_SELECT ] = _("S");
725 titles[ FIELD_COL_FIELD ] = _("LDIF Field Name");
726 titles[ FIELD_COL_ATTRIB ] = _("Attribute Name");
728 vbox = gtk_vbox_new(FALSE, 8);
729 gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
730 gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
732 label = gtk_label_new( pageLbl );
733 gtk_widget_show( label );
734 gtk_notebook_set_tab_label(
735 GTK_NOTEBOOK( impldif_dlg.notebook ),
736 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
739 /* Upper area - Field list */
740 vboxt = gtk_vbox_new( FALSE, 4 );
741 gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
743 clist_swin = gtk_scrolled_window_new( NULL, NULL );
744 gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
745 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
746 GTK_POLICY_AUTOMATIC,
747 GTK_POLICY_AUTOMATIC);
749 clist_field = gtk_cmclist_new_with_titles( FIELDS_N_COLS, titles );
750 gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
751 gtk_cmclist_set_selection_mode(
752 GTK_CMCLIST(clist_field), GTK_SELECTION_BROWSE );
753 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
754 FIELD_COL_RESERVED, FIELDS_COL_WIDTH_RESERVED );
755 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
756 FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
757 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
758 FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
759 gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
760 FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
762 /* Remove focus capability for column headers */
763 for( i = 0; i < FIELDS_N_COLS; i++ ) {
764 GTK_WIDGET_UNSET_FLAGS(
765 GTK_CMCLIST(clist_field)->column[i].button,
769 /* Lower area - Edit area */
770 vboxb = gtk_vbox_new( FALSE, 4 );
771 gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
773 /* Data entry area */
774 table = gtk_table_new( 3, 3, FALSE);
775 gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
776 gtk_table_set_row_spacings(GTK_TABLE(table), 4);
777 gtk_table_set_col_spacings(GTK_TABLE(table), 4);
781 label = gtk_label_new(_("LDIF Field"));
782 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
784 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
786 entryField = gtk_label_new( "" );
787 gtk_misc_set_alignment(GTK_MISC(entryField), 0.01, 0.5);
788 gtk_table_attach(GTK_TABLE(table), entryField, 1, 3, top, (top + 1),
789 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
793 label = gtk_label_new(_("Attribute"));
795 * Use an event box to attach some help in the form of a tooltip.
796 * Tried this for the clist but it looked bad.
798 eventBox = gtk_event_box_new();
799 gtk_container_add( GTK_CONTAINER(eventBox), label );
800 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
801 gtk_table_attach(GTK_TABLE(table), eventBox, 0, 1, top, (top + 1),
804 CLAWS_SET_TIP(eventBox, _(
805 "Choose the LDIF field that will be renamed or selected " \
806 "for import in the list above. Reserved fields (marked " \
807 "with a tick in the \"R\" column), are automatically " \
808 "imported and cannot be renamed. A single click in the " \
809 "Select (\"S\") column will select the field for import " \
810 "with a tick. A single click anywhere in the row will " \
811 "select that field for rename in the input area below " \
812 "the list. A double click anywhere in the row will also " \
813 "select the field for import."));
815 entryAttrib = gtk_entry_new();
816 gtk_table_attach(GTK_TABLE(table), entryAttrib, 1, 3, top, (top + 1),
817 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
819 CLAWS_SET_TIP(entryAttrib,
820 _( "The LDIF field can be renamed to the User Attribute name." ));
825 checkSelect = gtk_check_button_new_with_label( _( "Select for Import" ) );
826 gtk_table_attach(GTK_TABLE(table), checkSelect, 1, 2, top, (top + 1),
827 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
829 CLAWS_SET_TIP(checkSelect,
830 _( "Select the LDIF field for import into the address book." ));
832 btnModify = gtk_button_new_with_label( _(" Modify "));
833 gtk_table_attach(GTK_TABLE(table), btnModify, 2, 3, top, (top + 1),
836 CLAWS_SET_TIP(btnModify,
837 _( "This button will update the list above with the data supplied." ));
839 gtk_widget_show_all(vbox);
842 g_signal_connect( G_OBJECT(clist_field), "select_row",
843 G_CALLBACK(imp_ldif_field_list_selected), NULL );
844 g_signal_connect( G_OBJECT(clist_field), "button_press_event",
845 G_CALLBACK(imp_ldif_field_list_toggle), NULL );
846 g_signal_connect( G_OBJECT(btnModify), "clicked",
847 G_CALLBACK(imp_ldif_modify_pressed), NULL );
849 impldif_dlg.clist_field = clist_field;
850 impldif_dlg.entryField = entryField;
851 impldif_dlg.entryAttrib = entryAttrib;
852 impldif_dlg.checkSelect = checkSelect;
853 impldif_dlg.btnModify = btnModify;
857 * Format notebook finish page.
858 * \param pageNum Page (tab) number.
859 * \param pageLbl Page (tab) label.
861 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
865 GtkWidget *labelBook;
866 GtkWidget *labelFile;
867 GtkWidget *labelRecs;
870 vbox = gtk_vbox_new(FALSE, 8);
871 gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
872 gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
874 label = gtk_label_new( pageLbl );
875 gtk_widget_show( label );
876 gtk_notebook_set_tab_label(
877 GTK_NOTEBOOK( impldif_dlg.notebook ),
878 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
881 table = gtk_table_new(3, 2, FALSE);
882 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
883 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
884 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
885 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
889 label = gtk_label_new( _( "Address Book :" ) );
890 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
891 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
893 labelBook = gtk_label_new("");
894 gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
895 gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
899 label = gtk_label_new( _( "File Name :" ) );
900 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
901 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
903 labelFile = gtk_label_new("");
904 gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
905 gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
909 label = gtk_label_new( _("Records Imported :") );
910 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
911 gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
913 labelRecs = gtk_label_new("");
914 gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
915 gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
917 impldif_dlg.labelBook = labelBook;
918 impldif_dlg.labelFile = labelFile;
919 impldif_dlg.labelRecords = labelRecs;
923 * Create main dialog decorations (excluding notebook pages).
925 static void imp_ldif_dialog_create() {
933 GtkWidget *btnProceed;
934 GtkWidget *btnCancel;
936 GtkWidget *statusbar;
938 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importldif");
939 gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
940 gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
941 gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
942 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
943 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
944 g_signal_connect(G_OBJECT(window), "delete_event",
945 G_CALLBACK(imp_ldif_delete_event),
947 g_signal_connect(G_OBJECT(window), "key_press_event",
948 G_CALLBACK(imp_ldif_key_pressed),
951 vbox = gtk_vbox_new(FALSE, 4);
952 gtk_widget_show(vbox);
953 gtk_container_add(GTK_CONTAINER(window), vbox);
955 vnbox = gtk_vbox_new(FALSE, 4);
956 gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
957 gtk_widget_show(vnbox);
958 gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
961 notebook = gtk_notebook_new();
962 gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
963 gtk_widget_show(notebook);
964 gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
965 gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
968 hsbox = gtk_hbox_new(FALSE, 0);
969 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
970 statusbar = gtk_statusbar_new();
971 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
974 gtkut_stock_button_set_create(&hbbox,
975 &btnCancel, GTK_STOCK_CANCEL,
976 &btnPrev, GTK_STOCK_GO_BACK,
977 &btnNext, GTK_STOCK_GO_FORWARD);
979 btnProceed = gtk_button_new_with_mnemonic(_("Proceed"));
980 gtk_button_set_image(GTK_BUTTON(btnProceed),
981 gtk_image_new_from_stock(GTK_STOCK_OK, GTK_ICON_SIZE_BUTTON));
982 GTK_WIDGET_SET_FLAGS(btnProceed, GTK_CAN_DEFAULT);
983 gtk_box_pack_start(GTK_BOX(hbbox), btnProceed, TRUE, TRUE, 0);
984 gtk_widget_hide(btnProceed);
986 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
987 gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
988 gtk_widget_grab_default(btnNext);
990 /* Button handlers */
991 g_signal_connect(G_OBJECT(btnPrev), "clicked",
992 G_CALLBACK(imp_ldif_prev), NULL);
993 g_signal_connect(G_OBJECT(btnNext), "clicked",
994 G_CALLBACK(imp_ldif_next), NULL);
995 g_signal_connect(G_OBJECT(btnProceed), "clicked",
996 G_CALLBACK(imp_ldif_next), NULL);
997 g_signal_connect(G_OBJECT(btnCancel), "clicked",
998 G_CALLBACK(imp_ldif_cancel), NULL);
1000 gtk_widget_show_all(vbox);
1002 impldif_dlg.window = window;
1003 impldif_dlg.notebook = notebook;
1004 impldif_dlg.btnPrev = btnPrev;
1005 impldif_dlg.btnNext = btnNext;
1006 impldif_dlg.btnProceed = btnProceed;
1007 impldif_dlg.btnCancel = btnCancel;
1008 impldif_dlg.statusbar = statusbar;
1009 impldif_dlg.status_cid = gtk_statusbar_get_context_id(
1010 GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
1015 * Create import LDIF dialog.
1017 static void imp_ldif_create() {
1018 imp_ldif_dialog_create();
1019 imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
1020 imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
1021 imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
1022 gtk_widget_show_all( impldif_dlg.window );
1027 * \param addrIndex Address index.
1028 * \return Address book file of imported data, or <i>NULL</i> if import
1031 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
1032 _importedBook_ = NULL;
1033 _imp_addressIndex_ = addrIndex;
1035 if( ! impldif_dlg.window )
1038 gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
1040 gtk_widget_hide(impldif_dlg.btnProceed);
1041 gtk_widget_show(impldif_dlg.btnNext);
1043 impldif_dlg.cancelled = FALSE;
1044 gtk_widget_show(impldif_dlg.window);
1045 manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
1046 gtk_widget_grab_default(impldif_dlg.btnNext);
1048 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), IMPORTLDIF_GUESS_NAME );
1049 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), "" );
1050 gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
1051 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
1052 gtk_cmclist_clear( GTK_CMCLIST(impldif_dlg.clist_field) );
1053 gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
1054 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
1055 gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
1056 stock_pixbuf_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
1059 gtk_widget_grab_focus(impldif_dlg.entryFile);
1061 impldif_dlg.rowIndSelect = -1;
1062 impldif_dlg.rowCount = 0;
1063 g_free( impldif_dlg.nameBook );
1064 g_free( impldif_dlg.fileName );
1065 impldif_dlg.nameBook = NULL;
1066 impldif_dlg.fileName = NULL;
1068 _ldifFile_ = ldif_create();
1070 gtk_widget_hide(impldif_dlg.window);
1071 ldif_free( _ldifFile_ );
1073 _imp_addressIndex_ = NULL;
1075 g_free( impldif_dlg.nameBook );
1076 g_free( impldif_dlg.fileName );
1077 impldif_dlg.nameBook = NULL;
1078 impldif_dlg.fileName = NULL;
1080 if( impldif_dlg.cancelled == TRUE ) return NULL;
1081 return _importedBook_;
1085 * ============================================================================
1087 * ============================================================================