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