cb72b0f49edc3ee60e6c9711c8dd2902b0becc86
[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 #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 void 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 }
92
93 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
94         *cancelled = FALSE;
95         gtk_main_quit();
96 }
97
98 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
99         *cancelled = TRUE;
100         gtk_main_quit();
101 }
102
103 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
104         gint i;
105         GList *node, *customLbl = NULL;
106         gchar *labelName;
107         gboolean done, checked;
108         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
109                 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
110                 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
111         }
112
113         done = FALSE;
114         i = 0;
115         customLbl = jpilot_load_custom_label( jpf, customLbl );
116         node = customLbl;
117         while( ! done ) {
118                 if( node ) {
119                         labelName = node->data;
120                         gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), labelName );
121                         checked = jpilot_test_custom_label( jpf, labelName );
122                         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), checked );
123                         i++;
124                         if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
125                         node = g_list_next( node );
126                 }
127                 else {
128                         done = TRUE;
129                 }
130         }
131         mgu_free_dlist( customLbl );
132         customLbl = NULL;
133 }
134
135 static void edit_jpilot_fill_check_box_new() {
136         gint i;
137         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
138                 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
139                 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
140         }
141 }
142
143 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
144         gint i;
145         gchar *labelName;
146         jpilot_clear_custom_labels( pilotFile );
147         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
148                 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(jpilotedit.custom_check[i]) ) ) {
149                         labelName = GTK_LABEL(jpilotedit.custom_label[i])->label;
150                         jpilot_add_custom_label( pilotFile, labelName );
151                 }
152         }
153 }
154
155 static void edit_jpilot_file_check( void ) {
156         gint t = -1;
157         gchar *sFile;
158         gchar *sMsg;
159         gboolean flg;
160
161         flg = FALSE;
162         sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
163         if( sFile ) {
164                 g_strchomp( sFile ); g_strchug( sFile );
165                 if( *sFile != '\0' ) {
166                         // Attempt to read file
167                         JPilotFile *jpf = jpilot_create_path( sFile );
168                         t = jpilot_read_data( jpf );
169                         if( t == MGU_SUCCESS ) {
170                                 // Set check boxes
171                                 edit_jpilot_fill_check_box( jpf );
172                                 flg = TRUE;
173                         }
174                         jpilot_free( jpf );
175                         jpf = NULL;
176                 }
177         }
178         if( ! flg ) {
179                 // Clear all check boxes
180                 edit_jpilot_fill_check_box_new();
181         }
182         g_free( sFile );
183
184         // Display appropriate message
185         if( t == MGU_SUCCESS ) {
186                 sMsg = "";
187         }
188         else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
189                 sMsg = _("File does not appear to be JPilot format.");
190         }
191         else {
192                 sMsg = _("Could not read file.");
193         }
194         edit_jpilot_status_show( sMsg );
195 }
196
197 static void edit_jpilot_file_ok( GtkWidget *widget, gpointer data ) {
198         gchar *sFile;
199         AddressFileSelection *afs;
200         GtkWidget *fileSel;
201
202         afs = ( AddressFileSelection * ) data;
203         fileSel = afs->fileSelector;
204         sFile = gtk_file_selection_get_filename( GTK_FILE_SELECTION(fileSel) );
205
206         afs->cancelled = FALSE;
207         gtk_entry_set_text( GTK_ENTRY(jpilotedit.file_entry), sFile );
208         gtk_widget_hide( afs->fileSelector );
209         gtk_grab_remove( afs->fileSelector );
210         edit_jpilot_file_check();
211         gtk_widget_grab_focus( jpilotedit.file_entry );
212 }
213
214 static void edit_jpilot_file_cancel( GtkWidget *widget, gpointer data ) {
215         AddressFileSelection *afs = ( AddressFileSelection * ) data;
216         afs->cancelled = TRUE;
217         gtk_widget_hide( afs->fileSelector );
218         gtk_grab_remove( afs->fileSelector );
219         gtk_widget_grab_focus( jpilotedit.file_entry );
220 }
221
222 static void edit_jpilot_file_select_create( AddressFileSelection *afs ) {
223         GtkWidget *fileSelector;
224
225         fileSelector = gtk_file_selection_new( _("Select JPilot File") );
226         gtk_file_selection_hide_fileop_buttons( GTK_FILE_SELECTION(fileSelector) );
227         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->ok_button),
228                              "clicked", GTK_SIGNAL_FUNC (edit_jpilot_file_ok), ( gpointer ) afs );
229         gtk_signal_connect( GTK_OBJECT (GTK_FILE_SELECTION(fileSelector)->cancel_button),
230                              "clicked", GTK_SIGNAL_FUNC (edit_jpilot_file_cancel), ( gpointer ) afs );
231         afs->fileSelector = fileSelector;
232         afs->cancelled = TRUE;
233 }
234
235 static void edit_jpilot_file_select( void ) {
236         gchar *sFile;
237
238         if (! jpilot_file_selector.fileSelector )
239                 edit_jpilot_file_select_create( & jpilot_file_selector );
240
241         sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
242         gtk_file_selection_set_filename( GTK_FILE_SELECTION( jpilot_file_selector.fileSelector ), sFile );
243         g_free( sFile );
244         gtk_widget_show( jpilot_file_selector.fileSelector );
245         gtk_grab_add( jpilot_file_selector.fileSelector );
246 }
247
248 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
249         GtkWidget *window;
250         GtkWidget *vbox;
251         GtkWidget *table;
252         GtkWidget *label;
253         GtkWidget *name_entry;
254         GtkWidget *file_entry;
255         GtkWidget *vbox_custom;
256         GtkWidget *frame_custom;
257         GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
258         GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
259         GtkWidget *hlbox;
260         GtkWidget *hbbox;
261         GtkWidget *hsep;
262         GtkWidget *ok_btn;
263         GtkWidget *cancel_btn;
264         GtkWidget *check_btn;
265         GtkWidget *file_btn;
266         GtkWidget *hsbox;
267         GtkWidget *statusbar;
268         gint top, i;
269
270         window = gtk_window_new(GTK_WINDOW_DIALOG);
271         gtk_widget_set_usize(window, 450, -1);
272         gtk_container_set_border_width(GTK_CONTAINER(window), 0);
273         gtk_window_set_title(GTK_WINDOW(window), _("Edit JPilot Entry"));
274         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
275         gtk_window_set_modal(GTK_WINDOW(window), TRUE); 
276         gtk_signal_connect(GTK_OBJECT(window), "delete_event",
277                            GTK_SIGNAL_FUNC(edit_jpilot_delete_event),
278                            cancelled);
279         gtk_signal_connect(GTK_OBJECT(window), "key_press_event",
280                            GTK_SIGNAL_FUNC(edit_jpilot_key_pressed),
281                            cancelled);
282
283         vbox = gtk_vbox_new(FALSE, 8);
284         gtk_container_add(GTK_CONTAINER(window), vbox);
285         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
286
287         table = gtk_table_new(2 + JPILOT_NUM_CUSTOM_LABEL, 3, FALSE);
288         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
289         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
290         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
291         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
292
293         // First row
294         top = 0;
295         label = gtk_label_new(_("Name"));
296         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
297         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
298
299         name_entry = gtk_entry_new();
300         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
301
302         check_btn = gtk_button_new_with_label( _(" Check File "));
303         gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
304
305         // Second row
306         top = 1;
307         label = gtk_label_new(_("File"));
308         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
309         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
310
311         file_entry = gtk_entry_new();
312         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
313
314         file_btn = gtk_button_new_with_label( _(" ... "));
315         gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
316
317         // Third row
318         top = 2;
319         frame_custom = gtk_frame_new(_("Additional e-Mail address item(s)"));
320         gtk_table_attach(GTK_TABLE(table), frame_custom, 1, 2, top, (top + JPILOT_NUM_CUSTOM_LABEL), GTK_FILL, 0, 0, 0);
321
322         // Now do custom labels.
323         vbox_custom = gtk_vbox_new (FALSE, 8);
324         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
325                 hlbox = gtk_hbox_new( FALSE, 0 );
326                 custom_check[i] = gtk_check_button_new();
327                 custom_label[i] = gtk_label_new( "" );
328                 gtk_box_pack_start( GTK_BOX(hlbox), custom_check[i], FALSE, FALSE, 0 );
329                 gtk_box_pack_start( GTK_BOX(hlbox), custom_label[i], TRUE, TRUE, 0 );
330                 gtk_box_pack_start( GTK_BOX(vbox_custom), hlbox, TRUE, TRUE, 0 );
331                 gtk_misc_set_alignment(GTK_MISC(custom_label[i]), 0, 0.5);
332                 top++;
333         }
334         gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
335         gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
336
337         // Status line
338         hsbox = gtk_hbox_new(FALSE, 0);
339         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
340         statusbar = gtk_statusbar_new();
341         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
342
343         // Button panel
344         gtkut_button_set_create(&hbbox, &ok_btn, _("OK"),
345                                 &cancel_btn, _("Cancel"), NULL, NULL);
346         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
347         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
348         gtk_widget_grab_default(ok_btn);
349
350         hsep = gtk_hseparator_new();
351         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
352
353         gtk_signal_connect(GTK_OBJECT(ok_btn), "clicked",
354                            GTK_SIGNAL_FUNC(edit_jpilot_ok), cancelled);
355         gtk_signal_connect(GTK_OBJECT(cancel_btn), "clicked",
356                            GTK_SIGNAL_FUNC(edit_jpilot_cancel), cancelled);
357         gtk_signal_connect(GTK_OBJECT(file_btn), "clicked",
358                            GTK_SIGNAL_FUNC(edit_jpilot_file_select), NULL);
359         gtk_signal_connect(GTK_OBJECT(check_btn), "clicked",
360                            GTK_SIGNAL_FUNC(edit_jpilot_file_check), NULL);
361
362         gtk_widget_show_all(vbox);
363
364         jpilotedit.window     = window;
365         jpilotedit.name_entry = name_entry;
366         jpilotedit.file_entry = file_entry;
367         jpilotedit.ok_btn     = ok_btn;
368         jpilotedit.cancel_btn = cancel_btn;
369         jpilotedit.statusbar  = statusbar;
370         jpilotedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit JPilot Dialog" );
371         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
372                 jpilotedit.custom_check[i] = custom_check[i];
373                 jpilotedit.custom_label[i] = custom_label[i];
374         }
375 }
376
377 AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
378         static gboolean cancelled;
379         gchar *sName;
380         gchar *sFile;
381         AddressDataSource *ds = NULL;
382         JPilotFile *jpf = NULL;
383         gboolean fin;
384
385         if( ! jpilotedit.window )
386                 addressbook_edit_jpilot_create(&cancelled);
387         gtk_widget_grab_focus(jpilotedit.ok_btn);
388         gtk_widget_grab_focus(jpilotedit.name_entry);
389         gtk_widget_show(jpilotedit.window);
390         manage_window_set_transient(GTK_WINDOW(jpilotedit.window));
391
392         edit_jpilot_status_show( "" );
393         if( ads ) {
394                 ds = ads->dataSource;
395                 jpf = ds->rawDataSource;
396                 if (jpf->name)
397                         gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), jpf->name);
398                 if (jpf->path)
399                         gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), jpf->path);
400                 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Edit JPilot Entry"));
401                 edit_jpilot_fill_check_box( jpf );
402         }
403         else {
404                 gchar *guessFile = jpilot_find_pilotdb();
405                 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), ADDRESSBOOK_GUESS_JPILOT );
406                 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), guessFile );
407                 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Add New JPilot Entry"));
408                 edit_jpilot_fill_check_box_new();
409                 if( *guessFile != '\0' ) {
410                         edit_jpilot_file_check();
411                 }
412         }
413
414         gtk_main();
415         gtk_widget_hide(jpilotedit.window);
416         if (cancelled == TRUE) return NULL;
417
418         fin = FALSE;
419         sName = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.name_entry), 0, -1 );
420         sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
421         if( *sName == '\0' ) fin = TRUE;
422         if( *sFile == '\0' ) fin = TRUE;
423
424         if( ! fin ) {
425                 if( ! ads ) {
426                         jpf = jpilot_create();
427                         ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_JPILOT, jpf );
428                         ads = addressbook_create_ds_adapter( ds, ADDR_JPILOT, NULL );
429                 }
430                 addressbook_ads_set_name( ads, sName );
431                 jpilot_set_name( jpf, sName );
432                 jpilot_set_file( jpf, sFile );
433                 edit_jpilot_read_check_box( jpf );
434         }
435         g_free( sName );
436         g_free( sFile );
437
438         return ads;
439 }
440
441 #endif /* USE_JPILOT */
442
443 /*
444 * End of Source.
445 */
446