Fix truncated input widgets in preferences window
[claws.git] / src / addressadd.c
bloba44275cec23b09e3af823573aa65d3b6d2989dd3
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 * Add address to address book dialog.
24 #ifdef HAVE_CONFIG_H
25 # include "config.h"
26 #include "claws-features.h"
27 #endif
29 #include "defs.h"
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <gdk/gdkkeysyms.h>
34 #include <gtk/gtk.h>
36 #include "gtkutils.h"
37 #include "stock_pixmap.h"
38 #include "prefs_common.h"
39 #include "prefs_gtk.h"
40 #include "addressadd.h"
41 #ifndef USE_ALT_ADDRBOOK
42 #include "addritem.h"
43 #include "addrbook.h"
44 #include "addrindex.h"
45 #include "ldapserver.h"
46 #include "ldapupdate.h"
47 #else
48 #include "addressbook-dbus.h"
49 #endif
50 #include "manage_window.h"
51 #include "alertpanel.h"
53 #ifndef USE_ALT_ADDRBOOK
54 typedef struct {
55 AddressBookFile *book;
56 ItemFolder *folder;
57 } FolderInfo;
58 #else
59 typedef struct {
60 gchar* book;
61 } FolderInfo;
62 #endif
64 static struct _AddressAdd_dlg {
65 GtkWidget *window;
66 GtkWidget *picture;
67 GtkWidget *entry_name;
68 GtkWidget *label_address;
69 GtkWidget *entry_remarks;
70 GtkWidget *tree_folder;
71 GtkWidget *ok_btn;
72 GtkWidget *cancel_btn;
73 FolderInfo *fiSelected;
74 } addressadd_dlg;
76 static GdkPixbuf *folderXpm;
77 static GdkPixbuf *bookXpm;
79 static gboolean addressadd_cancelled;
81 #ifndef USE_ALT_ADDRBOOK
82 static FolderInfo *addressadd_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
84 FolderInfo *fi = g_new0( FolderInfo, 1 );
85 fi->book = abf;
86 fi->folder = folder;
87 return fi;
89 #else
90 static FolderInfo *addressadd_create_folderinfo(gchar* book) {
91 FolderInfo *fi = g_new0( FolderInfo, 1 );
92 fi->book = book;
93 return fi;
95 #endif
97 #ifndef USE_ALT_ADDRBOOK
98 static void addressadd_free_folderinfo( FolderInfo *fi ) {
99 fi->book = NULL;
100 fi->folder = NULL;
101 g_free( fi );
103 #else
104 static void addressadd_free_folderinfo( FolderInfo *fi ) {
105 fi->book = NULL;
106 g_free( fi );
108 #endif
110 static gint addressadd_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
111 addressadd_cancelled = TRUE;
112 gtk_main_quit();
113 return TRUE;
116 static gboolean addressadd_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
117 if (event && event->keyval == GDK_KEY_Escape) {
118 addressadd_cancelled = TRUE;
119 gtk_main_quit();
121 return FALSE;
124 static void addressadd_ok( GtkWidget *widget, gboolean *cancelled ) {
125 addressadd_cancelled = FALSE;
126 gtk_main_quit();
129 static void addressadd_cancel( GtkWidget *widget, gboolean *cancelled ) {
130 addressadd_cancelled = TRUE;
131 gtk_main_quit();
134 static void addressadd_folder_select( GtkCMCTree *ctree, GtkCMCTreeNode *node,
135 gint column, gpointer data )
137 addressadd_dlg.fiSelected = gtk_cmctree_node_get_row_data( ctree, node );
140 static gboolean addressadd_tree_button( GtkCMCTree *ctree, GdkEventButton *event, gpointer data ) {
141 if( ! event )
142 return FALSE;
143 if( event->button == 1 ) {
144 /* Handle double click */
145 if( event->type == GDK_2BUTTON_PRESS ) {
146 addressadd_cancelled = FALSE;
147 gtk_main_quit();
151 return FALSE;
154 static void addressadd_size_allocate_cb(GtkWidget *widget,
155 GtkAllocation *allocation)
157 cm_return_if_fail(allocation != NULL);
159 prefs_common.addressaddwin_width = allocation->width;
160 prefs_common.addressaddwin_height = allocation->height;
163 static void addressadd_create( void ) {
164 GtkWidget *window;
165 GtkWidget *vbox, *hbox;
166 GtkWidget *frame;
167 GtkWidget *table;
168 GtkWidget *label;
169 GtkWidget *picture;
170 GtkWidget *entry_name;
171 GtkWidget *label_addr;
172 GtkWidget *entry_rems;
173 GtkWidget *tree_folder;
174 GtkWidget *vlbox;
175 GtkWidget *tree_win;
176 GtkWidget *hbbox;
177 GtkWidget *ok_btn;
178 GtkWidget *cancel_btn;
179 gint top;
180 static GdkGeometry geometry;
181 gchar *titles[1];
183 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "addressadd");
184 gtk_container_set_border_width( GTK_CONTAINER(window), VBOX_BORDER );
185 gtk_window_set_title( GTK_WINDOW(window), _("Add to address book") );
186 gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
187 g_signal_connect( G_OBJECT(window), "delete_event",
188 G_CALLBACK(addressadd_delete_event), NULL );
189 g_signal_connect( G_OBJECT(window), "key_press_event",
190 G_CALLBACK(addressadd_key_pressed), NULL );
191 g_signal_connect(G_OBJECT(window), "size_allocate",
192 G_CALLBACK(addressadd_size_allocate_cb), NULL);
194 hbox = gtk_hbox_new(FALSE, 10);
195 gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
196 vbox = gtk_vbox_new(FALSE, 8);
197 gtk_container_add(GTK_CONTAINER(window), vbox);
199 picture = gtk_image_new();
200 gtk_box_pack_start(GTK_BOX(hbox), picture, FALSE, FALSE, 0);
202 table = gtk_table_new(3, 2, FALSE);
203 gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
204 gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
205 gtk_table_set_col_spacings(GTK_TABLE(table), HSPACING_NARROW);
207 frame = gtk_frame_new(_("Contact"));
208 gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
209 gtk_container_add(GTK_CONTAINER(frame), hbox);
210 gtk_container_set_border_width( GTK_CONTAINER(frame), 4);
211 gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);
213 /* First row */
214 top = 0;
215 label = gtk_label_new(_("Name"));
216 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
217 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
219 entry_name = gtk_entry_new();
220 gtk_widget_set_size_request(entry_name, 150, -1);
221 gtk_entry_set_text (GTK_ENTRY(entry_name),"");
223 gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND , 0, 0, 0);
225 /* Second row */
226 top = 1;
227 label = gtk_label_new(_("Address"));
228 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
229 gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
231 label_addr = gtk_label_new("");
232 gtk_widget_set_size_request(label_addr, 150, -1);
233 gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
234 gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
236 /* Third row */
237 top = 2;
238 label = gtk_label_new(_("Remarks"));
239 gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
240 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
242 entry_rems = gtk_entry_new();
243 gtk_widget_set_size_request(entry_rems, 150, -1);
244 gtk_table_attach(GTK_TABLE(table), entry_rems, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
246 /* Address book/folder tree */
247 vlbox = gtk_vbox_new(FALSE, VBOX_BORDER);
248 gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
249 gtk_container_set_border_width( GTK_CONTAINER(vlbox), 4);
251 tree_win = gtk_scrolled_window_new( NULL, NULL );
252 gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
253 GTK_POLICY_AUTOMATIC,
254 GTK_POLICY_AUTOMATIC );
255 gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
257 titles[0] = _( "Select Address Book Folder") ;
259 tree_folder = gtk_sctree_new_with_titles( 1, 0, titles );
260 gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
261 gtk_cmclist_column_titles_show( GTK_CMCLIST(tree_folder) );
262 gtk_cmctree_set_line_style(GTK_CMCTREE(tree_folder), GTK_CMCTREE_LINES_NONE);
263 gtk_cmctree_set_expander_style(GTK_CMCTREE(tree_folder),
264 GTK_CMCTREE_EXPANDER_TRIANGLE);
265 gtk_sctree_set_stripes(GTK_SCTREE(tree_folder), prefs_common.use_stripes_everywhere);
266 gtk_cmclist_set_selection_mode( GTK_CMCLIST(tree_folder), GTK_SELECTION_BROWSE );
267 gtk_cmctree_set_indent( GTK_CMCTREE(tree_folder), CTREE_INDENT );
268 gtk_cmclist_set_auto_sort( GTK_CMCLIST(tree_folder), TRUE );
270 /* Button panel */
271 gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
272 &ok_btn, GTK_STOCK_OK,
273 NULL, NULL);
274 gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
275 gtk_container_set_border_width( GTK_CONTAINER(hbbox), HSPACING_NARROW );
276 gtk_widget_grab_default(ok_btn);
278 g_signal_connect(G_OBJECT(ok_btn), "clicked",
279 G_CALLBACK(addressadd_ok), NULL);
280 g_signal_connect(G_OBJECT(cancel_btn), "clicked",
281 G_CALLBACK(addressadd_cancel), NULL);
282 g_signal_connect(G_OBJECT(tree_folder), "tree_select_row",
283 G_CALLBACK(addressadd_folder_select), NULL);
284 g_signal_connect(G_OBJECT(tree_folder), "button_press_event",
285 G_CALLBACK(addressadd_tree_button), NULL);
287 if (!geometry.min_height) {
288 geometry.min_width = 300;
289 geometry.min_height = 350;
292 gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
293 GDK_HINT_MIN_SIZE);
294 gtk_widget_set_size_request(window, prefs_common.addressaddwin_width,
295 prefs_common.addressaddwin_height);
297 addressadd_dlg.window = window;
298 addressadd_dlg.picture = picture;
299 addressadd_dlg.entry_name = entry_name;
300 addressadd_dlg.label_address = label_addr;
301 addressadd_dlg.entry_remarks = entry_rems;
302 addressadd_dlg.tree_folder = tree_folder;
303 addressadd_dlg.ok_btn = ok_btn;
304 addressadd_dlg.cancel_btn = cancel_btn;
306 gtk_widget_show_all( window );
308 stock_pixbuf_gdk(STOCK_PIXMAP_BOOK, &bookXpm );
309 stock_pixbuf_gdk(STOCK_PIXMAP_DIR_OPEN, &folderXpm );
312 #ifndef USE_ALT_ADDRBOOK
313 static void addressadd_load_folder( GtkCMCTreeNode *parentNode, ItemFolder *parentFolder,
314 FolderInfo *fiParent )
316 GtkCMCTree *tree = GTK_CMCTREE( addressadd_dlg.tree_folder );
317 GList *list;
318 ItemFolder *folder;
319 gchar *fName;
320 gchar **name;
321 GtkCMCTreeNode *node;
322 FolderInfo *fi;
324 list = parentFolder->listFolder;
325 while( list ) {
326 folder = list->data;
327 fName = g_strdup( ADDRITEM_NAME(folder) );
328 name = &fName;
329 node = gtk_sctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
330 folderXpm, folderXpm,
331 FALSE, TRUE );
332 g_free( fName );
333 fi = addressadd_create_folderinfo( fiParent->book, folder );
334 gtk_cmctree_node_set_row_data_full( tree, node, fi,
335 ( GDestroyNotify ) addressadd_free_folderinfo );
336 addressadd_load_folder( node, folder, fi );
337 list = g_list_next( list );
341 static void addressadd_load_data( AddressIndex *addrIndex ) {
342 AddressDataSource *ds;
343 GList *list, *nodeDS;
344 gchar **name;
345 gchar *dsName;
346 ItemFolder *rootFolder;
347 AddressBookFile *abf;
348 FolderInfo *fi;
349 GtkCMCTree *tree = GTK_CMCTREE( addressadd_dlg.tree_folder );
350 GtkCMCTreeNode *node;
352 gtk_cmclist_clear( GTK_CMCLIST( tree ) );
353 list = addrindex_get_interface_list( addrIndex );
354 while( list ) {
355 AddressInterface *ainterface = list->data;
356 if( ainterface->type == ADDR_IF_BOOK ||
357 ainterface->type == ADDR_IF_LDAP ) {
358 nodeDS = ainterface->listSource;
359 while( nodeDS ) {
360 ds = nodeDS->data;
361 dsName = g_strdup( addrindex_ds_get_name( ds ) );
363 /* Read address book */
364 if( ! addrindex_ds_get_read_flag( ds ) ) {
365 addrindex_ds_read_data( ds );
368 /* Add node for address book */
369 abf = ds->rawDataSource;
370 name = &dsName;
371 node = gtk_cmctree_insert_node( tree, NULL, NULL,
372 name, FOLDER_SPACING, bookXpm,
373 bookXpm,
374 FALSE, TRUE );
375 g_free( dsName );
377 fi = addressadd_create_folderinfo( abf, NULL );
378 gtk_cmctree_node_set_row_data_full( tree, node, fi,
379 ( GDestroyNotify ) addressadd_free_folderinfo );
381 rootFolder = addrindex_ds_get_root_folder( ds );
382 addressadd_load_folder( node, rootFolder, fi );
384 nodeDS = g_list_next( nodeDS );
387 list = g_list_next( list );
390 #else
391 static void addressadd_load_data() {
392 GSList *list;
393 gchar *name;
394 GtkCMCTree *tree = GTK_CMCTREE(addressadd_dlg.tree_folder);
395 GtkCMCTreeNode *node;
396 FolderInfo *fi = NULL;
397 GError* error = NULL;
399 gtk_cmclist_clear(GTK_CMCLIST(tree));
400 list = addressbook_dbus_get_books(&error);
401 for (; list; list = g_slist_next(list)) {
402 name = (gchar *) list->data;
403 node = gtk_cmctree_insert_node(tree, NULL, NULL,
404 &name, FOLDER_SPACING, bookXpm,
405 bookXpm, FALSE, TRUE);
406 fi = addressadd_create_folderinfo(name);
407 gtk_cmctree_node_set_row_data_full(tree, node, fi,
408 ( GDestroyNotify ) addressadd_free_folderinfo );
411 #endif
413 #ifndef USE_ALT_ADDRBOOK
414 gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name,
415 const gchar *address, const gchar *remarks, GdkPixbuf *picture ) {
416 #else
417 gboolean addressadd_selection(const gchar *name, const gchar *address,
418 const gchar *remarks, GdkPixbuf *picture ) {
419 #endif
420 gboolean retVal = FALSE;
421 #ifndef USE_ALT_ADDRBOOK
422 ItemPerson *person = NULL;
423 #endif
424 FolderInfo *fi = NULL;
425 addressadd_cancelled = FALSE;
427 if( ! addressadd_dlg.window ) addressadd_create();
429 addressadd_dlg.fiSelected = NULL;
430 #ifndef USE_ALT_ADDRBOOK
431 addressadd_load_data( addrIndex );
432 #else
433 addressadd_load_data();
434 #endif
435 gtk_cmclist_select_row( GTK_CMCLIST( addressadd_dlg.tree_folder ), 0, 0 );
436 gtk_widget_show(addressadd_dlg.window);
437 gtk_window_set_modal(GTK_WINDOW(addressadd_dlg.window), TRUE);
438 gtk_widget_grab_focus(addressadd_dlg.ok_btn);
440 manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
442 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), "" );
443 gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
444 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), "" );
445 if( name )
446 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), name );
447 if( address )
448 gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
449 if( remarks )
450 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), remarks );
451 if( picture ) {
452 gtk_image_set_from_pixbuf(GTK_IMAGE(addressadd_dlg.picture), picture);
453 gtk_widget_show(GTK_WIDGET(addressadd_dlg.picture));
454 } else {
455 gtk_widget_hide(GTK_WIDGET(addressadd_dlg.picture));
457 gtk_main();
458 gtk_widget_hide( addressadd_dlg.window );
459 gtk_window_set_modal(GTK_WINDOW(addressadd_dlg.window), FALSE);
460 if( ! addressadd_cancelled ) {
461 if( addressadd_dlg.fiSelected ) {
462 gchar *returned_name;
463 gchar *returned_remarks;
464 returned_name = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_name), 0, -1 );
465 returned_remarks = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_remarks), 0, -1 );
467 fi = addressadd_dlg.fiSelected;
469 #ifndef USE_ALT_ADDRBOOK
470 person = addrbook_add_contact( fi->book, fi->folder,
471 returned_name,
472 address,
473 returned_remarks);
475 if (person != NULL) {
476 person->status = ADD_ENTRY;
478 if (picture) {
479 GError *error = NULL;
480 gchar *name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S,
481 ADDRITEM_ID(person), ".png", NULL );
482 gdk_pixbuf_save(picture, name, "png", &error, NULL);
483 if (error) {
484 g_warning("Failed to save image: %s",
485 error->message);
486 g_error_free(error);
488 addritem_person_set_picture( person, ADDRITEM_ID(person) ) ;
489 g_free( name );
492 #else
493 ContactData* contact = g_new0(ContactData, 1);
494 GError* error = NULL;
496 if (returned_name)
497 contact->cn = g_strdup(returned_name);
498 else
499 contact->cn = g_strdup(address);
501 contact->name = g_strdup(returned_name);
502 contact->email = g_strdup(address);
503 contact->remarks = g_strdup(returned_remarks);
504 contact->book = g_strdup(fi->book);
505 contact->picture = picture;
507 if (addressbook_dbus_add_contact(contact, &error) == 0) {
508 debug_print("Added to addressbook:\n%s\n%s\n%s\n%s\n",
509 returned_name, address, returned_remarks, fi->book);
510 retVal = TRUE;
512 else {
513 retVal = FALSE;
514 if (error) {
515 GtkWidget* dialog = gtk_message_dialog_new (
516 GTK_WINDOW(addressadd_dlg.window),
517 GTK_DIALOG_DESTROY_WITH_PARENT,
518 GTK_MESSAGE_ERROR,
519 GTK_BUTTONS_CLOSE,
520 "%s", error->message);
521 gtk_dialog_run (GTK_DIALOG (dialog));
522 gtk_widget_destroy (dialog);
523 g_error_free(error);
526 contact_data_free(&contact);
527 #endif
528 #ifdef USE_LDAP
529 if (person != NULL && fi->book->type == ADBOOKTYPE_LDAP) {
530 LdapServer *server = (LdapServer *) fi->book;
531 ldapsvr_set_modified(server, TRUE);
532 ldapsvr_update_book(server, person);
533 if (server->retVal != LDAPRC_SUCCESS) {
534 alertpanel( _("Add address(es)"),
535 _("Can't add the specified address"),
536 GTK_STOCK_CLOSE, NULL, NULL );
537 return server->retVal;
540 #endif
541 g_free(returned_name);
542 g_free(returned_remarks);
543 #ifndef USE_ALT_ADDRBOOK
544 if( person ) retVal = TRUE;
545 #endif
549 gtk_cmclist_clear( GTK_CMCLIST( addressadd_dlg.tree_folder ) );
551 return retVal;
555 * End of Source.