514ed72ce0f50759a17aba3db3795f27b74d9c66
[claws.git] / src / addressadd.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2001-2007 Match Grun and the Claws Mail team
4  *
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.
9  *
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.
14  *
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/>.
17  * 
18  */
19
20 /*
21  * Add address to address book dialog.
22  */
23
24 #ifdef HAVE_CONFIG_H
25 #  include "config.h"
26 #endif
27
28 #include "defs.h"
29
30 #include <glib.h>
31 #include <glib/gi18n.h>
32 #include <gdk/gdkkeysyms.h>
33 #include <gtk/gtkwindow.h>
34 #include <gtk/gtksignal.h>
35 #include <gtk/gtkhbox.h>
36 #include <gtk/gtklabel.h>
37 #include <gtk/gtkentry.h>
38 #include <gtk/gtkhbbox.h>
39 #include <gtk/gtkbutton.h>
40
41 #include "gtkutils.h"
42 #include "stock_pixmap.h"
43 #include "prefs_common.h"
44 #include "prefs_gtk.h"
45 #include "addressadd.h"
46 #include "addritem.h"
47 #include "addrbook.h"
48 #include "addrindex.h"
49 #include "manage_window.h"
50 #include "ldapserver.h"
51 #include "ldapupdate.h"
52 #include "alertpanel.h"
53
54 typedef struct {
55         AddressBookFile *book;
56         ItemFolder      *folder;
57 } FolderInfo;
58
59 static struct _AddressAdd_dlg {
60         GtkWidget *window;
61         GtkWidget *picture;
62         GtkWidget *entry_name;
63         GtkWidget *label_address;
64         GtkWidget *entry_remarks;
65         GtkWidget *tree_folder;
66         GtkWidget *ok_btn;
67         GtkWidget *cancel_btn;
68         FolderInfo *fiSelected;
69 } addressadd_dlg;
70
71 static GdkPixmap *folderXpm;
72 static GdkBitmap *folderXpmMask;
73 static GdkPixmap *bookXpm;
74 static GdkBitmap *bookXpmMask;
75
76 static gboolean addressadd_cancelled;
77
78 static FolderInfo *addressadd_create_folderinfo( AddressBookFile *abf, ItemFolder *folder )
79 {
80         FolderInfo *fi = g_new0( FolderInfo, 1 );
81         fi->book   = abf;
82         fi->folder = folder;
83         return fi;
84 }
85
86 static void addressadd_free_folderinfo( FolderInfo *fi ) {
87         fi->book   = NULL;
88         fi->folder = NULL;
89         g_free( fi );
90 }
91
92 static gint addressadd_delete_event( GtkWidget *widget, GdkEventAny *event, gboolean *cancelled ) {
93         addressadd_cancelled = TRUE;
94         gtk_main_quit();
95         return TRUE;
96 }
97
98 static gboolean addressadd_key_pressed( GtkWidget *widget, GdkEventKey *event, gboolean *cancelled ) {
99         if (event && event->keyval == GDK_Escape) {
100                 addressadd_cancelled = TRUE;
101                 gtk_main_quit();
102         }
103         return FALSE;
104 }
105
106 static void addressadd_ok( GtkWidget *widget, gboolean *cancelled ) {
107         addressadd_cancelled = FALSE;
108         gtk_main_quit();
109 }
110
111 static void addressadd_cancel( GtkWidget *widget, gboolean *cancelled ) {
112         addressadd_cancelled = TRUE;
113         gtk_main_quit();
114 }
115
116 static void addressadd_folder_select( GtkCTree *ctree, GtkCTreeNode *node,
117                                       gint column, gpointer data )
118 {
119         addressadd_dlg.fiSelected = gtk_ctree_node_get_row_data( ctree, node );
120 }
121
122 static gboolean addressadd_tree_button( GtkCTree *ctree, GdkEventButton *event, gpointer data ) {
123         if( ! event )
124                 return FALSE;
125         if( event->button == 1 ) {
126                 /* Handle double click */
127                 if( event->type == GDK_2BUTTON_PRESS ) {
128                         addressadd_cancelled = FALSE;
129                         gtk_main_quit();
130                 }
131         }
132
133         return FALSE;
134 }
135
136 static void addressadd_size_allocate_cb(GtkWidget *widget,
137                                          GtkAllocation *allocation)
138 {
139         g_return_if_fail(allocation != NULL);
140
141         prefs_common.addressaddwin_width = allocation->width;
142         prefs_common.addressaddwin_height = allocation->height;
143 }
144
145 static void addressadd_create( void ) {
146         GtkWidget *window;
147         GtkWidget *vbox, *hbox;
148         GtkWidget *frame;
149         GtkWidget *table;
150         GtkWidget *label;
151         GtkWidget *picture;
152         GtkWidget *entry_name;
153         GtkWidget *label_addr;
154         GtkWidget *entry_rems;
155         GtkWidget *tree_folder;
156         GtkWidget *vlbox;
157         GtkWidget *tree_win;
158         GtkWidget *hbbox;
159         GtkWidget *ok_btn;
160         GtkWidget *cancel_btn;
161         gint top;
162         static GdkGeometry geometry;
163         gchar *titles[1];
164
165         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "addressadd");
166         gtk_container_set_border_width( GTK_CONTAINER(window), VBOX_BORDER );
167         gtk_window_set_title( GTK_WINDOW(window), _("Add to address book") );
168         gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
169         gtk_window_set_modal( GTK_WINDOW(window), TRUE );
170         g_signal_connect( G_OBJECT(window), "delete_event",
171                           G_CALLBACK(addressadd_delete_event), NULL );
172         g_signal_connect( G_OBJECT(window), "key_press_event",
173                           G_CALLBACK(addressadd_key_pressed), NULL );
174         g_signal_connect(G_OBJECT(window), "size_allocate",
175                          G_CALLBACK(addressadd_size_allocate_cb), NULL);
176         
177         hbox = gtk_hbox_new(FALSE, 10);
178         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
179         vbox = gtk_vbox_new(FALSE, 8);
180         gtk_container_add(GTK_CONTAINER(window), vbox);
181
182         picture = gtk_image_new();
183         gtk_box_pack_start(GTK_BOX(hbox), picture, FALSE, FALSE, 0);
184
185         table = gtk_table_new(3, 2, FALSE);
186         gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
187         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
188         gtk_table_set_col_spacings(GTK_TABLE(table), HSPACING_NARROW);
189
190         frame = gtk_frame_new(_("Contact"));
191         gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
192         gtk_container_add(GTK_CONTAINER(frame), hbox);
193         gtk_container_set_border_width( GTK_CONTAINER(frame), 4);
194         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);
195
196         /* First row */
197         top = 0;
198         label = gtk_label_new(_("Name"));
199         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
200         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
201
202         entry_name = gtk_entry_new();
203         gtk_widget_set_size_request(entry_name, 150, -1);
204         gtk_entry_set_text (GTK_ENTRY(entry_name),"");
205         
206         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND , 0, 0, 0);
207
208         /* Second row */
209         top = 1;
210         label = gtk_label_new(_("Address"));
211         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
212         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
213
214         label_addr = gtk_label_new("");
215         gtk_widget_set_size_request(label_addr, 150, -1);
216         gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
217         gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
218
219         /* Third row */
220         top = 2;
221         label = gtk_label_new(_("Remarks"));
222         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
223         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
224
225         entry_rems = gtk_entry_new();
226         gtk_widget_set_size_request(entry_rems, 150, -1);
227         gtk_table_attach(GTK_TABLE(table), entry_rems, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
228         
229         /* Address book/folder tree */
230         vlbox = gtk_vbox_new(FALSE, VBOX_BORDER);
231         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
232         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 4);
233
234         tree_win = gtk_scrolled_window_new( NULL, NULL );
235         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
236                                         GTK_POLICY_AUTOMATIC,
237                                         GTK_POLICY_AUTOMATIC );
238         gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
239
240         titles[0] = _( "Select Address Book Folder") ;
241
242         tree_folder = gtk_sctree_new_with_titles( 1, 0, titles );
243         gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
244         gtk_clist_column_titles_show( GTK_CLIST(tree_folder) );
245         if (prefs_common.enable_dotted_lines) {
246                 gtk_ctree_set_line_style(GTK_CTREE(tree_folder), GTK_CTREE_LINES_DOTTED);
247                 gtk_ctree_set_expander_style(GTK_CTREE(tree_folder),
248                                      GTK_CTREE_EXPANDER_SQUARE);
249         } else {
250                 gtk_ctree_set_line_style(GTK_CTREE(tree_folder), GTK_CTREE_LINES_NONE);
251                 gtk_ctree_set_expander_style(GTK_CTREE(tree_folder),
252                                      GTK_CTREE_EXPANDER_TRIANGLE);
253         }
254         gtk_sctree_set_stripes(GTK_SCTREE(tree_folder), prefs_common.use_stripes_everywhere);
255         gtk_clist_set_selection_mode( GTK_CLIST(tree_folder), GTK_SELECTION_BROWSE );
256         gtk_ctree_set_indent( GTK_CTREE(tree_folder), CTREE_INDENT );
257         gtk_clist_set_auto_sort( GTK_CLIST(tree_folder), TRUE );
258
259         /* Button panel */
260         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
261                                       &ok_btn, GTK_STOCK_OK,
262                                       NULL, NULL);
263         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
264         gtk_container_set_border_width( GTK_CONTAINER(hbbox), HSPACING_NARROW );
265         gtk_widget_grab_default(ok_btn);
266
267         g_signal_connect(G_OBJECT(ok_btn), "clicked",
268                          G_CALLBACK(addressadd_ok), NULL);
269         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
270                          G_CALLBACK(addressadd_cancel), NULL);
271         g_signal_connect(G_OBJECT(tree_folder), "tree_select_row",
272                          G_CALLBACK(addressadd_folder_select), NULL);
273         g_signal_connect(G_OBJECT(tree_folder), "button_press_event",
274                          G_CALLBACK(addressadd_tree_button), NULL);
275
276         if (!geometry.min_height) {
277                 geometry.min_width = 300;
278                 geometry.min_height = 350;
279         }
280
281         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
282                                       GDK_HINT_MIN_SIZE);
283         gtk_widget_set_size_request(window, prefs_common.addressaddwin_width,
284                                     prefs_common.addressaddwin_height);
285
286         addressadd_dlg.window        = window;
287         addressadd_dlg.picture       = picture;
288         addressadd_dlg.entry_name    = entry_name;
289         addressadd_dlg.label_address = label_addr;
290         addressadd_dlg.entry_remarks = entry_rems;
291         addressadd_dlg.tree_folder   = tree_folder;
292         addressadd_dlg.ok_btn        = ok_btn;
293         addressadd_dlg.cancel_btn    = cancel_btn;
294
295         gtk_widget_show_all( window );
296
297         stock_pixmap_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm, &bookXpmMask );
298         stock_pixmap_gdk( window, STOCK_PIXMAP_DIR_OPEN,
299                           &folderXpm, &folderXpmMask );
300 }
301
302 static void addressadd_load_folder( GtkCTreeNode *parentNode, ItemFolder *parentFolder,
303                                         FolderInfo *fiParent )
304 {
305         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
306         GList *list;
307         ItemFolder *folder;
308         gchar *fName;
309         gchar **name;
310         GtkCTreeNode *node;
311         FolderInfo *fi;
312
313         list = parentFolder->listFolder;
314         while( list ) {
315                 folder = list->data;
316                 fName = g_strdup( ADDRITEM_NAME(folder) );
317                 name = &fName;
318                 node = gtk_sctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
319                                 folderXpm, folderXpmMask, folderXpm, folderXpmMask,
320                                 FALSE, TRUE );
321                 g_free( fName );
322                 fi = addressadd_create_folderinfo( fiParent->book, folder );
323                 gtk_ctree_node_set_row_data_full( tree, node, fi,
324                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
325                 addressadd_load_folder( node, folder, fi );
326                 list = g_list_next( list );
327         }
328 }
329
330 static void addressadd_load_data( AddressIndex *addrIndex ) {
331         AddressDataSource *ds;
332         GList *list, *nodeDS;
333         gchar **name;
334         gchar *dsName;
335         ItemFolder *rootFolder;
336         AddressBookFile *abf;
337         FolderInfo *fi;
338         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
339         GtkCTreeNode *node;
340
341         gtk_clist_clear( GTK_CLIST( tree ) );
342         list = addrindex_get_interface_list( addrIndex );
343         while( list ) {
344                 AddressInterface *interface = list->data;
345                 if( interface->type == ADDR_IF_BOOK || 
346                                 interface->type == ADDR_IF_LDAP ) {
347                         nodeDS = interface->listSource;
348                         while( nodeDS ) {
349                                 ds = nodeDS->data;
350                                 dsName = g_strdup( addrindex_ds_get_name( ds ) );
351
352                                 /* Read address book */
353                                 if( ! addrindex_ds_get_read_flag( ds ) ) {
354                                         addrindex_ds_read_data( ds );
355                                 }
356
357                                 /* Add node for address book */
358                                 abf = ds->rawDataSource;
359                                 name = &dsName;
360                                 node = gtk_ctree_insert_node( tree, NULL, NULL,
361                                                 name, FOLDER_SPACING, bookXpm,
362                                                 bookXpmMask, bookXpm, bookXpmMask,
363                                                 FALSE, TRUE );
364                                 g_free( dsName );
365
366                                 fi = addressadd_create_folderinfo( abf, NULL );
367                                 gtk_ctree_node_set_row_data_full( tree, node, fi,
368                                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
369
370                                 rootFolder = addrindex_ds_get_root_folder( ds );
371                                 addressadd_load_folder( node, rootFolder, fi );
372
373                                 nodeDS = g_list_next( nodeDS );
374                         }
375                 }
376                 list = g_list_next( list );
377         }
378 }
379
380 gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, 
381                 const gchar *address, const gchar *remarks, GdkPixbuf *picture ) {
382         gboolean retVal = FALSE;
383         ItemPerson *person = NULL;
384         FolderInfo *fi = NULL;
385         addressadd_cancelled = FALSE;
386
387         if( ! addressadd_dlg.window ) addressadd_create();
388         gtk_widget_grab_focus(addressadd_dlg.ok_btn);
389         gtk_widget_show(addressadd_dlg.window);
390         manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
391
392         addressadd_dlg.fiSelected = NULL;
393         addressadd_load_data( addrIndex );
394         gtk_clist_select_row( GTK_CLIST( addressadd_dlg.tree_folder ), 0, 0 );
395         gtk_widget_show(addressadd_dlg.window);
396
397         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), "" );
398         gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
399         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), "" );
400         if( name )
401                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), name );
402         if( address )
403                 gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
404         if( remarks )
405                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), remarks );
406         if( picture ) {
407                 gtk_image_set_from_pixbuf(GTK_IMAGE(addressadd_dlg.picture), picture);
408                 gtk_widget_show(GTK_WIDGET(addressadd_dlg.picture));
409         } else {
410                 gtk_widget_hide(GTK_WIDGET(addressadd_dlg.picture));
411         }
412         gtk_main();
413         gtk_widget_hide( addressadd_dlg.window );
414
415         if( ! addressadd_cancelled ) {
416                 if( addressadd_dlg.fiSelected ) {
417                         gchar *returned_name;
418                         gchar *returned_remarks;
419                         returned_name = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_name), 0, -1 );
420                         returned_remarks = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_remarks), 0, -1 );
421
422                         fi = addressadd_dlg.fiSelected;
423                         
424                         person = addrbook_add_contact( fi->book, fi->folder, 
425                                                         returned_name, 
426                                                         address, 
427                                                         returned_remarks);
428                         person->status = ADD_ENTRY;
429
430                         if (picture) {
431                                 GError *error = NULL;
432                                 gchar *name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
433                                                         ADDRITEM_ID(person), ".png", NULL );
434                                 gdk_pixbuf_save(picture, name, "png", &error, NULL);
435                                 if (error) {
436                                         g_warning(_("Failed to save image: \n%s"),
437                                                         error->message);
438                                         g_error_free(error);
439                                 }
440                                 addritem_person_set_picture( person, ADDRITEM_ID(person) ) ;
441                                 g_free( name );
442                         }
443 #ifdef USE_LDAP
444                         if (fi->book->type == ADBOOKTYPE_LDAP) {
445                                 LdapServer *server = (LdapServer *) fi->book;
446                                 ldapsvr_set_modified(server, TRUE);
447                                 ldapsvr_update_book(server, person);
448                                 if (server->retVal != LDAPRC_SUCCESS) {
449                                         alertpanel( _("Add address(es)"),
450                                                 _("Can't add the specified address"),
451                                                 GTK_STOCK_CLOSE, NULL, NULL );
452                                         return server->retVal;
453                                 }
454                         }
455 #endif
456                         g_free(returned_name);
457                         g_free(returned_remarks);
458                         if( person ) retVal = TRUE;
459                 }
460         }
461
462         gtk_clist_clear( GTK_CLIST( addressadd_dlg.tree_folder ) );
463
464         return retVal;
465 }
466
467 /*
468 * End of Source.
469 */
470