add spam session stats to command line output
[claws.git] / src / editbook.c
blob565d0cf8d39d44396e8d15572682b05bd2dfdecc
1 /*
2 * Claws Mail -- a GTK based, lightweight, and fast e-mail client
3 * Copyright (C) 2001-2022 the Claws Mail team and Match Grun
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/>.
20 * Edit new address book entry.
23 #ifdef HAVE_CONFIG_H
24 # include "config.h"
25 #include "claws-features.h"
26 #endif
28 #include "defs.h"
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtk.h>
35 #include "utils.h"
36 #include "prefs_common.h"
37 #include "mgutils.h"
38 #include "addressbook.h"
39 #include "addressitem.h"
40 #include "addrindex.h"
41 #include "addrbook.h"
42 #include "manage_window.h"
43 #include "gtkutils.h"
45 #define ADDRESSBOOK_GUESS_BOOK "MyAddressBook"
47 static struct _AddrBookEdit_Dlg {
48 GtkWidget *window;
49 GtkWidget *name_entry;
50 GtkWidget *file_label;
51 GtkWidget *ok_btn;
52 GtkWidget *cancel_btn;
53 GtkWidget *check_btn;
54 /* GtkWidget *file_btn; */
55 GtkWidget *statusbar;
56 gint status_cid;
57 AddressBookFile *bookFile;
58 } addrbookedit_dlg;
61 * Edit functions.
63 static void edit_book_status_show( gchar *msg ) {
64 if( addrbookedit_dlg.statusbar != NULL ) {
65 gtk_statusbar_pop( GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid );
66 if( msg ) {
67 gtk_statusbar_push(
68 GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid, msg );
70 else {
71 gtk_statusbar_push(
72 GTK_STATUSBAR(addrbookedit_dlg.statusbar), addrbookedit_dlg.status_cid, "" );
77 static void edit_book_ok( GtkWidget *widget, gboolean *cancelled ) {
78 *cancelled = FALSE;
79 gtk_main_quit();
82 static void edit_book_cancel( GtkWidget *widget, gboolean *cancelled ) {
83 *cancelled = TRUE;
84 gtk_main_quit();
87 static gint edit_book_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
88 *cancelled = TRUE;
89 gtk_main_quit();
90 return TRUE;
93 static gboolean edit_book_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
94 if (event && event->keyval == GDK_KEY_Escape) {
95 *cancelled = TRUE;
96 gtk_main_quit();
98 return FALSE;
101 static void edit_book_file_check( void ) {
102 gint t;
103 gchar *sMsg;
104 AddressBookFile *abf = addrbookedit_dlg.bookFile;
106 t = addrbook_test_read_file( abf, abf->fileName );
107 if( t == MGU_SUCCESS ) {
108 sMsg = _("File appears to be OK.");
110 else if( t == MGU_BAD_FORMAT ) {
111 sMsg = _("File does not appear to be a valid address book format.");
113 else {
114 sMsg = _("Could not read file.");
116 edit_book_status_show( sMsg );
119 static void edit_book_enable_buttons( gboolean enable ) {
120 gtk_widget_set_sensitive( addrbookedit_dlg.check_btn, enable );
121 /* gtk_widget_set_sensitive( addrbookedit_dlg.file_btn, enable ); */
124 static void edit_book_name_focus( GtkWidget *widget, GdkEventFocus *event, gpointer data) {
125 edit_book_status_show( "" );
128 static void addressbook_edit_book_create( gboolean *cancelled ) {
129 GtkWidget *window;
130 GtkWidget *vbox;
131 GtkWidget *table;
132 GtkWidget *label;
133 GtkWidget *name_entry;
134 GtkWidget *file_label;
135 GtkWidget *hbbox;
136 GtkWidget *hsep;
137 GtkWidget *ok_btn;
138 GtkWidget *cancel_btn;
139 GtkWidget *check_btn;
140 /* GtkWidget *file_btn; */
141 GtkWidget *statusbar;
142 GtkWidget *hsbox;
144 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "editbook");
145 gtk_widget_set_size_request(window, 450, -1);
146 gtk_container_set_border_width( GTK_CONTAINER(window), 0 );
147 gtk_window_set_title(GTK_WINDOW(window), _("Edit Addressbook"));
148 gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
149 gtk_window_set_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
150 g_signal_connect(G_OBJECT(window), "delete_event",
151 G_CALLBACK(edit_book_delete_event),
152 cancelled);
153 g_signal_connect(G_OBJECT(window), "key_press_event",
154 G_CALLBACK(edit_book_key_pressed),
155 cancelled);
157 vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 8);
158 gtk_container_add(GTK_CONTAINER(window), vbox);
159 gtk_container_set_border_width( GTK_CONTAINER(vbox), 0 );
161 table = gtk_grid_new();
162 gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 0);
163 gtk_container_set_border_width( GTK_CONTAINER(table), 8 );
164 gtk_grid_set_row_spacing(GTK_GRID(table), 8);
165 gtk_grid_set_column_spacing(GTK_GRID(table), 8);
167 /* First row */
168 label = gtk_label_new(_("Name"));
169 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
170 gtk_grid_attach(GTK_GRID(table), label, 0, 0, 1, 1);
172 name_entry = gtk_entry_new();
173 gtk_grid_attach(GTK_GRID(table), name_entry, 1, 0, 1, 1);
174 gtk_widget_set_hexpand(name_entry, TRUE);
175 gtk_widget_set_halign(name_entry, GTK_ALIGN_FILL);
177 check_btn = gtk_button_new_with_label( _(" Check File "));
178 gtk_grid_attach(GTK_GRID(table), check_btn, 2, 0, 1, 1);
180 /* Second row */
181 label = gtk_label_new(_("File"));
182 gtk_label_set_xalign(GTK_LABEL(label), 0.0);
183 gtk_grid_attach(GTK_GRID(table), label, 0, 1, 1, 1);
185 file_label = gtk_label_new( "" );
186 gtk_label_set_xalign(GTK_LABEL(file_label), 0.0);
187 gtk_grid_attach(GTK_GRID(table), file_label, 1, 1, 1, 1);
189 /* Status line */
190 hsbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 0);
191 gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
192 statusbar = gtk_statusbar_new();
193 gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
195 /* Button panel */
196 gtkut_stock_button_set_create(&hbbox, &cancel_btn, NULL, _("_Cancel"),
197 &ok_btn, NULL, _("_OK"),
198 NULL, NULL, NULL);
199 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
200 gtk_container_set_border_width( GTK_CONTAINER(hbbox), 0 );
201 gtk_widget_grab_default(ok_btn);
203 hsep = gtk_separator_new(GTK_ORIENTATION_HORIZONTAL);
204 gtk_box_pack_end(GTK_BOX(vbox), hsep, FALSE, FALSE, 0);
206 g_signal_connect(G_OBJECT(name_entry), "focus_in_event",
207 G_CALLBACK(edit_book_name_focus), NULL );
208 g_signal_connect(G_OBJECT(ok_btn), "clicked",
209 G_CALLBACK(edit_book_ok), cancelled);
210 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
211 G_CALLBACK(edit_book_cancel), cancelled);
212 /* g_signal_connect(G_OBJECT(file_btn), "clicked", */
213 /* G_CALLBACK(edit_book_file_select), NULL); */
214 g_signal_connect(G_OBJECT(check_btn), "clicked",
215 G_CALLBACK(edit_book_file_check), NULL);
217 gtk_widget_show_all(vbox);
219 addrbookedit_dlg.window = window;
220 addrbookedit_dlg.name_entry = name_entry;
221 addrbookedit_dlg.file_label = file_label;
222 addrbookedit_dlg.ok_btn = ok_btn;
223 addrbookedit_dlg.cancel_btn = cancel_btn;
224 addrbookedit_dlg.check_btn = check_btn;
225 /* addrbookedit_dlg.file_btn = file_btn; */
226 addrbookedit_dlg.statusbar = statusbar;
227 addrbookedit_dlg.status_cid = gtk_statusbar_get_context_id(
228 GTK_STATUSBAR(statusbar), "Edit Addressbook Dialog" );
231 AdapterDSource *addressbook_edit_book( AddressIndex *addrIndex, AdapterDSource *ads ) {
232 static gboolean cancelled;
233 gchar *sName;
234 AddressDataSource *ds = NULL;
235 AddressBookFile *abf;
236 gboolean fin;
237 gboolean newBook = FALSE;
238 gchar *newFile = NULL;
240 if (!addrbookedit_dlg.window)
241 addressbook_edit_book_create(&cancelled);
242 gtk_widget_grab_focus(addrbookedit_dlg.ok_btn);
243 gtk_widget_grab_focus(addrbookedit_dlg.name_entry);
244 gtk_widget_show(addrbookedit_dlg.window);
245 manage_window_set_transient(GTK_WINDOW(addrbookedit_dlg.window));
246 gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg.window), TRUE);
248 edit_book_status_show( "" );
249 gtk_label_set_text( GTK_LABEL(addrbookedit_dlg.file_label), "" );
250 if( ads ) {
251 ds = ads->dataSource;
252 abf = ds->rawDataSource;
253 if ( addrbook_get_name( abf ) )
254 gtk_entry_set_text(GTK_ENTRY(addrbookedit_dlg.name_entry),
255 addrbook_get_name( abf ) );
256 if( abf->fileName )
257 gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), abf->fileName);
258 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Edit Addressbook"));
259 edit_book_enable_buttons( TRUE );
261 else {
262 gchar *tmp = NULL;
263 newBook = TRUE;
264 abf = addrbook_create_book();
265 addrbook_set_path( abf, addrIndex->filePath );
267 /* Take initial guess at file name */
268 newFile = addrbook_guess_next_file( abf );
269 if( newFile ) {
270 tmp = g_strdup_printf( "<%s>", newFile );
271 gtk_label_set_text(GTK_LABEL(addrbookedit_dlg.file_label), tmp );
272 g_free( tmp );
274 g_free( newFile );
276 gtk_entry_set_text( GTK_ENTRY(addrbookedit_dlg.name_entry), ADDRESSBOOK_GUESS_BOOK );
277 gtk_window_set_title( GTK_WINDOW(addrbookedit_dlg.window), _("Add New Addressbook") );
278 edit_book_enable_buttons( FALSE );
281 addrbookedit_dlg.bookFile = abf;
283 gtk_main();
284 gtk_widget_hide(addrbookedit_dlg.window);
285 gtk_window_set_modal(GTK_WINDOW(addrbookedit_dlg.window), FALSE);
286 if( cancelled == TRUE ) {
287 if( newBook ) {
288 addrbook_free_book( abf );
289 abf = NULL;
291 return NULL;
294 fin = FALSE;
295 sName = gtk_editable_get_chars( GTK_EDITABLE(addrbookedit_dlg.name_entry), 0, -1 );
296 if( *sName == '\0' ) fin = TRUE;
298 if( fin ) {
299 if( newBook ) {
300 addrbook_free_book( abf );
301 abf = NULL;
304 else {
305 if( newBook ) {
306 /* Get final file name in case it changed */
307 newFile = addrbook_guess_next_file( abf );
308 addrbook_set_file( abf, newFile );
309 g_free( newFile );
310 ds = addrindex_index_add_datasource( addrIndex, ADDR_IF_BOOK, abf );
311 ads = addressbook_create_ds_adapter( ds, ADDR_BOOK, NULL );
313 addressbook_ads_set_name( ads, sName );
314 addrbook_set_name( abf, sName );
316 g_free( sName );
318 /* Save data */
319 if( abf ) addrbook_save_data( abf );
321 return ads;
325 * End of Source.