Statistics
| Revision:

root / src / editbook.c @ 404

History | View | Annotate | Download (10.3 kB)

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