Statistics
| Revision:

root / src / editbook.c @ 1

History | View | Annotate | Download (10.2 KB)

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
#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
/* static struct _AddressFileSelection vcard_file_selector; */
66

    
67
/*
68
* Edit functions.
69
*/
70
void edit_book_status_show( gchar *msg ) {
71
        if( addrbookedit_dlg.statusbar != NULL ) {
72
                gtk_statusbar_pop( GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid );
73
                if( msg ) {
74
                        gtk_statusbar_push(
75
                                GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid, msg );
76
                }
77
                else {
78
                        gtk_statusbar_push(
79
                                GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid, "" );
80
                }
81
        }
82
}
83

    
84
static void edit_book_ok( GtkWidget *widget, gboolean *cancelled ) {
85
        *cancelled = FALSE;
86
        gtk_main_quit();
87
}
88

    
89
static void edit_book_cancel( GtkWidget *widget, gboolean *cancelled ) {
90
        *cancelled = TRUE;
91
        gtk_main_quit();
92
}
93

    
94
static gint edit_book_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
95
        *cancelled = TRUE;
96
        gtk_main_quit();
97
        return TRUE;
98
}
99

    
100
static gboolean edit_book_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
101
        if (event && event->keyval == GDK_Escape) {
102
                *cancelled = TRUE;
103
                gtk_main_quit();
104
        }
105
        return FALSE;
106
}
107

    
108
static void edit_book_file_check( void ) {
109
        gint t;
110
        gchar *sMsg;
111
        AddressBookFile *abf = addrbookedit_dlg.bookFile;
112

    
113
        t = addrbook_test_read_file( abf, abf->fileName );
114
        if( t == MGU_SUCCESS ) {
115
                sMsg = _("File appears to be Ok.");
116
        }
117
        else if( t == MGU_BAD_FORMAT ) {
118
                sMsg = _("File does not appear to be a valid address book format.");
119
        }
120
        else {
121
                sMsg = _("Could not read file.");
122
        }
123
        edit_book_status_show( sMsg );
124
}
125

    
126
static void edit_book_enable_buttons( gboolean enable ) {
127
        gtk_widget_set_sensitive( addrbookedit_dlg.check_btn, enable );
128
        /* gtk_widget_set_sensitive( addrbookedit_dlg.file_btn, enable ); */
129
}
130

    
131
static void edit_book_name_focus( GtkWidget *widget, GdkEventFocus *event, gpointer data) {
132
        edit_book_status_show( "" );
133
}
134

    
135
static gchar *edit_book_guess_file( AddressBookFile *abf ) {
136
        gchar *newFile = NULL;
137
        GList *fileList = NULL;
138
        gint fileNum = 1;
139
        fileList = addrbook_get_bookfile_list( abf );
140
        if( fileList ) {
141
                fileNum = 1 + abf->maxValue;
142
        }
143
        newFile = addrbook_gen_new_file_name( fileNum );
144
        g_list_free( fileList );
145
        fileList = NULL;
146
        return newFile;
147
}
148

    
149
static void addressbook_edit_book_create( gboolean *cancelled ) {
150
        GtkWidget *window;
151
        GtkWidget *vbox;
152
        GtkWidget *table;
153
        GtkWidget *label;
154
        GtkWidget *name_entry;
155
        GtkWidget *file_label;
156
        GtkWidget *hbbox;
157
        GtkWidget *hsep;
158
        GtkWidget *ok_btn;
159
        GtkWidget *cancel_btn;
160
        GtkWidget *check_btn;
161
        /* GtkWidget *file_btn; */
162
        GtkWidget *statusbar;
163
        GtkWidget *hsbox;
164
        gint top;
165

    
166
        window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
167
        gtk_widget_set_size_request(window, 450, -1);
168
        gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
169
        gtk_window_set_title(GTK_WINDOW(window), _("Edit Addressbook"));
170
        gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
171
        gtk_window_set_modal(GTK_WINDOW(window), TRUE);        
172
        g_signal_connect(G_OBJECT(window), "delete_event",
173
                         G_CALLBACK(edit_book_delete_event),
174
                         cancelled);
175
        g_signal_connect(G_OBJECT(window), "key_press_event",
176
                         G_CALLBACK(edit_book_key_pressed),
177
                         cancelled);
178

    
179
        vbox = gtk_vbox_new(FALSE, 8);
180
        gtk_container_add(GTK_CONTAINER(window), vbox);
181
        gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
182

    
183
        table = gtk_table_new(2, 3, FALSE);
184
        gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
185
        gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
186
        gtk_table_set_row_spacings(GTK_TABLE(table), 8);
187
        gtk_table_set_col_spacings(GTK_TABLE(table), 8 );
188

    
189
        /* First row */
190
        top = 0;
191
        label = gtk_label_new(_("Name"));
192
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
193
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
194

    
195
        name_entry = gtk_entry_new();
196
        gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
197

    
198
        check_btn = gtk_button_new_with_label( _(" Check File "));
199
        gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
200

    
201
        /* Second row */
202
        top = 1;
203
        label = gtk_label_new(_("File"));
204
        gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
205
        gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
206

    
207
        file_label = gtk_label_new( "" );
208
        gtk_misc_set_alignment(GTK_MISC(file_label), 0, 0.5);
209
        gtk_table_attach(GTK_TABLE(table), file_label, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
210

    
211
        /* file_btn = gtk_button_new_with_label( _(" ... ")); */
212
        /* gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0); */
213

    
214
        /* Status line */
215
        hsbox = gtk_hbox_new(FALSE, 0);
216
        gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
217
        statusbar = gtk_statusbar_new();
218
        gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
219

    
220
        /* Button panel */
221
        gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
222
                                &cancel_btn, _("Cancel"), NULL, NULL);
223
        gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
224
        gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
225
        gtk_widget_grab_default(ok_btn);
226

    
227
        hsep = gtk_hseparator_new();
228
        gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
229

    
230
        g_signal_connect(G_OBJECT(name_entry), "focus_in_event",
231
                         G_CALLBACK(edit_book_name_focus), NULL );
232
        g_signal_connect(G_OBJECT(ok_btn), "clicked",
233
                         G_CALLBACK(edit_book_ok), cancelled);
234
        g_signal_connect(G_OBJECT(cancel_btn), "clicked",
235
                         G_CALLBACK(edit_book_cancel), cancelled);
236
/*        g_signal_connect(G_OBJECT(file_btn), "clicked", */
237
/*                         G_CALLBACK(edit_book_file_select), NULL); */
238
        g_signal_connect(G_OBJECT(check_btn), "clicked",
239
                         G_CALLBACK(edit_book_file_check), NULL);
240

    
241
        gtk_widget_show_all(vbox);
242

    
243
        addrbookedit_dlg.window     = window;
244
        addrbookedit_dlg.name_entry = name_entry;
245
        addrbookedit_dlg.file_label = file_label;
246
        addrbookedit_dlg.ok_btn     = ok_btn;
247
        addrbookedit_dlg.cancel_btn = cancel_btn;
248
        addrbookedit_dlg.check_btn  = check_btn;
249
        /* addrbookedit_dlg.file_btn   = file_btn; */
250
        addrbookedit_dlg.statusbar  = statusbar;
251
        addrbookedit_dlg.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit Addressbook Dialog" );
252
}
253

    
254
AdapterDSource *addressbook_edit_book( AddressIndex *addrIndex, AdapterDSource *ads ) {
255
        static gboolean cancelled;
256
        gchar *sName;
257
        AddressDataSource *ds = NULL;
258
        AddressBookFile *abf;
259
        gboolean fin;
260
        gboolean newBook = FALSE;
261
        gchar *newFile = NULL;
262

    
263
        if (!addrbookedit_dlg.window)
264
                addressbook_edit_book_create(&cancelled);
265
        gtk_widget_grab_focus(addrbookedit_dlg.ok_btn);
266
        gtk_widget_grab_focus(addrbookedit_dlg.name_entry);
267
        gtk_widget_show(addrbookedit_dlg.window);
268
        manage_window_set_transient(GTK_WINDOW(addrbookedit_dlg.window));
269

    
270
        edit_book_status_show( "" );
271
        gtk_label_set_text( GTK_LABEL(addrbookedit_dlg.file_label), "" );
272
        if( ads ) {
273
                ds = ads->dataSource;
274
                abf = ds->rawDataSource;
275
                if (abf->name)
276
                        gtk_entry_set_text(GTK_ENTRY(addrbookedit_dlg.name_entry), abf->name);
277
                if( abf->fileName )
278
                        gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), abf->fileName);
279
                gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Edit Addressbook"));
280
                edit_book_enable_buttons( TRUE );
281
        }
282
        else {
283
                gchar *tmp = NULL;
284
                newBook = TRUE;
285
                abf = addrbook_create_book();
286
                addrbook_set_path( abf, addrIndex->filePath );
287

    
288
                /* Take initial guess at file name */
289
                newFile = edit_book_guess_file( abf );
290
                if( newFile ) {
291
                        tmp = g_strdup_printf( "<%s>", newFile );
292
                        gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), tmp );
293
                        g_free( tmp );
294
                }
295
                g_free( newFile );
296

    
297
                gtk_entry_set_text( GTK_ENTRY(addrbookedit_dlg.name_entry), ADDRESSBOOK_GUESS_BOOK );
298
                gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Add New Addressbook") );
299
                edit_book_enable_buttons( FALSE );
300
        }
301

    
302
        addrbookedit_dlg.bookFile = abf;
303

    
304
        gtk_main();
305
        gtk_widget_hide(addrbookedit_dlg.window);
306

    
307
        if( cancelled == TRUE ) {
308
                if( newBook ) {
309
                        addrbook_free_book( abf );
310
                        abf = NULL;
311
                }
312
                return NULL;
313
        }
314

    
315
        fin = FALSE;
316
        sName = gtk_editable_get_chars( GTK_EDITABLE(addrbookedit_dlg.name_entry), 0, -1 );
317
        if( *sName == '\0' ) fin = TRUE;
318

    
319
        if( fin ) {
320
                if( newBook ) {
321
                        addrbook_free_book( abf );
322
                        abf = NULL;
323
                }
324
        }
325
        else {
326
                if( newBook ) {
327
                        /* Get final file name in case it changed */
328
                        newFile = edit_book_guess_file( abf );
329
                        addrbook_set_file( abf, newFile );
330
                        g_free( newFile );
331
                        ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
332
                        ads = addressbook_create_ds_adapter( ds, ADDR_BOOK, NULL );
333
                }
334
                addressbook_ads_set_name( ads, sName );
335
                addrbook_set_name( abf, sName );
336
                abf->dirtyFlag = TRUE;
337
        }
338
        g_free( sName );
339

    
340
        /* Save data */
341
        if( abf ) addrbook_save_data( abf );
342

    
343
        return ads;
344
}
345

    
346
/*
347
* End of Source.
348
*/