Remove wrong initialization from max_message_size
[claws.git] / src / editvcard.c
blob98cd82c128678ef15b289b503e322361bf915b24
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2022 the Claws Mail team and Match Grun
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/>.
20 * Edit vCard address book data.
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #include "claws-features.h"
26 #endif
28 #include "defs.h"
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtk.h>
35 #include "addressbook.h"
36 #include "prefs_common.h"
37 #include "addressitem.h"
38 #include "vcard.h"
39 #include "mgutils.h"
40 #include "filesel.h"
41 #include "gtkutils.h"
42 #include "codeconv.h"
43 #include "manage_window.h"
45 #define ADDRESSBOOK_GUESS_VCARD "MyGnomeCard"
47 static struct _VCardEdit {
48 GtkWidget *window;
49 GtkWidget *name_entry;
50 GtkWidget *file_entry;
51 GtkWidget *ok_btn;
52 GtkWidget *cancel_btn;
53 GtkWidget *statusbar;
54 gint status_cid;
55 } vcardedit;
58 * Edit functions.
60 static void edit_vcard_status_show( gchar *msg ) {
61 if( vcardedit.statusbar != NULL ) {
62 gtk_statusbar_pop( GTK_STATUSBAR(vcardedit.statusbar), vcardedit.status_cid );
63 if( msg ) {
64 gtk_statusbar_push( GTK_STATUSBAR(vcardedit.statusbar), vcardedit.status_cid, msg );
69 static void edit_vcard_ok( GtkWidget *widget, gboolean *cancelled ) {
70 *cancelled = FALSE;
71 gtk_main_quit();
74 static void edit_vcard_cancel( GtkWidget *widget, gboolean *cancelled ) {
75 *cancelled = TRUE;
76 gtk_main_quit();
79 static void edit_vcard_file_check( void ) {
80 gint t;
81 gchar *sFile;
82 gchar *sFSFile;
83 gchar *sMsg;
85 sFile = gtk_editable_get_chars( GTK_EDITABLE(vcardedit.file_entry), 0, -1 );
86 sFSFile = conv_filename_from_utf8( sFile );
87 t = vcard_test_read_file( sFSFile );
88 g_free( sFSFile );
89 g_free( sFile );
90 if( t == MGU_SUCCESS ) {
91 sMsg = "";
93 else if( t == MGU_BAD_FORMAT ) {
94 sMsg = _("File does not appear to be vCard format.");
96 else {
97 sMsg = _("Could not read file.");
99 edit_vcard_status_show( sMsg );
102 static void edit_vcard_file_select( void ) {
103 gchar *sFile;
104 gchar *sUTF8File;
106 sFile = filesel_select_file_open( _("Select vCard File"), NULL);
108 if( sFile ) {
109 sUTF8File = conv_filename_to_utf8( sFile );
110 gtk_entry_set_text( GTK_ENTRY(vcardedit.file_entry), sUTF8File );
111 g_free( sUTF8File );
112 g_free( sFile );
113 edit_vcard_file_check();
117 static gint edit_vcard_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
118 *cancelled = TRUE;
119 gtk_main_quit();
120 return TRUE;
123 static gboolean edit_vcard_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
124 if (event && event->keyval == GDK_KEY_Escape) {
125 *cancelled = TRUE;
126 gtk_main_quit();
128 return FALSE;
131 static void addressbook_edit_vcard_create( gboolean *cancelled ) {
132 GtkWidget *window;
133 GtkWidget *vbox;
134 GtkWidget *table;
135 GtkWidget *label;
136 GtkWidget *name_entry;
137 GtkWidget *file_entry;
138 GtkWidget *hbbox;
139 GtkWidget *hsep;
140 GtkWidget *ok_btn;
141 GtkWidget *cancel_btn;
142 GtkWidget *check_btn;
143 GtkWidget *file_btn;
144 GtkWidget *statusbar;
145 GtkWidget *hsbox;
147 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editvcard");
148 gtk_widget_set_size_request(window, 450, -1);
149 gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
150 gtk_window_set_title(GTK_WINDOW(window), _("Edit vCard Entry"));
151 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
152 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
153 g_signal_connect(G_OBJECT(window), "delete_event",
154 G_CALLBACK(edit_vcard_delete_event),
155 cancelled);
156 g_signal_connect(G_OBJECT(window), "key_press_event",
157 G_CALLBACK(edit_vcard_key_pressed),
158 cancelled);
160 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
161 gtk_container_add(GTK_CONTAINER(window), vbox);
162 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
164 table = gtk_grid_new();
165 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
166 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
167 gtk_grid_set_row_spacing(GTK_GRID(table), 8);
168 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
170 /* First row */
171 label = gtk_label_new(_("Name"));
172 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
173 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
175 name_entry = gtk_entry_new();
176 gtk_grid_attach(GTK_GRID(table), name_entry, 1, 0, 1, 1);
177 gtk_widget_set_hexpand(name_entry, TRUE);
178 gtk_widget_set_halign(name_entry, GTK_ALIGN_FILL);
180 check_btn = gtk_button_new_with_label( _(" Check File "));
181 gtk_grid_attach(GTK_GRID(table), check_btn, 2, 0, 1, 1);
183 /* Second row */
184 label = gtk_label_new(_("File"));
185 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
186 gtk_grid_attach(GTK_GRID(table), label, 0, 1, 1, 1);
188 file_entry = gtk_entry_new();
189 gtk_grid_attach(GTK_GRID(table), file_entry, 1, 1, 1, 1);
190 gtk_widget_set_hexpand(file_entry, TRUE);
191 gtk_widget_set_halign(file_entry, GTK_ALIGN_FILL);
193 file_btn = gtkut_get_browse_file_btn(_("_Browse"));
194 gtk_grid_attach(GTK_GRID(table), file_btn, 2, 1, 1, 1);
196 /* Status line */
197 hsbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 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);
202 /* Button panel */
203 gtkut_stock_button_set_create(&hbbox, &cancel_btn, NULL, _("_Cancel"),
204 &ok_btn, NULL, _("_OK"),
205 NULL, 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);
210 hsep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
211 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
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);
222 gtk_widget_show_all(vbox);
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" );
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;
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"));
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"));
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;
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;
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 );
284 addressbook_ads_set_name( ads, sName );
285 vcard_set_name( vcf, sName );
286 vcard_set_file( vcf, sFSFile );
288 g_free( sFSFile );
289 g_free( sFile );
290 g_free( sName );
292 return ads;
296 * End of Source.