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