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