Add gtkut_tree_view_get_selected_pointer() helper function.
[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_COL_WIDTH_RESERVED  10
57 #define FIELDS_COL_WIDTH_SELECT    10
58 #define FIELDS_COL_WIDTH_FIELD     140
59 #define FIELDS_COL_WIDTH_ATTRIB    140
60
61 typedef enum {
62         FIELD_COL_RESERVED,
63         FIELD_COL_SELECT,
64         FIELD_COL_FIELD,
65         FIELD_COL_ATTRIB,
66         FIELD_COL_PTR,
67         FIELDS_N_COLS
68 } ImpLdif_FieldColPos;
69
70 /**
71  * LDIF dialog definition.
72  */
73 static struct _ImpLdif_Dlg {
74         GtkWidget *window;
75         GtkWidget *notebook;
76         GtkWidget *entryFile;
77         GtkWidget *entryName;
78         GtkWidget *view_fields;
79         GtkWidget *entryField;
80         GtkWidget *entryAttrib;
81         GtkWidget *checkSelect;
82         GtkWidget *btnModify;
83         GtkWidget *labelBook;
84         GtkWidget *labelFile;
85         GtkWidget *labelRecords;
86         GtkWidget *btnPrev;
87         GtkWidget *btnNext;
88         GtkWidget *btnProceed;
89         GtkWidget *btnCancel;
90         GtkWidget *statusbar;
91         gint      status_cid;
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 the line (represented by the GtkTreeIter) with data
198  * from the Ldif_FieldRec.
199  */
200 static void _populate_iter(GtkListStore *store, GtkTreeIter *iter,
201                 Ldif_FieldRec *rec)
202 {
203         gtk_list_store_set(store, iter,
204                         FIELD_COL_FIELD, rec->tagName,
205                         FIELD_COL_ATTRIB, rec->userName,
206                         FIELD_COL_PTR, rec,
207                         -1);
208         gtk_list_store_set(store, iter,
209                         FIELD_COL_SELECT, rec->selected ? markxpm : NULL, -1);
210         gtk_list_store_set(store, iter,
211                         FIELD_COL_RESERVED, rec->reserved ? markxpm : NULL, -1);
212 }
213
214 /**
215  * Load list with LDIF fields read from file.
216  * \param ldf LDIF control data.
217  */
218 static void imp_ldif_load_fields( LdifFile *ldf ) {
219         GtkWidget *view = impldif_dlg.view_fields;
220         GtkTreeModel *model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
221         GtkTreeIter iter;
222         GList *node, *list;
223
224         impldif_dlg.rowCount = 0;
225
226         if( ! ldf->accessFlag ) return;
227
228         gtk_list_store_clear(GTK_LIST_STORE(model));
229
230         list = ldif_get_fieldlist( ldf );
231         node = list;
232         while( node ) {
233                 Ldif_FieldRec *rec = node->data;
234
235                 gtk_list_store_append(GTK_LIST_STORE(model), &iter);
236                 _populate_iter(GTK_LIST_STORE(model), &iter, rec);
237                 impldif_dlg.rowCount++;
238                 node = g_list_next( node );
239         }
240         g_list_free( list );
241         list = NULL;
242         ldif_set_accessed( ldf, FALSE );
243 }
244
245 /**
246  * Test whether we can move off fields page.
247  * \return <i>TRUE</i> if OK to move off page.
248  */
249 static gboolean imp_ldif_field_move() {
250         gboolean retVal = FALSE;
251         gchar *newFile;
252         AddressBookFile *abf = NULL;
253
254         if( _importedBook_ ) {
255                 addrbook_free_book( _importedBook_ );
256         }
257
258         abf = addrbook_create_book();
259         addrbook_set_path( abf, _imp_addressIndex_->filePath );
260         addrbook_set_name( abf, impldif_dlg.nameBook );
261         newFile = addrbook_guess_next_file( abf );
262         addrbook_set_file( abf, newFile );
263         g_free( newFile );
264
265         /* Import data into file */
266         if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
267                 addrbook_save_data( abf );
268                 _importedBook_ = abf;
269                 retVal = TRUE;
270         }
271         else {
272                 addrbook_free_book( abf );
273         }
274
275         return retVal;
276 }
277
278 static void _update_selected_row()
279 {
280         GtkWidget *view = impldif_dlg.view_fields;
281         GtkTreeModel *model;
282         GtkTreeSelection *sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view));
283         GtkTreeIter iter;
284         Ldif_FieldRec *rec;
285
286         if (!gtk_tree_selection_get_selected(sel, &model, &iter))
287                 return;
288
289         gtk_tree_model_get(model, &iter, FIELD_COL_PTR, &rec, -1);
290         cm_return_if_fail(rec != NULL);
291
292         ldif_field_set_name(rec, gtk_entry_get_text(
293                                 GTK_ENTRY(impldif_dlg.entryAttrib)));
294         ldif_field_set_selected(rec, gtk_toggle_button_get_active(
295                                 GTK_TOGGLE_BUTTON(impldif_dlg.checkSelect)));
296
297         _populate_iter(GTK_LIST_STORE(model), &iter, rec);
298 }
299
300 static void imp_ldif_modify_pressed(GtkButton *widget, gpointer user_data)
301 {
302         _update_selected_row();
303 }
304
305 static void imp_ldif_entryattrib_activate(GtkEntry *entry, gpointer user_data)
306 {
307         _update_selected_row();
308 }
309
310 /**
311  * Test whether we can move off file page.
312  * \return <i>TRUE</i> if OK to move off page.
313  */
314 static gboolean imp_ldif_file_move() {
315         gboolean retVal = FALSE;
316         gchar *sName;
317         gchar *sFile;
318         gchar *sMsg = NULL;
319         gboolean errFlag = FALSE;
320
321         sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryFile), 0, -1 );
322         g_strstrip( sFile );
323
324         sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
325         g_strstrip( sName );
326
327         g_free( impldif_dlg.nameBook );
328         g_free( impldif_dlg.fileName );
329         impldif_dlg.nameBook = sName;
330         impldif_dlg.fileName = sFile;
331
332         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), sFile );
333         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), sName );
334
335         if( *sFile == '\0' ) {
336                 sMsg = _( "Please select a file." );
337                 gtk_widget_grab_focus(impldif_dlg.entryFile);
338                 errFlag = TRUE;
339         }
340
341         if( ! errFlag && *sName == '\0' ) {
342                 sMsg = _( "Address book name must be supplied." );
343                 gtk_widget_grab_focus(impldif_dlg.entryName);
344                 errFlag = TRUE;
345         }
346
347         if( ! errFlag ) {
348                 /* Read attribute list */
349                 ldif_set_file( _ldifFile_, sFile );
350                 if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
351                         /* Load fields */
352                         /* ldif_print_file( _ldifFile_, stdout ); */
353                         imp_ldif_load_fields( _ldifFile_ );
354                         retVal = TRUE;
355                 }
356                 else {
357                         sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
358                 }
359         }
360         imp_ldif_status_show( sMsg );
361
362         return retVal;
363 }
364
365 /**
366  * Display finish page.
367  */
368 static void imp_ldif_finish_show() {
369         gchar *sMsg;
370         gchar *name;
371
372         name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.entryName), 0, -1 );
373         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
374         g_free( name );
375         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
376         gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
377         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
378         gtk_widget_hide( impldif_dlg.btnNext );
379         gtk_widget_show( impldif_dlg.btnProceed );
380         gtk_widget_set_sensitive( impldif_dlg.btnProceed, FALSE );
381         if( _ldifFile_->retVal == MGU_SUCCESS ) {
382                 sMsg = _( "LDIF file imported successfully." );
383         }
384         else {
385                 sMsg = imp_ldif_err2string( _lutErrorsLDIF_, _ldifFile_->retVal );
386         }
387         imp_ldif_status_show( sMsg );
388         gtk_widget_grab_focus(impldif_dlg.btnCancel);
389 }
390
391 /**
392  * Callback function to select previous page.
393  * \param widget Widget (button).
394  */
395 static void imp_ldif_prev( GtkWidget *widget ) {
396         gint pageNum;
397
398         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
399         if( pageNum == PAGE_ATTRIBUTES ) {
400                 /* Goto file page stuff */
401                 gtk_notebook_set_current_page(
402                         GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
403                 gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
404                 gtk_widget_hide( impldif_dlg.btnProceed );
405                 gtk_widget_show( impldif_dlg.btnNext );
406         }
407         imp_ldif_message();
408 }
409
410 /**
411  * Callback function to select next page.
412  * \param widget Widget (button).
413  */
414 static void imp_ldif_next( GtkWidget *widget ) {
415         gint pageNum;
416
417         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
418         if( pageNum == PAGE_FILE_INFO ) {
419                 /* Goto attributes stuff */
420                 if( imp_ldif_file_move() ) {
421                         gtk_notebook_set_current_page(
422                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
423                         imp_ldif_message();
424                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
425                         gtk_widget_hide( impldif_dlg.btnNext );
426                         gtk_widget_show( impldif_dlg.btnProceed );
427                         gtk_widget_set_sensitive( impldif_dlg.btnProceed, TRUE );
428                 }
429                 else {
430                         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
431                         _ldifFile_->dirtyFlag = TRUE;
432                 }
433         }
434         else if( pageNum == PAGE_ATTRIBUTES ) {
435                 /* Goto finish stuff */
436                 if( imp_ldif_field_move() ) {
437                         gtk_notebook_set_current_page(
438                                 GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
439                         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
440                                              GTK_STOCK_CLOSE);
441                         imp_ldif_finish_show();
442                 }
443         }
444 }
445
446 /**
447  * Callback function to cancel and close dialog.
448  * \param widget Widget (button).
449  * \param data   User data.
450  */
451 static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
452         gint pageNum;
453
454         pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
455         if( pageNum != PAGE_FINISH ) {
456                 impldif_dlg.cancelled = TRUE;
457         }
458         gtk_main_quit();
459 }
460
461
462 /**
463  * Create LDIF file selection dialog.
464  * \param afs Address file selection data.
465  */
466 static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
467         gchar *file = filesel_select_file_open(_("Select LDIF File"), NULL);
468
469         if (file == NULL)
470                 afs->cancelled = TRUE;
471         else {
472                 afs->cancelled = FALSE;
473                 gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), file );
474                 g_free(file);
475         }
476 }
477
478 /**
479  * Callback function to display LDIF file selection dialog.
480  */
481 static void imp_ldif_file_select( void ) {
482         imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
483 }
484
485 /**
486  * Callback function to handle dialog close event.
487  * \param widget Widget (dialog).
488  * \param event  Event object.
489  * \param data   User data.
490  */
491 static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
492         imp_ldif_cancel( widget, data );
493         return TRUE;
494 }
495
496 /**
497  * Callback function to respond to dialog key press events.
498  * \param widget Widget.
499  * \param event  Event object.
500  * \param data   User data.
501  */
502 static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
503         if (event && event->keyval == GDK_KEY_Escape) {
504                 imp_ldif_cancel( widget, data );
505         }
506         return FALSE;
507 }
508
509 /**
510  * Format notebook "file" page.
511  * \param pageNum Page (tab) number.
512  * \param pageLbl Page (tab) label.
513  */
514 static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
515         GtkWidget *vbox;
516         GtkWidget *table;
517         GtkWidget *label;
518         GtkWidget *entryFile;
519         GtkWidget *entryName;
520         GtkWidget *btnFile;
521         gint top;
522
523         vbox = gtk_vbox_new(FALSE, 8);
524         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
525         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
526
527         label = gtk_label_new( pageLbl );
528         gtk_widget_show( label );
529         gtk_notebook_set_tab_label(
530                 GTK_NOTEBOOK( impldif_dlg.notebook ),
531                 gtk_notebook_get_nth_page(
532                         GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
533                 label );
534
535         table = gtk_table_new(2, 3, FALSE);
536         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
537         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
538         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
539         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
540
541         /* First row */
542         top = 0;
543         label = gtk_label_new(_("Address Book"));
544         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
545                 GTK_FILL, 0, 0, 0);
546         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
547
548         entryName = gtk_entry_new();
549         gtk_table_attach(GTK_TABLE(table), entryName, 1, 2, top, (top + 1),
550                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
551
552         CLAWS_SET_TIP(entryName, _( 
553                 "Specify the name for the address book that will " \
554                 "be created from the LDIF file data." ));
555
556         /* Second row */
557         top = 1;
558         label = gtk_label_new(_("File Name"));
559         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
560                 GTK_FILL, 0, 0, 0);
561         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
562
563         entryFile = gtk_entry_new();
564         gtk_table_attach(GTK_TABLE(table), entryFile, 1, 2, top, (top + 1),
565                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
566
567         CLAWS_SET_TIP(entryFile,
568                 _( "The full file specification of the LDIF file to import." ));
569
570         btnFile = gtkut_get_browse_file_btn(_("B_rowse"));
571         gtk_table_attach(GTK_TABLE(table), btnFile, 2, 3, top, (top + 1),
572                 GTK_FILL, 0, 3, 0);
573
574         CLAWS_SET_TIP(btnFile,
575                 _( "Select the LDIF file to import." ));
576
577         gtk_widget_show_all(vbox);
578
579         /* Button handler */
580         g_signal_connect(G_OBJECT(btnFile), "clicked",
581                          G_CALLBACK(imp_ldif_file_select), NULL);
582
583         impldif_dlg.entryFile = entryFile;
584         impldif_dlg.entryName = entryName;
585
586 }
587
588 static void imp_ldif_field_list_cursor_changed(GtkTreeView *view,
589                 gpointer user_data)
590 {
591         Ldif_FieldRec *rec;
592
593         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
594
595         rec = gtkut_tree_view_get_selected_pointer(view, FIELD_COL_PTR);
596
597         if( rec != NULL) {
598                 /* Update widget contents */
599                 gtk_label_set_text(
600                         GTK_LABEL(impldif_dlg.entryField), rec->tagName );
601                 if( rec->userName )
602                         gtk_entry_set_text(
603                                 GTK_ENTRY(impldif_dlg.entryAttrib), rec->userName );
604                 gtk_toggle_button_set_active(
605                         GTK_TOGGLE_BUTTON(impldif_dlg.checkSelect),
606                         rec->selected );
607
608                 /* Disable widgets for reserved fields */
609                 gtk_widget_set_sensitive(
610                         impldif_dlg.entryAttrib, ! rec->reserved );
611                 gtk_widget_set_sensitive(
612                         impldif_dlg.checkSelect, ! rec->reserved );
613                 gtk_widget_set_sensitive(
614                         impldif_dlg.btnModify,   ! rec->reserved );
615         }
616         gtk_widget_grab_focus(impldif_dlg.entryAttrib);
617 }
618
619 static void imp_ldif_field_list_row_activated(GtkTreeView *view,
620                 GtkTreePath *path, GtkTreeViewColumn *col,
621                 gpointer user_data)
622 {
623         GtkTreeModel *model = gtk_tree_view_get_model(view);
624         GtkTreeIter iter;
625         gboolean ok;
626         Ldif_FieldRec *rec;
627
628         ok = gtk_tree_model_get_iter(model, &iter, path);
629         if (!ok) {
630                 return; /* Huh? */
631         }
632
633         gtk_tree_model_get(model, &iter, FIELD_COL_PTR, &rec, -1);
634         cm_return_if_fail(rec != NULL);
635
636         /* Flip the "selected" state for the record, and update the
637          * "selected" column in the list view, as well as the
638          * "selected" checkbox. */
639         ldif_field_toggle(rec);
640         gtk_list_store_set(GTK_LIST_STORE(model), &iter,
641                         FIELD_COL_SELECT, rec->selected ? markxpm : NULL, -1);
642         gtk_toggle_button_set_active(
643                 GTK_TOGGLE_BUTTON(impldif_dlg.checkSelect),
644                 rec->selected );
645 }
646
647 /**
648  * Format notebook fields page.
649  * \param pageNum Page (tab) number.
650  * \param pageLbl Page (tab) label.
651  */
652 static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
653         GtkWidget *vbox;
654         GtkWidget *vboxt;
655         GtkWidget *vboxb;
656         GtkWidget *table;
657         GtkWidget *label;
658         GtkWidget *scrollwin;
659         GtkWidget *view_fields;
660         GtkWidget *entryField;
661         GtkWidget *entryAttrib;
662         GtkWidget *checkSelect;
663         GtkWidget *btnModify;
664         GtkWidget *eventBox;
665         gint top;
666         GtkListStore *store;
667         GtkCellRenderer *rdr;
668         GtkTreeViewColumn *col;
669         GtkTreeSelection *sel;
670
671         vbox = gtk_vbox_new(FALSE, 8);
672         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
673         gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
674
675         label = gtk_label_new( pageLbl );
676         gtk_widget_show( label );
677         gtk_notebook_set_tab_label(
678                 GTK_NOTEBOOK( impldif_dlg.notebook ),
679                 gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
680                 label );
681
682         /* Upper area - Field list */
683         vboxt = gtk_vbox_new( FALSE, 4 );
684         gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
685
686         scrollwin = gtk_scrolled_window_new( NULL, NULL );
687         gtk_container_add( GTK_CONTAINER(vboxt), scrollwin );
688         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(scrollwin),
689                                        GTK_POLICY_AUTOMATIC,
690                                        GTK_POLICY_AUTOMATIC);
691
692         store = gtk_list_store_new(FIELDS_N_COLS,
693                         GDK_TYPE_PIXBUF, GDK_TYPE_PIXBUF,
694                         G_TYPE_STRING, G_TYPE_STRING,
695                         G_TYPE_POINTER,
696                         -1);
697
698         view_fields = gtk_tree_view_new_with_model(GTK_TREE_MODEL(store));
699         g_object_unref(store);
700         gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(view_fields), TRUE);
701         gtk_tree_view_set_reorderable(GTK_TREE_VIEW(view_fields), FALSE);
702         sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(view_fields));
703         gtk_tree_selection_set_mode(sel, GTK_SELECTION_BROWSE);
704
705         rdr = gtk_cell_renderer_pixbuf_new();
706         col = gtk_tree_view_column_new_with_attributes(_("R"), rdr,
707                         "pixbuf", FIELD_COL_RESERVED, NULL);
708         gtk_tree_view_column_set_min_width(col, FIELD_COL_RESERVED);
709         gtk_tree_view_append_column(GTK_TREE_VIEW(view_fields), col);
710
711         col = gtk_tree_view_column_new_with_attributes(_("S"), rdr,
712                         "pixbuf", FIELD_COL_SELECT, NULL);
713         gtk_tree_view_column_set_min_width(col, FIELD_COL_SELECT);
714         gtk_tree_view_append_column(GTK_TREE_VIEW(view_fields), col);
715
716         rdr = gtk_cell_renderer_text_new();
717         col = gtk_tree_view_column_new_with_attributes(_("LDIF Field Name"), rdr,
718                         "markup", FIELD_COL_FIELD, NULL);
719         gtk_tree_view_column_set_min_width(col, FIELD_COL_FIELD);
720         gtk_tree_view_append_column(GTK_TREE_VIEW(view_fields), col);
721
722         col = gtk_tree_view_column_new_with_attributes(_("Attribute Name"), rdr,
723                         "markup", FIELD_COL_ATTRIB, NULL);
724         gtk_tree_view_column_set_min_width(col, FIELD_COL_ATTRIB);
725         gtk_tree_view_append_column(GTK_TREE_VIEW(view_fields), col);
726
727         gtk_container_add( GTK_CONTAINER(scrollwin), view_fields );
728
729         /* Lower area - Edit area */
730         vboxb = gtk_vbox_new( FALSE, 4 );
731         gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
732
733         /* Data entry area */
734         table = gtk_table_new( 3, 3, FALSE);
735         gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
736         gtk_table_set_row_spacings(GTK_TABLE(table), 4);
737         gtk_table_set_col_spacings(GTK_TABLE(table), 4);
738
739         /* First row */
740         top = 0;
741         label = gtk_label_new(_("LDIF Field"));
742         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
743                 GTK_FILL, 0, 0, 0);
744         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
745
746         entryField = gtk_label_new( "" );
747         gtk_misc_set_alignment(GTK_MISC(entryField), 0.01, 0.5);
748         gtk_table_attach(GTK_TABLE(table), entryField, 1, 3, top, (top + 1),
749                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
750
751         /* Second row */
752         ++top;
753         label = gtk_label_new(_("Attribute"));
754         /*
755          * Use an event box to attach some help in the form of a tooltip.
756          * Tried this for the clist but it looked bad.
757          */
758         eventBox = gtk_event_box_new();
759         gtk_container_add( GTK_CONTAINER(eventBox), label );
760         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
761         gtk_table_attach(GTK_TABLE(table), eventBox, 0, 1, top, (top + 1),
762                 GTK_FILL, 0, 0, 0);
763
764         CLAWS_SET_TIP(eventBox, _(
765                 "Choose the LDIF field that will be renamed or selected " \
766                 "for import in the list above. Reserved fields (marked " \
767                 "with a tick in the \"R\" column), are automatically " \
768                 "imported and cannot be renamed. A single click in the " \
769                 "Select (\"S\") column will select the field for import " \
770                 "with a tick. A single click anywhere in the row will " \
771                 "select that field for rename in the input area below " \
772                 "the list. A double click anywhere in the row will also " \
773                 "select the field for import."));
774
775         entryAttrib = gtk_entry_new();
776         gtk_table_attach(GTK_TABLE(table), entryAttrib, 1, 3, top, (top + 1),
777                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
778
779         CLAWS_SET_TIP(entryAttrib,
780                 _( "The LDIF field can be renamed to the User Attribute name." ));
781
782         /* Next row */
783         ++top;
784
785         checkSelect = gtk_check_button_new_with_label( _( "Select for Import" ) );
786         gtk_table_attach(GTK_TABLE(table), checkSelect, 1, 2, top, (top + 1),
787                 GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
788
789         CLAWS_SET_TIP(checkSelect,
790                 _( "Select the LDIF field for import into the address book." ));
791
792         btnModify = gtk_button_new_with_label( _(" Modify "));
793         gtk_table_attach(GTK_TABLE(table), btnModify, 2, 3, top, (top + 1),
794                 GTK_FILL, 0, 3, 0);
795
796         CLAWS_SET_TIP(btnModify,
797                 _( "This button will update the list above with the data supplied." ));
798
799         gtk_widget_show_all(vbox);
800
801         /* Event handlers */
802         g_signal_connect(G_OBJECT(view_fields), "cursor-changed",
803                         G_CALLBACK(imp_ldif_field_list_cursor_changed), NULL);
804         g_signal_connect(G_OBJECT(view_fields), "row-activated",
805                         G_CALLBACK(imp_ldif_field_list_row_activated), NULL);
806         g_signal_connect(G_OBJECT(btnModify), "clicked",
807                         G_CALLBACK(imp_ldif_modify_pressed), NULL );
808         g_signal_connect(G_OBJECT(entryAttrib), "activate",
809                         G_CALLBACK(imp_ldif_entryattrib_activate), NULL);
810
811         impldif_dlg.view_fields = view_fields;
812         impldif_dlg.entryField  = entryField;
813         impldif_dlg.entryAttrib = entryAttrib;
814         impldif_dlg.checkSelect = checkSelect;
815         impldif_dlg.btnModify   = btnModify;
816 }
817
818 /**
819  * Format notebook finish page.
820  * \param pageNum Page (tab) number.
821  * \param pageLbl Page (tab) label.
822  */
823 static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
824         GtkWidget *vbox;
825         GtkWidget *table;
826         GtkWidget *label;
827         GtkWidget *labelBook;
828         GtkWidget *labelFile;
829         GtkWidget *labelRecs;
830         gint top;
831
832         vbox = gtk_vbox_new(FALSE, 8);
833         gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
834         gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
835
836         label = gtk_label_new( pageLbl );
837         gtk_widget_show( label );
838         gtk_notebook_set_tab_label(
839                 GTK_NOTEBOOK( impldif_dlg.notebook ),
840                 gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
841                 label );
842
843         table = gtk_table_new(3, 2, FALSE);
844         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
845         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
846         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
847         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
848
849         /* First row */
850         top = 0;
851         label = gtk_label_new( _( "Address Book:" ) );
852         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
853         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
854
855         labelBook = gtk_label_new("");
856         gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
857         gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
858
859         /* Second row */
860         top++;
861         label = gtk_label_new( _( "File Name:" ) );
862         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
863         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
864
865         labelFile = gtk_label_new("");
866         gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
867         gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
868
869         /* Third row */
870         top++;
871         label = gtk_label_new( _("Records Imported:") );
872         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
873         gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
874
875         labelRecs = gtk_label_new("");
876         gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
877         gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
878
879         impldif_dlg.labelBook    = labelBook;
880         impldif_dlg.labelFile    = labelFile;
881         impldif_dlg.labelRecords = labelRecs;
882 }
883
884 /**
885  * Create main dialog decorations (excluding notebook pages).
886  */
887 static void imp_ldif_dialog_create() {
888         GtkWidget *window;
889         GtkWidget *vbox;
890         GtkWidget *vnbox;
891         GtkWidget *notebook;
892         GtkWidget *hbbox;
893         GtkWidget *btnPrev;
894         GtkWidget *btnNext;
895         GtkWidget *btnProceed;
896         GtkWidget *btnCancel;
897         GtkWidget *hsbox;
898         GtkWidget *statusbar;
899
900         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "importldif");
901         gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
902         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
903         gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
904         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
905         g_signal_connect(G_OBJECT(window), "delete_event",
906                          G_CALLBACK(imp_ldif_delete_event),
907                          NULL );
908         g_signal_connect(G_OBJECT(window), "key_press_event",
909                          G_CALLBACK(imp_ldif_key_pressed),
910                          NULL );
911
912         vbox = gtk_vbox_new(FALSE, 4);
913         gtk_widget_show(vbox);
914         gtk_container_add(GTK_CONTAINER(window), vbox);
915
916         vnbox = gtk_vbox_new(FALSE, 4);
917         gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
918         gtk_widget_show(vnbox);
919         gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
920
921         /* Notebook */
922         notebook = gtk_notebook_new();
923         gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
924         gtk_widget_show(notebook);
925         gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
926         gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
927
928         /* Status line */
929         hsbox = gtk_hbox_new(FALSE, 0);
930         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
931         statusbar = gtk_statusbar_new();
932         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
933
934         /* Button panel */
935         gtkut_stock_button_set_create(&hbbox,
936                                       &btnCancel, GTK_STOCK_CANCEL, 
937                                       &btnPrev, GTK_STOCK_GO_BACK,
938                                       &btnNext, GTK_STOCK_GO_FORWARD);
939
940         btnProceed = gtk_button_new_with_mnemonic(_("Proceed"));
941         gtk_button_set_image(GTK_BUTTON(btnProceed),
942                         gtk_image_new_from_stock(GTK_STOCK_OK, GTK_ICON_SIZE_BUTTON));
943         gtk_widget_set_can_default(btnProceed, TRUE);
944         gtk_box_pack_start(GTK_BOX(hbbox), btnProceed, TRUE, TRUE, 0);
945         gtk_widget_hide(btnProceed);
946
947         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
948         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
949         gtk_widget_grab_default(btnNext);
950
951         /* Button handlers */
952         g_signal_connect(G_OBJECT(btnPrev), "clicked",
953                          G_CALLBACK(imp_ldif_prev), NULL);
954         g_signal_connect(G_OBJECT(btnNext), "clicked",
955                          G_CALLBACK(imp_ldif_next), NULL);
956         g_signal_connect(G_OBJECT(btnProceed), "clicked",
957                          G_CALLBACK(imp_ldif_next), NULL);
958         g_signal_connect(G_OBJECT(btnCancel), "clicked",
959                          G_CALLBACK(imp_ldif_cancel), NULL);
960
961         gtk_widget_show_all(vbox);
962
963         impldif_dlg.window     = window;
964         impldif_dlg.notebook   = notebook;
965         impldif_dlg.btnPrev    = btnPrev;
966         impldif_dlg.btnNext    = btnNext;
967         impldif_dlg.btnProceed = btnProceed;
968         impldif_dlg.btnCancel  = btnCancel;
969         impldif_dlg.statusbar  = statusbar;
970         impldif_dlg.status_cid = gtk_statusbar_get_context_id(
971                         GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
972
973 }
974
975 /**
976  * Create import LDIF dialog.
977  */
978 static void imp_ldif_create() {
979         imp_ldif_dialog_create();
980         imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
981         imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
982         imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
983         gtk_widget_show_all( impldif_dlg.window );
984 }
985
986 /**
987  * Import LDIF file.
988  * \param  addrIndex Address index.
989  * \return Address book file of imported data, or <i>NULL</i> if import
990  *         was cancelled.
991  */
992 AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
993         GtkWidget *view;
994         GtkTreeModel *model;
995
996         _importedBook_ = NULL;
997         _imp_addressIndex_ = addrIndex;
998
999         if( ! impldif_dlg.window )
1000                 imp_ldif_create();
1001
1002         view = impldif_dlg.view_fields;
1003         model = gtk_tree_view_get_model(GTK_TREE_VIEW(view));
1004
1005         gtk_button_set_label(GTK_BUTTON(impldif_dlg.btnCancel),
1006                              GTK_STOCK_CANCEL);
1007         gtk_widget_hide(impldif_dlg.btnProceed);
1008         gtk_widget_show(impldif_dlg.btnNext);
1009
1010         impldif_dlg.cancelled = FALSE;
1011         gtk_widget_show(impldif_dlg.window);
1012         manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
1013         gtk_widget_grab_default(impldif_dlg.btnNext);
1014         gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), TRUE);
1015
1016         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryName), IMPORTLDIF_GUESS_NAME );
1017         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryFile), "" );
1018         gtk_label_set_text( GTK_LABEL(impldif_dlg.entryField), "" );
1019         gtk_entry_set_text( GTK_ENTRY(impldif_dlg.entryAttrib), "" );
1020         gtk_list_store_clear( GTK_LIST_STORE(model) );
1021         gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
1022         gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
1023         gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
1024         stock_pixbuf_gdk(STOCK_PIXMAP_MARK, &markxpm );
1025         imp_ldif_message();
1026         gtk_widget_grab_focus(impldif_dlg.entryFile);
1027
1028         impldif_dlg.rowCount = 0;
1029         g_free( impldif_dlg.nameBook );
1030         g_free( impldif_dlg.fileName );
1031         impldif_dlg.nameBook = NULL;
1032         impldif_dlg.fileName = NULL;
1033
1034         _ldifFile_ = ldif_create();
1035         gtk_main();
1036         gtk_widget_hide(impldif_dlg.window);
1037         gtk_window_set_modal(GTK_WINDOW(impldif_dlg.window), FALSE);
1038         ldif_free( _ldifFile_ );
1039         _ldifFile_ = NULL;
1040         _imp_addressIndex_ = NULL;
1041
1042         g_free( impldif_dlg.nameBook );
1043         g_free( impldif_dlg.fileName );
1044         impldif_dlg.nameBook = NULL;
1045         impldif_dlg.fileName = NULL;
1046
1047         if( impldif_dlg.cancelled == TRUE ) return NULL;
1048         return _importedBook_;
1049 }
1050
1051 /*
1052  * ============================================================================
1053  * End of Source.
1054  * ============================================================================
1055  */
1056