2.10.0 unleashed
[claws.git] / src / editjpilot.c
blob23b706582388856460234b0137064768518dd270
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2007 Match Grun and the Claws Mail team
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * Edit JPilot address book data.
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #endif
28 #ifdef USE_JPILOT
30 #include "defs.h"
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/gtkvbox.h>
42 #include <gtk/gtktable.h>
43 #include <gtk/gtkframe.h>
44 #include <gtk/gtkhseparator.h>
45 #include <gtk/gtkbutton.h>
46 #include <gtk/gtkstatusbar.h>
47 #include <gtk/gtkcheckbutton.h>
48 #include <gtk/gtkfilesel.h>
50 #include "addressbook.h"
51 #include "prefs_common.h"
52 #include "addressitem.h"
53 #include "jpilot.h"
54 #include "mgutils.h"
55 #include "filesel.h"
56 #include "gtkutils.h"
57 #include "codeconv.h"
58 #include "manage_window.h"
60 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
61 #define JPILOT_NUM_CUSTOM_LABEL 4
63 static struct _JPilotEdit {
64 GtkWidget *window;
65 GtkWidget *name_entry;
66 GtkWidget *file_entry;
67 GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
68 GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
69 GtkWidget *ok_btn;
70 GtkWidget *cancel_btn;
71 GtkWidget *statusbar;
72 gint status_cid;
73 } jpilotedit;
76 * Edit functions.
78 static void edit_jpilot_status_show( gchar *msg ) {
79 if( jpilotedit.statusbar != NULL ) {
80 gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
81 if( msg ) {
82 gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
87 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
88 *cancelled = TRUE;
89 gtk_main_quit();
90 return TRUE;
93 static gboolean edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
94 if (event && event->keyval == GDK_Escape) {
95 *cancelled = TRUE;
96 gtk_main_quit();
98 return FALSE;
101 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
102 *cancelled = FALSE;
103 gtk_main_quit();
106 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
107 *cancelled = TRUE;
108 gtk_main_quit();
111 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
112 gint i;
113 GList *node, *customLbl = NULL;
114 gchar *labelName;
115 gboolean done, checked;
116 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
117 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
118 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
121 done = FALSE;
122 i = 0;
123 customLbl = jpilot_load_custom_label( jpf, customLbl );
124 node = customLbl;
125 while( ! done ) {
126 if( node ) {
127 labelName = node->data;
128 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), labelName );
129 checked = jpilot_test_custom_label( jpf, labelName );
130 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), checked );
131 i++;
132 if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
133 node = g_list_next( node );
135 else {
136 done = TRUE;
139 mgu_free_dlist( customLbl );
140 customLbl = NULL;
143 static void edit_jpilot_fill_check_box_new() {
144 gint i;
145 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
146 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
147 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
151 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
152 gint i;
153 gchar *labelName;
154 jpilot_clear_custom_labels( pilotFile );
155 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
156 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(jpilotedit.custom_check[i]) ) ) {
157 labelName = GTK_LABEL(jpilotedit.custom_label[i])->label;
158 jpilot_add_custom_label( pilotFile, labelName );
163 static void edit_jpilot_file_check( void ) {
164 gint t = -1;
165 gchar *sFile;
166 gchar *sFSFile;
167 gchar *sMsg;
168 gboolean flg;
170 flg = FALSE;
171 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
172 if( sFile ) {
173 sFSFile = conv_filename_from_utf8( sFile );
174 if( sFSFile && *sFSFile != '\0' ) {
175 /* Attempt to read file */
176 JPilotFile *jpf;
177 jpf = jpilot_create_path( sFSFile );
178 t = jpilot_read_data( jpf );
179 if( t == MGU_SUCCESS ) {
180 /* Set check boxes */
181 edit_jpilot_fill_check_box( jpf );
182 flg = TRUE;
184 jpilot_free( jpf );
185 jpf = NULL;
187 g_free( sFSFile );
188 g_free( sFile );
190 if( ! flg ) {
191 /* Clear all check boxes */
192 edit_jpilot_fill_check_box_new();
195 /* Display appropriate message */
196 if( t == MGU_SUCCESS ) {
197 sMsg = "";
199 else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
200 sMsg = _("File does not appear to be JPilot format.");
202 else {
203 sMsg = _("Could not read file.");
205 edit_jpilot_status_show( sMsg );
208 static void edit_jpilot_file_select( void ) {
209 gchar *sFile;
210 gchar *sUTF8File;
212 sFile = filesel_select_file_open( _("Select JPilot File"), NULL);
214 if( sFile ) {
215 sUTF8File = conv_filename_to_utf8( sFile );
216 gtk_entry_set_text( GTK_ENTRY(jpilotedit.file_entry), sUTF8File );
217 g_free( sUTF8File );
218 g_free( sFile );
219 edit_jpilot_file_check();
223 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
224 GtkWidget *window;
225 GtkWidget *vbox;
226 GtkWidget *table;
227 GtkWidget *label;
228 GtkWidget *name_entry;
229 GtkWidget *file_entry;
230 GtkWidget *vbox_custom;
231 GtkWidget *frame_custom;
232 GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
233 GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
234 GtkWidget *hlbox;
235 GtkWidget *hbbox;
236 GtkWidget *hsep;
237 GtkWidget *ok_btn;
238 GtkWidget *cancel_btn;
239 GtkWidget *check_btn;
240 GtkWidget *file_btn;
241 GtkWidget *hsbox;
242 GtkWidget *statusbar;
243 gint top, i;
245 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editjpilot");
246 gtk_widget_set_size_request(window, 450, -1);
247 gtk_container_set_border_width(GTK_CONTAINER(window), 0);
248 gtk_window_set_title(GTK_WINDOW(window), _("Edit JPilot Entry"));
249 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
250 gtk_window_set_modal(GTK_WINDOW(window), TRUE);
251 g_signal_connect(G_OBJECT(window), "delete_event",
252 G_CALLBACK(edit_jpilot_delete_event),
253 cancelled);
254 g_signal_connect(G_OBJECT(window), "key_press_event",
255 G_CALLBACK(edit_jpilot_key_pressed),
256 cancelled);
258 vbox = gtk_vbox_new(FALSE, 8);
259 gtk_container_add(GTK_CONTAINER(window), vbox);
260 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
262 table = gtk_table_new(2 + JPILOT_NUM_CUSTOM_LABEL, 3, FALSE);
263 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
264 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
265 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
266 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
268 /* First row */
269 top = 0;
270 label = gtk_label_new(_("Name"));
271 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
272 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
274 name_entry = gtk_entry_new();
275 gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
277 check_btn = gtk_button_new_with_label( _(" Check File "));
278 gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
280 /* Second row */
281 top = 1;
282 label = gtk_label_new(_("File"));
283 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
284 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
286 file_entry = gtk_entry_new();
287 gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
289 file_btn = gtkut_get_browse_file_btn(_("_Browse"));
290 gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
292 /* Third row */
293 top = 2;
294 frame_custom = gtk_frame_new(_("Additional e-Mail address item(s)"));
295 gtk_table_attach(GTK_TABLE(table), frame_custom, 1, 2, top, (top + JPILOT_NUM_CUSTOM_LABEL), GTK_FILL, 0, 0, 0);
297 /* Now do custom labels. */
298 vbox_custom = gtk_vbox_new (FALSE, 8);
299 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
300 hlbox = gtk_hbox_new( FALSE, 0 );
301 custom_check[i] = gtk_check_button_new();
302 custom_label[i] = gtk_label_new( "" );
303 gtk_box_pack_start( GTK_BOX(hlbox), custom_check[i], FALSE, FALSE, 0 );
304 gtk_box_pack_start( GTK_BOX(hlbox), custom_label[i], TRUE, TRUE, 0 );
305 gtk_box_pack_start( GTK_BOX(vbox_custom), hlbox, TRUE, TRUE, 0 );
306 gtk_misc_set_alignment(GTK_MISC(custom_label[i]), 0, 0.5);
307 top++;
309 gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
310 gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
312 /* Status line */
313 hsbox = gtk_hbox_new(FALSE, 0);
314 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
315 statusbar = gtk_statusbar_new();
316 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
318 /* Button panel */
319 gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
320 &ok_btn, GTK_STOCK_OK,
321 NULL, NULL);
322 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
323 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
324 gtk_widget_grab_default(ok_btn);
326 hsep = gtk_hseparator_new();
327 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
329 g_signal_connect(G_OBJECT(ok_btn), "clicked",
330 G_CALLBACK(edit_jpilot_ok), cancelled);
331 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
332 G_CALLBACK(edit_jpilot_cancel), cancelled);
333 g_signal_connect(G_OBJECT(file_btn), "clicked",
334 G_CALLBACK(edit_jpilot_file_select), NULL);
335 g_signal_connect(G_OBJECT(check_btn), "clicked",
336 G_CALLBACK(edit_jpilot_file_check), NULL);
338 gtk_widget_show_all(vbox);
340 jpilotedit.window = window;
341 jpilotedit.name_entry = name_entry;
342 jpilotedit.file_entry = file_entry;
343 jpilotedit.ok_btn = ok_btn;
344 jpilotedit.cancel_btn = cancel_btn;
345 jpilotedit.statusbar = statusbar;
346 jpilotedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit JPilot Dialog" );
347 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
348 jpilotedit.custom_check[i] = custom_check[i];
349 jpilotedit.custom_label[i] = custom_label[i];
353 AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
354 static gboolean cancelled;
355 gchar *sName;
356 gchar *sFile;
357 gchar *sFSFile;
358 AddressDataSource *ds = NULL;
359 JPilotFile *jpf = NULL;
360 gboolean fin;
362 if( ! jpilotedit.window )
363 addressbook_edit_jpilot_create(&cancelled);
364 gtk_widget_grab_focus(jpilotedit.ok_btn);
365 gtk_widget_grab_focus(jpilotedit.name_entry);
366 gtk_widget_show(jpilotedit.window);
367 manage_window_set_transient(GTK_WINDOW(jpilotedit.window));
369 edit_jpilot_status_show( "" );
370 if( ads ) {
371 ds = ads->dataSource;
372 jpf = ds->rawDataSource;
373 if ( jpilot_get_name( jpf ) )
374 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry),
375 jpilot_get_name( jpf ) );
376 if (jpf->path)
377 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), jpf->path);
378 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Edit JPilot Entry"));
379 edit_jpilot_fill_check_box( jpf );
381 else {
382 gchar *guessFile = jpilot_find_pilotdb();
383 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), ADDRESSBOOK_GUESS_JPILOT );
384 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), guessFile );
385 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Add New JPilot Entry"));
386 edit_jpilot_fill_check_box_new();
387 if( *guessFile != '\0' ) {
388 edit_jpilot_file_check();
392 gtk_main();
393 gtk_widget_hide(jpilotedit.window);
394 if (cancelled == TRUE) return NULL;
396 fin = FALSE;
397 sName = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.name_entry), 0, -1 );
398 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
399 sFSFile = conv_filename_from_utf8( sFile );
400 if( *sName == '\0' ) fin = TRUE;
401 if( *sFile == '\0' ) fin = TRUE;
402 if( ! sFSFile || *sFSFile == '\0' ) fin = TRUE;
404 if( ! fin ) {
405 if( ! ads ) {
406 jpf = jpilot_create();
407 ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_JPILOT, jpf );
408 ads = addressbook_create_ds_adapter( ds, ADDR_JPILOT, NULL );
410 addressbook_ads_set_name( ads, sName );
411 jpilot_set_name( jpf, sName );
412 jpilot_set_file( jpf, sFSFile );
413 edit_jpilot_read_check_box( jpf );
415 g_free( sFSFile );
416 g_free( sFile );
417 g_free( sName );
419 return ads;
422 #endif /* USE_JPILOT */
425 * End of Source.