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