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 g_signal_connect(G_OBJECT(window
), "delete_event",
152 G_CALLBACK(edit_book_delete_event
),
154 g_signal_connect(G_OBJECT(window
), "key_press_event",
155 G_CALLBACK(edit_book_key_pressed
),
158 vbox
= gtk_vbox_new(FALSE
, 8);
159 gtk_container_add(GTK_CONTAINER(window
), vbox
);
160 gtk_container_set_border_width( GTK_CONTAINER(vbox
), 0 );
162 table
= gtk_table_new(2, 3, FALSE
);
163 gtk_box_pack_start(GTK_BOX(vbox
), table
, FALSE
, FALSE
, 0);
164 gtk_container_set_border_width( GTK_CONTAINER(table
), 8 );
165 gtk_table_set_row_spacings(GTK_TABLE(table
), 8);
166 gtk_table_set_col_spacings(GTK_TABLE(table
), 8 );
170 label
= gtk_label_new(_("Name"));
171 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, top
, (top
+ 1), GTK_FILL
, 0, 0, 0);
172 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
174 name_entry
= gtk_entry_new();
175 gtk_table_attach(GTK_TABLE(table
), name_entry
, 1, 2, top
, (top
+ 1), GTK_EXPAND
|GTK_SHRINK
|GTK_FILL
, 0, 0, 0);
177 check_btn
= gtk_button_new_with_label( _(" Check File "));
178 gtk_table_attach(GTK_TABLE(table
), check_btn
, 2, 3, top
, (top
+ 1), GTK_FILL
, 0, 3, 0);
182 label
= gtk_label_new(_("File"));
183 gtk_table_attach(GTK_TABLE(table
), label
, 0, 1, top
, (top
+ 1), GTK_FILL
, 0, 0, 0);
184 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
186 file_label
= gtk_label_new( "" );
187 gtk_misc_set_alignment(GTK_MISC(file_label
), 0, 0.5);
188 gtk_table_attach(GTK_TABLE(table
), file_label
, 1, 2, top
, (top
+ 1), GTK_EXPAND
|GTK_SHRINK
|GTK_FILL
, 0, 0, 0);
190 /* file_btn = gtk_button_new_with_label( _(" ... ")); */
191 /* gtk_table_attach(GTK_TABLE(table), file_btn, 2, 3, top, (top + 1), GTK_FILL, 0, 3, 0); */
194 hsbox
= gtk_hbox_new(FALSE
, 0);
195 gtk_box_pack_end(GTK_BOX(vbox
), hsbox
, FALSE
, FALSE
, BORDER_WIDTH
);
196 statusbar
= gtk_statusbar_new();
197 gtk_box_pack_start(GTK_BOX(hsbox
), statusbar
, TRUE
, TRUE
, BORDER_WIDTH
);
200 gtkut_stock_button_set_create(&hbbox
, &cancel_btn
, GTK_STOCK_CANCEL
,
201 &ok_btn
, GTK_STOCK_OK
,
203 gtk_box_pack_end(GTK_BOX(vbox
), hbbox
, FALSE
, FALSE
, 0);
204 gtk_container_set_border_width( GTK_CONTAINER(hbbox
), 0 );
205 gtk_widget_grab_default(ok_btn
);
207 hsep
= gtk_hseparator_new();
208 gtk_box_pack_end(GTK_BOX(vbox
), hsep
, FALSE
, FALSE
, 0);
210 g_signal_connect(G_OBJECT(name_entry
), "focus_in_event",
211 G_CALLBACK(edit_book_name_focus
), NULL
);
212 g_signal_connect(G_OBJECT(ok_btn
), "clicked",
213 G_CALLBACK(edit_book_ok
), cancelled
);
214 g_signal_connect(G_OBJECT(cancel_btn
), "clicked",
215 G_CALLBACK(edit_book_cancel
), cancelled
);
216 /* g_signal_connect(G_OBJECT(file_btn), "clicked", */
217 /* G_CALLBACK(edit_book_file_select), NULL); */
218 g_signal_connect(G_OBJECT(check_btn
), "clicked",
219 G_CALLBACK(edit_book_file_check
), NULL
);
221 gtk_widget_show_all(vbox
);
223 addrbookedit_dlg
.window
= window
;
224 addrbookedit_dlg
.name_entry
= name_entry
;
225 addrbookedit_dlg
.file_label
= file_label
;
226 addrbookedit_dlg
.ok_btn
= ok_btn
;
227 addrbookedit_dlg
.cancel_btn
= cancel_btn
;
228 addrbookedit_dlg
.check_btn
= check_btn
;
229 /* addrbookedit_dlg.file_btn = file_btn; */
230 addrbookedit_dlg
.statusbar
= statusbar
;
231 addrbookedit_dlg
.status_cid
= gtk_statusbar_get_context_id(
232 GTK_STATUSBAR(statusbar
), "Edit Addressbook Dialog" );
235 AdapterDSource
*addressbook_edit_book( AddressIndex
*addrIndex
, AdapterDSource
*ads
) {
236 static gboolean cancelled
;
238 AddressDataSource
*ds
= NULL
;
239 AddressBookFile
*abf
;
241 gboolean newBook
= FALSE
;
242 gchar
*newFile
= NULL
;
244 if (!addrbookedit_dlg
.window
)
245 addressbook_edit_book_create(&cancelled
);
246 gtk_widget_grab_focus(addrbookedit_dlg
.ok_btn
);
247 gtk_widget_grab_focus(addrbookedit_dlg
.name_entry
);
248 gtk_widget_show(addrbookedit_dlg
.window
);
249 manage_window_set_transient(GTK_WINDOW(addrbookedit_dlg
.window
));
250 gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg
.window
), TRUE
);
252 edit_book_status_show( "" );
253 gtk_label_set_text( GTK_LABEL(addrbookedit_dlg
.file_label
), "" );
255 ds
= ads
->dataSource
;
256 abf
= ds
->rawDataSource
;
257 if ( addrbook_get_name( abf
) )
258 gtk_entry_set_text(GTK_ENTRY(addrbookedit_dlg
.name_entry
),
259 addrbook_get_name( abf
) );
261 gtk_label_set_text(GTK_LABEL(addrbookedit_dlg
.file_label
), abf
->fileName
);
262 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg
.window
), _("Edit Addressbook"));
263 edit_book_enable_buttons( TRUE
);
268 abf
= addrbook_create_book();
269 addrbook_set_path( abf
, addrIndex
->filePath
);
271 /* Take initial guess at file name */
272 newFile
= addrbook_guess_next_file( abf
);
274 tmp
= g_strdup_printf( "<%s>", newFile
);
275 gtk_label_set_text(GTK_LABEL(addrbookedit_dlg
.file_label
), tmp
);
280 gtk_entry_set_text( GTK_ENTRY(addrbookedit_dlg
.name_entry
), ADDRESSBOOK_GUESS_BOOK
);
281 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg
.window
), _("Add New Addressbook") );
282 edit_book_enable_buttons( FALSE
);
285 addrbookedit_dlg
.bookFile
= abf
;
288 gtk_widget_hide(addrbookedit_dlg
.window
);
289 gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg
.window
), FALSE
);
290 if( cancelled
== TRUE
) {
292 addrbook_free_book( abf
);
299 sName
= gtk_editable_get_chars( GTK_EDITABLE(addrbookedit_dlg
.name_entry
), 0, -1 );
300 if( *sName
== '\0' ) fin
= TRUE
;
304 addrbook_free_book( abf
);
310 /* Get final file name in case it changed */
311 newFile
= addrbook_guess_next_file( abf
);
312 addrbook_set_file( abf
, newFile
);
314 ds
= addrindex_index_add_datasource( addrIndex
, ADDR_IF_BOOK
, abf
);
315 ads
= addressbook_create_ds_adapter( ds
, ADDR_BOOK
, NULL
);
317 addressbook_ads_set_name( ads
, sName
);
318 addrbook_set_name( abf
, sName
);
323 if( abf
) addrbook_save_data( abf
);