root / src / importldif.c @ 1060
History | View | Annotate | Download (25.8 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 <glib/gi18n.h> |
| 32 | #include <gdk/gdkkeysyms.h> |
| 33 | #include <gtk/gtkwindow.h> |
| 34 | #include <gtk/gtksignal.h> |
| 35 | #include <gtk/gtkvbox.h> |
| 36 | #include <gtk/gtklabel.h> |
| 37 | #include <gtk/gtkentry.h> |
| 38 | #include <gtk/gtktable.h> |
| 39 | #include <gtk/gtkbutton.h> |
| 40 | #include <gtk/gtkcheckbutton.h> |
| 41 | #include <gtk/gtktogglebutton.h> |
| 42 | #include <gtk/gtkstatusbar.h> |
| 43 | #include <gtk/gtknotebook.h> |
| 44 | #include <gtk/gtkfilesel.h> |
| 45 | #include <gtk/gtkstock.h> |
| 46 | |
| 47 | #include "addrbook.h" |
| 48 | #include "addressbook.h" |
| 49 | #include "addressitem.h" |
| 50 | #include "gtkutils.h" |
| 51 | #include "stock_pixmap.h" |
| 52 | #include "prefs_common.h" |
| 53 | #include "manage_window.h" |
| 54 | #include "mgutils.h" |
| 55 | #include "ldif.h" |
| 56 | #include "utils.h" |
| 57 | |
| 58 | #define IMPORTLDIF_GUESS_NAME "LDIF Import" |
| 59 | |
| 60 | #define PAGE_FILE_INFO 0 |
| 61 | #define PAGE_ATTRIBUTES 1 |
| 62 | #define PAGE_FINISH 2 |
| 63 | |
| 64 | #define IMPORTLDIF_WIDTH 380 |
| 65 | #define IMPORTLDIF_HEIGHT 300 |
| 66 | |
| 67 | #define FIELDS_N_COLS 3 |
| 68 | #define FIELDS_COL_WIDTH_SELECT 10 |
| 69 | #define FIELDS_COL_WIDTH_FIELD 140 |
| 70 | #define FIELDS_COL_WIDTH_ATTRIB 140 |
| 71 | |
| 72 | typedef enum { |
| 73 | FIELD_COL_SELECT = 0,
|
| 74 | FIELD_COL_FIELD = 1,
|
| 75 | FIELD_COL_ATTRIB = 2
|
| 76 | } ImpLdif_FieldColPos; |
| 77 | |
| 78 | static struct _ImpLdif_Dlg { |
| 79 | GtkWidget *window; |
| 80 | GtkWidget *notebook; |
| 81 | GtkWidget *file_entry; |
| 82 | GtkWidget *name_entry; |
| 83 | GtkWidget *clist_field; |
| 84 | GtkWidget *name_ldif; |
| 85 | GtkWidget *name_attrib; |
| 86 | GtkWidget *check_select; |
| 87 | GtkWidget *labelBook; |
| 88 | GtkWidget *labelFile; |
| 89 | GtkWidget *labelRecords; |
| 90 | GtkWidget *btnPrev; |
| 91 | GtkWidget *btnNext; |
| 92 | GtkWidget *btnCancel; |
| 93 | GtkWidget *statusbar; |
| 94 | gint status_cid; |
| 95 | gint rowIndSelect; |
| 96 | gint rowCount; |
| 97 | gchar *nameBook; |
| 98 | gchar *fileName; |
| 99 | gboolean cancelled; |
| 100 | } impldif_dlg; |
| 101 | |
| 102 | static struct _AddressFileSelection _imp_ldif_file_selector_; |
| 103 | static AddressBookFile *_importedBook_;
|
| 104 | static AddressIndex *_imp_addressIndex_;
|
| 105 | static LdifFile *_ldifFile_ = NULL; |
| 106 | |
| 107 | static GdkPixmap *markxpm;
|
| 108 | static GdkBitmap *markxpmmask;
|
| 109 | |
| 110 | static void imp_ldif_status_show( gchar *msg ) { |
| 111 | if( impldif_dlg.statusbar != NULL ) { |
| 112 | gtk_statusbar_pop( GTK_STATUSBAR(impldif_dlg.statusbar), impldif_dlg.status_cid ); |
| 113 | if( msg ) {
|
| 114 | gtk_statusbar_push( GTK_STATUSBAR(impldif_dlg.statusbar), impldif_dlg.status_cid, msg ); |
| 115 | } |
| 116 | } |
| 117 | } |
| 118 | |
| 119 | static void imp_ldif_message( void ) { |
| 120 | gchar *sMsg = NULL;
|
| 121 | gint pageNum; |
| 122 | |
| 123 | pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) ); |
| 124 | if( pageNum == PAGE_FILE_INFO ) {
|
| 125 | sMsg = _( "Please specify address book name and file to import." );
|
| 126 | } |
| 127 | else if( pageNum == PAGE_ATTRIBUTES ) { |
| 128 | sMsg = _( "Select and rename LDIF field names to import." );
|
| 129 | } |
| 130 | else if( pageNum == PAGE_FINISH ) { |
| 131 | sMsg = _( "File imported." );
|
| 132 | } |
| 133 | imp_ldif_status_show( sMsg ); |
| 134 | } |
| 135 | |
| 136 | static gchar *imp_ldif_guess_file( AddressBookFile *abf ) {
|
| 137 | gchar *newFile = NULL;
|
| 138 | GList *fileList = NULL;
|
| 139 | gint fileNum = 1;
|
| 140 | fileList = addrbook_get_bookfile_list( abf ); |
| 141 | if( fileList ) {
|
| 142 | fileNum = 1 + abf->maxValue;
|
| 143 | } |
| 144 | newFile = addrbook_gen_new_file_name( fileNum ); |
| 145 | g_list_free( fileList ); |
| 146 | fileList = NULL;
|
| 147 | return newFile;
|
| 148 | } |
| 149 | |
| 150 | static void imp_ldif_update_row( GtkCList *clist ) { |
| 151 | Ldif_FieldRec *rec; |
| 152 | gchar *text[ FIELDS_N_COLS ]; |
| 153 | gint row; |
| 154 | |
| 155 | if( impldif_dlg.rowIndSelect < 0 ) return; |
| 156 | row = impldif_dlg.rowIndSelect; |
| 157 | |
| 158 | rec = gtk_clist_get_row_data( clist, row ); |
| 159 | text[ FIELD_COL_SELECT ] = "";
|
| 160 | text[ FIELD_COL_FIELD ] = rec->tagName; |
| 161 | text[ FIELD_COL_ATTRIB ] = rec->userName; |
| 162 | |
| 163 | gtk_clist_freeze( clist ); |
| 164 | gtk_clist_remove( clist, row ); |
| 165 | if( row == impldif_dlg.rowCount - 1 ) { |
| 166 | gtk_clist_append( clist, text ); |
| 167 | } |
| 168 | else {
|
| 169 | gtk_clist_insert( clist, row, text ); |
| 170 | } |
| 171 | if( rec->selected )
|
| 172 | gtk_clist_set_pixmap( clist, row, FIELD_COL_SELECT, markxpm, markxpmmask ); |
| 173 | |
| 174 | gtk_clist_set_row_data( clist, row, rec ); |
| 175 | gtk_clist_thaw( clist ); |
| 176 | } |
| 177 | |
| 178 | static void imp_ldif_load_fields( LdifFile *ldf ) { |
| 179 | GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field); |
| 180 | GList *node, *list; |
| 181 | gchar *text[ FIELDS_N_COLS ]; |
| 182 | |
| 183 | impldif_dlg.rowIndSelect = -1;
|
| 184 | impldif_dlg.rowCount = 0;
|
| 185 | if( ! ldf->accessFlag ) return; |
| 186 | gtk_clist_clear( clist ); |
| 187 | list = ldif_get_fieldlist( ldf ); |
| 188 | node = list; |
| 189 | while( node ) {
|
| 190 | Ldif_FieldRec *rec = node->data; |
| 191 | gint row; |
| 192 | |
| 193 | if( ! rec->reserved ) {
|
| 194 | text[ FIELD_COL_SELECT ] = "";
|
| 195 | text[ FIELD_COL_FIELD ] = rec->tagName; |
| 196 | text[ FIELD_COL_ATTRIB ] = rec->userName; |
| 197 | row = gtk_clist_append( clist, text ); |
| 198 | gtk_clist_set_row_data( clist, row, rec ); |
| 199 | if( rec->selected )
|
| 200 | gtk_clist_set_pixmap( clist, row, FIELD_COL_SELECT, markxpm, markxpmmask ); |
| 201 | impldif_dlg.rowCount++; |
| 202 | } |
| 203 | node = g_list_next( node ); |
| 204 | } |
| 205 | g_list_free( list ); |
| 206 | list = NULL;
|
| 207 | ldif_set_accessed( ldf, FALSE ); |
| 208 | } |
| 209 | |
| 210 | static void imp_ldif_field_list_selected( GtkCList *clist, gint row, gint column, GdkEvent *event, gpointer data ) { |
| 211 | Ldif_FieldRec *rec = gtk_clist_get_row_data( clist, row ); |
| 212 | |
| 213 | impldif_dlg.rowIndSelect = row; |
| 214 | gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
|
| 215 | if( rec ) {
|
| 216 | gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), rec->tagName ); |
| 217 | if( rec->userName )
|
| 218 | gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), rec->userName ); |
| 219 | gtk_toggle_button_set_active( |
| 220 | GTK_TOGGLE_BUTTON( impldif_dlg.check_select), |
| 221 | rec->selected ); |
| 222 | } |
| 223 | gtk_widget_grab_focus(impldif_dlg.name_attrib); |
| 224 | } |
| 225 | |
| 226 | static gboolean imp_ldif_field_list_toggle( GtkCList *clist, GdkEventButton *event, gpointer data ) {
|
| 227 | if( ! event ) return FALSE; |
| 228 | if( impldif_dlg.rowIndSelect < 0 ) return FALSE; |
| 229 | if( event->button == 1 ) { |
| 230 | if( event->type == GDK_2BUTTON_PRESS ) {
|
| 231 | Ldif_FieldRec *rec = gtk_clist_get_row_data( clist, impldif_dlg.rowIndSelect ); |
| 232 | if( rec ) {
|
| 233 | rec->selected = ! rec->selected; |
| 234 | imp_ldif_update_row( clist ); |
| 235 | } |
| 236 | } |
| 237 | } |
| 238 | return FALSE;
|
| 239 | } |
| 240 | |
| 241 | static void imp_ldif_modify_pressed( GtkWidget *widget, gpointer data ) { |
| 242 | GtkCList *clist = GTK_CLIST(impldif_dlg.clist_field); |
| 243 | Ldif_FieldRec *rec; |
| 244 | gint row; |
| 245 | |
| 246 | if( impldif_dlg.rowIndSelect < 0 ) return; |
| 247 | row = impldif_dlg.rowIndSelect; |
| 248 | rec = gtk_clist_get_row_data( clist, impldif_dlg.rowIndSelect ); |
| 249 | |
| 250 | g_free( rec->userName ); |
| 251 | rec->userName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_attrib), 0, -1 ); |
| 252 | rec->selected = gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON( impldif_dlg.check_select) ); |
| 253 | imp_ldif_update_row( clist ); |
| 254 | gtk_clist_select_row( clist, row, 0 );
|
| 255 | gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), "" );
|
| 256 | gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
|
| 257 | gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( impldif_dlg.check_select), FALSE ); |
| 258 | } |
| 259 | |
| 260 | /*
|
| 261 | * Move off fields page. |
| 262 | * return: TRUE if OK to move off page. |
| 263 | */ |
| 264 | static gboolean imp_ldif_field_move() {
|
| 265 | gboolean retVal = FALSE; |
| 266 | gchar *newFile; |
| 267 | AddressBookFile *abf = NULL;
|
| 268 | |
| 269 | if( _importedBook_ ) {
|
| 270 | addrbook_free_book( _importedBook_ ); |
| 271 | } |
| 272 | |
| 273 | abf = addrbook_create_book(); |
| 274 | addrbook_set_path( abf, _imp_addressIndex_->filePath ); |
| 275 | addrbook_set_name( abf, impldif_dlg.nameBook ); |
| 276 | newFile = imp_ldif_guess_file( abf ); |
| 277 | addrbook_set_file( abf, newFile ); |
| 278 | g_free( newFile ); |
| 279 | |
| 280 | /* Import data into file */
|
| 281 | if( ldif_import_data( _ldifFile_, abf->addressCache ) == MGU_SUCCESS ) {
|
| 282 | addrbook_save_data( abf ); |
| 283 | abf->dirtyFlag = TRUE; |
| 284 | _importedBook_ = abf; |
| 285 | retVal = TRUE; |
| 286 | } |
| 287 | else {
|
| 288 | addrbook_free_book( abf ); |
| 289 | } |
| 290 | |
| 291 | return retVal;
|
| 292 | } |
| 293 | |
| 294 | /*
|
| 295 | * Move off fields page. |
| 296 | * return: TRUE if OK to move off page. |
| 297 | */ |
| 298 | static gboolean imp_ldif_file_move() {
|
| 299 | gboolean retVal = FALSE; |
| 300 | gchar *sName; |
| 301 | gchar *sFile; |
| 302 | gchar *sMsg = NULL;
|
| 303 | gboolean errFlag = FALSE; |
| 304 | |
| 305 | sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.file_entry), 0, -1 ); |
| 306 | g_strchug( sFile ); g_strchomp( sFile ); |
| 307 | |
| 308 | sName = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_entry), 0, -1 ); |
| 309 | g_strchug( sName ); g_strchomp( sName ); |
| 310 | |
| 311 | g_free( impldif_dlg.nameBook ); |
| 312 | g_free( impldif_dlg.fileName ); |
| 313 | impldif_dlg.nameBook = sName; |
| 314 | impldif_dlg.fileName = sFile; |
| 315 | |
| 316 | gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), sFile ); |
| 317 | gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_entry), sName ); |
| 318 | |
| 319 | if( *sFile == '\0'|| strlen( sFile ) < 1 ) { |
| 320 | sMsg = _( "Please select a file." );
|
| 321 | gtk_widget_grab_focus(impldif_dlg.file_entry); |
| 322 | errFlag = TRUE; |
| 323 | } |
| 324 | |
| 325 | if( *sName == '\0'|| strlen( sName ) < 1 ) { |
| 326 | if( ! errFlag ) sMsg = _( "Address book name must be supplied." ); |
| 327 | gtk_widget_grab_focus(impldif_dlg.name_entry); |
| 328 | errFlag = TRUE; |
| 329 | } |
| 330 | |
| 331 | if( ! errFlag ) {
|
| 332 | /* Read attribute list */
|
| 333 | ldif_set_file( _ldifFile_, sFile ); |
| 334 | if( ldif_read_tags( _ldifFile_ ) == MGU_SUCCESS ) {
|
| 335 | /* Load fields */
|
| 336 | /* ldif_print_file( _ldifFile_, stdout ); */
|
| 337 | imp_ldif_load_fields( _ldifFile_ ); |
| 338 | retVal = TRUE; |
| 339 | } |
| 340 | else {
|
| 341 | sMsg = _( "Error reading LDIF fields." );
|
| 342 | } |
| 343 | } |
| 344 | imp_ldif_status_show( sMsg ); |
| 345 | |
| 346 | return retVal;
|
| 347 | } |
| 348 | |
| 349 | /*
|
| 350 | * Display finish page. |
| 351 | */ |
| 352 | static void imp_ldif_finish_show() { |
| 353 | gchar *sMsg; |
| 354 | gchar *name; |
| 355 | |
| 356 | name = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.name_entry), 0, -1 ); |
| 357 | gtk_label_set_text( GTK_LABEL(impldif_dlg.labelBook), name ); |
| 358 | g_free( name ); |
| 359 | gtk_label_set_text( GTK_LABEL(impldif_dlg.labelFile), _ldifFile_->path ); |
| 360 | gtk_label_set_text( GTK_LABEL(impldif_dlg.labelRecords), itos( _ldifFile_->importCount ) ); |
| 361 | gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE ); |
| 362 | gtk_widget_set_sensitive( impldif_dlg.btnNext, FALSE ); |
| 363 | if( _ldifFile_->retVal == MGU_SUCCESS ) {
|
| 364 | sMsg = _( "LDIF file imported successfully." );
|
| 365 | } |
| 366 | else {
|
| 367 | sMsg = mgu_error2string( _ldifFile_->retVal ); |
| 368 | } |
| 369 | imp_ldif_status_show( sMsg ); |
| 370 | gtk_widget_grab_focus(impldif_dlg.btnCancel); |
| 371 | } |
| 372 | |
| 373 | static void imp_ldif_prev( GtkWidget *widget ) { |
| 374 | gint pageNum; |
| 375 | |
| 376 | pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) ); |
| 377 | if( pageNum == PAGE_ATTRIBUTES ) {
|
| 378 | /* Goto file page stuff */
|
| 379 | gtk_notebook_set_current_page( |
| 380 | GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO ); |
| 381 | gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE ); |
| 382 | } |
| 383 | imp_ldif_message(); |
| 384 | } |
| 385 | |
| 386 | static void imp_ldif_next( GtkWidget *widget ) { |
| 387 | gint pageNum; |
| 388 | |
| 389 | pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) ); |
| 390 | if( pageNum == PAGE_FILE_INFO ) {
|
| 391 | /* Goto attributes stuff */
|
| 392 | if( imp_ldif_file_move() ) {
|
| 393 | gtk_notebook_set_current_page( |
| 394 | GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_ATTRIBUTES ); |
| 395 | imp_ldif_message(); |
| 396 | gtk_widget_set_sensitive( impldif_dlg.btnPrev, TRUE ); |
| 397 | } |
| 398 | else {
|
| 399 | gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE ); |
| 400 | } |
| 401 | } |
| 402 | else if( pageNum == PAGE_ATTRIBUTES ) { |
| 403 | /* Goto finish stuff */
|
| 404 | if( imp_ldif_field_move() ) {
|
| 405 | gtk_notebook_set_current_page( |
| 406 | GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FINISH ); |
| 407 | imp_ldif_finish_show(); |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | static void imp_ldif_cancel( GtkWidget *widget, gpointer data ) { |
| 413 | gint pageNum; |
| 414 | |
| 415 | pageNum = gtk_notebook_get_current_page( GTK_NOTEBOOK(impldif_dlg.notebook) ); |
| 416 | if( pageNum != PAGE_FINISH ) {
|
| 417 | impldif_dlg.cancelled = TRUE; |
| 418 | } |
| 419 | gtk_main_quit(); |
| 420 | } |
| 421 | |
| 422 | static void imp_ldif_file_ok( GtkWidget *widget, gpointer data ) { |
| 423 | const gchar *sFile;
|
| 424 | AddressFileSelection *afs; |
| 425 | GtkWidget *fileSel; |
| 426 | |
| 427 | afs = ( AddressFileSelection * ) data; |
| 428 | fileSel = afs->fileSelector; |
| 429 | sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) ); |
| 430 | |
| 431 | afs->cancelled = FALSE; |
| 432 | gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), sFile ); |
| 433 | gtk_widget_hide( afs->fileSelector ); |
| 434 | gtk_grab_remove( afs->fileSelector ); |
| 435 | gtk_widget_grab_focus( impldif_dlg.file_entry ); |
| 436 | } |
| 437 | |
| 438 | static void imp_ldif_file_cancel( GtkWidget *widget, gpointer data ) { |
| 439 | AddressFileSelection *afs = ( AddressFileSelection * ) data; |
| 440 | afs->cancelled = TRUE; |
| 441 | gtk_widget_hide( afs->fileSelector ); |
| 442 | gtk_grab_remove( afs->fileSelector ); |
| 443 | gtk_widget_grab_focus( impldif_dlg.file_entry ); |
| 444 | } |
| 445 | |
| 446 | static void imp_ldif_file_select_create( AddressFileSelection *afs ) { |
| 447 | GtkWidget *fileSelector; |
| 448 | |
| 449 | fileSelector = gtk_file_selection_new( _("Select LDIF File") );
|
| 450 | gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) ); |
| 451 | g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button), |
| 452 | "clicked", G_CALLBACK (imp_ldif_file_ok), ( gpointer ) afs );
|
| 453 | g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button), |
| 454 | "clicked", G_CALLBACK (imp_ldif_file_cancel), ( gpointer ) afs );
|
| 455 | afs->fileSelector = fileSelector; |
| 456 | afs->cancelled = TRUE; |
| 457 | } |
| 458 | |
| 459 | static void imp_ldif_file_select( void ) { |
| 460 | gchar *sFile; |
| 461 | if( ! _imp_ldif_file_selector_.fileSelector )
|
| 462 | imp_ldif_file_select_create( & _imp_ldif_file_selector_ ); |
| 463 | |
| 464 | sFile = gtk_editable_get_chars( GTK_EDITABLE(impldif_dlg.file_entry), 0, -1 ); |
| 465 | gtk_file_selection_set_filename( |
| 466 | GTK_FILE_SELECTION( _imp_ldif_file_selector_.fileSelector ), |
| 467 | sFile ); |
| 468 | g_free( sFile ); |
| 469 | gtk_widget_show( _imp_ldif_file_selector_.fileSelector ); |
| 470 | gtk_grab_add( _imp_ldif_file_selector_.fileSelector ); |
| 471 | } |
| 472 | |
| 473 | static gint imp_ldif_delete_event( GtkWidget *widget, GdkEventAny *event, gpointer data ) {
|
| 474 | imp_ldif_cancel( widget, data ); |
| 475 | return TRUE;
|
| 476 | } |
| 477 | |
| 478 | static gboolean imp_ldif_key_pressed( GtkWidget *widget, GdkEventKey *event, gpointer data ) {
|
| 479 | if (event && event->keyval == GDK_Escape) {
|
| 480 | imp_ldif_cancel( widget, data ); |
| 481 | } |
| 482 | return FALSE;
|
| 483 | } |
| 484 | |
| 485 | static void imp_ldif_page_file( gint pageNum, gchar *pageLbl ) { |
| 486 | GtkWidget *vbox; |
| 487 | GtkWidget *table; |
| 488 | GtkWidget *label; |
| 489 | GtkWidget *file_entry; |
| 490 | GtkWidget *name_entry; |
| 491 | GtkWidget *file_btn; |
| 492 | gint top; |
| 493 | |
| 494 | vbox = gtk_vbox_new(FALSE, 8);
|
| 495 | gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox ); |
| 496 | gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH ); |
| 497 | |
| 498 | label = gtk_label_new( pageLbl ); |
| 499 | gtk_widget_show( label ); |
| 500 | gtk_notebook_set_tab_label( |
| 501 | GTK_NOTEBOOK( impldif_dlg.notebook ), |
| 502 | gtk_notebook_get_nth_page( |
| 503 | GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ), |
| 504 | label ); |
| 505 | |
| 506 | table = gtk_table_new(2, 3, FALSE); |
| 507 | gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
|
| 508 | gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
|
| 509 | gtk_table_set_row_spacings(GTK_TABLE(table), 8);
|
| 510 | gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
|
| 511 | |
| 512 | /* First row */
|
| 513 | top = 0;
|
| 514 | label = gtk_label_new(_("Address Book"));
|
| 515 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), |
| 516 | GTK_FILL, 0, 0, 0); |
| 517 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); |
| 518 | |
| 519 | name_entry = gtk_entry_new(); |
| 520 | gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), |
| 521 | GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); |
| 522 | |
| 523 | /* Second row */
|
| 524 | top = 1;
|
| 525 | label = gtk_label_new(_("File Name"));
|
| 526 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), |
| 527 | GTK_FILL, 0, 0, 0); |
| 528 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); |
| 529 | |
| 530 | file_entry = gtk_entry_new(); |
| 531 | gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), |
| 532 | GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); |
| 533 | |
| 534 | file_btn = gtk_button_new_with_label( _(" ... "));
|
| 535 | gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), |
| 536 | GTK_FILL, 0, 3, 0); |
| 537 | |
| 538 | gtk_widget_show_all(vbox); |
| 539 | |
| 540 | /* Button handler */
|
| 541 | g_signal_connect(G_OBJECT(file_btn), "clicked",
|
| 542 | G_CALLBACK(imp_ldif_file_select), NULL);
|
| 543 | |
| 544 | impldif_dlg.file_entry = file_entry; |
| 545 | impldif_dlg.name_entry = name_entry; |
| 546 | } |
| 547 | |
| 548 | static void imp_ldif_page_fields( gint pageNum, gchar *pageLbl ) { |
| 549 | GtkWidget *vbox; |
| 550 | GtkWidget *vboxt; |
| 551 | GtkWidget *vboxb; |
| 552 | GtkWidget *buttonMod; |
| 553 | |
| 554 | GtkWidget *table; |
| 555 | GtkWidget *label; |
| 556 | GtkWidget *clist_swin; |
| 557 | GtkWidget *clist_field; |
| 558 | GtkWidget *name_ldif; |
| 559 | GtkWidget *name_attrib; |
| 560 | GtkWidget *check_select; |
| 561 | gint top; |
| 562 | |
| 563 | gchar *titles[ FIELDS_N_COLS ]; |
| 564 | gint i; |
| 565 | |
| 566 | titles[ FIELD_COL_SELECT ] = _("S");
|
| 567 | titles[ FIELD_COL_FIELD ] = _("LDIF Field");
|
| 568 | titles[ FIELD_COL_ATTRIB ] = _("Attribute Name");
|
| 569 | |
| 570 | vbox = gtk_vbox_new(FALSE, 8);
|
| 571 | gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox ); |
| 572 | gtk_container_set_border_width( GTK_CONTAINER (vbox), 4 );
|
| 573 | |
| 574 | label = gtk_label_new( pageLbl ); |
| 575 | gtk_widget_show( label ); |
| 576 | gtk_notebook_set_tab_label( |
| 577 | GTK_NOTEBOOK( impldif_dlg.notebook ), |
| 578 | gtk_notebook_get_nth_page(GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ), |
| 579 | label ); |
| 580 | |
| 581 | /* Upper area - Field list */
|
| 582 | vboxt = gtk_vbox_new( FALSE, 4 );
|
| 583 | gtk_container_add( GTK_CONTAINER( vbox ), vboxt ); |
| 584 | |
| 585 | clist_swin = gtk_scrolled_window_new( NULL, NULL ); |
| 586 | gtk_container_add( GTK_CONTAINER(vboxt), clist_swin ); |
| 587 | gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin), |
| 588 | GTK_POLICY_AUTOMATIC, |
| 589 | GTK_POLICY_ALWAYS); |
| 590 | |
| 591 | clist_field = gtk_clist_new_with_titles( FIELDS_N_COLS, titles ); |
| 592 | gtk_container_add( GTK_CONTAINER(clist_swin), clist_field ); |
| 593 | gtk_clist_set_selection_mode( GTK_CLIST(clist_field), GTK_SELECTION_BROWSE ); |
| 594 | gtk_clist_set_column_width( |
| 595 | GTK_CLIST(clist_field), FIELD_COL_SELECT, FIELDS_COL_WIDTH_SELECT ); |
| 596 | gtk_clist_set_column_width( |
| 597 | GTK_CLIST(clist_field), FIELD_COL_FIELD, FIELDS_COL_WIDTH_FIELD ); |
| 598 | gtk_clist_set_column_width( |
| 599 | GTK_CLIST(clist_field), FIELD_COL_ATTRIB, FIELDS_COL_WIDTH_ATTRIB ); |
| 600 | |
| 601 | for( i = 0; i < FIELDS_N_COLS; i++ ) |
| 602 | GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist_field)->column[i].button, GTK_CAN_FOCUS); |
| 603 | |
| 604 | /* Lower area - Edit area */
|
| 605 | vboxb = gtk_vbox_new( FALSE, 4 );
|
| 606 | gtk_box_pack_end(GTK_BOX(vbox), vboxb, FALSE, FALSE, 2);
|
| 607 | |
| 608 | /* Data entry area */
|
| 609 | table = gtk_table_new( 3, 3, FALSE); |
| 610 | gtk_box_pack_start(GTK_BOX(vboxb), table, FALSE, FALSE, 0);
|
| 611 | gtk_table_set_row_spacings(GTK_TABLE(table), 4);
|
| 612 | gtk_table_set_col_spacings(GTK_TABLE(table), 4);
|
| 613 | |
| 614 | /* First row */
|
| 615 | top = 0;
|
| 616 | label = gtk_label_new(_("LDIF Field"));
|
| 617 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0); |
| 618 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); |
| 619 | |
| 620 | name_ldif = gtk_label_new( "" );
|
| 621 | gtk_misc_set_alignment(GTK_MISC(name_ldif), 0.01, 0.5); |
| 622 | gtk_table_attach(GTK_TABLE(table), name_ldif, 1, 3, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); |
| 623 | |
| 624 | /* Second row */
|
| 625 | ++top; |
| 626 | label = gtk_label_new(_("Attribute"));
|
| 627 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0); |
| 628 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); |
| 629 | |
| 630 | name_attrib = gtk_entry_new(); |
| 631 | gtk_table_attach(GTK_TABLE(table), name_attrib, 1, 3, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); |
| 632 | |
| 633 | /* Next row */
|
| 634 | ++top; |
| 635 | label = gtk_label_new(_("Select"));
|
| 636 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0); |
| 637 | gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5); |
| 638 | |
| 639 | check_select = gtk_check_button_new(); |
| 640 | gtk_table_attach(GTK_TABLE(table), check_select, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0); |
| 641 | |
| 642 | buttonMod = gtk_button_new_with_label( _("Modify"));
|
| 643 | gtk_table_attach(GTK_TABLE(table), buttonMod, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0); |
| 644 | |
| 645 | gtk_widget_show_all(vbox); |
| 646 | |
| 647 | /* Event handlers */
|
| 648 | g_signal_connect( G_OBJECT(clist_field), "select_row",
|
| 649 | G_CALLBACK(imp_ldif_field_list_selected), NULL );
|
| 650 | g_signal_connect( G_OBJECT(clist_field), "button_press_event",
|
| 651 | G_CALLBACK(imp_ldif_field_list_toggle), NULL );
|
| 652 | g_signal_connect( G_OBJECT(buttonMod), "clicked",
|
| 653 | G_CALLBACK(imp_ldif_modify_pressed), NULL );
|
| 654 | |
| 655 | impldif_dlg.clist_field = clist_field; |
| 656 | impldif_dlg.name_ldif = name_ldif; |
| 657 | impldif_dlg.name_attrib = name_attrib; |
| 658 | impldif_dlg.check_select = check_select; |
| 659 | } |
| 660 | |
| 661 | static void imp_ldif_page_finish( gint pageNum, gchar *pageLbl ) { |
| 662 | GtkWidget *vbox; |
| 663 | GtkWidget *table; |
| 664 | GtkWidget *label; |
| 665 | GtkWidget *labelBook; |
| 666 | GtkWidget *labelFile; |
| 667 | GtkWidget *labelRecs; |
| 668 | gint top; |
| 669 | |
| 670 | vbox = gtk_vbox_new(FALSE, 8);
|
| 671 | gtk_container_add( GTK_CONTAINER( impldif_dlg.notebook ), vbox ); |
| 672 | gtk_container_set_border_width( GTK_CONTAINER (vbox), BORDER_WIDTH ); |
| 673 | |
| 674 | label = gtk_label_new( pageLbl ); |
| 675 | gtk_widget_show( label ); |
| 676 | gtk_notebook_set_tab_label( |
| 677 | GTK_NOTEBOOK( impldif_dlg.notebook ), |
| 678 | gtk_notebook_get_nth_page( GTK_NOTEBOOK( impldif_dlg.notebook ), pageNum ), label ); |
| 679 | |
| 680 | table = gtk_table_new(3, 2, FALSE); |
| 681 | gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
|
| 682 | gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
|
| 683 | gtk_table_set_row_spacings(GTK_TABLE(table), 8);
|
| 684 | gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
|
| 685 | |
| 686 | /* First row */
|
| 687 | top = 0;
|
| 688 | label = gtk_label_new(_("Address Book :"));
|
| 689 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0); |
| 690 | gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); |
| 691 | |
| 692 | labelBook = gtk_label_new("");
|
| 693 | gtk_table_attach(GTK_TABLE(table), labelBook, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0); |
| 694 | gtk_misc_set_alignment(GTK_MISC(labelBook), 0, 0.5); |
| 695 | |
| 696 | /* Second row */
|
| 697 | top++; |
| 698 | label = gtk_label_new(_("File Name :"));
|
| 699 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0); |
| 700 | gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); |
| 701 | |
| 702 | labelFile = gtk_label_new("");
|
| 703 | gtk_table_attach(GTK_TABLE(table), labelFile, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0); |
| 704 | gtk_misc_set_alignment(GTK_MISC(labelFile), 0, 0.5); |
| 705 | |
| 706 | /* Third row */
|
| 707 | top++; |
| 708 | label = gtk_label_new(_("Records :"));
|
| 709 | gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0); |
| 710 | gtk_misc_set_alignment(GTK_MISC(label), 1, 0.5); |
| 711 | |
| 712 | labelRecs = gtk_label_new("");
|
| 713 | gtk_table_attach(GTK_TABLE(table), labelRecs, 1, 2, top, (top + 1), GTK_FILL, 0, 0, 0); |
| 714 | gtk_misc_set_alignment(GTK_MISC(labelRecs), 0, 0.5); |
| 715 | |
| 716 | impldif_dlg.labelBook = labelBook; |
| 717 | impldif_dlg.labelFile = labelFile; |
| 718 | impldif_dlg.labelRecords = labelRecs; |
| 719 | } |
| 720 | |
| 721 | static void imp_ldif_dialog_create() { |
| 722 | GtkWidget *window; |
| 723 | GtkWidget *vbox; |
| 724 | GtkWidget *vnbox; |
| 725 | GtkWidget *notebook; |
| 726 | GtkWidget *hbbox; |
| 727 | GtkWidget *btnPrev; |
| 728 | GtkWidget *btnNext; |
| 729 | GtkWidget *btnCancel; |
| 730 | GtkWidget *hsbox; |
| 731 | GtkWidget *statusbar; |
| 732 | |
| 733 | window = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
| 734 | gtk_widget_set_size_request(window, IMPORTLDIF_WIDTH, IMPORTLDIF_HEIGHT ); |
| 735 | gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
|
| 736 | gtk_window_set_title( GTK_WINDOW(window), _("Import LDIF file into Address Book") );
|
| 737 | gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER); |
| 738 | gtk_window_set_modal(GTK_WINDOW(window), TRUE); |
| 739 | g_signal_connect(G_OBJECT(window), "delete_event",
|
| 740 | G_CALLBACK(imp_ldif_delete_event), NULL);
|
| 741 | g_signal_connect(G_OBJECT(window), "key_press_event",
|
| 742 | G_CALLBACK(imp_ldif_key_pressed), NULL);
|
| 743 | |
| 744 | vbox = gtk_vbox_new(FALSE, 4);
|
| 745 | gtk_widget_show(vbox); |
| 746 | gtk_container_add(GTK_CONTAINER(window), vbox); |
| 747 | |
| 748 | vnbox = gtk_vbox_new(FALSE, 4);
|
| 749 | gtk_container_set_border_width(GTK_CONTAINER(vnbox), 4);
|
| 750 | gtk_widget_show(vnbox); |
| 751 | gtk_box_pack_start(GTK_BOX(vbox), vnbox, TRUE, TRUE, 0);
|
| 752 | |
| 753 | /* Notebook */
|
| 754 | notebook = gtk_notebook_new(); |
| 755 | gtk_notebook_set_show_tabs( GTK_NOTEBOOK(notebook), FALSE ); |
| 756 | gtk_widget_show(notebook); |
| 757 | gtk_box_pack_start(GTK_BOX(vnbox), notebook, TRUE, TRUE, 0);
|
| 758 | gtk_container_set_border_width(GTK_CONTAINER(notebook), 6);
|
| 759 | |
| 760 | /* Status line */
|
| 761 | hsbox = gtk_hbox_new(FALSE, 0);
|
| 762 | gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, 0);
|
| 763 | statusbar = gtk_statusbar_new(); |
| 764 | gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, 0);
|
| 765 | |
| 766 | /* Button panel */
|
| 767 | gtkut_stock_button_set_create(&hbbox, &btnPrev, _("Prev"),
|
| 768 | &btnNext, _("Next"),
|
| 769 | &btnCancel, GTK_STOCK_CANCEL); |
| 770 | gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
|
| 771 | gtk_container_set_border_width(GTK_CONTAINER(hbbox), 2);
|
| 772 | gtk_widget_grab_default(btnNext); |
| 773 | |
| 774 | /* Button handlers */
|
| 775 | g_signal_connect(G_OBJECT(btnPrev), "clicked",
|
| 776 | G_CALLBACK(imp_ldif_prev), NULL);
|
| 777 | g_signal_connect(G_OBJECT(btnNext), "clicked",
|
| 778 | G_CALLBACK(imp_ldif_next), NULL);
|
| 779 | g_signal_connect(G_OBJECT(btnCancel), "clicked",
|
| 780 | G_CALLBACK(imp_ldif_cancel), NULL);
|
| 781 | |
| 782 | gtk_widget_show_all(vbox); |
| 783 | |
| 784 | impldif_dlg.window = window; |
| 785 | impldif_dlg.notebook = notebook; |
| 786 | impldif_dlg.btnPrev = btnPrev; |
| 787 | impldif_dlg.btnNext = btnNext; |
| 788 | impldif_dlg.btnCancel = btnCancel; |
| 789 | impldif_dlg.statusbar = statusbar; |
| 790 | impldif_dlg.status_cid = gtk_statusbar_get_context_id( |
| 791 | GTK_STATUSBAR(statusbar), "Import LDIF Dialog" );
|
| 792 | |
| 793 | } |
| 794 | |
| 795 | static void imp_ldif_create() { |
| 796 | imp_ldif_dialog_create(); |
| 797 | imp_ldif_page_file( PAGE_FILE_INFO, _( "File Info" ) );
|
| 798 | imp_ldif_page_fields( PAGE_ATTRIBUTES, _( "Attributes" ) );
|
| 799 | imp_ldif_page_finish( PAGE_FINISH, _( "Finish" ) );
|
| 800 | gtk_widget_show_all( impldif_dlg.window ); |
| 801 | } |
| 802 | |
| 803 | AddressBookFile *addressbook_imp_ldif( AddressIndex *addrIndex ) {
|
| 804 | _importedBook_ = NULL;
|
| 805 | _imp_addressIndex_ = addrIndex; |
| 806 | |
| 807 | if( ! impldif_dlg.window )
|
| 808 | imp_ldif_create(); |
| 809 | impldif_dlg.cancelled = FALSE; |
| 810 | gtk_widget_show(impldif_dlg.window); |
| 811 | manage_window_set_transient(GTK_WINDOW(impldif_dlg.window)); |
| 812 | gtk_widget_grab_default(impldif_dlg.btnNext); |
| 813 | |
| 814 | gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_entry), IMPORTLDIF_GUESS_NAME ); |
| 815 | gtk_entry_set_text( GTK_ENTRY(impldif_dlg.file_entry), "" );
|
| 816 | gtk_label_set_text( GTK_LABEL(impldif_dlg.name_ldif), "" );
|
| 817 | gtk_entry_set_text( GTK_ENTRY(impldif_dlg.name_attrib), "" );
|
| 818 | gtk_clist_clear( GTK_CLIST(impldif_dlg.clist_field) ); |
| 819 | gtk_notebook_set_current_page( GTK_NOTEBOOK(impldif_dlg.notebook), PAGE_FILE_INFO ); |
| 820 | gtk_widget_set_sensitive( impldif_dlg.btnPrev, FALSE ); |
| 821 | gtk_widget_set_sensitive( impldif_dlg.btnNext, TRUE ); |
| 822 | stock_pixmap_gdk( impldif_dlg.window, STOCK_PIXMAP_MARK, |
| 823 | &markxpm, &markxpmmask ); |
| 824 | imp_ldif_message(); |
| 825 | gtk_widget_grab_focus(impldif_dlg.file_entry); |
| 826 | |
| 827 | impldif_dlg.rowIndSelect = -1;
|
| 828 | impldif_dlg.rowCount = 0;
|
| 829 | g_free( impldif_dlg.nameBook ); |
| 830 | g_free( impldif_dlg.fileName ); |
| 831 | impldif_dlg.nameBook = NULL;
|
| 832 | impldif_dlg.fileName = NULL;
|
| 833 | |
| 834 | _ldifFile_ = ldif_create(); |
| 835 | gtk_main(); |
| 836 | gtk_widget_hide(impldif_dlg.window); |
| 837 | ldif_free( _ldifFile_ ); |
| 838 | _ldifFile_ = NULL;
|
| 839 | _imp_addressIndex_ = NULL;
|
| 840 | |
| 841 | g_free( impldif_dlg.nameBook ); |
| 842 | g_free( impldif_dlg.fileName ); |
| 843 | impldif_dlg.nameBook = NULL;
|
| 844 | impldif_dlg.fileName = NULL;
|
| 845 | |
| 846 | if( impldif_dlg.cancelled == TRUE ) return NULL; |
| 847 | return _importedBook_;
|
| 848 | } |
| 849 | |
| 850 | /*
|
| 851 | * End of Source. |
| 852 | */ |
| 853 |