sync with sylpheed 0.5.0pre4 release
[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
48 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
49 #define JPILOT_NUM_CUSTOM_LABEL 4
50
51 static struct _JPilotEdit {
52         GtkWidget *window;
53         GtkWidget *name_entry;
54         GtkWidget *file_entry;
55         GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
56         GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
57         GtkWidget *ok_btn;
58         GtkWidget *cancel_btn;
59         GtkWidget *statusbar;
60         gint status_cid;
61 } jpilotedit;
62
63 static struct _AddressFileSelection jpilot_file_selector;
64
65 /*
66 * Edit functions.
67 */
68 void edit_jpilot_status_show( gchar *msg ) {
69         if( jpilotedit.statusbar != NULL ) {
70                 gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
71                 if( msg ) {
72                         gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
73                 }
74         }
75 }
76
77 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
78         *cancelled = TRUE;
79         gtk_main_quit();
80         return TRUE;
81 }
82
83 static void edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
84         if (event && event->keyval == GDK_Escape) {
85                 *cancelled = TRUE;
86                 gtk_main_quit();
87         }
88 }
89
90 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
91         *cancelled = FALSE;
92         gtk_main_quit();
93 }
94
95 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
96         *cancelled = TRUE;
97         gtk_main_quit();
98 }
99
100 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
101         gint i;
102         GList *node, *customLbl = NULL;
103         gchar *labelName;
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] ), "" );
108         }
109
110         done = FALSE;
111         i = 0;
112         customLbl = jpilot_load_custom_label( jpf, customLbl );
113         node = customLbl;
114         while( ! done ) {
115                 if( node ) {
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 );
120                         i++;
121                         if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
122                         node = g_list_next( node );
123                 }
124                 else {
125                         done = TRUE;
126                 }
127         }
128         mgu_free_dlist( customLbl );
129         customLbl = NULL;
130 }
131
132 static void edit_jpilot_fill_check_box_new() {
133         gint i;
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] ), "" );
137         }
138 }
139
140 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
141         gint i;
142         gchar *labelName;
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 );
148                 }
149         }
150 }
151
152 static void edit_jpilot_file_check( void ) {
153         gint t;
154         gchar *sFile;
155         gchar *sMsg;
156         gboolean flg;
157
158         flg = FALSE;
159         sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
160         if( sFile ) {
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 ) {
167                                 // Set check boxes
168                                 edit_jpilot_fill_check_box( jpf );
169                                 flg = TRUE;
170                         }
171                         jpilot_free( jpf );
172                 }
173         }
174         if( ! flg ) {
175                 // Clear all check boxes
176                 edit_jpilot_fill_check_box_new();
177         }
178         g_free( sFile );
179
180         // Display appropriate message
181         if( t == MGU_SUCCESS ) {
182                 sMsg = "";
183         }
184         else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
185                 sMsg = _("File does not appear to be JPilot format.");
186         }
187         else {
188                 sMsg = _("Could not read file.");
189         }
190         edit_jpilot_status_show( sMsg );
191 }
192
193 static void edit_jpilot_file_ok( GtkWidget *widget, gpointer data ) {
194         gchar *sFile;
195         AddressFileSelection *afs;
196         GtkWidget *fileSel;
197
198         afs = ( AddressFileSelection * ) data;
199         fileSel = afs->fileSelector;
200         sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
201
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 );
208 }
209
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 );
216 }
217
218 static void edit_jpilot_file_select_create( AddressFileSelection *afs ) {
219         GtkWidget *fileSelector;
220
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;
229 }
230
231 static void edit_jpilot_file_select( void ) {
232         gchar *sFile;
233
234         if (! jpilot_file_selector.fileSelector )
235                 edit_jpilot_file_select_create( & jpilot_file_selector );
236
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 );
239         g_free( sFile );
240         gtk_widget_show( jpilot_file_selector.fileSelector );
241         gtk_grab_add( jpilot_file_selector.fileSelector );
242 }
243
244 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
245         GtkWidget *window;
246         GtkWidget *vbox;
247         GtkWidget *table;
248         GtkWidget *label;
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];
255         GtkWidget *hlbox;
256         GtkWidget *hbbox;
257         GtkWidget *hsep;
258         GtkWidget *ok_btn;
259         GtkWidget *cancel_btn;
260         GtkWidget *check_btn;
261         GtkWidget *file_btn;
262         GtkWidget *hsbox;
263         GtkWidget *statusbar;
264         gint top, i;
265
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),
274                            cancelled);
275         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
276                            GTK_SIGNAL_FUNC(edit_jpilot_key_pressed),
277                            cancelled);
278
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 );
282
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);
288
289         // First row
290         top = 0;
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);
294
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);
297
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);
300
301         // Second row
302         top = 1;
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);
306
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);
309
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);
312
313         // Third row
314         top = 2;
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);
317
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);
328                 top++;
329         }
330         gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
331         gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
332
333         // Status line
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);
338
339         // Button panel
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);
345
346         hsep = gtk_hseparator_new();
347         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
348
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);
357
358         gtk_widget_show_all(vbox);
359
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];
370         }
371 }
372
373 AddressJPilot *addressbook_edit_jpilot( AddressJPilot *jpilot ) {
374         static gboolean cancelled;
375         gchar *sName;
376         gchar *sFile;
377         JPilotFile *jpf;
378         gboolean fin;
379
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));
386
387         edit_jpilot_status_show( "" );
388         if( jpilot ) {
389                 jpf = jpilot->pilotFile;
390                 if (jpf->name)
391                         gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), jpf->name);
392                 if (jpf->path)
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 );
396         }
397         else {
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();
406                 }
407         }
408
409         gtk_main();
410         gtk_widget_hide(jpilotedit.window);
411         if (cancelled == TRUE) return NULL;
412
413         fin = FALSE;
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;
418
419         if( ! fin ) {
420                 if( ! jpilot ) {
421                         jpilot = g_new0(AddressJPilot, 1);
422                         ADDRESS_OBJECT_TYPE(jpilot) = ADDR_JPILOT;
423                         jpf = jpilot_create();
424                         jpilot->pilotFile = jpf;
425                 }
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 );
431         }
432         g_free( sName );
433         g_free( sFile );
434
435         return jpilot;
436 }
437
438 #endif /* USE_JPILOT */
439
440 /*
441 * End of Source.
442 */
443