inital gtk2 patch
[claws.git] / src / editbook.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2002 Match Grun
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 2 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, write to the Free Software
17  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18  */
19
20 /*
21  * Edit new address book entry.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <gdk/gdkkeysyms.h>
32 #include <gtk/gtkwindow.h>
33 #include <gtk/gtksignal.h>
34 #include <gtk/gtklabel.h>
35 #include <gtk/gtkentry.h>
36 #include <gtk/gtktable.h>
37 #include <gtk/gtkbutton.h>
38
39 #include "intl.h"
40 #include "utils.h"
41 #include "prefs_common.h"
42 #include "mgutils.h"
43 #include "addressbook.h"
44 #include "addressitem.h"
45 #include "addrindex.h"
46 #include "addrbook.h"
47 #include "manage_window.h"
48 #include "gtkutils.h"
49
50 #define ADDRESSBOOK_GUESS_BOOK  "MyAddressBook"
51
52 static struct _AddrBookEdit_Dlg {
53         GtkWidget *window;
54         GtkWidget *name_entry;
55         GtkWidget *file_label;
56         GtkWidget *ok_btn;
57         GtkWidget *cancel_btn;
58         GtkWidget *check_btn;
59         /* GtkWidget *file_btn; */
60         GtkWidget *statusbar;
61         gint status_cid;
62         AddressBookFile *bookFile;
63 } addrbookedit_dlg;
64
65 /*
66 * Edit functions.
67 */
68 void edit_book_status_show( gchar *msg ) {
69         if( addrbookedit_dlg.statusbar != NULL ) {
70                 gtk_statusbar_pop( GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid );
71                 if( msg ) {
72                         gtk_statusbar_push(
73                                 GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid, msg );
74                 }
75                 else {
76                         gtk_statusbar_push(
77                                 GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid, "" );
78                 }
79         }
80 }
81
82 static void edit_book_ok( GtkWidget *widget, gboolean *cancelled ) {
83         *cancelled = FALSE;
84         gtk_main_quit();
85 }
86
87 static void edit_book_cancel( GtkWidget *widget, gboolean *cancelled ) {
88         *cancelled = TRUE;
89         gtk_main_quit();
90 }
91
92 static gint edit_book_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
93         *cancelled = TRUE;
94         gtk_main_quit();
95         return TRUE;
96 }
97
98 static gboolean edit_book_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
99         if (event && event->keyval == GDK_Escape) {
100                 *cancelled = TRUE;
101                 gtk_main_quit();
102         }
103         return FALSE;
104 }
105
106 static void edit_book_file_check( void ) {
107         gint t;
108         gchar *sMsg;
109         AddressBookFile *abf = addrbookedit_dlg.bookFile;
110
111         t = addrbook_test_read_file( abf, abf->fileName );
112         if( t == MGU_SUCCESS ) {
113                 sMsg = _("File appears to be Ok.");
114         }
115         else if( t == MGU_BAD_FORMAT ) {
116                 sMsg = _("File does not appear to be a valid address book format.");
117         }
118         else {
119                 sMsg = _("Could not read file.");
120         }
121         edit_book_status_show( sMsg );
122 }
123
124 static void edit_book_enable_buttons( gboolean enable ) {
125         gtk_widget_set_sensitive( addrbookedit_dlg.check_btn, enable );
126         /* gtk_widget_set_sensitive( addrbookedit_dlg.file_btn, enable ); */
127 }
128
129 static void edit_book_name_focus( GtkWidget *widget, GdkEventFocus *event, gpointer data) {
130         edit_book_status_show( "" );
131 }
132
133 static void addressbook_edit_book_create( gboolean *cancelled ) {
134         GtkWidget *window;
135         GtkWidget *vbox;
136         GtkWidget *table;
137         GtkWidget *label;
138         GtkWidget *name_entry;
139         GtkWidget *file_label;
140         GtkWidget *hbbox;
141         GtkWidget *hsep;
142         GtkWidget *ok_btn;
143         GtkWidget *cancel_btn;
144         GtkWidget *check_btn;
145         /* GtkWidget *file_btn; */
146         GtkWidget *statusbar;
147         GtkWidget *hsbox;
148         gint top;
149
150         window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
151         gtk_widget_set_size_request(window, 450, -1);
152         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
153         gtk_window_set_title(GTK_WINDOW(window), _("Edit Addressbook"));
154         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
155         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
156         g_signal_connect(G_OBJECT(window), "delete_event",
157                          G_CALLBACK(edit_book_delete_event),
158                          cancelled);
159         g_signal_connect(G_OBJECT(window), "key_press_event",
160                          G_CALLBACK(edit_book_key_pressed),
161                          cancelled);
162
163         vbox = gtk_vbox_new(FALSE, 8);
164         gtk_container_add(GTK_CONTAINER(window), vbox);
165         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
166
167         table = gtk_table_new(2, 3, FALSE);
168         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
169         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
170         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
171         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
172
173         /* First row */
174         top = 0;
175         label = gtk_label_new(_("Name"));
176         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
177         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
178
179         name_entry = gtk_entry_new();
180         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
181
182         check_btn = gtk_button_new_with_label( _(" Check File "));
183         gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
184
185         /* Second row */
186         top = 1;
187         label = gtk_label_new(_("File"));
188         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
189         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
190
191         file_label = gtk_label_new( "" );
192         gtk_misc_set_alignment(GTK_MISC(file_label), 0, 0.5);
193         gtk_table_attach(GTK_TABLE(table), file_label, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
194
195         /* file_btn = gtk_button_new_with_label( _(" ... ")); */
196         /* gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0); */
197
198         /* Status line */
199         hsbox = gtk_hbox_new(FALSE, 0);
200         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
201         statusbar = gtk_statusbar_new();
202         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
203
204         /* Button panel */
205         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
206                                 &cancel_btn, _("Cancel"), NULL, NULL);
207         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
208         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
209         gtk_widget_grab_default(ok_btn);
210
211         hsep = gtk_hseparator_new();
212         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
213
214         g_signal_connect(G_OBJECT(name_entry), "focus_in_event",
215                          G_CALLBACK(edit_book_name_focus), NULL );
216         g_signal_connect(G_OBJECT(ok_btn), "clicked",
217                          G_CALLBACK(edit_book_ok), cancelled);
218         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
219                          G_CALLBACK(edit_book_cancel), cancelled);
220 /*      g_signal_connect(G_OBJECT(file_btn), "clicked", */
221 /*                       G_CALLBACK(edit_book_file_select), NULL); */
222         g_signal_connect(G_OBJECT(check_btn), "clicked",
223                          G_CALLBACK(edit_book_file_check), NULL);
224
225         gtk_widget_show_all(vbox);
226
227         addrbookedit_dlg.window     = window;
228         addrbookedit_dlg.name_entry = name_entry;
229         addrbookedit_dlg.file_label = file_label;
230         addrbookedit_dlg.ok_btn     = ok_btn;
231         addrbookedit_dlg.cancel_btn = cancel_btn;
232         addrbookedit_dlg.check_btn  = check_btn;
233         /* addrbookedit_dlg.file_btn   = file_btn; */
234         addrbookedit_dlg.statusbar  = statusbar;
235         addrbookedit_dlg.status_cid = gtk_statusbar_get_context_id(
236                         GTK_STATUSBAR(statusbar), "Edit Addressbook Dialog" );
237 }
238
239 AdapterDSource *addressbook_edit_book( AddressIndex *addrIndex, AdapterDSource *ads ) {
240         static gboolean cancelled;
241         gchar *sName;
242         AddressDataSource *ds = NULL;
243         AddressBookFile *abf;
244         gboolean fin;
245         gboolean newBook = FALSE;
246         gchar *newFile = NULL;
247
248         if (!addrbookedit_dlg.window)
249                 addressbook_edit_book_create(&cancelled);
250         gtk_widget_grab_focus(addrbookedit_dlg.ok_btn);
251         gtk_widget_grab_focus(addrbookedit_dlg.name_entry);
252         gtk_widget_show(addrbookedit_dlg.window);
253         manage_window_set_transient(GTK_WINDOW(addrbookedit_dlg.window));
254
255         edit_book_status_show( "" );
256         gtk_label_set_text( GTK_LABEL(addrbookedit_dlg.file_label), "" );
257         if( ads ) {
258                 ds = ads->dataSource;
259                 abf = ds->rawDataSource;
260                 if ( addrbook_get_name( abf ) )
261                         gtk_entry_set_text(GTK_ENTRY(addrbookedit_dlg.name_entry),
262                                 addrbook_get_name( abf ) );
263                 if( abf->fileName )
264                         gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), abf->fileName);
265                 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Edit Addressbook"));
266                 edit_book_enable_buttons( TRUE );
267         }
268         else {
269                 gchar *tmp = NULL;
270                 newBook = TRUE;
271                 abf = addrbook_create_book();
272                 addrbook_set_path( abf, addrIndex->filePath );
273
274                 /* Take initial guess at file name */
275                 newFile = addrbook_guess_next_file( abf );
276                 if( newFile ) {
277                         tmp = g_strdup_printf( "<%s>", newFile );
278                         gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), tmp );
279                         g_free( tmp );
280                 }
281                 g_free( newFile );
282
283                 gtk_entry_set_text( GTK_ENTRY(addrbookedit_dlg.name_entry), ADDRESSBOOK_GUESS_BOOK );
284                 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Add New Addressbook") );
285                 edit_book_enable_buttons( FALSE );
286         }
287
288         addrbookedit_dlg.bookFile = abf;
289
290         gtk_main();
291         gtk_widget_hide(addrbookedit_dlg.window);
292
293         if( cancelled == TRUE ) {
294                 if( newBook ) {
295                         addrbook_free_book( abf );
296                         abf = NULL;
297                 }
298                 return NULL;
299         }
300
301         fin = FALSE;
302         sName = gtk_editable_get_chars( GTK_EDITABLE(addrbookedit_dlg.name_entry), 0, -1 );
303         if( *sName == '\0' ) fin = TRUE;
304
305         if( fin ) {
306                 if( newBook ) {
307                         addrbook_free_book( abf );
308                         abf = NULL;
309                 }
310         }
311         else {
312                 if( newBook ) {
313                         /* Get final file name in case it changed */
314                         newFile = addrbook_guess_next_file( abf );
315                         addrbook_set_file( abf, newFile );
316                         g_free( newFile );
317                         ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
318                         ads = addressbook_create_ds_adapter( ds, ADDR_BOOK, NULL );
319                 }
320                 addressbook_ads_set_name( ads, sName );
321                 addrbook_set_name( abf, sName );
322         }
323         g_free( sName );
324
325         /* Save data */
326         if( abf ) addrbook_save_data( abf );
327
328         return ads;
329 }
330
331 /*
332 * End of Source.
333 */