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 new address book entry.
26 #include "claws-features.h"
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
37 #include "prefs_common.h"
39 #include "addressbook.h"
40 #include "addressitem.h"
41 #include "addrindex.h"
43 #include "manage_window.h"
46 #define ADDRESSBOOK_GUESS_BOOK "MyAddressBook"
48 static struct _AddrBookEdit_Dlg
{
50 GtkWidget
*name_entry
;
51 GtkWidget
*file_label
;
53 GtkWidget
*cancel_btn
;
55 /* GtkWidget *file_btn; */
58 AddressBookFile
*bookFile
;
64 static void edit_book_status_show( gchar
*msg
) {
65 if( addrbookedit_dlg
.statusbar
!= NULL
) {
66 gtk_statusbar_pop( GTK_STATUSBAR(addrbookedit_dlg
.statusbar
), addrbookedit_dlg
.status_cid
);
69 GTK_STATUSBAR(addrbookedit_dlg
.statusbar
), addrbookedit_dlg
.status_cid
, msg
);
73 GTK_STATUSBAR(addrbookedit_dlg
.statusbar
), addrbookedit_dlg
.status_cid
, "" );
78 static void edit_book_ok( GtkWidget
*widget
, gboolean
*cancelled
) {
83 static void edit_book_cancel( GtkWidget
*widget
, gboolean
*cancelled
) {
88 static gint
edit_book_delete_event( GtkWidget
*widget
, GdkEventAny
*event
, gboolean
*cancelled
) {
94 static gboolean
edit_book_key_pressed( GtkWidget
*widget
, GdkEventKey
*event
, gboolean
*cancelled
) {
95 if (event
&& event
->keyval
== GDK_KEY_Escape
) {
102 static void edit_book_file_check( void ) {
105 AddressBookFile
*abf
= addrbookedit_dlg
.bookFile
;
107 t
= addrbook_test_read_file( abf
, abf
->fileName
);
108 if( t
== MGU_SUCCESS
) {
109 sMsg
= _("File appears to be OK.");
111 else if( t
== MGU_BAD_FORMAT
) {
112 sMsg
= _("File does not appear to be a valid address book format.");
115 sMsg
= _("Could not read file.");
117 edit_book_status_show( sMsg
);
120 static void edit_book_enable_buttons( gboolean enable
) {
121 gtk_widget_set_sensitive( addrbookedit_dlg
.check_btn
, enable
);
122 /* gtk_widget_set_sensitive( addrbookedit_dlg.file_btn, enable ); */
125 static void edit_book_name_focus( GtkWidget
*widget
, GdkEventFocus
*event
, gpointer data
) {
126 edit_book_status_show( "" );
129 static void addressbook_edit_book_create( gboolean
*cancelled
) {
134 GtkWidget
*name_entry
;
135 GtkWidget
*file_label
;
139 GtkWidget
*cancel_btn
;
140 GtkWidget
*check_btn
;
141 /* GtkWidget *file_btn; */
142 GtkWidget
*statusbar
;
146 window
= gtkut_window_new(GTK_WINDOW_TOPLEVEL
, "editbook");
147 gtk_widget_set_size_request(window
, 450, -1);
148 gtk_container_set_border_width( GTK_CONTAINER(window
), 0 );
149 gtk_window_set_title(GTK_WINDOW(window
), _("Edit Addressbook"));
150 gtk_window_set_position(GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
151 gtk_window_set_type_hint(GTK_WINDOW(window
), GDK_WINDOW_TYPE_HINT_DIALOG
);
152 g_signal_connect(G_OBJECT(window
), "delete_event",
153 G_CALLBACK(edit_book_delete_event
),
155 g_signal_connect(G_OBJECT(window
), "key_press_event",
156 G_CALLBACK(edit_book_key_pressed
),
159 vbox
= gtk_vbox_new(FALSE
, 8);
160 gtk_container_add(GTK_CONTAINER(window
), vbox
);
161 gtk_container_set_border_width( GTK_CONTAINER(vbox
), 0 );
163 table
= gtk_table_new(2, 3, FALSE
);
164 gtk_box_pack_start(GTK_BOX(vbox
), table
, FALSE
, FALSE
, 0);
165 gtk_container_set_border_width( GTK_CONTAINER(table
), 8 );
166 gtk_table_set_row_spacings(GTK_TABLE(table
), 8);
167 gtk_table_set_col_spacings(GTK_TABLE(table
), 8 );
171 label
= gtk_label_new(_("Name"));
172 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, top
, (top
+ 1), GTK_FILL
, 0, 0, 0);
173 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
175 name_entry
= gtk_entry_new();
176 gtk_table_attach(GTK_TABLE(table
), name_entry
, 1, 2, top
, (top
+ 1), GTK_EXPAND
|GTK_SHRINK
|GTK_FILL
, 0, 0, 0);
178 check_btn
= gtk_button_new_with_label( _(" Check File "));
179 gtk_table_attach(GTK_TABLE(table
), check_btn
, 2, 3, top
, (top
+ 1), GTK_FILL
, 0, 3, 0);
183 label
= gtk_label_new(_("File"));
184 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, top
, (top
+ 1), GTK_FILL
, 0, 0, 0);
185 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
187 file_label
= gtk_label_new( "" );
188 gtk_misc_set_alignment(GTK_MISC(file_label
), 0, 0.5);
189 gtk_table_attach(GTK_TABLE(table
), file_label
, 1, 2, top
, (top
+ 1), GTK_EXPAND
|GTK_SHRINK
|GTK_FILL
, 0, 0, 0);
191 /* file_btn = gtk_button_new_with_label( _(" ... ")); */
192 /* gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0); */
195 hsbox
= gtk_hbox_new(FALSE
, 0);
196 gtk_box_pack_end(GTK_BOX(vbox
), hsbox
, FALSE
, FALSE
, BORDER_WIDTH
);
197 statusbar
= gtk_statusbar_new();
198 gtk_box_pack_start(GTK_BOX(hsbox
), statusbar
, TRUE
, TRUE
, BORDER_WIDTH
);
201 gtkut_stock_button_set_create(&hbbox
, &cancel_btn
, GTK_STOCK_CANCEL
,
202 &ok_btn
, GTK_STOCK_OK
,
204 gtk_box_pack_end(GTK_BOX(vbox
), hbbox
, FALSE
, FALSE
, 0);
205 gtk_container_set_border_width( GTK_CONTAINER(hbbox
), 0 );
206 gtk_widget_grab_default(ok_btn
);
208 hsep
= gtk_hseparator_new();
209 gtk_box_pack_end(GTK_BOX(vbox
), hsep
, FALSE
, FALSE
, 0);
211 g_signal_connect(G_OBJECT(name_entry
), "focus_in_event",
212 G_CALLBACK(edit_book_name_focus
), NULL
);
213 g_signal_connect(G_OBJECT(ok_btn
), "clicked",
214 G_CALLBACK(edit_book_ok
), cancelled
);
215 g_signal_connect(G_OBJECT(cancel_btn
), "clicked",
216 G_CALLBACK(edit_book_cancel
), cancelled
);
217 /* g_signal_connect(G_OBJECT(file_btn), "clicked", */
218 /* G_CALLBACK(edit_book_file_select), NULL); */
219 g_signal_connect(G_OBJECT(check_btn
), "clicked",
220 G_CALLBACK(edit_book_file_check
), NULL
);
222 gtk_widget_show_all(vbox
);
224 addrbookedit_dlg
.window
= window
;
225 addrbookedit_dlg
.name_entry
= name_entry
;
226 addrbookedit_dlg
.file_label
= file_label
;
227 addrbookedit_dlg
.ok_btn
= ok_btn
;
228 addrbookedit_dlg
.cancel_btn
= cancel_btn
;
229 addrbookedit_dlg
.check_btn
= check_btn
;
230 /* addrbookedit_dlg.file_btn = file_btn; */
231 addrbookedit_dlg
.statusbar
= statusbar
;
232 addrbookedit_dlg
.status_cid
= gtk_statusbar_get_context_id(
233 GTK_STATUSBAR(statusbar
), "Edit Addressbook Dialog" );
236 AdapterDSource
*addressbook_edit_book( AddressIndex
*addrIndex
, AdapterDSource
*ads
) {
237 static gboolean cancelled
;
239 AddressDataSource
*ds
= NULL
;
240 AddressBookFile
*abf
;
242 gboolean newBook
= FALSE
;
243 gchar
*newFile
= NULL
;
245 if (!addrbookedit_dlg
.window
)
246 addressbook_edit_book_create(&cancelled
);
247 gtk_widget_grab_focus(addrbookedit_dlg
.ok_btn
);
248 gtk_widget_grab_focus(addrbookedit_dlg
.name_entry
);
249 gtk_widget_show(addrbookedit_dlg
.window
);
250 manage_window_set_transient(GTK_WINDOW(addrbookedit_dlg
.window
));
251 gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg
.window
), TRUE
);
253 edit_book_status_show( "" );
254 gtk_label_set_text( GTK_LABEL(addrbookedit_dlg
.file_label
), "" );
256 ds
= ads
->dataSource
;
257 abf
= ds
->rawDataSource
;
258 if ( addrbook_get_name( abf
) )
259 gtk_entry_set_text(GTK_ENTRY(addrbookedit_dlg
.name_entry
),
260 addrbook_get_name( abf
) );
262 gtk_label_set_text(GTK_LABEL(addrbookedit_dlg
.file_label
), abf
->fileName
);
263 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg
.window
), _("Edit Addressbook"));
264 edit_book_enable_buttons( TRUE
);
269 abf
= addrbook_create_book();
270 addrbook_set_path( abf
, addrIndex
->filePath
);
272 /* Take initial guess at file name */
273 newFile
= addrbook_guess_next_file( abf
);
275 tmp
= g_strdup_printf( "<%s>", newFile
);
276 gtk_label_set_text(GTK_LABEL(addrbookedit_dlg
.file_label
), tmp
);
281 gtk_entry_set_text( GTK_ENTRY(addrbookedit_dlg
.name_entry
), ADDRESSBOOK_GUESS_BOOK
);
282 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg
.window
), _("Add New Addressbook") );
283 edit_book_enable_buttons( FALSE
);
286 addrbookedit_dlg
.bookFile
= abf
;
289 gtk_widget_hide(addrbookedit_dlg
.window
);
290 gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg
.window
), FALSE
);
291 if( cancelled
== TRUE
) {
293 addrbook_free_book( abf
);
300 sName
= gtk_editable_get_chars( GTK_EDITABLE(addrbookedit_dlg
.name_entry
), 0, -1 );
301 if( *sName
== '\0' ) fin
= TRUE
;
305 addrbook_free_book( abf
);
311 /* Get final file name in case it changed */
312 newFile
= addrbook_guess_next_file( abf
);
313 addrbook_set_file( abf
, newFile
);
315 ds
= addrindex_index_add_datasource( addrIndex
, ADDR_IF_BOOK
, abf
);
316 ads
= addressbook_create_ds_adapter( ds
, ADDR_BOOK
, NULL
);
318 addressbook_ads_set_name( ads
, sName
);
319 addrbook_set_name( abf
, sName
);
324 if( abf
) addrbook_save_data( abf
);