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