2005-09-10 [colin] 1.9.14cvs14
[claws.git] / src / expldifdlg.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 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  * Export address book to LDIF file.
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 "gtkutils.h"
41 #include "prefs_common.h"
42 #include "alertpanel.h"
43 #include "mgutils.h"
44 #include "addrcache.h"
45 #include "addressitem.h"
46 #include "exportldif.h"
47 #include "utils.h"
48 #include "manage_window.h"
49
50 #define PAGE_FILE_INFO             0
51 #define PAGE_DN                    1
52 #define PAGE_FINISH                2
53
54 #define EXPORTLDIF_WIDTH           480
55 #define EXPORTLDIF_HEIGHT          -1
56
57 /**
58  * Dialog object.
59  */
60 static struct _ExpLdif_Dlg {
61         GtkWidget *window;
62         GtkWidget *notebook;
63         GtkWidget *labelBook;
64         GtkWidget *entryLdif;
65         GtkWidget *entrySuffix;
66         GtkWidget *optmenuRDN;
67         GtkWidget *checkUseDN;
68         GtkWidget *checkEMail;
69         GtkWidget *labelOutBook;
70         GtkWidget *labelOutFile;
71         GtkWidget *btnPrev;
72         GtkWidget *btnNext;
73         GtkWidget *btnCancel;
74         GtkWidget *statusbar;
75         gint      status_cid;
76         gboolean  cancelled;
77 } expldif_dlg;
78
79 static struct _AddressFileSelection _exp_ldif_file_selector_;
80
81 static ExportLdifCtl *_exportCtl_ = NULL;
82 static AddressCache *_addressCache_ = NULL;
83
84 /**
85  * Display message in status field.
86  * \param msg Message to display.
87  */
88 static void export_ldif_status_show( gchar *msg ) {
89         if( expldif_dlg.statusbar != NULL ) {
90                 gtk_statusbar_pop(
91                         GTK_STATUSBAR(expldif_dlg.statusbar),
92                         expldif_dlg.status_cid );
93                 if( msg ) {
94                         gtk_statusbar_push(
95                                 GTK_STATUSBAR(expldif_dlg.statusbar),
96                                 expldif_dlg.status_cid, msg );
97                 }
98         }
99 }
100
101 /**
102  * Select and display status message appropriate for the page being displayed.
103  */
104 static void export_ldif_message( void ) {
105         gchar *sMsg = NULL;
106         gint pageNum;
107
108         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(expldif_dlg.notebook) );
109         if( pageNum == PAGE_FILE_INFO ) {
110                 sMsg = _( "Please specify output directory and LDIF filename to create." );
111         }
112         else if( pageNum == PAGE_DN ) {
113                 sMsg = _( "Specify parameters to format distinguished name." );
114         }
115         else if( pageNum == PAGE_FINISH ) {
116                 sMsg = _( "File exported successfully." );
117         }
118         export_ldif_status_show( sMsg );
119 }
120
121 /**
122  * Callback function to cancel LDIF file selection dialog.
123  * \param widget Widget (button).
124  * \param data   User data.
125  */
126 static void export_ldif_cancel( GtkWidget *widget, gpointer data ) {
127         gint pageNum;
128
129         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(expldif_dlg.notebook) );
130         if( pageNum != PAGE_FINISH ) {
131                 expldif_dlg.cancelled = TRUE;
132         }
133         gtk_main_quit();
134 }
135
136 /**
137  * Callback function to handle dialog close event.
138  * \param widget Widget (dialog).
139  * \param event  Event object.
140  * \param data   User data.
141  */
142 static gint export_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
143         export_ldif_cancel( widget, data );
144         return TRUE;
145 }
146
147 /**
148  * Callback function to respond to dialog key press events.
149  * \param widget Widget.
150  * \param event  Event object.
151  * \param data   User data.
152  */
153 static gboolean export_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
154         if (event && event->keyval == GDK_Escape) {
155                 export_ldif_cancel( widget, data );
156         }
157         return FALSE;
158 }
159
160 /**
161  * Test whether we can move off file page.
162  * \return <i>TRUE</i> if OK to move off page.
163  */
164 static gboolean exp_ldif_move_file( void ) {
165         gchar *sFile, *msg, *reason;
166         gboolean errFlag = FALSE;
167         AlertValue aval;
168
169         sFile = gtk_editable_get_chars( GTK_EDITABLE(expldif_dlg.entryLdif), 0, -1 );
170         g_strchug( sFile ); g_strchomp( sFile );
171         gtk_entry_set_text( GTK_ENTRY(expldif_dlg.entryLdif), sFile );
172         exportldif_parse_filespec( _exportCtl_, sFile );
173
174         /* Test that something was supplied */
175         if( *sFile == '\0'|| strlen( sFile ) < 1 ) {
176                 gtk_widget_grab_focus( expldif_dlg.entryLdif );
177                 errFlag = TRUE;
178         }
179         g_free( sFile );
180         if( errFlag ) return FALSE;
181
182         /* Test for directory */
183         if( exportldif_test_dir( _exportCtl_ ) ) {
184                 return TRUE;
185         }
186
187         /* Prompt to create */
188         msg = g_strdup_printf( _(
189                 "LDIF Output Directory '%s'\n" \
190                 "does not exist. OK to create new directory?" ),
191                 _exportCtl_->dirOutput );
192         aval = alertpanel( _("Create Directory" ),
193                 msg, GTK_STOCK_YES, GTK_STOCK_NO, NULL );
194         g_free( msg );
195         if( aval != G_ALERTDEFAULT ) return FALSE;
196
197         /* Create directory */
198         if( ! exportldif_create_dir( _exportCtl_ ) ) {
199                 reason = exportldif_get_create_msg( _exportCtl_ );
200                 msg = g_strdup_printf( _(
201                         "Could not create output directory for LDIF file:\n%s" ),
202                         reason );
203                 aval = alertpanel_full(_("Failed to Create Directory"), msg,
204                                        GTK_STOCK_CLOSE, NULL, NULL, FALSE,
205                                        NULL, ALERT_ERROR, G_ALERTDEFAULT);
206                 g_free( msg );
207                 return FALSE;
208         }
209
210         return TRUE;
211 }
212
213 /**
214  * Test whether we can move off distinguished name page.
215  * \return <i>TRUE</i> if OK to move off page.
216  */
217 static gboolean exp_ldif_move_dn( void ) {
218         gboolean retVal = FALSE;
219         gboolean errFlag = FALSE;
220         gchar *suffix;
221         GtkWidget *menu, *menuItem;
222         gint id;
223
224         /* Set suffix */
225         suffix = gtk_editable_get_chars( GTK_EDITABLE(expldif_dlg.entrySuffix), 0, -1 );
226         g_strchug( suffix ); g_strchomp( suffix );
227
228         /* Set RDN format */
229         menu = gtk_option_menu_get_menu( GTK_OPTION_MENU( expldif_dlg.optmenuRDN ) );
230         menuItem = gtk_menu_get_active( GTK_MENU( menu ) );
231         id = GPOINTER_TO_INT( gtk_object_get_user_data(GTK_OBJECT(menuItem)) );
232         exportldif_set_rdn( _exportCtl_, id );
233
234         exportldif_set_suffix( _exportCtl_, suffix );
235         exportldif_set_use_dn( _exportCtl_,
236                 gtk_toggle_button_get_active(
237                         GTK_TOGGLE_BUTTON( expldif_dlg.checkUseDN ) ) );
238         exportldif_set_exclude_email( _exportCtl_,
239                 gtk_toggle_button_get_active(
240                         GTK_TOGGLE_BUTTON( expldif_dlg.checkEMail ) ) );
241
242         if( *suffix == '\0' || strlen( suffix ) < 1 ) {
243                 AlertValue aval;
244
245                 aval = alertpanel(
246                         _( "Suffix was not supplied" ),
247                         _(
248                                 "A suffix is required if data is to be used " \
249                                 "for an LDAP server. Are you sure you wish " \
250                                 "to proceed without a suffix?"
251                          ),
252                         GTK_STOCK_YES, GTK_STOCK_NO, NULL );
253                 if( aval != G_ALERTDEFAULT ) {
254                         gtk_widget_grab_focus( expldif_dlg.entrySuffix );
255                         errFlag = TRUE;
256                 }
257         }
258
259         if( ! errFlag ) {
260                 /* Process export */
261                 exportldif_process( _exportCtl_, _addressCache_ );
262                 if( _exportCtl_->retVal == MGU_SUCCESS ) {
263                         retVal = TRUE;
264                 }
265                 else {
266                         export_ldif_status_show( _( "Error creating LDIF file" ) );
267                 }
268         }
269
270         return retVal;
271 }
272
273 /**
274  * Display finish page.
275  */
276 static void exp_ldif_finish_show( void ) {
277         gtk_label_set_text( GTK_LABEL(expldif_dlg.labelOutFile), _exportCtl_->path );
278         gtk_widget_set_sensitive( expldif_dlg.btnNext, FALSE );
279         gtk_widget_grab_focus( expldif_dlg.btnCancel );
280 }
281
282 /**
283  * Callback function to select previous page.
284  * \param widget Widget (button).
285  */
286 static void export_ldif_prev( GtkWidget *widget ) {
287         gint pageNum;
288
289         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(expldif_dlg.notebook) );
290         if( pageNum == PAGE_DN ) {
291                 /* Goto file page stuff */
292                 gtk_notebook_set_current_page(
293                         GTK_NOTEBOOK(expldif_dlg.notebook), PAGE_FILE_INFO );
294                 gtk_widget_set_sensitive( expldif_dlg.btnPrev, FALSE );
295         }
296         else if( pageNum == PAGE_FINISH ) {
297                 /* Goto format page */
298                 gtk_notebook_set_current_page(
299                         GTK_NOTEBOOK(expldif_dlg.notebook), PAGE_DN );
300                 gtk_widget_set_sensitive( expldif_dlg.btnNext, TRUE );
301         }
302         export_ldif_message();
303 }
304
305 /**
306  * Callback function to select next page.
307  * \param widget Widget (button).
308  */
309 static void export_ldif_next( GtkWidget *widget ) {
310         gint pageNum;
311
312         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(expldif_dlg.notebook) );
313         if( pageNum == PAGE_FILE_INFO ) {
314                 /* Goto distinguished name page */
315                 if( exp_ldif_move_file() ) {
316                         gtk_notebook_set_current_page(
317                                 GTK_NOTEBOOK(expldif_dlg.notebook), PAGE_DN );
318                         gtk_widget_set_sensitive( expldif_dlg.btnPrev, TRUE );
319                 }
320                 export_ldif_message();
321         }
322         else if( pageNum == PAGE_DN ) {
323                 /* Goto finish page */
324                 if( exp_ldif_move_dn() ) {
325                         gtk_notebook_set_current_page(
326                                 GTK_NOTEBOOK(expldif_dlg.notebook), PAGE_FINISH );
327                         exp_ldif_finish_show();
328                         exportldif_save_settings( _exportCtl_ );
329                         export_ldif_message();
330                 }
331         }
332 }
333
334 /**
335  * Callback function to accept LDIF file selection.
336  * \param widget Widget (button).
337  * \param data   User data.
338  */
339 static void exp_ldif_file_ok( GtkWidget *widget, gpointer data ) {
340         const gchar *sFile;
341         AddressFileSelection *afs;
342         GtkWidget *fileSel;
343
344         afs = ( AddressFileSelection * ) data;
345         fileSel = afs->fileSelector;
346         sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
347
348         afs->cancelled = FALSE;
349         gtk_entry_set_text( GTK_ENTRY(expldif_dlg.entryLdif), sFile );
350         gtk_widget_hide( afs->fileSelector );
351         gtk_grab_remove( afs->fileSelector );
352         gtk_widget_grab_focus( expldif_dlg.entryLdif );
353 }
354
355 /**
356  * Callback function to cancel LDIF file selection dialog.
357  * \param widget Widget (button).
358  * \param data   User data.
359  */
360 static void exp_ldif_file_cancel( GtkWidget *widget, gpointer data ) {
361         AddressFileSelection *afs = ( AddressFileSelection * ) data;
362         afs->cancelled = TRUE;
363         gtk_widget_hide( afs->fileSelector );
364         gtk_grab_remove( afs->fileSelector );
365         gtk_widget_grab_focus( expldif_dlg.entryLdif );
366 }
367
368 /**
369  * Create LDIF file selection dialog.
370  * \param afs Address file selection data.
371  */
372 static void exp_ldif_file_select_create( AddressFileSelection *afs ) {
373         GtkWidget *fileSelector;
374
375         fileSelector = gtk_file_selection_new( _("Select LDIF Output File") );
376         gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
377         gtk_file_selection_complete( GTK_FILE_SELECTION(fileSelector), "*.html" );
378         g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(fileSelector)->ok_button),
379                          "clicked", 
380                          G_CALLBACK(exp_ldif_file_ok), (gpointer)afs);
381         g_signal_connect(G_OBJECT(GTK_FILE_SELECTION(fileSelector)->cancel_button),
382                          "clicked", 
383                          G_CALLBACK(exp_ldif_file_cancel), (gpointer)afs);
384         afs->fileSelector = fileSelector;
385         afs->cancelled = TRUE;
386 }
387
388 /**
389  * Callback function to display LDIF file selection dialog.
390  */
391 static void exp_ldif_file_select( void ) {
392         gchar *sFile;
393         if( ! _exp_ldif_file_selector_.fileSelector )
394                 exp_ldif_file_select_create( & _exp_ldif_file_selector_ );
395
396         sFile = gtk_editable_get_chars( GTK_EDITABLE(expldif_dlg.entryLdif), 0, -1 );
397         gtk_file_selection_set_filename(
398                 GTK_FILE_SELECTION( _exp_ldif_file_selector_.fileSelector ),
399                 sFile );
400         g_free( sFile );
401         gtk_widget_show( _exp_ldif_file_selector_.fileSelector );
402         gtk_grab_add( _exp_ldif_file_selector_.fileSelector );
403 }
404
405 /**
406  * Format notebook file specification page.
407  * \param pageNum Page (tab) number.
408  * \param pageLbl Page (tab) label.
409  */
410 static void export_ldif_page_file( gint pageNum, gchar *pageLbl ) {
411         GtkWidget *vbox;
412         GtkWidget *table;
413         GtkWidget *label;
414         GtkWidget *labelBook;
415         GtkWidget *entryLdif;
416         GtkWidget *btnFile;
417         gint top;
418
419         vbox = gtk_vbox_new(FALSE, 8);
420         gtk_container_add( GTK_CONTAINER( expldif_dlg.notebook ), vbox );
421         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
422
423         label = gtk_label_new( pageLbl );
424         gtk_widget_show( label );
425         gtk_notebook_set_tab_label(
426                 GTK_NOTEBOOK( expldif_dlg.notebook ),
427                 gtk_notebook_get_nth_page(
428                         GTK_NOTEBOOK( expldif_dlg.notebook ), pageNum ),
429                 label );
430
431         table = gtk_table_new( 3, 3, FALSE );
432         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
433         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
434         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
435         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
436
437         /* First row */
438         top = 0;
439         label = gtk_label_new( _( "Address Book" ) );
440         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
441                 GTK_FILL, 0, 0, 0);
442         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
443
444         labelBook = gtk_label_new( "Address book name goes here" );
445         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1),
446                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
447         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
448
449         /* Second row */
450         top++;
451         label = gtk_label_new( _( "LDIF Output File" ) );
452         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
453                 GTK_FILL, 0, 0, 0);
454         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
455
456         entryLdif = gtk_entry_new();
457         gtk_table_attach(GTK_TABLE(table), entryLdif, 1, 2, top, (top + 1),
458                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
459
460         btnFile = gtk_button_new_with_label( _(" ... "));
461         gtk_table_attach(GTK_TABLE(table), btnFile, 2, 3, top, (top + 1),
462                 GTK_FILL, 0, 3, 0);
463
464         gtk_widget_show_all(vbox);
465
466         /* Button handler */
467         g_signal_connect(G_OBJECT(btnFile), "clicked",
468                          G_CALLBACK(exp_ldif_file_select), NULL);
469
470         expldif_dlg.labelBook = labelBook;
471         expldif_dlg.entryLdif = entryLdif;
472 }
473
474 /**
475  * Format notebook distinguished name page.
476  * \param pageNum Page (tab) number.
477  * \param pageLbl Page (tab) label.
478  */
479 static void export_ldif_page_dn( gint pageNum, gchar *pageLbl ) {
480         GtkWidget *vbox;
481         GtkWidget *table;
482         GtkWidget *label;
483         GtkWidget *entrySuffix;
484         GtkWidget *optmenuRDN;
485         GtkWidget *checkUseDN;
486         GtkWidget *checkEMail;
487         GtkWidget *menu;
488         GtkWidget *menuItem;
489         GtkTooltips *toolTip;
490         gint top;
491
492         vbox = gtk_vbox_new(FALSE, 8);
493         gtk_container_add( GTK_CONTAINER( expldif_dlg.notebook ), vbox );
494         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
495
496         label = gtk_label_new( pageLbl );
497         gtk_widget_show( label );
498         gtk_notebook_set_tab_label(
499                 GTK_NOTEBOOK( expldif_dlg.notebook ),
500                 gtk_notebook_get_nth_page(
501                         GTK_NOTEBOOK( expldif_dlg.notebook ), pageNum ),
502                 label );
503
504         table = gtk_table_new( 5, 2, FALSE );
505         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
506         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
507         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
508         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
509
510         /* First row */
511         top = 0;
512         label = gtk_label_new( _( "Suffix" ) );
513         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
514                 GTK_FILL, 0, 0, 0);
515         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
516
517         entrySuffix = gtk_entry_new();
518         gtk_table_attach(GTK_TABLE(table), entrySuffix, 1, 2, top, (top + 1),
519                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
520
521         toolTip = gtk_tooltips_new();
522         gtk_tooltips_set_tip(
523                 GTK_TOOLTIPS(toolTip), entrySuffix, _(
524                 "The suffix is used to create a \"Distinguished Name\" " \
525                 "(or DN) for an LDAP entry. Examples include:\n" \
526                 "  dc=sylpheed,dc=org\n" \
527                 "  ou=people,dc=domainname,dc=com\n" \
528                 "  o=Organization Name,c=Country\n"
529                 ), NULL );
530
531         /* Second row */
532         top++;
533         label = gtk_label_new( _( "Relative DN" ) );
534         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
535                 GTK_FILL, 0, 0, 0);
536         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
537
538         menu = gtk_menu_new();
539
540         menuItem = gtk_menu_item_new_with_label( _( "Unique ID" ) );
541         gtk_object_set_user_data( GTK_OBJECT( menuItem ),
542                         GINT_TO_POINTER( EXPORT_LDIF_ID_UID ) );
543         gtk_menu_append( GTK_MENU(menu), menuItem );
544         gtk_widget_show( menuItem );
545         toolTip = gtk_tooltips_new();
546         gtk_tooltips_set_tip(
547                 GTK_TOOLTIPS(toolTip), menuItem, _(
548                 "The address book Unique ID is used to create a DN that is " \
549                 "formatted similar to:\n" \
550                 "  uid=102376,ou=people,dc=sylpheed,dc=org"
551                 ), NULL );
552
553         menuItem = gtk_menu_item_new_with_label( _( "Display Name" ) );
554         gtk_object_set_user_data( GTK_OBJECT( menuItem ),
555                         GINT_TO_POINTER( EXPORT_LDIF_ID_DNAME ) );
556         gtk_menu_append( GTK_MENU(menu), menuItem );
557         gtk_widget_show( menuItem );
558         toolTip = gtk_tooltips_new();
559         gtk_tooltips_set_tip(
560                 GTK_TOOLTIPS(toolTip), menuItem, _(
561                 "The address book Display Name is used to create a DN that " \
562                 "is formatted similar to:\n" \
563                 "  cn=John Doe,ou=people,dc=sylpheed,dc=org"
564                 ), NULL );
565
566         menuItem = gtk_menu_item_new_with_label( _( "E-Mail Address" ) );
567         gtk_object_set_user_data( GTK_OBJECT( menuItem ),
568                         GINT_TO_POINTER( EXPORT_LDIF_ID_EMAIL ) );
569         gtk_menu_append( GTK_MENU(menu), menuItem );
570         gtk_widget_show( menuItem );
571         toolTip = gtk_tooltips_new();
572         gtk_tooltips_set_tip(
573                 GTK_TOOLTIPS(toolTip), menuItem, _(
574                 "The first E-Mail Address belonging to a person is used to " \
575                 "create a DN that is formatted similar to:\n" \
576                 "  mail=john.doe@domain.com,ou=people,dc=sylpheed,dc=org"
577                 ), NULL );
578
579         optmenuRDN = gtk_option_menu_new();
580         gtk_option_menu_set_menu( GTK_OPTION_MENU( optmenuRDN ), menu );
581
582         gtk_table_attach(GTK_TABLE(table), optmenuRDN, 1, 2, top, (top + 1),
583                 GTK_FILL, 0, 0, 0);
584
585         toolTip = gtk_tooltips_new();
586         gtk_tooltips_set_tip(
587                 GTK_TOOLTIPS(toolTip), optmenuRDN, _(
588                 "The LDIF file contains several data records that " \
589                 "are usually loaded into an LDAP server. Each data " \
590                 "record in the LDIF file is uniquely identified by " \
591                 "a \"Distinguished Name\" (or DN). The suffix is " \
592                 "appended to the \"Relative Distinguished Name\" "\
593                 "(or RDN) to create the DN. Please select one of " \
594                 "the available RDN options that will be used to " \
595                 "create the DN."
596                 ), NULL );
597
598         /* Third row */
599         top++;
600         checkUseDN = gtk_check_button_new_with_label(
601                         _( "Use DN attribute if present in data" ) );
602         gtk_table_attach(GTK_TABLE(table), checkUseDN, 1, 2, top, (top + 1),
603                 GTK_FILL, 0, 0, 0);
604
605         toolTip = gtk_tooltips_new();
606         gtk_tooltips_set_tip(
607                 GTK_TOOLTIPS(toolTip), checkUseDN, _(
608                 "The addressbook may contain entries that were " \
609                 "previously imported from an LDIF file. The " \
610                 "\"Distinguished Name\" (DN) user attribute, if " \
611                 "present in the address book data, may be used in " \
612                 "the exported LDIF file. The RDN selected above " \
613                 "will be used if the DN user attribute is not found."
614                 ), NULL );
615
616         /* Fourth row */
617         top++;
618         checkEMail = gtk_check_button_new_with_label(
619                         _( "Exclude record if no E-Mail Address" ) );
620         gtk_table_attach(GTK_TABLE(table), checkEMail, 1, 2, top, (top + 1),
621                 GTK_FILL, 0, 0, 0);
622
623         toolTip = gtk_tooltips_new();
624         gtk_tooltips_set_tip(
625                 GTK_TOOLTIPS(toolTip), checkEMail, _(
626                 "An addressbook may contain entries without " \
627                 "E-Mail Addresses. Check this option to ignore " \
628                 "these records."
629                 ), NULL );
630
631
632         gtk_widget_show_all(vbox);
633
634         expldif_dlg.entrySuffix = entrySuffix;
635         expldif_dlg.optmenuRDN  = optmenuRDN;
636         expldif_dlg.checkUseDN  = checkUseDN;
637         expldif_dlg.checkEMail  = checkEMail;
638 }
639
640 /**
641  * Format notebook finish page.
642  * \param pageNum Page (tab) number.
643  * \param pageLbl Page (tab) label.
644  */
645 static void export_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
646         GtkWidget *vbox;
647         GtkWidget *table;
648         GtkWidget *label;
649         GtkWidget *labelBook;
650         GtkWidget *labelFile;
651         gint top;
652
653         vbox = gtk_vbox_new(FALSE, 8);
654         gtk_container_add( GTK_CONTAINER( expldif_dlg.notebook ), vbox );
655         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
656
657         label = gtk_label_new( pageLbl );
658         gtk_widget_show( label );
659         gtk_notebook_set_tab_label(
660                 GTK_NOTEBOOK( expldif_dlg.notebook ),
661                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( expldif_dlg.notebook ), pageNum ), label );
662
663         table = gtk_table_new( 3, 3, FALSE );
664         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
665         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
666         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
667         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
668
669         /* First row */
670         top = 0;
671         label = gtk_label_new( _( "Address Book :" ) );
672         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
673         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
674
675         labelBook = gtk_label_new("Full name of address book goes here");
676         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
677         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
678
679         /* Second row */
680         top++;
681         label = gtk_label_new( _( "File Name :" ) );
682         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
683         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
684
685         labelFile = gtk_label_new("File name goes here");
686         gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
687         gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
688
689         gtk_widget_show_all(vbox);
690
691         expldif_dlg.labelOutBook = labelBook;
692         expldif_dlg.labelOutFile = labelFile;
693 }
694
695 /**
696  * Create main dialog decorations (excluding notebook pages).
697  */
698 static void export_ldif_dialog_create( void ) {
699         GtkWidget *window;
700         GtkWidget *vbox;
701         GtkWidget *vnbox;
702         GtkWidget *notebook;
703         GtkWidget *hbbox;
704         GtkWidget *btnPrev;
705         GtkWidget *btnNext;
706         GtkWidget *btnCancel;
707         GtkWidget *hsbox;
708         GtkWidget *statusbar;
709
710         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
711         gtk_widget_set_size_request(window, EXPORTLDIF_WIDTH, EXPORTLDIF_HEIGHT );
712         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
713         gtk_window_set_title( GTK_WINDOW(window),
714                 _("Export Address Book to LDIF File") );
715         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
716         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
717         g_signal_connect(G_OBJECT(window), "delete_event",
718                          G_CALLBACK(export_ldif_delete_event),
719                          NULL );
720         g_signal_connect(G_OBJECT(window), "key_press_event",
721                          G_CALLBACK(export_ldif_key_pressed),
722                          NULL );
723
724         vbox = gtk_vbox_new(FALSE, 4);
725         gtk_widget_show(vbox);
726         gtk_container_add(GTK_CONTAINER(window), vbox);
727
728         vnbox = gtk_vbox_new(FALSE, 4);
729         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
730         gtk_widget_show(vnbox);
731         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
732
733         /* Notebook */
734         notebook = gtk_notebook_new();
735         gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE ); /* Hide */
736         /* gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), TRUE ); */
737         gtk_widget_show(notebook);
738         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
739         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
740
741         /* Status line */
742         hsbox = gtk_hbox_new(FALSE, 0);
743         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
744         statusbar = gtk_statusbar_new();
745         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
746
747         /* Button panel */
748         gtkut_stock_button_set_create(&hbbox, &btnPrev, GTK_STOCK_GO_BACK,
749                                       &btnNext, GTK_STOCK_GO_FORWARD,
750                                       &btnCancel, GTK_STOCK_CANCEL);
751         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
752         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
753         gtk_widget_grab_default(btnNext);
754
755         /* Button handlers */
756         g_signal_connect(G_OBJECT(btnPrev), "clicked",
757                          G_CALLBACK(export_ldif_prev), NULL);
758         g_signal_connect(G_OBJECT(btnNext), "clicked",
759                          G_CALLBACK(export_ldif_next), NULL);
760         g_signal_connect(G_OBJECT(btnCancel), "clicked",
761                          G_CALLBACK(export_ldif_cancel), NULL);
762
763         gtk_widget_show_all(vbox);
764
765         expldif_dlg.window     = window;
766         expldif_dlg.notebook   = notebook;
767         expldif_dlg.btnPrev    = btnPrev;
768         expldif_dlg.btnNext    = btnNext;
769         expldif_dlg.btnCancel  = btnCancel;
770         expldif_dlg.statusbar  = statusbar;
771         expldif_dlg.status_cid = gtk_statusbar_get_context_id(
772                         GTK_STATUSBAR(statusbar), "Export LDIF Dialog" );
773 }
774
775 /**
776  * Create export LDIF dialog.
777  */
778 static void export_ldif_create( void ) {
779         export_ldif_dialog_create();
780         export_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
781         export_ldif_page_dn( PAGE_DN, _( "Distguished Name" ) );
782         export_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
783         gtk_widget_show_all( expldif_dlg.window );
784 }
785
786 /**
787  * Populate fields from control data.
788  * \param ctl   Export control data.
789  */
790 static void export_ldif_fill_fields( ExportLdifCtl *ctl ) {
791         gtk_entry_set_text( GTK_ENTRY(expldif_dlg.entryLdif), "" );
792         if( ctl->path ) {
793                 gtk_entry_set_text( GTK_ENTRY(expldif_dlg.entryLdif),
794                         ctl->path );
795         }
796         gtk_entry_set_text( GTK_ENTRY(expldif_dlg.entrySuffix), "" );
797         if( ctl->suffix ) {
798                 gtk_entry_set_text( GTK_ENTRY(expldif_dlg.entrySuffix),
799                         ctl->suffix );
800         }
801
802         gtk_option_menu_set_history(
803                 GTK_OPTION_MENU( expldif_dlg.optmenuRDN ), ctl->rdnIndex );
804         gtk_toggle_button_set_active(
805                 GTK_TOGGLE_BUTTON( expldif_dlg.checkUseDN ), ctl->useDN );
806         gtk_toggle_button_set_active(
807                 GTK_TOGGLE_BUTTON( expldif_dlg.checkEMail ), ctl->excludeEMail );
808 }
809
810 /**
811  * Process export address dialog.
812  * \param cache Address book/data source cache.
813  */
814 void addressbook_exp_ldif( AddressCache *cache ) {
815         /* Set references to control data */
816         _addressCache_ = cache;
817
818         _exportCtl_ = exportldif_create();
819         exportldif_load_settings( _exportCtl_ );
820
821         /* Setup GUI */
822         if( ! expldif_dlg.window )
823                 export_ldif_create();
824         expldif_dlg.cancelled = FALSE;
825         gtk_widget_show(expldif_dlg.window);
826         manage_window_set_transient(GTK_WINDOW(expldif_dlg.window));
827
828         gtk_label_set_text( GTK_LABEL(expldif_dlg.labelBook), cache->name );
829         gtk_label_set_text( GTK_LABEL(expldif_dlg.labelOutBook), cache->name );
830         export_ldif_fill_fields( _exportCtl_ );
831
832         gtk_widget_grab_default(expldif_dlg.btnNext);
833         gtk_notebook_set_current_page( GTK_NOTEBOOK(expldif_dlg.notebook), PAGE_FILE_INFO );
834         gtk_widget_set_sensitive( expldif_dlg.btnPrev, FALSE );
835         gtk_widget_set_sensitive( expldif_dlg.btnNext, TRUE );
836
837         export_ldif_message();
838         gtk_widget_grab_focus(expldif_dlg.entryLdif);
839
840         gtk_main();
841         gtk_widget_hide(expldif_dlg.window);
842         exportldif_free( _exportCtl_ );
843         _exportCtl_ = NULL;
844
845         _addressCache_ = NULL;
846 }
847
848 /*
849  * ============================================================================
850  * End of Source.
851  * ============================================================================
852  */
853