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