2007-08-15 [ticho] 2.10.0cvs125
[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 item is 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 /**
712  * Format notebook fields page.
713  * \param pageNum Page (tab) number.
714  * \param pageLbl Page (tab) label.
715  */
716 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
717         GtkWidget *vbox;
718         GtkWidget *vboxt;
719         GtkWidget *vboxb;
720         GtkWidget *table;
721         GtkWidget *label;
722         GtkWidget *clist_swin;
723         GtkWidget *clist_field;
724         GtkWidget *entryField;
725         GtkWidget *entryAttrib;
726         GtkWidget *checkSelect;
727         GtkWidget *btnModify;
728         GtkWidget *eventBox;
729         GtkTooltips *toolTip;
730         gint top;
731
732         gchar *titles[ FIELDS_N_COLS ];
733         gint i;
734
735         titles[ FIELD_COL_RESERVED ] = _("R");
736         titles[ FIELD_COL_SELECT   ] = _("S");
737         titles[ FIELD_COL_FIELD    ] = _("LDIF Field Name");
738         titles[ FIELD_COL_ATTRIB   ] = _("Attribute Name");
739
740         vbox = gtk_vbox_new(FALSE, 8);
741         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
742         gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
743
744         label = gtk_label_new( pageLbl );
745         gtk_widget_show( label );
746         gtk_notebook_set_tab_label(
747                 GTK_NOTEBOOK( impldif_dlg.notebook ),
748                 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
749                 label );
750
751         /* Upper area - Field list */
752         vboxt = gtk_vbox_new( FALSE, 4 );
753         gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
754
755         clist_swin = gtk_scrolled_window_new( NULL, NULL );
756         gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
757         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
758                                        GTK_POLICY_AUTOMATIC,
759                                        GTK_POLICY_AUTOMATIC);
760
761         clist_field = gtk_clist_new_with_titles( FIELDS_N_COLS, titles );
762         gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
763         gtk_clist_set_selection_mode(
764                 GTK_CLIST(clist_field), GTK_SELECTION_BROWSE );
765         gtk_clist_set_column_width( GTK_CLIST(clist_field),
766                 FIELD_COL_RESERVED, FIELDS_COL_WIDTH_RESERVED );
767         gtk_clist_set_column_width( GTK_CLIST(clist_field),
768                 FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
769         gtk_clist_set_column_width( GTK_CLIST(clist_field),
770                 FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
771         gtk_clist_set_column_width( GTK_CLIST(clist_field),
772                 FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
773
774         /* Remove focus capability for column headers */
775         for( i = 0; i < FIELDS_N_COLS; i++ ) {
776                 GTK_WIDGET_UNSET_FLAGS(
777                         GTK_CLIST(clist_field)->column[i].button,
778                         GTK_CAN_FOCUS);
779         }
780
781         /* Lower area - Edit area */
782         vboxb = gtk_vbox_new( FALSE, 4 );
783         gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
784
785         /* Data entry area */
786         table = gtk_table_new( 3, 3, FALSE);
787         gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
788         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
789         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
790
791         /* First row */
792         top = 0;
793         label = gtk_label_new(_("LDIF Field"));
794         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
795                 GTK_FILL, 0, 0, 0);
796         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
797
798         entryField = gtk_label_new( "" );
799         gtk_misc_set_alignment(GTK_MISC(entryField), 0.01, 0.5);
800         gtk_table_attach(GTK_TABLE(table), entryField, 1, 3, top, (top + 1),
801                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
802
803         /* Second row */
804         ++top;
805         label = gtk_label_new(_("Attribute"));
806         /*
807          * Use an event box to attach some help in the form of a tooltip.
808          * Tried this for the clist but it looked bad.
809          */
810         eventBox = gtk_event_box_new();
811         gtk_container_add( GTK_CONTAINER(eventBox), label );
812         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
813         gtk_table_attach(GTK_TABLE(table), eventBox, 0, 1, top, (top + 1),
814                 GTK_FILL, 0, 0, 0);
815
816         toolTip = gtk_tooltips_new();
817         gtk_tooltips_set_tip( toolTip, eventBox, _(
818                 "Choose the LDIF field that will be renamed or selected " \
819                 "for import in the list above. Reserved fields (marked " \
820                 "with a tick in the \"R\" column), are automatically " \
821                 "imported and cannot be renamed. A single click in the " \
822                 "Select (\"S\") column will select the field for import " \
823                 "with a tick. A single click anywhere in the row will " \
824                 "select that field for rename in the input area below " \
825                 "the list. A double click anywhere in the row will also " \
826                 "select the field for import."
827                 ), NULL );
828
829         entryAttrib = gtk_entry_new();
830         gtk_table_attach(GTK_TABLE(table), entryAttrib, 1, 3, top, (top + 1),
831                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
832
833         toolTip = gtk_tooltips_new();
834         gtk_tooltips_set_tip( toolTip, entryAttrib,
835                 _( "The LDIF field can be renamed to the User Attribute name." ),
836                 NULL );
837
838         /* Next row */
839         ++top;
840
841         checkSelect = gtk_check_button_new_with_label( _( "Select for Import" ) );
842         gtk_table_attach(GTK_TABLE(table), checkSelect, 1, 2, top, (top + 1),
843                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
844
845         toolTip = gtk_tooltips_new();
846         gtk_tooltips_set_tip( toolTip, checkSelect,
847                 _( "Select the LDIF field for import into the address book." ),
848                 NULL );
849
850         btnModify = gtk_button_new_with_label( _(" Modify "));
851         gtk_table_attach(GTK_TABLE(table), btnModify, 2, 3, top, (top + 1),
852                 GTK_FILL, 0, 3, 0);
853
854         toolTip = gtk_tooltips_new();
855         gtk_tooltips_set_tip( toolTip, btnModify,
856                 _( "This button will update the list above with the data supplied." ),
857                 NULL );
858
859         gtk_widget_show_all(vbox);
860
861         /* Event handlers */
862         g_signal_connect( G_OBJECT(clist_field), "select_row",
863                           G_CALLBACK(imp_ldif_field_list_selected), NULL );
864         g_signal_connect( G_OBJECT(clist_field), "button_press_event",
865                           G_CALLBACK(imp_ldif_field_list_toggle), NULL );
866         g_signal_connect( G_OBJECT(btnModify), "clicked",
867                           G_CALLBACK(imp_ldif_modify_pressed), NULL );
868
869         impldif_dlg.clist_field = clist_field;
870         impldif_dlg.entryField  = entryField;
871         impldif_dlg.entryAttrib = entryAttrib;
872         impldif_dlg.checkSelect = checkSelect;
873         impldif_dlg.btnModify   = btnModify;
874 }
875
876 /**
877  * Format notebook finish page.
878  * \param pageNum Page (tab) number.
879  * \param pageLbl Page (tab) label.
880  */
881 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
882         GtkWidget *vbox;
883         GtkWidget *table;
884         GtkWidget *label;
885         GtkWidget *labelBook;
886         GtkWidget *labelFile;
887         GtkWidget *labelRecs;
888         gint top;
889
890         vbox = gtk_vbox_new(FALSE, 8);
891         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
892         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
893
894         label = gtk_label_new( pageLbl );
895         gtk_widget_show( label );
896         gtk_notebook_set_tab_label(
897                 GTK_NOTEBOOK( impldif_dlg.notebook ),
898                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
899                 label );
900
901         table = gtk_table_new(3, 2, FALSE);
902         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
903         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
904         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
905         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
906
907         /* First row */
908         top = 0;
909         label = gtk_label_new( _( "Address Book :" ) );
910         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
911         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
912
913         labelBook = gtk_label_new("");
914         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
915         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
916
917         /* Second row */
918         top++;
919         label = gtk_label_new( _( "File Name :" ) );
920         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
921         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
922
923         labelFile = gtk_label_new("");
924         gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
925         gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
926
927         /* Third row */
928         top++;
929         label = gtk_label_new( _("Records Imported :") );
930         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
931         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
932
933         labelRecs = gtk_label_new("");
934         gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
935         gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
936
937         impldif_dlg.labelBook    = labelBook;
938         impldif_dlg.labelFile    = labelFile;
939         impldif_dlg.labelRecords = labelRecs;
940 }
941
942 /**
943  * Create main dialog decorations (excluding notebook pages).
944  */
945 static void imp_ldif_dialog_create() {
946         GtkWidget *window;
947         GtkWidget *vbox;
948         GtkWidget *vnbox;
949         GtkWidget *notebook;
950         GtkWidget *hbbox;
951         GtkWidget *btnPrev;
952         GtkWidget *btnNext;
953         GtkWidget *btnProceed;
954         GtkWidget *btnCancel;
955         GtkWidget *hsbox;
956         GtkWidget *statusbar;
957
958         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importldif");
959         gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
960         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
961         gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
962         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
963         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
964         g_signal_connect(G_OBJECT(window), "delete_event",
965                          G_CALLBACK(imp_ldif_delete_event),
966                          NULL );
967         g_signal_connect(G_OBJECT(window), "key_press_event",
968                          G_CALLBACK(imp_ldif_key_pressed),
969                          NULL );
970
971         vbox = gtk_vbox_new(FALSE, 4);
972         gtk_widget_show(vbox);
973         gtk_container_add(GTK_CONTAINER(window), vbox);
974
975         vnbox = gtk_vbox_new(FALSE, 4);
976         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
977         gtk_widget_show(vnbox);
978         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
979
980         /* Notebook */
981         notebook = gtk_notebook_new();
982         gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
983         gtk_widget_show(notebook);
984         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
985         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
986
987         /* Status line */
988         hsbox = gtk_hbox_new(FALSE, 0);
989         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
990         statusbar = gtk_statusbar_new();
991         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
992
993         /* Button panel */
994         gtkut_stock_button_set_create(&hbbox,
995                                       &btnCancel, GTK_STOCK_CANCEL, 
996                                       &btnPrev, GTK_STOCK_GO_BACK,
997                                       &btnNext, GTK_STOCK_GO_FORWARD);
998
999         btnProceed = gtk_button_new_with_mnemonic(_("Proceed"));
1000         gtk_button_set_image(GTK_BUTTON(btnProceed),
1001                         gtk_image_new_from_stock(GTK_STOCK_OK, GTK_ICON_SIZE_BUTTON));
1002         GTK_WIDGET_SET_FLAGS(btnProceed, GTK_CAN_DEFAULT);
1003         gtk_box_pack_start(GTK_BOX(hbbox), btnProceed, TRUE, TRUE, 0);
1004         gtk_widget_hide(btnProceed);
1005
1006         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
1007         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
1008         gtk_widget_grab_default(btnNext);
1009
1010         /* Button handlers */
1011         g_signal_connect(G_OBJECT(btnPrev), "clicked",
1012                          G_CALLBACK(imp_ldif_prev), NULL);
1013         g_signal_connect(G_OBJECT(btnNext), "clicked",
1014                          G_CALLBACK(imp_ldif_next), NULL);
1015         g_signal_connect(G_OBJECT(btnProceed), "clicked",
1016                          G_CALLBACK(imp_ldif_next), NULL);
1017         g_signal_connect(G_OBJECT(btnCancel), "clicked",
1018                          G_CALLBACK(imp_ldif_cancel), NULL);
1019
1020         gtk_widget_show_all(vbox);
1021
1022         impldif_dlg.window     = window;
1023         impldif_dlg.notebook   = notebook;
1024         impldif_dlg.btnPrev    = btnPrev;
1025         impldif_dlg.btnNext    = btnNext;
1026         impldif_dlg.btnProceed = btnProceed;
1027         impldif_dlg.btnCancel  = btnCancel;
1028         impldif_dlg.statusbar  = statusbar;
1029         impldif_dlg.status_cid = gtk_statusbar_get_context_id(
1030                         GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
1031
1032 }
1033
1034 /**
1035  * Create import LDIF dialog.
1036  */
1037 static void imp_ldif_create() {
1038         imp_ldif_dialog_create();
1039         imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
1040         imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
1041         imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
1042         gtk_widget_show_all( impldif_dlg.window );
1043 }
1044
1045 /**
1046  * Import LDIF file.
1047  * \param  addrIndex Address index.
1048  * \return Address book file of imported data, or <i>NULL</i> if import
1049  *         was cancelled.
1050  */
1051 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
1052         _importedBook_ = NULL;
1053         _imp_addressIndex_ = addrIndex;
1054
1055         if( ! impldif_dlg.window )
1056                 imp_ldif_create();
1057
1058         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
1059                              GTK_STOCK_CANCEL);
1060         gtk_widget_hide(impldif_dlg.btnProceed);
1061         gtk_widget_show(impldif_dlg.btnNext);
1062
1063         impldif_dlg.cancelled = FALSE;
1064         gtk_widget_show(impldif_dlg.window);
1065         manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
1066         gtk_widget_grab_default(impldif_dlg.btnNext);
1067
1068         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), IMPORTLDIF_GUESS_NAME );
1069         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), "" );
1070         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
1071         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
1072         gtk_clist_clear( GTK_CLIST(impldif_dlg.clist_field) );
1073         gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
1074         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
1075         gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
1076         stock_pixmap_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
1077                           &markxpm, &markxpmmask );
1078         imp_ldif_message();
1079         gtk_widget_grab_focus(impldif_dlg.entryFile);
1080
1081         impldif_dlg.rowIndSelect = -1;
1082         impldif_dlg.rowCount = 0;
1083         g_free( impldif_dlg.nameBook );
1084         g_free( impldif_dlg.fileName );
1085         impldif_dlg.nameBook = NULL;
1086         impldif_dlg.fileName = NULL;
1087
1088         _ldifFile_ = ldif_create();
1089         gtk_main();
1090         gtk_widget_hide(impldif_dlg.window);
1091         ldif_free( _ldifFile_ );
1092         _ldifFile_ = NULL;
1093         _imp_addressIndex_ = NULL;
1094
1095         g_free( impldif_dlg.nameBook );
1096         g_free( impldif_dlg.fileName );
1097         impldif_dlg.nameBook = NULL;
1098         impldif_dlg.fileName = NULL;
1099
1100         if( impldif_dlg.cancelled == TRUE ) return NULL;
1101         return _importedBook_;
1102 }
1103
1104 /*
1105  * ============================================================================
1106  * End of Source.
1107  * ============================================================================
1108  */
1109