fix bug 4239, 'Preferences: Text Options Header Display modal is not modal' (sic)
[claws.git] / src / editbook.c
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 new address book entry.
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 "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"
45
46 #define ADDRESSBOOK_GUESS_BOOK  "MyAddressBook"
47
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;
60
61 /*
62 * Edit functions.
63 */
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 );
70                 }
71                 else {
72                         gtk_statusbar_push(
73                                 GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid, "" );
74                 }
75         }
76 }
77
78 static void edit_book_ok( GtkWidget *widget, gboolean *cancelled ) {
79         *cancelled = FALSE;
80         gtk_main_quit();
81 }
82
83 static void edit_book_cancel( GtkWidget *widget, gboolean *cancelled ) {
84         *cancelled = TRUE;
85         gtk_main_quit();
86 }
87
88 static gint edit_book_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
89         *cancelled = TRUE;
90         gtk_main_quit();
91         return TRUE;
92 }
93
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();
98         }
99         return FALSE;
100 }
101
102 static void edit_book_file_check( void ) {
103         gint t;
104         gchar *sMsg;
105         AddressBookFile *abf = addrbookedit_dlg.bookFile;
106
107         t = addrbook_test_read_file( abf, abf->fileName );
108         if( t == MGU_SUCCESS ) {
109                 sMsg = _("File appears to be OK.");
110         }
111         else if( t == MGU_BAD_FORMAT ) {
112                 sMsg = _("File does not appear to be a valid address book format.");
113         }
114         else {
115                 sMsg = _("Could not read file.");
116         }
117         edit_book_status_show( sMsg );
118 }
119
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 ); */
123 }
124
125 static void edit_book_name_focus( GtkWidget *widget, GdkEventFocus *event, gpointer data) {
126         edit_book_status_show( "" );
127 }
128
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;
145
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         gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
152         g_signal_connect(G_OBJECT(window), "delete_event",
153                          G_CALLBACK(edit_book_delete_event),
154                          cancelled);
155         g_signal_connect(G_OBJECT(window), "key_press_event",
156                          G_CALLBACK(edit_book_key_pressed),
157                          cancelled);
158
159         vbox = gtk_vbox_new(FALSE, 8);
160         gtk_container_add(GTK_CONTAINER(window), vbox);
161         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
162
163         table = gtk_table_new(2, 3, FALSE);
164         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
165         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
166         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
167         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
168
169         /* First row */
170         top = 0;
171         label = gtk_label_new(_("Name"));
172         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
173         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
174
175         name_entry = gtk_entry_new();
176         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
177
178         check_btn = gtk_button_new_with_label( _(" Check File "));
179         gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
180
181         /* Second row */
182         top = 1;
183         label = gtk_label_new(_("File"));
184         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
185         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
186
187         file_label = gtk_label_new( "" );
188         gtk_misc_set_alignment(GTK_MISC(file_label), 0, 0.5);
189         gtk_table_attach(GTK_TABLE(table), file_label, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
190
191         /* file_btn = gtk_button_new_with_label( _(" ... ")); */
192         /* gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0); */
193
194         /* Status line */
195         hsbox = gtk_hbox_new(FALSE, 0);
196         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
197         statusbar = gtk_statusbar_new();
198         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
199
200         /* Button panel */
201         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
202                                       &ok_btn, GTK_STOCK_OK,
203                                       NULL, NULL);
204         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
205         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
206         gtk_widget_grab_default(ok_btn);
207
208         hsep = gtk_hseparator_new();
209         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
210
211         g_signal_connect(G_OBJECT(name_entry), "focus_in_event",
212                          G_CALLBACK(edit_book_name_focus), NULL );
213         g_signal_connect(G_OBJECT(ok_btn), "clicked",
214                          G_CALLBACK(edit_book_ok), cancelled);
215         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
216                          G_CALLBACK(edit_book_cancel), cancelled);
217 /*      g_signal_connect(G_OBJECT(file_btn), "clicked", */
218 /*                       G_CALLBACK(edit_book_file_select), NULL); */
219         g_signal_connect(G_OBJECT(check_btn), "clicked",
220                          G_CALLBACK(edit_book_file_check), NULL);
221
222         gtk_widget_show_all(vbox);
223
224         addrbookedit_dlg.window     = window;
225         addrbookedit_dlg.name_entry = name_entry;
226         addrbookedit_dlg.file_label = file_label;
227         addrbookedit_dlg.ok_btn     = ok_btn;
228         addrbookedit_dlg.cancel_btn = cancel_btn;
229         addrbookedit_dlg.check_btn  = check_btn;
230         /* addrbookedit_dlg.file_btn   = file_btn; */
231         addrbookedit_dlg.statusbar  = statusbar;
232         addrbookedit_dlg.status_cid = gtk_statusbar_get_context_id(
233                         GTK_STATUSBAR(statusbar), "Edit Addressbook Dialog" );
234 }
235
236 AdapterDSource *addressbook_edit_book( AddressIndex *addrIndex, AdapterDSource *ads ) {
237         static gboolean cancelled;
238         gchar *sName;
239         AddressDataSource *ds = NULL;
240         AddressBookFile *abf;
241         gboolean fin;
242         gboolean newBook = FALSE;
243         gchar *newFile = NULL;
244
245         if (!addrbookedit_dlg.window)
246                 addressbook_edit_book_create(&cancelled);
247         gtk_widget_grab_focus(addrbookedit_dlg.ok_btn);
248         gtk_widget_grab_focus(addrbookedit_dlg.name_entry);
249         gtk_widget_show(addrbookedit_dlg.window);
250         manage_window_set_transient(GTK_WINDOW(addrbookedit_dlg.window));
251         gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg.window), TRUE);
252
253         edit_book_status_show( "" );
254         gtk_label_set_text( GTK_LABEL(addrbookedit_dlg.file_label), "" );
255         if( ads ) {
256                 ds = ads->dataSource;
257                 abf = ds->rawDataSource;
258                 if ( addrbook_get_name( abf ) )
259                         gtk_entry_set_text(GTK_ENTRY(addrbookedit_dlg.name_entry),
260                                 addrbook_get_name( abf ) );
261                 if( abf->fileName )
262                         gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), abf->fileName);
263                 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Edit Addressbook"));
264                 edit_book_enable_buttons( TRUE );
265         }
266         else {
267                 gchar *tmp = NULL;
268                 newBook = TRUE;
269                 abf = addrbook_create_book();
270                 addrbook_set_path( abf, addrIndex->filePath );
271
272                 /* Take initial guess at file name */
273                 newFile = addrbook_guess_next_file( abf );
274                 if( newFile ) {
275                         tmp = g_strdup_printf( "<%s>", newFile );
276                         gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), tmp );
277                         g_free( tmp );
278                 }
279                 g_free( newFile );
280
281                 gtk_entry_set_text( GTK_ENTRY(addrbookedit_dlg.name_entry), ADDRESSBOOK_GUESS_BOOK );
282                 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Add New Addressbook") );
283                 edit_book_enable_buttons( FALSE );
284         }
285
286         addrbookedit_dlg.bookFile = abf;
287
288         gtk_main();
289         gtk_widget_hide(addrbookedit_dlg.window);
290         gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg.window), FALSE);
291         if( cancelled == TRUE ) {
292                 if( newBook ) {
293                         addrbook_free_book( abf );
294                         abf = NULL;
295                 }
296                 return NULL;
297         }
298
299         fin = FALSE;
300         sName = gtk_editable_get_chars( GTK_EDITABLE(addrbookedit_dlg.name_entry), 0, -1 );
301         if( *sName == '\0' ) fin = TRUE;
302
303         if( fin ) {
304                 if( newBook ) {
305                         addrbook_free_book( abf );
306                         abf = NULL;
307                 }
308         }
309         else {
310                 if( newBook ) {
311                         /* Get final file name in case it changed */
312                         newFile = addrbook_guess_next_file( abf );
313                         addrbook_set_file( abf, newFile );
314                         g_free( newFile );
315                         ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
316                         ads = addressbook_create_ds_adapter( ds, ADDR_BOOK, NULL );
317                 }
318                 addressbook_ads_set_name( ads, sName );
319                 addrbook_set_name( abf, sName );
320         }
321         g_free( sName );
322
323         /* Save data */
324         if( abf ) addrbook_save_data( abf );
325
326         return ads;
327 }
328
329 /*
330 * End of Source.
331 */