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