Statistics
| Revision:

root / src / editvcard.c @ 1

History | View | Annotate | Download (9.98 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 "addressbook.h"
41
#include "prefs_common.h"
42
#include "addressitem.h"
43
#include "vcard.h"
44
#include "mgutils.h"
45
#include "gtkutils.h"
46
#include "manage_window.h"
47

    
48
#define ADDRESSBOOK_GUESS_VCARD  "MyGnomeCard"
49

    
50
static struct _VCardEdit {
51
        GtkWidget *window;
52
        GtkWidget *name_entry;
53
        GtkWidget *file_entry;
54
        GtkWidget *ok_btn;
55
        GtkWidget *cancel_btn;
56
        GtkWidget *statusbar;
57
        gint status_cid;
58
} vcardedit;
59

    
60
static struct _AddressFileSelection vcard_file_selector;
61

    
62
/*
63
* Edit functions.
64
*/
65
void edit_vcard_status_show( gchar *msg ) {
66
        if( vcardedit.statusbar != NULL ) {
67
                gtk_statusbar_pop( GTK_STATUSBAR(vcardedit.statusbar), vcardedit.status_cid );
68
                if( msg ) {
69
                        gtk_statusbar_push( GTK_STATUSBAR(vcardedit.statusbar), vcardedit.status_cid, msg );
70
                }
71
        }
72
}
73

    
74
static void edit_vcard_ok( GtkWidget *widget, gboolean *cancelled ) {
75
        *cancelled = FALSE;
76
        gtk_main_quit();
77
}
78

    
79
static void edit_vcard_cancel( GtkWidget *widget, gboolean *cancelled ) {
80
        *cancelled = TRUE;
81
        gtk_main_quit();
82
}
83

    
84
static void edit_vcard_file_check( void ) {
85
        gint t;
86
        gchar *sFile;
87
        gchar *sMsg;
88

    
89
        sFile = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.file_entry), 0, -1 );
90
        t = vcard_test_read_file( sFile );
91
        g_free( sFile );
92
        if( t == MGU_SUCCESS ) {
93
                sMsg = "";
94
        }
95
        else if( t == MGU_BAD_FORMAT ) {
96
                sMsg = _("File does not appear to be vCard format.");
97
        }
98
        else {
99
                sMsg = _("Could not read file.");
100
        }
101
        edit_vcard_status_show( sMsg );
102
}
103

    
104
static void edit_vcard_file_ok( GtkWidget *widget, gpointer data ) {
105
        const gchar *sFile;
106
        AddressFileSelection *afs;
107
        GtkWidget *fileSel;
108

    
109
        afs = ( AddressFileSelection * ) data;
110
        fileSel = afs->fileSelector;
111
        sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
112

    
113
        afs->cancelled = FALSE;
114
        gtk_entry_set_text( GTK_ENTRY(vcardedit.file_entry), sFile );
115
        gtk_widget_hide( afs->fileSelector );
116
        gtk_grab_remove( afs->fileSelector );
117
        edit_vcard_file_check();
118
        gtk_widget_grab_focus( vcardedit.file_entry );
119
}
120

    
121
static void edit_vcard_file_cancel( GtkWidget *widget, gpointer data ) {
122
        AddressFileSelection *afs = ( AddressFileSelection * ) data;
123
        afs->cancelled = TRUE;
124
        gtk_widget_hide( afs->fileSelector );
125
        gtk_grab_remove( afs->fileSelector );
126
        gtk_widget_grab_focus( vcardedit.file_entry );
127
}
128

    
129
static void edit_vcard_file_select_create( AddressFileSelection *afs ) {
130
        GtkWidget *fileSelector;
131

    
132
        fileSelector = gtk_file_selection_new( _("Select vCard File") );
133
        gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
134
        g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
135
                          "clicked", G_CALLBACK (edit_vcard_file_ok), ( gpointer ) afs );
136
        g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
137
                          "clicked", G_CALLBACK (edit_vcard_file_cancel), ( gpointer ) afs );
138
        afs->fileSelector = fileSelector;
139
        afs->cancelled = TRUE;
140
}
141

    
142
static void edit_vcard_file_select( void ) {
143
        gchar *sFile;
144

    
145
        if (! vcard_file_selector.fileSelector )
146
                edit_vcard_file_select_create( & vcard_file_selector );
147

    
148
        sFile = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.file_entry), 0, -1 );
149
        gtk_file_selection_set_filename( GTK_FILE_SELECTION( vcard_file_selector.fileSelector ), sFile );
150
        g_free( sFile );
151
        gtk_widget_show( vcard_file_selector.fileSelector );
152
        gtk_grab_add( vcard_file_selector.fileSelector );
153
}
154

    
155
static gint edit_vcard_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
156
        *cancelled = TRUE;
157
        gtk_main_quit();
158
        return TRUE;
159
}
160

    
161
static gboolean edit_vcard_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
162
        if (event && event->keyval == GDK_Escape) {
163
                *cancelled = TRUE;
164
                gtk_main_quit();
165
        }
166
        return FALSE;
167
}
168

    
169
static void addressbook_edit_vcard_create( gboolean *cancelled ) {
170
        GtkWidget *window;
171
        GtkWidget *vbox;
172
        GtkWidget *table;
173
        GtkWidget *label;
174
        GtkWidget *name_entry;
175
        GtkWidget *file_entry;
176
        GtkWidget *hbbox;
177
        GtkWidget *hsep;
178
        GtkWidget *ok_btn;
179
        GtkWidget *cancel_btn;
180
        GtkWidget *check_btn;
181
        GtkWidget *file_btn;
182
        GtkWidget *statusbar;
183
        GtkWidget *hsbox;
184
        gint top;
185

    
186
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
187
        gtk_widget_set_size_request(window, 450, -1);
188
        gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
189
        gtk_window_set_title(GTK_WINDOW(window), _("Edit vCard Entry"));
190
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
191
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);        
192
        g_signal_connect(G_OBJECT(window), "delete_event",
193
                         G_CALLBACK(edit_vcard_delete_event),
194
                         cancelled);
195
        g_signal_connect(G_OBJECT(window), "key_press_event",
196
                         G_CALLBACK(edit_vcard_key_pressed),
197
                         cancelled);
198

    
199
        vbox = gtk_vbox_new(FALSE, 8);
200
        gtk_container_add(GTK_CONTAINER(window), vbox);
201
        gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
202

    
203
        table = gtk_table_new(2, 3, FALSE);
204
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
205
        gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
206
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
207
        gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
208

    
209
        /* First row */
210
        top = 0;
211
        label = gtk_label_new(_("Name"));
212
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
213
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
214

    
215
        name_entry = gtk_entry_new();
216
        gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
217

    
218
        check_btn = gtk_button_new_with_label( _(" Check File "));
219
        gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
220

    
221
        /* Second row */
222
        top = 1;
223
        label = gtk_label_new(_("File"));
224
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
225
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
226

    
227
        file_entry = gtk_entry_new();
228
        gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
229

    
230
        file_btn = gtk_button_new_with_label( _(" ... "));
231
        gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
232

    
233
        /* Status line */
234
        hsbox = gtk_hbox_new(FALSE, 0);
235
        gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
236
        statusbar = gtk_statusbar_new();
237
        gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
238

    
239
        /* Button panel */
240
        gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
241
                                &cancel_btn, _("Cancel"), NULL, NULL);
242
        gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
243
        gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
244
        gtk_widget_grab_default(ok_btn);
245

    
246
        hsep = gtk_hseparator_new();
247
        gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
248

    
249
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
250
                         G_CALLBACK(edit_vcard_ok), cancelled);
251
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
252
                         G_CALLBACK(edit_vcard_cancel), cancelled);
253
        g_signal_connect(G_OBJECT(file_btn), "clicked",
254
                         G_CALLBACK(edit_vcard_file_select), NULL);
255
        g_signal_connect(G_OBJECT(check_btn), "clicked",
256
                         G_CALLBACK(edit_vcard_file_check), NULL);
257

    
258
        gtk_widget_show_all(vbox);
259

    
260
        vcardedit.window     = window;
261
        vcardedit.name_entry = name_entry;
262
        vcardedit.file_entry = file_entry;
263
        vcardedit.ok_btn     = ok_btn;
264
        vcardedit.cancel_btn = cancel_btn;
265
        vcardedit.statusbar  = statusbar;
266
        vcardedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit vCard Dialog" );
267
}
268

    
269
AdapterDSource *addressbook_edit_vcard( AddressIndex *addrIndex, AdapterDSource *ads ) {
270
        static gboolean cancelled;
271
        gchar *sName;
272
        gchar *sFile;
273
        AddressDataSource *ds = NULL;
274
        VCardFile *vcf = NULL;
275
        gboolean fin;
276

    
277
        if( ! vcardedit.window )
278
                addressbook_edit_vcard_create(&cancelled);
279
        gtk_widget_grab_focus(vcardedit.ok_btn);
280
        gtk_widget_grab_focus(vcardedit.name_entry);
281
        gtk_widget_show(vcardedit.window);
282
        manage_window_set_transient(GTK_WINDOW(vcardedit.window));
283

    
284
        edit_vcard_status_show( "" );
285
        if( ads ) {
286
                ds = ads->dataSource;
287
                vcf = ds->rawDataSource;
288
                if (vcf->name)
289
                        gtk_entry_set_text(GTK_ENTRY(vcardedit.name_entry), vcf->name);
290
                if (vcf->path)
291
                        gtk_entry_set_text(GTK_ENTRY(vcardedit.file_entry), vcf->path);
292
                gtk_window_set_title( GTK_WINDOW(vcardedit.window), _("Edit vCard Entry"));
293
        }
294
        else {
295
                gtk_entry_set_text(GTK_ENTRY(vcardedit.name_entry), ADDRESSBOOK_GUESS_VCARD );
296
                gtk_entry_set_text(GTK_ENTRY(vcardedit.file_entry), vcard_find_gnomecard() );
297
                gtk_window_set_title( GTK_WINDOW(vcardedit.window), _("Add New vCard Entry"));
298
        }
299

    
300
        gtk_main();
301
        gtk_widget_hide(vcardedit.window);
302
        if (cancelled == TRUE) return NULL;
303

    
304
        fin = FALSE;
305
        sName = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.name_entry), 0, -1 );
306
        sFile = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.file_entry), 0, -1 );
307
        if( *sName == '\0' ) fin = TRUE;
308
        if( *sFile == '\0' ) fin = TRUE;
309

    
310
        if( ! fin ) {
311
                if( ! ads ) {
312
                        vcf = vcard_create();
313
                        ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_VCARD, vcf );
314
                        ads = addressbook_create_ds_adapter( ds, ADDR_VCARD, NULL );
315
                }
316
                addressbook_ads_set_name( ads, sName );
317
                vcard_set_name( vcf, sName );
318
                vcard_set_file( vcf, sFile );
319
        }
320
        g_free( sName );
321
        g_free( sFile );
322

    
323
        return ads;
324
}
325

    
326
/*
327
* End of Source.
328
*/
329