Refernce bug
[claws.git] / src / importldif.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2011 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         if (!rec)
210                 return;
211
212         text[ FIELD_COL_RESERVED ] = "";
213         text[ FIELD_COL_SELECT   ] = "";
214         text[ FIELD_COL_FIELD    ] = rec->tagName;
215         text[ FIELD_COL_ATTRIB   ] = rec->userName;
216
217         gtk_cmclist_freeze( clist );
218         gtk_cmclist_remove( clist, row );
219         if( row == impldif_dlg.rowCount - 1 ) {
220                 gtk_cmclist_append( clist, text );
221         }
222         else {
223                 gtk_cmclist_insert( clist, row, text );
224         }
225         if( rec->selected ) {
226                 gtk_cmclist_set_pixbuf(
227                         clist, row, FIELD_COL_SELECT, markxpm );
228         }
229         if( rec->reserved ) {
230                 gtk_cmclist_set_pixbuf(
231                         clist, row, FIELD_COL_RESERVED, markxpm );
232         }
233
234         gtk_cmclist_set_row_data( clist, row, rec );
235         gtk_cmclist_thaw( clist );
236 }
237
238 /**
239  * Load list with LDIF fields read from file.
240  * \param ldf LDIF control data.
241  */
242 static void imp_ldif_load_fields( LdifFile *ldf ) {
243         GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
244         GList *node, *list;
245         gchar *text[ FIELDS_N_COLS ];
246
247         impldif_dlg.rowIndSelect = -1;
248         impldif_dlg.rowCount = 0;
249         if( ! ldf->accessFlag ) return;
250         gtk_cmclist_clear( clist );
251         list = ldif_get_fieldlist( ldf );
252         node = list;
253         while( node ) {
254                 Ldif_FieldRec *rec = node->data;
255                 gint row;
256
257                 text[ FIELD_COL_RESERVED ] = "";
258                 text[ FIELD_COL_SELECT   ] = "";
259                 text[ FIELD_COL_FIELD    ] = rec->tagName;
260                 text[ FIELD_COL_ATTRIB   ] = rec->userName;
261                 row = gtk_cmclist_append( clist, text );
262                 gtk_cmclist_set_row_data( clist, row, rec );
263                 if( rec->selected ) {
264                         gtk_cmclist_set_pixbuf( clist, row,
265                                 FIELD_COL_SELECT, markxpm );
266                 }
267                 if( rec->reserved ) {
268                         gtk_cmclist_set_pixbuf( clist, row,
269                                 FIELD_COL_RESERVED, markxpm );
270                 }
271                 impldif_dlg.rowCount++;
272                 node = g_list_next( node );
273         }
274         g_list_free( list );
275         list = NULL;
276         ldif_set_accessed( ldf, FALSE );
277 }
278
279 /**
280  * Callback function when list item is selected.
281  * \param clist List widget.
282  * \param row   Row.
283  * \param col   Column.
284  * \param event Event object.
285  * \param data  User data.
286  */
287 static void imp_ldif_field_list_selected(
288                 GtkCMCList *clist, gint row, gint column, GdkEvent *event,
289                 gpointer data )
290 {
291         Ldif_FieldRec *rec = gtk_cmclist_get_row_data( clist, row );
292
293         impldif_dlg.rowIndSelect = row;
294         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
295         if( rec ) {
296                 /* Update widget contents */
297                 gtk_label_set_text(
298                         GTK_LABEL(impldif_dlg.entryField), rec->tagName );
299                 if( rec->userName )
300                         gtk_entry_set_text(
301                                 GTK_ENTRY(impldif_dlg.entryAttrib), rec->userName );
302                 gtk_toggle_button_set_active(
303                         GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect),
304                         rec->selected );
305
306                 /* Disable widgets for reserved fields */
307                 gtk_widget_set_sensitive(
308                         impldif_dlg.entryAttrib, ! rec->reserved );
309                 gtk_widget_set_sensitive(
310                         impldif_dlg.checkSelect, ! rec->reserved );
311                 gtk_widget_set_sensitive(
312                         impldif_dlg.btnModify,   ! rec->reserved );
313         }
314         gtk_widget_grab_focus(impldif_dlg.entryAttrib);
315 }
316
317 /**
318  * Callback function to toggle selected LDIF field.
319  * \param clist List to update.
320  * \param event Event object.
321  * \param data  Data.
322  */
323 static gboolean imp_ldif_field_list_toggle(
324                 GtkCMCList *clist, GdkEventButton *event, gpointer data )
325 {
326         Ldif_FieldRec *rec;
327         gboolean toggle = FALSE;
328
329         if( ! event ) return FALSE;
330         if( impldif_dlg.rowIndSelect < 0 ) return FALSE;
331         if( event->button == 1 ) {
332                 /* If single click in select column */
333                 if( event->type == GDK_BUTTON_PRESS ) {
334                         gint x = event->x;
335                         gint y = event->y;
336                         gint row, col;
337
338                         gtk_cmclist_get_selection_info( clist, x, y, &row, &col );
339                         if( col != FIELD_COL_SELECT ) return FALSE;
340                         if( row > impldif_dlg.rowCount ) return FALSE;
341
342                         /* Set row */
343                         impldif_dlg.rowIndSelect = row;
344                         toggle = TRUE;
345                 }
346
347                 /* If double click anywhere in row */
348                 else if( event->type == GDK_2BUTTON_PRESS ) {
349                         toggle = TRUE;
350                 }
351         }
352         /* Toggle field selection */
353         if( toggle ) {
354                 rec = gtk_cmclist_get_row_data(
355                         clist, impldif_dlg.rowIndSelect );
356                 if( rec ) {
357                         ldif_field_toggle( rec );
358                         imp_ldif_update_row( clist );
359                 }
360         }
361         return FALSE;
362 }
363
364 /**
365  * Callback function to update LDIF field data from input fields.
366  * \param widget Widget (button).
367  * \param data   User data.
368  */
369 static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
370         GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
371         Ldif_FieldRec *rec;
372         gint row;
373
374         if( impldif_dlg.rowIndSelect < 0 ) return;
375         row = impldif_dlg.rowIndSelect;
376         rec = gtk_cmclist_get_row_data( clist, impldif_dlg.rowIndSelect );
377
378         ldif_field_set_name( rec, gtk_editable_get_chars(
379                 GTK_EDITABLE(impldif_dlg.entryAttrib), 0, -1 ) );
380         ldif_field_set_selected( rec, gtk_toggle_button_get_active(
381                 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect) ) );
382         imp_ldif_update_row( clist );
383         gtk_cmclist_select_row( clist, row, 0 );
384         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
385         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
386         gtk_toggle_button_set_active(
387                 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect), FALSE );
388 }
389
390 /**
391  * Test whether we can move off fields page.
392  * \return <i>TRUE</i> if OK to move off page.
393  */
394 static gboolean imp_ldif_field_move() {
395         gboolean retVal = FALSE;
396         gchar *newFile;
397         AddressBookFile *abf = NULL;
398
399         if( _importedBook_ ) {
400                 addrbook_free_book( _importedBook_ );
401         }
402
403         abf = addrbook_create_book();
404         addrbook_set_path( abf, _imp_addressIndex_->filePath );
405         addrbook_set_name( abf, impldif_dlg.nameBook );
406         newFile = addrbook_guess_next_file( abf );
407         addrbook_set_file( abf, newFile );
408         g_free( newFile );
409
410         /* Import data into file */
411         if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
412                 addrbook_save_data( abf );
413                 _importedBook_ = abf;
414                 retVal = TRUE;
415         }
416         else {
417                 addrbook_free_book( abf );
418         }
419
420         return retVal;
421 }
422
423 /**
424  * Test whether we can move off file page.
425  * \return <i>TRUE</i> if OK to move off page.
426  */
427 static gboolean imp_ldif_file_move() {
428         gboolean retVal = FALSE;
429         gchar *sName;
430         gchar *sFile;
431         gchar *sMsg = NULL;
432         gboolean errFlag = FALSE;
433
434         sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryFile), 0, -1 );
435         g_strchug( sFile ); g_strchomp( sFile );
436
437         sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
438         g_strchug( sName ); g_strchomp( sName );
439
440         g_free( impldif_dlg.nameBook );
441         g_free( impldif_dlg.fileName );
442         impldif_dlg.nameBook = sName;
443         impldif_dlg.fileName = sFile;
444
445         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), sFile );
446         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), sName );
447
448         if( *sFile == '\0' ) {
449                 sMsg = _( "Please select a file." );
450                 gtk_widget_grab_focus(impldif_dlg.entryFile);
451                 errFlag = TRUE;
452         }
453
454         if( ! errFlag && *sName == '\0' ) {
455                 sMsg = _( "Address book name must be supplied." );
456                 gtk_widget_grab_focus(impldif_dlg.entryName);
457                 errFlag = TRUE;
458         }
459
460         if( ! errFlag ) {
461                 /* Read attribute list */
462                 ldif_set_file( _ldifFile_, sFile );
463                 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
464                         /* Load fields */
465                         /* ldif_print_file( _ldifFile_, stdout ); */
466                         imp_ldif_load_fields( _ldifFile_ );
467                         retVal = TRUE;
468                 }
469                 else {
470                         sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
471                 }
472         }
473         imp_ldif_status_show( sMsg );
474
475         return retVal;
476 }
477
478 /**
479  * Display finish page.
480  */
481 static void imp_ldif_finish_show() {
482         gchar *sMsg;
483         gchar *name;
484
485         name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
486         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
487         g_free( name );
488         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
489         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
490         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
491         gtk_widget_hide( impldif_dlg.btnNext );
492         gtk_widget_show( impldif_dlg.btnProceed );
493         gtk_widget_set_sensitive( impldif_dlg.btnProceed, FALSE );
494         if( _ldifFile_->retVal == MGU_SUCCESS ) {
495                 sMsg = _( "LDIF file imported successfully." );
496         }
497         else {
498                 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
499         }
500         imp_ldif_status_show( sMsg );
501         gtk_widget_grab_focus(impldif_dlg.btnCancel);
502 }
503
504 /**
505  * Callback function to select previous page.
506  * \param widget Widget (button).
507  */
508 static void imp_ldif_prev( GtkWidget *widget ) {
509         gint pageNum;
510
511         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
512         if( pageNum == PAGE_ATTRIBUTES ) {
513                 /* Goto file page stuff */
514                 gtk_notebook_set_current_page(
515                         GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
516                 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
517                 gtk_widget_hide( impldif_dlg.btnProceed );
518                 gtk_widget_show( impldif_dlg.btnNext );
519         }
520         imp_ldif_message();
521 }
522
523 /**
524  * Callback function to select next page.
525  * \param widget Widget (button).
526  */
527 static void imp_ldif_next( GtkWidget *widget ) {
528         gint pageNum;
529
530         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
531         if( pageNum == PAGE_FILE_INFO ) {
532                 /* Goto attributes stuff */
533                 if( imp_ldif_file_move() ) {
534                         gtk_notebook_set_current_page(
535                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
536                         imp_ldif_message();
537                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
538                         gtk_widget_hide( impldif_dlg.btnNext );
539                         gtk_widget_show( impldif_dlg.btnProceed );
540                         gtk_widget_set_sensitive( impldif_dlg.btnProceed, TRUE );
541                 }
542                 else {
543                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
544                         _ldifFile_->dirtyFlag = TRUE;
545                 }
546         }
547         else if( pageNum == PAGE_ATTRIBUTES ) {
548                 /* Goto finish stuff */
549                 if( imp_ldif_field_move() ) {
550                         gtk_notebook_set_current_page(
551                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
552                         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
553                                              GTK_STOCK_CLOSE);
554                         imp_ldif_finish_show();
555                 }
556         }
557 }
558
559 /**
560  * Callback function to cancel and close dialog.
561  * \param widget Widget (button).
562  * \param data   User data.
563  */
564 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
565         gint pageNum;
566
567         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
568         if( pageNum != PAGE_FINISH ) {
569                 impldif_dlg.cancelled = TRUE;
570         }
571         gtk_main_quit();
572 }
573
574
575 /**
576  * Create LDIF file selection dialog.
577  * \param afs Address file selection data.
578  */
579 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
580         gchar *file = filesel_select_file_open(_("Select LDIF File"), NULL);
581
582         if (file == NULL)
583                 afs->cancelled = TRUE;
584         else {
585                 afs->cancelled = FALSE;
586                 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), file );
587                 g_free(file);
588         }
589 }
590
591 /**
592  * Callback function to display LDIF file selection dialog.
593  */
594 static void imp_ldif_file_select( void ) {
595         imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
596 }
597
598 /**
599  * Callback function to handle dialog close event.
600  * \param widget Widget (dialog).
601  * \param event  Event object.
602  * \param data   User data.
603  */
604 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
605         imp_ldif_cancel( widget, data );
606         return TRUE;
607 }
608
609 /**
610  * Callback function to respond to dialog key press events.
611  * \param widget Widget.
612  * \param event  Event object.
613  * \param data   User data.
614  */
615 static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
616         if (event && event->keyval == GDK_KEY_Escape) {
617                 imp_ldif_cancel( widget, data );
618         }
619         return FALSE;
620 }
621
622 /**
623  * Format notebook "file" page.
624  * \param pageNum Page (tab) number.
625  * \param pageLbl Page (tab) label.
626  */
627 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
628         GtkWidget *vbox;
629         GtkWidget *table;
630         GtkWidget *label;
631         GtkWidget *entryFile;
632         GtkWidget *entryName;
633         GtkWidget *btnFile;
634         gint top;
635
636         vbox = gtk_vbox_new(FALSE, 8);
637         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
638         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
639
640         label = gtk_label_new( pageLbl );
641         gtk_widget_show( label );
642         gtk_notebook_set_tab_label(
643                 GTK_NOTEBOOK( impldif_dlg.notebook ),
644                 gtk_notebook_get_nth_page(
645                         GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
646                 label );
647
648         table = gtk_table_new(2, 3, FALSE);
649         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
650         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
651         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
652         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
653
654         /* First row */
655         top = 0;
656         label = gtk_label_new(_("Address Book"));
657         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
658                 GTK_FILL, 0, 0, 0);
659         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
660
661         entryName = gtk_entry_new();
662         gtk_table_attach(GTK_TABLE(table), entryName, 1, 2, top, (top + 1),
663                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
664
665         CLAWS_SET_TIP(entryName, _( 
666                 "Specify the name for the address book that will " \
667                 "be created from the LDIF file data." ));
668
669         /* Second row */
670         top = 1;
671         label = gtk_label_new(_("File Name"));
672         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
673                 GTK_FILL, 0, 0, 0);
674         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
675
676         entryFile = gtk_entry_new();
677         gtk_table_attach(GTK_TABLE(table), entryFile, 1, 2, top, (top + 1),
678                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
679
680         CLAWS_SET_TIP(entryFile,
681                 _( "The full file specification of the LDIF file to import." ));
682
683         btnFile = gtkut_get_browse_file_btn(_("B_rowse"));
684         gtk_table_attach(GTK_TABLE(table), btnFile, 2, 3, top, (top + 1),
685                 GTK_FILL, 0, 3, 0);
686
687         CLAWS_SET_TIP(btnFile,
688                 _( "Select the LDIF file to import." ));
689
690         gtk_widget_show_all(vbox);
691
692         /* Button handler */
693         g_signal_connect(G_OBJECT(btnFile), "clicked",
694                          G_CALLBACK(imp_ldif_file_select), NULL);
695
696         impldif_dlg.entryFile = entryFile;
697         impldif_dlg.entryName = entryName;
698
699 }
700
701 /**
702  * Format notebook fields page.
703  * \param pageNum Page (tab) number.
704  * \param pageLbl Page (tab) label.
705  */
706 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
707         GtkWidget *vbox;
708         GtkWidget *vboxt;
709         GtkWidget *vboxb;
710         GtkWidget *table;
711         GtkWidget *label;
712         GtkWidget *clist_swin;
713         GtkWidget *clist_field;
714         GtkWidget *entryField;
715         GtkWidget *entryAttrib;
716         GtkWidget *checkSelect;
717         GtkWidget *btnModify;
718         GtkWidget *eventBox;
719         gint top;
720
721         gchar *titles[ FIELDS_N_COLS ];
722         gint i;
723
724         titles[ FIELD_COL_RESERVED ] = _("R");
725         titles[ FIELD_COL_SELECT   ] = _("S");
726         titles[ FIELD_COL_FIELD    ] = _("LDIF Field Name");
727         titles[ FIELD_COL_ATTRIB   ] = _("Attribute Name");
728
729         vbox = gtk_vbox_new(FALSE, 8);
730         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
731         gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
732
733         label = gtk_label_new( pageLbl );
734         gtk_widget_show( label );
735         gtk_notebook_set_tab_label(
736                 GTK_NOTEBOOK( impldif_dlg.notebook ),
737                 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
738                 label );
739
740         /* Upper area - Field list */
741         vboxt = gtk_vbox_new( FALSE, 4 );
742         gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
743
744         clist_swin = gtk_scrolled_window_new( NULL, NULL );
745         gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
746         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
747                                        GTK_POLICY_AUTOMATIC,
748                                        GTK_POLICY_AUTOMATIC);
749
750         clist_field = gtk_cmclist_new_with_titles( FIELDS_N_COLS, titles );
751         gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
752         gtk_cmclist_set_selection_mode(
753                 GTK_CMCLIST(clist_field), GTK_SELECTION_BROWSE );
754         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
755                 FIELD_COL_RESERVED, FIELDS_COL_WIDTH_RESERVED );
756         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
757                 FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
758         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
759                 FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
760         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
761                 FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
762
763         /* Remove focus capability for column headers */
764         for( i = 0; i < FIELDS_N_COLS; i++ ) {
765                 gtkut_widget_set_can_focus(
766                         GTK_CMCLIST(clist_field)->column[i].button,
767                         FALSE);
768         }
769
770         /* Lower area - Edit area */
771         vboxb = gtk_vbox_new( FALSE, 4 );
772         gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
773
774         /* Data entry area */
775         table = gtk_table_new( 3, 3, FALSE);
776         gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
777         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
778         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
779
780         /* First row */
781         top = 0;
782         label = gtk_label_new(_("LDIF Field"));
783         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
784                 GTK_FILL, 0, 0, 0);
785         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
786
787         entryField = gtk_label_new( "" );
788         gtk_misc_set_alignment(GTK_MISC(entryField), 0.01, 0.5);
789         gtk_table_attach(GTK_TABLE(table), entryField, 1, 3, top, (top + 1),
790                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
791
792         /* Second row */
793         ++top;
794         label = gtk_label_new(_("Attribute"));
795         /*
796          * Use an event box to attach some help in the form of a tooltip.
797          * Tried this for the clist but it looked bad.
798          */
799         eventBox = gtk_event_box_new();
800         gtk_container_add( GTK_CONTAINER(eventBox), label );
801         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
802         gtk_table_attach(GTK_TABLE(table), eventBox, 0, 1, top, (top + 1),
803                 GTK_FILL, 0, 0, 0);
804
805         CLAWS_SET_TIP(eventBox, _(
806                 "Choose the LDIF field that will be renamed or selected " \
807                 "for import in the list above. Reserved fields (marked " \
808                 "with a tick in the \"R\" column), are automatically " \
809                 "imported and cannot be renamed. A single click in the " \
810                 "Select (\"S\") column will select the field for import " \
811                 "with a tick. A single click anywhere in the row will " \
812                 "select that field for rename in the input area below " \
813                 "the list. A double click anywhere in the row will also " \
814                 "select the field for import."));
815
816         entryAttrib = gtk_entry_new();
817         gtk_table_attach(GTK_TABLE(table), entryAttrib, 1, 3, top, (top + 1),
818                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
819
820         CLAWS_SET_TIP(entryAttrib,
821                 _( "The LDIF field can be renamed to the User Attribute name." ));
822
823         /* Next row */
824         ++top;
825
826         checkSelect = gtk_check_button_new_with_label( _( "Select for Import" ) );
827         gtk_table_attach(GTK_TABLE(table), checkSelect, 1, 2, top, (top + 1),
828                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
829
830         CLAWS_SET_TIP(checkSelect,
831                 _( "Select the LDIF field for import into the address book." ));
832
833         btnModify = gtk_button_new_with_label( _(" Modify "));
834         gtk_table_attach(GTK_TABLE(table), btnModify, 2, 3, top, (top + 1),
835                 GTK_FILL, 0, 3, 0);
836
837         CLAWS_SET_TIP(btnModify,
838                 _( "This button will update the list above with the data supplied." ));
839
840         gtk_widget_show_all(vbox);
841
842         /* Event handlers */
843         g_signal_connect( G_OBJECT(clist_field), "select_row",
844                           G_CALLBACK(imp_ldif_field_list_selected), NULL );
845         g_signal_connect( G_OBJECT(clist_field), "button_press_event",
846                           G_CALLBACK(imp_ldif_field_list_toggle), NULL );
847         g_signal_connect( G_OBJECT(btnModify), "clicked",
848                           G_CALLBACK(imp_ldif_modify_pressed), NULL );
849
850         impldif_dlg.clist_field = clist_field;
851         impldif_dlg.entryField  = entryField;
852         impldif_dlg.entryAttrib = entryAttrib;
853         impldif_dlg.checkSelect = checkSelect;
854         impldif_dlg.btnModify   = btnModify;
855 }
856
857 /**
858  * Format notebook finish page.
859  * \param pageNum Page (tab) number.
860  * \param pageLbl Page (tab) label.
861  */
862 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
863         GtkWidget *vbox;
864         GtkWidget *table;
865         GtkWidget *label;
866         GtkWidget *labelBook;
867         GtkWidget *labelFile;
868         GtkWidget *labelRecs;
869         gint top;
870
871         vbox = gtk_vbox_new(FALSE, 8);
872         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
873         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
874
875         label = gtk_label_new( pageLbl );
876         gtk_widget_show( label );
877         gtk_notebook_set_tab_label(
878                 GTK_NOTEBOOK( impldif_dlg.notebook ),
879                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
880                 label );
881
882         table = gtk_table_new(3, 2, FALSE);
883         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
884         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
885         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
886         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
887
888         /* First row */
889         top = 0;
890         label = gtk_label_new( _( "Address Book :" ) );
891         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
892         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
893
894         labelBook = gtk_label_new("");
895         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
896         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
897
898         /* Second row */
899         top++;
900         label = gtk_label_new( _( "File Name :" ) );
901         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
902         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
903
904         labelFile = gtk_label_new("");
905         gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
906         gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
907
908         /* Third row */
909         top++;
910         label = gtk_label_new( _("Records Imported :") );
911         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
912         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
913
914         labelRecs = gtk_label_new("");
915         gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
916         gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
917
918         impldif_dlg.labelBook    = labelBook;
919         impldif_dlg.labelFile    = labelFile;
920         impldif_dlg.labelRecords = labelRecs;
921 }
922
923 /**
924  * Create main dialog decorations (excluding notebook pages).
925  */
926 static void imp_ldif_dialog_create() {
927         GtkWidget *window;
928         GtkWidget *vbox;
929         GtkWidget *vnbox;
930         GtkWidget *notebook;
931         GtkWidget *hbbox;
932         GtkWidget *btnPrev;
933         GtkWidget *btnNext;
934         GtkWidget *btnProceed;
935         GtkWidget *btnCancel;
936         GtkWidget *hsbox;
937         GtkWidget *statusbar;
938
939         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importldif");
940         gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
941         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
942         gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
943         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
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         gtkut_widget_set_can_default(btnProceed, TRUE);
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         gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), TRUE);
1048
1049         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), IMPORTLDIF_GUESS_NAME );
1050         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), "" );
1051         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
1052         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
1053         gtk_cmclist_clear( GTK_CMCLIST(impldif_dlg.clist_field) );
1054         gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
1055         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
1056         gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
1057         stock_pixbuf_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
1058                           &markxpm );
1059         imp_ldif_message();
1060         gtk_widget_grab_focus(impldif_dlg.entryFile);
1061
1062         impldif_dlg.rowIndSelect = -1;
1063         impldif_dlg.rowCount = 0;
1064         g_free( impldif_dlg.nameBook );
1065         g_free( impldif_dlg.fileName );
1066         impldif_dlg.nameBook = NULL;
1067         impldif_dlg.fileName = NULL;
1068
1069         _ldifFile_ = ldif_create();
1070         gtk_main();
1071         gtk_widget_hide(impldif_dlg.window);
1072         gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), FALSE);
1073         ldif_free( _ldifFile_ );
1074         _ldifFile_ = NULL;
1075         _imp_addressIndex_ = NULL;
1076
1077         g_free( impldif_dlg.nameBook );
1078         g_free( impldif_dlg.fileName );
1079         impldif_dlg.nameBook = NULL;
1080         impldif_dlg.fileName = NULL;
1081
1082         if( impldif_dlg.cancelled == TRUE ) return NULL;
1083         return _importedBook_;
1084 }
1085
1086 /*
1087  * ============================================================================
1088  * End of Source.
1089  * ============================================================================
1090  */
1091