2010-03-27 [pawel] 3.7.5cvs41
[claws.git] / src / editjpilot.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2009 Match Grun and the Claws Mail team
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 3 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, see <http://www.gnu.org/licenses/>.
17  * 
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/gtk.h>
36
37 #include "addressbook.h"
38 #include "prefs_common.h"
39 #include "addressitem.h"
40 #include "jpilot.h"
41 #include "mgutils.h"
42 #include "filesel.h"
43 #include "gtkutils.h"
44 #include "codeconv.h"
45 #include "manage_window.h"
46
47 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
48 #define JPILOT_NUM_CUSTOM_LABEL 4
49
50 static struct _JPilotEdit {
51         GtkWidget *window;
52         GtkWidget *name_entry;
53         GtkWidget *file_entry;
54         GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
55         GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
56         GtkWidget *ok_btn;
57         GtkWidget *cancel_btn;
58         GtkWidget *statusbar;
59         gint status_cid;
60 } jpilotedit;
61
62 /*
63 * Edit functions.
64 */
65 static void edit_jpilot_status_show( gchar *msg ) {
66         if( jpilotedit.statusbar != NULL ) {
67                 gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
68                 if( msg ) {
69                         gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
70                 }
71         }
72 }
73
74 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
75         *cancelled = TRUE;
76         gtk_main_quit();
77         return TRUE;
78 }
79
80 static gboolean edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
81         if (event && event->keyval == GDK_Escape) {
82                 *cancelled = TRUE;
83                 gtk_main_quit();
84         }
85         return FALSE;
86 }
87
88 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
89         *cancelled = FALSE;
90         gtk_main_quit();
91 }
92
93 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
94         *cancelled = TRUE;
95         gtk_main_quit();
96 }
97
98 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
99         gint i;
100         GList *node, *customLbl = NULL;
101         gchar *labelName;
102         gboolean done, checked;
103         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
104                 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
105                 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
106         }
107
108         done = FALSE;
109         i = 0;
110         customLbl = jpilot_load_custom_label( jpf, customLbl );
111         node = customLbl;
112         while( ! done ) {
113                 if( node ) {
114                         labelName = node->data;
115                         gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), labelName );
116                         checked = jpilot_test_custom_label( jpf, labelName );
117                         gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), checked );
118                         i++;
119                         if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
120                         node = g_list_next( node );
121                 }
122                 else {
123                         done = TRUE;
124                 }
125         }
126         mgu_free_dlist( customLbl );
127         customLbl = NULL;
128 }
129
130 static void edit_jpilot_fill_check_box_new() {
131         gint i;
132         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
133                 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
134                 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
135         }
136 }
137
138 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
139         gint i;
140         gchar *labelName;
141         jpilot_clear_custom_labels( pilotFile );
142         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
143                 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(jpilotedit.custom_check[i]) ) ) {
144                         labelName = GTK_LABEL(jpilotedit.custom_label[i])->label;
145                         jpilot_add_custom_label( pilotFile, labelName );
146                 }
147         }
148 }
149
150 static void edit_jpilot_file_check( void ) {
151         gint t = -1;
152         gchar *sFile;
153         gchar *sFSFile;
154         gchar *sMsg;
155         gboolean flg;
156
157         flg = FALSE;
158         sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
159         if( sFile ) {
160                 sFSFile = conv_filename_from_utf8( sFile );
161                 if( sFSFile && *sFSFile != '\0' ) {
162                         /* Attempt to read file */
163                         JPilotFile *jpf;
164                         jpf = jpilot_create_path( sFSFile );
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                         jpf = NULL;
173                 }
174                 g_free( sFSFile );
175                 g_free( sFile );
176         }
177         if( ! flg ) {
178                 /* Clear all check boxes */
179                 edit_jpilot_fill_check_box_new();
180         }
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_select( void ) {
196         gchar *sFile;
197         gchar *sUTF8File;
198
199         sFile = filesel_select_file_open( _("Select JPilot File"), NULL);
200
201         if( sFile ) {
202                 sUTF8File = conv_filename_to_utf8( sFile );
203                 gtk_entry_set_text( GTK_ENTRY(jpilotedit.file_entry), sUTF8File );
204                 g_free( sUTF8File );
205                 g_free( sFile );
206                 edit_jpilot_file_check();
207         }
208 }
209
210 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
211         GtkWidget *window;
212         GtkWidget *vbox;
213         GtkWidget *table;
214         GtkWidget *label;
215         GtkWidget *name_entry;
216         GtkWidget *file_entry;
217         GtkWidget *vbox_custom;
218         GtkWidget *frame_custom;
219         GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
220         GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
221         GtkWidget *hlbox;
222         GtkWidget *hbbox;
223         GtkWidget *hsep;
224         GtkWidget *ok_btn;
225         GtkWidget *cancel_btn;
226         GtkWidget *check_btn;
227         GtkWidget *file_btn;
228         GtkWidget *hsbox;
229         GtkWidget *statusbar;
230         gint top, i;
231
232         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editjpilot");
233         gtk_widget_set_size_request(window, 450, -1);
234         gtk_container_set_border_width(GTK_CONTAINER(window), 0);
235         gtk_window_set_title(GTK_WINDOW(window), _("Edit JPilot Entry"));
236         gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
237         g_signal_connect(G_OBJECT(window), "delete_event",
238                          G_CALLBACK(edit_jpilot_delete_event),
239                          cancelled);
240         g_signal_connect(G_OBJECT(window), "key_press_event",
241                          G_CALLBACK(edit_jpilot_key_pressed),
242                          cancelled);
243
244         vbox = gtk_vbox_new(FALSE, 8);
245         gtk_container_add(GTK_CONTAINER(window), vbox);
246         gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
247
248         table = gtk_table_new(2 + JPILOT_NUM_CUSTOM_LABEL, 3, FALSE);
249         gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
250         gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
251         gtk_table_set_row_spacings(GTK_TABLE(table), 8);
252         gtk_table_set_col_spacings(GTK_TABLE(table), 8);
253
254         /* First row */
255         top = 0;
256         label = gtk_label_new(_("Name"));
257         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
258         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
259
260         name_entry = gtk_entry_new();
261         gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
262
263         check_btn = gtk_button_new_with_label( _(" Check File "));
264         gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
265
266         /* Second row */
267         top = 1;
268         label = gtk_label_new(_("File"));
269         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
270         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
271
272         file_entry = gtk_entry_new();
273         gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
274
275         file_btn = gtkut_get_browse_file_btn(_("_Browse"));
276         gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
277
278         /* Third row */
279         top = 2;
280         frame_custom = gtk_frame_new(_("Additional e-Mail address item(s)"));
281         gtk_table_attach(GTK_TABLE(table), frame_custom, 1, 2, top, (top + JPILOT_NUM_CUSTOM_LABEL), GTK_FILL, 0, 0, 0);
282
283         /* Now do custom labels. */
284         vbox_custom = gtk_vbox_new (FALSE, 8);
285         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
286                 hlbox = gtk_hbox_new( FALSE, 0 );
287                 custom_check[i] = gtk_check_button_new();
288                 custom_label[i] = gtk_label_new( "" );
289                 gtk_box_pack_start( GTK_BOX(hlbox), custom_check[i], FALSE, FALSE, 0 );
290                 gtk_box_pack_start( GTK_BOX(hlbox), custom_label[i], TRUE, TRUE, 0 );
291                 gtk_box_pack_start( GTK_BOX(vbox_custom), hlbox, TRUE, TRUE, 0 );
292                 gtk_misc_set_alignment(GTK_MISC(custom_label[i]), 0, 0.5);
293                 top++;
294         }
295         gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
296         gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
297
298         /* Status line */
299         hsbox = gtk_hbox_new(FALSE, 0);
300         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
301         statusbar = gtk_statusbar_new();
302         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
303
304         /* Button panel */
305         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
306                                       &ok_btn, GTK_STOCK_OK,
307                                       NULL, NULL);
308         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
309         gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
310         gtk_widget_grab_default(ok_btn);
311
312         hsep = gtk_hseparator_new();
313         gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
314
315         g_signal_connect(G_OBJECT(ok_btn), "clicked",
316                          G_CALLBACK(edit_jpilot_ok), cancelled);
317         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
318                          G_CALLBACK(edit_jpilot_cancel), cancelled);
319         g_signal_connect(G_OBJECT(file_btn), "clicked",
320                          G_CALLBACK(edit_jpilot_file_select), NULL);
321         g_signal_connect(G_OBJECT(check_btn), "clicked",
322                          G_CALLBACK(edit_jpilot_file_check), NULL);
323
324         gtk_widget_show_all(vbox);
325
326         jpilotedit.window     = window;
327         jpilotedit.name_entry = name_entry;
328         jpilotedit.file_entry = file_entry;
329         jpilotedit.ok_btn     = ok_btn;
330         jpilotedit.cancel_btn = cancel_btn;
331         jpilotedit.statusbar  = statusbar;
332         jpilotedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit JPilot Dialog" );
333         for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
334                 jpilotedit.custom_check[i] = custom_check[i];
335                 jpilotedit.custom_label[i] = custom_label[i];
336         }
337 }
338
339 AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
340         static gboolean cancelled;
341         gchar *sName;
342         gchar *sFile;
343         gchar *sFSFile;
344         AddressDataSource *ds = NULL;
345         JPilotFile *jpf = NULL;
346         gboolean fin;
347
348         if( ! jpilotedit.window )
349                 addressbook_edit_jpilot_create(&cancelled);
350         gtk_widget_grab_focus(jpilotedit.ok_btn);
351         gtk_widget_grab_focus(jpilotedit.name_entry);
352         gtk_widget_show(jpilotedit.window);
353         manage_window_set_transient(GTK_WINDOW(jpilotedit.window));
354         gtk_window_set_modal(GTK_WINDOW(jpilotedit.window), TRUE);
355         edit_jpilot_status_show( "" );
356         if( ads ) {
357                 ds = ads->dataSource;
358                 jpf = ds->rawDataSource;
359                 if ( jpilot_get_name( jpf ) )
360                         gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry),
361                                 jpilot_get_name( jpf ) );
362                 if (jpf->path)
363                         gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), jpf->path);
364                 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Edit JPilot Entry"));
365                 edit_jpilot_fill_check_box( jpf );
366         }
367         else {
368                 gchar *guessFile = jpilot_find_pilotdb();
369                 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), ADDRESSBOOK_GUESS_JPILOT );
370                 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), guessFile );
371                 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Add New JPilot Entry"));
372                 edit_jpilot_fill_check_box_new();
373                 if( *guessFile != '\0' ) {
374                         edit_jpilot_file_check();
375                 }
376         }
377
378         gtk_main();
379         gtk_widget_hide(jpilotedit.window);
380         gtk_window_set_modal(GTK_WINDOW(jpilotedit.window), FALSE);
381         if (cancelled == TRUE) return NULL;
382
383         fin = FALSE;
384         sName = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.name_entry), 0, -1 );
385         sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
386         sFSFile = conv_filename_from_utf8( sFile );
387         if( *sName == '\0' ) fin = TRUE;
388         if( *sFile == '\0' ) fin = TRUE;
389         if( ! sFSFile || *sFSFile == '\0' ) fin = TRUE;
390
391         if( ! fin ) {
392                 if( ! ads ) {
393                         jpf = jpilot_create();
394                         ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_JPILOT, jpf );
395                         ads = addressbook_create_ds_adapter( ds, ADDR_JPILOT, NULL );
396                 }
397                 addressbook_ads_set_name( ads, sName );
398                 jpilot_set_name( jpf, sName );
399                 jpilot_set_file( jpf, sFSFile );
400                 edit_jpilot_read_check_box( jpf );
401         }
402         g_free( sFSFile );
403         g_free( sFile );
404         g_free( sName );
405
406         return ads;
407 }
408
409 #endif /* USE_JPILOT */
410
411 /*
412 * End of Source.
413 */
414