Refactor colors handling with more arrays
[claws.git] / src / editvcard.c
blob6911a8c0e6a20bb7c0ac1556db63b1a45fad10b3
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2012 Match Grun and the Claws Mail team
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.
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.
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/>.
21 * Edit vCard address book data.
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #include "claws-features.h"
27 #endif
29 #include "defs.h"
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtk.h>
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"
46 #define ADDRESSBOOK_GUESS_VCARD "MyGnomeCard"
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;
59 * Edit functions.
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 );
70 static void edit_vcard_ok( GtkWidget *widget, gboolean *cancelled ) {
71 *cancelled = FALSE;
72 gtk_main_quit();
75 static void edit_vcard_cancel( GtkWidget *widget, gboolean *cancelled ) {
76 *cancelled = TRUE;
77 gtk_main_quit();
80 static void edit_vcard_file_check( void ) {
81 gint t;
82 gchar *sFile;
83 gchar *sFSFile;
84 gchar *sMsg;
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 = "";
94 else if( t == MGU_BAD_FORMAT ) {
95 sMsg = _("File does not appear to be vCard format.");
97 else {
98 sMsg = _("Could not read file.");
100 edit_vcard_status_show( sMsg );
103 static void edit_vcard_file_select( void ) {
104 gchar *sFile;
105 gchar *sUTF8File;
107 sFile = filesel_select_file_open( _("Select vCard File"), NULL);
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();
118 static gint edit_vcard_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
119 *cancelled = TRUE;
120 gtk_main_quit();
121 return TRUE;
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();
129 return FALSE;
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;
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 g_signal_connect(G_OBJECT(window), "delete_event",
155 G_CALLBACK(edit_vcard_delete_event),
156 cancelled);
157 g_signal_connect(G_OBJECT(window), "key_press_event",
158 G_CALLBACK(edit_vcard_key_pressed),
159 cancelled);
161 vbox = gtk_vbox_new(FALSE, 8);
162 gtk_container_add(GTK_CONTAINER(window), vbox);
163 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
165 table = gtk_table_new(2, 3, FALSE);
166 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
167 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
168 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
169 gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
171 /* First row */
172 top = 0;
173 label = gtk_label_new(_("Name"));
174 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
175 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
177 name_entry = gtk_entry_new();
178 gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
180 check_btn = gtk_button_new_with_label( _(" Check File "));
181 gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
183 /* Second row */
184 top = 1;
185 label = gtk_label_new(_("File"));
186 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
187 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
189 file_entry = gtk_entry_new();
190 gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
192 file_btn = gtkut_get_browse_file_btn(_("_Browse"));
193 gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
195 /* Status line */
196 hsbox = gtk_hbox_new(FALSE, 0);
197 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
198 statusbar = gtk_statusbar_new();
199 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
201 /* Button panel */
202 gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
203 &ok_btn, GTK_STOCK_OK,
204 NULL, NULL);
205 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
206 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
207 gtk_widget_grab_default(ok_btn);
209 hsep = gtk_hseparator_new();
210 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
212 g_signal_connect(G_OBJECT(ok_btn), "clicked",
213 G_CALLBACK(edit_vcard_ok), cancelled);
214 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
215 G_CALLBACK(edit_vcard_cancel), cancelled);
216 g_signal_connect(G_OBJECT(file_btn), "clicked",
217 G_CALLBACK(edit_vcard_file_select), NULL);
218 g_signal_connect(G_OBJECT(check_btn), "clicked",
219 G_CALLBACK(edit_vcard_file_check), NULL);
221 gtk_widget_show_all(vbox);
223 vcardedit.window = window;
224 vcardedit.name_entry = name_entry;
225 vcardedit.file_entry = file_entry;
226 vcardedit.ok_btn = ok_btn;
227 vcardedit.cancel_btn = cancel_btn;
228 vcardedit.statusbar = statusbar;
229 vcardedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit vCard Dialog" );
232 AdapterDSource *addressbook_edit_vcard( AddressIndex *addrIndex, AdapterDSource *ads ) {
233 static gboolean cancelled;
234 gchar *sName;
235 gchar *sFile;
236 gchar *sFSFile;
237 AddressDataSource *ds = NULL;
238 VCardFile *vcf = NULL;
239 gboolean fin;
241 if( ! vcardedit.window )
242 addressbook_edit_vcard_create(&cancelled);
243 gtk_widget_grab_focus(vcardedit.ok_btn);
244 gtk_widget_grab_focus(vcardedit.name_entry);
245 gtk_widget_show(vcardedit.window);
246 manage_window_set_transient(GTK_WINDOW(vcardedit.window));
247 gtk_window_set_modal(GTK_WINDOW(vcardedit.window), TRUE);
248 edit_vcard_status_show( "" );
249 if( ads ) {
250 ds = ads->dataSource;
251 vcf = ds->rawDataSource;
252 if ( vcard_get_name( vcf ) )
253 gtk_entry_set_text(GTK_ENTRY(vcardedit.name_entry), vcard_get_name( vcf ) );
254 if (vcf->path)
255 gtk_entry_set_text(GTK_ENTRY(vcardedit.file_entry), vcf->path);
256 gtk_window_set_title( GTK_WINDOW(vcardedit.window), _("Edit vCard Entry"));
258 else {
259 gtk_entry_set_text(GTK_ENTRY(vcardedit.name_entry), ADDRESSBOOK_GUESS_VCARD );
260 gtk_entry_set_text(GTK_ENTRY(vcardedit.file_entry), vcard_find_gnomecard() );
261 gtk_window_set_title( GTK_WINDOW(vcardedit.window), _("Add New vCard Entry"));
264 gtk_main();
265 gtk_widget_hide(vcardedit.window);
266 gtk_window_set_modal(GTK_WINDOW(vcardedit.window), FALSE);
267 if (cancelled == TRUE) return NULL;
269 fin = FALSE;
270 sName = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.name_entry), 0, -1 );
271 sFile = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.file_entry), 0, -1 );
272 sFSFile = conv_filename_from_utf8( sFile );
273 if( *sName == '\0' ) fin = TRUE;
274 if( *sFile == '\0' ) fin = TRUE;
275 if( ! sFSFile || *sFSFile == '\0' ) fin = TRUE;
277 if( ! fin ) {
278 if( ! ads ) {
279 vcf = vcard_create();
280 ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_VCARD, vcf );
281 ads = addressbook_create_ds_adapter( ds, ADDR_VCARD, NULL );
283 addressbook_ads_set_name( ads, sName );
284 vcard_set_name( vcf, sName );
285 vcard_set_file( vcf, sFSFile );
287 g_free( sFSFile );
288 g_free( sFile );
289 g_free( sName );
291 return ads;
295 * End of Source.