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