Fix bug #3574: Template addressing
[claws.git] / src / editjpilot.c
blob9f1132ee98f5cd396b772f8a195c4f6201941ebe
1 /*
2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2012 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 3 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, see <http://www.gnu.org/licenses/>.
21 * Edit JPilot address book data.
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #include "claws-features.h"
27 #endif
29 #ifdef USE_JPILOT
31 #include "defs.h"
33 #include <glib.h>
34 #include <glib/gi18n.h>
35 #include <gdk/gdkkeysyms.h>
36 #include <gtk/gtk.h>
38 #include "addressbook.h"
39 #include "prefs_common.h"
40 #include "addressitem.h"
41 #include "jpilot.h"
42 #include "mgutils.h"
43 #include "filesel.h"
44 #include "gtkutils.h"
45 #include "codeconv.h"
46 #include "manage_window.h"
48 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
49 #define JPILOT_NUM_CUSTOM_LABEL 4
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;
64 * Edit functions.
66 static void edit_jpilot_status_show( gchar *msg ) {
67 if( jpilotedit.statusbar != NULL ) {
68 gtk_statusbar_pop( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid );
69 if( msg ) {
70 gtk_statusbar_push( GTK_STATUSBAR(jpilotedit.statusbar), jpilotedit.status_cid, msg );
75 static gint edit_jpilot_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
76 *cancelled = TRUE;
77 gtk_main_quit();
78 return TRUE;
81 static gboolean edit_jpilot_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
82 if (event && event->keyval == GDK_KEY_Escape) {
83 *cancelled = TRUE;
84 gtk_main_quit();
86 return FALSE;
89 static void edit_jpilot_ok( GtkWidget *widget, gboolean *cancelled ) {
90 *cancelled = FALSE;
91 gtk_main_quit();
94 static void edit_jpilot_cancel( GtkWidget *widget, gboolean *cancelled ) {
95 *cancelled = TRUE;
96 gtk_main_quit();
99 static void edit_jpilot_fill_check_box( JPilotFile *jpf ) {
100 gint i;
101 GList *node, *customLbl = NULL;
102 gchar *labelName;
103 gboolean done, checked;
104 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
105 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
106 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
109 done = FALSE;
110 i = 0;
111 customLbl = jpilot_load_custom_label( jpf, customLbl );
112 node = customLbl;
113 while( ! done ) {
114 if( node ) {
115 labelName = node->data;
116 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), labelName );
117 checked = jpilot_test_custom_label( jpf, labelName );
118 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), checked );
119 i++;
120 if( i >= JPILOT_NUM_CUSTOM_LABEL ) done = TRUE;
121 node = g_list_next( node );
123 else {
124 done = TRUE;
127 mgu_free_dlist( customLbl );
128 customLbl = NULL;
131 static void edit_jpilot_fill_check_box_new() {
132 gint i;
133 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
134 gtk_toggle_button_set_active( GTK_TOGGLE_BUTTON( jpilotedit.custom_check[i] ), FALSE );
135 gtk_label_set_text( GTK_LABEL( jpilotedit.custom_label[i] ), "" );
139 static void edit_jpilot_read_check_box( JPilotFile *pilotFile ) {
140 gint i;
141 gchar *labelName;
142 jpilot_clear_custom_labels( pilotFile );
143 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
144 if( gtk_toggle_button_get_active( GTK_TOGGLE_BUTTON(jpilotedit.custom_check[i]) ) ) {
145 labelName = GTK_LABEL(jpilotedit.custom_label[i])->label;
146 jpilot_add_custom_label( pilotFile, labelName );
151 static void edit_jpilot_file_check( void ) {
152 gint t = -1;
153 gchar *sFile;
154 gchar *sFSFile;
155 gchar *sMsg;
156 gboolean flg;
158 flg = FALSE;
159 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
160 if( sFile ) {
161 sFSFile = conv_filename_from_utf8( sFile );
162 if( sFSFile && *sFSFile != '\0' ) {
163 /* Attempt to read file */
164 JPilotFile *jpf;
165 jpf = jpilot_create_path( sFSFile );
166 t = jpilot_read_data( jpf );
167 if( t == MGU_SUCCESS ) {
168 /* Set check boxes */
169 edit_jpilot_fill_check_box( jpf );
170 flg = TRUE;
172 jpilot_free( jpf );
173 jpf = NULL;
175 g_free( sFSFile );
176 g_free( sFile );
178 if( ! flg ) {
179 /* Clear all check boxes */
180 edit_jpilot_fill_check_box_new();
183 /* Display appropriate message */
184 if( t == MGU_SUCCESS ) {
185 sMsg = "";
187 else if( t == MGU_BAD_FORMAT || t == MGU_OO_MEMORY ) {
188 sMsg = _("File does not appear to be JPilot format.");
190 else {
191 sMsg = _("Could not read file.");
193 edit_jpilot_status_show( sMsg );
196 static void edit_jpilot_file_select( void ) {
197 gchar *sFile;
198 gchar *sUTF8File;
200 sFile = filesel_select_file_open( _("Select JPilot File"), NULL);
202 if( sFile ) {
203 sUTF8File = conv_filename_to_utf8( sFile );
204 gtk_entry_set_text( GTK_ENTRY(jpilotedit.file_entry), sUTF8File );
205 g_free( sUTF8File );
206 g_free( sFile );
207 edit_jpilot_file_check();
211 static void addressbook_edit_jpilot_create( gboolean *cancelled ) {
212 GtkWidget *window;
213 GtkWidget *vbox;
214 GtkWidget *table;
215 GtkWidget *label;
216 GtkWidget *name_entry;
217 GtkWidget *file_entry;
218 GtkWidget *vbox_custom;
219 GtkWidget *frame_custom;
220 GtkWidget *custom_check[JPILOT_NUM_CUSTOM_LABEL];
221 GtkWidget *custom_label[JPILOT_NUM_CUSTOM_LABEL];
222 GtkWidget *hlbox;
223 GtkWidget *hbbox;
224 GtkWidget *hsep;
225 GtkWidget *ok_btn;
226 GtkWidget *cancel_btn;
227 GtkWidget *check_btn;
228 GtkWidget *file_btn;
229 GtkWidget *hsbox;
230 GtkWidget *statusbar;
231 gint top, i;
233 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editjpilot");
234 gtk_widget_set_size_request(window, 450, -1);
235 gtk_container_set_border_width(GTK_CONTAINER(window), 0);
236 gtk_window_set_title(GTK_WINDOW(window), _("Edit JPilot Entry"));
237 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
238 g_signal_connect(G_OBJECT(window), "delete_event",
239 G_CALLBACK(edit_jpilot_delete_event),
240 cancelled);
241 g_signal_connect(G_OBJECT(window), "key_press_event",
242 G_CALLBACK(edit_jpilot_key_pressed),
243 cancelled);
245 vbox = gtk_vbox_new(FALSE, 8);
246 gtk_container_add(GTK_CONTAINER(window), vbox);
247 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
249 table = gtk_table_new(2 + JPILOT_NUM_CUSTOM_LABEL, 3, FALSE);
250 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
251 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
252 gtk_table_set_row_spacings(GTK_TABLE(table), 8);
253 gtk_table_set_col_spacings(GTK_TABLE(table), 8);
255 /* First row */
256 top = 0;
257 label = gtk_label_new(_("Name"));
258 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
259 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
261 name_entry = gtk_entry_new();
262 gtk_table_attach(GTK_TABLE(table), name_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
264 check_btn = gtk_button_new_with_label( _(" Check File "));
265 gtk_table_attach(GTK_TABLE(table), check_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
267 /* Second row */
268 top = 1;
269 label = gtk_label_new(_("File"));
270 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
271 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
273 file_entry = gtk_entry_new();
274 gtk_table_attach(GTK_TABLE(table), file_entry, 1, 2, top, (top + 1), GTK_EXPAND|GTK_SHRINK|GTK_FILL, 0, 0, 0);
276 file_btn = gtkut_get_browse_file_btn(_("_Browse"));
277 gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0);
279 /* Third row */
280 top = 2;
281 frame_custom = gtk_frame_new(_("Additional e-Mail address item(s)"));
282 gtk_table_attach(GTK_TABLE(table), frame_custom, 1, 2, top, (top + JPILOT_NUM_CUSTOM_LABEL), GTK_FILL, 0, 0, 0);
284 /* Now do custom labels. */
285 vbox_custom = gtk_vbox_new (FALSE, 8);
286 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
287 hlbox = gtk_hbox_new( FALSE, 0 );
288 custom_check[i] = gtk_check_button_new();
289 custom_label[i] = gtk_label_new( "" );
290 gtk_box_pack_start( GTK_BOX(hlbox), custom_check[i], FALSE, FALSE, 0 );
291 gtk_box_pack_start( GTK_BOX(hlbox), custom_label[i], TRUE, TRUE, 0 );
292 gtk_box_pack_start( GTK_BOX(vbox_custom), hlbox, TRUE, TRUE, 0 );
293 gtk_misc_set_alignment(GTK_MISC(custom_label[i]), 0, 0.5);
294 top++;
296 gtk_container_add (GTK_CONTAINER (frame_custom), vbox_custom);
297 gtk_container_set_border_width( GTK_CONTAINER(vbox_custom), 8 );
299 /* Status line */
300 hsbox = gtk_hbox_new(FALSE, 0);
301 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
302 statusbar = gtk_statusbar_new();
303 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
305 /* Button panel */
306 gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
307 &ok_btn, GTK_STOCK_OK,
308 NULL, NULL);
309 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
310 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
311 gtk_widget_grab_default(ok_btn);
313 hsep = gtk_hseparator_new();
314 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
316 g_signal_connect(G_OBJECT(ok_btn), "clicked",
317 G_CALLBACK(edit_jpilot_ok), cancelled);
318 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
319 G_CALLBACK(edit_jpilot_cancel), cancelled);
320 g_signal_connect(G_OBJECT(file_btn), "clicked",
321 G_CALLBACK(edit_jpilot_file_select), NULL);
322 g_signal_connect(G_OBJECT(check_btn), "clicked",
323 G_CALLBACK(edit_jpilot_file_check), NULL);
325 gtk_widget_show_all(vbox);
327 jpilotedit.window = window;
328 jpilotedit.name_entry = name_entry;
329 jpilotedit.file_entry = file_entry;
330 jpilotedit.ok_btn = ok_btn;
331 jpilotedit.cancel_btn = cancel_btn;
332 jpilotedit.statusbar = statusbar;
333 jpilotedit.status_cid = gtk_statusbar_get_context_id( GTK_STATUSBAR(statusbar), "Edit JPilot Dialog" );
334 for( i = 0; i < JPILOT_NUM_CUSTOM_LABEL; i++ ) {
335 jpilotedit.custom_check[i] = custom_check[i];
336 jpilotedit.custom_label[i] = custom_label[i];
340 AdapterDSource *addressbook_edit_jpilot( AddressIndex *addrIndex, AdapterDSource *ads ) {
341 static gboolean cancelled;
342 gchar *sName;
343 gchar *sFile;
344 gchar *sFSFile;
345 AddressDataSource *ds = NULL;
346 JPilotFile *jpf = NULL;
347 gboolean fin;
349 if( ! jpilotedit.window )
350 addressbook_edit_jpilot_create(&cancelled);
351 gtk_widget_grab_focus(jpilotedit.ok_btn);
352 gtk_widget_grab_focus(jpilotedit.name_entry);
353 gtk_widget_show(jpilotedit.window);
354 manage_window_set_transient(GTK_WINDOW(jpilotedit.window));
355 gtk_window_set_modal(GTK_WINDOW(jpilotedit.window), TRUE);
356 edit_jpilot_status_show( "" );
357 if( ads ) {
358 ds = ads->dataSource;
359 jpf = ds->rawDataSource;
360 if ( jpilot_get_name( jpf ) )
361 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry),
362 jpilot_get_name( jpf ) );
363 if (jpf->path)
364 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), jpf->path);
365 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Edit JPilot Entry"));
366 edit_jpilot_fill_check_box( jpf );
368 else {
369 gchar *guessFile = jpilot_find_pilotdb();
370 gtk_entry_set_text(GTK_ENTRY(jpilotedit.name_entry), ADDRESSBOOK_GUESS_JPILOT );
371 gtk_entry_set_text(GTK_ENTRY(jpilotedit.file_entry), guessFile );
372 gtk_window_set_title( GTK_WINDOW(jpilotedit.window), _("Add New JPilot Entry"));
373 edit_jpilot_fill_check_box_new();
374 if( *guessFile != '\0' ) {
375 edit_jpilot_file_check();
379 gtk_main();
380 gtk_widget_hide(jpilotedit.window);
381 gtk_window_set_modal(GTK_WINDOW(jpilotedit.window), FALSE);
382 if (cancelled == TRUE) return NULL;
384 fin = FALSE;
385 sName = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.name_entry), 0, -1 );
386 sFile = gtk_editable_get_chars( GTK_EDITABLE(jpilotedit.file_entry), 0, -1 );
387 sFSFile = conv_filename_from_utf8( sFile );
388 if( *sName == '\0' ) fin = TRUE;
389 if( *sFile == '\0' ) fin = TRUE;
390 if( ! sFSFile || *sFSFile == '\0' ) fin = TRUE;
392 if( ! fin ) {
393 if( ! ads ) {
394 jpf = jpilot_create();
395 ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_JPILOT, jpf );
396 ads = addressbook_create_ds_adapter( ds, ADDR_JPILOT, NULL );
398 addressbook_ads_set_name( ads, sName );
399 jpilot_set_name( jpf, sName );
400 jpilot_set_file( jpf, sFSFile );
401 edit_jpilot_read_check_box( jpf );
403 g_free( sFSFile );
404 g_free( sFile );
405 g_free( sName );
407 return ads;
410 #endif /* USE_JPILOT */
413 * End of Source.