New address book.
[claws.git] / src / editbook.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 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
48 #define ADDRESSBOOK_GUESS_BOOK  "MyAddressBook"
49
50 static struct _AddrBookEdit_Dlg {
51         GtkWidget *window;
52         GtkWidget *name_entry;
53         GtkWidget *file_label;
54         GtkWidget *ok_btn;
55         GtkWidget *cancel_btn;
56         GtkWidget *check_btn;
57         // GtkWidget *file_btn;
58         GtkWidget *statusbar;
59         gint status_cid;
60         AddressBookFile *bookFile;
61 } addrbookedit_dlg;
62
63 static struct _AddressFileSelection vcard_file_selector;
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 void 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 }
104
105 static void edit_book_file_check( void ) {
106         gint t;
107         gchar *sMsg;
108         AddressBookFile *abf = addrbookedit_dlg.bookFile;
109
110         t = addrbook_test_read_file( abf, abf->fileName );
111         if( t == MGU_SUCCESS ) {
112                 sMsg = _("File appears to be Ok.");
113         }
114         else if( t == MGU_BAD_FORMAT ) {
115                 sMsg = _("File does not appear to be a valid address book format.");
116         }
117         else {
118                 sMsg = _("Could not read file.");
119         }
120         edit_book_status_show( sMsg );
121 }
122
123 static void edit_book_enable_buttons( gboolean enable ) {
124         gtk_widget_set_sensitive( addrbookedit_dlg.check_btn, enable );
125         // gtk_widget_set_sensitive( addrbookedit_dlg.file_btn, enable );
126 }
127
128 static void edit_book_name_focus( GtkWidget *widget, GdkEventFocus *event, gpointer data) {
129         edit_book_status_show( "" );
130 }
131
132 static gchar *edit_book_guess_file( AddressBookFile *abf ) {
133         gchar *newFile = NULL;
134         GList *fileList = NULL;
135         gint fileNum = 1;
136         fileList = addrbook_get_bookfile_list( abf );
137         if( fileList ) {
138                 fileNum = 1 + abf->maxValue;
139         }
140         newFile = addrbook_gen_new_file_name( fileNum );
141         g_list_free( fileList );
142         fileList = NULL;
143         return newFile;
144 }
145
146 static void addressbook_edit_book_create( gboolean *cancelled ) {
147         GtkWidget *window;
148         GtkWidget *vbox;
149         GtkWidget *table;
150         GtkWidget *label;
151         GtkWidget *name_entry;
152         GtkWidget *file_label;
153         GtkWidget *hbbox;
154         GtkWidget *hsep;
155         GtkWidget *ok_btn;
156         GtkWidget *cancel_btn;
157         GtkWidget *check_btn;
158         // GtkWidget *file_btn;
159         GtkWidget *statusbar;
160         GtkWidget *hsbox;
161         gint top;
162
163         window = gtk_window_new(GTK_WINDOW_DIALOG);
164         gtk_widget_set_usize(window, 450, -1);
165         gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
166         gtk_window_set_title(GTK_WINDOW(window), _("Edit Addressbook"));
167         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
168         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
169         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
170                            GTK_SIGNAL_FUNC(edit_book_delete_event),
171                            cancelled);
172         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
173                            GTK_SIGNAL_FUNC(edit_book_key_pressed),
174                            cancelled);
175
176         vbox = gtk_vbox_new(FALSE, 8);
177         gtk_container_add(GTK_CONTAINER(window), vbox);
178         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
179
180         table = gtk_table_new(2, 3, FALSE);
181         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
182         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
183         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
184         gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
185
186         // First row
187         top = 0;
188         label = gtk_label_new(_("Name"));
189         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
190         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
191
192         name_entry = gtk_entry_new();
193         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
194
195         check_btn = gtk_button_new_with_label( _(" Check File "));
196         gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
197
198         // Second row
199         top = 1;
200         label = gtk_label_new(_("File"));
201         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
202         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
203
204         file_label = gtk_label_new( "" );
205         gtk_misc_set_alignment(GTK_MISC(file_label), 0, 0.5);
206         gtk_table_attach(GTK_TABLE(table), file_label, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
207
208         // file_btn = gtk_button_new_with_label( _(" ... "));
209         // gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
210
211         // Status line
212         hsbox = gtk_hbox_new(FALSE, 0);
213         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
214         statusbar = gtk_statusbar_new();
215         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
216
217         // Button panel
218         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
219                                 &cancel_btn, _("Cancel"), NULL, NULL);
220         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
221         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
222         gtk_widget_grab_default(ok_btn);
223
224         hsep = gtk_hseparator_new();
225         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
226
227         gtk_signal_connect(GTK_OBJECT(name_entry), "focus_in_event",
228                            GTK_SIGNAL_FUNC(edit_book_name_focus), NULL );
229         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
230                            GTK_SIGNAL_FUNC(edit_book_ok), cancelled);
231         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
232                            GTK_SIGNAL_FUNC(edit_book_cancel), cancelled);
233 //      gtk_signal_connect(GTK_OBJECT(file_btn), "clicked",
234 //                         GTK_SIGNAL_FUNC(edit_book_file_select), NULL);
235         gtk_signal_connect(GTK_OBJECT(check_btn), "clicked",
236                            GTK_SIGNAL_FUNC(edit_book_file_check), NULL);
237
238         gtk_widget_show_all(vbox);
239
240         addrbookedit_dlg.window     = window;
241         addrbookedit_dlg.name_entry = name_entry;
242         addrbookedit_dlg.file_label = file_label;
243         addrbookedit_dlg.ok_btn     = ok_btn;
244         addrbookedit_dlg.cancel_btn = cancel_btn;
245         addrbookedit_dlg.check_btn  = check_btn;
246         // addrbookedit_dlg.file_btn   = file_btn;
247         addrbookedit_dlg.statusbar  = statusbar;
248         addrbookedit_dlg.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit Addressbook Dialog" );
249 }
250
251 AdapterDSource *addressbook_edit_book( AddressIndex *addrIndex, AdapterDSource *ads ) {
252         static gboolean cancelled;
253         gchar *sName;
254         AddressDataSource *ds = NULL;
255         AddressBookFile *abf;
256         gboolean fin;
257         gboolean newBook = FALSE;
258         gchar *newFile = NULL;
259         gint rc;
260
261         if (!addrbookedit_dlg.window)
262                 addressbook_edit_book_create(&cancelled);
263         gtk_widget_grab_focus(addrbookedit_dlg.ok_btn);
264         gtk_widget_grab_focus(addrbookedit_dlg.name_entry);
265         gtk_widget_show(addrbookedit_dlg.window);
266         manage_window_set_transient(GTK_WINDOW(addrbookedit_dlg.window));
267
268         edit_book_status_show( "" );
269         gtk_label_set_text( GTK_LABEL(addrbookedit_dlg.file_label), "" );
270         if( ads ) {
271                 ds = ads->dataSource;
272                 abf = ds->rawDataSource;
273                 if (abf->name)
274                         gtk_entry_set_text(GTK_ENTRY(addrbookedit_dlg.name_entry), abf->name);
275                 if( abf->fileName )
276                         gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), abf->fileName);
277                 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Edit Addressbook"));
278                 edit_book_enable_buttons( TRUE );
279         }
280         else {
281                 gchar *tmp = NULL;
282                 newBook = TRUE;
283                 abf = addrbook_create_book();
284                 addrbook_set_path( abf, addrIndex->filePath );
285
286                 // Take initial guess at file name
287                 newFile = edit_book_guess_file( abf );
288                 if( newFile ) {
289                         tmp = g_strdup_printf( "<%s>", newFile );
290                         gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), tmp );
291                         g_free( tmp );
292                 }
293                 g_free( newFile );
294
295                 gtk_entry_set_text( GTK_ENTRY(addrbookedit_dlg.name_entry), ADDRESSBOOK_GUESS_BOOK );
296                 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Add New Addressbook") );
297                 edit_book_enable_buttons( FALSE );
298         }
299
300         addrbookedit_dlg.bookFile = abf;
301
302         gtk_main();
303         gtk_widget_hide(addrbookedit_dlg.window);
304
305         if( cancelled == TRUE ) {
306                 if( newBook ) {
307                         addrbook_free_book( abf );
308                         abf = NULL;
309                 }
310                 return NULL;
311         }
312
313         fin = FALSE;
314         sName = gtk_editable_get_chars( GTK_EDITABLE(addrbookedit_dlg.name_entry), 0, -1 );
315         if( *sName == '\0' ) fin = TRUE;
316
317         if( fin ) {
318                 if( newBook ) {
319                         addrbook_free_book( abf );
320                         abf = NULL;
321                 }
322         }
323         else {
324                 if( newBook ) {
325                         // Get final file name in case it changed
326                         newFile = edit_book_guess_file( abf );
327                         addrbook_set_file( abf, newFile );
328                         g_free( newFile );
329                         ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
330                         ads = addressbook_create_ds_adapter( ds, ADDR_BOOK, NULL );
331                 }
332                 addressbook_ads_set_name( ads, sName );
333                 addrbook_set_name( abf, sName );
334                 abf->dirtyFlag = TRUE;
335         }
336         g_free( sName );
337
338         // Save data
339         if( abf ) addrbook_save_data( abf );
340
341         return ads;
342 }
343
344 /*
345 * End of Source.
346 */
347