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