2005-03-11 [paul] 1.0.3cvs2.1
[claws.git] / src / editjpilot.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2002 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 JPilot address book data.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #ifdef USE_JPILOT
29
30 #include "defs.h"
31
32 #include <glib.h>
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>
42
43 #include "addressbook.h"
44 #include "prefs_common.h"
45 #include "addressitem.h"
46 #include "jpilot.h"
47 #include "mgutils.h"
48 #include "gtkutils.h"
49 #include "manage_window.h"
50
51 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
52 #define JPILOT_NUM_CUSTOM_LABEL 4
53
54 static struct _JPilotEdit {
55         GtkWidget *window;
56         GtkWidget *name_entry;
57         GtkWidget *file_entry;
58         GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
59         GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
60         GtkWidget *ok_btn;
61         GtkWidget *cancel_btn;
62         GtkWidget *statusbar;
63         gint status_cid;
64 } jpilotedit;
65
66 static struct _AddressFileSelection jpilot_file_selector;
67
68 /*
69 * Edit functions.
70 */
71 void edit_jpilot_status_show( gchar *msg ) {
72         if( jpilotedit.statusbar != NULL ) {
73                 gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
74                 if( msg ) {
75                         gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
76                 }
77         }
78 }
79
80 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
81         *cancelled = TRUE;
82         gtk_main_quit();
83         return TRUE;
84 }
85
86 static gboolean edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
87         if (event && event->keyval == GDK_Escape) {
88                 *cancelled = TRUE;
89                 gtk_main_quit();
90         }
91         return FALSE;
92 }
93
94 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
95         *cancelled = FALSE;
96         gtk_main_quit();
97 }
98
99 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
100         *cancelled = TRUE;
101         gtk_main_quit();
102 }
103
104 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
105         gint i;
106         GList *node, *customLbl = NULL;
107         gchar *labelName;
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] ), "" );
112         }
113
114         done = FALSE;
115         i = 0;
116         customLbl = jpilot_load_custom_label( jpf, customLbl );
117         node = customLbl;
118         while( ! done ) {
119                 if( node ) {
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 );
124                         i++;
125                         if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
126                         node = g_list_next( node );
127                 }
128                 else {
129                         done = TRUE;
130                 }
131         }
132         mgu_free_dlist( customLbl );
133         customLbl = NULL;
134 }
135
136 static void edit_jpilot_fill_check_box_new() {
137         gint i;
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] ), "" );
141         }
142 }
143
144 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
145         gint i;
146         gchar *labelName;
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 );
152                 }
153         }
154 }
155
156 static void edit_jpilot_file_check( void ) {
157         gint t = -1;
158         gchar *sFile;
159         gchar *sMsg;
160         gboolean flg;
161
162         flg = FALSE;
163         sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
164         if( sFile ) {
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 );
173                                 flg = TRUE;
174                         }
175                         jpilot_free( jpf );
176                         jpf = NULL;
177                 }
178         }
179         if( ! flg ) {
180                 /* Clear all check boxes */
181                 edit_jpilot_fill_check_box_new();
182         }
183         g_free( sFile );
184
185         /* Display appropriate message */
186         if( t == MGU_SUCCESS ) {
187                 sMsg = "";
188         }
189         else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
190                 sMsg = _("File does not appear to be JPilot format.");
191         }
192         else {
193                 sMsg = _("Could not read file.");
194         }
195         edit_jpilot_status_show( sMsg );
196 }
197
198 static void edit_jpilot_file_ok( GtkWidget *widget, gpointer data ) {
199         const gchar *sFile;
200         AddressFileSelection *afs;
201         GtkWidget *fileSel;
202
203         afs = ( AddressFileSelection * ) data;
204         fileSel = afs->fileSelector;
205         sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
206
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 );
213 }
214
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 );
221 }
222
223 static void edit_jpilot_file_select_create( AddressFileSelection *afs ) {
224         GtkWidget *fileSelector;
225
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;
234 }
235
236 static void edit_jpilot_file_select( void ) {
237         gchar *sFile;
238
239         if (! jpilot_file_selector.fileSelector )
240                 edit_jpilot_file_select_create( & jpilot_file_selector );
241
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 );
244         g_free( sFile );
245         gtk_widget_show( jpilot_file_selector.fileSelector );
246         gtk_grab_add( jpilot_file_selector.fileSelector );
247 }
248
249 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
250         GtkWidget *window;
251         GtkWidget *vbox;
252         GtkWidget *table;
253         GtkWidget *label;
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];
260         GtkWidget *hlbox;
261         GtkWidget *hbbox;
262         GtkWidget *hsep;
263         GtkWidget *ok_btn;
264         GtkWidget *cancel_btn;
265         GtkWidget *check_btn;
266         GtkWidget *file_btn;
267         GtkWidget *hsbox;
268         GtkWidget *statusbar;
269         gint top, i;
270
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),
279                          cancelled);
280         g_signal_connect(G_OBJECT(window), "key_press_event",
281                          G_CALLBACK(edit_jpilot_key_pressed),
282                          cancelled);
283
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 );
287
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);
293
294         /* First row */
295         top = 0;
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);
299
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);
302
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);
305
306         /* Second row */
307         top = 1;
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);
311
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);
314
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);
317
318         /* Third row */
319         top = 2;
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);
322
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);
333                 top++;
334         }
335         gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
336         gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
337
338         /* Status line */
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);
343
344         /* Button panel */
345         gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
346                                       &cancel_btn, GTK_STOCK_CANCEL,
347                                       NULL, NULL);
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);
351
352         hsep = gtk_hseparator_new();
353         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
354
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);
363
364         gtk_widget_show_all(vbox);
365
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];
376         }
377 }
378
379 AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
380         static gboolean cancelled;
381         gchar *sName;
382         gchar *sFile;
383         AddressDataSource *ds = NULL;
384         JPilotFile *jpf = NULL;
385         gboolean fin;
386
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));
393
394         edit_jpilot_status_show( "" );
395         if( ads ) {
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 ) );
401                 if (jpf->path)
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 );
405         }
406         else {
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();
414                 }
415         }
416
417         gtk_main();
418         gtk_widget_hide(jpilotedit.window);
419         if (cancelled == TRUE) return NULL;
420
421         fin = FALSE;
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;
426
427         if( ! fin ) {
428                 if( ! ads ) {
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 );
432                 }
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 );
437         }
438         g_free( sName );
439         g_free( sFile );
440
441         return ads;
442 }
443
444 #endif /* USE_JPILOT */
445
446 /*
447 * End of Source.
448 */
449