Statistics
| Revision:

root / src / editjpilot.c @ 405

History | View | Annotate | Download (14.1 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 JPilot address book data.
22
 */
23
24
#ifdef HAVE_CONFIG_H
25
#  include "config.h"
26
#endif
27
28
#ifdef USE_JPILOT
29
30
#include "defs.h"
31
32
#include <glib.h>
33
#include <glib/gi18n.h>
34
#include <gdk/gdkkeysyms.h>
35
#include <gtk/gtkwindow.h>
36
#include <gtk/gtksignal.h>
37
#include <gtk/gtkhbox.h>
38
#include <gtk/gtklabel.h>
39
#include <gtk/gtkentry.h>
40
#include <gtk/gtkhbbox.h>
41
#include <gtk/gtkvbox.h>
42
#include <gtk/gtktable.h>
43
#include <gtk/gtkframe.h>
44
#include <gtk/gtkhseparator.h>
45
#include <gtk/gtkbutton.h>
46
#include <gtk/gtkstatusbar.h>
47
#include <gtk/gtkcheckbutton.h>
48
#include <gtk/gtkfilesel.h>
49
50
#include "addressbook.h"
51
#include "prefs_common.h"
52
#include "addressitem.h"
53
#include "jpilot.h"
54
#include "mgutils.h"
55
#include "gtkutils.h"
56
#include "manage_window.h"
57
58
#define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
59
#define JPILOT_NUM_CUSTOM_LABEL        4
60
61
static struct _JPilotEdit {
62
        GtkWidget *window;
63
        GtkWidget *name_entry;
64
        GtkWidget *file_entry;
65
        GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
66
        GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
67
        GtkWidget *hbbox;
68
        GtkWidget *ok_btn;
69
        GtkWidget *cancel_btn;
70
        GtkWidget *statusbar;
71
        gint status_cid;
72
} jpilotedit;
73
74
static struct _AddressFileSelection jpilot_file_selector;
75
76
/*
77
* Edit functions.
78
*/
79
void edit_jpilot_status_show( gchar *msg ) {
80
        if( jpilotedit.statusbar != NULL ) {
81
                gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
82
                if( msg ) {
83
                        gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
84
                }
85
        }
86
}
87
88
static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
89
        *cancelled = TRUE;
90
        gtk_main_quit();
91
        return TRUE;
92
}
93
94
static gboolean edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
95
        if (event && event->keyval == GDK_Escape) {
96
                *cancelled = TRUE;
97
                gtk_main_quit();
98
        }
99
        return FALSE;
100
}
101
102
static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
103
        *cancelled = FALSE;
104
        gtk_main_quit();
105
}
106
107
static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
108
        *cancelled = TRUE;
109
        gtk_main_quit();
110
}
111
112
static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
113
        gint i;
114
        GList *node, *customLbl = NULL;
115
        gchar *labelName;
116
        gboolean done, checked;
117
        for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
118
                gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
119
                gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
120
        }
121
122
        done = FALSE;
123
        i = 0;
124
        customLbl = jpilot_load_custom_label( jpf, customLbl );
125
        node = customLbl;
126
        while( ! done ) {
127
                if( node ) {
128
                        labelName = node->data;
129
                        gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), labelName );
130
                        checked = jpilot_test_custom_label( jpf, labelName );
131
                        gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), checked );
132
                        i++;
133
                        if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
134
                        node = g_list_next( node );
135
                }
136
                else {
137
                        done = TRUE;
138
                }
139
        }
140
        mgu_free_dlist( customLbl );
141
        customLbl = NULL;
142
}
143
144
static void edit_jpilot_fill_check_box_new() {
145
        gint i;
146
        for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
147
                gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
148
                gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
149
        }
150
}
151
152
static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
153
        gint i;
154
        gchar *labelName;
155
        jpilot_clear_custom_labels( pilotFile );
156
        for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
157
                if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(jpilotedit.custom_check[i]) ) ) {
158
                        labelName = GTK_LABEL(jpilotedit.custom_label[i])->label;
159
                        jpilot_add_custom_label( pilotFile, labelName );
160
                }
161
        }
162
}
163
164
static void edit_jpilot_file_check( void ) {
165
        gint t = -1;
166
        gchar *sFile;
167
        gchar *sMsg;
168
        gboolean flg;
169
170
        flg = FALSE;
171
        sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
172
        if( sFile ) {
173
                g_strchomp( sFile ); g_strchug( sFile );
174
                if( *sFile != '\0' ) {
175
                        /* Attempt to read file */
176
                        JPilotFile *jpf = jpilot_create_path( sFile );
177
                        t = jpilot_read_data( jpf );
178
                        if( t == MGU_SUCCESS ) {
179
                                /* Set check boxes */
180
                                edit_jpilot_fill_check_box( jpf );
181
                                flg = TRUE;
182
                        }
183
                        jpilot_free( jpf );
184
                        jpf = NULL;
185
                }
186
        }
187
        if( ! flg ) {
188
                /* Clear all check boxes */
189
                edit_jpilot_fill_check_box_new();
190
        }
191
        g_free( sFile );
192
193
        /* Display appropriate message */
194
        if( t == MGU_SUCCESS ) {
195
                sMsg = "";
196
        }
197
        else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
198
                sMsg = _("File does not appear to be JPilot format.");
199
        }
200
        else {
201
                sMsg = _("Could not read file.");
202
        }
203
        edit_jpilot_status_show( sMsg );
204
}
205
206
static void edit_jpilot_file_ok( GtkWidget *widget, gpointer data ) {
207
        const gchar *sFile;
208
        AddressFileSelection *afs;
209
        GtkWidget *fileSel;
210
211
        afs = ( AddressFileSelection * ) data;
212
        fileSel = afs->fileSelector;
213
        sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
214
215
        afs->cancelled = FALSE;
216
        gtk_entry_set_text( GTK_ENTRY(jpilotedit.file_entry), sFile );
217
        gtk_widget_hide( afs->fileSelector );
218
        gtk_grab_remove( afs->fileSelector );
219
        edit_jpilot_file_check();
220
        gtk_widget_grab_focus( jpilotedit.file_entry );
221
}
222
223
static void edit_jpilot_file_cancel( GtkWidget *widget, gpointer data ) {
224
        AddressFileSelection *afs = ( AddressFileSelection * ) data;
225
        afs->cancelled = TRUE;
226
        gtk_widget_hide( afs->fileSelector );
227
        gtk_grab_remove( afs->fileSelector );
228
        gtk_widget_grab_focus( jpilotedit.file_entry );
229
}
230
231
static void edit_jpilot_file_select_create( AddressFileSelection *afs ) {
232
        GtkWidget *fileSelector;
233
234
        fileSelector = gtk_file_selection_new( _("Select JPilot File") );
235
        gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
236
        g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
237
                          "clicked", G_CALLBACK (edit_jpilot_file_ok), ( gpointer ) afs );
238
        g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
239
                          "clicked", G_CALLBACK (edit_jpilot_file_cancel), ( gpointer ) afs );
240
        afs->fileSelector = fileSelector;
241
        afs->cancelled = TRUE;
242
}
243
244
static void edit_jpilot_file_select( void ) {
245
        gchar *sFile;
246
247
        if (! jpilot_file_selector.fileSelector )
248
                edit_jpilot_file_select_create( & jpilot_file_selector );
249
250
        sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
251
        gtk_file_selection_set_filename( GTK_FILE_SELECTION( jpilot_file_selector.fileSelector ), sFile );
252
        g_free( sFile );
253
        gtk_widget_show( jpilot_file_selector.fileSelector );
254
        gtk_grab_add( jpilot_file_selector.fileSelector );
255
}
256
257
static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
258
        GtkWidget *window;
259
        GtkWidget *vbox;
260
        GtkWidget *table;
261
        GtkWidget *label;
262
        GtkWidget *name_entry;
263
        GtkWidget *file_entry;
264
        GtkWidget *vbox_custom;
265
        GtkWidget *frame_custom;
266
        GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
267
        GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
268
        GtkWidget *hlbox;
269
        GtkWidget *hbbox;
270
        GtkWidget *hsep;
271
        GtkWidget *ok_btn;
272
        GtkWidget *cancel_btn;
273
        GtkWidget *check_btn;
274
        GtkWidget *file_btn;
275
        GtkWidget *hsbox;
276
        GtkWidget *statusbar;
277
        gint top, i;
278
279
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
280
        gtk_widget_set_size_request(window, 450, -1);
281
        gtk_container_set_border_width(GTK_CONTAINER(window), 0);
282
        gtk_window_set_title(GTK_WINDOW(window), _("Edit JPilot Entry"));
283
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
284
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);        
285
        g_signal_connect(G_OBJECT(window), "delete_event",
286
                         G_CALLBACK(edit_jpilot_delete_event),
287
                         cancelled);
288
        g_signal_connect(G_OBJECT(window), "key_press_event",
289
                         G_CALLBACK(edit_jpilot_key_pressed),
290
                         cancelled);
291
292
        vbox = gtk_vbox_new(FALSE, 8);
293
        gtk_container_add(GTK_CONTAINER(window), vbox);
294
        gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
295
296
        table = gtk_table_new(2 + JPILOT_NUM_CUSTOM_LABEL, 3, FALSE);
297
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
298
        gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
299
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
300
        gtk_table_set_col_spacings(GTK_TABLE(table), 8);
301
302
        /* First row */
303
        top = 0;
304
        label = gtk_label_new(_("Name"));
305
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
306
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
307
308
        name_entry = gtk_entry_new();
309
        gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
310
311
        check_btn = gtk_button_new_with_label( _(" Check File "));
312
        gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
313
314
        /* Second row */
315
        top = 1;
316
        label = gtk_label_new(_("File"));
317
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
318
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
319
320
        file_entry = gtk_entry_new();
321
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
322
323
        file_btn = gtk_button_new_with_label( _(" ... "));
324
        gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
325
326
        /* Third row */
327
        top = 2;
328
        frame_custom = gtk_frame_new(_("Additional e-Mail address item(s)"));
329
        gtk_table_attach(GTK_TABLE(table), frame_custom, 1, 2, top, (top + JPILOT_NUM_CUSTOM_LABEL), GTK_FILL, 0, 0, 0);
330
331
        /* Now do custom labels. */
332
        vbox_custom = gtk_vbox_new (FALSE, 8);
333
        for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
334
                hlbox = gtk_hbox_new( FALSE, 0 );
335
                custom_check[i] = gtk_check_button_new();
336
                custom_label[i] = gtk_label_new( "" );
337
                gtk_box_pack_start( GTK_BOX(hlbox), custom_check[i], FALSE, FALSE, 0 );
338
                gtk_box_pack_start( GTK_BOX(hlbox), custom_label[i], TRUE, TRUE, 0 );
339
                gtk_box_pack_start( GTK_BOX(vbox_custom), hlbox, TRUE, TRUE, 0 );
340
                gtk_misc_set_alignment(GTK_MISC(custom_label[i]), 0, 0.5);
341
                top++;
342
        }
343
        gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
344
        gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
345
346
        /* Status line */
347
        hsbox = gtk_hbox_new(FALSE, 0);
348
        gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
349
        statusbar = gtk_statusbar_new();
350
        gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
351
352
        /* Button panel */
353
        gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
354
                                      &cancel_btn, GTK_STOCK_CANCEL,
355
                                      NULL, NULL);
356
        gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
357
        gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
358
        gtk_widget_grab_default(ok_btn);
359
360
        hsep = gtk_hseparator_new();
361
        gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
362
363
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
364
                         G_CALLBACK(edit_jpilot_ok), cancelled);
365
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
366
                         G_CALLBACK(edit_jpilot_cancel), cancelled);
367
        g_signal_connect(G_OBJECT(file_btn), "clicked",
368
                         G_CALLBACK(edit_jpilot_file_select), NULL);
369
        g_signal_connect(G_OBJECT(check_btn), "clicked",
370
                         G_CALLBACK(edit_jpilot_file_check), NULL);
371
372
        gtk_widget_show_all(vbox);
373
374
        jpilotedit.window     = window;
375
        jpilotedit.name_entry = name_entry;
376
        jpilotedit.file_entry = file_entry;
377
        jpilotedit.hbbox      = hbbox;
378
        jpilotedit.ok_btn     = ok_btn;
379
        jpilotedit.cancel_btn = cancel_btn;
380
        jpilotedit.statusbar  = statusbar;
381
        jpilotedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit JPilot Dialog" );
382
        for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
383
                jpilotedit.custom_check[i] = custom_check[i];
384
                jpilotedit.custom_label[i] = custom_label[i];
385
        }
386
}
387
388
AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
389
        static gboolean cancelled;
390
        gchar *sName;
391
        gchar *sFile;
392
        AddressDataSource *ds = NULL;
393
        JPilotFile *jpf = NULL;
394
        gboolean fin;
395
396
        if( ! jpilotedit.window )
397
                addressbook_edit_jpilot_create(&cancelled);
398
        gtkut_box_set_reverse_order(GTK_BOX(jpilotedit.hbbox),
399
                                    !prefs_common.comply_gnome_hig);
400
        gtk_widget_grab_focus(jpilotedit.ok_btn);
401
        gtk_widget_grab_focus(jpilotedit.name_entry);
402
        gtk_widget_show(jpilotedit.window);
403
        manage_window_set_transient(GTK_WINDOW(jpilotedit.window));
404
405
        edit_jpilot_status_show( "" );
406
        if( ads ) {
407
                ds = ads->dataSource;
408
                jpf = ds->rawDataSource;
409
                if (jpf->name)
410
                        gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), jpf->name);
411
                if (jpf->path)
412
                        gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), jpf->path);
413
                gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Edit JPilot Entry"));
414
                edit_jpilot_fill_check_box( jpf );
415
        }
416
        else {
417
                gchar *guessFile = jpilot_find_pilotdb();
418
                gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), ADDRESSBOOK_GUESS_JPILOT );
419
                gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), guessFile );
420
                gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Add New JPilot Entry"));
421
                edit_jpilot_fill_check_box_new();
422
                if( *guessFile != '\0' ) {
423
                        edit_jpilot_file_check();
424
                }
425
        }
426
427
        gtk_main();
428
        gtk_widget_hide(jpilotedit.window);
429
        if (cancelled == TRUE) return NULL;
430
431
        fin = FALSE;
432
        sName = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.name_entry), 0, -1 );
433
        sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
434
        if( *sName == '\0' ) fin = TRUE;
435
        if( *sFile == '\0' ) fin = TRUE;
436
437
        if( ! fin ) {
438
                if( ! ads ) {
439
                        jpf = jpilot_create();
440
                        ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_JPILOT, jpf );
441
                        ads = addressbook_create_ds_adapter( ds, ADDR_JPILOT, NULL );
442
                }
443
                addressbook_ads_set_name( ads, sName );
444
                jpilot_set_name( jpf, sName );
445
                jpilot_set_file( jpf, sFile );
446
                edit_jpilot_read_check_box( jpf );
447
        }
448
        g_free( sName );
449
        g_free( sFile );
450
451
        return ads;
452
}
453
454
#endif /* USE_JPILOT */
455
456
/*
457
* End of Source.
458
*/
459