2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001 Match Grun
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.
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.
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.
21 * Edit JPilot address book data.
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtkwindow.h>
35 #include <gtk/gtksignal.h>
36 #include <gtk/gtkhbox.h>
37 #include <gtk/gtklabel.h>
38 #include <gtk/gtkentry.h>
39 #include <gtk/gtkhbbox.h>
40 #include <gtk/gtkbutton.h>
43 #include "addressbook.h"
44 #include "prefs_common.h"
45 #include "addressitem.h"
49 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
50 #define JPILOT_NUM_CUSTOM_LABEL 4
52 static struct _JPilotEdit {
54 GtkWidget *name_entry;
55 GtkWidget *file_entry;
56 GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
57 GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
59 GtkWidget *cancel_btn;
64 static struct _AddressFileSelection jpilot_file_selector;
69 void edit_jpilot_status_show( gchar *msg ) {
70 if( jpilotedit.statusbar != NULL ) {
71 gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
73 gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
78 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
84 static void edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
85 if (event && event->keyval == GDK_Escape) {
91 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
96 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
101 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
103 GList *node, *customLbl = NULL;
105 gboolean done, checked;
106 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
107 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
108 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
113 customLbl = jpilot_load_custom_label( jpf, customLbl );
117 labelName = node->data;
118 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), labelName );
119 checked = jpilot_test_custom_label( jpf, labelName );
120 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), checked );
122 if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
123 node = g_list_next( node );
129 mgu_free_dlist( customLbl );
133 static void edit_jpilot_fill_check_box_new() {
135 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
136 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
137 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
141 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
144 jpilot_clear_custom_labels( pilotFile );
145 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
146 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(jpilotedit.custom_check[i]) ) ) {
147 labelName = GTK_LABEL(jpilotedit.custom_label[i])->label;
148 jpilot_add_custom_label( pilotFile, labelName );
153 static void edit_jpilot_file_check( void ) {
160 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
162 g_strchomp( sFile ); g_strchug( sFile );
163 if( *sFile != '\0' ) {
164 // Attempt to read file
165 JPilotFile *jpf = jpilot_create_path( sFile );
166 t = jpilot_read_data( jpf );
167 if( t == MGU_SUCCESS ) {
169 edit_jpilot_fill_check_box( jpf );
177 // Clear all check boxes
178 edit_jpilot_fill_check_box_new();
182 // Display appropriate message
183 if( t == MGU_SUCCESS ) {
186 else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
187 sMsg = _("File does not appear to be JPilot format.");
190 sMsg = _("Could not read file.");
192 edit_jpilot_status_show( sMsg );
195 static void edit_jpilot_file_ok( GtkWidget *widget, gpointer data ) {
197 AddressFileSelection *afs;
200 afs = ( AddressFileSelection * ) data;
201 fileSel = afs->fileSelector;
202 sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
204 afs->cancelled = FALSE;
205 gtk_entry_set_text( GTK_ENTRY(jpilotedit.file_entry), sFile );
206 gtk_widget_hide( afs->fileSelector );
207 gtk_grab_remove( afs->fileSelector );
208 edit_jpilot_file_check();
209 gtk_widget_grab_focus( jpilotedit.file_entry );
212 static void edit_jpilot_file_cancel( GtkWidget *widget, gpointer data ) {
213 AddressFileSelection *afs = ( AddressFileSelection * ) data;
214 afs->cancelled = TRUE;
215 gtk_widget_hide( afs->fileSelector );
216 gtk_grab_remove( afs->fileSelector );
217 gtk_widget_grab_focus( jpilotedit.file_entry );
220 static void edit_jpilot_file_select_create( AddressFileSelection *afs ) {
221 GtkWidget *fileSelector;
223 fileSelector = gtk_file_selection_new( _("Select JPilot File") );
224 gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
225 gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
226 "clicked", GTK_SIGNAL_FUNC (edit_jpilot_file_ok), ( gpointer ) afs );
227 gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
228 "clicked", GTK_SIGNAL_FUNC (edit_jpilot_file_cancel), ( gpointer ) afs );
229 afs->fileSelector = fileSelector;
230 afs->cancelled = TRUE;
233 static void edit_jpilot_file_select( void ) {
236 if (! jpilot_file_selector.fileSelector )
237 edit_jpilot_file_select_create( & jpilot_file_selector );
239 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
240 gtk_file_selection_set_filename( GTK_FILE_SELECTION( jpilot_file_selector.fileSelector ), sFile );
242 gtk_widget_show( jpilot_file_selector.fileSelector );
243 gtk_grab_add( jpilot_file_selector.fileSelector );
246 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
251 GtkWidget *name_entry;
252 GtkWidget *file_entry;
253 GtkWidget *vbox_custom;
254 GtkWidget *frame_custom;
255 GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
256 GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
261 GtkWidget *cancel_btn;
262 GtkWidget *check_btn;
265 GtkWidget *statusbar;
268 window = gtk_window_new(GTK_WINDOW_DIALOG);
269 gtk_widget_set_usize(window, 450, -1);
270 gtk_container_set_border_width(GTK_CONTAINER(window), 0);
271 gtk_window_set_title(GTK_WINDOW(window), _("Edit JPilot Entry"));
272 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
273 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
274 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
275 GTK_SIGNAL_FUNC(edit_jpilot_delete_event),
277 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
278 GTK_SIGNAL_FUNC(edit_jpilot_key_pressed),
281 vbox = gtk_vbox_new(FALSE, 8);
282 gtk_container_add(GTK_CONTAINER(window), vbox);
283 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
285 table = gtk_table_new(2 + JPILOT_NUM_CUSTOM_LABEL, 3, FALSE);
286 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
287 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
288 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
289 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
293 label = gtk_label_new(_("Name"));
294 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
295 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
297 name_entry = gtk_entry_new();
298 gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
300 check_btn = gtk_button_new_with_label( _(" Check File "));
301 gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
305 label = gtk_label_new(_("File"));
306 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
307 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
309 file_entry = gtk_entry_new();
310 gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
312 file_btn = gtk_button_new_with_label( _(" ... "));
313 gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
317 frame_custom = gtk_frame_new(_("Additional e-Mail address item(s)"));
318 gtk_table_attach(GTK_TABLE(table), frame_custom, 1, 2, top, (top + JPILOT_NUM_CUSTOM_LABEL), GTK_FILL, 0, 0, 0);
320 // Now do custom labels.
321 vbox_custom = gtk_vbox_new (FALSE, 8);
322 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
323 hlbox = gtk_hbox_new( FALSE, 0 );
324 custom_check[i] = gtk_check_button_new();
325 custom_label[i] = gtk_label_new( "" );
326 gtk_box_pack_start( GTK_BOX(hlbox), custom_check[i], FALSE, FALSE, 0 );
327 gtk_box_pack_start( GTK_BOX(hlbox), custom_label[i], TRUE, TRUE, 0 );
328 gtk_box_pack_start( GTK_BOX(vbox_custom), hlbox, TRUE, TRUE, 0 );
329 gtk_misc_set_alignment(GTK_MISC(custom_label[i]), 0, 0.5);
332 gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
333 gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
336 hsbox = gtk_hbox_new(FALSE, 0);
337 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
338 statusbar = gtk_statusbar_new();
339 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
342 gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
343 &cancel_btn, _("Cancel"), NULL, NULL);
344 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
345 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
346 gtk_widget_grab_default(ok_btn);
348 hsep = gtk_hseparator_new();
349 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
351 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
352 GTK_SIGNAL_FUNC(edit_jpilot_ok), cancelled);
353 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
354 GTK_SIGNAL_FUNC(edit_jpilot_cancel), cancelled);
355 gtk_signal_connect(GTK_OBJECT(file_btn), "clicked",
356 GTK_SIGNAL_FUNC(edit_jpilot_file_select), NULL);
357 gtk_signal_connect(GTK_OBJECT(check_btn), "clicked",
358 GTK_SIGNAL_FUNC(edit_jpilot_file_check), NULL);
360 gtk_widget_show_all(vbox);
362 jpilotedit.window = window;
363 jpilotedit.name_entry = name_entry;
364 jpilotedit.file_entry = file_entry;
365 jpilotedit.ok_btn = ok_btn;
366 jpilotedit.cancel_btn = cancel_btn;
367 jpilotedit.statusbar = statusbar;
368 jpilotedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit JPilot Dialog" );
369 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
370 jpilotedit.custom_check[i] = custom_check[i];
371 jpilotedit.custom_label[i] = custom_label[i];
375 AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
376 static gboolean cancelled;
379 AddressDataSource *ds = NULL;
380 JPilotFile *jpf = NULL;
383 if( ! jpilotedit.window )
384 addressbook_edit_jpilot_create(&cancelled);
385 gtk_widget_grab_focus(jpilotedit.ok_btn);
386 gtk_widget_grab_focus(jpilotedit.name_entry);
387 gtk_widget_show(jpilotedit.window);
388 manage_window_set_transient(GTK_WINDOW(jpilotedit.window));
390 edit_jpilot_status_show( "" );
392 ds = ads->dataSource;
393 jpf = ds->rawDataSource;
395 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), jpf->name);
397 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), jpf->path);
398 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Edit JPilot Entry"));
399 edit_jpilot_fill_check_box( jpf );
402 gchar *guessFile = jpilot_find_pilotdb();
403 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), ADDRESSBOOK_GUESS_JPILOT );
404 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), guessFile );
405 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Add New JPilot Entry"));
406 edit_jpilot_fill_check_box_new();
407 if( *guessFile != '\0' ) {
408 edit_jpilot_file_check();
413 gtk_widget_hide(jpilotedit.window);
414 if (cancelled == TRUE) return NULL;
417 sName = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.name_entry), 0, -1 );
418 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
419 if( *sName == '\0' ) fin = TRUE;
420 if( *sFile == '\0' ) fin = TRUE;
424 jpf = jpilot_create();
425 ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_JPILOT, jpf );
426 ads = addressbook_create_ds_adapter( ds, ADDR_JPILOT, NULL );
428 addressbook_ads_set_name( ads, sName );
429 jpilot_set_name( jpf, sName );
430 jpilot_set_file( jpf, sFile );
431 edit_jpilot_read_check_box( jpf );
439 #endif /* USE_JPILOT */