2013-01-21 [paul] 3.9.0cvs56
[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                         gtk_cmclist_get_selection_info( clist, x, y, &row, &col );
340                         if( col != FIELD_COL_SELECT ) return FALSE;
341                         if( row > impldif_dlg.rowCount ) return FALSE;
342
343                         /* Set row */
344                         impldif_dlg.rowIndSelect = row;
345                         toggle = TRUE;
346                 }
347
348                 /* If double click anywhere in row */
349                 else if( event->type == GDK_2BUTTON_PRESS ) {
350                         toggle = TRUE;
351                 }
352         }
353         /* Toggle field selection */
354         if( toggle ) {
355                 rec = gtk_cmclist_get_row_data(
356                         clist, impldif_dlg.rowIndSelect );
357                 if( rec ) {
358                         ldif_field_toggle( rec );
359                         imp_ldif_update_row( clist );
360                 }
361         }
362         return FALSE;
363 }
364
365 /**
366  * Callback function to update LDIF field data from input fields.
367  * \param widget Widget (button).
368  * \param data   User data.
369  */
370 static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
371         GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
372         Ldif_FieldRec *rec;
373         gint row;
374
375         if( impldif_dlg.rowIndSelect < 0 ) return;
376         row = impldif_dlg.rowIndSelect;
377         rec = gtk_cmclist_get_row_data( clist, impldif_dlg.rowIndSelect );
378
379         ldif_field_set_name( rec, gtk_editable_get_chars(
380                 GTK_EDITABLE(impldif_dlg.entryAttrib), 0, -1 ) );
381         ldif_field_set_selected( rec, gtk_toggle_button_get_active(
382                 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect) ) );
383         imp_ldif_update_row( clist );
384         gtk_cmclist_select_row( clist, row, 0 );
385         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
386         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
387         gtk_toggle_button_set_active(
388                 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect), FALSE );
389 }
390
391 /**
392  * Test whether we can move off fields page.
393  * \return <i>TRUE</i> if OK to move off page.
394  */
395 static gboolean imp_ldif_field_move() {
396         gboolean retVal = FALSE;
397         gchar *newFile;
398         AddressBookFile *abf = NULL;
399
400         if( _importedBook_ ) {
401                 addrbook_free_book( _importedBook_ );
402         }
403
404         abf = addrbook_create_book();
405         addrbook_set_path( abf, _imp_addressIndex_->filePath );
406         addrbook_set_name( abf, impldif_dlg.nameBook );
407         newFile = addrbook_guess_next_file( abf );
408         addrbook_set_file( abf, newFile );
409         g_free( newFile );
410
411         /* Import data into file */
412         if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
413                 addrbook_save_data( abf );
414                 _importedBook_ = abf;
415                 retVal = TRUE;
416         }
417         else {
418                 addrbook_free_book( abf );
419         }
420
421         return retVal;
422 }
423
424 /**
425  * Test whether we can move off file page.
426  * \return <i>TRUE</i> if OK to move off page.
427  */
428 static gboolean imp_ldif_file_move() {
429         gboolean retVal = FALSE;
430         gchar *sName;
431         gchar *sFile;
432         gchar *sMsg = NULL;
433         gboolean errFlag = FALSE;
434
435         sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryFile), 0, -1 );
436         g_strchug( sFile ); g_strchomp( sFile );
437
438         sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
439         g_strchug( sName ); g_strchomp( sName );
440
441         g_free( impldif_dlg.nameBook );
442         g_free( impldif_dlg.fileName );
443         impldif_dlg.nameBook = sName;
444         impldif_dlg.fileName = sFile;
445
446         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), sFile );
447         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), sName );
448
449         if( *sFile == '\0' ) {
450                 sMsg = _( "Please select a file." );
451                 gtk_widget_grab_focus(impldif_dlg.entryFile);
452                 errFlag = TRUE;
453         }
454
455         if( ! errFlag && *sName == '\0' ) {
456                 sMsg = _( "Address book name must be supplied." );
457                 gtk_widget_grab_focus(impldif_dlg.entryName);
458                 errFlag = TRUE;
459         }
460
461         if( ! errFlag ) {
462                 /* Read attribute list */
463                 ldif_set_file( _ldifFile_, sFile );
464                 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
465                         /* Load fields */
466                         /* ldif_print_file( _ldifFile_, stdout ); */
467                         imp_ldif_load_fields( _ldifFile_ );
468                         retVal = TRUE;
469                 }
470                 else {
471                         sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
472                 }
473         }
474         imp_ldif_status_show( sMsg );
475
476         return retVal;
477 }
478
479 /**
480  * Display finish page.
481  */
482 static void imp_ldif_finish_show() {
483         gchar *sMsg;
484         gchar *name;
485
486         name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
487         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
488         g_free( name );
489         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
490         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
491         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
492         gtk_widget_hide( impldif_dlg.btnNext );
493         gtk_widget_show( impldif_dlg.btnProceed );
494         gtk_widget_set_sensitive( impldif_dlg.btnProceed, FALSE );
495         if( _ldifFile_->retVal == MGU_SUCCESS ) {
496                 sMsg = _( "LDIF file imported successfully." );
497         }
498         else {
499                 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
500         }
501         imp_ldif_status_show( sMsg );
502         gtk_widget_grab_focus(impldif_dlg.btnCancel);
503 }
504
505 /**
506  * Callback function to select previous page.
507  * \param widget Widget (button).
508  */
509 static void imp_ldif_prev( GtkWidget *widget ) {
510         gint pageNum;
511
512         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
513         if( pageNum == PAGE_ATTRIBUTES ) {
514                 /* Goto file page stuff */
515                 gtk_notebook_set_current_page(
516                         GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
517                 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
518                 gtk_widget_hide( impldif_dlg.btnProceed );
519                 gtk_widget_show( impldif_dlg.btnNext );
520         }
521         imp_ldif_message();
522 }
523
524 /**
525  * Callback function to select next page.
526  * \param widget Widget (button).
527  */
528 static void imp_ldif_next( GtkWidget *widget ) {
529         gint pageNum;
530
531         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
532         if( pageNum == PAGE_FILE_INFO ) {
533                 /* Goto attributes stuff */
534                 if( imp_ldif_file_move() ) {
535                         gtk_notebook_set_current_page(
536                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
537                         imp_ldif_message();
538                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
539                         gtk_widget_hide( impldif_dlg.btnNext );
540                         gtk_widget_show( impldif_dlg.btnProceed );
541                         gtk_widget_set_sensitive( impldif_dlg.btnProceed, TRUE );
542                 }
543                 else {
544                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
545                         _ldifFile_->dirtyFlag = TRUE;
546                 }
547         }
548         else if( pageNum == PAGE_ATTRIBUTES ) {
549                 /* Goto finish stuff */
550                 if( imp_ldif_field_move() ) {
551                         gtk_notebook_set_current_page(
552                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
553                         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
554                                              GTK_STOCK_CLOSE);
555                         imp_ldif_finish_show();
556                 }
557         }
558 }
559
560 /**
561  * Callback function to cancel and close dialog.
562  * \param widget Widget (button).
563  * \param data   User data.
564  */
565 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
566         gint pageNum;
567
568         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
569         if( pageNum != PAGE_FINISH ) {
570                 impldif_dlg.cancelled = TRUE;
571         }
572         gtk_main_quit();
573 }
574
575
576 /**
577  * Create LDIF file selection dialog.
578  * \param afs Address file selection data.
579  */
580 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
581         gchar *file = filesel_select_file_open(_("Select LDIF File"), NULL);
582
583         if (file == NULL)
584                 afs->cancelled = TRUE;
585         else {
586                 afs->cancelled = FALSE;
587                 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), file );
588                 g_free(file);
589         }
590 }
591
592 /**
593  * Callback function to display LDIF file selection dialog.
594  */
595 static void imp_ldif_file_select( void ) {
596         imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
597 }
598
599 /**
600  * Callback function to handle dialog close event.
601  * \param widget Widget (dialog).
602  * \param event  Event object.
603  * \param data   User data.
604  */
605 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
606         imp_ldif_cancel( widget, data );
607         return TRUE;
608 }
609
610 /**
611  * Callback function to respond to dialog key press events.
612  * \param widget Widget.
613  * \param event  Event object.
614  * \param data   User data.
615  */
616 static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
617         if (event && event->keyval == GDK_KEY_Escape) {
618                 imp_ldif_cancel( widget, data );
619         }
620         return FALSE;
621 }
622
623 /**
624  * Format notebook "file" page.
625  * \param pageNum Page (tab) number.
626  * \param pageLbl Page (tab) label.
627  */
628 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
629         GtkWidget *vbox;
630         GtkWidget *table;
631         GtkWidget *label;
632         GtkWidget *entryFile;
633         GtkWidget *entryName;
634         GtkWidget *btnFile;
635         gint top;
636
637         vbox = gtk_vbox_new(FALSE, 8);
638         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
639         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
640
641         label = gtk_label_new( pageLbl );
642         gtk_widget_show( label );
643         gtk_notebook_set_tab_label(
644                 GTK_NOTEBOOK( impldif_dlg.notebook ),
645                 gtk_notebook_get_nth_page(
646                         GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
647                 label );
648
649         table = gtk_table_new(2, 3, FALSE);
650         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
651         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
652         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
653         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
654
655         /* First row */
656         top = 0;
657         label = gtk_label_new(_("Address Book"));
658         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
659                 GTK_FILL, 0, 0, 0);
660         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
661
662         entryName = gtk_entry_new();
663         gtk_table_attach(GTK_TABLE(table), entryName, 1, 2, top, (top + 1),
664                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
665
666         CLAWS_SET_TIP(entryName, _( 
667                 "Specify the name for the address book that will " \
668                 "be created from the LDIF file data." ));
669
670         /* Second row */
671         top = 1;
672         label = gtk_label_new(_("File Name"));
673         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
674                 GTK_FILL, 0, 0, 0);
675         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
676
677         entryFile = gtk_entry_new();
678         gtk_table_attach(GTK_TABLE(table), entryFile, 1, 2, top, (top + 1),
679                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
680
681         CLAWS_SET_TIP(entryFile,
682                 _( "The full file specification of the LDIF file to import." ));
683
684         btnFile = gtkut_get_browse_file_btn(_("B_rowse"));
685         gtk_table_attach(GTK_TABLE(table), btnFile, 2, 3, top, (top + 1),
686                 GTK_FILL, 0, 3, 0);
687
688         CLAWS_SET_TIP(btnFile,
689                 _( "Select the LDIF file to import." ));
690
691         gtk_widget_show_all(vbox);
692
693         /* Button handler */
694         g_signal_connect(G_OBJECT(btnFile), "clicked",
695                          G_CALLBACK(imp_ldif_file_select), NULL);
696
697         impldif_dlg.entryFile = entryFile;
698         impldif_dlg.entryName = entryName;
699
700 }
701
702 /**
703  * Format notebook fields page.
704  * \param pageNum Page (tab) number.
705  * \param pageLbl Page (tab) label.
706  */
707 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
708         GtkWidget *vbox;
709         GtkWidget *vboxt;
710         GtkWidget *vboxb;
711         GtkWidget *table;
712         GtkWidget *label;
713         GtkWidget *clist_swin;
714         GtkWidget *clist_field;
715         GtkWidget *entryField;
716         GtkWidget *entryAttrib;
717         GtkWidget *checkSelect;
718         GtkWidget *btnModify;
719         GtkWidget *eventBox;
720         gint top;
721
722         gchar *titles[ FIELDS_N_COLS ];
723         gint i;
724
725         titles[ FIELD_COL_RESERVED ] = _("R");
726         titles[ FIELD_COL_SELECT   ] = _("S");
727         titles[ FIELD_COL_FIELD    ] = _("LDIF Field Name");
728         titles[ FIELD_COL_ATTRIB   ] = _("Attribute Name");
729
730         vbox = gtk_vbox_new(FALSE, 8);
731         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
732         gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
733
734         label = gtk_label_new( pageLbl );
735         gtk_widget_show( label );
736         gtk_notebook_set_tab_label(
737                 GTK_NOTEBOOK( impldif_dlg.notebook ),
738                 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
739                 label );
740
741         /* Upper area - Field list */
742         vboxt = gtk_vbox_new( FALSE, 4 );
743         gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
744
745         clist_swin = gtk_scrolled_window_new( NULL, NULL );
746         gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
747         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
748                                        GTK_POLICY_AUTOMATIC,
749                                        GTK_POLICY_AUTOMATIC);
750
751         clist_field = gtk_cmclist_new_with_titles( FIELDS_N_COLS, titles );
752         gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
753         gtk_cmclist_set_selection_mode(
754                 GTK_CMCLIST(clist_field), GTK_SELECTION_BROWSE );
755         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
756                 FIELD_COL_RESERVED, FIELDS_COL_WIDTH_RESERVED );
757         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
758                 FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
759         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
760                 FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
761         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
762                 FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
763
764         /* Remove focus capability for column headers */
765         for( i = 0; i < FIELDS_N_COLS; i++ ) {
766                 gtkut_widget_set_can_focus(
767                         GTK_CMCLIST(clist_field)->column[i].button,
768                         FALSE);
769         }
770
771         /* Lower area - Edit area */
772         vboxb = gtk_vbox_new( FALSE, 4 );
773         gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
774
775         /* Data entry area */
776         table = gtk_table_new( 3, 3, FALSE);
777         gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
778         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
779         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
780
781         /* First row */
782         top = 0;
783         label = gtk_label_new(_("LDIF Field"));
784         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
785                 GTK_FILL, 0, 0, 0);
786         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
787
788         entryField = gtk_label_new( "" );
789         gtk_misc_set_alignment(GTK_MISC(entryField), 0.01, 0.5);
790         gtk_table_attach(GTK_TABLE(table), entryField, 1, 3, top, (top + 1),
791                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
792
793         /* Second row */
794         ++top;
795         label = gtk_label_new(_("Attribute"));
796         /*
797          * Use an event box to attach some help in the form of a tooltip.
798          * Tried this for the clist but it looked bad.
799          */
800         eventBox = gtk_event_box_new();
801         gtk_container_add( GTK_CONTAINER(eventBox), label );
802         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
803         gtk_table_attach(GTK_TABLE(table), eventBox, 0, 1, top, (top + 1),
804                 GTK_FILL, 0, 0, 0);
805
806         CLAWS_SET_TIP(eventBox, _(
807                 "Choose the LDIF field that will be renamed or selected " \
808                 "for import in the list above. Reserved fields (marked " \
809                 "with a tick in the \"R\" column), are automatically " \
810                 "imported and cannot be renamed. A single click in the " \
811                 "Select (\"S\") column will select the field for import " \
812                 "with a tick. A single click anywhere in the row will " \
813                 "select that field for rename in the input area below " \
814                 "the list. A double click anywhere in the row will also " \
815                 "select the field for import."));
816
817         entryAttrib = gtk_entry_new();
818         gtk_table_attach(GTK_TABLE(table), entryAttrib, 1, 3, top, (top + 1),
819                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
820
821         CLAWS_SET_TIP(entryAttrib,
822                 _( "The LDIF field can be renamed to the User Attribute name." ));
823
824         /* Next row */
825         ++top;
826
827         checkSelect = gtk_check_button_new_with_label( _( "Select for Import" ) );
828         gtk_table_attach(GTK_TABLE(table), checkSelect, 1, 2, top, (top + 1),
829                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
830
831         CLAWS_SET_TIP(checkSelect,
832                 _( "Select the LDIF field for import into the address book." ));
833
834         btnModify = gtk_button_new_with_label( _(" Modify "));
835         gtk_table_attach(GTK_TABLE(table), btnModify, 2, 3, top, (top + 1),
836                 GTK_FILL, 0, 3, 0);
837
838         CLAWS_SET_TIP(btnModify,
839                 _( "This button will update the list above with the data supplied." ));
840
841         gtk_widget_show_all(vbox);
842
843         /* Event handlers */
844         g_signal_connect( G_OBJECT(clist_field), "select_row",
845                           G_CALLBACK(imp_ldif_field_list_selected), NULL );
846         g_signal_connect( G_OBJECT(clist_field), "button_press_event",
847                           G_CALLBACK(imp_ldif_field_list_toggle), NULL );
848         g_signal_connect( G_OBJECT(btnModify), "clicked",
849                           G_CALLBACK(imp_ldif_modify_pressed), NULL );
850
851         impldif_dlg.clist_field = clist_field;
852         impldif_dlg.entryField  = entryField;
853         impldif_dlg.entryAttrib = entryAttrib;
854         impldif_dlg.checkSelect = checkSelect;
855         impldif_dlg.btnModify   = btnModify;
856 }
857
858 /**
859  * Format notebook finish page.
860  * \param pageNum Page (tab) number.
861  * \param pageLbl Page (tab) label.
862  */
863 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
864         GtkWidget *vbox;
865         GtkWidget *table;
866         GtkWidget *label;
867         GtkWidget *labelBook;
868         GtkWidget *labelFile;
869         GtkWidget *labelRecs;
870         gint top;
871
872         vbox = gtk_vbox_new(FALSE, 8);
873         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
874         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
875
876         label = gtk_label_new( pageLbl );
877         gtk_widget_show( label );
878         gtk_notebook_set_tab_label(
879                 GTK_NOTEBOOK( impldif_dlg.notebook ),
880                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
881                 label );
882
883         table = gtk_table_new(3, 2, FALSE);
884         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
885         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
886         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
887         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
888
889         /* First row */
890         top = 0;
891         label = gtk_label_new( _( "Address Book :" ) );
892         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
893         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
894
895         labelBook = gtk_label_new("");
896         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
897         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
898
899         /* Second row */
900         top++;
901         label = gtk_label_new( _( "File Name :" ) );
902         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
903         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
904
905         labelFile = gtk_label_new("");
906         gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
907         gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
908
909         /* Third row */
910         top++;
911         label = gtk_label_new( _("Records Imported :") );
912         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
913         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
914
915         labelRecs = gtk_label_new("");
916         gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
917         gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
918
919         impldif_dlg.labelBook    = labelBook;
920         impldif_dlg.labelFile    = labelFile;
921         impldif_dlg.labelRecords = labelRecs;
922 }
923
924 /**
925  * Create main dialog decorations (excluding notebook pages).
926  */
927 static void imp_ldif_dialog_create() {
928         GtkWidget *window;
929         GtkWidget *vbox;
930         GtkWidget *vnbox;
931         GtkWidget *notebook;
932         GtkWidget *hbbox;
933         GtkWidget *btnPrev;
934         GtkWidget *btnNext;
935         GtkWidget *btnProceed;
936         GtkWidget *btnCancel;
937         GtkWidget *hsbox;
938         GtkWidget *statusbar;
939
940         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importldif");
941         gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
942         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
943         gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
944         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
945         g_signal_connect(G_OBJECT(window), "delete_event",
946                          G_CALLBACK(imp_ldif_delete_event),
947                          NULL );
948         g_signal_connect(G_OBJECT(window), "key_press_event",
949                          G_CALLBACK(imp_ldif_key_pressed),
950                          NULL );
951
952         vbox = gtk_vbox_new(FALSE, 4);
953         gtk_widget_show(vbox);
954         gtk_container_add(GTK_CONTAINER(window), vbox);
955
956         vnbox = gtk_vbox_new(FALSE, 4);
957         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
958         gtk_widget_show(vnbox);
959         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
960
961         /* Notebook */
962         notebook = gtk_notebook_new();
963         gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
964         gtk_widget_show(notebook);
965         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
966         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
967
968         /* Status line */
969         hsbox = gtk_hbox_new(FALSE, 0);
970         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
971         statusbar = gtk_statusbar_new();
972         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
973
974         /* Button panel */
975         gtkut_stock_button_set_create(&hbbox,
976                                       &btnCancel, GTK_STOCK_CANCEL, 
977                                       &btnPrev, GTK_STOCK_GO_BACK,
978                                       &btnNext, GTK_STOCK_GO_FORWARD);
979
980         btnProceed = gtk_button_new_with_mnemonic(_("Proceed"));
981         gtk_button_set_image(GTK_BUTTON(btnProceed),
982                         gtk_image_new_from_stock(GTK_STOCK_OK, GTK_ICON_SIZE_BUTTON));
983         gtkut_widget_set_can_default(btnProceed, TRUE);
984         gtk_box_pack_start(GTK_BOX(hbbox), btnProceed, TRUE, TRUE, 0);
985         gtk_widget_hide(btnProceed);
986
987         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
988         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
989         gtk_widget_grab_default(btnNext);
990
991         /* Button handlers */
992         g_signal_connect(G_OBJECT(btnPrev), "clicked",
993                          G_CALLBACK(imp_ldif_prev), NULL);
994         g_signal_connect(G_OBJECT(btnNext), "clicked",
995                          G_CALLBACK(imp_ldif_next), NULL);
996         g_signal_connect(G_OBJECT(btnProceed), "clicked",
997                          G_CALLBACK(imp_ldif_next), NULL);
998         g_signal_connect(G_OBJECT(btnCancel), "clicked",
999                          G_CALLBACK(imp_ldif_cancel), NULL);
1000
1001         gtk_widget_show_all(vbox);
1002
1003         impldif_dlg.window     = window;
1004         impldif_dlg.notebook   = notebook;
1005         impldif_dlg.btnPrev    = btnPrev;
1006         impldif_dlg.btnNext    = btnNext;
1007         impldif_dlg.btnProceed = btnProceed;
1008         impldif_dlg.btnCancel  = btnCancel;
1009         impldif_dlg.statusbar  = statusbar;
1010         impldif_dlg.status_cid = gtk_statusbar_get_context_id(
1011                         GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
1012
1013 }
1014
1015 /**
1016  * Create import LDIF dialog.
1017  */
1018 static void imp_ldif_create() {
1019         imp_ldif_dialog_create();
1020         imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
1021         imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
1022         imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
1023         gtk_widget_show_all( impldif_dlg.window );
1024 }
1025
1026 /**
1027  * Import LDIF file.
1028  * \param  addrIndex Address index.
1029  * \return Address book file of imported data, or <i>NULL</i> if import
1030  *         was cancelled.
1031  */
1032 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
1033         _importedBook_ = NULL;
1034         _imp_addressIndex_ = addrIndex;
1035
1036         if( ! impldif_dlg.window )
1037                 imp_ldif_create();
1038
1039         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
1040                              GTK_STOCK_CANCEL);
1041         gtk_widget_hide(impldif_dlg.btnProceed);
1042         gtk_widget_show(impldif_dlg.btnNext);
1043
1044         impldif_dlg.cancelled = FALSE;
1045         gtk_widget_show(impldif_dlg.window);
1046         manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
1047         gtk_widget_grab_default(impldif_dlg.btnNext);
1048         gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), TRUE);
1049
1050         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), IMPORTLDIF_GUESS_NAME );
1051         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), "" );
1052         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
1053         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
1054         gtk_cmclist_clear( GTK_CMCLIST(impldif_dlg.clist_field) );
1055         gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
1056         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
1057         gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
1058         stock_pixbuf_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
1059                           &markxpm );
1060         imp_ldif_message();
1061         gtk_widget_grab_focus(impldif_dlg.entryFile);
1062
1063         impldif_dlg.rowIndSelect = -1;
1064         impldif_dlg.rowCount = 0;
1065         g_free( impldif_dlg.nameBook );
1066         g_free( impldif_dlg.fileName );
1067         impldif_dlg.nameBook = NULL;
1068         impldif_dlg.fileName = NULL;
1069
1070         _ldifFile_ = ldif_create();
1071         gtk_main();
1072         gtk_widget_hide(impldif_dlg.window);
1073         gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), FALSE);
1074         ldif_free( _ldifFile_ );
1075         _ldifFile_ = NULL;
1076         _imp_addressIndex_ = NULL;
1077
1078         g_free( impldif_dlg.nameBook );
1079         g_free( impldif_dlg.fileName );
1080         impldif_dlg.nameBook = NULL;
1081         impldif_dlg.fileName = NULL;
1082
1083         if( impldif_dlg.cancelled == TRUE ) return NULL;
1084         return _importedBook_;
1085 }
1086
1087 /*
1088  * ============================================================================
1089  * End of Source.
1090  * ============================================================================
1091  */
1092