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