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"
48 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
49 #define JPILOT_NUM_CUSTOM_LABEL 4
51 static struct _JPilotEdit {
53 GtkWidget *name_entry;
54 GtkWidget *file_entry;
55 GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
56 GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
58 GtkWidget *cancel_btn;
63 static struct _AddressFileSelection jpilot_file_selector;
68 void edit_jpilot_status_show( gchar *msg ) {
69 if( jpilotedit.statusbar != NULL ) {
70 gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
72 gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
77 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
83 static void edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
84 if (event && event->keyval == GDK_Escape) {
90 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
95 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
100 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
102 GList *node, *customLbl = NULL;
104 gboolean done, checked;
105 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
106 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
107 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
112 customLbl = jpilot_load_custom_label( jpf, customLbl );
116 labelName = node->data;
117 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), labelName );
118 checked = jpilot_test_custom_label( jpf, labelName );
119 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), checked );
121 if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
122 node = g_list_next( node );
128 mgu_free_dlist( customLbl );
132 static void edit_jpilot_fill_check_box_new() {
134 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
135 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
136 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
140 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
143 jpilot_clear_custom_labels( pilotFile );
144 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
145 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(jpilotedit.custom_check[i]) ) ) {
146 labelName = g_strdup( GTK_LABEL(jpilotedit.custom_label[i])->label );
147 jpilot_add_custom_label( pilotFile, labelName );
152 static void edit_jpilot_file_check( void ) {
159 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
161 g_strchomp( sFile ); g_strchug( sFile );
162 if( *sFile != '\0' ) {
163 // Attempt to read file
164 JPilotFile *jpf = jpilot_create_path( sFile );
165 t = jpilot_read_data( jpf );
166 if( t == MGU_SUCCESS ) {
168 edit_jpilot_fill_check_box( jpf );
175 // Clear all check boxes
176 edit_jpilot_fill_check_box_new();
180 // Display appropriate message
181 if( t == MGU_SUCCESS ) {
184 else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
185 sMsg = _("File does not appear to be JPilot format.");
188 sMsg = _("Could not read file.");
190 edit_jpilot_status_show( sMsg );
193 static void edit_jpilot_file_ok( GtkWidget *widget, gpointer data ) {
195 AddressFileSelection *afs;
198 afs = ( AddressFileSelection * ) data;
199 fileSel = afs->fileSelector;
200 sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
202 afs->cancelled = FALSE;
203 gtk_entry_set_text( GTK_ENTRY(jpilotedit.file_entry), sFile );
204 gtk_widget_hide( afs->fileSelector );
205 gtk_grab_remove( afs->fileSelector );
206 edit_jpilot_file_check();
207 gtk_widget_grab_focus( jpilotedit.file_entry );
210 static void edit_jpilot_file_cancel( GtkWidget *widget, gpointer data ) {
211 AddressFileSelection *afs = ( AddressFileSelection * ) data;
212 afs->cancelled = TRUE;
213 gtk_widget_hide( afs->fileSelector );
214 gtk_grab_remove( afs->fileSelector );
215 gtk_widget_grab_focus( jpilotedit.file_entry );
218 static void edit_jpilot_file_select_create( AddressFileSelection *afs ) {
219 GtkWidget *fileSelector;
221 fileSelector = gtk_file_selection_new( _("Select JPilot File") );
222 gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
223 gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
224 "clicked", GTK_SIGNAL_FUNC (edit_jpilot_file_ok), ( gpointer ) afs );
225 gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
226 "clicked", GTK_SIGNAL_FUNC (edit_jpilot_file_cancel), ( gpointer ) afs );
227 afs->fileSelector = fileSelector;
228 afs->cancelled = TRUE;
231 static void edit_jpilot_file_select( void ) {
234 if (! jpilot_file_selector.fileSelector )
235 edit_jpilot_file_select_create( & jpilot_file_selector );
237 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
238 gtk_file_selection_set_filename( GTK_FILE_SELECTION( jpilot_file_selector.fileSelector ), sFile );
240 gtk_widget_show( jpilot_file_selector.fileSelector );
241 gtk_grab_add( jpilot_file_selector.fileSelector );
244 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
249 GtkWidget *name_entry;
250 GtkWidget *file_entry;
251 GtkWidget *vbox_custom;
252 GtkWidget *frame_custom;
253 GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
254 GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
259 GtkWidget *cancel_btn;
260 GtkWidget *check_btn;
263 GtkWidget *statusbar;
266 window = gtk_window_new(GTK_WINDOW_DIALOG);
267 gtk_widget_set_usize(window, 450, -1);
268 gtk_container_set_border_width(GTK_CONTAINER(window), 0);
269 gtk_window_set_title(GTK_WINDOW(window), _("Edit JPilot Entry"));
270 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
271 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
272 gtk_signal_connect(GTK_OBJECT(window), "delete_event",
273 GTK_SIGNAL_FUNC(edit_jpilot_delete_event),
275 gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
276 GTK_SIGNAL_FUNC(edit_jpilot_key_pressed),
279 vbox = gtk_vbox_new(FALSE, 8);
280 gtk_container_add(GTK_CONTAINER(window), vbox);
281 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
283 table = gtk_table_new(2 + JPILOT_NUM_CUSTOM_LABEL, 3, FALSE);
284 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
285 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
286 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
287 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
291 label = gtk_label_new(_("Name"));
292 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
293 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
295 name_entry = gtk_entry_new();
296 gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
298 check_btn = gtk_button_new_with_label( _(" Check File "));
299 gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
303 label = gtk_label_new(_("File"));
304 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
305 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
307 file_entry = gtk_entry_new();
308 gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
310 file_btn = gtk_button_new_with_label( _(" ... "));
311 gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
315 frame_custom = gtk_frame_new(_("Additional e-Mail address item(s)"));
316 gtk_table_attach(GTK_TABLE(table), frame_custom, 1, 2, top, (top + JPILOT_NUM_CUSTOM_LABEL), GTK_FILL, 0, 0, 0);
318 // Now do custom labels.
319 vbox_custom = gtk_vbox_new (FALSE, 8);
320 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
321 hlbox = gtk_hbox_new( FALSE, 0 );
322 custom_check[i] = gtk_check_button_new();
323 custom_label[i] = gtk_label_new( "" );
324 gtk_box_pack_start( GTK_BOX(hlbox), custom_check[i], FALSE, FALSE, 0 );
325 gtk_box_pack_start( GTK_BOX(hlbox), custom_label[i], TRUE, TRUE, 0 );
326 gtk_box_pack_start( GTK_BOX(vbox_custom), hlbox, TRUE, TRUE, 0 );
327 gtk_misc_set_alignment(GTK_MISC(custom_label[i]), 0, 0.5);
330 gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
331 gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
334 hsbox = gtk_hbox_new(FALSE, 0);
335 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
336 statusbar = gtk_statusbar_new();
337 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
340 gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
341 &cancel_btn, _("Cancel"), NULL, NULL);
342 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
343 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
344 gtk_widget_grab_default(ok_btn);
346 hsep = gtk_hseparator_new();
347 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
349 gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
350 GTK_SIGNAL_FUNC(edit_jpilot_ok), cancelled);
351 gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
352 GTK_SIGNAL_FUNC(edit_jpilot_cancel), cancelled);
353 gtk_signal_connect(GTK_OBJECT(file_btn), "clicked",
354 GTK_SIGNAL_FUNC(edit_jpilot_file_select), NULL);
355 gtk_signal_connect(GTK_OBJECT(check_btn), "clicked",
356 GTK_SIGNAL_FUNC(edit_jpilot_file_check), NULL);
358 gtk_widget_show_all(vbox);
360 jpilotedit.window = window;
361 jpilotedit.name_entry = name_entry;
362 jpilotedit.file_entry = file_entry;
363 jpilotedit.ok_btn = ok_btn;
364 jpilotedit.cancel_btn = cancel_btn;
365 jpilotedit.statusbar = statusbar;
366 jpilotedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit JPilot Dialog" );
367 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
368 jpilotedit.custom_check[i] = custom_check[i];
369 jpilotedit.custom_label[i] = custom_label[i];
373 AddressJPilot *addressbook_edit_jpilot( AddressJPilot *jpilot ) {
374 static gboolean cancelled;
380 if (!jpilotedit.window)
381 addressbook_edit_jpilot_create(&cancelled);
382 gtk_widget_grab_focus(jpilotedit.ok_btn);
383 gtk_widget_grab_focus(jpilotedit.name_entry);
384 gtk_widget_show(jpilotedit.window);
385 manage_window_set_transient(GTK_WINDOW(jpilotedit.window));
387 edit_jpilot_status_show( "" );
389 jpf = jpilot->pilotFile;
391 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), jpf->name);
393 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), jpf->path);
394 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Edit JPilot Entry"));
395 edit_jpilot_fill_check_box( jpf );
398 gchar *guessFile = jpilot_find_pilotdb();
399 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), ADDRESSBOOK_GUESS_JPILOT );
400 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), guessFile );
401 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Add New JPilot Entry"));
402 edit_jpilot_fill_check_box_new();
403 // Attempt to load labels
404 if( *guessFile != '\0' ) {
405 edit_jpilot_file_check();
410 gtk_widget_hide(jpilotedit.window);
411 if (cancelled == TRUE) return NULL;
414 sName = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.name_entry), 0, -1 );
415 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
416 if( *sName == '\0' ) fin = TRUE;
417 if( *sFile == '\0' ) fin = TRUE;
421 jpilot = g_new0(AddressJPilot, 1);
422 ADDRESS_OBJECT_TYPE(jpilot) = ADDR_JPILOT;
423 jpf = jpilot_create();
424 jpilot->pilotFile = jpf;
426 g_free( jpilot->name );
427 jpilot->name = g_strdup( sName );
428 jpilot_set_name( jpf, sName );
429 jpilot_set_file( jpf, sFile );
430 edit_jpilot_read_check_box( jpf );
438 #endif /* USE_JPILOT */