remove space in front of colon
[claws.git] / src / importldif.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2015 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  * Import LDIF address book data.
21  */
22
23 #ifdef HAVE_CONFIG_H
24 #  include "config.h"
25 #include "claws-features.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/gtk.h>
34
35 #include "addrbook.h"
36 #include "addressbook.h"
37 #include "addressitem.h"
38 #include "gtkutils.h"
39 #include "stock_pixmap.h"
40 #include "prefs_common.h"
41 #include "manage_window.h"
42 #include "mgutils.h"
43 #include "ldif.h"
44 #include "utils.h"
45 #include "filesel.h"
46
47 #define IMPORTLDIF_GUESS_NAME      "LDIF Import"
48
49 #define PAGE_FILE_INFO             0
50 #define PAGE_ATTRIBUTES            1
51 #define PAGE_FINISH                2
52
53 #define IMPORTLDIF_WIDTH           390
54 #define IMPORTLDIF_HEIGHT          300
55
56 #define FIELDS_N_COLS              4
57 #define FIELDS_COL_WIDTH_RESERVED  10
58 #define FIELDS_COL_WIDTH_SELECT    10
59 #define FIELDS_COL_WIDTH_FIELD     140
60 #define FIELDS_COL_WIDTH_ATTRIB    140
61
62 typedef enum {
63         FIELD_COL_RESERVED = 0,
64         FIELD_COL_SELECT   = 1,
65         FIELD_COL_FIELD    = 2,
66         FIELD_COL_ATTRIB   = 3
67 } ImpLdif_FieldColPos;
68
69 /**
70  * LDIF dialog definition.
71  */
72 static struct _ImpLdif_Dlg {
73         GtkWidget *window;
74         GtkWidget *notebook;
75         GtkWidget *entryFile;
76         GtkWidget *entryName;
77         GtkWidget *clist_field;
78         GtkWidget *entryField;
79         GtkWidget *entryAttrib;
80         GtkWidget *checkSelect;
81         GtkWidget *btnModify;
82         GtkWidget *labelBook;
83         GtkWidget *labelFile;
84         GtkWidget *labelRecords;
85         GtkWidget *btnPrev;
86         GtkWidget *btnNext;
87         GtkWidget *btnProceed;
88         GtkWidget *btnCancel;
89         GtkWidget *statusbar;
90         gint      status_cid;
91         gint      rowIndSelect;
92         gint      rowCount;
93         gchar     *nameBook;
94         gchar     *fileName;
95         gboolean  cancelled;
96 } impldif_dlg;
97
98 static struct _AddressFileSelection _imp_ldif_file_selector_;
99 static AddressBookFile *_importedBook_;
100 static AddressIndex *_imp_addressIndex_;
101 static LdifFile *_ldifFile_ = NULL;
102
103 static GdkPixbuf *markxpm;
104
105 /**
106  * Structure of error message table.
107  */
108 typedef struct _ErrMsgTableEntry ErrMsgTableEntry;
109 struct _ErrMsgTableEntry {
110         gint    code;
111         gchar   *description;
112 };
113
114 static gchar *_errMsgUnknown_ = N_( "Unknown" );
115
116 /**
117  * Lookup table of error messages for general errors. Note that a NULL
118  * description signifies the end of the table.
119  */
120 static ErrMsgTableEntry _lutErrorsLDIF_[] = {
121         { MGU_SUCCESS,          N_("Success") },
122         { MGU_BAD_ARGS,         N_("Bad arguments") },
123         { MGU_NO_FILE,          N_("File not specified") },
124         { MGU_OPEN_FILE,        N_("Error opening file") },
125         { MGU_ERROR_READ,       N_("Error reading file") },
126         { MGU_EOF,              N_("End of file encountered") },
127         { MGU_OO_MEMORY,        N_("Error allocating memory") },
128         { MGU_BAD_FORMAT,       N_("Bad file format") },
129         { MGU_ERROR_WRITE,      N_("Error writing to file") },
130         { MGU_OPEN_DIRECTORY,   N_("Error opening directory") },
131         { MGU_NO_PATH,          N_("No path specified") },
132         { 0,                    NULL }
133 };
134
135 /**
136  * Lookup message for specified error code.
137  * \param lut  Lookup table.
138  * \param code Code to lookup.
139  * \return Description associated to code.
140  */
141 static gchar *imp_ldif_err2string( ErrMsgTableEntry lut[], gint code ) {
142         gchar *desc = NULL;
143         ErrMsgTableEntry entry;
144         gint i;
145
146         for( i = 0; ; i++ ) {
147                 entry = lut[ i ];
148                 if( entry.description == NULL ) break;
149                 if( entry.code == code ) {
150                         desc = entry.description;
151                         break;
152                 }
153         }
154         if( ! desc ) {
155                 desc = _errMsgUnknown_;
156         }
157         return desc;
158 }
159
160 /**
161  * Display message in status field.
162  * \param msg Message to display.
163  */
164 static void imp_ldif_status_show( gchar *msg ) {
165         if( impldif_dlg.statusbar != NULL ) {
166                 gtk_statusbar_pop( GTK_STATUSBAR(impldif_dlg.statusbar),
167                         impldif_dlg.status_cid );
168                 if( msg ) {
169                         gtk_statusbar_push(
170                                 GTK_STATUSBAR(impldif_dlg.statusbar),
171                                 impldif_dlg.status_cid, msg );
172                 }
173         }
174 }
175
176 /**
177  * Select and display status message appropriate for the page being displayed.
178  */
179 static void imp_ldif_message( void ) {
180         gchar *sMsg = NULL;
181         gint pageNum;
182
183         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
184         if( pageNum == PAGE_FILE_INFO ) {
185                 sMsg = _( "Please specify address book name and file to import." );
186         }
187         else if( pageNum == PAGE_ATTRIBUTES ) {
188                 sMsg = _( "Select and rename LDIF field names to import." );
189         }
190         else if( pageNum == PAGE_FINISH ) {
191                 sMsg = _( "File imported." );
192         }
193         imp_ldif_status_show( sMsg );
194 }
195
196 /**
197  * Update list with data for current row.
198  * \param clist List to update.
199  */
200 static void imp_ldif_update_row( GtkCMCList *clist ) {
201         Ldif_FieldRec *rec;
202         gchar *text[ FIELDS_N_COLS ];
203         gint row;
204
205         if( impldif_dlg.rowIndSelect < 0 ) return;
206         row = impldif_dlg.rowIndSelect;
207
208         rec = gtk_cmclist_get_row_data( clist, row );
209         if (!rec)
210                 return;
211
212         text[ FIELD_COL_RESERVED ] = "";
213         text[ FIELD_COL_SELECT   ] = "";
214         text[ FIELD_COL_FIELD    ] = rec->tagName;
215         text[ FIELD_COL_ATTRIB   ] = rec->userName;
216
217         gtk_cmclist_freeze( clist );
218         gtk_cmclist_remove( clist, row );
219         if( row == impldif_dlg.rowCount - 1 ) {
220                 gtk_cmclist_append( clist, text );
221         }
222         else {
223                 gtk_cmclist_insert( clist, row, text );
224         }
225         if( rec->selected ) {
226                 gtk_cmclist_set_pixbuf(
227                         clist, row, FIELD_COL_SELECT, markxpm );
228         }
229         if( rec->reserved ) {
230                 gtk_cmclist_set_pixbuf(
231                         clist, row, FIELD_COL_RESERVED, markxpm );
232         }
233
234         gtk_cmclist_set_row_data( clist, row, rec );
235         gtk_cmclist_thaw( clist );
236 }
237
238 /**
239  * Load list with LDIF fields read from file.
240  * \param ldf LDIF control data.
241  */
242 static void imp_ldif_load_fields( LdifFile *ldf ) {
243         GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
244         GList *node, *list;
245         gchar *text[ FIELDS_N_COLS ];
246
247         impldif_dlg.rowIndSelect = -1;
248         impldif_dlg.rowCount = 0;
249         if( ! ldf->accessFlag ) return;
250         gtk_cmclist_clear( clist );
251         list = ldif_get_fieldlist( ldf );
252         node = list;
253         while( node ) {
254                 Ldif_FieldRec *rec = node->data;
255                 gint row;
256
257                 text[ FIELD_COL_RESERVED ] = "";
258                 text[ FIELD_COL_SELECT   ] = "";
259                 text[ FIELD_COL_FIELD    ] = rec->tagName;
260                 text[ FIELD_COL_ATTRIB   ] = rec->userName;
261                 row = gtk_cmclist_append( clist, text );
262                 gtk_cmclist_set_row_data( clist, row, rec );
263                 if( rec->selected ) {
264                         gtk_cmclist_set_pixbuf( clist, row,
265                                 FIELD_COL_SELECT, markxpm );
266                 }
267                 if( rec->reserved ) {
268                         gtk_cmclist_set_pixbuf( clist, row,
269                                 FIELD_COL_RESERVED, markxpm );
270                 }
271                 impldif_dlg.rowCount++;
272                 node = g_list_next( node );
273         }
274         g_list_free( list );
275         list = NULL;
276         ldif_set_accessed( ldf, FALSE );
277 }
278
279 /**
280  * Callback function when list item is selected.
281  * \param clist List widget.
282  * \param row   Row.
283  * \param col   Column.
284  * \param event Event object.
285  * \param data  User data.
286  */
287 static void imp_ldif_field_list_selected(
288                 GtkCMCList *clist, gint row, gint column, GdkEvent *event,
289                 gpointer data )
290 {
291         Ldif_FieldRec *rec = gtk_cmclist_get_row_data( clist, row );
292
293         impldif_dlg.rowIndSelect = row;
294         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
295         if( rec ) {
296                 /* Update widget contents */
297                 gtk_label_set_text(
298                         GTK_LABEL(impldif_dlg.entryField), rec->tagName );
299                 if( rec->userName )
300                         gtk_entry_set_text(
301                                 GTK_ENTRY(impldif_dlg.entryAttrib), rec->userName );
302                 gtk_toggle_button_set_active(
303                         GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect),
304                         rec->selected );
305
306                 /* Disable widgets for reserved fields */
307                 gtk_widget_set_sensitive(
308                         impldif_dlg.entryAttrib, ! rec->reserved );
309                 gtk_widget_set_sensitive(
310                         impldif_dlg.checkSelect, ! rec->reserved );
311                 gtk_widget_set_sensitive(
312                         impldif_dlg.btnModify,   ! rec->reserved );
313         }
314         gtk_widget_grab_focus(impldif_dlg.entryAttrib);
315 }
316
317 /**
318  * Callback function to toggle selected LDIF field.
319  * \param clist List to update.
320  * \param event Event object.
321  * \param data  Data.
322  */
323 static gboolean imp_ldif_field_list_toggle(
324                 GtkCMCList *clist, GdkEventButton *event, gpointer data )
325 {
326         Ldif_FieldRec *rec;
327         gboolean toggle = FALSE;
328
329         if( ! event ) return FALSE;
330         if( impldif_dlg.rowIndSelect < 0 ) return FALSE;
331         if( event->button == 1 ) {
332                 /* If single click in select column */
333                 if( event->type == GDK_BUTTON_PRESS ) {
334                         gint x = event->x;
335                         gint y = event->y;
336                         gint row, col;
337
338                         if (!gtk_cmclist_get_selection_info( clist, x, y, &row, &col ))
339                                 return FALSE;
340                         if( col != FIELD_COL_SELECT ) return FALSE;
341                         if( row > impldif_dlg.rowCount ) return FALSE;
342
343                         /* Set row */
344                         impldif_dlg.rowIndSelect = row;
345                         toggle = TRUE;
346                 }
347
348                 /* If double click anywhere in row */
349                 else if( event->type == GDK_2BUTTON_PRESS ) {
350                         toggle = TRUE;
351                 }
352         }
353         /* Toggle field selection */
354         if( toggle ) {
355                 rec = gtk_cmclist_get_row_data(
356                         clist, impldif_dlg.rowIndSelect );
357                 if( rec ) {
358                         ldif_field_toggle( rec );
359                         imp_ldif_update_row( clist );
360                 }
361         }
362         return FALSE;
363 }
364
365 /**
366  * Callback function to update LDIF field data from input fields.
367  * \param widget Widget (button).
368  * \param data   User data.
369  */
370 static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
371         GtkCMCList *clist = GTK_CMCLIST(impldif_dlg.clist_field);
372         Ldif_FieldRec *rec;
373         gint row;
374
375         if( impldif_dlg.rowIndSelect < 0 ) return;
376         row = impldif_dlg.rowIndSelect;
377         rec = gtk_cmclist_get_row_data( clist, impldif_dlg.rowIndSelect );
378
379         ldif_field_set_name( rec, gtk_editable_get_chars(
380                 GTK_EDITABLE(impldif_dlg.entryAttrib), 0, -1 ) );
381         ldif_field_set_selected( rec, gtk_toggle_button_get_active(
382                 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect) ) );
383         imp_ldif_update_row( clist );
384         gtk_cmclist_select_row( clist, row, 0 );
385         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
386         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
387         gtk_toggle_button_set_active(
388                 GTK_TOGGLE_BUTTON( impldif_dlg.checkSelect), FALSE );
389 }
390
391 /**
392  * Test whether we can move off fields page.
393  * \return <i>TRUE</i> if OK to move off page.
394  */
395 static gboolean imp_ldif_field_move() {
396         gboolean retVal = FALSE;
397         gchar *newFile;
398         AddressBookFile *abf = NULL;
399
400         if( _importedBook_ ) {
401                 addrbook_free_book( _importedBook_ );
402         }
403
404         abf = addrbook_create_book();
405         addrbook_set_path( abf, _imp_addressIndex_->filePath );
406         addrbook_set_name( abf, impldif_dlg.nameBook );
407         newFile = addrbook_guess_next_file( abf );
408         addrbook_set_file( abf, newFile );
409         g_free( newFile );
410
411         /* Import data into file */
412         if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
413                 addrbook_save_data( abf );
414                 _importedBook_ = abf;
415                 retVal = TRUE;
416         }
417         else {
418                 addrbook_free_book( abf );
419         }
420
421         return retVal;
422 }
423
424 /**
425  * Test whether we can move off file page.
426  * \return <i>TRUE</i> if OK to move off page.
427  */
428 static gboolean imp_ldif_file_move() {
429         gboolean retVal = FALSE;
430         gchar *sName;
431         gchar *sFile;
432         gchar *sMsg = NULL;
433         gboolean errFlag = FALSE;
434
435         sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryFile), 0, -1 );
436         g_strstrip( sFile );
437
438         sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
439         g_strstrip( sName );
440
441         g_free( impldif_dlg.nameBook );
442         g_free( impldif_dlg.fileName );
443         impldif_dlg.nameBook = sName;
444         impldif_dlg.fileName = sFile;
445
446         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), sFile );
447         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), sName );
448
449         if( *sFile == '\0' ) {
450                 sMsg = _( "Please select a file." );
451                 gtk_widget_grab_focus(impldif_dlg.entryFile);
452                 errFlag = TRUE;
453         }
454
455         if( ! errFlag && *sName == '\0' ) {
456                 sMsg = _( "Address book name must be supplied." );
457                 gtk_widget_grab_focus(impldif_dlg.entryName);
458                 errFlag = TRUE;
459         }
460
461         if( ! errFlag ) {
462                 /* Read attribute list */
463                 ldif_set_file( _ldifFile_, sFile );
464                 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
465                         /* Load fields */
466                         /* ldif_print_file( _ldifFile_, stdout ); */
467                         imp_ldif_load_fields( _ldifFile_ );
468                         retVal = TRUE;
469                 }
470                 else {
471                         sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
472                 }
473         }
474         imp_ldif_status_show( sMsg );
475
476         return retVal;
477 }
478
479 /**
480  * Display finish page.
481  */
482 static void imp_ldif_finish_show() {
483         gchar *sMsg;
484         gchar *name;
485
486         name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
487         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
488         g_free( name );
489         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
490         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
491         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
492         gtk_widget_hide( impldif_dlg.btnNext );
493         gtk_widget_show( impldif_dlg.btnProceed );
494         gtk_widget_set_sensitive( impldif_dlg.btnProceed, FALSE );
495         if( _ldifFile_->retVal == MGU_SUCCESS ) {
496                 sMsg = _( "LDIF file imported successfully." );
497         }
498         else {
499                 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
500         }
501         imp_ldif_status_show( sMsg );
502         gtk_widget_grab_focus(impldif_dlg.btnCancel);
503 }
504
505 /**
506  * Callback function to select previous page.
507  * \param widget Widget (button).
508  */
509 static void imp_ldif_prev( GtkWidget *widget ) {
510         gint pageNum;
511
512         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
513         if( pageNum == PAGE_ATTRIBUTES ) {
514                 /* Goto file page stuff */
515                 gtk_notebook_set_current_page(
516                         GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
517                 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
518                 gtk_widget_hide( impldif_dlg.btnProceed );
519                 gtk_widget_show( impldif_dlg.btnNext );
520         }
521         imp_ldif_message();
522 }
523
524 /**
525  * Callback function to select next page.
526  * \param widget Widget (button).
527  */
528 static void imp_ldif_next( GtkWidget *widget ) {
529         gint pageNum;
530
531         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
532         if( pageNum == PAGE_FILE_INFO ) {
533                 /* Goto attributes stuff */
534                 if( imp_ldif_file_move() ) {
535                         gtk_notebook_set_current_page(
536                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
537                         imp_ldif_message();
538                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
539                         gtk_widget_hide( impldif_dlg.btnNext );
540                         gtk_widget_show( impldif_dlg.btnProceed );
541                         gtk_widget_set_sensitive( impldif_dlg.btnProceed, TRUE );
542                 }
543                 else {
544                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
545                         _ldifFile_->dirtyFlag = TRUE;
546                 }
547         }
548         else if( pageNum == PAGE_ATTRIBUTES ) {
549                 /* Goto finish stuff */
550                 if( imp_ldif_field_move() ) {
551                         gtk_notebook_set_current_page(
552                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
553                         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
554                                              GTK_STOCK_CLOSE);
555                         imp_ldif_finish_show();
556                 }
557         }
558 }
559
560 /**
561  * Callback function to cancel and close dialog.
562  * \param widget Widget (button).
563  * \param data   User data.
564  */
565 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
566         gint pageNum;
567
568         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
569         if( pageNum != PAGE_FINISH ) {
570                 impldif_dlg.cancelled = TRUE;
571         }
572         gtk_main_quit();
573 }
574
575
576 /**
577  * Create LDIF file selection dialog.
578  * \param afs Address file selection data.
579  */
580 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
581         gchar *file = filesel_select_file_open(_("Select LDIF File"), NULL);
582
583         if (file == NULL)
584                 afs->cancelled = TRUE;
585         else {
586                 afs->cancelled = FALSE;
587                 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), file );
588                 g_free(file);
589         }
590 }
591
592 /**
593  * Callback function to display LDIF file selection dialog.
594  */
595 static void imp_ldif_file_select( void ) {
596         imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
597 }
598
599 /**
600  * Callback function to handle dialog close event.
601  * \param widget Widget (dialog).
602  * \param event  Event object.
603  * \param data   User data.
604  */
605 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
606         imp_ldif_cancel( widget, data );
607         return TRUE;
608 }
609
610 /**
611  * Callback function to respond to dialog key press events.
612  * \param widget Widget.
613  * \param event  Event object.
614  * \param data   User data.
615  */
616 static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
617         if (event && event->keyval == GDK_KEY_Escape) {
618                 imp_ldif_cancel( widget, data );
619         }
620         return FALSE;
621 }
622
623 /**
624  * Format notebook "file" page.
625  * \param pageNum Page (tab) number.
626  * \param pageLbl Page (tab) label.
627  */
628 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
629         GtkWidget *vbox;
630         GtkWidget *table;
631         GtkWidget *label;
632         GtkWidget *entryFile;
633         GtkWidget *entryName;
634         GtkWidget *btnFile;
635         gint top;
636
637         vbox = gtk_vbox_new(FALSE, 8);
638         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
639         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
640
641         label = gtk_label_new( pageLbl );
642         gtk_widget_show( label );
643         gtk_notebook_set_tab_label(
644                 GTK_NOTEBOOK( impldif_dlg.notebook ),
645                 gtk_notebook_get_nth_page(
646                         GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
647                 label );
648
649         table = gtk_table_new(2, 3, FALSE);
650         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
651         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
652         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
653         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
654
655         /* First row */
656         top = 0;
657         label = gtk_label_new(_("Address Book"));
658         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
659                 GTK_FILL, 0, 0, 0);
660         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
661
662         entryName = gtk_entry_new();
663         gtk_table_attach(GTK_TABLE(table), entryName, 1, 2, top, (top + 1),
664                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
665
666         CLAWS_SET_TIP(entryName, _( 
667                 "Specify the name for the address book that will " \
668                 "be created from the LDIF file data." ));
669
670         /* Second row */
671         top = 1;
672         label = gtk_label_new(_("File Name"));
673         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
674                 GTK_FILL, 0, 0, 0);
675         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
676
677         entryFile = gtk_entry_new();
678         gtk_table_attach(GTK_TABLE(table), entryFile, 1, 2, top, (top + 1),
679                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
680
681         CLAWS_SET_TIP(entryFile,
682                 _( "The full file specification of the LDIF file to import." ));
683
684         btnFile = gtkut_get_browse_file_btn(_("B_rowse"));
685         gtk_table_attach(GTK_TABLE(table), btnFile, 2, 3, top, (top + 1),
686                 GTK_FILL, 0, 3, 0);
687
688         CLAWS_SET_TIP(btnFile,
689                 _( "Select the LDIF file to import." ));
690
691         gtk_widget_show_all(vbox);
692
693         /* Button handler */
694         g_signal_connect(G_OBJECT(btnFile), "clicked",
695                          G_CALLBACK(imp_ldif_file_select), NULL);
696
697         impldif_dlg.entryFile = entryFile;
698         impldif_dlg.entryName = entryName;
699
700 }
701
702 /**
703  * Format notebook fields page.
704  * \param pageNum Page (tab) number.
705  * \param pageLbl Page (tab) label.
706  */
707 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
708         GtkWidget *vbox;
709         GtkWidget *vboxt;
710         GtkWidget *vboxb;
711         GtkWidget *table;
712         GtkWidget *label;
713         GtkWidget *clist_swin;
714         GtkWidget *clist_field;
715         GtkWidget *entryField;
716         GtkWidget *entryAttrib;
717         GtkWidget *checkSelect;
718         GtkWidget *btnModify;
719         GtkWidget *eventBox;
720         gint top;
721
722         gchar *titles[ FIELDS_N_COLS ];
723         gint i;
724
725         titles[ FIELD_COL_RESERVED ] = _("R");
726         titles[ FIELD_COL_SELECT   ] = _("S");
727         titles[ FIELD_COL_FIELD    ] = _("LDIF Field Name");
728         titles[ FIELD_COL_ATTRIB   ] = _("Attribute Name");
729
730         vbox = gtk_vbox_new(FALSE, 8);
731         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
732         gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
733
734         label = gtk_label_new( pageLbl );
735         gtk_widget_show( label );
736         gtk_notebook_set_tab_label(
737                 GTK_NOTEBOOK( impldif_dlg.notebook ),
738                 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
739                 label );
740
741         /* Upper area - Field list */
742         vboxt = gtk_vbox_new( FALSE, 4 );
743         gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
744
745         clist_swin = gtk_scrolled_window_new( NULL, NULL );
746         gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
747         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
748                                        GTK_POLICY_AUTOMATIC,
749                                        GTK_POLICY_AUTOMATIC);
750
751         clist_field = gtk_cmclist_new_with_titles( FIELDS_N_COLS, titles );
752         gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
753         gtk_cmclist_set_selection_mode(
754                 GTK_CMCLIST(clist_field), GTK_SELECTION_BROWSE );
755         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
756                 FIELD_COL_RESERVED, FIELDS_COL_WIDTH_RESERVED );
757         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
758                 FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
759         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
760                 FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
761         gtk_cmclist_set_column_width( GTK_CMCLIST(clist_field),
762                 FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
763
764         /* Remove focus capability for column headers */
765         for( i = 0; i < FIELDS_N_COLS; i++ ) {
766                 gtkut_widget_set_can_focus(
767                         GTK_CMCLIST(clist_field)->column[i].button,
768                         FALSE);
769         }
770
771         /* Lower area - Edit area */
772         vboxb = gtk_vbox_new( FALSE, 4 );
773         gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
774
775         /* Data entry area */
776         table = gtk_table_new( 3, 3, FALSE);
777         gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
778         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
779         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
780
781         /* First row */
782         top = 0;
783         label = gtk_label_new(_("LDIF Field"));
784         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
785                 GTK_FILL, 0, 0, 0);
786         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
787
788         entryField = gtk_label_new( "" );
789         gtk_misc_set_alignment(GTK_MISC(entryField), 0.01, 0.5);
790         gtk_table_attach(GTK_TABLE(table), entryField, 1, 3, top, (top + 1),
791                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
792
793         /* Second row */
794         ++top;
795         label = gtk_label_new(_("Attribute"));
796         /*
797          * Use an event box to attach some help in the form of a tooltip.
798          * Tried this for the clist but it looked bad.
799          */
800         eventBox = gtk_event_box_new();
801         gtk_container_add( GTK_CONTAINER(eventBox), label );
802         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
803         gtk_table_attach(GTK_TABLE(table), eventBox, 0, 1, top, (top + 1),
804                 GTK_FILL, 0, 0, 0);
805
806         CLAWS_SET_TIP(eventBox, _(
807                 "Choose the LDIF field that will be renamed or selected " \
808                 "for import in the list above. Reserved fields (marked " \
809                 "with a tick in the \"R\" column), are automatically " \
810                 "imported and cannot be renamed. A single click in the " \
811                 "Select (\"S\") column will select the field for import " \
812                 "with a tick. A single click anywhere in the row will " \
813                 "select that field for rename in the input area below " \
814                 "the list. A double click anywhere in the row will also " \
815                 "select the field for import."));
816
817         entryAttrib = gtk_entry_new();
818         gtk_table_attach(GTK_TABLE(table), entryAttrib, 1, 3, top, (top + 1),
819                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
820
821         CLAWS_SET_TIP(entryAttrib,
822                 _( "The LDIF field can be renamed to the User Attribute name." ));
823
824         /* Next row */
825         ++top;
826
827         checkSelect = gtk_check_button_new_with_label( _( "Select for Import" ) );
828         gtk_table_attach(GTK_TABLE(table), checkSelect, 1, 2, top, (top + 1),
829                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
830
831         CLAWS_SET_TIP(checkSelect,
832                 _( "Select the LDIF field for import into the address book." ));
833
834         btnModify = gtk_button_new_with_label( _(" Modify "));
835         gtk_table_attach(GTK_TABLE(table), btnModify, 2, 3, top, (top + 1),
836                 GTK_FILL, 0, 3, 0);
837
838         CLAWS_SET_TIP(btnModify,
839                 _( "This button will update the list above with the data supplied." ));
840
841         gtk_widget_show_all(vbox);
842
843         /* Event handlers */
844         g_signal_connect( G_OBJECT(clist_field), "select_row",
845                           G_CALLBACK(imp_ldif_field_list_selected), NULL );
846         g_signal_connect( G_OBJECT(clist_field), "button_press_event",
847                           G_CALLBACK(imp_ldif_field_list_toggle), NULL );
848         g_signal_connect( G_OBJECT(btnModify), "clicked",
849                           G_CALLBACK(imp_ldif_modify_pressed), NULL );
850
851         impldif_dlg.clist_field = clist_field;
852         impldif_dlg.entryField  = entryField;
853         impldif_dlg.entryAttrib = entryAttrib;
854         impldif_dlg.checkSelect = checkSelect;
855         impldif_dlg.btnModify   = btnModify;
856 }
857
858 /**
859  * Format notebook finish page.
860  * \param pageNum Page (tab) number.
861  * \param pageLbl Page (tab) label.
862  */
863 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
864         GtkWidget *vbox;
865         GtkWidget *table;
866         GtkWidget *label;
867         GtkWidget *labelBook;
868         GtkWidget *labelFile;
869         GtkWidget *labelRecs;
870         gint top;
871
872         vbox = gtk_vbox_new(FALSE, 8);
873         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
874         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
875
876         label = gtk_label_new( pageLbl );
877         gtk_widget_show( label );
878         gtk_notebook_set_tab_label(
879                 GTK_NOTEBOOK( impldif_dlg.notebook ),
880                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
881                 label );
882
883         table = gtk_table_new(3, 2, FALSE);
884         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
885         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
886         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
887         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
888
889         /* First row */
890         top = 0;
891         label = gtk_label_new( _( "Address Book:" ) );
892         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
893         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
894
895         labelBook = gtk_label_new("");
896         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
897         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
898
899         /* Second row */
900         top++;
901         label = gtk_label_new( _( "File Name:" ) );
902         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
903         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
904
905         labelFile = gtk_label_new("");
906         gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
907         gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
908
909         /* Third row */
910         top++;
911         label = gtk_label_new( _("Records Imported:") );
912         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
913         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
914
915         labelRecs = gtk_label_new("");
916         gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
917         gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
918
919         impldif_dlg.labelBook    = labelBook;
920         impldif_dlg.labelFile    = labelFile;
921         impldif_dlg.labelRecords = labelRecs;
922 }
923
924 /**
925  * Create main dialog decorations (excluding notebook pages).
926  */
927 static void imp_ldif_dialog_create() {
928         GtkWidget *window;
929         GtkWidget *vbox;
930         GtkWidget *vnbox;
931         GtkWidget *notebook;
932         GtkWidget *hbbox;
933         GtkWidget *btnPrev;
934         GtkWidget *btnNext;
935         GtkWidget *btnProceed;
936         GtkWidget *btnCancel;
937         GtkWidget *hsbox;
938         GtkWidget *statusbar;
939
940         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importldif");
941         gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
942         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
943         gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
944         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
945         g_signal_connect(G_OBJECT(window), "delete_event",
946                          G_CALLBACK(imp_ldif_delete_event),
947                          NULL );
948         g_signal_connect(G_OBJECT(window), "key_press_event",
949                          G_CALLBACK(imp_ldif_key_pressed),
950                          NULL );
951
952         vbox = gtk_vbox_new(FALSE, 4);
953         gtk_widget_show(vbox);
954         gtk_container_add(GTK_CONTAINER(window), vbox);
955
956         vnbox = gtk_vbox_new(FALSE, 4);
957         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
958         gtk_widget_show(vnbox);
959         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
960
961         /* Notebook */
962         notebook = gtk_notebook_new();
963         gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
964         gtk_widget_show(notebook);
965         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
966         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
967
968         /* Status line */
969         hsbox = gtk_hbox_new(FALSE, 0);
970         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
971         statusbar = gtk_statusbar_new();
972         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
973
974         /* Button panel */
975         gtkut_stock_button_set_create(&hbbox,
976                                       &btnCancel, GTK_STOCK_CANCEL, 
977                                       &btnPrev, GTK_STOCK_GO_BACK,
978                                       &btnNext, GTK_STOCK_GO_FORWARD);
979
980         btnProceed = gtk_button_new_with_mnemonic(_("Proceed"));
981         gtk_button_set_image(GTK_BUTTON(btnProceed),
982                         gtk_image_new_from_stock(GTK_STOCK_OK, GTK_ICON_SIZE_BUTTON));
983         gtkut_widget_set_can_default(btnProceed, TRUE);
984         gtk_box_pack_start(GTK_BOX(hbbox), btnProceed, TRUE, TRUE, 0);
985         gtk_widget_hide(btnProceed);
986
987         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
988         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
989         gtk_widget_grab_default(btnNext);
990
991         /* Button handlers */
992         g_signal_connect(G_OBJECT(btnPrev), "clicked",
993                          G_CALLBACK(imp_ldif_prev), NULL);
994         g_signal_connect(G_OBJECT(btnNext), "clicked",
995                          G_CALLBACK(imp_ldif_next), NULL);
996         g_signal_connect(G_OBJECT(btnProceed), "clicked",
997                          G_CALLBACK(imp_ldif_next), NULL);
998         g_signal_connect(G_OBJECT(btnCancel), "clicked",
999                          G_CALLBACK(imp_ldif_cancel), NULL);
1000
1001         gtk_widget_show_all(vbox);
1002
1003         impldif_dlg.window     = window;
1004         impldif_dlg.notebook   = notebook;
1005         impldif_dlg.btnPrev    = btnPrev;
1006         impldif_dlg.btnNext    = btnNext;
1007         impldif_dlg.btnProceed = btnProceed;
1008         impldif_dlg.btnCancel  = btnCancel;
1009         impldif_dlg.statusbar  = statusbar;
1010         impldif_dlg.status_cid = gtk_statusbar_get_context_id(
1011                         GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
1012
1013 }
1014
1015 /**
1016  * Create import LDIF dialog.
1017  */
1018 static void imp_ldif_create() {
1019         imp_ldif_dialog_create();
1020         imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
1021         imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
1022         imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
1023         gtk_widget_show_all( impldif_dlg.window );
1024 }
1025
1026 /**
1027  * Import LDIF file.
1028  * \param  addrIndex Address index.
1029  * \return Address book file of imported data, or <i>NULL</i> if import
1030  *         was cancelled.
1031  */
1032 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
1033         _importedBook_ = NULL;
1034         _imp_addressIndex_ = addrIndex;
1035
1036         if( ! impldif_dlg.window )
1037                 imp_ldif_create();
1038
1039         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
1040                              GTK_STOCK_CANCEL);
1041         gtk_widget_hide(impldif_dlg.btnProceed);
1042         gtk_widget_show(impldif_dlg.btnNext);
1043
1044         impldif_dlg.cancelled = FALSE;
1045         gtk_widget_show(impldif_dlg.window);
1046         manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
1047         gtk_widget_grab_default(impldif_dlg.btnNext);
1048         gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), TRUE);
1049
1050         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), IMPORTLDIF_GUESS_NAME );
1051         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), "" );
1052         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
1053         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
1054         gtk_cmclist_clear( GTK_CMCLIST(impldif_dlg.clist_field) );
1055         gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
1056         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
1057         gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
1058         stock_pixbuf_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
1059                           &markxpm );
1060         imp_ldif_message();
1061         gtk_widget_grab_focus(impldif_dlg.entryFile);
1062
1063         impldif_dlg.rowIndSelect = -1;
1064         impldif_dlg.rowCount = 0;
1065         g_free( impldif_dlg.nameBook );
1066         g_free( impldif_dlg.fileName );
1067         impldif_dlg.nameBook = NULL;
1068         impldif_dlg.fileName = NULL;
1069
1070         _ldifFile_ = ldif_create();
1071         gtk_main();
1072         gtk_widget_hide(impldif_dlg.window);
1073         gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), FALSE);
1074         ldif_free( _ldifFile_ );
1075         _ldifFile_ = NULL;
1076         _imp_addressIndex_ = NULL;
1077
1078         g_free( impldif_dlg.nameBook );
1079         g_free( impldif_dlg.fileName );
1080         impldif_dlg.nameBook = NULL;
1081         impldif_dlg.fileName = NULL;
1082
1083         if( impldif_dlg.cancelled == TRUE ) return NULL;
1084         return _importedBook_;
1085 }
1086
1087 /*
1088  * ============================================================================
1089  * End of Source.
1090  * ============================================================================
1091  */
1092