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