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 <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 
49 #include "addressbook.h"
50 #include "prefs_common.h"
51 #include "addressitem.h"
52 #include "jpilot.h"
53 #include "mgutils.h"
54 #include "filesel.h"
55 #include "gtkutils.h"
56 #include "codeconv.h"
57 #include "manage_window.h"
58 
59 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
60 #define JPILOT_NUM_CUSTOM_LABEL	4
61 
62 static struct _JPilotEdit {
63 	GtkWidget *window;
64 	GtkWidget *name_entry;
65 	GtkWidget *file_entry;
66 	GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
67 	GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
68 	GtkWidget *hbbox;
69 	GtkWidget *ok_btn;
70 	GtkWidget *cancel_btn;
71 	GtkWidget *statusbar;
72 	gint status_cid;
73 } jpilotedit;
74 
75 /*
76 * Edit functions.
77 */
edit_jpilot_status_show(gchar * msg)78 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 );
83 		}
84 	}
85 }
86 
edit_jpilot_delete_event(GtkWidget * widget,GdkEventAny * event,gboolean * cancelled)87 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
88 	*cancelled = TRUE;
89 	gtk_main_quit();
90 	return TRUE;
91 }
92 
edit_jpilot_key_pressed(GtkWidget * widget,GdkEventKey * event,gboolean * cancelled)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();
97 	}
98 	return FALSE;
99 }
100 
edit_jpilot_ok(GtkWidget * widget,gboolean * cancelled)101 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
102 	*cancelled = FALSE;
103 	gtk_main_quit();
104 }
105 
edit_jpilot_cancel(GtkWidget * widget,gboolean * cancelled)106 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
107 	*cancelled = TRUE;
108 	gtk_main_quit();
109 }
110 
edit_jpilot_fill_check_box(JPilotFile * jpf)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] ), "" );
119 	}
120 
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 );
134 		}
135 		else {
136 			done = TRUE;
137 		}
138 	}
139 	mgu_free_dlist( customLbl );
140 	customLbl = NULL;
141 }
142 
edit_jpilot_fill_check_box_new()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] ), "" );
148 	}
149 }
150 
edit_jpilot_read_check_box(JPilotFile * pilotFile)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 );
159 		}
160 	}
161 }
162 
edit_jpilot_file_check(void)163 static void edit_jpilot_file_check( void ) {
164 	gint t = -1;
165 	gchar *sFile;
166 	gchar *sFSFile;
167 	gchar *sMsg;
168 	gboolean flg;
169 
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;
183 			}
184 			jpilot_free( jpf );
185 			jpf = NULL;
186 		}
187 		g_free( sFSFile );
188 		g_free( sFile );
189 	}
190 	if( ! flg ) {
191 		/* Clear all check boxes */
192 		edit_jpilot_fill_check_box_new();
193 	}
194 
195 	/* Display appropriate message */
196 	if( t == MGU_SUCCESS ) {
197 		sMsg = "";
198 	}
199 	else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
200 		sMsg = _("File does not appear to be JPilot format.");
201 	}
202 	else {
203 		sMsg = _("Could not read file.");
204 	}
205 	edit_jpilot_status_show( sMsg );
206 }
207 
edit_jpilot_file_select(void)208 static void edit_jpilot_file_select( void ) {
209 	gchar *sFile;
210 	gchar *sUTF8File;
211 
212 	sFile = filesel_select_file( _("Select JPilot File"), NULL, GTK_FILE_CHOOSER_ACTION_OPEN );
213 
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();
220 	}
221 }
222 
addressbook_edit_jpilot_create(gboolean * cancelled)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;
244 
245 	window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
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);
257 
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 );
261 
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);
267 
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);
273 
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);
276 
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);
279 
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);
285 
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);
288 
289 	file_btn = gtk_button_new_with_label( _(" ... "));
290 	gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
291 
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);
296 
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++;
308 	}
309 	gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
310 	gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
311 
312 	/* Status line */
313 	hsbox = gtk_hbox_new(FALSE, 0);
314 	gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, 0);
315 	statusbar = gtk_statusbar_new();
316 	gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, 0);
317 
318 	/* Button panel */
319 	gtkut_stock_button_set_create(&hbbox, &ok_btn, GTK_STOCK_OK,
320 				      &cancel_btn, GTK_STOCK_CANCEL,
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);
325 
326 	hsep = gtk_hseparator_new();
327 	gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
328 
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);
337 
338 	gtk_widget_show_all(vbox);
339 
340 	jpilotedit.window     = window;
341 	jpilotedit.name_entry = name_entry;
342 	jpilotedit.file_entry = file_entry;
343 	jpilotedit.hbbox      = hbbox;
344 	jpilotedit.ok_btn     = ok_btn;
345 	jpilotedit.cancel_btn = cancel_btn;
346 	jpilotedit.statusbar  = statusbar;
347 	jpilotedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit JPilot Dialog" );
348 	for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
349 		jpilotedit.custom_check[i] = custom_check[i];
350 		jpilotedit.custom_label[i] = custom_label[i];
351 	}
352 }
353 
addressbook_edit_jpilot(AddressIndex * addrIndex,AdapterDSource * ads)354 AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
355 	static gboolean cancelled;
356 	gchar *sName;
357 	gchar *sFile;
358 	gchar *sFSFile;
359 	AddressDataSource *ds = NULL;
360 	JPilotFile *jpf = NULL;
361 	gboolean fin;
362 
363 	if( ! jpilotedit.window )
364 		addressbook_edit_jpilot_create(&cancelled);
365 	gtkut_box_set_reverse_order(GTK_BOX(jpilotedit.hbbox),
366 				    !prefs_common.comply_gnome_hig);
367 	gtk_widget_grab_focus(jpilotedit.ok_btn);
368 	gtk_widget_grab_focus(jpilotedit.name_entry);
369 	gtk_widget_show(jpilotedit.window);
370 	manage_window_set_transient(GTK_WINDOW(jpilotedit.window));
371 
372 	edit_jpilot_status_show( "" );
373 	if( ads ) {
374 		ds = ads->dataSource;
375 		jpf = ds->rawDataSource;
376 		if (jpf->name)
377 			gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), jpf->name);
378 		if (jpf->path)
379 			gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), jpf->path);
380 		gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Edit JPilot Entry"));
381 		edit_jpilot_fill_check_box( jpf );
382 	}
383 	else {
384 		gchar *guessFile = jpilot_find_pilotdb();
385 		gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), ADDRESSBOOK_GUESS_JPILOT );
386 		gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), guessFile );
387 		gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Add New JPilot Entry"));
388 		edit_jpilot_fill_check_box_new();
389 		if( *guessFile != '\0' ) {
390 			edit_jpilot_file_check();
391 		}
392 	}
393 
394 	gtk_main();
395 	gtk_widget_hide(jpilotedit.window);
396 	if (cancelled == TRUE) return NULL;
397 
398 	fin = FALSE;
399 	sName = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.name_entry), 0, -1 );
400 	sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
401 	sFSFile = conv_filename_from_utf8( sFile );
402 	if( *sName == '\0' ) fin = TRUE;
403 	if( *sFile == '\0' ) fin = TRUE;
404 	if( ! sFSFile || *sFSFile == '\0' ) fin = TRUE;
405 
406 	if( ! fin ) {
407 		if( ! ads ) {
408 			jpf = jpilot_create();
409 			ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_JPILOT, jpf );
410 			ads = addressbook_create_ds_adapter( ds, ADDR_JPILOT, NULL );
411 		}
412 		addressbook_ads_set_name( ads, sName );
413 		jpilot_set_name( jpf, sName );
414 		jpilot_set_file( jpf, sFSFile );
415 		edit_jpilot_read_check_box( jpf );
416 	}
417 	g_free( sFSFile );
418 	g_free( sFile );
419 	g_free( sName );
420 
421 	return ads;
422 }
423 
424 #endif /* USE_JPILOT */
425 
426 /*
427 * End of Source.
428 */
429 
430