2007-07-27 [colin] 2.10.0cvs72
[claws.git] / src / importldif.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2007 Match Grun and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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 <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtkwindow.h>
34 #include <gtk/gtksignal.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtktable.h>
38 #include <gtk/gtkbutton.h>
39
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 #include "filesel.h"
51
52 #define IMPORTLDIF_GUESS_NAME      "LDIF Import"
53
54 #define PAGE_FILE_INFO             0
55 #define PAGE_ATTRIBUTES            1
56 #define PAGE_FINISH                2
57
58 #define IMPORTLDIF_WIDTH           390
59 #define IMPORTLDIF_HEIGHT          300
60
61 #define FIELDS_N_COLS              4
62 #define FIELDS_COL_WIDTH_RESERVED  10
63 #define FIELDS_COL_WIDTH_SELECT    10
64 #define FIELDS_COL_WIDTH_FIELD     140
65 #define FIELDS_COL_WIDTH_ATTRIB    140
66
67 typedef enum {
68         FIELD_COL_RESERVED = 0,
69         FIELD_COL_SELECT   = 1,
70         FIELD_COL_FIELD    = 2,
71         FIELD_COL_ATTRIB   = 3
72 } ImpLdif_FieldColPos;
73
74 /**
75  * LDIF dialog definition.
76  */
77 static struct _ImpLdif_Dlg {
78         GtkWidget *window;
79         GtkWidget *notebook;
80         GtkWidget *entryFile;
81         GtkWidget *entryName;
82         GtkWidget *clist_field;
83         GtkWidget *entryField;
84         GtkWidget *entryAttrib;
85         GtkWidget *checkSelect;
86         GtkWidget *btnModify;
87         GtkWidget *labelBook;
88         GtkWidget *labelFile;
89         GtkWidget *labelRecords;
90         GtkWidget *btnPrev;
91         GtkWidget *btnNext;
92         GtkWidget *btnProceed;
93         GtkWidget *btnCancel;
94         GtkWidget *statusbar;
95         gint      status_cid;
96         gint      rowIndSelect;
97         gint      rowCount;
98         gchar     *nameBook;
99         gchar     *fileName;
100         gboolean  cancelled;
101 } impldif_dlg;
102
103 static struct _AddressFileSelection _imp_ldif_file_selector_;
104 static AddressBookFile *_importedBook_;
105 static AddressIndex *_imp_addressIndex_;
106 static LdifFile *_ldifFile_ = NULL;
107
108 static GdkPixmap *markxpm;
109 static GdkBitmap *markxpmmask;
110
111 /**
112  * Structure of error message table.
113  */
114 typedef struct _ErrMsgTableEntry ErrMsgTableEntry;
115 struct _ErrMsgTableEntry {
116         gint    code;
117         gchar   *description;
118 };
119
120 static gchar *_errMsgUnknown_ = N_( "Unknown" );
121
122 /**
123  * Lookup table of error messages for general errors. Note that a NULL
124  * description signifies the end of the table.
125  */
126 static ErrMsgTableEntry _lutErrorsLDIF_[] = {
127         { MGU_SUCCESS,          N_("Success") },
128         { MGU_BAD_ARGS,         N_("Bad arguments") },
129         { MGU_NO_FILE,          N_("File not specified") },
130         { MGU_OPEN_FILE,        N_("Error opening file") },
131         { MGU_ERROR_READ,       N_("Error reading file") },
132         { MGU_EOF,              N_("End of file encountered") },
133         { MGU_OO_MEMORY,        N_("Error allocating memory") },
134         { MGU_BAD_FORMAT,       N_("Bad file format") },
135         { MGU_ERROR_WRITE,      N_("Error writing to file") },
136         { MGU_OPEN_DIRECTORY,   N_("Error opening directory") },
137         { MGU_NO_PATH,          N_("No path specified") },
138         { 0,                    NULL }
139 };
140
141 /**
142  * Lookup message for specified error code.
143  * \param lut  Lookup table.
144  * \param code Code to lookup.
145  * \return Description associated to code.
146  */
147 static gchar *imp_ldif_err2string( ErrMsgTableEntry lut[], gint code ) {
148         gchar *desc = NULL;
149         ErrMsgTableEntry entry;
150         gint i;
151
152         for( i = 0; ; i++ ) {
153                 entry = lut[ i ];
154                 if( entry.description == NULL ) break;
155                 if( entry.code == code ) {
156                         desc = entry.description;
157                         break;
158                 }
159         }
160         if( ! desc ) {
161                 desc = _errMsgUnknown_;
162         }
163         return desc;
164 }
165
166 /**
167  * Display message in status field.
168  * \param msg Message to display.
169  */
170 static void imp_ldif_status_show( gchar *msg ) {
171         if( impldif_dlg.statusbar != NULL ) {
172                 gtk_statusbar_pop( GTK_STATUSBAR(impldif_dlg.statusbar),
173                         impldif_dlg.status_cid );
174                 if( msg ) {
175                         gtk_statusbar_push(
176                                 GTK_STATUSBAR(impldif_dlg.statusbar),
177                                 impldif_dlg.status_cid, msg );
178                 }
179         }
180 }
181
182 /**
183  * Select and display status message appropriate for the page being displayed.
184  */
185 static void imp_ldif_message( void ) {
186         gchar *sMsg = NULL;
187         gint pageNum;
188
189         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
190         if( pageNum == PAGE_FILE_INFO ) {
191                 sMsg = _( "Please specify address book name and file to import." );
192         }
193         else if( pageNum == PAGE_ATTRIBUTES ) {
194                 sMsg = _( "Select and rename LDIF field names to import." );
195         }
196         else if( pageNum == PAGE_FINISH ) {
197                 sMsg = _( "File imported." );
198         }
199         imp_ldif_status_show( sMsg );
200 }
201
202 /**
203  * Update list with data for current row.
204  * \param clist List to update.
205  */
206 static void imp_ldif_update_row( GtkCList *clist ) {
207         Ldif_FieldRec *rec;
208         gchar *text[ FIELDS_N_COLS ];
209         gint row;
210
211         if( impldif_dlg.rowIndSelect < 0 ) return;
212         row = impldif_dlg.rowIndSelect;
213
214         rec = gtk_clist_get_row_data( clist, row );
215         text[ FIELD_COL_RESERVED ] = "";
216         text[ FIELD_COL_SELECT   ] = "";
217         text[ FIELD_COL_FIELD    ] = rec->tagName;
218         text[ FIELD_COL_ATTRIB   ] = rec->userName;
219
220         gtk_clist_freeze( clist );
221         gtk_clist_remove( clist, row );
222         if( row == impldif_dlg.rowCount - 1 ) {
223                 gtk_clist_append( clist, text );
224         }
225         else {
226                 gtk_clist_insert( clist, row, text );
227         }
228         if( rec->selected ) {
229                 gtk_clist_set_pixmap(
230                         clist, row, FIELD_COL_SELECT, markxpm, markxpmmask );
231         }
232         if( rec->reserved ) {
233                 gtk_clist_set_pixmap(
234                         clist, row, FIELD_COL_RESERVED, markxpm, markxpmmask );
235         }
236
237         gtk_clist_set_row_data( clist, row, rec );
238         gtk_clist_thaw( clist );
239 }
240
241 /**
242  * Load list with LDIF fields read from file.
243  * \param ldf LDIF control data.
244  */
245 static void imp_ldif_load_fields( LdifFile *ldf ) {
246         GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field);
247         GList *node, *list;
248         gchar *text[ FIELDS_N_COLS ];
249
250         impldif_dlg.rowIndSelect = -1;
251         impldif_dlg.rowCount = 0;
252         if( ! ldf->accessFlag ) return;
253         gtk_clist_clear( clist );
254         list = ldif_get_fieldlist( ldf );
255         node = list;
256         while( node ) {
257                 Ldif_FieldRec *rec = node->data;
258                 gint row;
259
260                 text[ FIELD_COL_RESERVED ] = "";
261                 text[ FIELD_COL_SELECT   ] = "";
262                 text[ FIELD_COL_FIELD    ] = rec->tagName;
263                 text[ FIELD_COL_ATTRIB   ] = rec->userName;
264                 row = gtk_clist_append( clist, text );
265                 gtk_clist_set_row_data( clist, row, rec );
266                 if( rec->selected ) {
267                         gtk_clist_set_pixmap( clist, row,
268                                 FIELD_COL_SELECT, markxpm, markxpmmask );
269                 }
270                 if( rec->reserved ) {
271                         gtk_clist_set_pixmap( clist, row,
272                                 FIELD_COL_RESERVED, markxpm, markxpmmask );
273                 }
274                 impldif_dlg.rowCount++;
275                 node = g_list_next( node );
276         }
277         g_list_free( list );
278         list = NULL;
279         ldif_set_accessed( ldf, FALSE );
280 }
281
282 /**
283  * Callback function when list iterm selected.
284  * \param clist List widget.
285  * \param row   Row.
286  * \param col   Column.
287  * \param event Event object.
288  * \param data  User data.
289  */
290 static void imp_ldif_field_list_selected(
291                 GtkCList *clist, gint row, gint column, GdkEvent *event,
292                 gpointer data )
293 {
294         Ldif_FieldRec *rec = gtk_clist_get_row_data( clist, row );
295
296         impldif_dlg.rowIndSelect = row;
297         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
298         if( rec ) {
299                 /* Update widget contents */
300                 gtk_label_set_text(
301                         GTK_LABEL(impldif_dlg.entryField), rec->tagName );
302                 if( rec->userName )
303                         gtk_entry_set_text(
304                                 GTK_ENTRY(impldif_dlg.entryAttrib), rec->userName );
305                 gtk_toggle_button_set_active(
306                         GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect),
307                         rec->selected );
308
309                 /* Disable widgets for reserved fields */
310                 gtk_widget_set_sensitive(
311                         impldif_dlg.entryAttrib, ! rec->reserved );
312                 gtk_widget_set_sensitive(
313                         impldif_dlg.checkSelect, ! rec->reserved );
314                 gtk_widget_set_sensitive(
315                         impldif_dlg.btnModify,   ! rec->reserved );
316         }
317         gtk_widget_grab_focus(impldif_dlg.entryAttrib);
318 }
319
320 /**
321  * Callback function to toggle selected LDIF field.
322  * \param clist List to update.
323  * \param event Event object.
324  * \param data  Data.
325  */
326 static gboolean imp_ldif_field_list_toggle(
327                 GtkCList *clist, GdkEventButton *event, gpointer data )
328 {
329         Ldif_FieldRec *rec;
330         gboolean toggle = FALSE;
331
332         if( ! event ) return FALSE;
333         if( impldif_dlg.rowIndSelect < 0 ) return FALSE;
334         if( event->button == 1 ) {
335                 /* If single click in select column */
336                 if( event->type == GDK_BUTTON_PRESS ) {
337                         gint x = event->x;
338                         gint y = event->y;
339                         gint row, col;
340
341                         gtk_clist_get_selection_info( clist, x, y, &row, &col );
342                         if( col != FIELD_COL_SELECT ) return FALSE;
343                         if( row > impldif_dlg.rowCount ) return FALSE;
344
345                         /* Set row */
346                         impldif_dlg.rowIndSelect = row;
347                         toggle = TRUE;
348                 }
349
350                 /* If double click anywhere in row */
351                 else if( event->type == GDK_2BUTTON_PRESS ) {
352                         toggle = TRUE;
353                 }
354         }
355         /* Toggle field selection */
356         if( toggle ) {
357                 rec = gtk_clist_get_row_data(
358                         clist, impldif_dlg.rowIndSelect );
359                 if( rec ) {
360                         ldif_field_toggle( rec );
361                         imp_ldif_update_row( clist );
362                 }
363         }
364         return FALSE;
365 }
366
367 /**
368  * Callback function to update LDIF field data from input fields.
369  * \param widget Widget (button).
370  * \param data   User data.
371  */
372 static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
373         GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field);
374         Ldif_FieldRec *rec;
375         gint row;
376
377         if( impldif_dlg.rowIndSelect < 0 ) return;
378         row = impldif_dlg.rowIndSelect;
379         rec = gtk_clist_get_row_data( clist, impldif_dlg.rowIndSelect );
380
381         ldif_field_set_name( rec, gtk_editable_get_chars(
382                 GTK_EDITABLE(impldif_dlg.entryAttrib), 0, -1 ) );
383         ldif_field_set_selected( rec, gtk_toggle_button_get_active(
384                 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect) ) );
385         imp_ldif_update_row( clist );
386         gtk_clist_select_row( clist, row, 0 );
387         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
388         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
389         gtk_toggle_button_set_active(
390                 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect), FALSE );
391 }
392
393 /**
394  * Test whether we can move off fields page.
395  * \return <i>TRUE</i> if OK to move off page.
396  */
397 static gboolean imp_ldif_field_move() {
398         gboolean retVal = FALSE;
399         gchar *newFile;
400         AddressBookFile *abf = NULL;
401
402         if( _importedBook_ ) {
403                 addrbook_free_book( _importedBook_ );
404         }
405
406         abf = addrbook_create_book();
407         addrbook_set_path( abf, _imp_addressIndex_->filePath );
408         addrbook_set_name( abf, impldif_dlg.nameBook );
409         newFile = addrbook_guess_next_file( abf );
410         addrbook_set_file( abf, newFile );
411         g_free( newFile );
412
413         /* Import data into file */
414         if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
415                 addrbook_save_data( abf );
416                 _importedBook_ = abf;
417                 retVal = TRUE;
418         }
419         else {
420                 addrbook_free_book( abf );
421         }
422
423         return retVal;
424 }
425
426 /**
427  * Test whether we can move off file page.
428  * \return <i>TRUE</i> if OK to move off page.
429  */
430 static gboolean imp_ldif_file_move() {
431         gboolean retVal = FALSE;
432         gchar *sName;
433         gchar *sFile;
434         gchar *sMsg = NULL;
435         gboolean errFlag = FALSE;
436
437         sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryFile), 0, -1 );
438         g_strchug( sFile ); g_strchomp( sFile );
439
440         sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
441         g_strchug( sName ); g_strchomp( sName );
442
443         g_free( impldif_dlg.nameBook );
444         g_free( impldif_dlg.fileName );
445         impldif_dlg.nameBook = sName;
446         impldif_dlg.fileName = sFile;
447
448         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), sFile );
449         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), sName );
450
451         if( *sFile == '\0' ) {
452                 sMsg = _( "Please select a file." );
453                 gtk_widget_grab_focus(impldif_dlg.entryFile);
454                 errFlag = TRUE;
455         }
456
457         if( ! errFlag && *sName == '\0' ) {
458                 sMsg = _( "Address book name must be supplied." );
459                 gtk_widget_grab_focus(impldif_dlg.entryName);
460                 errFlag = TRUE;
461         }
462
463         if( ! errFlag ) {
464                 /* Read attribute list */
465                 ldif_set_file( _ldifFile_, sFile );
466                 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
467                         /* Load fields */
468                         /* ldif_print_file( _ldifFile_, stdout ); */
469                         imp_ldif_load_fields( _ldifFile_ );
470                         retVal = TRUE;
471                 }
472                 else {
473                         sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
474                 }
475         }
476         imp_ldif_status_show( sMsg );
477
478         return retVal;
479 }
480
481 /**
482  * Display finish page.
483  */
484 static void imp_ldif_finish_show() {
485         gchar *sMsg;
486         gchar *name;
487
488         name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
489         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
490         g_free( name );
491         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
492         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
493         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
494         gtk_widget_hide( impldif_dlg.btnNext );
495         gtk_widget_show( impldif_dlg.btnProceed );
496         gtk_widget_set_sensitive( impldif_dlg.btnProceed, FALSE );
497         if( _ldifFile_->retVal == MGU_SUCCESS ) {
498                 sMsg = _( "LDIF file imported successfully." );
499         }
500         else {
501                 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
502         }
503         imp_ldif_status_show( sMsg );
504         gtk_widget_grab_focus(impldif_dlg.btnCancel);
505 }
506
507 /**
508  * Callback function to select previous page.
509  * \param widget Widget (button).
510  */
511 static void imp_ldif_prev( GtkWidget *widget ) {
512         gint pageNum;
513
514         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
515         if( pageNum == PAGE_ATTRIBUTES ) {
516                 /* Goto file page stuff */
517                 gtk_notebook_set_current_page(
518                         GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
519                 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
520                 gtk_widget_hide( impldif_dlg.btnProceed );
521                 gtk_widget_show( impldif_dlg.btnNext );
522         }
523         imp_ldif_message();
524 }
525
526 /**
527  * Callback function to select next page.
528  * \param widget Widget (button).
529  */
530 static void imp_ldif_next( GtkWidget *widget ) {
531         gint pageNum;
532
533         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
534         if( pageNum == PAGE_FILE_INFO ) {
535                 /* Goto attributes stuff */
536                 if( imp_ldif_file_move() ) {
537                         gtk_notebook_set_current_page(
538                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
539                         imp_ldif_message();
540                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
541                         gtk_widget_hide( impldif_dlg.btnNext );
542                         gtk_widget_show( impldif_dlg.btnProceed );
543                         gtk_widget_set_sensitive( impldif_dlg.btnProceed, TRUE );
544                 }
545                 else {
546                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
547                         _ldifFile_->dirtyFlag = TRUE;
548                 }
549         }
550         else if( pageNum == PAGE_ATTRIBUTES ) {
551                 /* Goto finish stuff */
552                 if( imp_ldif_field_move() ) {
553                         gtk_notebook_set_current_page(
554                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
555                         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
556                                              GTK_STOCK_CLOSE);
557                         imp_ldif_finish_show();
558                 }
559         }
560 }
561
562 /**
563  * Callback function to cancel and close dialog.
564  * \param widget Widget (button).
565  * \param data   User data.
566  */
567 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
568         gint pageNum;
569
570         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
571         if( pageNum != PAGE_FINISH ) {
572                 impldif_dlg.cancelled = TRUE;
573         }
574         gtk_main_quit();
575 }
576
577
578 /**
579  * Create LDIF file selection dialog.
580  * \param afs Address file selection data.
581  */
582 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
583         gchar *file = filesel_select_file_open(_("Select LDIF File"), NULL);
584         
585         if (file == NULL)
586                 afs->cancelled = TRUE;
587         else {
588                 afs->cancelled = FALSE;
589                 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), file );
590                 g_free(file);
591         }
592 }
593
594 /**
595  * Callback function to display LDIF file selection dialog.
596  */
597 static void imp_ldif_file_select( void ) {
598         imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
599 }
600
601 /**
602  * Callback function to handle dialog close event.
603  * \param widget Widget (dialog).
604  * \param event  Event object.
605  * \param data   User data.
606  */
607 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
608         imp_ldif_cancel( widget, data );
609         return TRUE;
610 }
611
612 /**
613  * Callback function to respond to dialog key press events.
614  * \param widget Widget.
615  * \param event  Event object.
616  * \param data   User data.
617  */
618 static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
619         if (event && event->keyval == GDK_Escape) {
620                 imp_ldif_cancel( widget, data );
621         }
622         return FALSE;
623 }
624
625 /**
626  * Format notebook "file" page.
627  * \param pageNum Page (tab) number.
628  * \param pageLbl Page (tab) label.
629  */
630 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
631         GtkWidget *vbox;
632         GtkWidget *table;
633         GtkWidget *label;
634         GtkWidget *entryFile;
635         GtkWidget *entryName;
636         GtkWidget *btnFile;
637         GtkTooltips *toolTip;
638         gint top;
639
640         vbox = gtk_vbox_new(FALSE, 8);
641         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
642         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
643
644         label = gtk_label_new( pageLbl );
645         gtk_widget_show( label );
646         gtk_notebook_set_tab_label(
647                 GTK_NOTEBOOK( impldif_dlg.notebook ),
648                 gtk_notebook_get_nth_page(
649                         GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
650                 label );
651
652         table = gtk_table_new(2, 3, FALSE);
653         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
654         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
655         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
656         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
657
658         /* First row */
659         top = 0;
660         label = gtk_label_new(_("Address Book"));
661         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
662                 GTK_FILL, 0, 0, 0);
663         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
664
665         entryName = gtk_entry_new();
666         gtk_table_attach(GTK_TABLE(table), entryName, 1, 2, top, (top + 1),
667                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
668
669         toolTip = gtk_tooltips_new();
670         gtk_tooltips_set_tip( toolTip, entryName, _( 
671                 "Specify the name for the address book that will " \
672                 "be created from the LDIF file data." ),
673                 NULL );
674
675         /* Second row */
676         top = 1;
677         label = gtk_label_new(_("File Name"));
678         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
679                 GTK_FILL, 0, 0, 0);
680         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
681
682         entryFile = gtk_entry_new();
683         gtk_table_attach(GTK_TABLE(table), entryFile, 1, 2, top, (top + 1),
684                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
685
686         toolTip = gtk_tooltips_new();
687         gtk_tooltips_set_tip( toolTip, entryFile,
688                 _( "The full file specification of the LDIF file to import." ),
689                 NULL );
690
691         btnFile = gtkut_get_browse_file_btn(_("B_rowse"));
692         gtk_table_attach(GTK_TABLE(table), btnFile, 2, 3, top, (top + 1),
693                 GTK_FILL, 0, 3, 0);
694
695         toolTip = gtk_tooltips_new();
696         gtk_tooltips_set_tip( toolTip, btnFile,
697                 _( "Select the LDIF file to import." ),
698                 NULL );
699
700         gtk_widget_show_all(vbox);
701
702         /* Button handler */
703         g_signal_connect(G_OBJECT(btnFile), "clicked",
704                          G_CALLBACK(imp_ldif_file_select), NULL);
705
706         impldif_dlg.entryFile = entryFile;
707         impldif_dlg.entryName = entryName;
708 }
709
710 /**
711  * Format notebook fields page.
712  * \param pageNum Page (tab) number.
713  * \param pageLbl Page (tab) label.
714  */
715 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
716         GtkWidget *vbox;
717         GtkWidget *vboxt;
718         GtkWidget *vboxb;
719         GtkWidget *table;
720         GtkWidget *label;
721         GtkWidget *clist_swin;
722         GtkWidget *clist_field;
723         GtkWidget *entryField;
724         GtkWidget *entryAttrib;
725         GtkWidget *checkSelect;
726         GtkWidget *btnModify;
727         GtkWidget *eventBox;
728         GtkTooltips *toolTip;
729         gint top;
730
731         gchar *titles[ FIELDS_N_COLS ];
732         gint i;
733
734         titles[ FIELD_COL_RESERVED ] = _("R");
735         titles[ FIELD_COL_SELECT   ] = _("S");
736         titles[ FIELD_COL_FIELD    ] = _("LDIF Field Name");
737         titles[ FIELD_COL_ATTRIB   ] = _("Attribute Name");
738
739         vbox = gtk_vbox_new(FALSE, 8);
740         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
741         gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
742
743         label = gtk_label_new( pageLbl );
744         gtk_widget_show( label );
745         gtk_notebook_set_tab_label(
746                 GTK_NOTEBOOK( impldif_dlg.notebook ),
747                 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
748                 label );
749
750         /* Upper area - Field list */
751         vboxt = gtk_vbox_new( FALSE, 4 );
752         gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
753
754         clist_swin = gtk_scrolled_window_new( NULL, NULL );
755         gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
756         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
757                                        GTK_POLICY_AUTOMATIC,
758                                        GTK_POLICY_AUTOMATIC);
759
760         clist_field = gtk_clist_new_with_titles( FIELDS_N_COLS, titles );
761         gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
762         gtk_clist_set_selection_mode(
763                 GTK_CLIST(clist_field), GTK_SELECTION_BROWSE );
764         gtk_clist_set_column_width( GTK_CLIST(clist_field),
765                 FIELD_COL_RESERVED, FIELDS_COL_WIDTH_RESERVED );
766         gtk_clist_set_column_width( GTK_CLIST(clist_field),
767                 FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
768         gtk_clist_set_column_width( GTK_CLIST(clist_field),
769                 FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
770         gtk_clist_set_column_width( GTK_CLIST(clist_field),
771                 FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
772
773         /* Remove focus capability for column headers */
774         for( i = 0; i < FIELDS_N_COLS; i++ ) {
775                 GTK_WIDGET_UNSET_FLAGS(
776                         GTK_CLIST(clist_field)->column[i].button,
777                         GTK_CAN_FOCUS);
778         }
779
780         /* Lower area - Edit area */
781         vboxb = gtk_vbox_new( FALSE, 4 );
782         gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
783
784         /* Data entry area */
785         table = gtk_table_new( 3, 3, FALSE);
786         gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
787         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
788         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
789
790         /* First row */
791         top = 0;
792         label = gtk_label_new(_("LDIF Field"));
793         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
794                 GTK_FILL, 0, 0, 0);
795         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
796
797         entryField = gtk_label_new( "" );
798         gtk_misc_set_alignment(GTK_MISC(entryField), 0.01, 0.5);
799         gtk_table_attach(GTK_TABLE(table), entryField, 1, 3, top, (top + 1),
800                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
801
802         /* Second row */
803         ++top;
804         label = gtk_label_new(_("Attribute"));
805         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
806                 GTK_FILL, 0, 0, 0);
807         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
808
809         entryAttrib = gtk_entry_new();
810         gtk_table_attach(GTK_TABLE(table), entryAttrib, 1, 3, top, (top + 1),
811                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
812
813         toolTip = gtk_tooltips_new();
814         gtk_tooltips_set_tip( toolTip, entryAttrib,
815                 _( "The LDIF field can be renamed to the User Attribute name." ),
816                 NULL );
817
818         /* Next row */
819         ++top;
820
821         /*
822          * Use an event box to attach some help in the form of a tooltip.
823          * Tried this for the clist but it looked bad.
824          */
825         eventBox = gtk_event_box_new();
826         gtk_container_add( GTK_CONTAINER(eventBox), label );
827         gtk_table_attach(GTK_TABLE(table), eventBox, 0, 1, top, (top + 1),
828                 GTK_FILL, 0, 0, 0);
829
830         toolTip = gtk_tooltips_new();
831         gtk_tooltips_set_tip( toolTip, eventBox, _(
832                 "Choose the LDIF field that will be renamed or selected " \
833                 "for import in the list above. Reserved fields (marked " \
834                 "with a tick in the \"R\" column), are automatically " \
835                 "imported and cannot be renamed. A single click in the " \
836                 "Select (\"S\") column will select the field for import " \
837                 "with a tick. A single click anywhere in the row will " \
838                 "select that field for rename in the input area below " \
839                 "the list. A double click anywhere in the row will also " \
840                 "select the field for import."
841                 ), NULL );
842
843
844         checkSelect = gtk_check_button_new_with_label( _( "Select for Import" ) );
845         gtk_table_attach(GTK_TABLE(table), checkSelect, 1, 2, top, (top + 1),
846                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
847
848         toolTip = gtk_tooltips_new();
849         gtk_tooltips_set_tip( toolTip, checkSelect,
850                 _( "Select the LDIF field for import into the address book." ),
851                 NULL );
852
853         btnModify = gtk_button_new_with_label( _(" Modify "));
854         gtk_table_attach(GTK_TABLE(table), btnModify, 2, 3, top, (top + 1),
855                 GTK_FILL, 0, 3, 0);
856
857         toolTip = gtk_tooltips_new();
858         gtk_tooltips_set_tip( toolTip, btnModify,
859                 _( "This button will update the list above with the data supplied." ),
860                 NULL );
861
862         gtk_widget_show_all(vbox);
863
864         /* Event handlers */
865         g_signal_connect( G_OBJECT(clist_field), "select_row",
866                           G_CALLBACK(imp_ldif_field_list_selected), NULL );
867         g_signal_connect( G_OBJECT(clist_field), "button_press_event",
868                           G_CALLBACK(imp_ldif_field_list_toggle), NULL );
869         g_signal_connect( G_OBJECT(btnModify), "clicked",
870                           G_CALLBACK(imp_ldif_modify_pressed), NULL );
871
872         impldif_dlg.clist_field = clist_field;
873         impldif_dlg.entryField  = entryField;
874         impldif_dlg.entryAttrib = entryAttrib;
875         impldif_dlg.checkSelect = checkSelect;
876         impldif_dlg.btnModify   = btnModify;
877 }
878
879 /**
880  * Format notebook finish page.
881  * \param pageNum Page (tab) number.
882  * \param pageLbl Page (tab) label.
883  */
884 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
885         GtkWidget *vbox;
886         GtkWidget *table;
887         GtkWidget *label;
888         GtkWidget *labelBook;
889         GtkWidget *labelFile;
890         GtkWidget *labelRecs;
891         gint top;
892
893         vbox = gtk_vbox_new(FALSE, 8);
894         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
895         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
896
897         label = gtk_label_new( pageLbl );
898         gtk_widget_show( label );
899         gtk_notebook_set_tab_label(
900                 GTK_NOTEBOOK( impldif_dlg.notebook ),
901                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
902                 label );
903
904         table = gtk_table_new(3, 2, FALSE);
905         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
906         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
907         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
908         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
909
910         /* First row */
911         top = 0;
912         label = gtk_label_new( _( "Address Book :" ) );
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);
915
916         labelBook = gtk_label_new("");
917         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
918         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
919
920         /* Second row */
921         top++;
922         label = gtk_label_new( _( "File Name :" ) );
923         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
924         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
925
926         labelFile = gtk_label_new("");
927         gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
928         gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
929
930         /* Third row */
931         top++;
932         label = gtk_label_new( _("Records Imported :") );
933         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
934         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
935
936         labelRecs = gtk_label_new("");
937         gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
938         gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
939
940         impldif_dlg.labelBook    = labelBook;
941         impldif_dlg.labelFile    = labelFile;
942         impldif_dlg.labelRecords = labelRecs;
943 }
944
945 /**
946  * Create main dialog decorations (excluding notebook pages).
947  */
948 static void imp_ldif_dialog_create() {
949         GtkWidget *window;
950         GtkWidget *vbox;
951         GtkWidget *vnbox;
952         GtkWidget *notebook;
953         GtkWidget *hbbox;
954         GtkWidget *btnPrev;
955         GtkWidget *btnNext;
956         GtkWidget *btnProceed;
957         GtkWidget *btnCancel;
958         GtkWidget *hsbox;
959         GtkWidget *statusbar;
960
961         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importldif");
962         gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
963         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
964         gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
965         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
966         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
967         g_signal_connect(G_OBJECT(window), "delete_event",
968                          G_CALLBACK(imp_ldif_delete_event),
969                          NULL );
970         g_signal_connect(G_OBJECT(window), "key_press_event",
971                          G_CALLBACK(imp_ldif_key_pressed),
972                          NULL );
973
974         vbox = gtk_vbox_new(FALSE, 4);
975         gtk_widget_show(vbox);
976         gtk_container_add(GTK_CONTAINER(window), vbox);
977
978         vnbox = gtk_vbox_new(FALSE, 4);
979         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
980         gtk_widget_show(vnbox);
981         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
982
983         /* Notebook */
984         notebook = gtk_notebook_new();
985         gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
986         gtk_widget_show(notebook);
987         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
988         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
989
990         /* Status line */
991         hsbox = gtk_hbox_new(FALSE, 0);
992         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
993         statusbar = gtk_statusbar_new();
994         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
995
996         /* Button panel */
997         gtkut_stock_button_set_create(&hbbox,
998                                       &btnCancel, GTK_STOCK_CANCEL, 
999                                       &btnPrev, GTK_STOCK_GO_BACK,
1000                                       &btnNext, GTK_STOCK_GO_FORWARD);
1001
1002         btnProceed = gtk_button_new_with_mnemonic(_("Proceed"));
1003         gtk_button_set_image(GTK_BUTTON(btnProceed),
1004                         gtk_image_new_from_stock(GTK_STOCK_OK, GTK_ICON_SIZE_BUTTON));
1005         GTK_WIDGET_SET_FLAGS(btnProceed, GTK_CAN_DEFAULT);
1006         gtk_box_pack_start(GTK_BOX(hbbox), btnProceed, TRUE, TRUE, 0);
1007         gtk_widget_hide(btnProceed);
1008
1009         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
1010         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
1011         gtk_widget_grab_default(btnNext);
1012
1013         /* Button handlers */
1014         g_signal_connect(G_OBJECT(btnPrev), "clicked",
1015                          G_CALLBACK(imp_ldif_prev), NULL);
1016         g_signal_connect(G_OBJECT(btnNext), "clicked",
1017                          G_CALLBACK(imp_ldif_next), NULL);
1018         g_signal_connect(G_OBJECT(btnProceed), "clicked",
1019                          G_CALLBACK(imp_ldif_next), NULL);
1020         g_signal_connect(G_OBJECT(btnCancel), "clicked",
1021                          G_CALLBACK(imp_ldif_cancel), NULL);
1022
1023         gtk_widget_show_all(vbox);
1024
1025         impldif_dlg.window     = window;
1026         impldif_dlg.notebook   = notebook;
1027         impldif_dlg.btnPrev    = btnPrev;
1028         impldif_dlg.btnNext    = btnNext;
1029         impldif_dlg.btnProceed = btnProceed;
1030         impldif_dlg.btnCancel  = btnCancel;
1031         impldif_dlg.statusbar  = statusbar;
1032         impldif_dlg.status_cid = gtk_statusbar_get_context_id(
1033                         GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
1034
1035 }
1036
1037 /**
1038  * Create import LDIF dialog.
1039  */
1040 static void imp_ldif_create() {
1041         imp_ldif_dialog_create();
1042         imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
1043         imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
1044         imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
1045         gtk_widget_show_all( impldif_dlg.window );
1046 }
1047
1048 /**
1049  * Import LDIF file.
1050  * \param  addrIndex Address index.
1051  * \return Address book file of imported data, or <i>NULL</i> if import
1052  *         was cancelled.
1053  */
1054 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
1055         _importedBook_ = NULL;
1056         _imp_addressIndex_ = addrIndex;
1057
1058         if( ! impldif_dlg.window )
1059                 imp_ldif_create();
1060
1061         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
1062                              GTK_STOCK_CANCEL);
1063         gtk_widget_hide(impldif_dlg.btnProceed);
1064         gtk_widget_show(impldif_dlg.btnNext);
1065
1066         impldif_dlg.cancelled = FALSE;
1067         gtk_widget_show(impldif_dlg.window);
1068         manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
1069         gtk_widget_grab_default(impldif_dlg.btnNext);
1070
1071         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), IMPORTLDIF_GUESS_NAME );
1072         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), "" );
1073         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
1074         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
1075         gtk_clist_clear( GTK_CLIST(impldif_dlg.clist_field) );
1076         gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
1077         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
1078         gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
1079         stock_pixmap_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
1080                           &markxpm, &markxpmmask );
1081         imp_ldif_message();
1082         gtk_widget_grab_focus(impldif_dlg.entryFile);
1083
1084         impldif_dlg.rowIndSelect = -1;
1085         impldif_dlg.rowCount = 0;
1086         g_free( impldif_dlg.nameBook );
1087         g_free( impldif_dlg.fileName );
1088         impldif_dlg.nameBook = NULL;
1089         impldif_dlg.fileName = NULL;
1090
1091         _ldifFile_ = ldif_create();
1092         gtk_main();
1093         gtk_widget_hide(impldif_dlg.window);
1094         ldif_free( _ldifFile_ );
1095         _ldifFile_ = NULL;
1096         _imp_addressIndex_ = NULL;
1097
1098         g_free( impldif_dlg.nameBook );
1099         g_free( impldif_dlg.fileName );
1100         impldif_dlg.nameBook = NULL;
1101         impldif_dlg.fileName = NULL;
1102
1103         if( impldif_dlg.cancelled == TRUE ) return NULL;
1104         return _importedBook_;
1105 }
1106
1107 /*
1108  * ============================================================================
1109  * End of Source.
1110  * ============================================================================
1111  */
1112