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