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.
26 #include "claws-features.h"
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
36 #include "addressbook.h"
37 #include "prefs_common.h"
38 #include "addressitem.h"
44 #include "manage_window.h"
46 #define ADDRESSBOOK_GUESS_VCARD "MyGnomeCard"
48 static struct _VCardEdit
{
50 GtkWidget
*name_entry
;
51 GtkWidget
*file_entry
;
53 GtkWidget
*cancel_btn
;
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
);
65 gtk_statusbar_push( GTK_STATUSBAR(vcardedit
.statusbar
), vcardedit
.status_cid
, msg
);
70 static void edit_vcard_ok( GtkWidget
*widget
, gboolean
*cancelled
) {
75 static void edit_vcard_cancel( GtkWidget
*widget
, gboolean
*cancelled
) {
80 static void edit_vcard_file_check( void ) {
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
);
91 if( t
== MGU_SUCCESS
) {
94 else if( t
== MGU_BAD_FORMAT
) {
95 sMsg
= _("File does not appear to be vCard format.");
98 sMsg
= _("Could not read file.");
100 edit_vcard_status_show( sMsg
);
103 static void edit_vcard_file_select( void ) {
107 sFile
= filesel_select_file_open( _("Select vCard File"), NULL
);
110 sUTF8File
= conv_filename_to_utf8( sFile
);
111 gtk_entry_set_text( GTK_ENTRY(vcardedit
.file_entry
), sUTF8File
);
114 edit_vcard_file_check();
118 static gint
edit_vcard_delete_event( GtkWidget
*widget
, GdkEventAny
*event
, gboolean
*cancelled
) {
124 static gboolean
edit_vcard_key_pressed( GtkWidget
*widget
, GdkEventKey
*event
, gboolean
*cancelled
) {
125 if (event
&& event
->keyval
== GDK_KEY_Escape
) {
132 static void addressbook_edit_vcard_create( gboolean
*cancelled
) {
137 GtkWidget
*name_entry
;
138 GtkWidget
*file_entry
;
142 GtkWidget
*cancel_btn
;
143 GtkWidget
*check_btn
;
145 GtkWidget
*statusbar
;
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
),
157 g_signal_connect(G_OBJECT(window
), "key_press_event",
158 G_CALLBACK(edit_vcard_key_pressed
),
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 );
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);
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);
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
);
202 gtkut_stock_button_set_create(&hbbox
, &cancel_btn
, GTK_STOCK_CANCEL
,
203 &ok_btn
, GTK_STOCK_OK
,
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
;
237 AddressDataSource
*ds
= NULL
;
238 VCardFile
*vcf
= NULL
;
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( "" );
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
) );
255 gtk_entry_set_text(GTK_ENTRY(vcardedit
.file_entry
), vcf
->path
);
256 gtk_window_set_title( GTK_WINDOW(vcardedit
.window
), _("Edit vCard Entry"));
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"));
265 gtk_widget_hide(vcardedit
.window
);
266 gtk_window_set_modal(GTK_WINDOW(vcardedit
.window
), FALSE
);
267 if (cancelled
== TRUE
) return NULL
;
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
;
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
);