Sync with hiro's cvs 10 to 17.
[claws.git] / src / importldif.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 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  * Edit VCard 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 <gdk/gdkkeysyms.h>
32 #include <gtk/gtkwindow.h>
33 #include <gtk/gtksignal.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtktable.h>
37 #include <gtk/gtkbutton.h>
38
39 #include "intl.h"
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
50 #define IMPORTLDIF_GUESS_NAME "LDIF Import"
51
52 #define PAGE_FILE_INFO             0
53 #define PAGE_ATTRIBUTES            1
54 #define PAGE_FINISH                2
55
56 #define IMPORTLDIF_WIDTH           380
57 #define IMPORTLDIF_HEIGHT          300
58
59 #define FIELDS_N_COLS              3
60 #define FIELDS_COL_WIDTH_SELECT    10
61 #define FIELDS_COL_WIDTH_FIELD     140
62 #define FIELDS_COL_WIDTH_ATTRIB    140
63
64 typedef enum {
65         FIELD_COL_SELECT  = 0,
66         FIELD_COL_FIELD   = 1,
67         FIELD_COL_ATTRIB  = 2
68 } ImpLdif_FieldColPos;
69
70 static struct _ImpLdif_Dlg {
71         GtkWidget *window;
72         GtkWidget *notebook;
73         GtkWidget *file_entry;
74         GtkWidget *name_entry;
75         GtkWidget *clist_field;
76         GtkWidget *name_ldif;
77         GtkWidget *name_attrib;
78         GtkWidget *check_select;
79         GtkWidget *labelBook;
80         GtkWidget *labelFile;
81         GtkWidget *labelRecords;
82         GtkWidget *btnPrev;
83         GtkWidget *btnNext;
84         GtkWidget *btnCancel;
85         GtkWidget *statusbar;
86         gint      status_cid;
87         gint      rowIndSelect;
88         gint      rowCount;
89         gchar     *nameBook;
90         gchar     *fileName;
91         gboolean  cancelled;
92 } impldif_dlg;
93
94 static struct _AddressFileSelection _imp_ldif_file_selector_;
95 static AddressBookFile *_importedBook_;
96 static AddressIndex *_imp_addressIndex_;
97 static LdifFile *_ldifFile_ = NULL;
98
99 static GdkPixmap *markxpm;
100 static GdkBitmap *markxpmmask;
101
102 static void imp_ldif_status_show( gchar *msg ) {
103         if( impldif_dlg.statusbar != NULL ) {
104                 gtk_statusbar_pop( GTK_STATUSBAR(impldif_dlg.statusbar), impldif_dlg.status_cid );
105                 if( msg ) {
106                         gtk_statusbar_push( GTK_STATUSBAR(impldif_dlg.statusbar), impldif_dlg.status_cid, msg );
107                 }
108         }
109 }
110
111 static void imp_ldif_message( void ) {
112         gchar *sMsg = NULL;
113         gint pageNum;
114
115         pageNum = gtk_notebook_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
116         if( pageNum == PAGE_FILE_INFO ) {
117                 sMsg = _( "Please specify address book name and file to import." );
118         }
119         else if( pageNum == PAGE_ATTRIBUTES ) {
120                 sMsg = _( "Select and rename LDIF field names to import." );
121         }
122         else if( pageNum == PAGE_FINISH ) {
123                 sMsg = _( "File imported." );
124         }
125         imp_ldif_status_show( sMsg );
126 }
127
128 static gchar *imp_ldif_guess_file( AddressBookFile *abf ) {
129         gchar *newFile = NULL;
130         GList *fileList = NULL;
131         gint fileNum = 1;
132         fileList = addrbook_get_bookfile_list( abf );
133         if( fileList ) {
134                 fileNum = 1 + abf->maxValue;
135         }
136         newFile = addrbook_gen_new_file_name( fileNum );
137         g_list_free( fileList );
138         fileList = NULL;
139         return newFile;
140 }
141
142 static void imp_ldif_update_row( GtkCList *clist ) {
143         Ldif_FieldRec *rec;
144         gchar *text[ FIELDS_N_COLS ];
145         gint row;
146
147         if( impldif_dlg.rowIndSelect < 0 ) return;
148         row = impldif_dlg.rowIndSelect;
149
150         rec = gtk_clist_get_row_data( clist, row );
151         text[ FIELD_COL_SELECT ] = "";
152         text[ FIELD_COL_FIELD  ] = rec->tagName;
153         text[ FIELD_COL_ATTRIB ] = rec->userName;
154
155         gtk_clist_freeze( clist );
156         gtk_clist_remove( clist, row );
157         if( row == impldif_dlg.rowCount - 1 ) {
158                 gtk_clist_append( clist, text );
159         }
160         else {
161                 gtk_clist_insert( clist, row, text );
162         }
163         if( rec->selected )
164                 gtk_clist_set_pixmap( clist, row, FIELD_COL_SELECT, markxpm, markxpmmask );
165
166         gtk_clist_set_row_data( clist, row, rec );
167         gtk_clist_thaw( clist );
168 }
169
170 static void imp_ldif_load_fields( LdifFile *ldf ) {
171         GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field);
172         GList *node, *list;
173         gchar *text[ FIELDS_N_COLS ];
174
175         impldif_dlg.rowIndSelect = -1;
176         impldif_dlg.rowCount = 0;
177         if( ! ldf->accessFlag ) return;
178         gtk_clist_clear( clist );
179         list = ldif_get_fieldlist( ldf );
180         node = list;
181         while( node ) {
182                 Ldif_FieldRec *rec = node->data;
183                 gint row;
184
185                 if( ! rec->reserved ) {
186                         text[ FIELD_COL_SELECT ] = "";
187                         text[ FIELD_COL_FIELD  ] = rec->tagName;
188                         text[ FIELD_COL_ATTRIB ] = rec->userName;
189                         row = gtk_clist_append( clist, text );
190                         gtk_clist_set_row_data( clist, row, rec );
191                         if( rec->selected )
192                                 gtk_clist_set_pixmap( clist, row, FIELD_COL_SELECT, markxpm, markxpmmask );
193                         impldif_dlg.rowCount++;
194                 }
195                 node = g_list_next( node );
196         }
197         g_list_free( list );
198         list = NULL;
199         ldif_set_accessed( ldf, FALSE );
200 }
201
202 static void imp_ldif_field_list_selected( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) {
203         Ldif_FieldRec *rec = gtk_clist_get_row_data( clist, row );
204
205         impldif_dlg.rowIndSelect = row;
206         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
207         if( rec ) {
208                 gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), rec->tagName );
209                 if( rec->userName )
210                         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), rec->userName );
211                 gtk_toggle_button_set_active(
212                         GTK_TOGGLE_BUTTON( impldif_dlg.check_select),
213                         rec->selected );
214         }
215         gtk_widget_grab_focus(impldif_dlg.name_attrib);
216 }
217
218 static void imp_ldif_field_list_toggle( GtkCList *clist, GdkEventButton *event, gpointer data ) {
219         if( ! event ) return;
220         if( impldif_dlg.rowIndSelect < 0 ) return;
221         if( event->button == 1 ) {
222                 if( event->type == GDK_2BUTTON_PRESS ) {
223                         Ldif_FieldRec *rec = gtk_clist_get_row_data( clist, impldif_dlg.rowIndSelect );
224                         if( rec ) {
225                                 rec->selected = ! rec->selected;
226                                 imp_ldif_update_row( clist );
227                         }
228                 }
229         }
230 }
231
232 static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
233         GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field);
234         Ldif_FieldRec *rec;
235         gint row;
236
237         if( impldif_dlg.rowIndSelect < 0 ) return;
238         row = impldif_dlg.rowIndSelect;
239         rec = gtk_clist_get_row_data( clist, impldif_dlg.rowIndSelect );
240
241         g_free( rec->userName );
242         rec->userName = g_strdup( gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_attrib), 0, -1 ) );
243         rec->selected = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( impldif_dlg.check_select) );
244         imp_ldif_update_row( clist );
245         gtk_clist_select_row( clist, row, 0 );
246         gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), "" );
247         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
248         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( impldif_dlg.check_select), FALSE );
249 }
250
251 /*
252 * Move off fields page.
253 * return: TRUE if OK to move off page.
254 */
255 static gboolean imp_ldif_field_move() {
256         gboolean retVal = FALSE;
257         gchar *newFile;
258         AddressBookFile *abf = NULL;
259
260         if( _importedBook_ ) {
261                 addrbook_free_book( _importedBook_ );
262         }
263
264         abf = addrbook_create_book();
265         addrbook_set_path( abf, _imp_addressIndex_->filePath );
266         addrbook_set_name( abf, impldif_dlg.nameBook );
267         newFile = imp_ldif_guess_file( abf );
268         addrbook_set_file( abf, newFile );
269         g_free( newFile );
270
271         /* Import data into file */
272         if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
273                 addrbook_save_data( abf );
274                 abf->dirtyFlag = TRUE;
275                 _importedBook_ = abf;
276                 retVal = TRUE;
277         }
278         else {
279                 addrbook_free_book( abf );
280         }
281
282         return retVal;
283 }
284
285 /*
286 * Move off fields page.
287 * return: TRUE if OK to move off page.
288 */
289 static gboolean imp_ldif_file_move() {
290         gboolean retVal = FALSE;
291         gchar *sName;
292         gchar *sFile;
293         gchar *sMsg = NULL;
294         gboolean errFlag = FALSE;
295
296         sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.file_entry), 0, -1 );
297         g_strchug( sFile ); g_strchomp( sFile );
298
299         sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_entry), 0, -1 );
300         g_strchug( sName ); g_strchomp( sName );
301
302         g_free( impldif_dlg.nameBook );
303         g_free( impldif_dlg.fileName );
304         impldif_dlg.nameBook = sName;
305         impldif_dlg.fileName = sFile;
306
307         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), sFile );
308         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_entry), sName );
309
310         if( *sFile == '\0'|| strlen( sFile ) < 1 ) {
311                 sMsg = _( "Please select a file." );
312                 gtk_widget_grab_focus(impldif_dlg.file_entry);
313                 errFlag = TRUE;
314         }
315
316         if( *sName == '\0'|| strlen( sName ) < 1 ) {
317                 if( ! errFlag ) sMsg = _( "Address book name must be supplied." );
318                 gtk_widget_grab_focus(impldif_dlg.name_entry);
319                 errFlag = TRUE;
320         }
321
322         if( ! errFlag ) {
323                 /* Read attribute list */
324                 ldif_set_file( _ldifFile_, sFile );
325                 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
326                         /* Load fields */
327                         /* ldif_print_file( _ldifFile_, stdout ); */
328                         imp_ldif_load_fields( _ldifFile_ );
329                         retVal = TRUE;
330                 }
331                 else {
332                         sMsg = _( "Error reading LDIF fields." );
333                 }
334         }
335         imp_ldif_status_show( sMsg );
336
337         return retVal;
338 }
339
340 /*
341  * Display finish page.
342  */
343 static void imp_ldif_finish_show() {
344         gchar *recs;
345         gchar *sMsg;
346
347         recs = g_strdup_printf( "%d", _ldifFile_->importCount );
348         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook),
349                 gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_entry), 0, -1 ) );
350         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
351         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), recs );
352         g_free( recs );
353         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
354         gtk_widget_set_sensitive( impldif_dlg.btnNext, FALSE );
355         if( _ldifFile_->retVal == MGU_SUCCESS ) {
356                 sMsg = _( "LDIF file imported successfully." );
357         }
358         else {
359                 sMsg = mgu_error2string( _ldifFile_->retVal );
360         }
361         imp_ldif_status_show( sMsg );
362         gtk_widget_grab_focus(impldif_dlg.btnCancel);
363 }
364
365 static void imp_ldif_prev( GtkWidget *widget ) {
366         gint pageNum;
367
368         pageNum = gtk_notebook_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
369         if( pageNum == PAGE_ATTRIBUTES ) {
370                 /* Goto file page stuff */
371                 gtk_notebook_set_page(
372                         GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
373                 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
374         }
375         imp_ldif_message();
376 }
377
378 static void imp_ldif_next( GtkWidget *widget ) {
379         gint pageNum;
380
381         pageNum = gtk_notebook_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
382         if( pageNum == PAGE_FILE_INFO ) {
383                 /* Goto attributes stuff */
384                 if( imp_ldif_file_move() ) {
385                         gtk_notebook_set_page(
386                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
387                         imp_ldif_message();
388                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
389                 }
390                 else {
391                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
392                 }
393         }
394         else if( pageNum == PAGE_ATTRIBUTES ) {
395                 /* Goto finish stuff */
396                 if( imp_ldif_field_move() ) {
397                         gtk_notebook_set_page(
398                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
399                         imp_ldif_finish_show();
400                 }
401         }
402 }
403
404 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
405         gint pageNum;
406
407         pageNum = gtk_notebook_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
408         if( pageNum != PAGE_FINISH ) {
409                 impldif_dlg.cancelled = TRUE;
410         }
411         gtk_main_quit();
412 }
413
414 static void imp_ldif_file_ok( GtkWidget *widget, gpointer data ) {
415         gchar *sFile;
416         AddressFileSelection *afs;
417         GtkWidget *fileSel;
418
419         afs = ( AddressFileSelection * ) data;
420         fileSel = afs->fileSelector;
421         sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
422
423         afs->cancelled = FALSE;
424         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), sFile );
425         gtk_widget_hide( afs->fileSelector );
426         gtk_grab_remove( afs->fileSelector );
427         gtk_widget_grab_focus( impldif_dlg.file_entry );
428 }
429
430 static void imp_ldif_file_cancel( GtkWidget *widget, gpointer data ) {
431         AddressFileSelection *afs = ( AddressFileSelection * ) data;
432         afs->cancelled = TRUE;
433         gtk_widget_hide( afs->fileSelector );
434         gtk_grab_remove( afs->fileSelector );
435         gtk_widget_grab_focus( impldif_dlg.file_entry );
436 }
437
438 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
439         GtkWidget *fileSelector;
440
441         fileSelector = gtk_file_selection_new( _("Select LDIF File") );
442         gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
443         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
444                 "clicked", GTK_SIGNAL_FUNC (imp_ldif_file_ok), ( gpointer ) afs );
445         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
446                 "clicked", GTK_SIGNAL_FUNC (imp_ldif_file_cancel), ( gpointer ) afs );
447         afs->fileSelector = fileSelector;
448         afs->cancelled = TRUE;
449 }
450
451 static void imp_ldif_file_select( void ) {
452         gchar *sFile;
453         if( ! _imp_ldif_file_selector_.fileSelector )
454                 imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
455
456         sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.file_entry), 0, -1 );
457         gtk_file_selection_set_filename(
458                 GTK_FILE_SELECTION( _imp_ldif_file_selector_.fileSelector ),
459                 sFile );
460         g_free( sFile );
461         gtk_widget_show( _imp_ldif_file_selector_.fileSelector );
462         gtk_grab_add( _imp_ldif_file_selector_.fileSelector );
463 }
464
465 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
466         *cancelled = TRUE;
467         gtk_main_quit();
468         return TRUE;
469 }
470
471 static void imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
472         if (event && event->keyval == GDK_Escape) {
473                 imp_ldif_cancel( widget, data );
474         }
475 }
476
477 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
478         GtkWidget *vbox;
479         GtkWidget *table;
480         GtkWidget *label;
481         GtkWidget *file_entry;
482         GtkWidget *name_entry;
483         GtkWidget *file_btn;
484         gint top;
485
486         vbox = gtk_vbox_new(FALSE, 8);
487         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
488         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
489
490         label = gtk_label_new( pageLbl );
491         gtk_widget_show( label );
492         gtk_notebook_set_tab_label(
493                 GTK_NOTEBOOK( impldif_dlg.notebook ),
494                 gtk_notebook_get_nth_page(
495                         GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
496                 label );
497
498         table = gtk_table_new(2, 3, FALSE);
499         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
500         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
501         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
502         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
503
504         /* First row */
505         top = 0;
506         label = gtk_label_new(_("Address Book"));
507         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
508                 GTK_FILL, 0, 0, 0);
509         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
510
511         name_entry = gtk_entry_new();
512         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1),
513                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
514
515         /* Second row */
516         top = 1;
517         label = gtk_label_new(_("File Name"));
518         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
519                 GTK_FILL, 0, 0, 0);
520         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
521
522         file_entry = gtk_entry_new();
523         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1),
524                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
525
526         file_btn = gtk_button_new_with_label( _(" ... "));
527         gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1),
528                 GTK_FILL, 0, 3, 0);
529
530         gtk_widget_show_all(vbox);
531
532         /* Button handler */
533         gtk_signal_connect(GTK_OBJECT(file_btn), "clicked",
534                            GTK_SIGNAL_FUNC(imp_ldif_file_select), NULL);
535
536         impldif_dlg.file_entry = file_entry;
537         impldif_dlg.name_entry = name_entry;
538 }
539
540 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
541         GtkWidget *vbox;
542         GtkWidget *vboxt;
543         GtkWidget *vboxb;
544         GtkWidget *buttonMod;
545
546         GtkWidget *table;
547         GtkWidget *label;
548         GtkWidget *clist_swin;
549         GtkWidget *clist_field;
550         GtkWidget *name_ldif;
551         GtkWidget *name_attrib;
552         GtkWidget *check_select;
553         gint top;
554
555         gchar *titles[ FIELDS_N_COLS ] =
556                 { _("S"), _("LDIF Field"), _("Attribute Name") };
557         gint i;
558
559         vbox = gtk_vbox_new(FALSE, 8);
560         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
561         gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
562
563         label = gtk_label_new( pageLbl );
564         gtk_widget_show( label );
565         gtk_notebook_set_tab_label(
566                 GTK_NOTEBOOK( impldif_dlg.notebook ),
567                 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
568                 label );
569
570         /* Upper area - Field list */
571         vboxt = gtk_vbox_new( FALSE, 4 );
572         gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
573
574         clist_swin = gtk_scrolled_window_new( NULL, NULL );
575         gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
576         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
577                                        GTK_POLICY_AUTOMATIC,
578                                        GTK_POLICY_ALWAYS);
579
580         clist_field = gtk_clist_new_with_titles( FIELDS_N_COLS, titles );
581         gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
582         gtk_clist_set_selection_mode( GTK_CLIST(clist_field), GTK_SELECTION_BROWSE );
583         gtk_clist_set_column_width(
584                         GTK_CLIST(clist_field), FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
585         gtk_clist_set_column_width(
586                         GTK_CLIST(clist_field), FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
587         gtk_clist_set_column_width(
588                         GTK_CLIST(clist_field), FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
589
590         for( i = 0; i < FIELDS_N_COLS; i++ )
591                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_field)->column[i].button, GTK_CAN_FOCUS);
592
593         /* Lower area - Edit area */
594         vboxb = gtk_vbox_new( FALSE, 4 );
595         gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
596
597         /* Data entry area */
598         table = gtk_table_new( 3, 3, FALSE);
599         gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
600         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
601         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
602
603         /* First row */
604         top = 0;
605         label = gtk_label_new(_("LDIF Field"));
606         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
607         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
608
609         name_ldif = gtk_label_new( "" );
610         gtk_misc_set_alignment(GTK_MISC(name_ldif), 0.01, 0.5);
611         gtk_table_attach(GTK_TABLE(table), name_ldif, 1, 3, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
612
613         /* Second row */
614         ++top;
615         label = gtk_label_new(_("Attribute"));
616         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
617         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
618
619         name_attrib = gtk_entry_new();
620         gtk_table_attach(GTK_TABLE(table), name_attrib, 1, 3, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
621
622         /* Next row */
623         ++top;
624         label = gtk_label_new(_("Select"));
625         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
626         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
627
628         check_select = gtk_check_button_new();
629         gtk_table_attach(GTK_TABLE(table), check_select, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
630
631         buttonMod = gtk_button_new_with_label( _("Modify"));
632         gtk_table_attach(GTK_TABLE(table), buttonMod, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
633
634         gtk_widget_show_all(vbox);
635
636         /* Event handlers */
637         gtk_signal_connect( GTK_OBJECT(clist_field), "select_row",
638                         GTK_SIGNAL_FUNC(imp_ldif_field_list_selected), NULL );
639         gtk_signal_connect( GTK_OBJECT(clist_field), "button_press_event",
640                         GTK_SIGNAL_FUNC(imp_ldif_field_list_toggle), NULL );
641         gtk_signal_connect( GTK_OBJECT(buttonMod), "clicked",
642                         GTK_SIGNAL_FUNC(imp_ldif_modify_pressed), NULL );
643
644         impldif_dlg.clist_field  = clist_field;
645         impldif_dlg.name_ldif    = name_ldif;
646         impldif_dlg.name_attrib  = name_attrib;
647         impldif_dlg.check_select = check_select;
648 }
649
650 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
651         GtkWidget *vbox;
652         GtkWidget *table;
653         GtkWidget *label;
654         GtkWidget *labelBook;
655         GtkWidget *labelFile;
656         GtkWidget *labelRecs;
657         gint top;
658
659         vbox = gtk_vbox_new(FALSE, 8);
660         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
661         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
662
663         label = gtk_label_new( pageLbl );
664         gtk_widget_show( label );
665         gtk_notebook_set_tab_label(
666                 GTK_NOTEBOOK( impldif_dlg.notebook ),
667                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ), label );
668
669         table = gtk_table_new(3, 2, FALSE);
670         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
671         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
672         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
673         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
674
675         /* First row */
676         top = 0;
677         label = gtk_label_new(_("Address Book :"));
678         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
679         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
680
681         labelBook = gtk_label_new("");
682         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
683         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
684
685         /* Second row */
686         top++;
687         label = gtk_label_new(_("File Name :"));
688         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
689         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
690
691         labelFile = gtk_label_new("");
692         gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
693         gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
694
695         /* Third row */
696         top++;
697         label = gtk_label_new(_("Records :"));
698         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
699         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
700
701         labelRecs = gtk_label_new("");
702         gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
703         gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
704
705         impldif_dlg.labelBook    = labelBook;
706         impldif_dlg.labelFile    = labelFile;
707         impldif_dlg.labelRecords = labelRecs;
708 }
709
710 static void imp_ldif_dialog_create() {
711         GtkWidget *window;
712         GtkWidget *vbox;
713         GtkWidget *vnbox;
714         GtkWidget *notebook;
715         GtkWidget *hbbox;
716         GtkWidget *btnPrev;
717         GtkWidget *btnNext;
718         GtkWidget *btnCancel;
719         GtkWidget *hsbox;
720         GtkWidget *statusbar;
721
722         window = gtk_window_new(GTK_WINDOW_DIALOG);
723         gtk_widget_set_usize(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
724         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
725         gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
726         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
727         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
728         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
729                            GTK_SIGNAL_FUNC(imp_ldif_delete_event),
730                            NULL );
731         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
732                            GTK_SIGNAL_FUNC(imp_ldif_key_pressed),
733                            NULL );
734
735         vbox = gtk_vbox_new(FALSE, 4);
736         gtk_widget_show(vbox);
737         gtk_container_add(GTK_CONTAINER(window), vbox);
738
739         vnbox = gtk_vbox_new(FALSE, 4);
740         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
741         gtk_widget_show(vnbox);
742         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
743
744         /* Notebook */
745         notebook = gtk_notebook_new();
746         gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
747         gtk_widget_show(notebook);
748         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
749         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
750
751         /* Status line */
752         hsbox = gtk_hbox_new(FALSE, 0);
753         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
754         statusbar = gtk_statusbar_new();
755         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
756
757         /* Button panel */
758         gtkut_button_set_create(&hbbox, &btnPrev, _( "Prev" ),
759                                 &btnNext, _( "Next" ),
760                                 &btnCancel, _( "Cancel" ) );
761         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
762         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
763         gtk_widget_grab_default(btnNext);
764
765         /* Button handlers */
766         gtk_signal_connect(GTK_OBJECT(btnPrev), "clicked",
767                            GTK_SIGNAL_FUNC(imp_ldif_prev), NULL);
768         gtk_signal_connect(GTK_OBJECT(btnNext), "clicked",
769                            GTK_SIGNAL_FUNC(imp_ldif_next), NULL);
770         gtk_signal_connect(GTK_OBJECT(btnCancel), "clicked",
771                            GTK_SIGNAL_FUNC(imp_ldif_cancel), NULL);
772
773         gtk_widget_show_all(vbox);
774
775         impldif_dlg.window     = window;
776         impldif_dlg.notebook   = notebook;
777         impldif_dlg.btnPrev    = btnPrev;
778         impldif_dlg.btnNext    = btnNext;
779         impldif_dlg.btnCancel  = btnCancel;
780         impldif_dlg.statusbar  = statusbar;
781         impldif_dlg.status_cid = gtk_statusbar_get_context_id(
782                         GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
783
784 }
785
786 static void imp_ldif_create() {
787         imp_ldif_dialog_create();
788         imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
789         imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
790         imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
791         gtk_widget_show_all( impldif_dlg.window );
792 }
793
794 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
795         _importedBook_ = NULL;
796         _imp_addressIndex_ = addrIndex;
797
798         if( ! impldif_dlg.window )
799                 imp_ldif_create();
800         impldif_dlg.cancelled = FALSE;
801         gtk_widget_show(impldif_dlg.window);
802         manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
803         gtk_widget_grab_default(impldif_dlg.btnNext);
804
805         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_entry), IMPORTLDIF_GUESS_NAME );
806         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), "" );
807         gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), "" );
808         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
809         gtk_clist_clear( GTK_CLIST(impldif_dlg.clist_field) );
810         gtk_notebook_set_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
811         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
812         gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
813         stock_pixmap_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
814                           &markxpm, &markxpmmask );
815         imp_ldif_message();
816         gtk_widget_grab_focus(impldif_dlg.file_entry);
817
818         impldif_dlg.rowIndSelect = -1;
819         impldif_dlg.rowCount = 0;
820         g_free( impldif_dlg.nameBook );
821         g_free( impldif_dlg.fileName );
822         impldif_dlg.nameBook = NULL;
823         impldif_dlg.fileName = NULL;
824
825         _ldifFile_ = ldif_create();
826         gtk_main();
827         gtk_widget_hide(impldif_dlg.window);
828         ldif_free( _ldifFile_ );
829         _ldifFile_ = NULL;
830         _imp_addressIndex_ = NULL;
831
832         g_free( impldif_dlg.nameBook );
833         g_free( impldif_dlg.fileName );
834         impldif_dlg.nameBook = NULL;
835         impldif_dlg.fileName = NULL;
836
837         if( impldif_dlg.cancelled == TRUE ) return NULL;
838         return _importedBook_;
839 }
840
841 /*
842 * End of Source.
843 */
844