Statistics
| Revision:

root / src / importldif.c @ 1

History | View | Annotate | Download (25.6 KB)

1
/*
2
 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3
 * Copyright (C) 2001 Match Grun
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 2 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, write to the Free Software
17
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18
 */
19

    
20
/*
21
 * Edit VCard 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 <gdk/gdkkeysyms.h>
32
#include <gtk/gtkwindow.h>
33
#include <gtk/gtksignal.h>
34
#include <gtk/gtklabel.h>
35
#include <gtk/gtkentry.h>
36
#include <gtk/gtktable.h>
37
#include <gtk/gtkbutton.h>
38

    
39
#include "intl.h"
40
#include "addrbook.h"
41
#include "addressbook.h"
42
#include "addressitem.h"
43
#include "gtkutils.h"
44
#include "stock_pixmap.h"
45
#include "prefs_common.h"
46
#include "manage_window.h"
47
#include "mgutils.h"
48
#include "ldif.h"
49
#include "utils.h"
50

    
51
#define IMPORTLDIF_GUESS_NAME "LDIF Import"
52

    
53
#define PAGE_FILE_INFO             0
54
#define PAGE_ATTRIBUTES            1
55
#define PAGE_FINISH                2
56

    
57
#define IMPORTLDIF_WIDTH           380
58
#define IMPORTLDIF_HEIGHT          300
59

    
60
#define FIELDS_N_COLS              3
61
#define FIELDS_COL_WIDTH_SELECT    10
62
#define FIELDS_COL_WIDTH_FIELD     140
63
#define FIELDS_COL_WIDTH_ATTRIB    140
64

    
65
typedef enum {
66
        FIELD_COL_SELECT  = 0,
67
        FIELD_COL_FIELD   = 1,
68
        FIELD_COL_ATTRIB  = 2
69
} ImpLdif_FieldColPos;
70

    
71
static struct _ImpLdif_Dlg {
72
        GtkWidget *window;
73
        GtkWidget *notebook;
74
        GtkWidget *file_entry;
75
        GtkWidget *name_entry;
76
        GtkWidget *clist_field;
77
        GtkWidget *name_ldif;
78
        GtkWidget *name_attrib;
79
        GtkWidget *check_select;
80
        GtkWidget *labelBook;
81
        GtkWidget *labelFile;
82
        GtkWidget *labelRecords;
83
        GtkWidget *btnPrev;
84
        GtkWidget *btnNext;
85
        GtkWidget *btnCancel;
86
        GtkWidget *statusbar;
87
        gint      status_cid;
88
        gint      rowIndSelect;
89
        gint      rowCount;
90
        gchar     *nameBook;
91
        gchar     *fileName;
92
        gboolean  cancelled;
93
} impldif_dlg;
94

    
95
static struct _AddressFileSelection _imp_ldif_file_selector_;
96
static AddressBookFile *_importedBook_;
97
static AddressIndex *_imp_addressIndex_;
98
static LdifFile *_ldifFile_ = NULL;
99

    
100
static GdkPixmap *markxpm;
101
static GdkBitmap *markxpmmask;
102

    
103
static void imp_ldif_status_show( gchar *msg ) {
104
        if( impldif_dlg.statusbar != NULL ) {
105
                gtk_statusbar_pop( GTK_STATUSBAR(impldif_dlg.statusbar), impldif_dlg.status_cid );
106
                if( msg ) {
107
                        gtk_statusbar_push( GTK_STATUSBAR(impldif_dlg.statusbar), impldif_dlg.status_cid, msg );
108
                }
109
        }
110
}
111

    
112
static void imp_ldif_message( void ) {
113
        gchar *sMsg = NULL;
114
        gint pageNum;
115

    
116
        pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
117
        if( pageNum == PAGE_FILE_INFO ) {
118
                sMsg = _( "Please specify address book name and file to import." );
119
        }
120
        else if( pageNum == PAGE_ATTRIBUTES ) {
121
                sMsg = _( "Select and rename LDIF field names to import." );
122
        }
123
        else if( pageNum == PAGE_FINISH ) {
124
                sMsg = _( "File imported." );
125
        }
126
        imp_ldif_status_show( sMsg );
127
}
128

    
129
static gchar *imp_ldif_guess_file( AddressBookFile *abf ) {
130
        gchar *newFile = NULL;
131
        GList *fileList = NULL;
132
        gint fileNum = 1;
133
        fileList = addrbook_get_bookfile_list( abf );
134
        if( fileList ) {
135
                fileNum = 1 + abf->maxValue;
136
        }
137
        newFile = addrbook_gen_new_file_name( fileNum );
138
        g_list_free( fileList );
139
        fileList = NULL;
140
        return newFile;
141
}
142

    
143
static void imp_ldif_update_row( GtkCList *clist ) {
144
        Ldif_FieldRec *rec;
145
        gchar *text[ FIELDS_N_COLS ];
146
        gint row;
147

    
148
        if( impldif_dlg.rowIndSelect < 0 ) return;
149
        row = impldif_dlg.rowIndSelect;
150

    
151
        rec = gtk_clist_get_row_data( clist, row );
152
        text[ FIELD_COL_SELECT ] = "";
153
        text[ FIELD_COL_FIELD  ] = rec->tagName;
154
        text[ FIELD_COL_ATTRIB ] = rec->userName;
155

    
156
        gtk_clist_freeze( clist );
157
        gtk_clist_remove( clist, row );
158
        if( row == impldif_dlg.rowCount - 1 ) {
159
                gtk_clist_append( clist, text );
160
        }
161
        else {
162
                gtk_clist_insert( clist, row, text );
163
        }
164
        if( rec->selected )
165
                gtk_clist_set_pixmap( clist, row, FIELD_COL_SELECT, markxpm, markxpmmask );
166

    
167
        gtk_clist_set_row_data( clist, row, rec );
168
        gtk_clist_thaw( clist );
169
}
170

    
171
static void imp_ldif_load_fields( LdifFile *ldf ) {
172
        GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field);
173
        GList *node, *list;
174
        gchar *text[ FIELDS_N_COLS ];
175

    
176
        impldif_dlg.rowIndSelect = -1;
177
        impldif_dlg.rowCount = 0;
178
        if( ! ldf->accessFlag ) return;
179
        gtk_clist_clear( clist );
180
        list = ldif_get_fieldlist( ldf );
181
        node = list;
182
        while( node ) {
183
                Ldif_FieldRec *rec = node->data;
184
                gint row;
185

    
186
                if( ! rec->reserved ) {
187
                        text[ FIELD_COL_SELECT ] = "";
188
                        text[ FIELD_COL_FIELD  ] = rec->tagName;
189
                        text[ FIELD_COL_ATTRIB ] = rec->userName;
190
                        row = gtk_clist_append( clist, text );
191
                        gtk_clist_set_row_data( clist, row, rec );
192
                        if( rec->selected )
193
                                gtk_clist_set_pixmap( clist, row, FIELD_COL_SELECT, markxpm, markxpmmask );
194
                        impldif_dlg.rowCount++;
195
                }
196
                node = g_list_next( node );
197
        }
198
        g_list_free( list );
199
        list = NULL;
200
        ldif_set_accessed( ldf, FALSE );
201
}
202

    
203
static void imp_ldif_field_list_selected( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) {
204
        Ldif_FieldRec *rec = gtk_clist_get_row_data( clist, row );
205

    
206
        impldif_dlg.rowIndSelect = row;
207
        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
208
        if( rec ) {
209
                gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), rec->tagName );
210
                if( rec->userName )
211
                        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), rec->userName );
212
                gtk_toggle_button_set_active(
213
                        GTK_TOGGLE_BUTTON( impldif_dlg.check_select),
214
                        rec->selected );
215
        }
216
        gtk_widget_grab_focus(impldif_dlg.name_attrib);
217
}
218

    
219
static gboolean imp_ldif_field_list_toggle( GtkCList *clist, GdkEventButton *event, gpointer data ) {
220
        if( ! event ) return FALSE;
221
        if( impldif_dlg.rowIndSelect < 0 ) return FALSE;
222
        if( event->button == 1 ) {
223
                if( event->type == GDK_2BUTTON_PRESS ) {
224
                        Ldif_FieldRec *rec = gtk_clist_get_row_data( clist, impldif_dlg.rowIndSelect );
225
                        if( rec ) {
226
                                rec->selected = ! rec->selected;
227
                                imp_ldif_update_row( clist );
228
                        }
229
                }
230
        }
231
        return FALSE;
232
}
233

    
234
static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) {
235
        GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field);
236
        Ldif_FieldRec *rec;
237
        gint row;
238

    
239
        if( impldif_dlg.rowIndSelect < 0 ) return;
240
        row = impldif_dlg.rowIndSelect;
241
        rec = gtk_clist_get_row_data( clist, impldif_dlg.rowIndSelect );
242

    
243
        g_free( rec->userName );
244
        rec->userName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_attrib), 0, -1 );
245
        rec->selected = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( impldif_dlg.check_select) );
246
        imp_ldif_update_row( clist );
247
        gtk_clist_select_row( clist, row, 0 );
248
        gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), "" );
249
        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
250
        gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( impldif_dlg.check_select), FALSE );
251
}
252

    
253
/*
254
* Move off fields page.
255
* return: TRUE if OK to move off page.
256
*/
257
static gboolean imp_ldif_field_move() {
258
        gboolean retVal = FALSE;
259
        gchar *newFile;
260
        AddressBookFile *abf = NULL;
261

    
262
        if( _importedBook_ ) {
263
                addrbook_free_book( _importedBook_ );
264
        }
265

    
266
        abf = addrbook_create_book();
267
        addrbook_set_path( abf, _imp_addressIndex_->filePath );
268
        addrbook_set_name( abf, impldif_dlg.nameBook );
269
        newFile = imp_ldif_guess_file( abf );
270
        addrbook_set_file( abf, newFile );
271
        g_free( newFile );
272

    
273
        /* Import data into file */
274
        if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
275
                addrbook_save_data( abf );
276
                abf->dirtyFlag = TRUE;
277
                _importedBook_ = abf;
278
                retVal = TRUE;
279
        }
280
        else {
281
                addrbook_free_book( abf );
282
        }
283

    
284
        return retVal;
285
}
286

    
287
/*
288
* Move off fields page.
289
* return: TRUE if OK to move off page.
290
*/
291
static gboolean imp_ldif_file_move() {
292
        gboolean retVal = FALSE;
293
        gchar *sName;
294
        gchar *sFile;
295
        gchar *sMsg = NULL;
296
        gboolean errFlag = FALSE;
297

    
298
        sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.file_entry), 0, -1 );
299
        g_strchug( sFile ); g_strchomp( sFile );
300

    
301
        sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_entry), 0, -1 );
302
        g_strchug( sName ); g_strchomp( sName );
303

    
304
        g_free( impldif_dlg.nameBook );
305
        g_free( impldif_dlg.fileName );
306
        impldif_dlg.nameBook = sName;
307
        impldif_dlg.fileName = sFile;
308

    
309
        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), sFile );
310
        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_entry), sName );
311

    
312
        if( *sFile == '\0'|| strlen( sFile ) < 1 ) {
313
                sMsg = _( "Please select a file." );
314
                gtk_widget_grab_focus(impldif_dlg.file_entry);
315
                errFlag = TRUE;
316
        }
317

    
318
        if( *sName == '\0'|| strlen( sName ) < 1 ) {
319
                if( ! errFlag ) sMsg = _( "Address book name must be supplied." );
320
                gtk_widget_grab_focus(impldif_dlg.name_entry);
321
                errFlag = TRUE;
322
        }
323

    
324
        if( ! errFlag ) {
325
                /* Read attribute list */
326
                ldif_set_file( _ldifFile_, sFile );
327
                if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
328
                        /* Load fields */
329
                        /* ldif_print_file( _ldifFile_, stdout ); */
330
                        imp_ldif_load_fields( _ldifFile_ );
331
                        retVal = TRUE;
332
                }
333
                else {
334
                        sMsg = _( "Error reading LDIF fields." );
335
                }
336
        }
337
        imp_ldif_status_show( sMsg );
338

    
339
        return retVal;
340
}
341

    
342
/*
343
 * Display finish page.
344
 */
345
static void imp_ldif_finish_show() {
346
        gchar *sMsg;
347
        gchar *name;
348

    
349
        name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_entry), 0, -1 );
350
        gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name );
351
        g_free( name );
352
        gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path );
353
        gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) );
354
        gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
355
        gtk_widget_set_sensitive( impldif_dlg.btnNext, FALSE );
356
        if( _ldifFile_->retVal == MGU_SUCCESS ) {
357
                sMsg = _( "LDIF file imported successfully." );
358
        }
359
        else {
360
                sMsg = mgu_error2string( _ldifFile_->retVal );
361
        }
362
        imp_ldif_status_show( sMsg );
363
        gtk_widget_grab_focus(impldif_dlg.btnCancel);
364
}
365

    
366
static void imp_ldif_prev( GtkWidget *widget ) {
367
        gint pageNum;
368

    
369
        pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
370
        if( pageNum == PAGE_ATTRIBUTES ) {
371
                /* Goto file page stuff */
372
                gtk_notebook_set_current_page(
373
                        GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
374
                gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
375
        }
376
        imp_ldif_message();
377
}
378

    
379
static void imp_ldif_next( GtkWidget *widget ) {
380
        gint pageNum;
381

    
382
        pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
383
        if( pageNum == PAGE_FILE_INFO ) {
384
                /* Goto attributes stuff */
385
                if( imp_ldif_file_move() ) {
386
                        gtk_notebook_set_current_page(
387
                                GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES );
388
                        imp_ldif_message();
389
                        gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE );
390
                }
391
                else {
392
                        gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
393
                }
394
        }
395
        else if( pageNum == PAGE_ATTRIBUTES ) {
396
                /* Goto finish stuff */
397
                if( imp_ldif_field_move() ) {
398
                        gtk_notebook_set_current_page(
399
                                GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH );
400
                        imp_ldif_finish_show();
401
                }
402
        }
403
}
404

    
405
static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) {
406
        gint pageNum;
407

    
408
        pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) );
409
        if( pageNum != PAGE_FINISH ) {
410
                impldif_dlg.cancelled = TRUE;
411
        }
412
        gtk_main_quit();
413
}
414

    
415
static void imp_ldif_file_ok( GtkWidget *widget, gpointer data ) {
416
        const gchar *sFile;
417
        AddressFileSelection *afs;
418
        GtkWidget *fileSel;
419

    
420
        afs = ( AddressFileSelection * ) data;
421
        fileSel = afs->fileSelector;
422
        sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
423

    
424
        afs->cancelled = FALSE;
425
        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), sFile );
426
        gtk_widget_hide( afs->fileSelector );
427
        gtk_grab_remove( afs->fileSelector );
428
        gtk_widget_grab_focus( impldif_dlg.file_entry );
429
}
430

    
431
static void imp_ldif_file_cancel( GtkWidget *widget, gpointer data ) {
432
        AddressFileSelection *afs = ( AddressFileSelection * ) data;
433
        afs->cancelled = TRUE;
434
        gtk_widget_hide( afs->fileSelector );
435
        gtk_grab_remove( afs->fileSelector );
436
        gtk_widget_grab_focus( impldif_dlg.file_entry );
437
}
438

    
439
static void imp_ldif_file_select_create( AddressFileSelection *afs ) {
440
        GtkWidget *fileSelector;
441

    
442
        fileSelector = gtk_file_selection_new( _("Select LDIF File") );
443
        gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
444
        g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
445
                          "clicked", G_CALLBACK (imp_ldif_file_ok), ( gpointer ) afs );
446
        g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
447
                          "clicked", G_CALLBACK (imp_ldif_file_cancel), ( gpointer ) afs );
448
        afs->fileSelector = fileSelector;
449
        afs->cancelled = TRUE;
450
}
451

    
452
static void imp_ldif_file_select( void ) {
453
        gchar *sFile;
454
        if( ! _imp_ldif_file_selector_.fileSelector )
455
                imp_ldif_file_select_create( & _imp_ldif_file_selector_ );
456

    
457
        sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.file_entry), 0, -1 );
458
        gtk_file_selection_set_filename(
459
                GTK_FILE_SELECTION( _imp_ldif_file_selector_.fileSelector ),
460
                sFile );
461
        g_free( sFile );
462
        gtk_widget_show( _imp_ldif_file_selector_.fileSelector );
463
        gtk_grab_add( _imp_ldif_file_selector_.fileSelector );
464
}
465

    
466
static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
467
        imp_ldif_cancel( widget, data );
468
        return TRUE;
469
}
470

    
471
static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
472
        if (event && event->keyval == GDK_Escape) {
473
                imp_ldif_cancel( widget, data );
474
        }
475
        return FALSE;
476
}
477

    
478
static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) {
479
        GtkWidget *vbox;
480
        GtkWidget *table;
481
        GtkWidget *label;
482
        GtkWidget *file_entry;
483
        GtkWidget *name_entry;
484
        GtkWidget *file_btn;
485
        gint top;
486

    
487
        vbox = gtk_vbox_new(FALSE, 8);
488
        gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
489
        gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
490

    
491
        label = gtk_label_new( pageLbl );
492
        gtk_widget_show( label );
493
        gtk_notebook_set_tab_label(
494
                GTK_NOTEBOOK( impldif_dlg.notebook ),
495
                gtk_notebook_get_nth_page(
496
                        GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
497
                label );
498

    
499
        table = gtk_table_new(2, 3, FALSE);
500
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
501
        gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
502
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
503
        gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
504

    
505
        /* First row */
506
        top = 0;
507
        label = gtk_label_new(_("Address Book"));
508
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
509
                GTK_FILL, 0, 0, 0);
510
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
511

    
512
        name_entry = gtk_entry_new();
513
        gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1),
514
                GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
515

    
516
        /* Second row */
517
        top = 1;
518
        label = gtk_label_new(_("File Name"));
519
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1),
520
                GTK_FILL, 0, 0, 0);
521
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
522

    
523
        file_entry = gtk_entry_new();
524
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1),
525
                GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
526

    
527
        file_btn = gtk_button_new_with_label( _(" ... "));
528
        gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1),
529
                GTK_FILL, 0, 3, 0);
530

    
531
        gtk_widget_show_all(vbox);
532

    
533
        /* Button handler */
534
        g_signal_connect(G_OBJECT(file_btn), "clicked",
535
                         G_CALLBACK(imp_ldif_file_select), NULL);
536

    
537
        impldif_dlg.file_entry = file_entry;
538
        impldif_dlg.name_entry = name_entry;
539
}
540

    
541
static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) {
542
        GtkWidget *vbox;
543
        GtkWidget *vboxt;
544
        GtkWidget *vboxb;
545
        GtkWidget *buttonMod;
546

    
547
        GtkWidget *table;
548
        GtkWidget *label;
549
        GtkWidget *clist_swin;
550
        GtkWidget *clist_field;
551
        GtkWidget *name_ldif;
552
        GtkWidget *name_attrib;
553
        GtkWidget *check_select;
554
        gint top;
555

    
556
        gchar *titles[ FIELDS_N_COLS ];
557
        gint i;
558

    
559
        titles[ FIELD_COL_SELECT ] = _("S");
560
        titles[ FIELD_COL_FIELD  ] = _("LDIF Field");
561
        titles[ FIELD_COL_ATTRIB ] = _("Attribute Name");
562

    
563
        vbox = gtk_vbox_new(FALSE, 8);
564
        gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
565
        gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
566

    
567
        label = gtk_label_new( pageLbl );
568
        gtk_widget_show( label );
569
        gtk_notebook_set_tab_label(
570
                GTK_NOTEBOOK( impldif_dlg.notebook ),
571
                gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ),
572
                label );
573

    
574
        /* Upper area - Field list */
575
        vboxt = gtk_vbox_new( FALSE, 4 );
576
        gtk_container_add( GTK_CONTAINER( vbox ), vboxt );
577

    
578
        clist_swin = gtk_scrolled_window_new( NULL, NULL );
579
        gtk_container_add( GTK_CONTAINER(vboxt), clist_swin );
580
        gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
581
                                       GTK_POLICY_AUTOMATIC,
582
                                       GTK_POLICY_ALWAYS);
583

    
584
        clist_field = gtk_clist_new_with_titles( FIELDS_N_COLS, titles );
585
        gtk_container_add( GTK_CONTAINER(clist_swin), clist_field );
586
        gtk_clist_set_selection_mode( GTK_CLIST(clist_field), GTK_SELECTION_BROWSE );
587
        gtk_clist_set_column_width(
588
                        GTK_CLIST(clist_field), FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT );
589
        gtk_clist_set_column_width(
590
                        GTK_CLIST(clist_field), FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD );
591
        gtk_clist_set_column_width(
592
                        GTK_CLIST(clist_field), FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB );
593

    
594
        for( i = 0; i < FIELDS_N_COLS; i++ )
595
                GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_field)->column[i].button, GTK_CAN_FOCUS);
596

    
597
        /* Lower area - Edit area */
598
        vboxb = gtk_vbox_new( FALSE, 4 );
599
        gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
600

    
601
        /* Data entry area */
602
        table = gtk_table_new( 3, 3, FALSE);
603
        gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
604
        gtk_table_set_row_spacings(GTK_TABLE(table), 4);
605
        gtk_table_set_col_spacings(GTK_TABLE(table), 4);
606

    
607
        /* First row */
608
        top = 0;
609
        label = gtk_label_new(_("LDIF Field"));
610
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
611
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
612

    
613
        name_ldif = gtk_label_new( "" );
614
        gtk_misc_set_alignment(GTK_MISC(name_ldif), 0.01, 0.5);
615
        gtk_table_attach(GTK_TABLE(table), name_ldif, 1, 3, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
616

    
617
        /* Second row */
618
        ++top;
619
        label = gtk_label_new(_("Attribute"));
620
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
621
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
622

    
623
        name_attrib = gtk_entry_new();
624
        gtk_table_attach(GTK_TABLE(table), name_attrib, 1, 3, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
625

    
626
        /* Next row */
627
        ++top;
628
        label = gtk_label_new(_("Select"));
629
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
630
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
631

    
632
        check_select = gtk_check_button_new();
633
        gtk_table_attach(GTK_TABLE(table), check_select, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
634

    
635
        buttonMod = gtk_button_new_with_label( _("Modify"));
636
        gtk_table_attach(GTK_TABLE(table), buttonMod, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
637

    
638
        gtk_widget_show_all(vbox);
639

    
640
        /* Event handlers */
641
        g_signal_connect( G_OBJECT(clist_field), "select_row",
642
                          G_CALLBACK(imp_ldif_field_list_selected), NULL );
643
        g_signal_connect( G_OBJECT(clist_field), "button_press_event",
644
                          G_CALLBACK(imp_ldif_field_list_toggle), NULL );
645
        g_signal_connect( G_OBJECT(buttonMod), "clicked",
646
                          G_CALLBACK(imp_ldif_modify_pressed), NULL );
647

    
648
        impldif_dlg.clist_field  = clist_field;
649
        impldif_dlg.name_ldif    = name_ldif;
650
        impldif_dlg.name_attrib  = name_attrib;
651
        impldif_dlg.check_select = check_select;
652
}
653

    
654
static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) {
655
        GtkWidget *vbox;
656
        GtkWidget *table;
657
        GtkWidget *label;
658
        GtkWidget *labelBook;
659
        GtkWidget *labelFile;
660
        GtkWidget *labelRecs;
661
        gint top;
662

    
663
        vbox = gtk_vbox_new(FALSE, 8);
664
        gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox );
665
        gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH );
666

    
667
        label = gtk_label_new( pageLbl );
668
        gtk_widget_show( label );
669
        gtk_notebook_set_tab_label(
670
                GTK_NOTEBOOK( impldif_dlg.notebook ),
671
                gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ), label );
672

    
673
        table = gtk_table_new(3, 2, FALSE);
674
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
675
        gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
676
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
677
        gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
678

    
679
        /* First row */
680
        top = 0;
681
        label = gtk_label_new(_("Address Book :"));
682
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
683
        gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
684

    
685
        labelBook = gtk_label_new("");
686
        gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
687
        gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5);
688

    
689
        /* Second row */
690
        top++;
691
        label = gtk_label_new(_("File Name :"));
692
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
693
        gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
694

    
695
        labelFile = gtk_label_new("");
696
        gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
697
        gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5);
698

    
699
        /* Third row */
700
        top++;
701
        label = gtk_label_new(_("Records :"));
702
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
703
        gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5);
704

    
705
        labelRecs = gtk_label_new("");
706
        gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0);
707
        gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5);
708

    
709
        impldif_dlg.labelBook    = labelBook;
710
        impldif_dlg.labelFile    = labelFile;
711
        impldif_dlg.labelRecords = labelRecs;
712
}
713

    
714
static void imp_ldif_dialog_create() {
715
        GtkWidget *window;
716
        GtkWidget *vbox;
717
        GtkWidget *vnbox;
718
        GtkWidget *notebook;
719
        GtkWidget *hbbox;
720
        GtkWidget *btnPrev;
721
        GtkWidget *btnNext;
722
        GtkWidget *btnCancel;
723
        GtkWidget *hsbox;
724
        GtkWidget *statusbar;
725

    
726
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
727
        gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT );
728
        gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
729
        gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
730
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
731
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);        
732
        g_signal_connect(G_OBJECT(window), "delete_event",
733
                         G_CALLBACK(imp_ldif_delete_event), NULL);
734
        g_signal_connect(G_OBJECT(window), "key_press_event",
735
                         G_CALLBACK(imp_ldif_key_pressed), NULL);
736

    
737
        vbox = gtk_vbox_new(FALSE, 4);
738
        gtk_widget_show(vbox);
739
        gtk_container_add(GTK_CONTAINER(window), vbox);
740

    
741
        vnbox = gtk_vbox_new(FALSE, 4);
742
        gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
743
        gtk_widget_show(vnbox);
744
        gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
745

    
746
        /* Notebook */
747
        notebook = gtk_notebook_new();
748
        gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE );
749
        gtk_widget_show(notebook);
750
        gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
751
        gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
752

    
753
        /* Status line */
754
        hsbox = gtk_hbox_new(FALSE, 0);
755
        gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
756
        statusbar = gtk_statusbar_new();
757
        gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
758

    
759
        /* Button panel */
760
        gtkut_button_set_create(&hbbox, &btnPrev, _( "Prev" ),
761
                                &btnNext, _( "Next" ),
762
                                &btnCancel, _( "Cancel" ) );
763
        gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
764
        gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
765
        gtk_widget_grab_default(btnNext);
766

    
767
        /* Button handlers */
768
        g_signal_connect(G_OBJECT(btnPrev), "clicked",
769
                         G_CALLBACK(imp_ldif_prev), NULL);
770
        g_signal_connect(G_OBJECT(btnNext), "clicked",
771
                         G_CALLBACK(imp_ldif_next), NULL);
772
        g_signal_connect(G_OBJECT(btnCancel), "clicked",
773
                         G_CALLBACK(imp_ldif_cancel), NULL);
774

    
775
        gtk_widget_show_all(vbox);
776

    
777
        impldif_dlg.window     = window;
778
        impldif_dlg.notebook   = notebook;
779
        impldif_dlg.btnPrev    = btnPrev;
780
        impldif_dlg.btnNext    = btnNext;
781
        impldif_dlg.btnCancel  = btnCancel;
782
        impldif_dlg.statusbar  = statusbar;
783
        impldif_dlg.status_cid = gtk_statusbar_get_context_id(
784
                        GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
785

    
786
}
787

    
788
static void imp_ldif_create() {
789
        imp_ldif_dialog_create();
790
        imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
791
        imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
792
        imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
793
        gtk_widget_show_all( impldif_dlg.window );
794
}
795

    
796
AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
797
        _importedBook_ = NULL;
798
        _imp_addressIndex_ = addrIndex;
799

    
800
        if( ! impldif_dlg.window )
801
                imp_ldif_create();
802
        impldif_dlg.cancelled = FALSE;
803
        gtk_widget_show(impldif_dlg.window);
804
        manage_window_set_transient(GTK_WINDOW(impldif_dlg.window));
805
        gtk_widget_grab_default(impldif_dlg.btnNext);
806

    
807
        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_entry), IMPORTLDIF_GUESS_NAME );
808
        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), "" );
809
        gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), "" );
810
        gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
811
        gtk_clist_clear( GTK_CLIST(impldif_dlg.clist_field) );
812
        gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO );
813
        gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE );
814
        gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE );
815
        stock_pixmap_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK,
816
                          &markxpm, &markxpmmask );
817
        imp_ldif_message();
818
        gtk_widget_grab_focus(impldif_dlg.file_entry);
819

    
820
        impldif_dlg.rowIndSelect = -1;
821
        impldif_dlg.rowCount = 0;
822
        g_free( impldif_dlg.nameBook );
823
        g_free( impldif_dlg.fileName );
824
        impldif_dlg.nameBook = NULL;
825
        impldif_dlg.fileName = NULL;
826

    
827
        _ldifFile_ = ldif_create();
828
        gtk_main();
829
        gtk_widget_hide(impldif_dlg.window);
830
        ldif_free( _ldifFile_ );
831
        _ldifFile_ = NULL;
832
        _imp_addressIndex_ = NULL;
833

    
834
        g_free( impldif_dlg.nameBook );
835
        g_free( impldif_dlg.fileName );
836
        impldif_dlg.nameBook = NULL;
837
        impldif_dlg.fileName = NULL;
838

    
839
        if( impldif_dlg.cancelled == TRUE ) return NULL;
840
        return _importedBook_;
841
}
842

    
843
/*
844
* End of Source.
845
*/
846