* src/mainwindow.c
[claws.git] / src / importldif.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2003 Match Grun
4  *
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 2 of the License, or
8  * (at your option) any later version.
9  *
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.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Import LDIF address book data.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <gtk/gtkwindow.h>
33 #include <gtk/gtksignal.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtktable.h>
37 #include <gtk/gtkbutton.h>
38
39 #include "intl.h"
40 #include "addrbook.h"
41 #include "addressbook.h"
42 #include "addressitem.h"
43 #include "gtkutils.h"
44 #include "stock_pixmap.h"
45 #include "prefs_common.h"
46 #include "manage_window.h"
47 #include "mgutils.h"
48 #include "ldif.h"
49 #include "utils.h"
50
51 #define IMPORTLDIF_GUESS_NAME "LDIF Import"
52
53 #define PAGE_FILE_INFO             0
54 #define PAGE_ATTRIBUTES            1
55 #define PAGE_FINISH                2
56
57 #define IMPORTLDIF_WIDTH           380
58 #define IMPORTLDIF_HEIGHT          300
59
60 #define FIELDS_N_COLS              3
61 #define FIELDS_COL_WIDTH_SELECT    10
62 #define FIELDS_COL_WIDTH_FIELD     140
63 #define FIELDS_COL_WIDTH_ATTRIB    140
64
65 typedef enum {
66         FIELD_COL_SELECT  = 0,
67         FIELD_COL_FIELD   = 1,
68         FIELD_COL_ATTRIB  = 2
69 } ImpLdif_FieldColPos;
70
71 static struct _ImpLdif_Dlg {
72         GtkWidget *window;
73         GtkWidget *notebook;
74         GtkWidget *file_entry;
75         GtkWidget *name_entry;
76         GtkWidget *clist_field;
77         GtkWidget *name_ldif;
78         GtkWidget *name_attrib;
79         GtkWidget *check_select;
80         GtkWidget *labelBook;
81         GtkWidget *labelFile;
82         GtkWidget *labelRecords;
83         GtkWidget *btnPrev;
84         GtkWidget *btnNext;
85         GtkWidget *btnCancel;
86         GtkWidget *statusbar;
87         gint      status_cid;
88         gint      rowIndSelect;
89         gint      rowCount;
90         gchar     *nameBook;
91         gchar     *fileName;
92         gboolean  cancelled;
93 } impldif_dlg;
94
95 static struct _AddressFileSelection _imp_ldif_file_selector_;
96 static AddressBookFile *_importedBook_;
97 static AddressIndex *_imp_addressIndex_;
98 static LdifFile *_ldifFile_ = NULL;
99
100 static GdkPixmap *markxpm;
101 static GdkBitmap *markxpmmask;
102
103 /**
104  * Structure of error message table.
105  */
106 typedef struct _ErrMsgTableEntry ErrMsgTableEntry;
107 struct _ErrMsgTableEntry {
108         gint    code;
109         gchar   *description;
110 };
111
112 static gchar *_errMsgUnknown_ = N_( "Unknown" );
113
114 /**
115  * Lookup table of error messages for general errors. Note that a NULL
116  * description signifies the end of the table.
117  */
118 static ErrMsgTableEntry _lutErrorsLDIF_[] = {
119         { MGU_SUCCESS,          N_("Success") },
120         { MGU_BAD_ARGS,         N_("Bad arguments") },
121         { MGU_NO_FILE,          N_("File not specified") },
122         { MGU_OPEN_FILE,        N_("Error opening file") },
123         { MGU_ERROR_READ,       N_("Error reading file") },
124         { MGU_EOF,              N_("End of file encountered") },
125         { MGU_OO_MEMORY,        N_("Error allocating memory") },
126         { MGU_BAD_FORMAT,       N_("Bad file format") },
127         { MGU_ERROR_WRITE,      N_("Error writing to file") },
128         { MGU_OPEN_DIRECTORY,   N_("Error opening directory") },
129         { MGU_NO_PATH,          N_("No path specified") },
130         { 0,                    NULL }
131 };
132
133 /**
134  * Lookup message for specified error code.
135  * \param lut  Lookup table.
136  * \param code Code to lookup.
137  * \return Description associated to code.
138  */
139 static gchar *imp_ldif_err2string( ErrMsgTableEntry lut[], gint code ) {
140         gchar *desc = NULL;
141         ErrMsgTableEntry entry;
142         gint i;
143
144         for( i = 0; ; i++ ) {
145                 entry = lut[ i ];
146                 if( entry.description == NULL ) break;
147                 if( entry.code == code ) {
148                         desc = entry.description;
149                         break;
150                 }
151         }
152         if( ! desc ) {
153                 desc = _errMsgUnknown_;
154         }
155         return desc;
156 }
157
158 static void imp_ldif_status_show( gchar *msg ) {
159         if( impldif_dlg.statusbar != NULL ) {
160                 gtk_statusbar_pop( GTK_STATUSBAR(impldif_dlg.statusbar),
161                         impldif_dlg.status_cid );
162                 if( msg ) {
163                         gtk_statusbar_push(
164                                 GTK_STATUSBAR(impldif_dlg.statusbar),
165                                 impldif_dlg.status_cid, msg );
166                 }
167         }
168 }
169
170 static void imp_ldif_message( void ) {
171         gchar *sMsg = NULL;
172         gint pageNum;
173
174         pageNum = gtk_notebook_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
175         if( pageNum == PAGE_FILE_INFO ) {
176                 sMsg = _( "Please specify address book name and file to import." );
177         }
178         else if( pageNum == PAGE_ATTRIBUTES ) {
179                 sMsg = _( "Select and rename LDIF field names to import." );
180         }
181         else if( pageNum == PAGE_FINISH ) {
182                 sMsg = _( "File imported." );
183         }
184         imp_ldif_status_show( sMsg );
185 }
186
187 static void imp_ldif_update_row( GtkCList *clist ) {
188         Ldif_FieldRec *rec;
189         gchar *text[ FIELDS_N_COLS ];
190         gint row;
191
192         if( impldif_dlg.rowIndSelect < 0 ) return;
193         row = impldif_dlg.rowIndSelect;
194
195         rec = gtk_clist_get_row_data( clist, row );
196         text[ FIELD_COL_SELECT ] = "";
197         text[ FIELD_COL_FIELD  ] = rec->tagName;
198         text[ FIELD_COL_ATTRIB ] = rec->userName;
199
200         gtk_clist_freeze( clist );
201         gtk_clist_remove( clist, row );
202         if( row == impldif_dlg.rowCount - 1 ) {
203                 gtk_clist_append( clist, text );
204         }
205         else {
206                 gtk_clist_insert( clist, row, text );
207         }
208         if( rec->selected )
209                 gtk_clist_set_pixmap( clist, row, FIELD_COL_SELECT, markxpm, markxpmmask );
210
211         gtk_clist_set_row_data( clist, row, rec );
212         gtk_clist_thaw( clist );
213 }
214
215 static void imp_ldif_load_fields( LdifFile *ldf ) {
216         GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field);
217         GList *node, *list;
218         gchar *text[ FIELDS_N_COLS ];
219
220         impldif_dlg.rowIndSelect = -1;
221         impldif_dlg.rowCount = 0;
222         if( ! ldf->accessFlag ) return;
223         gtk_clist_clear( clist );
224         list = ldif_get_fieldlist( ldf );
225         node = list;
226         while( node ) {
227                 Ldif_FieldRec *rec = node->data;
228                 gint row;
229
230                 if( ! rec->reserved ) {
231                         text[ FIELD_COL_SELECT ] = "";
232                         text[ FIELD_COL_FIELD  ] = rec->tagName;
233                         text[ FIELD_COL_ATTRIB ] = rec->userName;
234                         row = gtk_clist_append( clist, text );
235                         gtk_clist_set_row_data( clist, row, rec );
236                         if( rec->selected )
237                                 gtk_clist_set_pixmap(
238                                         clist, row, FIELD_COL_SELECT, markxpm,
239                                         markxpmmask );
240                         impldif_dlg.rowCount++;
241                 }
242                 node = g_list_next( node );
243         }
244         g_list_free( list );
245         list = NULL;
246         ldif_set_accessed( ldf, FALSE );
247 }
248
249 static void imp_ldif_field_list_selected(
250                 GtkCList *clist, gint row, gint column, GdkEvent *event,
251                 gpointer data )
252 {
253         Ldif_FieldRec *rec = gtk_clist_get_row_data( clist, row );
254
255         impldif_dlg.rowIndSelect = row;
256         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
257         if( rec ) {
258                 gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), rec->tagName );
259                 if( rec->userName )
260                         gtk_entry_set_text(
261                                 GTK_ENTRY(impldif_dlg.name_attrib), rec->userName );
262                 gtk_toggle_button_set_active(
263                         GTK_TOGGLE_BUTTON( impldif_dlg.check_select),
264                         rec->selected );
265         }
266         gtk_widget_grab_focus(impldif_dlg.name_attrib);
267 }
268
269 static void imp_ldif_field_list_toggle(
270                 GtkCList *clist, GdkEventButton *event, gpointer data )
271 {
272         if( ! event ) return;
273         if( impldif_dlg.rowIndSelect < 0 ) return;
274         if( event->button == 1 ) {
275                 if( event->type == GDK_2BUTTON_PRESS ) {
276                         Ldif_FieldRec *rec = gtk_clist_get_row_data(
277                                 clist, impldif_dlg.rowIndSelect );
278                         if( rec ) {
279                                 rec->selected = ! rec->selected;
280                                 imp_ldif_update_row( clist );
281                         }
282                 }
283         }
284 }
285
286 static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
287         GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field);
288         Ldif_FieldRec *rec;
289         gint row;
290
291         if( impldif_dlg.rowIndSelect < 0 ) return;
292         row = impldif_dlg.rowIndSelect;
293         rec = gtk_clist_get_row_data( clist, impldif_dlg.rowIndSelect );
294
295         g_free( rec->userName );
296         rec->userName = gtk_editable_get_chars(
297                                 GTK_EDITABLE(impldif_dlg.name_attrib), 0, -1 );
298         rec->selected = gtk_toggle_button_get_active(
299                                 GTK_TOGGLE_BUTTON( impldif_dlg.check_select) );
300         imp_ldif_update_row( clist );
301         gtk_clist_select_row( clist, row, 0 );
302         gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), "" );
303         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
304         gtk_toggle_button_set_active(
305                 GTK_TOGGLE_BUTTON( impldif_dlg.check_select), FALSE );
306 }
307
308 /*
309 * Move off fields page.
310 * return: TRUE if OK to move off page.
311 */
312 static gboolean imp_ldif_field_move() {
313         gboolean retVal = FALSE;
314         gchar *newFile;
315         AddressBookFile *abf = NULL;
316
317         if( _importedBook_ ) {
318                 addrbook_free_book( _importedBook_ );
319         }
320
321         abf = addrbook_create_book();
322         addrbook_set_path( abf, _imp_addressIndex_->filePath );
323         addrbook_set_name( abf, impldif_dlg.nameBook );
324         newFile = addrbook_guess_next_file( abf );
325         addrbook_set_file( abf, newFile );
326         g_free( newFile );
327
328         /* Import data into file */
329         if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
330                 addrbook_save_data( abf );
331                 _importedBook_ = abf;
332                 retVal = TRUE;
333         }
334         else {
335                 addrbook_free_book( abf );
336         }
337
338         return retVal;
339 }
340
341 /*
342 * Move off fields page.
343 * return: TRUE if OK to move off page.
344 */
345 static gboolean imp_ldif_file_move() {
346         gboolean retVal = FALSE;
347         gchar *sName;
348         gchar *sFile;
349         gchar *sMsg = NULL;
350         gboolean errFlag = FALSE;
351
352         sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.file_entry), 0, -1 );
353         g_strchug( sFile ); g_strchomp( sFile );
354
355         sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_entry), 0, -1 );
356         g_strchug( sName ); g_strchomp( sName );
357
358         g_free( impldif_dlg.nameBook );
359         g_free( impldif_dlg.fileName );
360         impldif_dlg.nameBook = sName;
361         impldif_dlg.fileName = sFile;
362
363         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), sFile );
364         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_entry), sName );
365
366         if( *sFile == '\0'|| strlen( sFile ) < 1 ) {
367                 sMsg = _( "Please select a file." );
368                 gtk_widget_grab_focus(impldif_dlg.file_entry);
369                 errFlag = TRUE;
370         }
371
372         if( *sName == '\0'|| strlen( sName ) < 1 ) {
373                 if( ! errFlag ) sMsg = _( "Address book name must be supplied." );
374                 gtk_widget_grab_focus(impldif_dlg.name_entry);
375                 errFlag = TRUE;
376         }
377
378         if( ! errFlag ) {
379                 /* Read attribute list */
380                 ldif_set_file( _ldifFile_, sFile );
381                 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
382                         /* Load fields */
383                         /* ldif_print_file( _ldifFile_, stdout ); */
384                         imp_ldif_load_fields( _ldifFile_ );
385                         retVal = TRUE;
386                 }
387                 else {
388                         sMsg = _( "Error reading LDIF fields." );
389                 }
390         }
391         imp_ldif_status_show( sMsg );
392
393         return retVal;
394 }
395
396 /*
397  * Display finish page.
398  */
399 static void imp_ldif_finish_show() {
400         gchar *sMsg;
401         gchar *name;
402
403         name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_entry), 0, -1 );
404         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
405         g_free( name );
406         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
407         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
408         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
409         gtk_widget_set_sensitive( impldif_dlg.btnNext, FALSE );
410         if( _ldifFile_->retVal == MGU_SUCCESS ) {
411                 sMsg = _( "LDIF file imported successfully." );
412         }
413         else {
414                 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
415         }
416         imp_ldif_status_show( sMsg );
417         gtk_widget_grab_focus(impldif_dlg.btnCancel);
418 }
419
420 static void imp_ldif_prev( GtkWidget *widget ) {
421         gint pageNum;
422
423         pageNum = gtk_notebook_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
424         if( pageNum == PAGE_ATTRIBUTES ) {
425                 /* Goto file page stuff */
426                 gtk_notebook_set_page(
427                         GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
428                 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
429         }
430         imp_ldif_message();
431 }
432
433 static void imp_ldif_next( GtkWidget *widget ) {
434         gint pageNum;
435
436         pageNum = gtk_notebook_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
437         if( pageNum == PAGE_FILE_INFO ) {
438                 /* Goto attributes stuff */
439                 if( imp_ldif_file_move() ) {
440                         gtk_notebook_set_page(
441                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
442                         imp_ldif_message();
443                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
444                 }
445                 else {
446                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
447                 }
448         }
449         else if( pageNum == PAGE_ATTRIBUTES ) {
450                 /* Goto finish stuff */
451                 if( imp_ldif_field_move() ) {
452                         gtk_notebook_set_page(
453                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
454                         imp_ldif_finish_show();
455                 }
456         }
457 }
458
459 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
460         gint pageNum;
461
462         pageNum = gtk_notebook_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
463         if( pageNum != PAGE_FINISH ) {
464                 impldif_dlg.cancelled = TRUE;
465         }
466         gtk_main_quit();
467 }
468
469 static void imp_ldif_file_ok( GtkWidget *widget, gpointer data ) {
470         gchar *sFile;
471         AddressFileSelection *afs;
472         GtkWidget *fileSel;
473
474         afs = ( AddressFileSelection * ) data;
475         fileSel = afs->fileSelector;
476         sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
477
478         afs->cancelled = FALSE;
479         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), sFile );
480         gtk_widget_hide( afs->fileSelector );
481         gtk_grab_remove( afs->fileSelector );
482         gtk_widget_grab_focus( impldif_dlg.file_entry );
483 }
484
485 static void imp_ldif_file_cancel( GtkWidget *widget, gpointer data ) {
486         AddressFileSelection *afs = ( AddressFileSelection * ) data;
487         afs->cancelled = TRUE;
488         gtk_widget_hide( afs->fileSelector );
489         gtk_grab_remove( afs->fileSelector );
490         gtk_widget_grab_focus( impldif_dlg.file_entry );
491 }
492
493 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
494         GtkWidget *fileSelector;
495
496         fileSelector = gtk_file_selection_new( _("Select LDIF File") );
497         gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
498         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
499                 "clicked", GTK_SIGNAL_FUNC (imp_ldif_file_ok), ( gpointer ) afs );
500         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
501                 "clicked", GTK_SIGNAL_FUNC (imp_ldif_file_cancel), ( gpointer ) afs );
502         afs->fileSelector = fileSelector;
503         afs->cancelled = TRUE;
504 }
505
506 static void imp_ldif_file_select( void ) {
507         gchar *sFile;
508         if( ! _imp_ldif_file_selector_.fileSelector )
509                 imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
510
511         sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.file_entry), 0, -1 );
512         gtk_file_selection_set_filename(
513                 GTK_FILE_SELECTION( _imp_ldif_file_selector_.fileSelector ),
514                 sFile );
515         g_free( sFile );
516         gtk_widget_show( _imp_ldif_file_selector_.fileSelector );
517         gtk_grab_add( _imp_ldif_file_selector_.fileSelector );
518 }
519
520 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
521         imp_ldif_cancel( widget, data );
522         return TRUE;
523 }
524
525 static void imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
526         if (event && event->keyval == GDK_Escape) {
527                 imp_ldif_cancel( widget, data );
528         }
529 }
530
531 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
532         GtkWidget *vbox;
533         GtkWidget *table;
534         GtkWidget *label;
535         GtkWidget *file_entry;
536         GtkWidget *name_entry;
537         GtkWidget *file_btn;
538         gint top;
539
540         vbox = gtk_vbox_new(FALSE, 8);
541         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
542         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
543
544         label = gtk_label_new( pageLbl );
545         gtk_widget_show( label );
546         gtk_notebook_set_tab_label(
547                 GTK_NOTEBOOK( impldif_dlg.notebook ),
548                 gtk_notebook_get_nth_page(
549                         GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
550                 label );
551
552         table = gtk_table_new(2, 3, FALSE);
553         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
554         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
555         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
556         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
557
558         /* First row */
559         top = 0;
560         label = gtk_label_new(_("Address Book"));
561         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
562                 GTK_FILL, 0, 0, 0);
563         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
564
565         name_entry = gtk_entry_new();
566         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1),
567                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
568
569         /* Second row */
570         top = 1;
571         label = gtk_label_new(_("File Name"));
572         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
573                 GTK_FILL, 0, 0, 0);
574         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
575
576         file_entry = gtk_entry_new();
577         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1),
578                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
579
580         file_btn = gtk_button_new_with_label( _(" ... "));
581         gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1),
582                 GTK_FILL, 0, 3, 0);
583
584         gtk_widget_show_all(vbox);
585
586         /* Button handler */
587         gtk_signal_connect(GTK_OBJECT(file_btn), "clicked",
588                            GTK_SIGNAL_FUNC(imp_ldif_file_select), NULL);
589
590         impldif_dlg.file_entry = file_entry;
591         impldif_dlg.name_entry = name_entry;
592 }
593
594 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
595         GtkWidget *vbox;
596         GtkWidget *vboxt;
597         GtkWidget *vboxb;
598         GtkWidget *buttonMod;
599
600         GtkWidget *table;
601         GtkWidget *label;
602         GtkWidget *clist_swin;
603         GtkWidget *clist_field;
604         GtkWidget *name_ldif;
605         GtkWidget *name_attrib;
606         GtkWidget *check_select;
607         gint top;
608
609         gchar *titles[ FIELDS_N_COLS ];
610         gint i;
611
612         titles[ FIELD_COL_SELECT ] = _("S");
613         titles[ FIELD_COL_FIELD  ] = _("LDIF Field");
614         titles[ FIELD_COL_ATTRIB ] = _("Attribute Name");
615
616         vbox = gtk_vbox_new(FALSE, 8);
617         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
618         gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
619
620         label = gtk_label_new( pageLbl );
621         gtk_widget_show( label );
622         gtk_notebook_set_tab_label(
623                 GTK_NOTEBOOK( impldif_dlg.notebook ),
624                 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
625                 label );
626
627         /* Upper area - Field list */
628         vboxt = gtk_vbox_new( FALSE, 4 );
629         gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
630
631         clist_swin = gtk_scrolled_window_new( NULL, NULL );
632         gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
633         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
634                                        GTK_POLICY_AUTOMATIC,
635                                        GTK_POLICY_ALWAYS);
636
637         clist_field = gtk_clist_new_with_titles( FIELDS_N_COLS, titles );
638         gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
639         gtk_clist_set_selection_mode( GTK_CLIST(clist_field), GTK_SELECTION_BROWSE );
640         gtk_clist_set_column_width(
641                         GTK_CLIST(clist_field), FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
642         gtk_clist_set_column_width(
643                         GTK_CLIST(clist_field), FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
644         gtk_clist_set_column_width(
645                         GTK_CLIST(clist_field), FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
646
647         for( i = 0; i < FIELDS_N_COLS; i++ )
648                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_field)->column[i].button, GTK_CAN_FOCUS);
649
650         /* Lower area - Edit area */
651         vboxb = gtk_vbox_new( FALSE, 4 );
652         gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
653
654         /* Data entry area */
655         table = gtk_table_new( 3, 3, FALSE);
656         gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
657         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
658         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
659
660         /* First row */
661         top = 0;
662         label = gtk_label_new(_("LDIF Field"));
663         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
664                 GTK_FILL, 0, 0, 0);
665         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
666
667         name_ldif = gtk_label_new( "" );
668         gtk_misc_set_alignment(GTK_MISC(name_ldif), 0.01, 0.5);
669         gtk_table_attach(GTK_TABLE(table), name_ldif, 1, 3, top, (top + 1),
670                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
671
672         /* Second row */
673         ++top;
674         label = gtk_label_new(_("Attribute"));
675         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
676                 GTK_FILL, 0, 0, 0);
677         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
678
679         name_attrib = gtk_entry_new();
680         gtk_table_attach(GTK_TABLE(table), name_attrib, 1, 3, top, (top + 1),
681                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
682
683         /* Next row */
684         ++top;
685         label = gtk_label_new(_("Select"));
686         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
687                 GTK_FILL, 0, 0, 0);
688         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
689
690         check_select = gtk_check_button_new();
691         gtk_table_attach(GTK_TABLE(table), check_select, 1, 2, top, (top + 1),
692                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
693
694         buttonMod = gtk_button_new_with_label( _("Modify"));
695         gtk_table_attach(GTK_TABLE(table), buttonMod, 2, 3, top, (top + 1),
696                 GTK_FILL, 0, 3, 0);
697
698         gtk_widget_show_all(vbox);
699
700         /* Event handlers */
701         gtk_signal_connect( GTK_OBJECT(clist_field), "select_row",
702                         GTK_SIGNAL_FUNC(imp_ldif_field_list_selected), NULL );
703         gtk_signal_connect( GTK_OBJECT(clist_field), "button_press_event",
704                         GTK_SIGNAL_FUNC(imp_ldif_field_list_toggle), NULL );
705         gtk_signal_connect( GTK_OBJECT(buttonMod), "clicked",
706                         GTK_SIGNAL_FUNC(imp_ldif_modify_pressed), NULL );
707
708         impldif_dlg.clist_field  = clist_field;
709         impldif_dlg.name_ldif    = name_ldif;
710         impldif_dlg.name_attrib  = name_attrib;
711         impldif_dlg.check_select = check_select;
712 }
713
714 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
715         GtkWidget *vbox;
716         GtkWidget *table;
717         GtkWidget *label;
718         GtkWidget *labelBook;
719         GtkWidget *labelFile;
720         GtkWidget *labelRecs;
721         gint top;
722
723         vbox = gtk_vbox_new(FALSE, 8);
724         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
725         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
726
727         label = gtk_label_new( pageLbl );
728         gtk_widget_show( label );
729         gtk_notebook_set_tab_label(
730                 GTK_NOTEBOOK( impldif_dlg.notebook ),
731                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
732                 label );
733
734         table = gtk_table_new(3, 2, FALSE);
735         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
736         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
737         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
738         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
739
740         /* First row */
741         top = 0;
742         label = gtk_label_new(_("Address Book :"));
743         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
744         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
745
746         labelBook = gtk_label_new("");
747         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
748         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
749
750         /* Second row */
751         top++;
752         label = gtk_label_new(_("File Name :"));
753         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
754         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
755
756         labelFile = gtk_label_new("");
757         gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
758         gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
759
760         /* Third row */
761         top++;
762         label = gtk_label_new(_("Records :"));
763         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
764         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
765
766         labelRecs = gtk_label_new("");
767         gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
768         gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
769
770         impldif_dlg.labelBook    = labelBook;
771         impldif_dlg.labelFile    = labelFile;
772         impldif_dlg.labelRecords = labelRecs;
773 }
774
775 static void imp_ldif_dialog_create() {
776         GtkWidget *window;
777         GtkWidget *vbox;
778         GtkWidget *vnbox;
779         GtkWidget *notebook;
780         GtkWidget *hbbox;
781         GtkWidget *btnPrev;
782         GtkWidget *btnNext;
783         GtkWidget *btnCancel;
784         GtkWidget *hsbox;
785         GtkWidget *statusbar;
786
787         window = gtk_window_new(GTK_WINDOW_DIALOG);
788         gtk_widget_set_usize(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
789         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
790         gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
791         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
792         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
793         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
794                            GTK_SIGNAL_FUNC(imp_ldif_delete_event),
795                            NULL );
796         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
797                            GTK_SIGNAL_FUNC(imp_ldif_key_pressed),
798                            NULL );
799
800         vbox = gtk_vbox_new(FALSE, 4);
801         gtk_widget_show(vbox);
802         gtk_container_add(GTK_CONTAINER(window), vbox);
803
804         vnbox = gtk_vbox_new(FALSE, 4);
805         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
806         gtk_widget_show(vnbox);
807         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
808
809         /* Notebook */
810         notebook = gtk_notebook_new();
811         gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
812         gtk_widget_show(notebook);
813         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
814         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
815
816         /* Status line */
817         hsbox = gtk_hbox_new(FALSE, 0);
818         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
819         statusbar = gtk_statusbar_new();
820         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
821
822         /* Button panel */
823         gtkut_button_set_create(&hbbox, &btnPrev, _( "Prev" ),
824                                 &btnNext, _( "Next" ),
825                                 &btnCancel, _( "Cancel" ) );
826         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
827         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
828         gtk_widget_grab_default(btnNext);
829
830         /* Button handlers */
831         gtk_signal_connect(GTK_OBJECT(btnPrev), "clicked",
832                            GTK_SIGNAL_FUNC(imp_ldif_prev), NULL);
833         gtk_signal_connect(GTK_OBJECT(btnNext), "clicked",
834                            GTK_SIGNAL_FUNC(imp_ldif_next), NULL);
835         gtk_signal_connect(GTK_OBJECT(btnCancel), "clicked",
836                            GTK_SIGNAL_FUNC(imp_ldif_cancel), NULL);
837
838         gtk_widget_show_all(vbox);
839
840         impldif_dlg.window     = window;
841         impldif_dlg.notebook   = notebook;
842         impldif_dlg.btnPrev    = btnPrev;
843         impldif_dlg.btnNext    = btnNext;
844         impldif_dlg.btnCancel  = btnCancel;
845         impldif_dlg.statusbar  = statusbar;
846         impldif_dlg.status_cid = gtk_statusbar_get_context_id(
847                         GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
848
849 }
850
851 static void imp_ldif_create() {
852         imp_ldif_dialog_create();
853         imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
854         imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
855         imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
856         gtk_widget_show_all( impldif_dlg.window );
857 }
858
859 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
860         _importedBook_ = NULL;
861         _imp_addressIndex_ = addrIndex;
862
863         if( ! impldif_dlg.window )
864                 imp_ldif_create();
865         impldif_dlg.cancelled = FALSE;
866         gtk_widget_show(impldif_dlg.window);
867         manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
868         gtk_widget_grab_default(impldif_dlg.btnNext);
869
870         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_entry), IMPORTLDIF_GUESS_NAME );
871         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), "" );
872         gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), "" );
873         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
874         gtk_clist_clear( GTK_CLIST(impldif_dlg.clist_field) );
875         gtk_notebook_set_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
876         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
877         gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
878         stock_pixmap_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
879                           &markxpm, &markxpmmask );
880         imp_ldif_message();
881         gtk_widget_grab_focus(impldif_dlg.file_entry);
882
883         impldif_dlg.rowIndSelect = -1;
884         impldif_dlg.rowCount = 0;
885         g_free( impldif_dlg.nameBook );
886         g_free( impldif_dlg.fileName );
887         impldif_dlg.nameBook = NULL;
888         impldif_dlg.fileName = NULL;
889
890         _ldifFile_ = ldif_create();
891         gtk_main();
892         gtk_widget_hide(impldif_dlg.window);
893         ldif_free( _ldifFile_ );
894         _ldifFile_ = NULL;
895         _imp_addressIndex_ = NULL;
896
897         g_free( impldif_dlg.nameBook );
898         g_free( impldif_dlg.fileName );
899         impldif_dlg.nameBook = NULL;
900         impldif_dlg.fileName = NULL;
901
902         if( impldif_dlg.cancelled == TRUE ) return NULL;
903         return _importedBook_;
904 }
905
906 /*
907 * End of Source.
908 */
909