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