Document hidden pref 'warn_sending_many_recipients_num'
[claws.git] / src / editbook.c
blobcc66fe13d852b81c544dd7bf553e2e1ad3aee89f
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 new address book entry.
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 "utils.h"
37 #include "prefs_common.h"
38 #include "mgutils.h"
39 #include "addressbook.h"
40 #include "addressitem.h"
41 #include "addrindex.h"
42 #include "addrbook.h"
43 #include "manage_window.h"
44 #include "gtkutils.h"
46 #define ADDRESSBOOK_GUESS_BOOK "MyAddressBook"
48 static struct _AddrBookEdit_Dlg {
49 GtkWidget *window;
50 GtkWidget *name_entry;
51 GtkWidget *file_label;
52 GtkWidget *ok_btn;
53 GtkWidget *cancel_btn;
54 GtkWidget *check_btn;
55 /* GtkWidget *file_btn; */
56 GtkWidget *statusbar;
57 gint status_cid;
58 AddressBookFile *bookFile;
59 } addrbookedit_dlg;
62 * Edit functions.
64 static void edit_book_status_show( gchar *msg ) {
65 if( addrbookedit_dlg.statusbar != NULL ) {
66 gtk_statusbar_pop( GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid );
67 if( msg ) {
68 gtk_statusbar_push(
69 GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid, msg );
71 else {
72 gtk_statusbar_push(
73 GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid, "" );
78 static void edit_book_ok( GtkWidget *widget, gboolean *cancelled ) {
79 *cancelled = FALSE;
80 gtk_main_quit();
83 static void edit_book_cancel( GtkWidget *widget, gboolean *cancelled ) {
84 *cancelled = TRUE;
85 gtk_main_quit();
88 static gint edit_book_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
89 *cancelled = TRUE;
90 gtk_main_quit();
91 return TRUE;
94 static gboolean edit_book_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
95 if (event && event->keyval == GDK_KEY_Escape) {
96 *cancelled = TRUE;
97 gtk_main_quit();
99 return FALSE;
102 static void edit_book_file_check( void ) {
103 gint t;
104 gchar *sMsg;
105 AddressBookFile *abf = addrbookedit_dlg.bookFile;
107 t = addrbook_test_read_file( abf, abf->fileName );
108 if( t == MGU_SUCCESS ) {
109 sMsg = _("File appears to be OK.");
111 else if( t == MGU_BAD_FORMAT ) {
112 sMsg = _("File does not appear to be a valid address book format.");
114 else {
115 sMsg = _("Could not read file.");
117 edit_book_status_show( sMsg );
120 static void edit_book_enable_buttons( gboolean enable ) {
121 gtk_widget_set_sensitive( addrbookedit_dlg.check_btn, enable );
122 /* gtk_widget_set_sensitive( addrbookedit_dlg.file_btn, enable ); */
125 static void edit_book_name_focus( GtkWidget *widget, GdkEventFocus *event, gpointer data) {
126 edit_book_status_show( "" );
129 static void addressbook_edit_book_create( gboolean *cancelled ) {
130 GtkWidget *window;
131 GtkWidget *vbox;
132 GtkWidget *table;
133 GtkWidget *label;
134 GtkWidget *name_entry;
135 GtkWidget *file_label;
136 GtkWidget *hbbox;
137 GtkWidget *hsep;
138 GtkWidget *ok_btn;
139 GtkWidget *cancel_btn;
140 GtkWidget *check_btn;
141 /* GtkWidget *file_btn; */
142 GtkWidget *statusbar;
143 GtkWidget *hsbox;
144 gint top;
146 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editbook");
147 gtk_widget_set_size_request(window, 450, -1);
148 gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
149 gtk_window_set_title(GTK_WINDOW(window), _("Edit Addressbook"));
150 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
151 g_signal_connect(G_OBJECT(window), "delete_event",
152 G_CALLBACK(edit_book_delete_event),
153 cancelled);
154 g_signal_connect(G_OBJECT(window), "key_press_event",
155 G_CALLBACK(edit_book_key_pressed),
156 cancelled);
158 vbox = gtk_vbox_new(FALSE, 8);
159 gtk_container_add(GTK_CONTAINER(window), vbox);
160 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
162 table = gtk_table_new(2, 3, FALSE);
163 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
164 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
165 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
166 gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
168 /* First row */
169 top = 0;
170 label = gtk_label_new(_("Name"));
171 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
172 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
174 name_entry = gtk_entry_new();
175 gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
177 check_btn = gtk_button_new_with_label( _(" Check File "));
178 gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
180 /* Second row */
181 top = 1;
182 label = gtk_label_new(_("File"));
183 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
184 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
186 file_label = gtk_label_new( "" );
187 gtk_misc_set_alignment(GTK_MISC(file_label), 0, 0.5);
188 gtk_table_attach(GTK_TABLE(table), file_label, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
190 /* file_btn = gtk_button_new_with_label( _(" ... ")); */
191 /* gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0); */
193 /* Status line */
194 hsbox = gtk_hbox_new(FALSE, 0);
195 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
196 statusbar = gtk_statusbar_new();
197 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
199 /* Button panel */
200 gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
201 &ok_btn, GTK_STOCK_OK,
202 NULL, NULL);
203 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
204 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
205 gtk_widget_grab_default(ok_btn);
207 hsep = gtk_hseparator_new();
208 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
210 g_signal_connect(G_OBJECT(name_entry), "focus_in_event",
211 G_CALLBACK(edit_book_name_focus), NULL );
212 g_signal_connect(G_OBJECT(ok_btn), "clicked",
213 G_CALLBACK(edit_book_ok), cancelled);
214 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
215 G_CALLBACK(edit_book_cancel), cancelled);
216 /* g_signal_connect(G_OBJECT(file_btn), "clicked", */
217 /* G_CALLBACK(edit_book_file_select), NULL); */
218 g_signal_connect(G_OBJECT(check_btn), "clicked",
219 G_CALLBACK(edit_book_file_check), NULL);
221 gtk_widget_show_all(vbox);
223 addrbookedit_dlg.window = window;
224 addrbookedit_dlg.name_entry = name_entry;
225 addrbookedit_dlg.file_label = file_label;
226 addrbookedit_dlg.ok_btn = ok_btn;
227 addrbookedit_dlg.cancel_btn = cancel_btn;
228 addrbookedit_dlg.check_btn = check_btn;
229 /* addrbookedit_dlg.file_btn = file_btn; */
230 addrbookedit_dlg.statusbar = statusbar;
231 addrbookedit_dlg.status_cid = gtk_statusbar_get_context_id(
232 GTK_STATUSBAR(statusbar), "Edit Addressbook Dialog" );
235 AdapterDSource *addressbook_edit_book( AddressIndex *addrIndex, AdapterDSource *ads ) {
236 static gboolean cancelled;
237 gchar *sName;
238 AddressDataSource *ds = NULL;
239 AddressBookFile *abf;
240 gboolean fin;
241 gboolean newBook = FALSE;
242 gchar *newFile = NULL;
244 if (!addrbookedit_dlg.window)
245 addressbook_edit_book_create(&cancelled);
246 gtk_widget_grab_focus(addrbookedit_dlg.ok_btn);
247 gtk_widget_grab_focus(addrbookedit_dlg.name_entry);
248 gtk_widget_show(addrbookedit_dlg.window);
249 manage_window_set_transient(GTK_WINDOW(addrbookedit_dlg.window));
250 gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg.window), TRUE);
252 edit_book_status_show( "" );
253 gtk_label_set_text( GTK_LABEL(addrbookedit_dlg.file_label), "" );
254 if( ads ) {
255 ds = ads->dataSource;
256 abf = ds->rawDataSource;
257 if ( addrbook_get_name( abf ) )
258 gtk_entry_set_text(GTK_ENTRY(addrbookedit_dlg.name_entry),
259 addrbook_get_name( abf ) );
260 if( abf->fileName )
261 gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), abf->fileName);
262 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Edit Addressbook"));
263 edit_book_enable_buttons( TRUE );
265 else {
266 gchar *tmp = NULL;
267 newBook = TRUE;
268 abf = addrbook_create_book();
269 addrbook_set_path( abf, addrIndex->filePath );
271 /* Take initial guess at file name */
272 newFile = addrbook_guess_next_file( abf );
273 if( newFile ) {
274 tmp = g_strdup_printf( "<%s>", newFile );
275 gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), tmp );
276 g_free( tmp );
278 g_free( newFile );
280 gtk_entry_set_text( GTK_ENTRY(addrbookedit_dlg.name_entry), ADDRESSBOOK_GUESS_BOOK );
281 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Add New Addressbook") );
282 edit_book_enable_buttons( FALSE );
285 addrbookedit_dlg.bookFile = abf;
287 gtk_main();
288 gtk_widget_hide(addrbookedit_dlg.window);
289 gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg.window), FALSE);
290 if( cancelled == TRUE ) {
291 if( newBook ) {
292 addrbook_free_book( abf );
293 abf = NULL;
295 return NULL;
298 fin = FALSE;
299 sName = gtk_editable_get_chars( GTK_EDITABLE(addrbookedit_dlg.name_entry), 0, -1 );
300 if( *sName == '\0' ) fin = TRUE;
302 if( fin ) {
303 if( newBook ) {
304 addrbook_free_book( abf );
305 abf = NULL;
308 else {
309 if( newBook ) {
310 /* Get final file name in case it changed */
311 newFile = addrbook_guess_next_file( abf );
312 addrbook_set_file( abf, newFile );
313 g_free( newFile );
314 ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
315 ads = addressbook_create_ds_adapter( ds, ADDR_BOOK, NULL );
317 addressbook_ads_set_name( ads, sName );
318 addrbook_set_name( abf, sName );
320 g_free( sName );
322 /* Save data */
323 if( abf ) addrbook_save_data( abf );
325 return ads;
329 * End of Source.