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.
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"
58 #include "manage_window.h"
60 #define ADDRESSBOOK_GUESS_JPILOT "MyJPilot"
61 #define JPILOT_NUM_CUSTOM_LABEL 4
63 static struct _JPilotEdit
{
65 GtkWidget
*name_entry
;
66 GtkWidget
*file_entry
;
67 GtkWidget
*custom_check
[JPILOT_NUM_CUSTOM_LABEL
];
68 GtkWidget
*custom_label
[JPILOT_NUM_CUSTOM_LABEL
];
70 GtkWidget
*cancel_btn
;
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
);
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
) {
93 static gboolean
edit_jpilot_key_pressed( GtkWidget
*widget
, GdkEventKey
*event
, gboolean
*cancelled
) {
94 if (event
&& event
->keyval
== GDK_Escape
) {
101 static void edit_jpilot_ok( GtkWidget
*widget
, gboolean
*cancelled
) {
106 static void edit_jpilot_cancel( GtkWidget
*widget
, gboolean
*cancelled
) {
111 static void edit_jpilot_fill_check_box( JPilotFile
*jpf
) {
113 GList
*node
, *customLbl
= NULL
;
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
] ), "" );
123 customLbl
= jpilot_load_custom_label( jpf
, customLbl
);
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
);
132 if( i
>= JPILOT_NUM_CUSTOM_LABEL
) done
= TRUE
;
133 node
= g_list_next( node
);
139 mgu_free_dlist( customLbl
);
143 static void edit_jpilot_fill_check_box_new() {
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
) {
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 ) {
171 sFile
= gtk_editable_get_chars( GTK_EDITABLE(jpilotedit
.file_entry
), 0, -1 );
173 sFSFile
= conv_filename_from_utf8( sFile
);
174 if( sFSFile
&& *sFSFile
!= '\0' ) {
175 /* Attempt to read file */
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
);
191 /* Clear all check boxes */
192 edit_jpilot_fill_check_box_new();
195 /* Display appropriate message */
196 if( t
== MGU_SUCCESS
) {
199 else if( t
== MGU_BAD_FORMAT
|| t
== MGU_OO_MEMORY
) {
200 sMsg
= _("File does not appear to be JPilot format.");
203 sMsg
= _("Could not read file.");
205 edit_jpilot_status_show( sMsg
);
208 static void edit_jpilot_file_select( void ) {
212 sFile
= filesel_select_file_open( _("Select JPilot File"), NULL
);
215 sUTF8File
= conv_filename_to_utf8( sFile
);
216 gtk_entry_set_text( GTK_ENTRY(jpilotedit
.file_entry
), sUTF8File
);
219 edit_jpilot_file_check();
223 static void addressbook_edit_jpilot_create( gboolean
*cancelled
) {
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
];
238 GtkWidget
*cancel_btn
;
239 GtkWidget
*check_btn
;
242 GtkWidget
*statusbar
;
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
),
254 g_signal_connect(G_OBJECT(window
), "key_press_event",
255 G_CALLBACK(edit_jpilot_key_pressed
),
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);
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);
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);
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);
309 gtk_container_add (GTK_CONTAINER (frame_custom
), vbox_custom
);
310 gtk_container_set_border_width( GTK_CONTAINER(vbox_custom
), 8 );
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
);
319 gtkut_stock_button_set_create(&hbbox
, &cancel_btn
, GTK_STOCK_CANCEL
,
320 &ok_btn
, GTK_STOCK_OK
,
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
;
358 AddressDataSource
*ds
= NULL
;
359 JPilotFile
*jpf
= NULL
;
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( "" );
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
) );
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
);
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();
393 gtk_widget_hide(jpilotedit
.window
);
394 if (cancelled
== TRUE
) return NULL
;
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
;
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
);
422 #endif /* USE_JPILOT */