c67e56cc118cf4ba318794ea90e8aea03196a6e7
[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, gint row, gint column,
117                                         GdkEvent *event, gpointer data )
118 {
119         addressadd_dlg.fiSelected = gtk_clist_get_row_data( GTK_CLIST(ctree), row );
120 }
121
122 static gboolean addressadd_tree_button( GtkCTree *ctree, GdkEventButton *event, gpointer data ) {
123         if( ! event ) return FALSE;
124         if( event->button == 1 ) {
125                 /* Handle double click */
126                 if( event->type == GDK_2BUTTON_PRESS ) {
127                         addressadd_cancelled = FALSE;
128                         gtk_main_quit();
129                 }
130         }
131
132         return FALSE;
133 }
134
135 static void addressadd_size_allocate_cb(GtkWidget *widget,
136                                          GtkAllocation *allocation)
137 {
138         g_return_if_fail(allocation != NULL);
139
140         prefs_common.addressaddwin_width = allocation->width;
141         prefs_common.addressaddwin_height = allocation->height;
142 }
143
144 static void addressadd_create( void ) {
145         GtkWidget *window;
146         GtkWidget *vbox, *hbox;
147         GtkWidget *frame;
148         GtkWidget *table;
149         GtkWidget *label;
150         GtkWidget *picture;
151         GtkWidget *entry_name;
152         GtkWidget *label_addr;
153         GtkWidget *entry_rems;
154         GtkWidget *tree_folder;
155         GtkWidget *vlbox;
156         GtkWidget *tree_win;
157         GtkWidget *hbbox;
158         GtkWidget *ok_btn;
159         GtkWidget *cancel_btn;
160         gint top;
161         static GdkGeometry geometry;
162
163         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "addressadd");
164         gtk_container_set_border_width( GTK_CONTAINER(window), VBOX_BORDER );
165         gtk_window_set_title( GTK_WINDOW(window), _("Add to address book") );
166         gtk_window_set_position( GTK_WINDOW(window), GTK_WIN_POS_MOUSE );
167         gtk_window_set_modal( GTK_WINDOW(window), TRUE );
168         g_signal_connect( G_OBJECT(window), "delete_event",
169                           G_CALLBACK(addressadd_delete_event), NULL );
170         g_signal_connect( G_OBJECT(window), "key_press_event",
171                           G_CALLBACK(addressadd_key_pressed), NULL );
172         g_signal_connect(G_OBJECT(window), "size_allocate",
173                          G_CALLBACK(addressadd_size_allocate_cb), NULL);
174         
175         hbox = gtk_hbox_new(FALSE, 10);
176         gtk_container_set_border_width(GTK_CONTAINER(hbox), 4);
177         vbox = gtk_vbox_new(FALSE, 8);
178         gtk_container_add(GTK_CONTAINER(window), vbox);
179
180         picture = gtk_image_new();
181         gtk_box_pack_start(GTK_BOX(hbox), picture, FALSE, FALSE, 0);
182
183         table = gtk_table_new(3, 2, FALSE);
184         gtk_box_pack_start(GTK_BOX(hbox), table, TRUE, TRUE, 0);
185         gtk_table_set_row_spacings(GTK_TABLE(table), VSPACING_NARROW);
186         gtk_table_set_col_spacings(GTK_TABLE(table), HSPACING_NARROW);
187
188         frame = gtk_frame_new(_("Contact"));
189         gtk_frame_set_label_align(GTK_FRAME(frame), 0.01, 0.5);
190         gtk_container_add(GTK_CONTAINER(frame), hbox);
191         gtk_container_set_border_width( GTK_CONTAINER(frame), 4);
192         gtk_box_pack_start(GTK_BOX(vbox), frame, FALSE, FALSE, 0);
193
194         /* First row */
195         top = 0;
196         label = gtk_label_new(_("Name"));
197         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
198         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
199
200         entry_name = gtk_entry_new();
201         gtk_widget_set_size_request(entry_name, 150, -1);
202         gtk_entry_set_text (GTK_ENTRY(entry_name),"");
203         
204         gtk_table_attach(GTK_TABLE(table), entry_name, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND , 0, 0, 0);
205
206         /* Second row */
207         top = 1;
208         label = gtk_label_new(_("Address"));
209         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
210         gtk_misc_set_alignment(GTK_MISC(label), 0, 0);
211
212         label_addr = gtk_label_new("");
213         gtk_widget_set_size_request(label_addr, 150, -1);
214         gtk_table_attach(GTK_TABLE(table), label_addr, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
215         gtk_misc_set_alignment(GTK_MISC(label_addr), 0, 0.5);
216
217         /* Third row */
218         top = 2;
219         label = gtk_label_new(_("Remarks"));
220         gtk_table_attach(GTK_TABLE(table), label, 0, 1, top, (top + 1), GTK_FILL, 0, 0, 0);
221         gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
222
223         entry_rems = gtk_entry_new();
224         gtk_widget_set_size_request(entry_rems, 150, -1);
225         gtk_table_attach(GTK_TABLE(table), entry_rems, 1, 2, top, (top + 1), GTK_FILL | GTK_EXPAND, 0, 0, 0);
226         
227         /* Address book/folder tree */
228         vlbox = gtk_vbox_new(FALSE, VBOX_BORDER);
229         gtk_box_pack_start(GTK_BOX(vbox), vlbox, TRUE, TRUE, 0);
230         gtk_container_set_border_width( GTK_CONTAINER(vlbox), 4);
231
232         tree_win = gtk_scrolled_window_new( NULL, NULL );
233         gtk_scrolled_window_set_policy( GTK_SCROLLED_WINDOW(tree_win),
234                                         GTK_POLICY_AUTOMATIC,
235                                         GTK_POLICY_AUTOMATIC );
236         gtk_box_pack_start( GTK_BOX(vlbox), tree_win, TRUE, TRUE, 0 );
237
238         tree_folder = gtk_ctree_new( 1, 0 );
239         gtk_container_add( GTK_CONTAINER(tree_win), tree_folder );
240         gtk_clist_column_titles_show( GTK_CLIST(tree_folder) );
241         gtk_clist_set_column_title( GTK_CLIST(tree_folder), 0, _( "Select Address Book Folder" ) );
242         gtk_ctree_set_line_style( GTK_CTREE(tree_folder), GTK_CTREE_LINES_DOTTED );
243         gtk_clist_set_selection_mode( GTK_CLIST(tree_folder), GTK_SELECTION_BROWSE );
244         gtk_ctree_set_expander_style( GTK_CTREE(tree_folder), GTK_CTREE_EXPANDER_SQUARE );
245         gtk_ctree_set_indent( GTK_CTREE(tree_folder), CTREE_INDENT );
246         gtk_clist_set_auto_sort( GTK_CLIST(tree_folder), TRUE );
247
248         /* Button panel */
249         gtkut_stock_button_set_create(&hbbox, &cancel_btn, GTK_STOCK_CANCEL,
250                                       &ok_btn, GTK_STOCK_OK,
251                                       NULL, NULL);
252         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
253         gtk_container_set_border_width( GTK_CONTAINER(hbbox), HSPACING_NARROW );
254         gtk_widget_grab_default(ok_btn);
255
256         g_signal_connect(G_OBJECT(ok_btn), "clicked",
257                          G_CALLBACK(addressadd_ok), NULL);
258         g_signal_connect(G_OBJECT(cancel_btn), "clicked",
259                          G_CALLBACK(addressadd_cancel), NULL);
260         g_signal_connect(G_OBJECT(tree_folder), "select_row",
261                          G_CALLBACK(addressadd_folder_select), NULL);
262         g_signal_connect(G_OBJECT(tree_folder), "button_press_event",
263                          G_CALLBACK(addressadd_tree_button), NULL);
264
265         if (!geometry.min_height) {
266                 geometry.min_width = 300;
267                 geometry.min_height = 350;
268         }
269
270         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
271                                       GDK_HINT_MIN_SIZE);
272         gtk_widget_set_size_request(window, prefs_common.addressaddwin_width,
273                                     prefs_common.addressaddwin_height);
274
275         addressadd_dlg.window        = window;
276         addressadd_dlg.picture       = picture;
277         addressadd_dlg.entry_name    = entry_name;
278         addressadd_dlg.label_address = label_addr;
279         addressadd_dlg.entry_remarks = entry_rems;
280         addressadd_dlg.tree_folder   = tree_folder;
281         addressadd_dlg.ok_btn        = ok_btn;
282         addressadd_dlg.cancel_btn    = cancel_btn;
283
284         gtk_widget_show_all( window );
285
286         stock_pixmap_gdk( window, STOCK_PIXMAP_BOOK, &bookXpm, &bookXpmMask );
287         stock_pixmap_gdk( window, STOCK_PIXMAP_DIR_OPEN,
288                           &folderXpm, &folderXpmMask );
289 }
290
291 static void addressadd_load_folder( GtkCTreeNode *parentNode, ItemFolder *parentFolder,
292                                         FolderInfo *fiParent )
293 {
294         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
295         GList *list;
296         ItemFolder *folder;
297         gchar *fName;
298         gchar **name;
299         GtkCTreeNode *node;
300         FolderInfo *fi;
301
302         list = parentFolder->listFolder;
303         while( list ) {
304                 folder = list->data;
305                 fName = g_strdup( ADDRITEM_NAME(folder) );
306                 name = &fName;
307                 node = gtk_sctree_insert_node( tree, parentNode, NULL, name, FOLDER_SPACING,
308                                 folderXpm, folderXpmMask, folderXpm, folderXpmMask,
309                                 FALSE, TRUE );
310                 g_free( fName );
311                 fi = addressadd_create_folderinfo( fiParent->book, folder );
312                 gtk_ctree_node_set_row_data_full( tree, node, fi,
313                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
314                 addressadd_load_folder( node, folder, fi );
315                 list = g_list_next( list );
316         }
317 }
318
319 static void addressadd_load_data( AddressIndex *addrIndex ) {
320         AddressDataSource *ds;
321         GList *list, *nodeDS;
322         gchar **name;
323         gchar *dsName;
324         ItemFolder *rootFolder;
325         AddressBookFile *abf;
326         FolderInfo *fi;
327         GtkCTree *tree = GTK_CTREE( addressadd_dlg.tree_folder );
328         GtkCTreeNode *node;
329
330         gtk_clist_clear( GTK_CLIST( tree ) );
331         list = addrindex_get_interface_list( addrIndex );
332         while( list ) {
333                 AddressInterface *interface = list->data;
334                 if( interface->type == ADDR_IF_BOOK || 
335                                 interface->type == ADDR_IF_LDAP ) {
336                         nodeDS = interface->listSource;
337                         while( nodeDS ) {
338                                 ds = nodeDS->data;
339                                 dsName = g_strdup( addrindex_ds_get_name( ds ) );
340
341                                 /* Read address book */
342                                 if( ! addrindex_ds_get_read_flag( ds ) ) {
343                                         addrindex_ds_read_data( ds );
344                                 }
345
346                                 /* Add node for address book */
347                                 abf = ds->rawDataSource;
348                                 name = &dsName;
349                                 node = gtk_ctree_insert_node( tree, NULL, NULL,
350                                                 name, FOLDER_SPACING, bookXpm,
351                                                 bookXpmMask, bookXpm, bookXpmMask,
352                                                 FALSE, TRUE );
353                                 g_free( dsName );
354
355                                 fi = addressadd_create_folderinfo( abf, NULL );
356                                 gtk_ctree_node_set_row_data_full( tree, node, fi,
357                                                 ( GtkDestroyNotify ) addressadd_free_folderinfo );
358
359                                 rootFolder = addrindex_ds_get_root_folder( ds );
360                                 addressadd_load_folder( node, rootFolder, fi );
361
362                                 nodeDS = g_list_next( nodeDS );
363                         }
364                 }
365                 list = g_list_next( list );
366         }
367 }
368
369 gboolean addressadd_selection( AddressIndex *addrIndex, const gchar *name, 
370                 const gchar *address, const gchar *remarks, GdkPixbuf *picture ) {
371         gboolean retVal = FALSE;
372         ItemPerson *person = NULL;
373         FolderInfo *fi = NULL;
374         addressadd_cancelled = FALSE;
375
376         if( ! addressadd_dlg.window ) addressadd_create();
377         gtk_widget_grab_focus(addressadd_dlg.ok_btn);
378         gtk_widget_show(addressadd_dlg.window);
379         manage_window_set_transient(GTK_WINDOW(addressadd_dlg.window));
380
381         addressadd_dlg.fiSelected = NULL;
382         addressadd_load_data( addrIndex );
383         gtk_clist_select_row( GTK_CLIST( addressadd_dlg.tree_folder ), 0, 0 );
384         gtk_widget_show(addressadd_dlg.window);
385
386         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), "" );
387         gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), "" );
388         gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), "" );
389         if( name )
390                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_name ), name );
391         if( address )
392                 gtk_label_set_text( GTK_LABEL(addressadd_dlg.label_address ), address );
393         if( remarks )
394                 gtk_entry_set_text( GTK_ENTRY(addressadd_dlg.entry_remarks ), remarks );
395         if( picture ) {
396                 gtk_image_set_from_pixbuf(GTK_IMAGE(addressadd_dlg.picture), picture);
397                 gtk_widget_show(GTK_WIDGET(addressadd_dlg.picture));
398         } else {
399                 gtk_widget_hide(GTK_WIDGET(addressadd_dlg.picture));
400         }
401         gtk_main();
402         gtk_widget_hide( addressadd_dlg.window );
403
404         if( ! addressadd_cancelled ) {
405                 if( addressadd_dlg.fiSelected ) {
406                         gchar *returned_name;
407                         gchar *returned_remarks;
408                         returned_name = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_name), 0, -1 );
409                         returned_remarks = gtk_editable_get_chars( GTK_EDITABLE(addressadd_dlg.entry_remarks), 0, -1 );
410
411                         fi = addressadd_dlg.fiSelected;
412                         
413                         person = addrbook_add_contact( fi->book, fi->folder, 
414                                                         returned_name, 
415                                                         address, 
416                                                         returned_remarks);
417                         person->status = ADD_ENTRY;
418
419                         if (picture) {
420                                 GError *error = NULL;
421                                 gchar *name = g_strconcat( get_rc_dir(), G_DIR_SEPARATOR_S, ADDRBOOK_DIR, G_DIR_SEPARATOR_S, 
422                                                         ADDRITEM_ID(person), ".png", NULL );
423                                 gdk_pixbuf_save(picture, name, "png", &error, NULL);
424                                 if (error) {
425                                         g_warning(_("Failed to save image: \n%s"),
426                                                         error->message);
427                                         g_error_free(error);
428                                 }
429                                 addritem_person_set_picture( person, ADDRITEM_ID(person) ) ;
430                                 g_free( name );
431                         }
432 #ifdef USE_LDAP
433                         if (fi->book->type == ADBOOKTYPE_LDAP) {
434                                 LdapServer *server = (LdapServer *) fi->book;
435                                 ldapsvr_set_modified(server, TRUE);
436                                 ldapsvr_update_book(server, person);
437                                 if (server->retVal != LDAPRC_SUCCESS) {
438                                         alertpanel( _("Add address(es)"),
439                                                 _("Can't add the specified address"),
440                                                 GTK_STOCK_CLOSE, NULL, NULL );
441                                         return server->retVal;
442                                 }
443                         }
444 #endif
445                         g_free(returned_name);
446                         g_free(returned_remarks);
447                         if( person ) retVal = TRUE;
448                 }
449         }
450
451         gtk_clist_clear( GTK_CLIST( addressadd_dlg.tree_folder ) );
452
453         return retVal;
454 }
455
456 /*
457 * End of Source.
458 */
459