2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2002 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 <glib/gi18n.h>
34 #include <gdk/gdkkeysyms.h>
35 #include <gtk/gtkwindow.h>
36 #include <gtk/gtksignal.h>
37 #include <gtk/gtkhbox.h>
38 #include <gtk/gtklabel.h>
39 #include <gtk/gtkentry.h>
40 #include <gtk/gtkhbbox.h>
41 #include <gtk/gtkbutton.h>
43 #include "addressbook.h"
44 #include "prefs_common.h"
45 #include "addressitem.h"
49 #include "manage_window.h"
51 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
52 #define JPILOT_NUM_CUSTOM_LABEL 4
54 static struct _JPilotEdit {
56 GtkWidget *name_entry;
57 GtkWidget *file_entry;
58 GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
59 GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
61 GtkWidget *cancel_btn;
66 static struct _AddressFileSelection jpilot_file_selector;
71 void edit_jpilot_status_show( gchar *msg ) {
72 if( jpilotedit.statusbar != NULL ) {
73 gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
75 gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
80 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
86 static gboolean edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
87 if (event && event->keyval == GDK_Escape) {
94 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
99 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
104 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
106 GList *node, *customLbl = NULL;
108 gboolean done, checked;
109 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
110 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
111 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
116 customLbl = jpilot_load_custom_label( jpf, customLbl );
120 labelName = node->data;
121 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), labelName );
122 checked = jpilot_test_custom_label( jpf, labelName );
123 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), checked );
125 if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
126 node = g_list_next( node );
132 mgu_free_dlist( customLbl );
136 static void edit_jpilot_fill_check_box_new() {
138 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
139 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
140 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
144 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
147 jpilot_clear_custom_labels( pilotFile );
148 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
149 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(jpilotedit.custom_check[i]) ) ) {
150 labelName = GTK_LABEL(jpilotedit.custom_label[i])->label;
151 jpilot_add_custom_label( pilotFile, labelName );
156 static void edit_jpilot_file_check( void ) {
163 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
165 g_strchomp( sFile ); g_strchug( sFile );
166 if( *sFile != '\0' ) {
167 /* Attempt to read file */
168 JPilotFile *jpf = jpilot_create_path( sFile );
169 t = jpilot_read_data( jpf );
170 if( t == MGU_SUCCESS ) {
171 /* Set check boxes */
172 edit_jpilot_fill_check_box( jpf );
180 /* Clear all check boxes */
181 edit_jpilot_fill_check_box_new();
185 /* Display appropriate message */
186 if( t == MGU_SUCCESS ) {
189 else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
190 sMsg = _("File does not appear to be JPilot format.");
193 sMsg = _("Could not read file.");
195 edit_jpilot_status_show( sMsg );
198 static void edit_jpilot_file_ok( GtkWidget *widget, gpointer data ) {
200 AddressFileSelection *afs;
203 afs = ( AddressFileSelection * ) data;
204 fileSel = afs->fileSelector;
205 sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
207 afs->cancelled = FALSE;
208 gtk_entry_set_text( GTK_ENTRY(jpilotedit.file_entry), sFile );
209 gtk_widget_hide( afs->fileSelector );
210 gtk_grab_remove( afs->fileSelector );
211 edit_jpilot_file_check();
212 gtk_widget_grab_focus( jpilotedit.file_entry );
215 static void edit_jpilot_file_cancel( GtkWidget *widget, gpointer data ) {
216 AddressFileSelection *afs = ( AddressFileSelection * ) data;
217 afs->cancelled = TRUE;
218 gtk_widget_hide( afs->fileSelector );
219 gtk_grab_remove( afs->fileSelector );
220 gtk_widget_grab_focus( jpilotedit.file_entry );
223 static void edit_jpilot_file_select_create( AddressFileSelection *afs ) {
224 GtkWidget *fileSelector;
226 fileSelector = gtk_file_selection_new( _("Select JPilot File") );
227 gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
228 g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
229 "clicked", G_CALLBACK (edit_jpilot_file_ok), ( gpointer ) afs );
230 g_signal_connect( G_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
231 "clicked", G_CALLBACK (edit_jpilot_file_cancel), ( gpointer ) afs );
232 afs->fileSelector = fileSelector;
233 afs->cancelled = TRUE;
236 static void edit_jpilot_file_select( void ) {
239 if (! jpilot_file_selector.fileSelector )
240 edit_jpilot_file_select_create( & jpilot_file_selector );
242 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
243 gtk_file_selection_set_filename( GTK_FILE_SELECTION( jpilot_file_selector.fileSelector ), sFile );
245 gtk_widget_show( jpilot_file_selector.fileSelector );
246 gtk_grab_add( jpilot_file_selector.fileSelector );
249 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
254 GtkWidget *name_entry;
255 GtkWidget *file_entry;
256 GtkWidget *vbox_custom;
257 GtkWidget *frame_custom;
258 GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
259 GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
264 GtkWidget *cancel_btn;
265 GtkWidget *check_btn;
268 GtkWidget *statusbar;
271 window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
272 gtk_widget_set_size_request(window, 450, -1);
273 gtk_container_set_border_width(GTK_CONTAINER(window), 0);
274 gtk_window_set_title(GTK_WINDOW(window), _("Edit JPilot Entry"));
275 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
276 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
277 g_signal_connect(G_OBJECT(window), "delete_event",
278 G_CALLBACK(edit_jpilot_delete_event),
280 g_signal_connect(G_OBJECT(window), "key_press_event",
281 G_CALLBACK(edit_jpilot_key_pressed),
284 vbox = gtk_vbox_new(FALSE, 8);
285 gtk_container_add(GTK_CONTAINER(window), vbox);
286 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
288 table = gtk_table_new(2 + JPILOT_NUM_CUSTOM_LABEL, 3, FALSE);
289 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
290 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
291 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
292 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
296 label = gtk_label_new(_("Name"));
297 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
298 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
300 name_entry = gtk_entry_new();
301 gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
303 check_btn = gtk_button_new_with_label( _(" Check File "));
304 gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
308 label = gtk_label_new(_("File"));
309 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
310 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
312 file_entry = gtk_entry_new();
313 gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
315 file_btn = gtk_button_new_with_label( _(" ... "));
316 gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
320 frame_custom = gtk_frame_new(_("Additional e-Mail address item(s)"));
321 gtk_table_attach(GTK_TABLE(table), frame_custom, 1, 2, top, (top + JPILOT_NUM_CUSTOM_LABEL), GTK_FILL, 0, 0, 0);
323 /* Now do custom labels. */
324 vbox_custom = gtk_vbox_new (FALSE, 8);
325 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
326 hlbox = gtk_hbox_new( FALSE, 0 );
327 custom_check[i] = gtk_check_button_new();
328 custom_label[i] = gtk_label_new( "" );
329 gtk_box_pack_start( GTK_BOX(hlbox), custom_check[i], FALSE, FALSE, 0 );
330 gtk_box_pack_start( GTK_BOX(hlbox), custom_label[i], TRUE, TRUE, 0 );
331 gtk_box_pack_start( GTK_BOX(vbox_custom), hlbox, TRUE, TRUE, 0 );
332 gtk_misc_set_alignment(GTK_MISC(custom_label[i]), 0, 0.5);
335 gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
336 gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
339 hsbox = gtk_hbox_new(FALSE, 0);
340 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
341 statusbar = gtk_statusbar_new();
342 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
345 gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
346 &cancel_btn, GTK_STOCK_CANCEL,
348 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
349 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
350 gtk_widget_grab_default(ok_btn);
352 hsep = gtk_hseparator_new();
353 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
355 g_signal_connect(G_OBJECT(ok_btn), "clicked",
356 G_CALLBACK(edit_jpilot_ok), cancelled);
357 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
358 G_CALLBACK(edit_jpilot_cancel), cancelled);
359 g_signal_connect(G_OBJECT(file_btn), "clicked",
360 G_CALLBACK(edit_jpilot_file_select), NULL);
361 g_signal_connect(G_OBJECT(check_btn), "clicked",
362 G_CALLBACK(edit_jpilot_file_check), NULL);
364 gtk_widget_show_all(vbox);
366 jpilotedit.window = window;
367 jpilotedit.name_entry = name_entry;
368 jpilotedit.file_entry = file_entry;
369 jpilotedit.ok_btn = ok_btn;
370 jpilotedit.cancel_btn = cancel_btn;
371 jpilotedit.statusbar = statusbar;
372 jpilotedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit JPilot Dialog" );
373 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
374 jpilotedit.custom_check[i] = custom_check[i];
375 jpilotedit.custom_label[i] = custom_label[i];
379 AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
380 static gboolean cancelled;
383 AddressDataSource *ds = NULL;
384 JPilotFile *jpf = NULL;
387 if( ! jpilotedit.window )
388 addressbook_edit_jpilot_create(&cancelled);
389 gtk_widget_grab_focus(jpilotedit.ok_btn);
390 gtk_widget_grab_focus(jpilotedit.name_entry);
391 gtk_widget_show(jpilotedit.window);
392 manage_window_set_transient(GTK_WINDOW(jpilotedit.window));
394 edit_jpilot_status_show( "" );
396 ds = ads->dataSource;
397 jpf = ds->rawDataSource;
398 if ( jpilot_get_name( jpf ) )
399 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry),
400 jpilot_get_name( jpf ) );
402 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), jpf->path);
403 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Edit JPilot Entry"));
404 edit_jpilot_fill_check_box( jpf );
407 gchar *guessFile = jpilot_find_pilotdb();
408 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), ADDRESSBOOK_GUESS_JPILOT );
409 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), guessFile );
410 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Add New JPilot Entry"));
411 edit_jpilot_fill_check_box_new();
412 if( *guessFile != '\0' ) {
413 edit_jpilot_file_check();
418 gtk_widget_hide(jpilotedit.window);
419 if (cancelled == TRUE) return NULL;
422 sName = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.name_entry), 0, -1 );
423 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
424 if( *sName == '\0' ) fin = TRUE;
425 if( *sFile == '\0' ) fin = TRUE;
429 jpf = jpilot_create();
430 ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_JPILOT, jpf );
431 ads = addressbook_create_ds_adapter( ds, ADDR_JPILOT, NULL );
433 addressbook_ads_set_name( ads, sName );
434 jpilot_set_name( jpf, sName );
435 jpilot_set_file( jpf, sFile );
436 edit_jpilot_read_check_box( jpf );
444 #endif /* USE_JPILOT */