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