sync with sylpheed 0.6.0cvs4
[claws.git] / src / editjpilot.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001 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 <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>
41
42 #include "intl.h"
43 #include "addressbook.h"
44 #include "prefs_common.h"
45 #include "addressitem.h"
46 #include "jpilot.h"
47 #include "mgutils.h"
48
49 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
50 #define JPILOT_NUM_CUSTOM_LABEL 4
51
52 static struct _JPilotEdit {
53         GtkWidget *window;
54         GtkWidget *name_entry;
55         GtkWidget *file_entry;
56         GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
57         GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
58         GtkWidget *ok_btn;
59         GtkWidget *cancel_btn;
60         GtkWidget *statusbar;
61         gint status_cid;
62 } jpilotedit;
63
64 static struct _AddressFileSelection jpilot_file_selector;
65
66 /*
67 * Edit functions.
68 */
69 void edit_jpilot_status_show( gchar *msg ) {
70         if( jpilotedit.statusbar != NULL ) {
71                 gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
72                 if( msg ) {
73                         gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
74                 }
75         }
76 }
77
78 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
79         *cancelled = TRUE;
80         gtk_main_quit();
81         return TRUE;
82 }
83
84 static void edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
85         if (event && event->keyval == GDK_Escape) {
86                 *cancelled = TRUE;
87                 gtk_main_quit();
88         }
89 }
90
91 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
92         *cancelled = FALSE;
93         gtk_main_quit();
94 }
95
96 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
97         *cancelled = TRUE;
98         gtk_main_quit();
99 }
100
101 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
102         gint i;
103         GList *node, *customLbl = NULL;
104         gchar *labelName;
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] ), "" );
109         }
110
111         done = FALSE;
112         i = 0;
113         customLbl = jpilot_load_custom_label( jpf, customLbl );
114         node = customLbl;
115         while( ! done ) {
116                 if( node ) {
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 );
121                         i++;
122                         if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
123                         node = g_list_next( node );
124                 }
125                 else {
126                         done = TRUE;
127                 }
128         }
129         mgu_free_dlist( customLbl );
130         customLbl = NULL;
131 }
132
133 static void edit_jpilot_fill_check_box_new() {
134         gint i;
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] ), "" );
138         }
139 }
140
141 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
142         gint i;
143         gchar *labelName;
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 );
149                 }
150         }
151 }
152
153 static void edit_jpilot_file_check( void ) {
154         gint t;
155         gchar *sFile;
156         gchar *sMsg;
157         gboolean flg;
158
159         flg = FALSE;
160         sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
161         if( sFile ) {
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 ) {
168                                 // Set check boxes
169                                 edit_jpilot_fill_check_box( jpf );
170                                 flg = TRUE;
171                         }
172                         jpilot_free( jpf );
173                         jpf = NULL;
174                 }
175         }
176         if( ! flg ) {
177                 // Clear all check boxes
178                 edit_jpilot_fill_check_box_new();
179         }
180         g_free( sFile );
181
182         // Display appropriate message
183         if( t == MGU_SUCCESS ) {
184                 sMsg = "";
185         }
186         else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
187                 sMsg = _("File does not appear to be JPilot format.");
188         }
189         else {
190                 sMsg = _("Could not read file.");
191         }
192         edit_jpilot_status_show( sMsg );
193 }
194
195 static void edit_jpilot_file_ok( GtkWidget *widget, gpointer data ) {
196         gchar *sFile;
197         AddressFileSelection *afs;
198         GtkWidget *fileSel;
199
200         afs = ( AddressFileSelection * ) data;
201         fileSel = afs->fileSelector;
202         sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
203
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 );
210 }
211
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 );
218 }
219
220 static void edit_jpilot_file_select_create( AddressFileSelection *afs ) {
221         GtkWidget *fileSelector;
222
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;
231 }
232
233 static void edit_jpilot_file_select( void ) {
234         gchar *sFile;
235
236         if (! jpilot_file_selector.fileSelector )
237                 edit_jpilot_file_select_create( & jpilot_file_selector );
238
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 );
241         g_free( sFile );
242         gtk_widget_show( jpilot_file_selector.fileSelector );
243         gtk_grab_add( jpilot_file_selector.fileSelector );
244 }
245
246 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
247         GtkWidget *window;
248         GtkWidget *vbox;
249         GtkWidget *table;
250         GtkWidget *label;
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];
257         GtkWidget *hlbox;
258         GtkWidget *hbbox;
259         GtkWidget *hsep;
260         GtkWidget *ok_btn;
261         GtkWidget *cancel_btn;
262         GtkWidget *check_btn;
263         GtkWidget *file_btn;
264         GtkWidget *hsbox;
265         GtkWidget *statusbar;
266         gint top, i;
267
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),
276                            cancelled);
277         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
278                            GTK_SIGNAL_FUNC(edit_jpilot_key_pressed),
279                            cancelled);
280
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 );
284
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);
290
291         // First row
292         top = 0;
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);
296
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);
299
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);
302
303         // Second row
304         top = 1;
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);
308
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);
311
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);
314
315         // Third row
316         top = 2;
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);
319
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);
330                 top++;
331         }
332         gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
333         gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
334
335         // Status line
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);
340
341         // Button panel
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);
347
348         hsep = gtk_hseparator_new();
349         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
350
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);
359
360         gtk_widget_show_all(vbox);
361
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];
372         }
373 }
374
375 AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
376         static gboolean cancelled;
377         gchar *sName;
378         gchar *sFile;
379         AddressDataSource *ds = NULL;
380         JPilotFile *jpf = NULL;
381         gboolean fin;
382
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));
389
390         edit_jpilot_status_show( "" );
391         if( ads ) {
392                 ds = ads->dataSource;
393                 jpf = ds->rawDataSource;
394                 if (jpf->name)
395                         gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), jpf->name);
396                 if (jpf->path)
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 );
400         }
401         else {
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();
409                 }
410         }
411
412         gtk_main();
413         gtk_widget_hide(jpilotedit.window);
414         if (cancelled == TRUE) return NULL;
415
416         fin = FALSE;
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;
421
422         if( ! fin ) {
423                 if( ! ads ) {
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 );
427                 }
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 );
432         }
433         g_free( sName );
434         g_free( sFile );
435
436         return ads;
437 }
438
439 #endif /* USE_JPILOT */
440
441 /*
442 * End of Source.
443 */
444