2011-08-29 [colin] 3.7.10cvs7
[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_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         CLAWS_TIP_DECL();
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         CLAWS_TIP_DECL();
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