07275e1d14c90368ea4a3b122a44847826a454e1
[claws.git] / src / addressbook.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2007 Hiroyuki Yamamoto 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 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #endif
23
24 #include "defs.h"
25
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <gdk/gdkkeysyms.h>
29 #include <gtk/gtkwindow.h>
30 #include <gtk/gtksignal.h>
31 #include <gtk/gtkvbox.h>
32 #include <gtk/gtkscrolledwindow.h>
33 #include <gtk/gtkhpaned.h>
34 #include <gtk/gtkhbox.h>
35 #include <gtk/gtklabel.h>
36 #include <gtk/gtkentry.h>
37 #include <gtk/gtkctree.h>
38 #include <gtk/gtkclist.h>
39 #include <gtk/gtktable.h>
40 #include <gtk/gtkhbbox.h>
41 #include <gtk/gtkbutton.h>
42 #include <gtk/gtkmenu.h>
43 #include <gtk/gtkmenuitem.h>
44 #include <gtk/gtkitemfactory.h>
45 #include <string.h>
46 #include <setjmp.h>
47 #include <sys/types.h>
48 #include <dirent.h>
49
50 #include "main.h"
51 #include "addressbook.h"
52 #include "manage_window.h"
53 #include "prefs_common.h"
54 #include "alertpanel.h"
55 #include "inputdialog.h"
56 #include "menu.h"
57 #include "stock_pixmap.h"
58 #include "xml.h"
59 #include "prefs_gtk.h"
60 #include "procmime.h"
61 #include "utils.h"
62 #include "gtkutils.h"
63 #include "codeconv.h"
64 #include "about.h"
65 #include "addr_compl.h"
66
67 #include "mgutils.h"
68 #include "addressitem.h"
69 #include "addritem.h"
70 #include "addrcache.h"
71 #include "addrbook.h"
72 #include "addrindex.h"
73 #include "addressadd.h"
74 #include "addrduplicates.h"
75 #include "addressbook_foldersel.h"
76 #include "vcard.h"
77 #include "editvcard.h"
78 #include "editgroup.h"
79 #include "editaddress.h"
80 #include "editbook.h"
81 #include "importldif.h"
82 #include "importmutt.h"
83 #include "importpine.h"
84 #include "manual.h"
85
86 #ifdef USE_JPILOT
87 #include "jpilot.h"
88 #include "editjpilot.h"
89 #endif
90
91 #ifdef USE_LDAP
92 #include <pthread.h>
93 #include "ldapserver.h"
94 #include "editldap.h"
95 #include "ldapupdate.h"
96
97 #define ADDRESSBOOK_LDAP_BUSYMSG "Busy"
98 #endif
99
100 #include "addrquery.h"
101 #include "addrselect.h"
102 #include "addrclip.h"
103 #include "addrgather.h"
104 #include "adbookbase.h"
105 #include "exphtmldlg.h"
106 #include "expldifdlg.h"
107 #include "browseldap.h"
108
109 typedef enum
110 {
111         COL_SOURCES     = 0,
112         N_INDEX_COLS    = 1
113 } AddressIndexColumns;
114
115 typedef enum
116 {
117         COL_NAME        = 0,
118         COL_ADDRESS     = 1,
119         COL_REMARKS     = 2,
120         N_LIST_COLS     = 3
121 } AddressListColumns;
122
123 typedef struct {
124         AddressBookFile *book;
125         ItemFolder      *folder;
126 } FolderInfo;
127
128 typedef struct {
129         gchar **folder_path;
130         gboolean matched;
131         gint index;
132         AddressDataSource *book;
133         ItemFolder *folder;
134 } FolderPathMatch;
135
136 static gchar *list_titles[] = { N_("Name"),
137                                 N_("Email Address"),
138                                 N_("Remarks") };
139
140 #define COL_NAME_WIDTH          164
141 #define COL_ADDRESS_WIDTH       156
142
143 #define COL_FOLDER_WIDTH        170
144 #define ADDRESSBOOK_WIDTH       640
145 #define ADDRESSBOOK_HEIGHT      360
146
147 #define ADDRESSBOOK_MSGBUF_SIZE 2048
148
149 static GdkPixmap *folderxpm;
150 static GdkBitmap *folderxpmmask;
151 static GdkPixmap *folderopenxpm;
152 static GdkBitmap *folderopenxpmmask;
153 static GdkPixmap *groupxpm;
154 static GdkBitmap *groupxpmmask;
155 static GdkPixmap *interfacexpm;
156 static GdkBitmap *interfacexpmmask;
157 static GdkPixmap *bookxpm;
158 static GdkBitmap *bookxpmmask;
159 static GdkPixmap *addressxpm;
160 static GdkBitmap *addressxpmmask;
161 static GdkPixmap *vcardxpm;
162 static GdkBitmap *vcardxpmmask;
163 static GdkPixmap *jpilotxpm;
164 static GdkBitmap *jpilotxpmmask;
165 static GdkPixmap *categoryxpm;
166 static GdkBitmap *categoryxpmmask;
167 static GdkPixmap *ldapxpm;
168 static GdkBitmap *ldapxpmmask;
169 static GdkPixmap *addrsearchxpm;
170 static GdkPixmap *addrsearchxpmmask;
171
172 /* Message buffer */
173 static gchar addressbook_msgbuf[ ADDRESSBOOK_MSGBUF_SIZE ];
174
175 /* Address list selection */
176 static AddrSelectList *_addressSelect_ = NULL;
177 static AddressClipboard *_clipBoard_ = NULL;
178
179 /* Address index file and interfaces */
180 static AddressIndex *_addressIndex_ = NULL;
181 static GList *_addressInterfaceList_ = NULL;
182 static GList *_addressIFaceSelection_ = NULL;
183 #define ADDRESSBOOK_IFACE_SELECTION "1/y,3/y,4/y,2/n"
184
185 static AddressBook_win addrbook;
186
187 static GHashTable *_addressBookTypeHash_ = NULL;
188 static GList *_addressBookTypeList_ = NULL;
189
190 static void addressbook_new_address_from_book_post_cb( ItemPerson *person );
191 static void addressbook_new_address_from_folder_post_cb( ItemPerson *person );
192 static void addressbook_edit_address_post_cb( ItemPerson *person );
193
194 static void addressbook_create                  (void);
195 static gint addressbook_close                   (void);
196 static void addressbook_button_set_sensitive    (void);
197
198 static gboolean address_index_has_focus = FALSE;
199 static gboolean address_list_has_focus = FALSE;
200
201 /* callback functions */
202 static void addressbook_del_clicked             (GtkButton      *button,
203                                                  gpointer        data);
204 static void addressbook_reg_clicked             (GtkButton      *button,
205                                                  gpointer        data);
206 static void addressbook_to_clicked              (GtkButton      *button,
207                                                  gpointer        data);
208 static void addressbook_lup_clicked             (GtkButton      *button,
209                                                  gpointer       data);
210 static void addressbook_close_clicked           (GtkButton      *button,
211                                                  gpointer       data);
212
213 static void addressbook_tree_selected           (GtkCTree       *ctree,
214                                                  GtkCTreeNode   *node,
215                                                  gint            column,
216                                                  gpointer        data);
217 static void addressbook_select_row_tree         (GtkCTree       *ctree,
218                                                  GtkCTreeNode   *node,
219                                                  gint            column,
220                                                  gpointer        data);
221 static void addressbook_list_row_selected       (GtkCTree       *clist,
222                                                  GtkCTreeNode   *node,
223                                                  gint            column,
224                                                  gpointer        data);
225 static void addressbook_list_row_unselected     (GtkCTree       *clist,
226                                                  GtkCTreeNode   *node,
227                                                  gint            column,
228                                                  gpointer        data);
229 static void addressbook_person_expand_node      (GtkCTree       *ctree,
230                                                  GList          *node,
231                                                  gpointer       *data );
232 static void addressbook_person_collapse_node    (GtkCTree       *ctree,
233                                                  GList          *node,
234                                                  gpointer       *data );
235
236 static gboolean addressbook_list_button_pressed (GtkWidget      *widget,
237                                                  GdkEventButton *event,
238                                                  gpointer        data);
239 static gboolean addressbook_list_button_released(GtkWidget      *widget,
240                                                  GdkEventButton *event,
241                                                  gpointer        data);
242 static gboolean addressbook_tree_button_pressed (GtkWidget      *ctree,
243                                                  GdkEventButton *event,
244                                                  gpointer        data);
245 static gboolean addressbook_tree_button_released(GtkWidget      *ctree,
246                                                  GdkEventButton *event,
247                                                  gpointer        data);
248
249 static void addressbook_new_folder_cb           (gpointer        data,
250                                                  guint           action,
251                                                  GtkWidget      *widget);
252 static void addressbook_new_group_cb            (gpointer        data,
253                                                  guint           action,
254                                                  GtkWidget      *widget);
255 static void addressbook_treenode_edit_cb        (gpointer        data,
256                                                  guint           action,
257                                                  GtkWidget      *widget);
258 static void addressbook_treenode_delete_cb      (gpointer        data,
259                                                  guint           action,
260                                                  GtkWidget      *widget);
261
262 static void addressbook_change_node_name        (GtkCTreeNode   *node,
263                                                  const gchar    *name);
264
265 static void addressbook_new_address_cb          (gpointer        data,
266                                                  guint           action,
267                                                  GtkWidget      *widget);
268 static void addressbook_edit_address_cb         (gpointer        data,
269                                                  guint           action,
270                                                  GtkWidget      *widget);
271 static void addressbook_delete_address_cb       (gpointer        data,
272                                                  guint           action,
273                                                  GtkWidget      *widget);
274
275 static void close_cb                            (gpointer        data,
276                                                  guint           action,
277                                                  GtkWidget      *widget);
278 static void addressbook_file_save_cb            (gpointer        data,
279                                                  guint           action,
280                                                  GtkWidget      *widget);
281
282 /* Data source edit stuff */
283 static void addressbook_new_book_cb             (gpointer        data,
284                                                  guint           action,
285                                                  GtkWidget      *widget);
286 static void addressbook_new_vcard_cb            (gpointer        data,
287                                                  guint           action,
288                                                  GtkWidget      *widget);
289
290 #ifdef USE_JPILOT
291 static void addressbook_new_jpilot_cb           (gpointer        data,
292                                                  guint           action,
293                                                  GtkWidget      *widget);
294 #endif
295
296 #ifdef USE_LDAP
297 static void addressbook_new_ldap_cb             (gpointer        data,
298                                                  guint           action,
299                                                  GtkWidget      *widget);
300 #endif
301
302 static void addressbook_set_clist               (AddressObject  *obj,
303                                                  gboolean        refresh);
304
305 static void addressbook_load_tree               (void);
306 void addressbook_read_file                      (void);
307
308 static GtkCTreeNode *addressbook_add_object     (GtkCTreeNode   *node,
309                                                  AddressObject  *obj);
310 static void addressbook_treenode_remove_item    ( void );
311
312 static AddressDataSource *addressbook_find_datasource
313                                                 (GtkCTreeNode   *node );
314
315 static AddressBookFile *addressbook_get_book_file(void);
316
317 static GtkCTreeNode *addressbook_node_add_folder
318                                                 (GtkCTreeNode   *node,
319                                                 AddressDataSource *ds,
320                                                 ItemFolder      *itemFolder,
321                                                 AddressObjectType otype);
322 static GtkCTreeNode *addressbook_node_add_group (GtkCTreeNode   *node,
323                                                 AddressDataSource *ds,
324                                                 ItemGroup       *itemGroup);
325 static void addressbook_tree_remove_children    (GtkCTree       *ctree,
326                                                 GtkCTreeNode    *parent);
327 static void addressbook_move_nodes_up           (GtkCTree       *ctree,
328                                                 GtkCTreeNode    *node);
329 static GtkCTreeNode *addressbook_find_group_node (GtkCTreeNode  *parent,
330                                                    ItemGroup    *group);
331 static gboolean addressbook_entry_key_pressed   (GtkWidget      *widget,
332                                                  GdkEventKey    *event,
333                                                  gpointer        data);
334 static gint addressbook_treenode_compare_func   (GtkCList       *clist,
335                                                  gconstpointer   ptr1,
336                                                  gconstpointer   ptr2);
337 static void addressbook_folder_load_one_person  (GtkCTree *clist, 
338                                                  ItemPerson *person,  
339                                                  AddressTypeControlItem *atci, 
340                                                  AddressTypeControlItem *atciMail);
341 static void addressbook_folder_refresh_one_person(GtkCTree *clist, 
342                                                   ItemPerson *person);
343 static void addressbook_folder_remove_one_person(GtkCTree *clist, 
344                                                  ItemPerson *person);
345 static void addressbook_folder_remove_node      (GtkCTree *clist, 
346                                                  GtkCTreeNode *node);
347
348 static void addressbook_edit_address( gpointer data, guint action, GtkWidget *widget,
349                                                                           gboolean force_focus );
350
351 /* LUT's and IF stuff */
352 static void addressbook_free_treenode           ( gpointer data );
353 static AddressTypeControlItem *addrbookctl_lookup       (gint            ot);
354 static AddressTypeControlItem *addrbookctl_lookup_iface(AddressIfType    ifType);
355
356 static void addrbookctl_build_map                       (GtkWidget      *window);
357 static void addrbookctl_build_iflist                    (void);
358 static AdapterInterface *addrbookctl_find_interface     (AddressIfType   ifType);
359 static void addrbookctl_build_ifselect                  (void);
360
361 static void addrbookctl_free_interface          (AdapterInterface *adapter);
362 static void addrbookctl_free_datasource         (AdapterDSource   *adapter);
363 static void addrbookctl_free_folder             (AdapterFolder    *adapter);
364 static void addrbookctl_free_group              (AdapterGroup     *adapter);
365
366 static void addressbook_list_select_clear       ( void );
367 static void addressbook_list_select_add         ( AddrItemObject    *aio,
368                                                   AddressDataSource *ds );
369 static void addressbook_list_select_remove      ( AddrItemObject    *aio );
370
371 static void addressbook_import_ldif_cb          ( void );
372 static void addressbook_find_duplicates_cb      ( void );
373 static void addressbook_import_mutt_cb          ( void );
374 static void addressbook_import_pine_cb          ( void );
375 static void addressbook_export_html_cb          ( void );
376 static void addressbook_export_ldif_cb          ( void );
377 static void addressbook_select_all_cb           ( void );
378 static void addressbook_clip_cut_cb             ( void );
379 static void addressbook_clip_copy_cb            ( void );
380 static void addressbook_clip_paste_cb           ( void );
381 static void addressbook_treenode_cut_cb         ( void );
382 static void addressbook_treenode_copy_cb        ( void );
383 static void addressbook_treenode_paste_cb       ( void );
384
385 static void addressbook_mail_to_cb              ( void );
386
387 #ifdef USE_LDAP
388 static void addressbook_browse_entry_cb         ( void );
389 #endif
390 static void addressbook_edit_clicked(GtkButton *button, gpointer data);
391
392 static void addressbook_start_drag(GtkWidget *widget, gint button, 
393                                    GdkEvent *event,
394                                    void *data);
395 static void addressbook_drag_data_get(GtkWidget        *widget,
396                                      GdkDragContext   *drag_context,
397                                      GtkSelectionData *selection_data,
398                                      guint             info,
399                                      guint             time,
400                                      void             *data);
401 static gboolean addressbook_drag_motion_cb(GtkWidget      *widget,
402                                           GdkDragContext *context,
403                                           gint            x,
404                                           gint            y,
405                                           guint           time,
406                                           void           *data);
407 static void addressbook_drag_leave_cb(GtkWidget      *widget,
408                                      GdkDragContext *context,
409                                      guint           time,
410                                      void           *data);
411 static void addressbook_drag_received_cb(GtkWidget        *widget,
412                                         GdkDragContext   *drag_context,
413                                         gint              x,
414                                         gint              y,
415                                         GtkSelectionData *data,
416                                         guint             info,
417                                         guint             time,
418                                         void             *pdata);
419 static void addressbook_list_menu_setup( void );
420
421 static GtkTargetEntry addressbook_drag_types[] =
422 {
423         {"claws-mail/internal", GTK_TARGET_SAME_APP, TARGET_DUMMY}
424 };
425
426 static GtkTargetList *addressbook_target_list = NULL;
427
428
429 static GtkItemFactoryEntry addressbook_entries[] =
430 {
431         {N_("/_Book"),                  NULL,           NULL, 0, "<Branch>", NULL},
432         {N_("/_Book/New _Book"),        "<control>B",   addressbook_new_book_cb,        0, NULL, NULL},
433         {N_("/_Book/New _Folder"),      "<control>R",   addressbook_new_folder_cb,      0, NULL, NULL},
434         {N_("/_Book/New _vCard"),       "<control><shift>D",    addressbook_new_vcard_cb,       0, NULL, NULL},
435 #ifdef USE_JPILOT
436         {N_("/_Book/New _JPilot"),      "<control>J",   addressbook_new_jpilot_cb,      0, NULL, NULL},
437 #endif
438 #ifdef USE_LDAP
439         {N_("/_Book/New LDAP _Server"), "<control><shift>S",    addressbook_new_ldap_cb,        0, NULL, NULL},
440 #endif
441         {N_("/_Book/---"),              NULL,           NULL, 0, "<Separator>", NULL},
442         {N_("/_Book/_Edit book"),       NULL,           addressbook_treenode_edit_cb,   0, NULL, NULL},
443         {N_("/_Book/_Delete book"),     NULL,           addressbook_treenode_delete_cb, 0, NULL, NULL},
444         {N_("/_Book/---"),              NULL,           NULL, 0, "<Separator>", NULL},
445         {N_("/_Book/_Save"),            "<control>S",   addressbook_file_save_cb,       0, NULL, NULL},
446         {N_("/_Book/_Close"),           "<control>W",   close_cb,                       0, NULL, NULL},
447         {N_("/_Address"),                       NULL,           NULL, 0, "<Branch>", NULL},
448         {N_("/_Address/_Select all"),   "<control>A",   addressbook_select_all_cb,      0, NULL, NULL},
449         {N_("/_Address/---"),           NULL,           NULL, 0, "<Separator>", NULL},
450         {N_("/_Address/C_ut"),          "<control>X",   addressbook_clip_cut_cb,        0, NULL, NULL},
451         {N_("/_Address/_Copy"),         "<control>C",   addressbook_clip_copy_cb,       0, NULL, NULL},
452         {N_("/_Address/_Paste"),        "<control>V",   addressbook_clip_paste_cb,      0, NULL, NULL},
453         {N_("/_Address/---"),           NULL,           NULL, 0, "<Separator>", NULL},
454         {N_("/_Address/_Edit"),         "<control>Return",addressbook_edit_address_cb,    0, NULL, NULL},
455         {N_("/_Address/_Delete"),       "<control>D",   addressbook_delete_address_cb,  0, NULL, NULL},
456         {N_("/_Address/---"),           NULL,           NULL, 0, "<Separator>", NULL},
457         {N_("/_Address/New _Address"),  "<control>N",   addressbook_new_address_cb,     0, NULL, NULL},
458         {N_("/_Address/New _Group"),    "<control>G",   addressbook_new_group_cb,       0, NULL, NULL},
459         {N_("/_Address/---"),           NULL,           NULL, 0, "<Separator>", NULL},
460         {N_("/_Address/_Mail To"),      NULL,           addressbook_mail_to_cb,         0, NULL, NULL},
461         {N_("/_Tools"),                 NULL,           NULL, 0, "<Branch>", NULL},
462         {N_("/_Tools/Import _LDIF file..."), NULL,      addressbook_import_ldif_cb,     0, NULL, NULL},
463         {N_("/_Tools/Import M_utt file..."), NULL,      addressbook_import_mutt_cb,     0, NULL, NULL},
464         {N_("/_Tools/Import _Pine file..."), NULL,      addressbook_import_pine_cb,     0, NULL, NULL},
465         {N_("/_Tools/---"),             NULL,           NULL, 0, "<Separator>", NULL},
466         {N_("/_Tools/Export _HTML..."), NULL,           addressbook_export_html_cb,     0, NULL, NULL},
467         {N_("/_Tools/Export LDI_F..."), NULL,           addressbook_export_ldif_cb,     0, NULL, NULL},
468         {N_("/_Tools/---"),             NULL,           NULL, 0, "<Separator>", NULL},
469         {N_("/_Tools/Find duplicates..."), NULL, addressbook_find_duplicates_cb,        0, NULL, NULL},
470         {N_("/_Help"),                  NULL,           NULL, 0, "<Branch>", NULL},
471         {N_("/_Help/_About"),           NULL,           about_show, 0, NULL, NULL}
472 };
473
474 static GtkItemFactoryEntry addressbook_tree_popup_entries[] =
475 {
476         {N_("/_Edit"),          NULL, addressbook_treenode_edit_cb,   0, NULL, NULL},
477         {N_("/_Delete"),        NULL, addressbook_treenode_delete_cb, 0, NULL, NULL},
478         {"/---",                NULL, NULL, 0, "<Separator>", NULL},
479         {N_("/New _Book"),      NULL, addressbook_new_book_cb,      0, NULL, NULL},
480         {N_("/New _Folder"),    NULL, addressbook_new_folder_cb,      0, NULL, NULL},
481         {N_("/New _Group"),     NULL, addressbook_new_group_cb,      0, NULL, NULL},
482         {"/---",                NULL, NULL, 0, "<Separator>", NULL},
483         {N_("/C_ut"),           NULL, addressbook_treenode_cut_cb,    0, NULL, NULL},
484         {N_("/_Copy"),          NULL, addressbook_treenode_copy_cb,   0, NULL, NULL},
485         {N_("/_Paste"),         NULL, addressbook_treenode_paste_cb,  0, NULL, NULL}
486 };
487
488 static GtkItemFactoryEntry addressbook_list_popup_entries[] =
489 {
490         {N_("/_Select all"),    NULL, addressbook_select_all_cb, 0, NULL, NULL},
491         {"/---",                NULL, NULL, 0, "<Separator>", NULL},
492         {N_("/_Edit"),          NULL, addressbook_edit_address_cb,   0, NULL, NULL},
493         {N_("/_Delete"),        NULL, addressbook_delete_address_cb, 0, NULL, NULL},
494         {"/---",                NULL, NULL, 0, "<Separator>", NULL},
495         {N_("/New _Address"),   NULL, addressbook_new_address_cb,    0, NULL, NULL},
496         {N_("/New _Group"),     NULL, addressbook_new_group_cb,      0, NULL, NULL},
497         {"/---",                NULL, NULL, 0, "<Separator>", NULL},
498         {N_("/C_ut"),           NULL, addressbook_clip_cut_cb,       0, NULL, NULL},
499         {N_("/_Copy"),          NULL, addressbook_clip_copy_cb,      0, NULL, NULL},
500         {N_("/_Paste"),         NULL, addressbook_clip_paste_cb,     0, NULL, NULL},
501         {"/---",                NULL, NULL, 0, "<Separator>", NULL},
502 /*      {N_("/Pa_ste Address"), NULL, addressbook_clip_paste_address_cb, 0, NULL, NULL},*/
503         {N_("/_Mail To"),       NULL, addressbook_mail_to_cb,            0, NULL, NULL},
504 #ifdef USE_LDAP
505         {N_("/_Browse Entry"),  NULL, addressbook_browse_entry_cb,       0, NULL, NULL},
506 #endif  
507 };
508
509 /**
510  * Structure of error message table.
511  */
512 typedef struct _ErrMsgTableEntry ErrMsgTableEntry;
513 struct _ErrMsgTableEntry {
514         gint    code;
515         gchar   *description;
516 };
517
518 static gchar *_errMsgUnknown_ = N_( "Unknown" );
519
520 /**
521  * Lookup table of error messages for general errors. Note that a NULL
522  * description signifies the end of the table.
523  */
524 static ErrMsgTableEntry _lutErrorsGeneral_[] = {
525         { MGU_SUCCESS,          N_("Success") },
526         { MGU_BAD_ARGS,         N_("Bad arguments") },
527         { MGU_NO_FILE,          N_("File not specified") },
528         { MGU_OPEN_FILE,        N_("Error opening file") },
529         { MGU_ERROR_READ,       N_("Error reading file") },
530         { MGU_EOF,              N_("End of file encountered") },
531         { MGU_OO_MEMORY,        N_("Error allocating memory") },
532         { MGU_BAD_FORMAT,       N_("Bad file format") },
533         { MGU_ERROR_WRITE,      N_("Error writing to file") },
534         { MGU_OPEN_DIRECTORY,   N_("Error opening directory") },
535         { MGU_NO_PATH,          N_("No path specified") },
536         { 0,                    NULL }
537 };
538
539 #ifdef USE_LDAP
540 /**
541  * Lookup table of error messages for LDAP errors.
542  */
543 static ErrMsgTableEntry _lutErrorsLDAP_[] = {
544         { LDAPRC_SUCCESS,                       N_("Success") },
545         { LDAPRC_CONNECT,                       N_("Error connecting to LDAP server") },
546         { LDAPRC_INIT,                          N_("Error initializing LDAP") },
547         { LDAPRC_BIND,                          N_("Error binding to LDAP server") },
548         { LDAPRC_SEARCH,                        N_("Error searching LDAP database") },
549         { LDAPRC_TIMEOUT,                       N_("Timeout performing LDAP operation") },
550         { LDAPRC_CRITERIA,                      N_("Error in LDAP search criteria") },
551         { LDAPRC_NOENTRIES,                     N_("No LDAP entries found for search criteria") },
552         { LDAPRC_STOP_FLAG,                     N_("LDAP search terminated on request") },
553         { LDAPRC_TLS,                           N_("Error starting TLS connection") },
554         { LDAPRC_NODN,                          N_("Distinguished Name (dn) is missing") },
555         { LDAPRC_NAMING_VIOLATION,              N_("Missing required information") },
556         { LDAPRC_ALREADY_EXIST,                 N_("Another contact exists with that key") },
557         { LDAPRC_STRONG_AUTH,                   N_("Strong(er) authentication required") },
558         { 0,                                    NULL }
559 };
560 #endif
561
562 /**
563  * Lookup message for specified error code.
564  * \param lut  Lookup table.
565  * \param code Code to lookup.
566  * \return Description associated to code.
567  */
568 static gchar *addressbook_err2string( ErrMsgTableEntry lut[], gint code ) {
569         gchar *desc = NULL;
570         ErrMsgTableEntry entry;
571         gint i;
572
573         for( i = 0; ; i++ ) {
574                 entry = lut[ i ];
575                 if( entry.description == NULL ) break;
576                 if( entry.code == code ) {
577                         desc = entry.description;
578                         break;
579                 }
580         }
581         if( ! desc ) {
582                 desc = _errMsgUnknown_;
583         }
584         return desc;
585 }
586
587 static gboolean lastCanLookup = FALSE;
588
589 static void addressbook_show_buttons(gboolean add_and_delete, gboolean lookup, gboolean mail_ops)
590 {
591         if (add_and_delete) {
592                 gtk_widget_show(addrbook.edit_btn);
593                 gtk_widget_show(addrbook.del_btn);
594                 gtk_widget_show(addrbook.reg_btn);
595         } else {
596                 gtk_widget_hide(addrbook.edit_btn);
597                 gtk_widget_hide(addrbook.del_btn);
598                 gtk_widget_hide(addrbook.reg_btn);
599         }
600         
601         if (lookup) {
602                 gtk_widget_show(addrbook.lup_btn);
603                 gtk_widget_show(addrbook.entry);
604                 gtk_widget_show(addrbook.label);
605         } else {
606                 gtk_widget_hide(addrbook.lup_btn);
607                 gtk_widget_hide(addrbook.entry);
608                 gtk_widget_hide(addrbook.label);
609         }
610
611         lastCanLookup = lookup;
612
613         if (mail_ops) {
614                 gtk_widget_show(addrbook.to_btn);
615                 gtk_widget_show(addrbook.cc_btn);
616                 gtk_widget_show(addrbook.bcc_btn);
617         } else {
618                 gtk_widget_hide(addrbook.to_btn);
619                 gtk_widget_hide(addrbook.cc_btn);
620                 gtk_widget_hide(addrbook.bcc_btn);
621         }
622 }
623
624 void addressbook_open(Compose *target)
625 {
626         /* Initialize all static members */
627         if( _clipBoard_ == NULL ) {
628                 _clipBoard_ = addrclip_create();
629         }
630         if( _addressIndex_ != NULL ) {
631                 addrclip_set_index( _clipBoard_, _addressIndex_ );
632         }
633         if( _addressSelect_ == NULL ) {
634                 _addressSelect_ = addrselect_list_create();
635         }
636         if (!addrbook.window) {
637                 addressbook_read_file();
638                 addressbook_create();
639                 addressbook_load_tree();
640                 gtk_sctree_select( GTK_SCTREE(addrbook.ctree),
641                                  GTK_CTREE_NODE(GTK_CLIST(addrbook.ctree)->row_list));
642         }
643         else {
644                 gtk_widget_hide(addrbook.window);
645         }
646
647         gtk_widget_show_all(addrbook.window);
648 #ifdef MAEMO
649                 maemo_window_full_screen_if_needed(GTK_WINDOW(addrbook.window));
650                 maemo_connect_key_press_to_mainwindow(GTK_WINDOW(addrbook.window));
651 #endif
652         if (!prefs_common.addressbook_use_editaddress_dialog)
653                 addressbook_edit_person_widgetset_hide();
654
655         address_completion_start(addrbook.window);
656
657         addressbook_show_buttons(target == NULL, lastCanLookup, target != NULL);
658         addressbook_set_target_compose(target);
659 }
660
661 /**
662  * Destroy addressbook.
663  */
664 void addressbook_destroy( void ) {
665         /* Free up address stuff */
666         if( _addressSelect_ != NULL ) {
667                 addrselect_list_free( _addressSelect_ );
668         }
669         if( _clipBoard_ != NULL ) {
670                 addrclip_free( _clipBoard_ );
671         }
672         if( _addressIndex_ != NULL ) {
673                 addrindex_free_index( _addressIndex_ );
674                 addrindex_teardown();
675         }
676         _addressSelect_ = NULL;
677         _clipBoard_ = NULL;
678         _addressIndex_ = NULL;
679 }
680
681 void addressbook_set_target_compose(Compose *target)
682 {
683         addrbook.target_compose = target;
684         addressbook_button_set_sensitive();
685 }
686
687 Compose *addressbook_get_target_compose(void)
688 {
689         return addrbook.target_compose;
690 }
691
692 /**
693  * Refresh addressbook and save to file(s).
694  */
695 void addressbook_refresh( void )
696 {
697         if (addrbook.window) {
698                 if (addrbook.treeSelected) {
699                         gtk_sctree_select( GTK_SCTREE(addrbook.ctree),
700                                          addrbook.treeSelected);
701                         addressbook_set_clist(
702                                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
703                                         addrbook.treeSelected),
704                                 TRUE);
705
706                 }
707         }
708         addressbook_export_to_file();
709 }
710
711 static gboolean key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
712 {
713         if (event && event->keyval == GDK_Escape)
714                 addressbook_close();
715         else if (event && event->keyval == GDK_Delete) {
716                 /* TODO: enable deletion when focus is in ctree (needs implementation in _del_clicked() */
717                 if ( /* address_index_has_focus || */ address_list_has_focus )
718                         addressbook_del_clicked(NULL, NULL);
719         }
720         return FALSE;
721 }
722
723 /*!
724  *\brief        Save Gtk object size to prefs dataset
725  */
726 static void addressbook_size_allocate_cb(GtkWidget *widget,
727                                          GtkAllocation *allocation)
728 {
729         g_return_if_fail(allocation != NULL);
730
731         prefs_common.addressbookwin_width = allocation->width;
732         prefs_common.addressbookwin_height = allocation->height;
733 }
734
735 static gint sort_column_number = 0;
736 static GtkSortType sort_column_type = GTK_SORT_ASCENDING;
737
738 static gint list_case_sort(
739         GtkCList *clist, gconstpointer ptr1, gconstpointer ptr2 )
740 {
741         GtkCListRow *row1 = (GtkCListRow *) ptr1;
742         GtkCListRow *row2 = (GtkCListRow *) ptr2;
743         gchar *name1 = NULL, *name2 = NULL;
744         AddrItemObject *aio1 = ((GtkCListRow *)ptr1)->data;
745         AddrItemObject *aio2 = ((GtkCListRow *)ptr2)->data;
746
747         if( aio1->type == aio2->type ) {
748                 if( row1 ) 
749                         name1 = GTK_CELL_TEXT (row1->cell[sort_column_number])->text;
750                 if( row2 ) 
751                         name2 = GTK_CELL_TEXT (row2->cell[sort_column_number])->text;
752                 if( ! name1 ) return ( name2 != NULL );
753                 if( ! name2 ) return -1;
754                 return g_utf8_collate( name1, name2 );
755         } else {
756                 /* Order groups before person */
757                 if( aio1->type == ITEMTYPE_GROUP ) {
758                         return (sort_column_type==GTK_SORT_ASCENDING) ? -1:+1;
759                 } else if( aio2->type == ITEMTYPE_GROUP ) {
760                         return (sort_column_type==GTK_SORT_ASCENDING) ? +1:-1;
761                 }
762                 return 0;
763         }
764 }
765
766 static void addressbook_sort_list(GtkCList *clist, const gint col,
767                 const GtkSortType sort_type)
768 {
769         gint pos;
770         GtkWidget *hbox, *label, *arrow;
771
772         sort_column_number = col;
773         sort_column_type = sort_type;
774         gtk_clist_set_compare_func(clist, list_case_sort);
775         gtk_clist_set_sort_type(clist, sort_type);
776         gtk_clist_set_sort_column(clist, col);  
777
778         gtk_clist_freeze(clist);
779         gtk_clist_sort(clist);
780         
781         for(pos = 0 ; pos < N_LIST_COLS ; pos++) {
782                 hbox = gtk_hbox_new(FALSE, 4);
783                 label = gtk_label_new(gettext(list_titles[pos]));
784                 gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
785                 
786                 if(pos == col) {
787                         arrow = gtk_arrow_new(sort_type == GTK_SORT_ASCENDING ?
788                                 GTK_ARROW_DOWN : GTK_ARROW_UP, GTK_SHADOW_IN);
789                         gtk_box_pack_end(GTK_BOX(hbox), arrow, FALSE, FALSE, 0);
790                 }
791                 
792                 gtk_widget_show_all(hbox);
793                 gtk_clist_set_column_widget(clist, pos, hbox);
794         }
795         
796         gtk_clist_thaw(clist);  
797 }
798
799 static void addressbook_name_clicked(GtkWidget *button, GtkCList *clist)
800 {
801         static GtkSortType sort_type = GTK_SORT_ASCENDING;
802         
803         sort_type = (sort_type == GTK_SORT_ASCENDING) ? GTK_SORT_DESCENDING :
804                         GTK_SORT_ASCENDING;
805         addressbook_sort_list(clist, COL_NAME, sort_type);
806 }
807
808 static void addressbook_address_clicked(GtkWidget *button, GtkCList *clist)
809 {
810         static GtkSortType sort_type = GTK_SORT_ASCENDING;
811
812         sort_type = (sort_type == GTK_SORT_ASCENDING) ? GTK_SORT_DESCENDING :
813                         GTK_SORT_ASCENDING;
814         addressbook_sort_list(clist, COL_ADDRESS, sort_type);
815 }
816
817 static void addressbook_remarks_clicked(GtkWidget *button, GtkCList *clist)
818 {
819         static GtkSortType sort_type = GTK_SORT_ASCENDING;
820
821         sort_type = (sort_type == GTK_SORT_ASCENDING) ? GTK_SORT_DESCENDING :
822                         GTK_SORT_ASCENDING;
823         addressbook_sort_list(clist, COL_REMARKS, sort_type);
824 }
825
826 static gboolean addressbook_address_index_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
827                                                                                          gpointer data)
828 {
829         address_index_has_focus = TRUE;
830         return FALSE;
831 }
832
833 static gboolean addressbook_address_index_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
834                                                                                          gpointer data)
835 {
836         address_index_has_focus = FALSE;
837         if (!prefs_common.addressbook_use_editaddress_dialog
838                         && !address_list_has_focus)
839                 addressbook_address_list_disable_some_actions();
840         return FALSE;
841 }
842
843 static gboolean addressbook_address_list_focus_evt_in(GtkWidget *widget, GdkEventFocus *event,
844                                                                                          gpointer data)
845 {
846         address_list_has_focus = TRUE;
847         return FALSE;
848 }
849
850 static gboolean addressbook_address_list_focus_evt_out(GtkWidget *widget, GdkEventFocus *event,
851                                                                                          gpointer data)
852 {
853         address_list_has_focus = FALSE;
854         if (!prefs_common.addressbook_use_editaddress_dialog
855                         && !address_index_has_focus)
856                 addressbook_address_list_disable_some_actions();
857         return FALSE;
858 }
859
860 /* save hpane and vpane's handle position when it moves */
861 static void addressbook_pane_save_position(void)
862 {
863         if (addrbook.hpaned)
864                 prefs_common.addressbook_hpaned_pos = 
865                         gtk_paned_get_position(GTK_PANED(addrbook.hpaned));
866         if (addrbook.vpaned)
867                 prefs_common.addressbook_vpaned_pos = 
868                         gtk_paned_get_position(GTK_PANED(addrbook.vpaned));
869 }
870
871 /*
872 * Create the address book widgets. The address book contains two CTree widgets: the
873 * address index tree on the left and the address list on the right.
874 *
875 * The address index tree displays a hierarchy of interfaces and groups. Each node in
876 * this tree is linked to an address Adapter. Adapters have been created for interfaces,
877 * data sources and folder objects.
878 *
879 * The address list displays group, person and email objects. These items are linked
880 * directly to ItemGroup, ItemPerson and ItemEMail objects inside the address book data
881 * sources.
882 *
883 * In the tradition of MVC architecture, the data stores have been separated from the
884 * GUI components. The addrindex.c file provides the interface to all data stores.
885 */
886 static void addressbook_create(void)
887 {
888         GtkWidget *window;
889         GtkWidget *vbox;
890         GtkWidget *menubar;
891         GtkWidget *vbox2;
892         GtkWidget *ctree_swin;
893         GtkWidget *ctree;
894         GtkWidget *editaddress_vbox;
895         GtkWidget *clist_vbox;
896         GtkWidget *clist_swin;
897         GtkWidget *clist;
898         GtkWidget *hpaned;
899         GtkWidget *vpaned;
900         GtkWidget *hbox;
901         GtkWidget *label;
902         GtkWidget *entry;
903         GtkWidget *statusbar;
904         GtkWidget *hbbox;
905         GtkWidget *hsbox;
906         GtkWidget *help_btn;
907         GtkWidget *del_btn;
908         GtkWidget *edit_btn;
909         GtkWidget *reg_btn;
910         GtkWidget *lup_btn;
911         GtkWidget *to_btn;
912         GtkWidget *cc_btn;
913         GtkWidget *bcc_btn;
914         GtkWidget *close_btn;
915         GtkWidget *tree_popup;
916         GtkWidget *list_popup;
917         GtkItemFactory *tree_factory;
918         GtkItemFactory *list_factory;
919         GtkItemFactory *menu_factory;
920         gint n_entries;
921         GList *nodeIf;
922
923         gchar *index_titles[N_INDEX_COLS];
924         gchar *text;
925         gint i;
926
927         static GdkGeometry geometry;
928
929         debug_print("Creating addressbook window...\n");
930
931         index_titles[COL_SOURCES] = _("Sources");
932
933         /* Address book window */
934         window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "addressbook");
935         gtk_window_set_title(GTK_WINDOW(window), _("Address book"));
936         gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
937         gtk_widget_realize(window);
938
939         g_signal_connect(G_OBJECT(window), "delete_event",
940                          G_CALLBACK(addressbook_close), NULL);
941         g_signal_connect(G_OBJECT(window), "size_allocate",
942                          G_CALLBACK(addressbook_size_allocate_cb), NULL);
943         g_signal_connect(G_OBJECT(window), "key_press_event",
944                          G_CALLBACK(key_pressed), NULL);
945         MANAGE_WINDOW_SIGNALS_CONNECT(window);
946
947         vbox = gtk_vbox_new(FALSE, 0);
948         gtk_container_add(GTK_CONTAINER(window), vbox);
949
950         /* Menu bar */
951         n_entries = sizeof(addressbook_entries) /
952                 sizeof(addressbook_entries[0]);
953         menubar = menubar_create(window, addressbook_entries, n_entries,
954                                  "<AddressBook>", NULL);
955         gtk_box_pack_start(GTK_BOX(vbox), menubar, FALSE, TRUE, 0);
956         menu_factory = gtk_item_factory_from_widget(menubar);
957
958         vbox2 = gtk_vbox_new(FALSE, BORDER_WIDTH);
959         gtk_container_set_border_width(GTK_CONTAINER(vbox2), BORDER_WIDTH);
960         gtk_box_pack_start(GTK_BOX(vbox), vbox2, TRUE, TRUE, 0);
961
962         ctree_swin = gtk_scrolled_window_new(NULL, NULL);
963         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(ctree_swin),
964                                        GTK_POLICY_AUTOMATIC,
965                                        GTK_POLICY_AUTOMATIC);
966         gtk_widget_set_size_request(ctree_swin, COL_FOLDER_WIDTH + 20, -1);
967
968         /* Address index */
969         ctree = gtk_sctree_new_with_titles(N_INDEX_COLS, 0, index_titles);
970         gtk_container_add(GTK_CONTAINER(ctree_swin), ctree);
971         gtk_clist_set_selection_mode(GTK_CLIST(ctree), GTK_SELECTION_BROWSE);
972         gtk_clist_set_column_width(GTK_CLIST(ctree), 0, COL_FOLDER_WIDTH);
973         gtk_ctree_set_line_style(GTK_CTREE(ctree), GTK_CTREE_LINES_DOTTED);
974         gtk_ctree_set_expander_style(GTK_CTREE(ctree),
975                                      GTK_CTREE_EXPANDER_SQUARE);
976         gtk_ctree_set_indent(GTK_CTREE(ctree), CTREE_INDENT);
977         gtk_clist_set_compare_func(GTK_CLIST(ctree),
978                                    addressbook_treenode_compare_func);
979
980         g_signal_connect(G_OBJECT(ctree), "tree_select_row",
981                          G_CALLBACK(addressbook_tree_selected), NULL);
982         g_signal_connect(G_OBJECT(ctree), "button_press_event",
983                          G_CALLBACK(addressbook_tree_button_pressed),
984                          NULL);
985         g_signal_connect(G_OBJECT(ctree), "button_release_event",
986                          G_CALLBACK(addressbook_tree_button_released),
987                          NULL);
988         /* TEMPORARY */
989         g_signal_connect(G_OBJECT(ctree), "select_row",
990                          G_CALLBACK(addressbook_select_row_tree), NULL);
991
992         gtk_drag_dest_set(ctree, GTK_DEST_DEFAULT_ALL & ~GTK_DEST_DEFAULT_HIGHLIGHT,
993                           addressbook_drag_types, 1,
994                           GDK_ACTION_MOVE | GDK_ACTION_COPY | GDK_ACTION_DEFAULT);
995         g_signal_connect(G_OBJECT(ctree), "drag_motion",
996                          G_CALLBACK(addressbook_drag_motion_cb),
997                          ctree);
998         g_signal_connect(G_OBJECT(ctree), "drag_leave",
999                          G_CALLBACK(addressbook_drag_leave_cb),
1000                          ctree);
1001         g_signal_connect(G_OBJECT(ctree), "drag_data_received",
1002                          G_CALLBACK(addressbook_drag_received_cb),
1003                          ctree);
1004         g_signal_connect(G_OBJECT(ctree), "focus_in_event",
1005                 G_CALLBACK(addressbook_address_index_focus_evt_in), NULL);
1006         g_signal_connect(G_OBJECT(ctree), "focus_out_event",
1007                 G_CALLBACK(addressbook_address_index_focus_evt_out), NULL);
1008
1009         clist_vbox = gtk_vbox_new(FALSE, 4);
1010
1011         clist_swin = gtk_scrolled_window_new(NULL, NULL);
1012         gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(clist_swin),
1013                                        GTK_POLICY_AUTOMATIC,
1014                                        GTK_POLICY_AUTOMATIC);
1015         gtk_box_pack_start(GTK_BOX(clist_vbox), clist_swin, TRUE, TRUE, 0);
1016
1017         /* Address list */
1018         clist = gtk_sctree_new_with_titles(N_LIST_COLS, 0, list_titles);
1019         gtk_container_add(GTK_CONTAINER(clist_swin), clist);
1020         gtk_clist_set_selection_mode(GTK_CLIST(clist), GTK_SELECTION_EXTENDED);
1021         gtk_ctree_set_line_style(GTK_CTREE(clist), GTK_CTREE_LINES_NONE);
1022         gtk_ctree_set_expander_style(GTK_CTREE(clist), GTK_CTREE_EXPANDER_SQUARE);
1023         gtk_ctree_set_indent(GTK_CTREE(clist), CTREE_INDENT);
1024         gtk_clist_set_column_width(GTK_CLIST(clist), COL_NAME,
1025                                    COL_NAME_WIDTH);
1026         gtk_clist_set_column_width(GTK_CLIST(clist), COL_ADDRESS,
1027                                    COL_ADDRESS_WIDTH);
1028         gtk_widget_set_size_request(clist, -1, 80);
1029
1030         addressbook_sort_list(GTK_CLIST(clist), COL_NAME, GTK_SORT_ASCENDING);
1031         g_signal_connect(G_OBJECT(GTK_CLIST(clist)->column[COL_NAME].button),
1032                 "clicked", G_CALLBACK(addressbook_name_clicked), clist);
1033         g_signal_connect(G_OBJECT(GTK_CLIST(clist)->column[COL_ADDRESS].button),
1034                 "clicked", G_CALLBACK(addressbook_address_clicked), clist);
1035         g_signal_connect(G_OBJECT(GTK_CLIST(clist)->column[COL_REMARKS].button),
1036                 "clicked", G_CALLBACK(addressbook_remarks_clicked), clist);
1037         g_signal_connect(G_OBJECT(clist), "focus_in_event",
1038                 G_CALLBACK(addressbook_address_list_focus_evt_in), NULL);
1039         g_signal_connect(G_OBJECT(clist), "focus_out_event",
1040                 G_CALLBACK(addressbook_address_list_focus_evt_out), NULL);
1041
1042         for (i = 0; i < N_LIST_COLS; i++)
1043                 GTK_WIDGET_UNSET_FLAGS(GTK_CLIST(clist)->column[i].button,
1044                                        GTK_CAN_FOCUS);
1045
1046         g_signal_connect(G_OBJECT(clist), "tree_select_row",
1047                          G_CALLBACK(addressbook_list_row_selected), NULL);
1048         g_signal_connect(G_OBJECT(clist), "tree_unselect_row",
1049                          G_CALLBACK(addressbook_list_row_unselected), NULL);
1050         g_signal_connect(G_OBJECT(clist), "button_press_event",
1051                          G_CALLBACK(addressbook_list_button_pressed),
1052                          NULL);
1053         g_signal_connect(G_OBJECT(clist), "button_release_event",
1054                          G_CALLBACK(addressbook_list_button_released),
1055                          NULL);
1056         g_signal_connect(G_OBJECT(clist), "tree_expand",
1057                          G_CALLBACK(addressbook_person_expand_node), NULL );
1058         g_signal_connect(G_OBJECT(clist), "tree_collapse",
1059                          G_CALLBACK(addressbook_person_collapse_node), NULL );
1060         g_signal_connect(G_OBJECT(clist), "start_drag",
1061                          G_CALLBACK(addressbook_start_drag), NULL);
1062         g_signal_connect(G_OBJECT(clist), "drag_data_get",
1063                          G_CALLBACK(addressbook_drag_data_get), NULL);  
1064         hbox = gtk_hbox_new(FALSE, 4);
1065         gtk_box_pack_start(GTK_BOX(clist_vbox), hbox, FALSE, FALSE, 0);
1066
1067         label = gtk_label_new(_("Lookup name:"));
1068         gtk_box_pack_start(GTK_BOX(hbox), label, FALSE, FALSE, 0);
1069
1070         entry = gtk_entry_new();
1071         gtk_box_pack_start(GTK_BOX(hbox), entry, TRUE, TRUE, 0);
1072
1073         address_completion_register_entry(GTK_ENTRY(entry), FALSE);
1074
1075         g_signal_connect(G_OBJECT(entry), "key_press_event",
1076                          G_CALLBACK(addressbook_entry_key_pressed),
1077                          NULL);
1078
1079         if (!prefs_common.addressbook_use_editaddress_dialog) {
1080                 editaddress_vbox = gtk_vbox_new(FALSE, 4);
1081                 vpaned = gtk_vpaned_new();
1082                 gtk_paned_pack1(GTK_PANED(vpaned), clist_vbox, FALSE, FALSE);
1083                 gtk_paned_pack2(GTK_PANED(vpaned), editaddress_vbox, TRUE, FALSE);
1084         } else {
1085                 vpaned = NULL;
1086                 editaddress_vbox = NULL;
1087         }
1088         hpaned = gtk_hpaned_new();
1089         gtk_box_pack_start(GTK_BOX(vbox2), hpaned, TRUE, TRUE, 0);
1090         gtk_paned_pack1(GTK_PANED(hpaned), ctree_swin, FALSE, FALSE);
1091         if (prefs_common.addressbook_use_editaddress_dialog)
1092                 gtk_paned_pack2(GTK_PANED(hpaned), clist_vbox, TRUE, FALSE);
1093         else
1094                 gtk_paned_pack2(GTK_PANED(hpaned), vpaned, TRUE, FALSE);
1095
1096         /* Status bar */
1097         hsbox = gtk_hbox_new(FALSE, 0);
1098         gtk_box_pack_end(GTK_BOX(vbox), hsbox, FALSE, FALSE, BORDER_WIDTH);
1099         statusbar = gtk_statusbar_new();
1100         gtk_box_pack_start(GTK_BOX(hsbox), statusbar, TRUE, TRUE, BORDER_WIDTH);
1101
1102         /* Button panel */
1103         hbbox = gtk_hbutton_box_new();
1104         gtk_button_box_set_layout(GTK_BUTTON_BOX(hbbox), GTK_BUTTONBOX_END);
1105         gtk_box_set_spacing(GTK_BOX(hbbox), 2);
1106         gtk_container_set_border_width(GTK_CONTAINER(hbbox), 4);
1107         gtk_box_pack_end(GTK_BOX(vbox), hbbox, FALSE, FALSE, 0);
1108
1109         gtkut_stock_button_add_help(hbbox, &help_btn);
1110
1111         edit_btn = gtk_button_new_from_stock(GTK_STOCK_EDIT);
1112         GTK_WIDGET_SET_FLAGS(edit_btn, GTK_CAN_DEFAULT);
1113         gtk_box_pack_start(GTK_BOX(hbbox), edit_btn, TRUE, TRUE, 0);
1114         del_btn = gtk_button_new_from_stock(GTK_STOCK_DELETE);
1115         GTK_WIDGET_SET_FLAGS(del_btn, GTK_CAN_DEFAULT);
1116         gtk_box_pack_start(GTK_BOX(hbbox), del_btn, TRUE, TRUE, 0);
1117         reg_btn = gtk_button_new_from_stock(GTK_STOCK_NEW);
1118         GTK_WIDGET_SET_FLAGS(reg_btn, GTK_CAN_DEFAULT);
1119         gtk_box_pack_start(GTK_BOX(hbbox), reg_btn, TRUE, TRUE, 0);
1120
1121
1122         lup_btn = gtk_button_new_from_stock(GTK_STOCK_FIND);
1123         GTK_WIDGET_SET_FLAGS(lup_btn, GTK_CAN_DEFAULT);
1124         gtk_box_pack_start(GTK_BOX(hbox), lup_btn, TRUE, TRUE, 0);
1125
1126         g_signal_connect(G_OBJECT(help_btn), "clicked",
1127                          G_CALLBACK(manual_open_with_anchor_cb),
1128                          MANUAL_ANCHOR_ADDRBOOK);
1129
1130         g_signal_connect(G_OBJECT(edit_btn), "clicked",
1131                          G_CALLBACK(addressbook_edit_clicked), NULL);
1132         g_signal_connect(G_OBJECT(del_btn), "clicked",
1133                          G_CALLBACK(addressbook_del_clicked), NULL);
1134         g_signal_connect(G_OBJECT(reg_btn), "clicked",
1135                          G_CALLBACK(addressbook_reg_clicked), NULL);
1136         g_signal_connect(G_OBJECT(lup_btn), "clicked",
1137                          G_CALLBACK(addressbook_lup_clicked), NULL);
1138
1139         to_btn = gtk_button_new_with_label
1140                 (prefs_common_translated_header_name("To:"));
1141         GTK_WIDGET_SET_FLAGS(to_btn, GTK_CAN_DEFAULT);
1142         gtk_box_pack_start(GTK_BOX(hbbox), to_btn, TRUE, TRUE, 0);
1143         cc_btn = gtk_button_new_with_label
1144                 (prefs_common_translated_header_name("Cc:"));
1145         GTK_WIDGET_SET_FLAGS(cc_btn, GTK_CAN_DEFAULT);
1146         gtk_box_pack_start(GTK_BOX(hbbox), cc_btn, TRUE, TRUE, 0);
1147         bcc_btn = gtk_button_new_with_label
1148                 (prefs_common_translated_header_name("Bcc:"));
1149         GTK_WIDGET_SET_FLAGS(bcc_btn, GTK_CAN_DEFAULT);
1150         gtk_box_pack_start(GTK_BOX(hbbox), bcc_btn, TRUE, TRUE, 0);
1151
1152         close_btn = gtk_button_new_from_stock(GTK_STOCK_CLOSE);
1153         GTK_WIDGET_SET_FLAGS(close_btn, GTK_CAN_DEFAULT);
1154         gtk_box_pack_start(GTK_BOX(hbbox), close_btn, TRUE, TRUE, 0);
1155
1156         g_signal_connect(G_OBJECT(to_btn), "clicked",
1157                          G_CALLBACK(addressbook_to_clicked),
1158                          GINT_TO_POINTER(COMPOSE_TO));
1159         g_signal_connect(G_OBJECT(cc_btn), "clicked",
1160                          G_CALLBACK(addressbook_to_clicked),
1161                          GINT_TO_POINTER(COMPOSE_CC));
1162         g_signal_connect(G_OBJECT(bcc_btn), "clicked",
1163                          G_CALLBACK(addressbook_to_clicked),
1164                          GINT_TO_POINTER(COMPOSE_BCC));
1165         g_signal_connect(G_OBJECT(close_btn), "clicked",
1166                          G_CALLBACK(addressbook_close_clicked), NULL);
1167
1168         /* Build icons for interface */
1169         stock_pixmap_gdk( window, STOCK_PIXMAP_INTERFACE,
1170                           &interfacexpm, &interfacexpmmask );
1171
1172         /* Build control tables */
1173         addrbookctl_build_map(window);
1174         addrbookctl_build_iflist();
1175         addrbookctl_build_ifselect();
1176
1177         addrbook.clist   = NULL;
1178
1179         /* Add each interface into the tree as a root level folder */
1180         nodeIf = _addressInterfaceList_;
1181         while( nodeIf ) {
1182                 AdapterInterface *adapter = nodeIf->data;
1183                 AddressInterface *iface = adapter->interface;
1184                 nodeIf = g_list_next(nodeIf);
1185
1186                 if(iface->useInterface) {
1187                         AddressTypeControlItem *atci = adapter->atci;
1188                         text = atci->displayName;
1189                         adapter->treeNode =
1190                                 gtk_sctree_insert_node( GTK_CTREE(ctree),
1191                                         NULL, NULL, &text, FOLDER_SPACING,
1192                                         interfacexpm, interfacexpmmask,
1193                                         interfacexpm, interfacexpmmask,
1194                                         FALSE, FALSE );
1195                         menu_set_sensitive( menu_factory, atci->menuCommand, adapter->haveLibrary );
1196                         gtk_ctree_node_set_row_data_full(
1197                                 GTK_CTREE(ctree), adapter->treeNode, adapter,
1198                                 addressbook_free_treenode );
1199                 }
1200         }
1201
1202         /* Popup menu */
1203         n_entries = sizeof(addressbook_tree_popup_entries) /
1204                 sizeof(addressbook_tree_popup_entries[0]);
1205         tree_popup = menu_create_items(addressbook_tree_popup_entries,
1206                                        n_entries,
1207                                        "<AddressBookTree>", &tree_factory,
1208                                        NULL);
1209         n_entries = sizeof(addressbook_list_popup_entries) /
1210                 sizeof(addressbook_list_popup_entries[0]);
1211         list_popup = menu_create_items(addressbook_list_popup_entries,
1212                                        n_entries,
1213                                        "<AddressBookList>", &list_factory,
1214                                        NULL);
1215
1216         addrbook.window  = window;
1217         addrbook.hpaned  = hpaned;
1218         addrbook.vpaned  = vpaned;
1219         addrbook.menubar = menubar;
1220         addrbook.ctree   = ctree;
1221         addrbook.ctree_swin
1222                          = ctree_swin;
1223         addrbook.editaddress_vbox = editaddress_vbox;
1224         addrbook.clist   = clist;
1225         addrbook.label   = label;
1226         addrbook.entry   = entry;
1227         addrbook.statusbar = statusbar;
1228         addrbook.status_cid = gtk_statusbar_get_context_id(
1229                         GTK_STATUSBAR(statusbar), "Addressbook Window" );
1230
1231         addrbook.help_btn = help_btn;
1232         addrbook.edit_btn = edit_btn;
1233         addrbook.del_btn = del_btn;
1234         addrbook.reg_btn = reg_btn;
1235         addrbook.lup_btn = lup_btn;
1236         addrbook.to_btn  = to_btn;
1237         addrbook.cc_btn  = cc_btn;
1238         addrbook.bcc_btn = bcc_btn;
1239
1240         addrbook.tree_popup   = tree_popup;
1241         addrbook.list_popup   = list_popup;
1242         addrbook.tree_factory = tree_factory;
1243         addrbook.list_factory = list_factory;
1244         addrbook.menu_factory = menu_factory;
1245
1246         addrbook.listSelected = NULL;
1247
1248         if (!geometry.min_height) {
1249                 geometry.min_width = ADDRESSBOOK_WIDTH;
1250                 geometry.min_height = ADDRESSBOOK_HEIGHT;
1251         }
1252
1253         gtk_window_set_geometry_hints(GTK_WINDOW(window), NULL, &geometry,
1254                                       GDK_HINT_MIN_SIZE);
1255         gtk_widget_set_size_request(window, prefs_common.addressbookwin_width,
1256                                     prefs_common.addressbookwin_height);
1257
1258         if (!prefs_common.addressbook_use_editaddress_dialog) {
1259                 if (prefs_common.addressbook_vpaned_pos > 0)
1260                         gtk_paned_set_position(GTK_PANED(vpaned), 
1261                                 prefs_common.addressbook_vpaned_pos);
1262         }       
1263         if (prefs_common.addressbook_hpaned_pos > 0)
1264                 gtk_paned_set_position(GTK_PANED(hpaned), 
1265                         prefs_common.addressbook_hpaned_pos);
1266
1267
1268         gtk_widget_show_all(window);
1269 }
1270
1271 /**
1272  * Close address book window and save to file(s).
1273  */
1274 static gint addressbook_close( void ) {
1275         address_completion_end(addrbook.window);
1276         if (!prefs_common.addressbook_use_editaddress_dialog)
1277                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
1278         
1279         addressbook_pane_save_position();
1280         
1281         gtk_widget_hide(addrbook.window);
1282         addressbook_export_to_file();
1283         return TRUE;
1284 }
1285
1286 /**
1287  * Display message in status line.
1288  * \param msg Message to display.
1289  */
1290 static void addressbook_status_show( gchar *msg ) {
1291         if( addrbook.statusbar != NULL ) {
1292                 gtk_statusbar_pop(
1293                         GTK_STATUSBAR(addrbook.statusbar),
1294                         addrbook.status_cid );
1295                 if( msg ) {
1296                         gtk_statusbar_push(
1297                                 GTK_STATUSBAR(addrbook.statusbar),
1298                                 addrbook.status_cid, msg );
1299                 }
1300         }
1301 }
1302
1303 static void addressbook_ds_status_message( AddressDataSource *ds, gchar *msg ) {
1304         *addressbook_msgbuf = '\0';
1305         if( ds ) {
1306                 gchar *name;
1307
1308                 name = addrindex_ds_get_name( ds );
1309                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
1310                             "%s: %s", name, msg );
1311         }
1312         else {
1313                 g_snprintf( addressbook_msgbuf, sizeof(addressbook_msgbuf),
1314                             "%s", msg );
1315         }
1316         addressbook_status_show( addressbook_msgbuf );
1317 }
1318
1319 static void addressbook_ds_show_message( AddressDataSource *ds ) {
1320         gint retVal;
1321         gchar *name;
1322         gchar *desc;
1323         *addressbook_msgbuf = '\0';
1324         if( ds ) {
1325                 name = addrindex_ds_get_name( ds );
1326                 retVal = addrindex_ds_get_status_code( ds );
1327                 if( retVal == MGU_SUCCESS ) {
1328                         g_snprintf( addressbook_msgbuf,
1329                                     sizeof(addressbook_msgbuf), "%s", name );
1330                 }
1331                 else {
1332                         desc = addressbook_err2string( _lutErrorsGeneral_, retVal );
1333                         g_snprintf( addressbook_msgbuf, 
1334                             sizeof(addressbook_msgbuf), "%s: %s", name, desc );
1335                 }
1336         }
1337         addressbook_status_show( addressbook_msgbuf );
1338 }
1339
1340 static void addressbook_button_set_sensitive(void)
1341 {
1342         gboolean to_sens  = FALSE;
1343         gboolean cc_sens  = FALSE;
1344         gboolean bcc_sens = FALSE;
1345
1346         if (!addrbook.window) return;
1347
1348         if (addrbook.target_compose) {
1349                 to_sens = TRUE;
1350                 cc_sens = TRUE;
1351                 bcc_sens = TRUE;
1352         }
1353
1354         gtk_widget_set_sensitive(addrbook.to_btn, to_sens);
1355         gtk_widget_set_sensitive(addrbook.cc_btn, cc_sens);
1356         gtk_widget_set_sensitive(addrbook.bcc_btn, bcc_sens);
1357 }
1358
1359 static void addressbook_edit_clicked(GtkButton *button, gpointer data)
1360 {
1361         addressbook_edit_address_cb(NULL, 0, NULL);
1362 }
1363
1364 /*
1365 * Delete one or more objects from address list.
1366 */
1367 static void addressbook_del_clicked(GtkButton *button, gpointer data)
1368 {
1369         GtkCTree *clist = GTK_CTREE(addrbook.clist);
1370         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1371         AddressObject *pobj;
1372         AdapterDSource *ads = NULL;
1373         GtkCTreeNode *nodeList;
1374         gboolean procFlag;
1375         AlertValue aval;
1376         AddressBookFile *abf = NULL;
1377         AddressDataSource *ds = NULL;
1378         AddressInterface *iface;
1379         AddrItemObject *aio;
1380         AddrSelectItem *item;
1381         GList *list, *node;
1382         gboolean refreshList = FALSE;
1383         
1384         pobj = gtk_ctree_node_get_row_data(ctree, addrbook.opened );
1385         g_return_if_fail(pobj != NULL);
1386
1387         /* Test whether anything selected for deletion */
1388         nodeList = addrbook.listSelected;
1389
1390         aio = gtk_ctree_node_get_row_data( clist, nodeList );
1391         if( aio == NULL) return;
1392         ds = addressbook_find_datasource( addrbook.treeSelected );
1393         if( ds == NULL ) return;
1394
1395         /* Test for read only */
1396         iface = ds->interface;
1397         if( iface->readOnly ) {
1398                 alertpanel( _("Delete address(es)"),
1399                         _("This address data is readonly and cannot be deleted."),
1400                         GTK_STOCK_CLOSE, NULL, NULL );
1401                 return;
1402         }
1403
1404         /* Test whether Ok to proceed */
1405         procFlag = FALSE;
1406         if( pobj->type == ADDR_DATASOURCE ) {
1407                 ads = ADAPTER_DSOURCE(pobj);
1408                 if( ads->subType == ADDR_BOOK ) procFlag = TRUE;
1409         }
1410         else if( pobj->type == ADDR_ITEM_FOLDER ) {
1411                 procFlag = TRUE;
1412         }
1413         else if( pobj->type == ADDR_ITEM_GROUP ) {
1414                 procFlag = TRUE;
1415         }
1416         if( ! procFlag ) return;
1417         abf = ds->rawDataSource;
1418         if( abf == NULL ) return;
1419
1420
1421         /* Process deletions */
1422         if( pobj->type == ADDR_DATASOURCE || pobj->type == ADDR_ITEM_FOLDER ) {
1423                 gboolean group_delete = TRUE;
1424                 /* Items inside folders */
1425                 list = addrselect_get_list( _addressSelect_ );
1426                 /* Confirm deletion */
1427                 node = list;
1428                 while( node ) {
1429                         item = node->data;
1430                         node = g_list_next( node );
1431                         aio = ( AddrItemObject * ) item->addressItem;
1432                         if( aio->type == ADDR_ITEM_PERSON || aio->type == ADDR_ITEM_EMAIL ) {
1433                                 group_delete = FALSE;
1434                                 break;
1435                         }
1436                 }
1437                 if (group_delete) {
1438                         aval = alertpanel( _("Delete group"),
1439                                         _("Really delete the group(s)?\n"
1440                                           "The addresses it contains will not be lost."),
1441                                         GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
1442                         if( aval != G_ALERTALTERNATE ) return;
1443                 } else {
1444                         aval = alertpanel( _("Delete address(es)"),
1445                                         _("Really delete the address(es)?"),
1446                                         GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
1447                         if( aval != G_ALERTALTERNATE ) return;
1448                 }
1449
1450                 node = list;
1451                 while( node ) {
1452                         item = node->data;
1453                         node = g_list_next( node );
1454                         aio = ( AddrItemObject * ) item->addressItem;
1455                         if (!aio)
1456                                 continue;
1457                         if( aio->type == ADDR_ITEM_GROUP ) {
1458                                 ItemGroup *item = ( ItemGroup * ) aio;
1459                                 GtkCTreeNode *nd = NULL;
1460
1461                                 nd = addressbook_find_group_node( addrbook.opened, item );
1462                                 item = addrbook_remove_group( abf, item );
1463                                 if( item ) {
1464                                         addritem_free_item_group( item );
1465                                 }
1466                                 /* Remove group from parent node */
1467                                 gtk_ctree_remove_node( ctree, nd );
1468                                 refreshList = TRUE;
1469                         }
1470                         else if( aio->type == ADDR_ITEM_PERSON ) {
1471                                 ItemPerson *item = ( ItemPerson * ) aio;
1472                                 item->status = DELETE_ENTRY; 
1473                                 addressbook_folder_remove_one_person( clist, item );
1474                                 if (pobj->type == ADDR_ITEM_FOLDER)
1475                                         addritem_folder_remove_person(ADAPTER_FOLDER(pobj)->itemFolder, item);
1476                                 item = addrbook_remove_person( abf, item );
1477 #ifdef USE_LDAP
1478                                 if (ds && ds->type == ADDR_IF_LDAP) {
1479                                         LdapServer *server = ds->rawDataSource;
1480                                         ldapsvr_set_modified(server, TRUE);
1481                                         ldapsvr_update_book(server, item);
1482                                 }
1483 #endif
1484                                 if( item ) {
1485                                         gchar *filename = addritem_person_get_picture(item);
1486                                         if (filename && is_file_exist(filename))
1487                                                 g_unlink(filename);
1488                                         g_free(filename);
1489                                         addritem_free_item_person( item );
1490                                 }
1491                         }
1492                         else if( aio->type == ADDR_ITEM_EMAIL ) {
1493                                 ItemEMail *item = ( ItemEMail * ) aio;
1494                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1495                                 item = addrbook_person_remove_email( abf, person, item );
1496                                 if( item ) {
1497                                         addrcache_remove_email(abf->addressCache, item);
1498                                         addritem_free_item_email( item );
1499                                 }
1500                                 addressbook_folder_refresh_one_person( clist, person );
1501                         }
1502                 }
1503                 g_list_free( list );
1504                 addressbook_list_select_clear();
1505                 if( refreshList ) {
1506                         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened);
1507                         addressbook_set_clist(
1508                                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
1509                                         addrbook.opened),
1510                                 TRUE);
1511                 }
1512                 addrbook_set_dirty(abf, TRUE);
1513                 addressbook_export_to_file();
1514                 addressbook_list_menu_setup();
1515                 return;
1516         }
1517         else if( pobj->type == ADDR_ITEM_GROUP ) {
1518                 /* Items inside groups */
1519                 list = addrselect_get_list( _addressSelect_ );
1520                 node = list;
1521                 while( node ) {
1522                         item = node->data;
1523                         node = g_list_next( node );
1524                         aio = ( AddrItemObject * ) item->addressItem;
1525                         if( aio->type == ADDR_ITEM_EMAIL ) {
1526                                 ItemEMail *item = ( ItemEMail * ) aio;
1527                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1528                                 item = addrbook_person_remove_email( abf, person, item );
1529                                 if( item ) {
1530                                         addritem_free_item_email( item );
1531                                 }
1532                         }
1533                 }
1534                 g_list_free( list );
1535                 addressbook_list_select_clear();
1536                 gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened);
1537                 addressbook_set_clist(
1538                         gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
1539                                 addrbook.opened),
1540                         TRUE);
1541                 
1542                 addrbook_set_dirty(abf, TRUE);
1543                 addressbook_export_to_file();
1544                 addressbook_list_menu_setup();
1545                 return;
1546         }
1547
1548         gtk_ctree_node_set_row_data( clist, nodeList, NULL );
1549         gtk_ctree_remove_node( clist, nodeList );
1550
1551 }
1552
1553 static void addressbook_reg_clicked(GtkButton *button, gpointer data)
1554 {
1555         addressbook_new_address_cb( NULL, 0, NULL );
1556 }
1557
1558 static gchar *addressbook_format_address( AddrItemObject * aio ) {
1559         gchar *buf = NULL;
1560         gchar *name = NULL;
1561         gchar *address = NULL;
1562
1563         if( aio->type == ADDR_ITEM_EMAIL ) {
1564                 ItemPerson *person = NULL;
1565                 ItemEMail *email = ( ItemEMail * ) aio;
1566
1567                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
1568                 if( email->address ) {
1569                         if( ADDRITEM_NAME(email) ) {
1570                                 name = ADDRITEM_NAME(email);
1571                                 if( *name == '\0' ) {
1572                                         name = ADDRITEM_NAME(person);
1573                                 }
1574                         }
1575                         else if( ADDRITEM_NAME(person) ) {
1576                                 name = ADDRITEM_NAME(person);
1577                         }
1578                         else {
1579                                 buf = g_strdup( email->address );
1580                         }
1581                         address = email->address;
1582                 }
1583         }
1584         else if( aio->type == ADDR_ITEM_PERSON ) {
1585                 ItemPerson *person = ( ItemPerson * ) aio;
1586                 GList *node = person->listEMail;
1587
1588                 name = ADDRITEM_NAME(person);
1589                 if( node ) {
1590                         ItemEMail *email = ( ItemEMail * ) node->data;
1591                         address = email->address;
1592                 }
1593         }
1594         if( address ) {
1595                 if( name && name[0] != '\0' ) {
1596                         if( strchr_with_skip_quote( name, '"', ',' ) )
1597                                 buf = g_strdup_printf( "\"%s\" <%s>", name, address );
1598                         else
1599                                 buf = g_strdup_printf( "%s <%s>", name, address );
1600                 }
1601                 else {
1602                         buf = g_strdup( address );
1603                 }
1604         }
1605
1606         return buf;
1607 }
1608
1609 static void addressbook_to_clicked(GtkButton *button, gpointer data)
1610 {
1611         GList *list, *node;
1612         Compose *compose;
1613         AddrSelectItem *item;
1614         AddrItemObject *aio;
1615         gchar *addr;
1616
1617         compose = addrbook.target_compose;
1618         if( ! compose ) return;
1619
1620         /* Nothing selected, but maybe there is something in text entry */
1621         addr = (char *)gtk_entry_get_text( GTK_ENTRY( addrbook.entry) );
1622         if ( addr ) {
1623                 compose_entry_append(
1624                         compose, addr, (ComposeEntryType)data );
1625         }
1626
1627         /* Select from address list */
1628         list = addrselect_get_list( _addressSelect_ );
1629         node = list;
1630         if (node) {
1631                 while( node ) {
1632                         item = node->data;
1633                         node = g_list_next( node );
1634                         aio = item->addressItem;
1635                         if( aio->type == ADDR_ITEM_PERSON ||
1636                             aio->type == ADDR_ITEM_EMAIL ) {
1637                                 addr = addressbook_format_address( aio );
1638                                 compose_entry_append(
1639                                         compose, addr, (ComposeEntryType) data );
1640                                 g_free( addr );
1641                         }
1642                         else if( aio->type == ADDR_ITEM_GROUP ) {
1643                                 ItemGroup *group = ( ItemGroup * ) aio;
1644                                 GList *nodeMail = group->listEMail;
1645                                 while( nodeMail ) {
1646                                         ItemEMail *email = nodeMail->data;
1647
1648                                         addr = addressbook_format_address(
1649                                                         ( AddrItemObject * ) email );
1650                                         compose_entry_append(
1651                                                 compose, addr, (ComposeEntryType) data );
1652                                         g_free( addr );
1653                                         nodeMail = g_list_next( nodeMail );
1654                                 }
1655                         }
1656                 }
1657         } else {
1658                 AddressObject *obj = NULL;
1659
1660                 obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1661         
1662                 if( obj && obj->type == ADDR_ITEM_GROUP ) {
1663                         ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
1664                         GList *nodeMail = itemGroup->listEMail;
1665                         while( nodeMail ) {
1666                                 ItemEMail *email = nodeMail->data;
1667
1668                                 addr = addressbook_format_address(
1669                                                 ( AddrItemObject * ) email );
1670                                 compose_entry_append(
1671                                         compose, addr, (ComposeEntryType) data );
1672                                 g_free( addr );
1673                                 nodeMail = g_list_next( nodeMail );
1674                         }
1675                 }
1676         }
1677         g_list_free( list );
1678 }
1679
1680 static void addressbook_menubar_set_sensitive( gboolean sensitive ) {
1681         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",   sensitive );
1682         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book", sensitive );
1683         menu_set_sensitive( addrbook.menu_factory, "/Book/New Folder",  sensitive );
1684
1685         menu_set_sensitive( addrbook.menu_factory, "/Address/Select all",    TRUE );
1686         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",    sensitive );
1687         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",   sensitive );
1688         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",  sensitive );
1689
1690         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", sensitive );
1691         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   sensitive );
1692         menu_set_sensitive( addrbook.menu_factory, "/Address/Mail To",     sensitive );
1693         gtk_widget_set_sensitive( addrbook.reg_btn, sensitive );
1694         gtk_widget_set_sensitive( addrbook.edit_btn, sensitive );
1695         gtk_widget_set_sensitive( addrbook.del_btn, sensitive );
1696 }
1697
1698 static void addressbook_menuitem_set_sensitive( AddressObject *obj, GtkCTreeNode *node ) {
1699         gboolean canEdit = FALSE;
1700         gboolean canDelete = TRUE;
1701         gboolean canAdd = FALSE;
1702         gboolean canEditTr = TRUE;
1703         gboolean editAddress = FALSE;
1704         gboolean canExport = TRUE;
1705         AddressTypeControlItem *atci = NULL;
1706         AddressDataSource *ds = NULL;
1707         AddressInterface *iface = NULL;
1708
1709         if( obj == NULL ) return;
1710         if( obj->type == ADDR_INTERFACE ) {
1711                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
1712                 iface = adapter->interface;
1713                 if( iface ) {
1714                         if( iface->haveLibrary ) {
1715                                 /* Enable appropriate File / New command */
1716                                 atci = adapter->atci;
1717                                 menu_set_sensitive( addrbook.menu_factory, atci->menuCommand, TRUE );
1718                         }
1719                 }
1720                 canEditTr = canExport = FALSE;
1721         }
1722         else if( obj->type == ADDR_DATASOURCE ) {
1723                 AdapterDSource *ads = ADAPTER_DSOURCE(obj);
1724                 ds = ads->dataSource;
1725                 iface = ds->interface;
1726                 if( ! iface->readOnly ) {
1727                         canAdd = canEdit = editAddress = canDelete = TRUE;
1728                 }
1729                 if( ! iface->haveLibrary ) {
1730                         canAdd = canEdit = editAddress = canExport = canDelete = FALSE;
1731                 }
1732         }
1733         else if( obj->type == ADDR_ITEM_FOLDER ) {
1734                 ds = addressbook_find_datasource( addrbook.treeSelected );
1735                 if( ds ) {
1736                         iface = ds->interface;
1737                         if( iface->readOnly ) {
1738                                 canEditTr = FALSE;
1739                                 canDelete = FALSE;
1740                         }
1741                         else {
1742                                 canAdd = editAddress = TRUE;
1743                         }
1744                 }
1745         }
1746         else if( obj->type == ADDR_ITEM_GROUP ) {
1747                 ds = addressbook_find_datasource( addrbook.treeSelected );
1748                 if( ds ) {
1749                         iface = ds->interface;
1750                         if( ! iface->readOnly ) {
1751                                 editAddress = TRUE;
1752                         }
1753                 }
1754         }
1755
1756         if( addrbook.listSelected == NULL ) canEdit = FALSE;
1757
1758         /* Enable add */
1759         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", editAddress );
1760         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   canAdd );
1761         menu_set_sensitive( addrbook.menu_factory, "/Book/New Folder",  canAdd );
1762         gtk_widget_set_sensitive( addrbook.reg_btn, editAddress );
1763
1764         /* Enable edit */
1765         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
1766         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canDelete );
1767         gtk_widget_set_sensitive( addrbook.edit_btn, canEdit );
1768         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1769
1770         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",      canEditTr );
1771         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book",    canEditTr );
1772
1773         /* Export data */
1774         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export HTML...", canExport );
1775         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export LDIF...", canExport );
1776 }
1777
1778 static void addressbook_list_menu_setup( void );
1779
1780 /**
1781  * Address book tree callback function that responds to selection of tree
1782  * items.
1783  *
1784  * \param ctree  Tree widget.
1785  * \param node   Node that was selected.
1786  * \param column Column number where selected occurred.
1787  * \param data   Pointer to user data.
1788  */
1789 static void addressbook_tree_selected(GtkCTree *ctree, GtkCTreeNode *node,
1790                                       gint column, gpointer data)
1791 {
1792         AddressObject *obj = NULL;
1793         AdapterDSource *ads = NULL;
1794         AddressDataSource *ds = NULL;
1795         ItemFolder *rootFolder = NULL;
1796         AddressObjectType aot;
1797
1798         addrbook.treeSelected = node;
1799         addrbook.listSelected = NULL;
1800         addressbook_status_show( "" );
1801         if( addrbook.entry != NULL ) gtk_entry_set_text(GTK_ENTRY(addrbook.entry), "");
1802
1803         if( node ) obj = gtk_ctree_node_get_row_data( ctree, node );
1804         if( obj == NULL ) {
1805                 addressbook_set_clist(NULL, TRUE);
1806                 return;
1807         }
1808         addrbook.opened = node;
1809
1810         if( obj->type == ADDR_DATASOURCE ) {
1811                 /* Read from file */
1812                 static gboolean tVal = TRUE;
1813
1814                 ads = ADAPTER_DSOURCE(obj);
1815                 if( ads == NULL ) return;
1816                 ds = ads->dataSource;
1817                 if( ds == NULL ) return;                
1818
1819                 if( addrindex_ds_get_modify_flag( ds ) ) {
1820                         addrindex_ds_read_data( ds );
1821                 }
1822
1823                 if( ! addrindex_ds_get_read_flag( ds ) ) {
1824                         addrindex_ds_read_data( ds );
1825                 }
1826                 addressbook_ds_show_message( ds );
1827
1828                 if( ! addrindex_ds_get_access_flag( ds ) ) {
1829                         /* Remove existing folders and groups */
1830                         gtk_clist_freeze( GTK_CLIST(ctree) );
1831                         addressbook_tree_remove_children( ctree, node );
1832                         gtk_clist_thaw( GTK_CLIST(ctree) );
1833
1834                         /* Load folders into the tree */
1835                         rootFolder = addrindex_ds_get_root_folder( ds );
1836                         if( ds && ds->type == ADDR_IF_JPILOT ) {
1837                                 aot = ADDR_CATEGORY;
1838                         }
1839                         else if( ds && ds->type == ADDR_IF_LDAP ) {
1840                                 aot = ADDR_LDAP_QUERY;
1841                         }
1842                         else {
1843                                 aot = ADDR_ITEM_FOLDER;
1844                         }
1845                         addressbook_node_add_folder( node, ds, rootFolder, aot );
1846                         addrindex_ds_set_access_flag( ds, &tVal );
1847                         gtk_ctree_expand( ctree, node );
1848                 }
1849         } else {
1850                 addressbook_set_clist(NULL, TRUE);
1851         }
1852
1853         /* Update address list */
1854         g_signal_handlers_block_by_func
1855                 (G_OBJECT(ctree),
1856                  G_CALLBACK(addressbook_tree_selected), NULL);
1857         addressbook_set_clist( obj, FALSE );
1858         g_signal_handlers_unblock_by_func
1859                 (G_OBJECT(ctree),
1860                  G_CALLBACK(addressbook_tree_selected), NULL);
1861         if (!prefs_common.addressbook_use_editaddress_dialog)
1862                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
1863
1864         /* Setup main menu selections */
1865         addressbook_menubar_set_sensitive( FALSE );
1866         addressbook_list_menu_setup();
1867         addressbook_menuitem_set_sensitive( obj, node );
1868
1869         addressbook_list_select_clear();
1870         addressbook_list_menu_setup();
1871         return;
1872 }
1873
1874 /**
1875  * Setup address list popup menu items. Items are enabled or disabled as
1876  * required.
1877  */
1878 static void addressbook_list_menu_setup( void ) {
1879         GtkCTree *clist = NULL;
1880         AddressObject *pobj = NULL;
1881         AddressObject *obj = NULL;
1882         AdapterDSource *ads = NULL;
1883         AddressInterface *iface = NULL;
1884         AddressDataSource *ds = NULL;
1885         gboolean canEdit = FALSE;
1886         gboolean canDelete = FALSE;
1887         gboolean canCut = FALSE;
1888         gboolean canCopy = FALSE;
1889         gboolean canPaste = FALSE;
1890         gboolean canBrowse = FALSE;
1891
1892         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1893         if( pobj == NULL ) return;
1894
1895         clist = GTK_CTREE(addrbook.clist);
1896         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1897         if( obj == NULL ) canEdit = FALSE;
1898
1899         menu_set_insensitive_all( GTK_MENU_SHELL(addrbook.list_popup) );
1900         menu_set_sensitive( addrbook.list_factory, "/Select all", TRUE );
1901
1902         if( pobj->type == ADDR_DATASOURCE ) {
1903                 /* Parent object is a data source */
1904                 ads = ADAPTER_DSOURCE(pobj);
1905                 ds = ads->dataSource;
1906                 iface = ds->interface;
1907                 if( ! iface->readOnly ) {
1908                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1909                         if (iface->type != ADDR_IF_LDAP)
1910                                 menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1911                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1912                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1913                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1914                         if( obj ) canEdit = TRUE;
1915                 }
1916                 canDelete = canEdit;
1917         }
1918         else if( pobj->type != ADDR_INTERFACE ) {
1919                 /* Parent object is not an interface */
1920                 ds = addressbook_find_datasource( addrbook.treeSelected );
1921                 if (!ds)
1922                         return;
1923                 iface = ds->interface;
1924                 if( ! iface->readOnly ) {
1925                         /* Folder or group */
1926                         if( pobj->type == ADDR_ITEM_FOLDER || pobj->type == ADDR_ITEM_GROUP ) {
1927                                 menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1928                                 gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1929                                 if( obj ) canEdit = TRUE;
1930                         }
1931                         /* Folder */
1932                         if( pobj->type == ADDR_ITEM_FOLDER ) {
1933                                 if (iface->type != ADDR_IF_LDAP)
1934                                         menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1935                                 if( obj ) canEdit = TRUE;
1936                         }
1937                         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
1938                         if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
1939                         canDelete = canEdit;
1940                 }
1941                 if( iface && iface->type == ADDR_IF_LDAP ) {
1942                         if( obj ) canBrowse = TRUE;
1943                         canEdit = TRUE;
1944                         canDelete = TRUE;
1945                 }
1946         }
1947         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
1948
1949         /* Disable edit or browse if more than one row selected */
1950         if( GTK_CLIST(clist)->selection && GTK_CLIST(clist)->selection->next ) {
1951                 canEdit = FALSE;
1952                 canBrowse = FALSE;
1953         }
1954
1955         /* Now go finalize menu items */
1956         menu_set_sensitive( addrbook.list_factory, "/Edit",   canEdit );
1957         menu_set_sensitive( addrbook.list_factory, "/Delete", canDelete );
1958
1959         menu_set_sensitive( addrbook.list_factory, "/Cut",           canCut );
1960         menu_set_sensitive( addrbook.list_factory, "/Copy",          canCopy );
1961         menu_set_sensitive( addrbook.list_factory, "/Paste",         canPaste );
1962 /*      menu_set_sensitive( addrbook.list_factory, "/Paste Address", canPaste );*/
1963
1964         menu_set_sensitive( addrbook.list_factory, "/Mail To",       canCopy );
1965
1966         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",           canCut );
1967         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",          canCopy );
1968         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",         canPaste );
1969 /*      menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );*/
1970
1971         menu_set_sensitive( addrbook.tree_factory, "/Cut",             canCut );
1972         menu_set_sensitive( addrbook.tree_factory, "/Copy",            canCopy );
1973         menu_set_sensitive( addrbook.tree_factory, "/Paste",           canPaste );
1974
1975         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",    canEdit );
1976         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete",  canDelete );
1977         menu_set_sensitive( addrbook.menu_factory, "/Address/Mail To", canCopy );
1978
1979         gtk_widget_set_sensitive( addrbook.edit_btn, canEdit );
1980         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1981
1982 #ifdef USE_LDAP
1983         menu_set_sensitive( addrbook.list_factory, "/Browse Entry",    canBrowse );
1984 #endif
1985 }
1986
1987 static void addressbook_select_row_tree (GtkCTree       *ctree,
1988                                          GtkCTreeNode   *node,
1989                                          gint            column,
1990                                          gpointer        data)
1991 {
1992 }
1993
1994 /**
1995  * Add list of items into tree node below specified tree node.
1996  * \param treeNode  Tree node.
1997  * \param ds        Data source.
1998  * \param listItems List of items.
1999  */
2000 static void addressbook_treenode_add_list(
2001         GtkCTreeNode *treeNode, AddressDataSource *ds, GList *listItems )
2002 {
2003         GList *node;
2004
2005         node = listItems;
2006         while( node ) {
2007                 AddrItemObject *aio;
2008                 GtkCTreeNode *nn;
2009
2010                 aio = node->data;
2011                 if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_GROUP ) {
2012                         ItemGroup *group;
2013
2014                         group = ( ItemGroup * ) aio;
2015                         nn = addressbook_node_add_group( treeNode, ds, group );
2016                 }
2017                 else if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_FOLDER ) {
2018                         ItemFolder *folder;
2019
2020                         folder = ( ItemFolder * ) aio;
2021                         nn = addressbook_node_add_folder(
2022                                 treeNode, ds, folder, ADDR_ITEM_FOLDER );
2023                 }
2024                 node = g_list_next( node );
2025         }
2026 }
2027
2028 static void addressbook_select_all_cb( void ) {
2029         gtk_clist_select_all(GTK_CLIST(addrbook.clist));
2030 }
2031
2032 /**
2033  * Cut from address list widget.
2034  */
2035 static void addressbook_clip_cut_cb( void ) {
2036         _clipBoard_->cutFlag = TRUE;
2037         addrclip_clear( _clipBoard_ );
2038         addrclip_add( _clipBoard_, _addressSelect_ );
2039         /* addrclip_list_show( _clipBoard_, stdout ); */
2040 }
2041
2042 /**
2043  * Copy from address list widget.
2044  */
2045 static void addressbook_clip_copy_cb( void ) {
2046         _clipBoard_->cutFlag = FALSE;
2047         addrclip_clear( _clipBoard_ );
2048         addrclip_add( _clipBoard_, _addressSelect_ );
2049         /* addrclip_list_show( _clipBoard_, stdout ); */
2050 }
2051
2052 /**
2053  * Paste clipboard into address list widget.
2054  */
2055 static void addressbook_clip_paste_cb( void ) {
2056         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2057         AddressObject *pobj = NULL;
2058         AddressDataSource *ds = NULL;
2059         AddressBookFile *abf = NULL;
2060         ItemFolder *folder = NULL;
2061         GList *folderGroup = NULL;
2062
2063         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2064         if( ds == NULL ) return;
2065         if( addrindex_ds_get_readonly( ds ) ) {
2066                 addressbook_ds_status_message(
2067                         ds, _( "Cannot paste. Target address book is readonly." ) );
2068                 return;
2069         }
2070
2071         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2072         if( pobj ) {
2073                 if( pobj->type == ADDR_ITEM_FOLDER ) {
2074                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
2075                 }
2076                 else if( pobj->type == ADDR_ITEM_GROUP ) {
2077                         addressbook_ds_status_message(
2078                                 ds, _( "Cannot paste into an address group." ) );
2079                         return;
2080                 }
2081         }
2082
2083         /* Get an address book */
2084         abf = addressbook_get_book_file();
2085         if( abf == NULL ) return;
2086
2087         if( _clipBoard_->cutFlag ) {
2088                 /* Paste/Cut */
2089                 folderGroup = addrclip_paste_cut( _clipBoard_, abf, folder );
2090
2091                 /* Remove all groups and folders in clipboard from tree node */
2092                 addressbook_treenode_remove_item();
2093
2094                 /* Remove all "cut" items */
2095                 addrclip_delete_item( _clipBoard_ );
2096
2097                 /* Clear clipboard - cut items??? */
2098                 addrclip_clear( _clipBoard_ );
2099         }
2100         else {
2101                 /* Paste/Copy */
2102                 folderGroup = addrclip_paste_copy( _clipBoard_, abf, folder );
2103         }
2104
2105         /* addrclip_list_show( _clipBoard_, stdout ); */
2106         if( folderGroup ) {
2107                 /* Update tree by inserting node for each folder or group */
2108                 addressbook_treenode_add_list(
2109                         addrbook.treeSelected, ds, folderGroup );
2110                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2111                 g_list_free( folderGroup );
2112                 folderGroup = NULL;
2113         }
2114
2115         /* Display items pasted */
2116         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened );
2117         addressbook_set_clist(
2118                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
2119                         addrbook.opened),
2120                 TRUE);
2121         
2122
2123 }
2124
2125 /**
2126  * Add current treenode object to clipboard. Note that widget only allows
2127  * one entry from the tree list to be selected.
2128  */
2129 static void addressbook_treenode_to_clipboard( void ) {
2130         AddressObject *obj = NULL;
2131         AddressDataSource *ds = NULL;
2132         AddrSelectItem *item;
2133         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2134         GtkCTreeNode *node;
2135
2136         node = addrbook.treeSelected;
2137         if( node == NULL ) return;
2138         obj = gtk_ctree_node_get_row_data( ctree, node );
2139         if( obj == NULL ) return;
2140
2141         ds = addressbook_find_datasource( node );
2142         if( ds == NULL ) return;
2143
2144         item = NULL;
2145         if( obj->type == ADDR_ITEM_FOLDER ) {
2146                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2147                 ItemFolder *folder = adapter->itemFolder;
2148
2149                 item = addrselect_create_node( obj );
2150                 item->uid = g_strdup( ADDRITEM_ID(folder) );
2151         }
2152         else if( obj->type == ADDR_ITEM_GROUP ) {
2153                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2154                 ItemGroup *group = adapter->itemGroup;
2155
2156                 item = addrselect_create_node( obj );
2157                 item->uid = g_strdup( ADDRITEM_ID(group) );
2158         }
2159         else if( obj->type == ADDR_DATASOURCE ) {
2160                 /* Data source */
2161                 item = addrselect_create_node( obj );
2162                 item->uid = NULL;
2163         }
2164
2165         if( item ) {
2166                 /* Clear existing list and add item into list */
2167                 gchar *cacheID;
2168
2169                 addressbook_list_select_clear();
2170                 cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2171                 addrselect_list_add( _addressSelect_, item, cacheID );
2172                 g_free( cacheID );
2173         }
2174 }
2175
2176 /**
2177  * Cut from tree widget.
2178  */
2179 static void addressbook_treenode_cut_cb( void ) {
2180         _clipBoard_->cutFlag = TRUE;
2181         addressbook_treenode_to_clipboard();
2182         addrclip_clear( _clipBoard_ );
2183         addrclip_add( _clipBoard_, _addressSelect_ );
2184         /* addrclip_list_show( _clipBoard_, stdout ); */
2185 }
2186
2187 /**
2188  * Copy from tree widget.
2189  */
2190 static void addressbook_treenode_copy_cb( void ) {
2191         _clipBoard_->cutFlag = FALSE;
2192         addressbook_treenode_to_clipboard();
2193         addrclip_clear( _clipBoard_ );
2194         addrclip_add( _clipBoard_, _addressSelect_ );
2195         /* addrclip_list_show( _clipBoard_, stdout ); */
2196 }
2197
2198 /**
2199  * Paste clipboard into address tree widget.
2200  */
2201 static void addressbook_treenode_paste_cb( void ) {
2202         addressbook_clip_paste_cb();
2203 }
2204
2205 /**
2206  * Clear selected entries in clipboard.
2207  */
2208 static void addressbook_list_select_clear( void ) {
2209         addrselect_list_clear( _addressSelect_ );
2210 }
2211
2212 /**
2213  * Add specified address item to selected address list.
2214  * \param aio Address item object.
2215  * \param ds  Datasource.
2216  */
2217 static void addressbook_list_select_add( AddrItemObject *aio, AddressDataSource *ds ) {
2218         gchar *cacheID;
2219
2220         if( ds == NULL ) return;
2221         cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2222         addrselect_list_add_obj( _addressSelect_, aio, cacheID );
2223         g_free( cacheID );
2224 }
2225
2226 /**
2227  * Remove specified address item from selected address list.
2228  * \param aio Address item object.
2229  */
2230 static void addressbook_list_select_remove( AddrItemObject *aio ) {
2231         addrselect_list_remove( _addressSelect_, aio );
2232 }
2233
2234 /**
2235  * Invoke EMail compose window with addresses in selected address list.
2236  */
2237 static void addressbook_mail_to_cb( void ) {
2238         GList *listAddress;
2239
2240         if( ! addrselect_test_empty( _addressSelect_ ) ) {
2241                 listAddress = addrselect_build_list( _addressSelect_ );
2242                 compose_new_with_list( NULL, listAddress );
2243                 mgu_free_dlist( listAddress );
2244                 listAddress = NULL;
2245         }
2246 }
2247
2248 static void addressbook_list_row_selected( GtkCTree *clist,
2249                                            GtkCTreeNode *node,
2250                                            gint column,
2251                                            gpointer data )
2252 {
2253         GtkEntry *entry = GTK_ENTRY(addrbook.entry);
2254         AddrItemObject *aio = NULL;
2255         AddressObject *pobj = NULL;
2256         AdapterDSource *ads = NULL;
2257         AddressDataSource *ds = NULL;
2258
2259         gtk_entry_set_text( entry, "" );
2260         addrbook.listSelected = node;
2261
2262         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
2263         if( pobj == NULL ) return;
2264
2265         if( pobj->type == ADDR_DATASOURCE ) {
2266                 ads = ADAPTER_DSOURCE(pobj);
2267                 ds = ads->dataSource;
2268         }
2269         else if( pobj->type != ADDR_INTERFACE ) {
2270                 ds = addressbook_find_datasource( addrbook.treeSelected );
2271         }
2272
2273         aio = gtk_ctree_node_get_row_data( clist, node );
2274         if( aio ) {
2275                 /* g_print( "list select: %d : '%s'\n", aio->type, aio->name ); */
2276                 addressbook_list_select_add( aio, ds );
2277         }
2278
2279         addressbook_list_menu_setup();
2280
2281         if (!addrbook.target_compose && !prefs_common.addressbook_use_editaddress_dialog)
2282                 addressbook_edit_address(NULL, 0, NULL, FALSE);
2283 }
2284
2285 static void addressbook_list_row_unselected( GtkCTree *ctree,
2286                                              GtkCTreeNode *node,
2287                                              gint column,
2288                                              gpointer data )
2289 {
2290         AddrItemObject *aio;
2291
2292         aio = gtk_ctree_node_get_row_data( ctree, node );
2293         if( aio != NULL ) {
2294                 /* g_print( "list unselect: %d : '%s'\n", aio->type, aio->name ); */
2295                 addressbook_list_select_remove( aio );
2296         }
2297
2298         if (!prefs_common.addressbook_use_editaddress_dialog)
2299                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
2300 }
2301
2302 /* from gdkevents.c */
2303 #define DOUBLE_CLICK_TIME 250
2304
2305 static gboolean addressbook_list_button_pressed(GtkWidget *widget,
2306                                                 GdkEventButton *event,
2307                                                 gpointer data)
2308 {
2309         static guint32 lasttime = 0;
2310         if( ! event ) return FALSE;
2311
2312         addressbook_list_menu_setup();
2313
2314         if( event->button == 3 ) {
2315                 gtk_menu_popup( GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
2316                        event->button, event->time );
2317         } else if (event->button == 1) {
2318                 if (event->time - lasttime < DOUBLE_CLICK_TIME) {
2319                         if (prefs_common.add_address_by_click &&
2320                             addrbook.target_compose)
2321                                 addressbook_to_clicked(NULL, GINT_TO_POINTER(COMPOSE_TO));
2322                         else
2323                                 if (prefs_common.addressbook_use_editaddress_dialog)
2324                                 addressbook_edit_address_cb(NULL, 0, NULL);
2325
2326                         lasttime = 0;
2327                 } else
2328                         lasttime = event->time;
2329         }
2330
2331         return FALSE;
2332 }
2333
2334 static gboolean addressbook_list_button_released(GtkWidget *widget,
2335                                                  GdkEventButton *event,
2336                                                  gpointer data)
2337 {
2338         return FALSE;
2339 }
2340
2341 static gboolean addressbook_tree_button_pressed(GtkWidget *ctree,
2342                                                 GdkEventButton *event,
2343                                                 gpointer data)
2344 {
2345         GtkCList *clist = GTK_CLIST(ctree);
2346         gint row, column;
2347         AddressObject *obj = NULL;
2348         AdapterDSource *ads = NULL;
2349         AddressInterface *iface = NULL;
2350         AddressDataSource *ds = NULL;
2351         gboolean canEdit = FALSE;
2352         gboolean canDelete = FALSE;
2353         gboolean canCut = FALSE;
2354         gboolean canCopy = FALSE;
2355         gboolean canPaste = FALSE;
2356         gboolean canTreeCut = FALSE;
2357         gboolean canTreeCopy = FALSE;
2358         gboolean canTreePaste = FALSE;
2359         gboolean canLookup = FALSE;
2360         GtkCTreeNode *node = NULL;
2361
2362         if( ! event ) return FALSE;
2363         addressbook_menubar_set_sensitive( FALSE );
2364
2365         if( gtk_clist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
2366                 gtkut_clist_set_focus_row(clist, row);
2367                 obj = gtk_clist_get_row_data( clist, row );
2368         }
2369
2370         menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.tree_popup));
2371
2372         if( obj == NULL ) return FALSE;
2373         node = gtk_ctree_node_nth(GTK_CTREE(clist), row);
2374
2375         if( ! addrclip_is_empty( _clipBoard_ ) ) {
2376                 canTreePaste = TRUE;
2377         }
2378
2379         if (obj->type == ADDR_INTERFACE) {
2380                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
2381                 iface = adapter->interface;
2382                 canEdit = FALSE;
2383                 canDelete = FALSE;
2384                 canTreeCopy = FALSE;
2385                 if( iface->readOnly ) {
2386                         canTreePaste = FALSE;
2387                 }
2388                 else {
2389                         menu_set_sensitive( addrbook.tree_factory, "/New Book", TRUE );
2390                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2391                 }
2392                 if( iface->externalQuery ) canLookup = TRUE;
2393         }
2394         if (obj->type == ADDR_DATASOURCE) {
2395                 ads = ADAPTER_DSOURCE(obj);
2396                 ds = ads->dataSource;
2397                 if (!ds)
2398                         goto just_set_sens;
2399                 iface = ds->interface;
2400                 if (!iface)
2401                         goto just_set_sens;
2402                 canEdit = TRUE;
2403                 canDelete = TRUE;
2404                 if( iface->readOnly ) {
2405                         canTreePaste = FALSE;
2406                 }
2407                 else {
2408                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2409                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2410                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2411                 }
2412                 canTreeCopy = TRUE;
2413                 if( iface->externalQuery ) canLookup = TRUE;
2414         }
2415         else if (obj->type == ADDR_ITEM_FOLDER) {
2416                 ds = addressbook_find_datasource( node );
2417                 if (!ds) {
2418                         goto just_set_sens;
2419                 }
2420                 iface = ds->interface;
2421                 if (!iface)
2422                         goto just_set_sens;
2423                 if( iface->readOnly ) {
2424                         canTreePaste = FALSE;
2425                 }
2426                 else {
2427                         canEdit = TRUE;
2428                         canDelete = TRUE;
2429                         canTreeCut = TRUE;
2430                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2431                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2432                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2433                 }
2434                 canTreeCopy = TRUE;
2435
2436                 if( iface->externalQuery ) {
2437                         /* Enable deletion of LDAP folder */
2438                         canLookup = TRUE;
2439                         canDelete = TRUE;
2440                 }
2441         }
2442         else if (obj->type == ADDR_ITEM_GROUP) {
2443                 ds = addressbook_find_datasource( node );
2444                 if (!ds)
2445                         goto just_set_sens;
2446                 iface = ds->interface;
2447                 if (!iface)
2448                         goto just_set_sens;
2449                 if( ! iface->readOnly ) {
2450                         canEdit = TRUE;
2451                         canDelete = TRUE;
2452                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
2453                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2454                 }
2455         }
2456         else if (obj->type == ADDR_INTERFACE) {
2457                 canTreePaste = FALSE;
2458         }
2459
2460         if( canEdit ) {
2461                 if( ! addrselect_test_empty( _addressSelect_ ) ) canCut = TRUE;
2462         }
2463         if( ! addrselect_test_empty( _addressSelect_ ) ) canCopy = TRUE;
2464         if( ! addrclip_is_empty( _clipBoard_ ) ) canPaste = TRUE;
2465
2466 just_set_sens:
2467         /* Enable edit */
2468         menu_set_sensitive( addrbook.tree_factory, "/Edit",   canEdit );
2469         menu_set_sensitive( addrbook.tree_factory, "/Delete", canDelete );
2470         menu_set_sensitive( addrbook.tree_factory, "/Cut",    canTreeCut );
2471         menu_set_sensitive( addrbook.tree_factory, "/Copy",   canTreeCopy );
2472         menu_set_sensitive( addrbook.tree_factory, "/Paste",  canTreePaste );
2473
2474         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",          canEdit );
2475         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book",        canEdit );
2476         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",           canCut );
2477         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",          canCopy );
2478         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",         canPaste );
2479 /*      menu_set_sensitive( addrbook.menu_factory, "/Edit/Paste Address", canPaste );*/
2480
2481         addressbook_show_buttons(addrbook.target_compose == NULL, canLookup, addrbook.target_compose != NULL);
2482         if( event->button == 3 ) {
2483                 gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
2484                                event->button, event->time);
2485         }
2486
2487         return FALSE;
2488 }
2489
2490 static gboolean addressbook_tree_button_released(GtkWidget *ctree,
2491                                                  GdkEventButton *event,
2492                                                  gpointer data)
2493 {
2494         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree), addrbook.opened);
2495         return FALSE;
2496 }
2497
2498 static void addressbook_new_folder_cb(gpointer data, guint action,
2499                                       GtkWidget *widget)
2500 {
2501         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2502         AddressObject *obj = NULL;
2503         AddressDataSource *ds = NULL;
2504         AddressBookFile *abf = NULL;
2505         ItemFolder *parentFolder = NULL;
2506         ItemFolder *folder = NULL;
2507
2508         if( ! addrbook.treeSelected ) return;
2509         obj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2510         if( obj == NULL ) return;
2511         ds = addressbook_find_datasource( addrbook.treeSelected );
2512         if( ds == NULL ) return;
2513
2514         if( obj->type == ADDR_DATASOURCE ) {
2515                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2516         }
2517         else if( obj->type == ADDR_ITEM_FOLDER ) {
2518                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2519         }
2520         else {
2521                 return;
2522         }
2523
2524         abf = ds->rawDataSource;
2525         if( abf == NULL ) return;
2526         folder = addressbook_edit_folder( abf, parentFolder, NULL );
2527         if( folder ) {
2528                 GtkCTreeNode *nn;
2529                 nn = addressbook_node_add_folder(
2530                         addrbook.treeSelected, ds, folder, ADDR_ITEM_FOLDER );
2531                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2532                 if( addrbook.treeSelected == addrbook.opened )
2533                         addressbook_set_clist(obj, TRUE);
2534         }
2535
2536 }
2537
2538 static void addressbook_new_group_cb(gpointer data, guint action,
2539                                      GtkWidget *widget)
2540 {
2541         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2542         AddressObject *obj = NULL;
2543         AddressDataSource *ds = NULL;
2544         AddressBookFile *abf = NULL;
2545         ItemFolder *parentFolder = NULL;
2546         ItemGroup *group = NULL;
2547
2548         if( ! addrbook.treeSelected ) return;
2549         obj = gtk_ctree_node_get_row_data(ctree, addrbook.treeSelected);
2550         if( obj == NULL ) return;
2551         ds = addressbook_find_datasource( addrbook.treeSelected );
2552         if( ds == NULL ) return;
2553
2554         if( obj->type == ADDR_DATASOURCE ) {
2555                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2556         }
2557         else if( obj->type == ADDR_ITEM_FOLDER ) {
2558                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2559         }
2560         else {
2561                 return;
2562         }
2563
2564         abf = ds->rawDataSource;
2565         if( abf == NULL ) return;
2566         group = addressbook_edit_group( abf, parentFolder, NULL );
2567         if( group ) {
2568                 GtkCTreeNode *nn;
2569                 nn = addressbook_node_add_group( addrbook.treeSelected, ds, group );
2570                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2571                 if( addrbook.treeSelected == addrbook.opened )
2572                         addressbook_set_clist(obj, TRUE);
2573         }
2574
2575 }
2576
2577 static void addressbook_change_node_name(GtkCTreeNode *node, const gchar *name)
2578 {
2579         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2580         gchar *text[1];
2581         guint8 spacing;
2582         GdkPixmap *pix_cl, *pix_op;
2583         GdkBitmap *mask_cl, *mask_op;
2584         gboolean is_leaf, expanded;
2585
2586         gtk_ctree_get_node_info(ctree, node, text, &spacing,
2587                                 &pix_cl, &mask_cl, &pix_op, &mask_op,
2588                                 &is_leaf, &expanded);
2589         gtk_sctree_set_node_info(ctree, node, name, spacing,
2590                                 pix_cl, mask_cl, pix_op, mask_op,
2591                                 is_leaf, expanded);
2592 }
2593
2594 /**
2595  * Edit data source.
2596  * \param obj  Address object to edit.
2597  * \param node Node in tree.
2598  * \return New name of data source.
2599  */
2600 static gchar *addressbook_edit_datasource( AddressObject *obj, GtkCTreeNode *node ) {
2601         gchar *newName = NULL;
2602         AddressDataSource *ds = NULL;
2603         AddressInterface *iface = NULL;
2604         AdapterDSource *ads = NULL;
2605
2606         ds = addressbook_find_datasource( node );
2607         if( ds == NULL ) return NULL;
2608         iface = ds->interface;
2609         if( ! iface->haveLibrary ) return NULL;
2610
2611         /* Read data from data source */
2612         if( addrindex_ds_get_modify_flag( ds ) ) {
2613                 addrindex_ds_read_data( ds );
2614         }
2615
2616         if( ! addrindex_ds_get_read_flag( ds ) ) {
2617                 addrindex_ds_read_data( ds );
2618         }
2619
2620         /* Handle edit */
2621         ads = ADAPTER_DSOURCE(obj);
2622         if( ads->subType == ADDR_BOOK ) {
2623                 if( addressbook_edit_book( _addressIndex_, ads ) == NULL ) return NULL;
2624         }
2625         else if( ads->subType == ADDR_VCARD ) {
2626                 if( addressbook_edit_vcard( _addressIndex_, ads ) == NULL ) return NULL;
2627         }
2628 #ifdef USE_JPILOT
2629         else if( ads->subType == ADDR_JPILOT ) {
2630                 if( addressbook_edit_jpilot( _addressIndex_, ads ) == NULL ) return NULL;
2631         }
2632 #endif
2633 #ifdef USE_LDAP
2634         else if( ads->subType == ADDR_LDAP ) {
2635                 if( addressbook_edit_ldap( _addressIndex_, ads ) == NULL ) return NULL;
2636         }
2637 #endif
2638         else {
2639                 return NULL;
2640         }
2641         newName = obj->name;
2642         return newName;
2643 }
2644
2645 /*
2646 * Edit an object that is in the address tree area.
2647 */
2648 static void addressbook_treenode_edit_cb(gpointer data, guint action,
2649                                        GtkWidget *widget)
2650 {
2651         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2652         AddressObject *obj;
2653         AddressDataSource *ds = NULL;
2654         AddressBookFile *abf = NULL;
2655         GtkCTreeNode *node = NULL, *parentNode = NULL;
2656         gchar *name = NULL;
2657
2658         if( ! addrbook.treeSelected ) return;
2659         node = addrbook.treeSelected;
2660         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2661         obj = gtk_ctree_node_get_row_data( ctree, node );
2662         if( obj == NULL ) return;
2663         parentNode = GTK_CTREE_ROW(node)->parent;
2664
2665         ds = addressbook_find_datasource( node );
2666         if( ds == NULL ) return;
2667
2668         if( obj->type == ADDR_DATASOURCE ) {
2669                 name = addressbook_edit_datasource( obj, node );
2670                 if( name == NULL ) return;
2671         }
2672         else {
2673                 abf = ds->rawDataSource;
2674                 if( abf == NULL ) return;
2675                 if( obj->type == ADDR_ITEM_FOLDER ) {
2676                         AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2677                         ItemFolder *item = adapter->itemFolder;
2678                         ItemFolder *parentFolder = NULL;
2679                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2680                         if( addressbook_edit_folder( abf, parentFolder, item ) == NULL ) return;
2681                         name = ADDRITEM_NAME(item);
2682                 }
2683                 else if( obj->type == ADDR_ITEM_GROUP ) {
2684                         AdapterGroup *adapter = ADAPTER_GROUP(obj);
2685                         ItemGroup *item = adapter->itemGroup;
2686                         ItemFolder *parentFolder = NULL;
2687                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2688                         if( addressbook_edit_group( abf, parentFolder, item ) == NULL ) return;
2689                         name = ADDRITEM_NAME(item);
2690                 }
2691         }
2692         if( name && parentNode ) {
2693                 /* Update node in tree view */
2694                 addressbook_change_node_name( node, name );
2695                 gtk_sctree_sort_node(ctree, parentNode);
2696                 gtk_ctree_expand( ctree, node );
2697                 gtk_sctree_select( GTK_SCTREE( ctree), node );
2698         }
2699 }
2700
2701 typedef enum {
2702         ADDRTREE_DEL_NONE,
2703         ADDRTREE_DEL_DATA,
2704         ADDRTREE_DEL_FOLDER_ONLY,
2705         ADDRTREE_DEL_FOLDER_ADDR
2706 } TreeItemDelType ;
2707
2708 /**
2709  * Delete an item from the tree widget.
2710  * \param data   Data passed in.
2711  * \param action Action.
2712  * \param widget Widget issuing callback.
2713  */
2714 static void addressbook_treenode_delete_cb(
2715                 gpointer data, guint action, GtkWidget *widget )
2716 {
2717         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2718         GtkCTreeNode *node = NULL;
2719         AddressObject *obj;
2720         gchar *message;
2721         AlertValue aval;
2722         AddrBookBase *adbase;
2723         AddressCache *cache;
2724         AdapterDSource *ads = NULL;
2725         AddressInterface *iface = NULL;
2726         AddressDataSource *ds = NULL;
2727         gboolean remFlag = FALSE;
2728         TreeItemDelType delType;
2729
2730         if( ! addrbook.treeSelected ) return;
2731         node = addrbook.treeSelected;
2732         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2733
2734         obj = gtk_ctree_node_get_row_data( ctree, node );
2735         g_return_if_fail(obj != NULL);
2736
2737         if( obj->type == ADDR_DATASOURCE ) {
2738                 ads = ADAPTER_DSOURCE(obj);
2739                 if( ads == NULL ) return;
2740                 ds = ads->dataSource;
2741                 if( ds == NULL ) return;
2742         }
2743         else {
2744                 /* Must be folder or something else */
2745                 ds = addressbook_find_datasource( node );
2746                 if( ds == NULL ) return;
2747
2748                 /* Only allow deletion from non-readOnly */
2749                 iface = ds->interface;
2750                 if( iface->readOnly ) {
2751                         /* Allow deletion of query results */
2752                         if( ! iface->externalQuery ) return;
2753                 }
2754         }
2755
2756         /* Confirm deletion */
2757         delType = ADDRTREE_DEL_NONE;
2758         if( obj->type == ADDR_ITEM_FOLDER ) {
2759                 if( iface->externalQuery ) {
2760                         message = g_strdup_printf( _(
2761                                 "Do you want to delete the query " \
2762                                 "results and addresses in '%s' ?" ),
2763                                 obj->name );
2764                         aval = alertpanel( _("Delete"), message,
2765                                 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
2766                         g_free(message);
2767                         if( aval == G_ALERTALTERNATE ) {
2768                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2769                         }
2770                 }
2771                 else {
2772                         message = g_strdup_printf
2773                                 ( _( "Do you want to delete '%s' ?"
2774                                      "If you delete the folder only, the addresses it contains will be moved into the parent folder." ),
2775                                  obj->name );
2776                         aval = alertpanel( _("Delete folder"), message,
2777                                 GTK_STOCK_CANCEL, _("+Delete _folder only"), _("Delete folder and _addresses"));
2778                         g_free(message);
2779                         if( aval == G_ALERTALTERNATE ) {
2780                                 delType = ADDRTREE_DEL_FOLDER_ONLY;
2781                         }
2782                         else if( aval == G_ALERTOTHER ) {
2783                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2784                         }
2785                 }
2786         }
2787         else if( obj->type == ADDR_ITEM_GROUP ) {
2788                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2789                                             "The addresses it contains will not be lost."), obj->name);
2790                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
2791                                 "+" GTK_STOCK_DELETE, NULL);
2792                 g_free(message);
2793                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_FOLDER_ONLY;
2794         } else {
2795                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2796                                             "The addresses it contains will be lost."), obj->name);
2797                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
2798                                 "+" GTK_STOCK_DELETE, NULL);
2799                 g_free(message);
2800                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_DATA;
2801         }
2802         if( delType == ADDRTREE_DEL_NONE ) return;
2803
2804         /* Proceed with deletion */
2805         if( obj->type == ADDR_DATASOURCE ) {
2806                 /* Remove node from tree */
2807                 gtk_ctree_remove_node( ctree, node );
2808         
2809                 /* Remove data source. */
2810                 if( addrindex_index_remove_datasource( _addressIndex_, ds ) ) {
2811                         addrindex_free_datasource( ds );
2812                 }
2813                 return;
2814         }
2815
2816         /* Get reference to cache */
2817         adbase = ( AddrBookBase * ) ds->rawDataSource;
2818         if( adbase == NULL ) return;
2819         cache = adbase->addressCache;
2820
2821         /* Remove query results folder */
2822         if( iface->externalQuery ) {
2823                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2824                 ItemFolder *folder = adapter->itemFolder;
2825
2826                 adapter->itemFolder = NULL;
2827                 /*
2828                 g_print( "remove folder for ::%s::\n", obj->name );
2829                 g_print( "      folder name ::%s::\n", ADDRITEM_NAME(folder) );
2830                 g_print( "-------------- remove results\n" );
2831                 */
2832                 addrindex_remove_results( ds, folder );
2833                 /* g_print( "-------------- remove node\n" ); */
2834                 gtk_ctree_remove_node( ctree, node );
2835                 return;
2836         }
2837
2838         /* Code below is valid for regular address book deletion */
2839         if( obj->type == ADDR_ITEM_FOLDER ) {
2840                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2841                 ItemFolder *item = adapter->itemFolder;
2842
2843                 if( delType == ADDRTREE_DEL_FOLDER_ONLY ) {
2844                         /* Remove folder only */
2845                         item = addrcache_remove_folder( cache, item );
2846                         if( item ) {
2847                                 addritem_free_item_folder( item );
2848                                 addressbook_move_nodes_up( ctree, node );
2849                                 remFlag = TRUE;
2850                         }
2851                 }
2852                 else if( delType == ADDRTREE_DEL_FOLDER_ADDR ) {
2853                         /* Remove folder and addresses */
2854                         item = addrcache_remove_folder_delete( cache, item );
2855                         if( item ) {
2856                                 addritem_free_item_folder( item );
2857                                 remFlag = TRUE;
2858                         }
2859                 }
2860         }
2861         else if( obj->type == ADDR_ITEM_GROUP ) {
2862                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2863                 ItemGroup *item = adapter->itemGroup;
2864
2865                 item = addrcache_remove_group( cache, item );
2866                 if( item ) {
2867                         addritem_free_item_group( item );
2868                         remFlag = TRUE;
2869                 }
2870         }
2871
2872         if( remFlag ) {
2873                 /* Remove node. */
2874                 gtk_ctree_remove_node(ctree, node );
2875         }
2876 }
2877
2878 static void addressbook_new_address_from_book_post_cb( ItemPerson *person )
2879 {
2880         if( person && addrbook.treeSelected == addrbook.opened ) {
2881                 person->status = ADD_ENTRY;
2882                 gtk_clist_unselect_all( GTK_CLIST(addrbook.clist) );
2883                 addressbook_folder_refresh_one_person(
2884                         GTK_CTREE(addrbook.clist), person );
2885         }
2886         addressbook_address_list_set_focus();
2887 }
2888
2889 static void addressbook_new_address_from_folder_post_cb( ItemPerson *person )
2890 {
2891         if( person && addrbook.treeSelected == addrbook.opened) {
2892                 person->status = ADD_ENTRY;
2893                 gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
2894                 addressbook_set_clist(
2895                         gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
2896                                 addrbook.opened),
2897                         TRUE);
2898         }
2899         addressbook_address_list_set_focus();
2900 }
2901
2902 /**
2903  * Label (a format string) that is used to name each folder.
2904  */
2905 static gchar *_queryFolderLabel_ = N_( "Search '%s'" );
2906
2907 /**
2908  * Search ctree widget callback function.
2909  * \param  pA Pointer to node.
2910  * \param  pB Pointer to data item being sought.
2911  * \return Zero (0) if folder found.
2912  */
2913 static int addressbook_treenode_find_folder_cb( gconstpointer pA, gconstpointer pB ) {
2914         AddressObject *aoA;
2915
2916         aoA = ( AddressObject * ) pA;
2917         if( aoA->type == ADDR_ITEM_FOLDER ) {
2918                 ItemFolder *folder, *fld;
2919
2920                 fld = ADAPTER_FOLDER(aoA)->itemFolder;
2921                 folder = ( ItemFolder * ) pB;
2922                 if( fld == folder ) return 0;   /* Found folder */
2923         }
2924         return 1;
2925 }
2926
2927 static ItemFolder * addressbook_setup_subf(
2928                 AddressDataSource *ds, gchar *title,
2929                 GtkCTreeNode *pNode )
2930 {
2931         AddrBookBase *adbase;
2932         AddressCache *cache;
2933         ItemFolder *folder;
2934         GtkCTree *ctree;
2935         GtkCTreeNode *nNode;
2936         gchar *name;
2937         AddressObjectType aoType = ADDR_NONE;
2938         GList *children;
2939         /* Setup a query */
2940         if( *title == '\0' || strlen( title ) < 1 ) return NULL;
2941
2942         if( ds && ds->type == ADDR_IF_LDAP ) {
2943 #if USE_LDAP
2944                 aoType = ADDR_LDAP_QUERY;
2945 #endif
2946         }
2947         else {
2948                 return NULL;
2949         }
2950
2951         ctree = GTK_CTREE(addrbook.ctree);
2952         /* Get reference to address cache */    
2953         adbase = ( AddrBookBase * ) ds->rawDataSource;
2954         cache = adbase->addressCache;
2955         
2956         if ((children = addrcache_get_list_folder(cache)) != NULL) {
2957                 GList *cur = children;
2958                 for (; cur; cur = cur->next) {
2959                         ItemFolder *child = (ItemFolder *) cur->data;
2960                         if (!strcmp2(ADDRITEM_NAME(child), title)) {
2961                                 nNode = gtk_ctree_find_by_row_data_custom(
2962                                         ctree, NULL, child,
2963                                         addressbook_treenode_find_folder_cb );
2964                                 if( nNode ) {
2965                                         addrindex_remove_results( ds, child );
2966                                         while( child->listPerson ) {
2967                                                 ItemPerson *item = ( ItemPerson * ) child->listPerson->data;
2968                                                 item = addrcache_remove_person( cache, item );
2969                                                 if( item ) {
2970                                                         addritem_free_item_person( item );
2971                                                         item = NULL;
2972                                                 }
2973                                         }
2974                                         gtk_sctree_select( GTK_SCTREE(ctree), nNode );
2975                                         addrbook.treeSelected = nNode;
2976                                 }       
2977                                 return child;
2978                         }
2979                 }
2980         }
2981         
2982         /* Create a folder */
2983         folder = addrcache_add_new_folder( cache, NULL );
2984         name = g_strdup_printf( "%s", title );
2985         addritem_folder_set_name( folder, name );
2986         addritem_folder_set_remarks( folder, "" );
2987         g_free( name );
2988
2989         /* Now let's see the folder */
2990         nNode = addressbook_node_add_folder( pNode, ds, folder, aoType );
2991         gtk_ctree_expand( ctree, pNode );
2992         if( nNode ) {
2993                 gtk_sctree_select( GTK_SCTREE(ctree), nNode );
2994                 addrbook.treeSelected = nNode;
2995                 return folder;
2996         }
2997         return NULL;
2998 }
2999
3000 static void addressbook_new_address_cb( gpointer data, guint action, GtkWidget *widget ) {
3001         AddressObject *pobj = NULL;
3002         AddressDataSource *ds = NULL;
3003         AddressBookFile *abf = NULL;
3004         debug_print("adding address\n");
3005         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
3006         if( pobj == NULL ) {
3007                 debug_print("no row data\n");
3008                 return;
3009         }
3010         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3011         if( ds == NULL ) {
3012                 debug_print("no datasource\n");
3013                 return;
3014         }
3015
3016         abf = ds->rawDataSource;
3017         if( abf == NULL ) {
3018                 g_print("no addressbook file\n");
3019                 return;
3020         }
3021
3022         if( pobj->type == ADDR_DATASOURCE ) {
3023                 if (ADAPTER_DSOURCE(pobj)->subType == ADDR_BOOK ||
3024                     ADAPTER_DSOURCE(pobj)->subType == ADDR_LDAP) {
3025                         ItemPerson *person;
3026                         ItemFolder *folder = NULL;
3027 #ifdef USE_LDAP
3028                         if (abf && abf->type == ADDR_IF_LDAP) {
3029                                 GtkCTreeNode *parentNode;
3030                                 ds = addressbook_find_datasource( GTK_CTREE_NODE( addrbook.treeSelected ) );
3031                                 if( ds == NULL ) return;
3032
3033                                 /* We must have a datasource that is an external interface */
3034                                 if( ! ds->interface->haveLibrary ) return;
3035                                 if( ! ds->interface->externalQuery ) return;
3036
3037                                 if( pobj->type == ADDR_ITEM_FOLDER ) {
3038                                         parentNode = GTK_CTREE_ROW(GTK_CTREE_NODE( addrbook.treeSelected ) )->parent;
3039                                 }
3040                                 else {
3041                                         parentNode = GTK_CTREE_NODE( addrbook.treeSelected );
3042                                 }
3043                                 folder = addressbook_setup_subf( ds, _("New Contacts"), parentNode );
3044
3045                                 pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
3046                                 ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3047                                 abf = ds->rawDataSource;
3048                         }
3049 #endif
3050                         person = addressbook_edit_person( abf, folder, NULL, FALSE,
3051                                                                   addrbook.editaddress_vbox,
3052                                                                   addressbook_new_address_from_book_post_cb,
3053                                                                   TRUE );
3054 #ifdef USE_LDAP
3055                         if (abf && abf->type == ADDR_IF_LDAP) {
3056                                 LdapServer *server = ds->rawDataSource;
3057                                 ldapsvr_set_modified(server, TRUE);
3058                                 ldapsvr_update_book(server, NULL);
3059                                 if (server->retVal != LDAPRC_SUCCESS) {
3060                                         alertpanel( _("Add address(es)"),
3061                                                 addressbook_err2string(_lutErrorsLDAP_, server->retVal),
3062                                                 GTK_STOCK_CLOSE, NULL, NULL );
3063                                         server->retVal = LDAPRC_SUCCESS;
3064                                         return;
3065                                 }
3066                         }
3067 #endif
3068                         if (prefs_common.addressbook_use_editaddress_dialog)
3069                                 addressbook_new_address_from_book_post_cb( person );
3070                 }
3071         }
3072         else if( pobj->type == ADDR_ITEM_FOLDER ) {
3073                 /* New address */
3074                 ItemFolder *folder = ADAPTER_FOLDER(pobj)->itemFolder;
3075                 ItemPerson *person;
3076 #ifdef USE_LDAP
3077                 if (abf && abf->type == ADDR_IF_LDAP) {
3078                         GtkCTreeNode *parentNode;
3079                         ds = addressbook_find_datasource( GTK_CTREE_NODE( addrbook.treeSelected ) );
3080                         if( ds == NULL ) return;
3081
3082                         /* We must have a datasource that is an external interface */
3083                         if( ! ds->interface->haveLibrary ) return;
3084                         if( ! ds->interface->externalQuery ) return;
3085
3086                         if( pobj->type == ADDR_ITEM_FOLDER ) {
3087                                 parentNode = GTK_CTREE_ROW(GTK_CTREE_NODE( addrbook.treeSelected ) )->parent;
3088                         }
3089                         else {
3090                                 parentNode = GTK_CTREE_NODE( addrbook.treeSelected );
3091                         }
3092                         folder = addressbook_setup_subf( ds, _("New Contacts"), parentNode );
3093                         if (!folder)
3094                                 return;
3095                         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
3096                         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3097                         abf = ds->rawDataSource;
3098                 }
3099 #endif
3100                 person = addressbook_edit_person( abf, folder, NULL, FALSE,
3101                                                           addrbook.editaddress_vbox,
3102                                                           addressbook_new_address_from_folder_post_cb,
3103                                                           TRUE );
3104 #ifdef USE_LDAP
3105                 if (abf && abf->type == ADDR_IF_LDAP) {
3106                         LdapServer *server = ds->rawDataSource;
3107                         ldapsvr_set_modified(server, TRUE);
3108                         ldapsvr_update_book(server, NULL);
3109                         if (server->retVal != LDAPRC_SUCCESS) {
3110                                 alertpanel( _("Add address(es)"),
3111                                                 addressbook_err2string(_lutErrorsLDAP_, server->retVal),
3112                                         GTK_STOCK_CLOSE, NULL, NULL );
3113                                 return;
3114                         }
3115                 }
3116 #endif
3117                 if (prefs_common.addressbook_use_editaddress_dialog)
3118                         addressbook_new_address_from_folder_post_cb( person );
3119         }
3120         else if( pobj->type == ADDR_ITEM_GROUP ) {
3121                 /* New address in group */
3122                 ItemGroup *group = ADAPTER_GROUP(pobj)->itemGroup;
3123                 if( addressbook_edit_group( abf, NULL, group ) == NULL ) return;
3124                 if (addrbook.treeSelected == addrbook.opened) {
3125                         /* Change node name in tree. */
3126                         addressbook_change_node_name( addrbook.treeSelected, ADDRITEM_NAME(group) );
3127                         gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
3128                         addressbook_set_clist(
3129                                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
3130                                         addrbook.opened),
3131                                 TRUE);
3132                 }
3133         }
3134 }
3135
3136 /**
3137  * Search for specified child group node in address index tree.
3138  * \param parent Parent node.
3139  * \param group  Group to find.
3140  */
3141 static GtkCTreeNode *addressbook_find_group_node( GtkCTreeNode *parent, ItemGroup *group ) {
3142         GtkCTreeNode *node = NULL;
3143         GtkCTreeRow *currRow;
3144
3145         currRow = GTK_CTREE_ROW( parent );
3146         if( currRow ) {
3147                 node = currRow->children;
3148                 while( node ) {
3149                         AddressObject *obj;
3150
3151                         obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
3152                         if( obj->type == ADDR_ITEM_GROUP ) {
3153                                 ItemGroup *g = ADAPTER_GROUP(obj)->itemGroup;
3154                                 if( g == group ) return node;
3155                         }
3156                         currRow = GTK_CTREE_ROW(node);
3157                         node = currRow->sibling;
3158                 }
3159         }
3160         return NULL;
3161 }
3162
3163 static AddressBookFile *addressbook_get_book_file() {
3164         AddressBookFile *abf = NULL;
3165         AddressDataSource *ds = NULL;
3166
3167         ds = addressbook_find_datasource( addrbook.treeSelected );
3168         if( ds == NULL ) return NULL;
3169         if( ds->type == ADDR_IF_BOOK || ds->type == ADDR_IF_LDAP ) abf = ds->rawDataSource;
3170         return abf;
3171 }
3172
3173 static void addressbook_tree_remove_children( GtkCTree *ctree, GtkCTreeNode *parent ) {
3174         GtkCTreeNode *node;
3175         GtkCTreeRow *row;
3176
3177         /* Remove existing folders and groups */
3178         row = GTK_CTREE_ROW( parent );
3179         if( row ) {
3180                 while( (node = row->children) ) {
3181                         gtk_ctree_remove_node( ctree, node );
3182                 }
3183         }
3184 }
3185
3186 static void addressbook_move_nodes_up( GtkCTree *ctree, GtkCTreeNode *node ) {
3187         GtkCTreeNode *parent, *child;
3188         GtkCTreeRow *currRow;
3189         currRow = GTK_CTREE_ROW( node );
3190         if( currRow ) {
3191                 parent = currRow->parent;
3192                 while( (child = currRow->children) ) {
3193                         gtk_ctree_move( ctree, child, parent, node );
3194                 }
3195                 gtk_sctree_sort_node( ctree, parent );
3196         }
3197 }
3198
3199 static void addressbook_edit_address_post_cb( ItemPerson *person )
3200 {
3201         if( person ) {
3202
3203                 addressbook_folder_refresh_one_person( GTK_CTREE(addrbook.clist), person );
3204                 invalidate_address_completion();
3205         }
3206         addressbook_address_list_set_focus();
3207 }
3208
3209 void addressbook_address_list_set_focus( void )
3210 {
3211         if (!prefs_common.addressbook_use_editaddress_dialog) {
3212                 gtk_window_set_focus(GTK_WINDOW(addrbook.window), addrbook.clist);
3213                 addressbook_list_menu_setup();
3214         }
3215 }
3216
3217 void addressbook_address_list_disable_some_actions(void)
3218 {
3219         /* disable address copy/pasting when editing contact's detail (embedded form) */
3220         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",   FALSE );
3221         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",  FALSE );
3222         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste", FALSE );
3223
3224         /* we're already editing contact's detail here */
3225         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",  FALSE );
3226         gtk_widget_set_sensitive( addrbook.edit_btn, FALSE );
3227 }
3228
3229 static void addressbook_edit_address_cb( gpointer data, guint action, GtkWidget *widget ) {
3230         addressbook_edit_address(data, action, widget, TRUE);
3231 }
3232         
3233 static void addressbook_edit_address( gpointer data, guint action, GtkWidget *widget,
3234                                                                           gboolean force_focus ) {
3235         GtkCTree *clist = GTK_CTREE(addrbook.clist);
3236         GtkCTree *ctree;
3237         AddressObject *obj = NULL, *pobj = NULL;
3238         AddressDataSource *ds = NULL;
3239         GtkCTreeNode *node = NULL, *parentNode = NULL;
3240         gchar *name = NULL;
3241         AddressBookFile *abf = NULL;
3242
3243         if( addrbook.listSelected == NULL ) return;
3244         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
3245         g_return_if_fail(obj != NULL);
3246
3247         ctree = GTK_CTREE( addrbook.ctree );
3248         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
3249         node = gtk_ctree_find_by_row_data( ctree, addrbook.treeSelected, obj );
3250
3251         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3252         if( ds == NULL ) return;
3253
3254         abf = addressbook_get_book_file();
3255         
3256         if( obj->type == ADDR_ITEM_EMAIL ) {
3257                 ItemEMail *email = ( ItemEMail * ) obj;
3258                 if( email == NULL ) return;
3259                 if( pobj && pobj->type == ADDR_ITEM_GROUP ) {
3260                         /* Edit parent group */
3261                         AdapterGroup *adapter = ADAPTER_GROUP(pobj);
3262                         ItemGroup *itemGrp = adapter->itemGroup;
3263                         if( abf == NULL ) return;
3264                         if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
3265                         name = ADDRITEM_NAME(itemGrp);
3266                         node = addrbook.treeSelected;
3267                         parentNode = GTK_CTREE_ROW(node)->parent;
3268                 }
3269                 else {
3270                         /* Edit person - email page */
3271                         ItemPerson *person;
3272                         person = ( ItemPerson * ) ADDRITEM_PARENT(email);
3273                         if  ( addressbook_edit_person( abf, NULL, person, TRUE, addrbook.editaddress_vbox,
3274                                                                                    addressbook_edit_address_post_cb,
3275                                                                                    (prefs_common.addressbook_use_editaddress_dialog||force_focus) )
3276                                   != NULL ) { 
3277 #ifdef USE_LDAP
3278                                 if (abf && abf->type == ADDR_IF_LDAP) {
3279                                         ldapsvr_set_modified( (LdapServer *) abf, TRUE );
3280                                         person->status = UPDATE_ENTRY;
3281                                 }
3282 #endif
3283                                 if (prefs_common.addressbook_use_editaddress_dialog)
3284                                         addressbook_edit_address_post_cb( person );
3285                         }
3286                         return;
3287                 }
3288         }
3289         else if( obj->type == ADDR_ITEM_PERSON ) {
3290                 /* Edit person - basic page */
3291                 ItemPerson *person = ( ItemPerson * ) obj;
3292                 if( addressbook_edit_person( abf, NULL, person, FALSE, addrbook.editaddress_vbox,
3293                                                                           addressbook_edit_address_post_cb,
3294                                                                           (prefs_common.addressbook_use_editaddress_dialog||force_focus) )
3295                         != NULL ) {
3296 #ifdef USE_LDAP
3297                                 if (abf && abf->type == ADDR_IF_LDAP) {
3298                                         ldapsvr_set_modified( (LdapServer *) abf, TRUE );
3299                                         person->status = UPDATE_ENTRY;
3300                                 }
3301 #endif
3302                                 if (prefs_common.addressbook_use_editaddress_dialog)
3303                                         addressbook_edit_address_post_cb( person );
3304                 }
3305                 return;
3306         }
3307         else if( obj->type == ADDR_ITEM_GROUP ) {
3308                 ItemGroup *itemGrp = ( ItemGroup * ) obj;
3309                 if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
3310                 parentNode = addrbook.treeSelected;
3311                 node = addressbook_find_group_node( parentNode, itemGrp );
3312                 name = ADDRITEM_NAME(itemGrp);
3313                 invalidate_address_completion();
3314         }
3315         else {
3316                 return;
3317         }
3318
3319         /* Update tree node with node name */
3320         if( node == NULL ) return;
3321         addressbook_change_node_name( node, name );
3322         gtk_sctree_sort_node( ctree, parentNode );
3323         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened ); 
3324         addressbook_set_clist(
3325                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
3326                         addrbook.opened),
3327                 TRUE);
3328 }
3329
3330 static void addressbook_delete_address_cb(gpointer data, guint action,
3331                                           GtkWidget *widget)
3332 {
3333         gtk_clist_freeze(GTK_CLIST(addrbook.clist));
3334         addressbook_del_clicked(NULL, NULL);
3335         gtk_clist_thaw(GTK_CLIST(addrbook.clist));
3336 }
3337
3338 static void close_cb(gpointer data, guint action, GtkWidget *widget)
3339 {
3340         addressbook_close();
3341 }
3342
3343 static void addressbook_file_save_cb( gpointer data, guint action, GtkWidget *widget ) {
3344         addressbook_export_to_file();
3345 }
3346
3347 static void addressbook_person_expand_node( GtkCTree *ctree, GList *node, gpointer *data ) {
3348         if( node ) {
3349                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
3350                 if( person ) addritem_person_set_opened( person, TRUE );
3351         }
3352 }
3353
3354 static void addressbook_person_collapse_node( GtkCTree *ctree, GList *node, gpointer *data ) {
3355         if( node ) {
3356                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
3357                 if( person ) addritem_person_set_opened( person, FALSE );
3358         }
3359 }
3360
3361 static gchar *addressbook_format_item_clist( ItemPerson *person, ItemEMail *email ) {
3362         gchar *str = NULL;
3363         gchar *eMailAlias = ADDRITEM_NAME(email);
3364         if( eMailAlias && *eMailAlias != '\0' ) {
3365                 if( person ) {
3366                         str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), eMailAlias );
3367                 }
3368                 else {
3369                         str = g_strdup( eMailAlias );
3370                 }
3371         }
3372         return str;
3373 }
3374
3375 static void addressbook_load_group( GtkCTree *clist, ItemGroup *itemGroup ) {
3376         GList *items = itemGroup->listEMail;
3377         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
3378         for( ; items != NULL; items = g_list_next( items ) ) {
3379                 GtkCTreeNode *nodeEMail = NULL;
3380                 gchar *text[N_LIST_COLS];
3381                 ItemEMail *email = items->data;
3382                 ItemPerson *person;
3383                 gchar *str = NULL;
3384
3385                 if( ! email ) continue;
3386
3387                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
3388                 str = addressbook_format_item_clist( person, email );
3389                 if( str ) {
3390                         text[COL_NAME] = str;
3391                 }
3392                 else {
3393                         text[COL_NAME] = ADDRITEM_NAME(person);
3394                 }
3395                 text[COL_ADDRESS] = email->address;
3396                 text[COL_REMARKS] = email->remarks;
3397                 nodeEMail = gtk_sctree_insert_node(
3398                                 clist, NULL, NULL,
3399                                 text, FOLDER_SPACING,
3400                                 atci->iconXpm, atci->maskXpm,
3401                                 atci->iconXpmOpen, atci->maskXpmOpen,
3402                                 FALSE, FALSE );
3403                 gtk_ctree_node_set_row_data( clist, nodeEMail, email );
3404                 g_free( str );
3405                 str = NULL;
3406         }
3407 }
3408
3409 static void addressbook_folder_load_one_person(
3410                 GtkCTree *clist, ItemPerson *person,
3411                 AddressTypeControlItem *atci,
3412                 AddressTypeControlItem *atciMail )
3413 {
3414         GtkCTreeNode *nodePerson = NULL;
3415         GtkCTreeNode *nodeEMail = NULL;
3416         gchar *text[N_LIST_COLS];
3417         gboolean flgFirst = TRUE, haveAddr = FALSE;
3418         GList *node;
3419 #ifdef USE_LDAP
3420         AddressBookFile *abf = addressbook_get_book_file();
3421 #endif
3422
3423         if( person == NULL ) return;
3424
3425         text[COL_NAME] = "";
3426         node = person->listEMail;
3427         while( node ) {
3428                 ItemEMail *email = node->data;
3429                 gchar *eMailAddr = NULL;
3430                 node = g_list_next( node );
3431
3432                 text[COL_ADDRESS] = email->address;
3433                 text[COL_REMARKS] = email->remarks;
3434                 eMailAddr = ADDRITEM_NAME(email);
3435                 if( eMailAddr && *eMailAddr == '\0' ) eMailAddr = NULL;
3436                 if( flgFirst ) {
3437                         /* First email belongs with person */
3438                         gchar *str = addressbook_format_item_clist( person, email );
3439                         if( str ) {
3440                                 text[COL_NAME] = str;
3441                         }
3442 #ifdef USE_LDAP
3443                         else if( abf && abf->type == ADDR_IF_LDAP && 
3444                                  person && person->nickName ) {
3445                                 if (person->nickName) {
3446                                         if (strcmp(person->nickName, "") != 0) {
3447                                                 text[COL_NAME] = person->nickName;
3448                                         }
3449                                         else {
3450                                                 text[COL_NAME] = ADDRITEM_NAME(person);
3451                                         }
3452                                 }
3453                         }
3454 #endif
3455                         else {
3456                                 text[COL_NAME] = ADDRITEM_NAME(person);
3457                         }
3458                         nodePerson = gtk_sctree_insert_node(
3459                                         clist, NULL, NULL,
3460                                         text, FOLDER_SPACING,
3461                                         atci->iconXpm, atci->maskXpm,
3462                                         atci->iconXpmOpen, atci->maskXpmOpen,
3463                                         FALSE, person->isOpened );
3464                         g_free( str );
3465                         str = NULL;
3466                         gtk_ctree_node_set_row_data(clist, nodePerson, person );
3467                 }
3468                 else {
3469                         /* Subsequent email is a child node of person */
3470                         text[COL_NAME] = ADDRITEM_NAME(email);
3471                         nodeEMail = gtk_sctree_insert_node(
3472                                         clist, nodePerson, NULL,
3473                                         text, FOLDER_SPACING,
3474                                         atciMail->iconXpm, atciMail->maskXpm,
3475                                         atciMail->iconXpmOpen, atciMail->maskXpmOpen,
3476                                         FALSE, TRUE );
3477                         gtk_ctree_node_set_row_data(clist, nodeEMail, email );
3478                 }
3479                 flgFirst = FALSE;
3480                 haveAddr = TRUE;
3481         }
3482         if( ! haveAddr ) {
3483                 /* Have name without EMail */
3484                 text[COL_NAME] = ADDRITEM_NAME(person);
3485                 text[COL_ADDRESS] = "";
3486                 text[COL_REMARKS] = "";
3487                 nodePerson = gtk_sctree_insert_node(
3488                                 clist, NULL, NULL,
3489                                 text, FOLDER_SPACING,
3490                                 atci->iconXpm, atci->maskXpm,
3491                                 atci->iconXpmOpen, atci->maskXpmOpen,
3492                                 FALSE, person->isOpened );
3493                 gtk_ctree_node_set_row_data(clist, nodePerson, person );
3494         }
3495         return;
3496 }
3497
3498 static void addressbook_folder_load_person( GtkCTree *clist, ItemFolder *itemFolder ) {
3499         GList *items;
3500         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
3501         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
3502
3503         if( atci == NULL ) return;
3504         if( atciMail == NULL ) return;
3505
3506         /* Load email addresses */
3507         items = addritem_folder_get_person_list( itemFolder );
3508         for( ; items != NULL; items = g_list_next( items ) ) {
3509                 addressbook_folder_load_one_person( clist, items->data, atci, atciMail );
3510         }
3511         /* Free up the list */
3512         mgu_clear_list( items );
3513         g_list_free( items );
3514 }
3515
3516 static void addressbook_folder_remove_node( GtkCTree *clist, GtkCTreeNode *node ) { 
3517         addrbook.listSelected = NULL;
3518         gtk_ctree_remove_node( clist, node );
3519         addressbook_menubar_set_sensitive( FALSE );
3520         addressbook_menuitem_set_sensitive(
3521                 gtk_ctree_node_get_row_data(
3522                         GTK_CTREE(clist), addrbook.treeSelected ),
3523                 addrbook.treeSelected );
3524 }
3525
3526 static void addressbook_folder_refresh_one_person( GtkCTree *clist, ItemPerson *person ) {
3527         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
3528         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
3529         GtkCTreeNode *node;
3530         if( atci == NULL ) return;
3531         if( atciMail == NULL ) return;
3532         if( person == NULL ) return;
3533         /* unload the person */
3534         
3535         node = gtk_ctree_find_by_row_data( clist, NULL, person );
3536         if( node )
3537                 addressbook_folder_remove_node( clist, node );
3538         addressbook_folder_load_one_person( clist, person, atci, atciMail );
3539         gtk_sctree_sort_node( clist, NULL );
3540         node = gtk_ctree_find_by_row_data( clist, NULL, person );
3541         if( node ) {
3542                 gtk_sctree_select( GTK_SCTREE(clist), node );
3543                 if (!gtk_ctree_node_is_visible( clist, node ) ) 
3544                         gtk_ctree_node_moveto( clist, node, 0, 0, 0 );
3545         }
3546 }
3547
3548 static void addressbook_folder_remove_one_person( GtkCTree *clist, ItemPerson *person ) {
3549         GtkCTreeNode *node;
3550         gint row;
3551         
3552         if( person == NULL ) return;
3553         node = gtk_ctree_find_by_row_data( clist, NULL, person );
3554         row  = gtk_clist_find_row_from_data( GTK_CLIST(clist), person );
3555         if( node ) {
3556                 addressbook_folder_remove_node( clist, node );
3557         }
3558 }
3559
3560 static void addressbook_folder_load_group( GtkCTree *clist, ItemFolder *itemFolder ) {
3561         GList *items;
3562         AddressTypeControlItem *atci =  addrbookctl_lookup( ADDR_ITEM_GROUP );
3563
3564         /* Load any groups */
3565         if( ! atci ) return;
3566         items = addritem_folder_get_group_list( itemFolder );
3567         for( ; items != NULL; items = g_list_next( items ) ) {
3568                 GtkCTreeNode *nodeGroup = NULL;
3569                 gchar *text[N_LIST_COLS];
3570                 ItemGroup *group = items->data;
3571                 if( group == NULL ) continue;
3572                 text[COL_NAME] = ADDRITEM_NAME(group);
3573                 text[COL_ADDRESS] = "";
3574                 text[COL_REMARKS] = "";
3575                 nodeGroup = gtk_sctree_insert_node(clist, NULL, NULL,
3576                                       text, FOLDER_SPACING,
3577                                       atci->iconXpm, atci->maskXpm,
3578                                       atci->iconXpmOpen, atci->maskXpmOpen,
3579                                       FALSE, FALSE);
3580                 gtk_ctree_node_set_row_data(clist, nodeGroup, group );
3581                 gtk_sctree_sort_node(clist, NULL);
3582         }
3583         /* Free up the list */
3584         mgu_clear_list( items );
3585         g_list_free( items );
3586 }
3587
3588 /**
3589  * Search ctree widget callback function.
3590  * \param  pA Pointer to node.
3591  * \param  pB Pointer to data item being sought.
3592  * \return Zero (0) if group found.
3593  */
3594 static int addressbook_treenode_find_group_cb( gconstpointer pA, gconstpointer pB ) {
3595         AddressObject *aoA;
3596
3597         aoA = ( AddressObject * ) pA;
3598         if( aoA->type == ADDR_ITEM_GROUP ) {
3599                 ItemGroup *group, *grp;
3600
3601                 grp = ADAPTER_GROUP(aoA)->itemGroup;
3602                 group = ( ItemGroup * ) pB;
3603                 if( grp == group ) return 0;    /* Found group */
3604         }
3605         return 1;
3606 }
3607
3608 /*
3609 * Remove folder and group nodes from tree widget for items contained ("cut")
3610 * in clipboard.
3611 */
3612 static void addressbook_treenode_remove_item( void ) {
3613         GList *node;
3614         AddrSelectItem *cutItem;
3615         AddressCache *cache;
3616         AddrItemObject *aio;
3617         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
3618         GtkCTreeNode *tn;
3619
3620         node = _clipBoard_->objectList;
3621         while( node ) {
3622                 cutItem = node->data;
3623                 node = g_list_next( node );
3624                 cache = addrindex_get_cache(
3625                         _clipBoard_->addressIndex, cutItem->cacheID );
3626                 if( cache == NULL ) continue;
3627                 aio = addrcache_get_object( cache, cutItem->uid );
3628                 if( aio ) {
3629                         tn = NULL;
3630                         if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
3631                                 ItemFolder *folder;
3632
3633                                 folder = ( ItemFolder * ) aio;
3634                                 tn = gtk_ctree_find_by_row_data_custom(
3635                                         ctree, NULL, folder,
3636                                         addressbook_treenode_find_folder_cb );
3637                         }
3638                         else if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
3639                                 ItemGroup *group;
3640
3641                                 group = ( ItemGroup * ) aio;
3642                                 tn = gtk_ctree_find_by_row_data_custom(
3643                                         ctree, NULL, group,
3644                                         addressbook_treenode_find_group_cb );
3645                         }
3646
3647                         if( tn ) {
3648                                 /* Free up adapter and remove node. */
3649                                 gtk_ctree_remove_node( ctree, tn );
3650                         }
3651                 }
3652         }
3653 }
3654
3655 /**
3656  * Find parent datasource for specified tree node.
3657  * \param  node Node to test.
3658  * \return Data source, or NULL if not found.
3659  */
3660 static AddressDataSource *addressbook_find_datasource( GtkCTreeNode *node ) {
3661         AddressDataSource *ds = NULL;
3662         AddressObject *ao;
3663
3664         g_return_val_if_fail(addrbook.ctree != NULL, NULL);
3665
3666         while( node ) {
3667                 if( GTK_CTREE_ROW(node)->level < 2 ) return NULL;
3668                 ao = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
3669                 if( ao ) {
3670                         /* g_print( "ao->type = %d\n", ao->type ); */
3671                         if( ao->type == ADDR_DATASOURCE ) {
3672                                 AdapterDSource *ads = ADAPTER_DSOURCE(ao);
3673                                 /* g_print( "found it\n" ); */
3674                                 ds = ads->dataSource;
3675                                 break;
3676                         }
3677                 }
3678                 node = GTK_CTREE_ROW(node)->parent;
3679         }
3680         return ds;
3681 }
3682
3683 /**
3684  * Load address list widget with children of specified object.
3685  * \param obj Parent object to be loaded.
3686  */
3687 static void addressbook_set_clist( AddressObject *obj, gboolean refresh ) {
3688         GtkCTree *ctreelist = GTK_CTREE(addrbook.clist);
3689         GtkCList *clist = GTK_CLIST(addrbook.clist);
3690         AddressDataSource *ds = NULL;
3691         AdapterDSource *ads = NULL;
3692         static AddressObject *last_obj = NULL;
3693
3694         if (addrbook.clist == NULL) {
3695                 return;
3696         }
3697         if (obj == last_obj && !refresh)
3698                 return;
3699
3700         last_obj = obj;
3701         if( obj == NULL ) {
3702                 gtk_clist_clear(clist);
3703                 return;
3704         }
3705
3706         if( obj->type == ADDR_INTERFACE ) {
3707                 /* g_print( "set_clist: loading datasource...\n" ); */
3708                 /* addressbook_node_load_datasource( GTK_CTREE(clist), obj ); */
3709                 return;
3710         }
3711
3712         gtk_clist_freeze(clist);
3713         gtk_clist_clear(clist);
3714
3715         if( obj->type == ADDR_DATASOURCE ) {
3716                 ads = ADAPTER_DSOURCE(obj);
3717                 ds = ADAPTER_DSOURCE(obj)->dataSource;
3718                 if( ds ) {
3719                         /* Load root folder */
3720                         ItemFolder *rootFolder = NULL;
3721                         rootFolder = addrindex_ds_get_root_folder( ds );
3722                         addressbook_folder_load_person(
3723                                 ctreelist, addrindex_ds_get_root_folder( ds ) );
3724                         addressbook_folder_load_group(
3725                                 ctreelist, addrindex_ds_get_root_folder( ds ) );
3726                 }
3727         }
3728         else {
3729                 if( obj->type == ADDR_ITEM_GROUP ) {
3730                         /* Load groups */
3731                         ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
3732                         addressbook_load_group( ctreelist, itemGroup );
3733                 }
3734                 else if( obj->type == ADDR_ITEM_FOLDER ) {
3735                         /* Load folders */
3736                         ItemFolder *itemFolder = ADAPTER_FOLDER(obj)->itemFolder;
3737                         addressbook_folder_load_person( ctreelist, itemFolder );
3738                         addressbook_folder_load_group( ctreelist, itemFolder );
3739                 }
3740         }
3741         gtk_sctree_sort_recursive(GTK_CTREE(clist), NULL);
3742         clist->focus_row = -1;
3743         gtk_clist_thaw(clist);
3744 }
3745
3746 /**
3747  * Call back function to free adaptor. Call back is setup by function
3748  * gtk_ctree_node_set_row_data_full() when node is populated. This function is
3749  * called when the address book tree widget node is removed by calling
3750  * function gtk_ctree_remove_node().
3751  * 
3752  * \param data Tree node's row data.
3753  */
3754 static void addressbook_free_treenode( gpointer data ) {
3755         AddressObject *ao;
3756
3757         ao = ( AddressObject * ) data;
3758         if( ao == NULL ) return;
3759         if( ao->type == ADDR_INTERFACE ) {
3760                 AdapterInterface *ai = ADAPTER_INTERFACE(ao);
3761                 addrbookctl_free_interface( ai );
3762         }
3763         else if( ao->type == ADDR_DATASOURCE ) {
3764