2005-07-11 [paul] 1.9.12cvs34
[claws.git] / src / editvcard.c
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/gtkstatusbar.h>
41 #include <gtk/gtkfilesel.h>
42 #include <gtk/gtkhseparator.h>
43 #include <gtk/gtkstock.h>
44
45 #include "addressbook.h"
46 #include "prefs_common.h"
47 #include "addressitem.h"
48 #include "vcard.h"
49 #include "mgutils.h"
50 #include "filesel.h"
51 #include "gtkutils.h"
52 #include "codeconv.h"
53 #include "manage_window.h"
54
55 #define ADDRESSBOOK_GUESS_VCARD  "MyGnomeCard"
56
57 static struct _VCardEdit {
58         GtkWidget *window;
59         GtkWidget *name_entry;
60         GtkWidget *file_entry;
61         GtkWidget *ok_btn;
62         GtkWidget *cancel_btn;
63         GtkWidget *statusbar;
64         gint status_cid;
65 } vcardedit;
66
67 /*
68 * Edit functions.
69 */
70 void edit_vcard_status_show( gchar *msg ) {
71         if( vcardedit.statusbar != NULL ) {
72                 gtk_statusbar_pop( GTK_STATUSBAR(vcardedit.statusbar), vcardedit.status_cid );
73                 if( msg ) {
74                         gtk_statusbar_push( GTK_STATUSBAR(vcardedit.statusbar), vcardedit.status_cid, msg );
75                 }
76         }
77 }
78
79 static void edit_vcard_ok( GtkWidget *widget, gboolean *cancelled ) {
80         *cancelled = FALSE;
81         gtk_main_quit();
82 }
83
84 static void edit_vcard_cancel( GtkWidget *widget, gboolean *cancelled ) {
85         *cancelled = TRUE;
86         gtk_main_quit();
87 }
88
89 static void edit_vcard_file_check( void ) {
90         gint t;
91         gchar *sFile;
92         gchar *sFSFile;
93         gchar *sMsg;
94
95         sFile = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.file_entry), 0, -1 );
96         sFSFile = conv_filename_from_utf8( sFile );
97         t = vcard_test_read_file( sFSFile );
98         g_free( sFSFile );
99         g_free( sFile );
100         if( t == MGU_SUCCESS ) {
101                 sMsg = "";
102         }
103         else if( t == MGU_BAD_FORMAT ) {
104                 sMsg = _("File does not appear to be vCard format.");
105         }
106         else {
107                 sMsg = _("Could not read file.");
108         }
109         edit_vcard_status_show( sMsg );
110 }
111
112 static void edit_vcard_file_select( void ) {
113         gchar *sFile;
114         gchar *sUTF8File;
115
116         sFile = filesel_select_file_open( _("Select vCard File"), NULL);
117
118         if( sFile ) {
119                 sUTF8File = conv_filename_to_utf8( sFile );
120                 gtk_entry_set_text( GTK_ENTRY(vcardedit.file_entry), sUTF8File );
121                 g_free( sUTF8File );
122                 g_free( sFile );
123                 edit_vcard_file_check();
124         }
125 }
126
127 static gint edit_vcard_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
128         *cancelled = TRUE;
129         gtk_main_quit();
130         return TRUE;
131 }
132
133 static gboolean edit_vcard_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
134         if (event && event->keyval == GDK_Escape) {
135                 *cancelled = TRUE;
136                 gtk_main_quit();
137         }
138         return FALSE;
139 }
140
141 static void addressbook_edit_vcard_create( gboolean *cancelled ) {
142         GtkWidget *window;
143         GtkWidget *vbox;
144         GtkWidget *table;
145         GtkWidget *label;
146         GtkWidget *name_entry;
147         GtkWidget *file_entry;
148         GtkWidget *hbbox;
149         GtkWidget *hsep;
150         GtkWidget *ok_btn;
151         GtkWidget *cancel_btn;
152         GtkWidget *check_btn;
153         GtkWidget *file_btn;
154         GtkWidget *statusbar;
155         GtkWidget *hsbox;
156         gint top;
157
158         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
159         gtk_widget_set_size_request(window, 450, -1);
160         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
161         gtk_window_set_title(GTK_WINDOW(window), _("Edit vCard Entry"));
162         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
163         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
164         g_signal_connect(G_OBJECT(window), "delete_event",
165                          G_CALLBACK(edit_vcard_delete_event),
166                          cancelled);
167         g_signal_connect(G_OBJECT(window), "key_press_event",
168                          G_CALLBACK(edit_vcard_key_pressed),
169                          cancelled);
170
171         vbox = gtk_vbox_new(FALSE, 8);
172         gtk_container_add(GTK_CONTAINER(window), vbox);
173         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
174
175         table = gtk_table_new(2, 3, FALSE);
176         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
177         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
178         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
179         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
180
181         /* First row */
182         top = 0;
183         label = gtk_label_new(_("Name"));
184         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
185         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
186
187         name_entry = gtk_entry_new();
188         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
189
190         check_btn = gtk_button_new_with_label( _(" Check File "));
191         gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
192
193         /* Second row */
194         top = 1;
195         label = gtk_label_new(_("File"));
196         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
197         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
198
199         file_entry = gtk_entry_new();
200         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
201
202         file_btn = gtk_button_new_with_label( _(" ... "));
203         gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
204
205         /* Status line */
206         hsbox = gtk_hbox_new(FALSE, 0);
207         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
208         statusbar = gtk_statusbar_new();
209         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
210
211         /* Button panel */
212         gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
213                                       &cancel_btn, GTK_STOCK_CANCEL,
214                                       NULL, NULL);
215         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
216         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
217         gtk_widget_grab_default(ok_btn);
218
219         hsep = gtk_hseparator_new();
220         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
221
222         g_signal_connect(G_OBJECT(ok_btn), "clicked",
223                          G_CALLBACK(edit_vcard_ok), cancelled);
224         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
225                          G_CALLBACK(edit_vcard_cancel), cancelled);
226         g_signal_connect(G_OBJECT(file_btn), "clicked",
227                          G_CALLBACK(edit_vcard_file_select), NULL);
228         g_signal_connect(G_OBJECT(check_btn), "clicked",
229                          G_CALLBACK(edit_vcard_file_check), NULL);
230
231         gtk_widget_show_all(vbox);
232
233         vcardedit.window     = window;
234         vcardedit.name_entry = name_entry;
235         vcardedit.file_entry = file_entry;
236         vcardedit.ok_btn     = ok_btn;
237         vcardedit.cancel_btn = cancel_btn;
238         vcardedit.statusbar  = statusbar;
239         vcardedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit vCard Dialog" );
240 }
241
242 AdapterDSource *addressbook_edit_vcard( AddressIndex *addrIndex, AdapterDSource *ads ) {
243         static gboolean cancelled;
244         gchar *sName;
245         gchar *sFile;
246         gchar *sFSFile;
247         AddressDataSource *ds = NULL;
248         VCardFile *vcf = NULL;
249         gboolean fin;
250
251         if( ! vcardedit.window )
252                 addressbook_edit_vcard_create(&cancelled);
253         gtk_widget_grab_focus(vcardedit.ok_btn);
254         gtk_widget_grab_focus(vcardedit.name_entry);
255         gtk_widget_show(vcardedit.window);
256         manage_window_set_transient(GTK_WINDOW(vcardedit.window));
257
258         edit_vcard_status_show( "" );
259         if( ads ) {
260                 ds = ads->dataSource;
261                 vcf = ds->rawDataSource;
262                 if ( vcard_get_name( vcf ) )
263                         gtk_entry_set_text(GTK_ENTRY(vcardedit.name_entry), vcard_get_name( vcf ) );
264                 if (vcf->path)
265                         gtk_entry_set_text(GTK_ENTRY(vcardedit.file_entry), vcf->path);
266                 gtk_window_set_title( GTK_WINDOW(vcardedit.window), _("Edit vCard Entry"));
267         }
268         else {
269                 gtk_entry_set_text(GTK_ENTRY(vcardedit.name_entry), ADDRESSBOOK_GUESS_VCARD );
270                 gtk_entry_set_text(GTK_ENTRY(vcardedit.file_entry), vcard_find_gnomecard() );
271                 gtk_window_set_title( GTK_WINDOW(vcardedit.window), _("Add New vCard Entry"));
272         }
273
274         gtk_main();
275         gtk_widget_hide(vcardedit.window);
276         if (cancelled == TRUE) return NULL;
277
278         fin = FALSE;
279         sName = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.name_entry), 0, -1 );
280         sFile = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.file_entry), 0, -1 );
281         sFSFile = conv_filename_from_utf8( sFile );
282         if( *sName == '\0' ) fin = TRUE;
283         if( *sFile == '\0' ) fin = TRUE;
284         if( ! sFSFile || *sFSFile == '\0' ) fin = TRUE;
285
286         if( ! fin ) {
287                 if( ! ads ) {
288                         vcf = vcard_create();
289                         ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_VCARD, vcf );
290                         ads = addressbook_create_ds_adapter( ds, ADDR_VCARD, NULL );
291                 }
292                 addressbook_ads_set_name( ads, sName );
293                 vcard_set_name( vcf, sName );
294                 vcard_set_file( vcf, sFSFile );
295         }
296         g_free( sFSFile );
297         g_free( sFile );
298         g_free( sName );
299
300         return ads;
301 }
302
303 /*
304 * End of Source.
305 */
306