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