11873e29a3899ccb8abf62aafccecff1e37fb66f
[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
1876         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1877         if( pobj == NULL ) return;
1878
1879         clist = GTK_CTREE(addrbook.clist);
1880         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1881         if( obj == NULL ) canEdit = FALSE;
1882
1883         menu_set_insensitive_all( GTK_MENU_SHELL(addrbook.list_popup) );
1884         menu_set_sensitive( addrbook.list_factory, "/Select all", TRUE );
1885
1886         if( pobj->type == ADDR_DATASOURCE ) {
1887                 /* Parent object is a data source */
1888                 ads = ADAPTER_DSOURCE(pobj);
1889                 ds = ads->dataSource;
1890                 if (!ds)
1891                         return;
1892                 iface = ds->interface;
1893                 if (!iface)
1894                         return;
1895                 if( ! iface->readOnly ) {
1896                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1897                         if (iface->type != ADDR_IF_LDAP)
1898                                 menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1899                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1900                         if( obj )
1901                                 canEdit = TRUE;
1902                         canDelete = canEdit;
1903                 }
1904         }
1905         else if( pobj->type != ADDR_INTERFACE ) {
1906                 /* Parent object is not an interface */
1907                 ds = addressbook_find_datasource( addrbook.treeSelected );
1908                 if (!ds)
1909                         return;
1910                 iface = ds->interface;
1911                 if (!iface)
1912                         return;
1913                 if( ! iface->readOnly ) {
1914                         /* Folder or group */
1915                         if( pobj->type == ADDR_ITEM_FOLDER || pobj->type == ADDR_ITEM_GROUP ) {
1916                                 menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1917                                 gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1918                                 if( obj ) canEdit = TRUE;
1919                         }
1920                         /* Folder */
1921                         if( pobj->type == ADDR_ITEM_FOLDER ) {
1922                                 if (iface->type != ADDR_IF_LDAP)
1923                                         menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1924                                 if( obj ) canEdit = TRUE;
1925                         }
1926                         canDelete = canEdit;
1927                 }
1928                 if( iface->type == ADDR_IF_LDAP ) {
1929                         if( obj ) canBrowse = TRUE;
1930                         canEdit = TRUE;
1931                         canDelete = TRUE;
1932                 }
1933         }
1934
1935         if( iface ) {
1936                 /* Enable cut and paste */
1937                 if( ! addrclip_is_empty( _clipBoard_ ) )
1938                         canPaste = TRUE;
1939                 if( ! addrselect_test_empty( _addressSelect_ ) )
1940                         canCut = TRUE;
1941                 /* Enable copy if something is selected */
1942                 if( ! addrselect_test_empty( _addressSelect_ ) )
1943                         canCopy = TRUE;
1944         }
1945
1946         /* Disable edit or browse if more than one row selected */
1947         if( GTK_CLIST(clist)->selection && GTK_CLIST(clist)->selection->next ) {
1948                 canEdit = FALSE;
1949                 canBrowse = FALSE;
1950         }
1951
1952         /* Forbid write changes when read-only */
1953         if( iface && iface->readOnly ) {
1954                 canCut = FALSE;
1955                 canDelete = FALSE;
1956                 canPaste = FALSE;
1957         }
1958
1959         /* Now go finalize menu items */
1960         menu_set_sensitive( addrbook.list_factory, "/Edit",   canEdit );
1961         menu_set_sensitive( addrbook.list_factory, "/Delete", canDelete );
1962
1963         menu_set_sensitive( addrbook.list_factory, "/Cut",           canCut );
1964         menu_set_sensitive( addrbook.list_factory, "/Copy",          canCopy );
1965         menu_set_sensitive( addrbook.list_factory, "/Paste",         canPaste );
1966
1967         menu_set_sensitive( addrbook.list_factory, "/Mail To",       canCopy );
1968
1969         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",           canCut );
1970         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",          canCopy );
1971         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",         canPaste );
1972
1973         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",    canEdit );
1974         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete",  canDelete );
1975         menu_set_sensitive( addrbook.menu_factory, "/Address/Mail To", canCopy );
1976
1977         gtk_widget_set_sensitive( addrbook.edit_btn, canEdit );
1978         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1979
1980 #ifdef USE_LDAP
1981         menu_set_sensitive( addrbook.list_factory, "/Browse Entry",    canBrowse );
1982 #endif
1983 }
1984
1985 static void addressbook_select_row_tree (GtkCTree       *ctree,
1986                                          GtkCTreeNode   *node,
1987                                          gint            column,
1988                                          gpointer        data)
1989 {
1990 }
1991
1992 /**
1993  * Add list of items into tree node below specified tree node.
1994  * \param treeNode  Tree node.
1995  * \param ds        Data source.
1996  * \param listItems List of items.
1997  */
1998 static void addressbook_treenode_add_list(
1999         GtkCTreeNode *treeNode, AddressDataSource *ds, GList *listItems )
2000 {
2001         GList *node;
2002
2003         node = listItems;
2004         while( node ) {
2005                 AddrItemObject *aio;
2006                 GtkCTreeNode *nn;
2007
2008                 aio = node->data;
2009                 if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_GROUP ) {
2010                         ItemGroup *group;
2011
2012                         group = ( ItemGroup * ) aio;
2013                         nn = addressbook_node_add_group( treeNode, ds, group );
2014                 }
2015                 else if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_FOLDER ) {
2016                         ItemFolder *folder;
2017
2018                         folder = ( ItemFolder * ) aio;
2019                         nn = addressbook_node_add_folder(
2020                                 treeNode, ds, folder, ADDR_ITEM_FOLDER );
2021                 }
2022                 node = g_list_next( node );
2023         }
2024 }
2025
2026 static void addressbook_select_all_cb( void ) {
2027         gtk_clist_select_all(GTK_CLIST(addrbook.clist));
2028 }
2029
2030 /**
2031  * Cut from address list widget.
2032  */
2033 static void addressbook_clip_cut_cb( void ) {
2034         _clipBoard_->cutFlag = TRUE;
2035         addrclip_clear( _clipBoard_ );
2036         addrclip_add( _clipBoard_, _addressSelect_ );
2037         /* addrclip_list_show( _clipBoard_, stdout ); */
2038 }
2039
2040 /**
2041  * Copy from address list widget.
2042  */
2043 static void addressbook_clip_copy_cb( void ) {
2044         _clipBoard_->cutFlag = FALSE;
2045         addrclip_clear( _clipBoard_ );
2046         addrclip_add( _clipBoard_, _addressSelect_ );
2047         /* addrclip_list_show( _clipBoard_, stdout ); */
2048 }
2049
2050 /**
2051  * Paste clipboard into address list widget.
2052  */
2053 static void addressbook_clip_paste_cb( void ) {
2054         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2055         AddressObject *pobj = NULL;
2056         AddressDataSource *ds = NULL;
2057         AddressBookFile *abf = NULL;
2058         ItemFolder *folder = NULL;
2059         GList *folderGroup = NULL;
2060
2061         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2062         if( ds == NULL ) return;
2063         if( addrindex_ds_get_readonly( ds ) ) {
2064                 alertpanel_error( _("Cannot paste. Target address book is readonly.") );
2065                 return;
2066         }
2067
2068         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2069         if( pobj ) {
2070                 if( pobj->type == ADDR_ITEM_FOLDER ) {
2071                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
2072                 }
2073                 else if( pobj->type == ADDR_ITEM_GROUP ) {
2074                         alertpanel_error( _("Cannot paste into an address group.") );
2075                         return;
2076                 }
2077         }
2078
2079         /* Get an address book */
2080         abf = addressbook_get_book_file();
2081         if( abf == NULL ) return;
2082
2083         if( _clipBoard_->cutFlag ) {
2084                 /* Paste/Cut */
2085                 folderGroup = addrclip_paste_cut( _clipBoard_, abf, folder );
2086
2087                 /* Remove all groups and folders in clipboard from tree node */
2088                 addressbook_treenode_remove_item();
2089
2090                 /* Remove all "cut" items */
2091                 addrclip_delete_item( _clipBoard_ );
2092
2093                 /* Clear clipboard - cut items??? */
2094                 addrclip_clear( _clipBoard_ );
2095         }
2096         else {
2097                 /* Paste/Copy */
2098                 folderGroup = addrclip_paste_copy( _clipBoard_, abf, folder );
2099         }
2100
2101         /* addrclip_list_show( _clipBoard_, stdout ); */
2102         if( folderGroup ) {
2103                 /* Update tree by inserting node for each folder or group */
2104                 addressbook_treenode_add_list(
2105                         addrbook.treeSelected, ds, folderGroup );
2106                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2107                 g_list_free( folderGroup );
2108                 folderGroup = NULL;
2109         }
2110
2111         /* Display items pasted */
2112         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened );
2113         addressbook_set_clist(
2114                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
2115                         addrbook.opened),
2116                 TRUE);
2117         
2118
2119 }
2120
2121 /**
2122  * Add current treenode object to clipboard. Note that widget only allows
2123  * one entry from the tree list to be selected.
2124  */
2125 static void addressbook_treenode_to_clipboard( void ) {
2126         AddressObject *obj = NULL;
2127         AddressDataSource *ds = NULL;
2128         AddrSelectItem *item;
2129         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2130         GtkCTreeNode *node;
2131
2132         node = addrbook.treeSelected;
2133         if( node == NULL ) return;
2134         obj = gtk_ctree_node_get_row_data( ctree, node );
2135         if( obj == NULL ) return;
2136
2137         ds = addressbook_find_datasource( node );
2138         if( ds == NULL ) return;
2139
2140         item = NULL;
2141         if( obj->type == ADDR_ITEM_FOLDER ) {
2142                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2143                 ItemFolder *folder = adapter->itemFolder;
2144
2145                 item = addrselect_create_node( obj );
2146                 item->uid = g_strdup( ADDRITEM_ID(folder) );
2147         }
2148         else if( obj->type == ADDR_ITEM_GROUP ) {
2149                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2150                 ItemGroup *group = adapter->itemGroup;
2151
2152                 item = addrselect_create_node( obj );
2153                 item->uid = g_strdup( ADDRITEM_ID(group) );
2154         }
2155         else if( obj->type == ADDR_DATASOURCE ) {
2156                 /* Data source */
2157                 item = addrselect_create_node( obj );
2158                 item->uid = NULL;
2159         }
2160
2161         if( item ) {
2162                 /* Clear existing list and add item into list */
2163                 gchar *cacheID;
2164
2165                 addressbook_list_select_clear();
2166                 cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2167                 addrselect_list_add( _addressSelect_, item, cacheID );
2168                 g_free( cacheID );
2169         }
2170 }
2171
2172 /**
2173  * Cut from tree widget.
2174  */
2175 static void addressbook_treenode_cut_cb( void ) {
2176         _clipBoard_->cutFlag = TRUE;
2177         addressbook_treenode_to_clipboard();
2178         addrclip_clear( _clipBoard_ );
2179         addrclip_add( _clipBoard_, _addressSelect_ );
2180         /* addrclip_list_show( _clipBoard_, stdout ); */
2181 }
2182
2183 /**
2184  * Copy from tree widget.
2185  */
2186 static void addressbook_treenode_copy_cb( void ) {
2187         _clipBoard_->cutFlag = FALSE;
2188         addressbook_treenode_to_clipboard();
2189         addrclip_clear( _clipBoard_ );
2190         addrclip_add( _clipBoard_, _addressSelect_ );
2191         /* addrclip_list_show( _clipBoard_, stdout ); */
2192 }
2193
2194 /**
2195  * Paste clipboard into address tree widget.
2196  */
2197 static void addressbook_treenode_paste_cb( void ) {
2198         addressbook_clip_paste_cb();
2199 }
2200
2201 /**
2202  * Clear selected entries in clipboard.
2203  */
2204 static void addressbook_list_select_clear( void ) {
2205         addrselect_list_clear( _addressSelect_ );
2206 }
2207
2208 /**
2209  * Add specified address item to selected address list.
2210  * \param aio Address item object.
2211  * \param ds  Datasource.
2212  */
2213 static void addressbook_list_select_add( AddrItemObject *aio, AddressDataSource *ds ) {
2214         gchar *cacheID;
2215
2216         if( ds == NULL ) return;
2217         cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2218         addrselect_list_add_obj( _addressSelect_, aio, cacheID );
2219         g_free( cacheID );
2220 }
2221
2222 /**
2223  * Remove specified address item from selected address list.
2224  * \param aio Address item object.
2225  */
2226 static void addressbook_list_select_remove( AddrItemObject *aio ) {
2227         addrselect_list_remove( _addressSelect_, aio );
2228 }
2229
2230 /**
2231  * Invoke EMail compose window with addresses in selected address list.
2232  */
2233 static void addressbook_mail_to_cb( void ) {
2234         GList *listAddress;
2235
2236         if( ! addrselect_test_empty( _addressSelect_ ) ) {
2237                 listAddress = addrselect_build_list( _addressSelect_ );
2238                 compose_new_with_list( NULL, listAddress );
2239                 mgu_free_dlist( listAddress );
2240                 listAddress = NULL;
2241         }
2242 }
2243
2244 static void addressbook_list_row_selected( GtkCTree *clist,
2245                                            GtkCTreeNode *node,
2246                                            gint column,
2247                                            gpointer data )
2248 {
2249         GtkEntry *entry = GTK_ENTRY(addrbook.entry);
2250         AddrItemObject *aio = NULL;
2251         AddressObject *pobj = NULL;
2252         AdapterDSource *ads = NULL;
2253         AddressDataSource *ds = NULL;
2254
2255         gtk_entry_set_text( entry, "" );
2256         addrbook.listSelected = node;
2257
2258         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
2259         if( pobj == NULL ) return;
2260
2261         if( pobj->type == ADDR_DATASOURCE ) {
2262                 ads = ADAPTER_DSOURCE(pobj);
2263                 ds = ads->dataSource;
2264         }
2265         else if( pobj->type != ADDR_INTERFACE ) {
2266                 ds = addressbook_find_datasource( addrbook.treeSelected );
2267         }
2268
2269         aio = gtk_ctree_node_get_row_data( clist, node );
2270         if( aio ) {
2271                 /* g_print( "list select: %d : '%s'\n", aio->type, aio->name ); */
2272                 addressbook_list_select_add( aio, ds );
2273         }
2274
2275         addressbook_list_menu_setup();
2276
2277         if (!addrbook.target_compose && !prefs_common.addressbook_use_editaddress_dialog) {
2278                 AddressObject *obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
2279
2280                 if (obj && obj->type != ADDR_ITEM_GROUP)
2281                         addressbook_edit_address(NULL, 0, NULL, FALSE);
2282         }
2283 }
2284
2285 static void addressbook_list_row_unselected( GtkCTree *ctree,
2286                                              GtkCTreeNode *node,
2287                                              gint column,
2288                                              gpointer data )
2289 {
2290         AddrItemObject *aio;
2291
2292         aio = gtk_ctree_node_get_row_data( ctree, node );
2293         if( aio != NULL ) {
2294                 /* g_print( "list unselect: %d : '%s'\n", aio->type, aio->name ); */
2295                 addressbook_list_select_remove( aio );
2296         }
2297
2298         if (!prefs_common.addressbook_use_editaddress_dialog)
2299                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
2300 }
2301
2302 /* from gdkevents.c */
2303 #define DOUBLE_CLICK_TIME 250
2304
2305 static gboolean addressbook_list_button_pressed(GtkWidget *widget,
2306                                                 GdkEventButton *event,
2307                                                 gpointer data)
2308 {
2309         static guint32 lasttime = 0;
2310         if( ! event ) return FALSE;
2311
2312         addressbook_list_menu_setup();
2313
2314         if( event->button == 3 ) {
2315                 gtk_menu_popup( GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
2316                        event->button, event->time );
2317         } else if (event->button == 1) {
2318                 if (event->time - lasttime < DOUBLE_CLICK_TIME) {
2319                         if (prefs_common.add_address_by_click &&
2320                             addrbook.target_compose)
2321                                 addressbook_to_clicked(NULL, GINT_TO_POINTER(COMPOSE_TO));
2322                         else
2323                                 if (prefs_common.addressbook_use_editaddress_dialog)
2324                                         addressbook_edit_address_cb(NULL, 0, NULL);
2325                                 else {
2326                                         GtkCTree *clist = GTK_CTREE(addrbook.clist);
2327                                         AddressObject *obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
2328                                         if( obj && obj->type == ADDR_ITEM_GROUP )
2329                                                 addressbook_edit_address_cb(NULL, 0, NULL);
2330                                 }
2331
2332                         lasttime = 0;
2333                 } else
2334                         lasttime = event->time;
2335         }
2336
2337         return FALSE;
2338 }
2339
2340 static gboolean addressbook_list_button_released(GtkWidget *widget,
2341                                                  GdkEventButton *event,
2342                                                  gpointer data)
2343 {
2344         return FALSE;
2345 }
2346
2347 static gboolean addressbook_tree_button_pressed(GtkWidget *ctree,
2348                                                 GdkEventButton *event,
2349                                                 gpointer data)
2350 {
2351         GtkCList *clist = GTK_CLIST(ctree);
2352         gint row, column;
2353         AddressObject *obj = NULL;
2354         AdapterDSource *ads = NULL;
2355         AddressInterface *iface = NULL;
2356         AddressDataSource *ds = NULL;
2357         gboolean canEdit = FALSE;
2358         gboolean canDelete = FALSE;
2359         gboolean canCut = FALSE;
2360         gboolean canCopy = FALSE;
2361         gboolean canPaste = FALSE;
2362         gboolean canTreeCut = FALSE;
2363         gboolean canTreeCopy = FALSE;
2364         gboolean canTreePaste = FALSE;
2365         gboolean canLookup = FALSE;
2366         GtkCTreeNode *node = NULL;
2367
2368         if( ! event ) return FALSE;
2369         addressbook_menubar_set_sensitive( FALSE );
2370
2371         if( gtk_clist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
2372                 gtkut_clist_set_focus_row(clist, row);
2373                 obj = gtk_clist_get_row_data( clist, row );
2374         }
2375
2376         menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.tree_popup));
2377
2378         if( obj == NULL ) return FALSE;
2379         node = gtk_ctree_node_nth(GTK_CTREE(clist), row);
2380
2381         if( ! addrclip_is_empty( _clipBoard_ ) ) {
2382                 canTreePaste = TRUE;
2383         }
2384         if (obj->type == ADDR_INTERFACE) {
2385                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
2386                 if( !adapter )
2387                         goto just_set_sens;
2388                 iface = adapter->interface;
2389                 if( !iface )
2390                         goto just_set_sens;
2391                 if( !iface->readOnly ) {
2392                         menu_set_sensitive( addrbook.tree_factory, "/New Book", TRUE );
2393                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2394                 }
2395                 if( iface->externalQuery ) canLookup = TRUE;
2396         }
2397         if (obj->type == ADDR_DATASOURCE) {
2398                 ads = ADAPTER_DSOURCE(obj);
2399                 ds = ads->dataSource;
2400                 if( !ds )
2401                         goto just_set_sens;
2402                 iface = ds->interface;
2403                 if( !iface )
2404                         goto just_set_sens;
2405                 if( !iface->readOnly ) {
2406                         canDelete = TRUE;
2407                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2408                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2409                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2410                 }
2411                 canEdit = TRUE;
2412                 canTreeCopy = TRUE;
2413                 if( iface->externalQuery ) canLookup = TRUE;
2414         }
2415         else if (obj->type == ADDR_ITEM_FOLDER) {
2416                 ds = addressbook_find_datasource( node );
2417                 if( !ds )
2418                         goto just_set_sens;
2419                 iface = ds->interface;
2420                 if( !iface )
2421                         goto just_set_sens;
2422                 if( !iface->readOnly ) {
2423                         canEdit = TRUE;
2424                         canDelete = TRUE;
2425                         canTreeCut = TRUE;
2426                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2427                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2428                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2429                 }
2430                 canTreeCopy = TRUE;
2431
2432                 if( iface->externalQuery ) {
2433                         /* Enable deletion of LDAP folder */
2434                         canLookup = TRUE;
2435                         canDelete = TRUE;
2436                 }
2437         }
2438         else if (obj->type == ADDR_ITEM_GROUP) {
2439                 ds = addressbook_find_datasource( node );
2440                 if( !ds )
2441                         goto just_set_sens;
2442                 iface = ds->interface;
2443                 if( !iface )
2444                         goto just_set_sens;
2445                 if( ! iface->readOnly ) {
2446                         canEdit = TRUE;
2447                         canDelete = TRUE;
2448                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
2449                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2450                 }
2451         }
2452
2453         if( canEdit ) {
2454                 if( ! addrselect_test_empty( _addressSelect_ ) )
2455                         canCut = TRUE;
2456         }
2457         if( ! addrselect_test_empty( _addressSelect_ ) )
2458                 canCopy = TRUE;
2459         if( ! addrclip_is_empty( _clipBoard_ ) )
2460                 canPaste = TRUE;
2461
2462         /* Forbid write changes when read-only */
2463         if( iface && iface->readOnly ) {
2464                 canTreeCut = FALSE;
2465                 canTreePaste = FALSE;
2466                 canCut = FALSE;
2467                 canDelete = FALSE;
2468                 canPaste = FALSE;
2469         }
2470
2471 just_set_sens:
2472         /* Enable edit */
2473         menu_set_sensitive( addrbook.tree_factory, "/Edit",   canEdit );
2474         menu_set_sensitive( addrbook.tree_factory, "/Delete", canDelete );
2475         menu_set_sensitive( addrbook.tree_factory, "/Cut",    canTreeCut );
2476         menu_set_sensitive( addrbook.tree_factory, "/Copy",   canTreeCopy );
2477         menu_set_sensitive( addrbook.tree_factory, "/Paste",  canTreePaste );
2478
2479         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",          canEdit );
2480         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book",        canEdit );
2481         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",           canCut );
2482         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",          canCopy );
2483         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",         canPaste );
2484
2485         addressbook_show_buttons(addrbook.target_compose == NULL, canLookup,
2486                         addrbook.target_compose != NULL);
2487
2488         if( event->button == 3 ) {
2489                 gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
2490                                event->button, event->time);
2491         }
2492
2493         return FALSE;
2494 }
2495
2496 static gboolean addressbook_tree_button_released(GtkWidget *ctree,
2497                                                  GdkEventButton *event,
2498                                                  gpointer data)
2499 {
2500         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree), addrbook.opened);
2501         return FALSE;
2502 }
2503
2504 static void addressbook_new_folder_cb(gpointer data, guint action,
2505                                       GtkWidget *widget)
2506 {
2507         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2508         AddressObject *obj = NULL;
2509         AddressDataSource *ds = NULL;
2510         AddressBookFile *abf = NULL;
2511         ItemFolder *parentFolder = NULL;
2512         ItemFolder *folder = NULL;
2513
2514         if( ! addrbook.treeSelected ) return;
2515         obj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2516         if( obj == NULL ) return;
2517         ds = addressbook_find_datasource( addrbook.treeSelected );
2518         if( ds == NULL ) return;
2519
2520         if( obj->type == ADDR_DATASOURCE ) {
2521                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2522         }
2523         else if( obj->type == ADDR_ITEM_FOLDER ) {
2524                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2525         }
2526         else {
2527                 return;
2528         }
2529
2530         abf = ds->rawDataSource;
2531         if( abf == NULL ) return;
2532         folder = addressbook_edit_folder( abf, parentFolder, NULL );
2533         if( folder ) {
2534                 GtkCTreeNode *nn;
2535                 nn = addressbook_node_add_folder(
2536                         addrbook.treeSelected, ds, folder, ADDR_ITEM_FOLDER );
2537                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2538                 if( addrbook.treeSelected == addrbook.opened )
2539                         addressbook_set_clist(obj, TRUE);
2540         }
2541
2542 }
2543
2544 static void addressbook_new_group_cb(gpointer data, guint action,
2545                                      GtkWidget *widget)
2546 {
2547         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2548         AddressObject *obj = NULL;
2549         AddressDataSource *ds = NULL;
2550         AddressBookFile *abf = NULL;
2551         ItemFolder *parentFolder = NULL;
2552         ItemGroup *group = NULL;
2553
2554         if( ! addrbook.treeSelected ) return;
2555         obj = gtk_ctree_node_get_row_data(ctree, addrbook.treeSelected);
2556         if( obj == NULL ) return;
2557         ds = addressbook_find_datasource( addrbook.treeSelected );
2558         if( ds == NULL ) return;
2559
2560         if( obj->type == ADDR_DATASOURCE ) {
2561                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2562         }
2563         else if( obj->type == ADDR_ITEM_FOLDER ) {
2564                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2565         }
2566         else {
2567                 return;
2568         }
2569
2570         abf = ds->rawDataSource;
2571         if( abf == NULL ) return;
2572         group = addressbook_edit_group( abf, parentFolder, NULL );
2573         if( group ) {
2574                 GtkCTreeNode *nn;
2575                 nn = addressbook_node_add_group( addrbook.treeSelected, ds, group );
2576                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2577                 if( addrbook.treeSelected == addrbook.opened )
2578                         addressbook_set_clist(obj, TRUE);
2579         }
2580
2581 }
2582
2583 static void addressbook_change_node_name(GtkCTreeNode *node, const gchar *name)
2584 {
2585         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2586         gchar *text[1];
2587         guint8 spacing;
2588         GdkPixmap *pix_cl, *pix_op;
2589         GdkBitmap *mask_cl, *mask_op;
2590         gboolean is_leaf, expanded;
2591
2592         gtk_ctree_get_node_info(ctree, node, text, &spacing,
2593                                 &pix_cl, &mask_cl, &pix_op, &mask_op,
2594                                 &is_leaf, &expanded);
2595         gtk_sctree_set_node_info(ctree, node, name, spacing,
2596                                 pix_cl, mask_cl, pix_op, mask_op,
2597                                 is_leaf, expanded);
2598 }
2599
2600 /**
2601  * Edit data source.
2602  * \param obj  Address object to edit.
2603  * \param node Node in tree.
2604  * \return New name of data source.
2605  */
2606 static gchar *addressbook_edit_datasource( AddressObject *obj, GtkCTreeNode *node ) {
2607         gchar *newName = NULL;
2608         AddressDataSource *ds = NULL;
2609         AddressInterface *iface = NULL;
2610         AdapterDSource *ads = NULL;
2611
2612         ds = addressbook_find_datasource( node );
2613         if( ds == NULL ) return NULL;
2614         iface = ds->interface;
2615         if( ! iface->haveLibrary ) return NULL;
2616
2617         /* Read data from data source */
2618         if( addrindex_ds_get_modify_flag( ds ) ) {
2619                 addrindex_ds_read_data( ds );
2620         }
2621
2622         if( ! addrindex_ds_get_read_flag( ds ) ) {
2623                 addrindex_ds_read_data( ds );
2624         }
2625
2626         /* Handle edit */
2627         ads = ADAPTER_DSOURCE(obj);
2628         if( ads->subType == ADDR_BOOK ) {
2629                 if( addressbook_edit_book( _addressIndex_, ads ) == NULL ) return NULL;
2630         }
2631         else if( ads->subType == ADDR_VCARD ) {
2632                 if( addressbook_edit_vcard( _addressIndex_, ads ) == NULL ) return NULL;
2633         }
2634 #ifdef USE_JPILOT
2635         else if( ads->subType == ADDR_JPILOT ) {
2636                 if( addressbook_edit_jpilot( _addressIndex_, ads ) == NULL ) return NULL;
2637         }
2638 #endif
2639 #ifdef USE_LDAP
2640         else if( ads->subType == ADDR_LDAP ) {
2641                 if( addressbook_edit_ldap( _addressIndex_, ads ) == NULL ) return NULL;
2642         }
2643 #endif
2644         else {
2645                 return NULL;
2646         }
2647         newName = obj->name;
2648         return newName;
2649 }
2650
2651 /*
2652 * Edit an object that is in the address tree area.
2653 */
2654 static void addressbook_treenode_edit_cb(gpointer data, guint action,
2655                                        GtkWidget *widget)
2656 {
2657         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2658         AddressObject *obj;
2659         AddressDataSource *ds = NULL;
2660         AddressBookFile *abf = NULL;
2661         GtkCTreeNode *node = NULL, *parentNode = NULL;
2662         gchar *name = NULL;
2663
2664         if( ! addrbook.treeSelected ) return;
2665         node = addrbook.treeSelected;
2666         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2667         obj = gtk_ctree_node_get_row_data( ctree, node );
2668         if( obj == NULL ) return;
2669         parentNode = GTK_CTREE_ROW(node)->parent;
2670
2671         ds = addressbook_find_datasource( node );
2672         if( ds == NULL ) return;
2673
2674         if( obj->type == ADDR_DATASOURCE ) {
2675                 name = addressbook_edit_datasource( obj, node );
2676                 if( name == NULL ) return;
2677         }
2678         else {
2679                 abf = ds->rawDataSource;
2680                 if( abf == NULL ) return;
2681                 if( obj->type == ADDR_ITEM_FOLDER ) {
2682                         AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2683                         ItemFolder *item = adapter->itemFolder;
2684                         ItemFolder *parentFolder = NULL;
2685                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2686                         if( addressbook_edit_folder( abf, parentFolder, item ) == NULL ) return;
2687                         name = ADDRITEM_NAME(item);
2688                 }
2689                 else if( obj->type == ADDR_ITEM_GROUP ) {
2690                         AdapterGroup *adapter = ADAPTER_GROUP(obj);
2691                         ItemGroup *item = adapter->itemGroup;
2692                         ItemFolder *parentFolder = NULL;
2693                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2694                         if( addressbook_edit_group( abf, parentFolder, item ) == NULL ) return;
2695                         name = ADDRITEM_NAME(item);
2696                 }
2697         }
2698         if( name && parentNode ) {
2699                 /* Update node in tree view */
2700                 addressbook_change_node_name( node, name );
2701                 gtk_sctree_sort_node(ctree, parentNode);
2702                 gtk_ctree_expand( ctree, node );
2703                 gtk_sctree_select( GTK_SCTREE( ctree), node );
2704         }
2705 }
2706
2707 typedef enum {
2708         ADDRTREE_DEL_NONE,
2709         ADDRTREE_DEL_DATA,
2710         ADDRTREE_DEL_FOLDER_ONLY,
2711         ADDRTREE_DEL_FOLDER_ADDR
2712 } TreeItemDelType ;
2713
2714 /**
2715  * Delete an item from the tree widget.
2716  * \param data   Data passed in.
2717  * \param action Action.
2718  * \param widget Widget issuing callback.
2719  */
2720 static void addressbook_treenode_delete_cb(
2721                 gpointer data, guint action, GtkWidget *widget )
2722 {
2723         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2724         GtkCTreeNode *node = NULL;
2725         AddressObject *obj;
2726         gchar *message;
2727         AlertValue aval;
2728         AddrBookBase *adbase;
2729         AddressCache *cache;
2730         AdapterDSource *ads = NULL;
2731         AddressInterface *iface = NULL;
2732         AddressDataSource *ds = NULL;
2733         gboolean remFlag = FALSE;
2734         TreeItemDelType delType;
2735
2736         if( ! addrbook.treeSelected ) return;
2737         node = addrbook.treeSelected;
2738         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2739
2740         obj = gtk_ctree_node_get_row_data( ctree, node );
2741         g_return_if_fail(obj != NULL);
2742
2743         if( obj->type == ADDR_DATASOURCE ) {
2744                 ads = ADAPTER_DSOURCE(obj);
2745                 if( ads == NULL ) return;
2746                 ds = ads->dataSource;
2747                 if( ds == NULL ) return;
2748         }
2749         else {
2750                 /* Must be folder or something else */
2751                 ds = addressbook_find_datasource( node );
2752                 if( ds == NULL ) return;
2753
2754                 /* Only allow deletion from non-readOnly */
2755                 iface = ds->interface;
2756                 if( iface->readOnly ) {
2757                         /* Allow deletion of query results */
2758                         if( ! iface->externalQuery ) return;
2759                 }
2760         }
2761
2762         /* Confirm deletion */
2763         delType = ADDRTREE_DEL_NONE;
2764         if( obj->type == ADDR_ITEM_FOLDER ) {
2765                 if( iface->externalQuery ) {
2766                         message = g_strdup_printf( _(
2767                                 "Do you want to delete the query " \
2768                                 "results and addresses in '%s' ?" ),
2769                                 obj->name );
2770                         aval = alertpanel( _("Delete"), message,
2771                                 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
2772                         g_free(message);
2773                         if( aval == G_ALERTALTERNATE ) {
2774                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2775                         }
2776                 }
2777                 else {
2778                         message = g_strdup_printf
2779                                 ( _( "Do you want to delete '%s' ?"
2780                                      "If you delete the folder only, the addresses it contains will be moved into the parent folder." ),
2781                                  obj->name );
2782                         aval = alertpanel( _("Delete folder"), message,
2783                                 GTK_STOCK_CANCEL, _("+Delete _folder only"), _("Delete folder and _addresses"));
2784                         g_free(message);
2785                         if( aval == G_ALERTALTERNATE ) {
2786                                 delType = ADDRTREE_DEL_FOLDER_ONLY;
2787                         }
2788                         else if( aval == G_ALERTOTHER ) {
2789                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2790                         }
2791                 }
2792         }
2793         else if( obj->type == ADDR_ITEM_GROUP ) {
2794                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2795                                             "The addresses it contains will not be lost."), obj->name);
2796                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
2797                                 "+" GTK_STOCK_DELETE, NULL);
2798                 g_free(message);
2799                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_FOLDER_ONLY;
2800         } else {
2801                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2802                                             "The addresses it contains will be lost."), obj->name);
2803                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
2804                                 "+" GTK_STOCK_DELETE, NULL);
2805                 g_free(message);
2806                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_DATA;
2807         }
2808         if( delType == ADDRTREE_DEL_NONE ) return;
2809
2810         /* Proceed with deletion */
2811         if( obj->type == ADDR_DATASOURCE ) {
2812                 /* Remove node from tree */
2813                 gtk_ctree_remove_node( ctree, node );
2814         
2815                 /* Remove data source. */
2816                 if( addrindex_index_remove_datasource( _addressIndex_, ds ) ) {
2817                         addrindex_free_datasource( ds );
2818                 }
2819                 return;
2820         }
2821
2822         /* Get reference to cache */
2823         adbase = ( AddrBookBase * ) ds->rawDataSource;
2824         if( adbase == NULL ) return;
2825         cache = adbase->addressCache;
2826
2827         /* Remove query results folder */
2828         if( iface->externalQuery ) {
2829                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2830                 ItemFolder *folder = adapter->itemFolder;
2831
2832                 adapter->itemFolder = NULL;
2833                 /*
2834                 g_print( "remove folder for ::%s::\n", obj->name );
2835                 g_print( "      folder name ::%s::\n", ADDRITEM_NAME(folder) );
2836                 g_print( "-------------- remove results\n" );
2837                 */
2838                 addrindex_remove_results( ds, folder );
2839                 /* g_print( "-------------- remove node\n" ); */
2840                 gtk_ctree_remove_node( ctree, node );
2841                 return;
2842         }
2843
2844         /* Code below is valid for regular address book deletion */
2845         if( obj->type == ADDR_ITEM_FOLDER ) {
2846                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2847                 ItemFolder *item = adapter->itemFolder;
2848
2849                 if( delType == ADDRTREE_DEL_FOLDER_ONLY ) {
2850                         /* Remove folder only */
2851                         item = addrcache_remove_folder( cache, item );
2852                         if( item ) {
2853                                 addritem_free_item_folder( item );
2854                                 addressbook_move_nodes_up( ctree, node );
2855                                 remFlag = TRUE;
2856                         }
2857                 }
2858                 else if( delType == ADDRTREE_DEL_FOLDER_ADDR ) {
2859                         /* Remove folder and addresses */
2860                         item = addrcache_remove_folder_delete( cache, item );
2861                         if( item ) {
2862                                 addritem_free_item_folder( item );
2863                                 remFlag = TRUE;
2864                         }
2865                 }
2866         }
2867         else if( obj->type == ADDR_ITEM_GROUP ) {
2868                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2869                 ItemGroup *item = adapter->itemGroup;
2870
2871                 item = addrcache_remove_group( cache, item );
2872                 if( item ) {
2873                         addritem_free_item_group( item );
2874                         remFlag = TRUE;
2875                 }
2876         }
2877
2878         if( remFlag ) {
2879                 /* Remove node. */
2880                 gtk_ctree_remove_node(ctree, node );
2881         }
2882 }
2883
2884 static void addressbook_new_address_from_book_post_cb( ItemPerson *person )
2885 {
2886         if( person && addrbook.treeSelected == addrbook.opened ) {
2887                 person->status = ADD_ENTRY;
2888                 gtk_clist_unselect_all( GTK_CLIST(addrbook.clist) );
2889                 addressbook_folder_refresh_one_person(
2890                         GTK_CTREE(addrbook.clist), person );
2891         }
2892         addressbook_address_list_set_focus();
2893 }
2894
2895 static void addressbook_new_address_from_folder_post_cb( ItemPerson *person )
2896 {
2897         if( person && addrbook.treeSelected == addrbook.opened) {
2898                 person->status = ADD_ENTRY;
2899                 gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
2900                 addressbook_set_clist(
2901                         gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
2902                                 addrbook.opened),
2903                         TRUE);
2904         }
2905         addressbook_address_list_set_focus();
2906 }
2907
2908 /**
2909  * Label (a format string) that is used to name each folder.
2910  */
2911 static gchar *_queryFolderLabel_ = N_( "Search '%s'" );
2912
2913 /**
2914  * Search ctree widget callback function.
2915  * \param  pA Pointer to node.
2916  * \param  pB Pointer to data item being sought.
2917  * \return Zero (0) if folder found.
2918  */
2919 static int addressbook_treenode_find_folder_cb( gconstpointer pA, gconstpointer pB ) {
2920         AddressObject *aoA;
2921
2922         aoA = ( AddressObject * ) pA;
2923         if( aoA->type == ADDR_ITEM_FOLDER ) {
2924                 ItemFolder *folder, *fld;
2925
2926                 fld = ADAPTER_FOLDER(aoA)->itemFolder;
2927                 folder = ( ItemFolder * ) pB;
2928                 if( fld == folder ) return 0;   /* Found folder */
2929         }
2930         return 1;
2931 }
2932
2933 static ItemFolder * addressbook_setup_subf(
2934                 AddressDataSource *ds, gchar *title,
2935                 GtkCTreeNode *pNode )
2936 {
2937         AddrBookBase *adbase;
2938         AddressCache *cache;
2939         ItemFolder *folder;
2940         GtkCTree *ctree;
2941         GtkCTreeNode *nNode;
2942         gchar *name;
2943         AddressObjectType aoType = ADDR_NONE;
2944         GList *children;
2945         /* Setup a query */
2946         if( *title == '\0' || strlen( title ) < 1 ) return NULL;
2947
2948         if( ds && ds->type == ADDR_IF_LDAP ) {
2949 #if USE_LDAP
2950                 aoType = ADDR_LDAP_QUERY;
2951 #endif
2952         }
2953         else {
2954                 return NULL;
2955         }
2956
2957         ctree = GTK_CTREE(addrbook.ctree);
2958         /* Get reference to address cache */    
2959         adbase = ( AddrBookBase * ) ds->rawDataSource;
2960         cache = adbase->addressCache;
2961         
2962         if ((children = addrcache_get_list_folder(cache)) != NULL) {
2963                 GList *cur = children;
2964                 for (; cur; cur = cur->next) {
2965                         ItemFolder *child = (ItemFolder *) cur->data;
2966                         if (!strcmp2(ADDRITEM_NAME(child), title)) {
2967                                 nNode = gtk_ctree_find_by_row_data_custom(
2968                                         ctree, NULL, child,
2969                                         addressbook_treenode_find_folder_cb );
2970                                 if( nNode ) {
2971                                         addrindex_remove_results( ds, child );
2972                                         while( child->listPerson ) {
2973                                                 ItemPerson *item = ( ItemPerson * ) child->listPerson->data;
2974                                                 item = addrcache_remove_person( cache, item );
2975                                                 if( item ) {
2976                                                         addritem_free_item_person( item );
2977                                                         item = NULL;
2978                                                 }
2979                                         }
2980                                         gtk_sctree_select( GTK_SCTREE(ctree), nNode );
2981                                         addrbook.treeSelected = nNode;
2982                                 }       
2983                                 return child;
2984                         }
2985                 }
2986         }
2987         
2988         /* Create a folder */
2989         folder = addrcache_add_new_folder( cache, NULL );
2990         name = g_strdup_printf( "%s", title );
2991         addritem_folder_set_name( folder, name );
2992         addritem_folder_set_remarks( folder, "" );
2993         g_free( name );
2994
2995         /* Now let's see the folder */
2996         nNode = addressbook_node_add_folder( pNode, ds, folder, aoType );
2997         gtk_ctree_expand( ctree, pNode );
2998         if( nNode ) {
2999                 gtk_sctree_select( GTK_SCTREE(ctree), nNode );
3000                 addrbook.treeSelected = nNode;
3001                 return folder;
3002         }
3003         return NULL;
3004 }
3005
3006 static void addressbook_new_address_cb( gpointer data, guint action, GtkWidget *widget ) {
3007         AddressObject *pobj = NULL;
3008         AddressDataSource *ds = NULL;
3009         AddressBookFile *abf = NULL;
3010         debug_print("adding address\n");
3011         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
3012         if( pobj == NULL ) {
3013                 debug_print("no row data\n");
3014                 return;
3015         }
3016         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3017         if( ds == NULL ) {
3018                 debug_print("no datasource\n");
3019                 return;
3020         }
3021
3022         abf = ds->rawDataSource;
3023         if( abf == NULL ) {
3024                 g_print("no addressbook file\n");
3025                 return;
3026         }
3027
3028         if( pobj->type == ADDR_DATASOURCE ) {
3029                 if (ADAPTER_DSOURCE(pobj)->subType == ADDR_BOOK ||
3030                     ADAPTER_DSOURCE(pobj)->subType == ADDR_LDAP) {
3031                         ItemPerson *person;
3032                         ItemFolder *folder = NULL;
3033 #ifdef USE_LDAP
3034                         if (abf && abf->type == ADDR_IF_LDAP) {
3035                                 GtkCTreeNode *parentNode;
3036                                 ds = addressbook_find_datasource( GTK_CTREE_NODE( addrbook.treeSelected ) );
3037                                 if( ds == NULL ) return;
3038
3039                                 /* We must have a datasource that is an external interface */
3040                                 if( ! ds->interface->haveLibrary ) return;
3041                                 if( ! ds->interface->externalQuery ) return;
3042
3043                                 if( pobj->type == ADDR_ITEM_FOLDER ) {
3044                                         parentNode = GTK_CTREE_ROW(GTK_CTREE_NODE( addrbook.treeSelected ) )->parent;
3045                                 }
3046                                 else {
3047                                         parentNode = GTK_CTREE_NODE( addrbook.treeSelected );
3048                                 }
3049                                 folder = addressbook_setup_subf( ds, _("New Contacts"), parentNode );
3050
3051                                 pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
3052                                 ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3053                                 abf = ds->rawDataSource;
3054                         }
3055 #endif
3056                         person = addressbook_edit_person( abf, folder, NULL, FALSE,
3057                                                                   addrbook.editaddress_vbox,
3058                                                                   addressbook_new_address_from_book_post_cb,
3059                                                                   TRUE );
3060 #ifdef USE_LDAP
3061                         if (abf && abf->type == ADDR_IF_LDAP) {
3062                                 LdapServer *server = ds->rawDataSource;
3063                                 ldapsvr_set_modified(server, TRUE);
3064                                 ldapsvr_update_book(server, NULL);
3065                                 if (server->retVal != LDAPRC_SUCCESS) {
3066                                         alertpanel( _("Add address(es)"),
3067                                                 addressbook_err2string(_lutErrorsLDAP_, server->retVal),
3068                                                 GTK_STOCK_CLOSE, NULL, NULL );
3069                                         server->retVal = LDAPRC_SUCCESS;
3070                                         return;
3071                                 }
3072                         }
3073 #endif
3074                         if (prefs_common.addressbook_use_editaddress_dialog)
3075                                 addressbook_new_address_from_book_post_cb( person );
3076                 }
3077         }
3078         else if( pobj->type == ADDR_ITEM_FOLDER ) {
3079                 /* New address */
3080                 ItemFolder *folder = ADAPTER_FOLDER(pobj)->itemFolder;
3081                 ItemPerson *person;
3082 #ifdef USE_LDAP
3083                 if (abf && abf->type == ADDR_IF_LDAP) {
3084                         GtkCTreeNode *parentNode;
3085                         ds = addressbook_find_datasource( GTK_CTREE_NODE( addrbook.treeSelected ) );
3086                         if( ds == NULL ) return;
3087
3088                         /* We must have a datasource that is an external interface */
3089                         if( ! ds->interface->haveLibrary ) return;
3090                         if( ! ds->interface->externalQuery ) return;
3091
3092                         if( pobj->type == ADDR_ITEM_FOLDER ) {
3093                                 parentNode = GTK_CTREE_ROW(GTK_CTREE_NODE( addrbook.treeSelected ) )->parent;
3094                         }
3095                         else {
3096                                 parentNode = GTK_CTREE_NODE( addrbook.treeSelected );
3097                         }
3098                         folder = addressbook_setup_subf( ds, _("New Contacts"), parentNode );
3099                         if (!folder)
3100                                 return;
3101                         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
3102                         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3103                         abf = ds->rawDataSource;
3104                 }
3105 #endif
3106                 person = addressbook_edit_person( abf, folder, NULL, FALSE,
3107                                                           addrbook.editaddress_vbox,
3108                                                           addressbook_new_address_from_folder_post_cb,
3109                                                           TRUE );
3110 #ifdef USE_LDAP
3111                 if (abf && abf->type == ADDR_IF_LDAP) {
3112                         LdapServer *server = ds->rawDataSource;
3113                         ldapsvr_set_modified(server, TRUE);
3114                         ldapsvr_update_book(server, NULL);
3115                         if (server->retVal != LDAPRC_SUCCESS) {
3116                                 alertpanel( _("Add address(es)"),
3117                                                 addressbook_err2string(_lutErrorsLDAP_, server->retVal),
3118                                         GTK_STOCK_CLOSE, NULL, NULL );
3119                                 return;
3120                         }
3121                 }
3122 #endif
3123                 if (prefs_common.addressbook_use_editaddress_dialog)
3124                         addressbook_new_address_from_folder_post_cb( person );
3125         }
3126         else if( pobj->type == ADDR_ITEM_GROUP ) {
3127                 /* New address in group */
3128                 ItemGroup *group = ADAPTER_GROUP(pobj)->itemGroup;
3129                 if( addressbook_edit_group( abf, NULL, group ) == NULL ) return;
3130                 if (addrbook.treeSelected == addrbook.opened) {
3131                         /* Change node name in tree. */
3132                         addressbook_change_node_name( addrbook.treeSelected, ADDRITEM_NAME(group) );
3133                         gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
3134                         addressbook_set_clist(
3135                                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
3136                                         addrbook.opened),
3137                                 TRUE);
3138                 }
3139         }
3140 }
3141
3142 /**
3143  * Search for specified child group node in address index tree.
3144  * \param parent Parent node.
3145  * \param group  Group to find.
3146  */
3147 static GtkCTreeNode *addressbook_find_group_node( GtkCTreeNode *parent, ItemGroup *group ) {
3148         GtkCTreeNode *node = NULL;
3149         GtkCTreeRow *currRow;
3150
3151         currRow = GTK_CTREE_ROW( parent );
3152         if( currRow ) {
3153                 node = currRow->children;
3154                 while( node ) {
3155                         AddressObject *obj;
3156
3157                         obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
3158                         if( obj->type == ADDR_ITEM_GROUP ) {
3159                                 ItemGroup *g = ADAPTER_GROUP(obj)->itemGroup;
3160                                 if( g == group ) return node;
3161                         }
3162                         currRow = GTK_CTREE_ROW(node);
3163                         node = currRow->sibling;
3164                 }
3165         }
3166         return NULL;
3167 }
3168
3169 static AddressBookFile *addressbook_get_book_file() {
3170         AddressBookFile *abf = NULL;
3171         AddressDataSource *ds = NULL;
3172
3173         ds = addressbook_find_datasource( addrbook.treeSelected );
3174         if( ds == NULL ) return NULL;
3175         if( ds->type == ADDR_IF_BOOK || ds->type == ADDR_IF_LDAP ) abf = ds->rawDataSource;
3176         return abf;
3177 }
3178
3179 static void addressbook_tree_remove_children( GtkCTree *ctree, GtkCTreeNode *parent ) {
3180         GtkCTreeNode *node;
3181         GtkCTreeRow *row;
3182
3183         /* Remove existing folders and groups */
3184         row = GTK_CTREE_ROW( parent );
3185         if( row ) {
3186                 while( (node = row->children) ) {
3187                         gtk_ctree_remove_node( ctree, node );
3188                 }
3189         }
3190 }
3191
3192 static void addressbook_move_nodes_up( GtkCTree *ctree, GtkCTreeNode *node ) {
3193         GtkCTreeNode *parent, *child;
3194         GtkCTreeRow *currRow;
3195         currRow = GTK_CTREE_ROW( node );
3196         if( currRow ) {
3197                 parent = currRow->parent;
3198                 while( (child = currRow->children) ) {
3199                         gtk_ctree_move( ctree, child, parent, node );
3200                 }
3201                 gtk_sctree_sort_node( ctree, parent );
3202         }
3203 }
3204
3205 static void addressbook_edit_address_post_cb( ItemPerson *person )
3206 {
3207         if( person ) {
3208 #ifdef USE_LDAP
3209                 if (strcmp2(person->nickName, ADDRITEM_NAME(person)))
3210                         addritem_person_set_nick_name( person, ADDRITEM_NAME(person));
3211 #endif
3212                 addressbook_folder_refresh_one_person( GTK_CTREE(addrbook.clist), person );
3213                 invalidate_address_completion();
3214         }
3215         addressbook_address_list_set_focus();
3216 }
3217
3218 void addressbook_address_list_set_focus( void )
3219 {
3220         if (!prefs_common.addressbook_use_editaddress_dialog) {
3221                 gtk_window_set_focus(GTK_WINDOW(addrbook.window), addrbook.clist);
3222                 addressbook_list_menu_setup();
3223         }
3224 }
3225
3226 void addressbook_address_list_disable_some_actions(void)
3227 {
3228         /* disable address copy/pasting when editing contact's detail (embedded form) */
3229         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",   FALSE );
3230         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",  FALSE );
3231         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste", FALSE );
3232 }
3233
3234 static void addressbook_edit_address_cb( gpointer data, guint action, GtkWidget *widget ) {
3235         addressbook_edit_address(data, action, widget, TRUE);
3236 }
3237         
3238 static void addressbook_edit_address( gpointer data, guint action, GtkWidget *widget,
3239                                                                           gboolean force_focus ) {
3240         GtkCTree *clist = GTK_CTREE(addrbook.clist);
3241         GtkCTree *ctree;
3242         AddressObject *obj = NULL, *pobj = NULL;
3243         AddressDataSource *ds = NULL;
3244         GtkCTreeNode *node = NULL, *parentNode = NULL;
3245         gchar *name = NULL;
3246         AddressBookFile *abf = NULL;
3247
3248         if( addrbook.listSelected == NULL ) return;
3249         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
3250         g_return_if_fail(obj != NULL);
3251
3252         ctree = GTK_CTREE( addrbook.ctree );
3253         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
3254         node = gtk_ctree_find_by_row_data( ctree, addrbook.treeSelected, obj );
3255
3256         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3257         if( ds == NULL ) return;
3258
3259         abf = addressbook_get_book_file();
3260         
3261         if( obj->type == ADDR_ITEM_EMAIL ) {
3262                 ItemEMail *email = ( ItemEMail * ) obj;
3263                 if( email == NULL ) return;
3264                 if( pobj && pobj->type == ADDR_ITEM_GROUP ) {
3265                         /* Edit parent group */
3266                         AdapterGroup *adapter = ADAPTER_GROUP(pobj);
3267                         ItemGroup *itemGrp = adapter->itemGroup;
3268                         if( abf == NULL ) return;
3269                         if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
3270                         name = ADDRITEM_NAME(itemGrp);
3271                         node = addrbook.treeSelected;
3272                         parentNode = GTK_CTREE_ROW(node)->parent;
3273                 }
3274                 else {
3275                         /* Edit person - email page */
3276                         ItemPerson *person;
3277                         person = ( ItemPerson * ) ADDRITEM_PARENT(email);
3278                         if  ( addressbook_edit_person( abf, NULL, person, TRUE, addrbook.editaddress_vbox,
3279                                                                                    addressbook_edit_address_post_cb,
3280                                                                                    (prefs_common.addressbook_use_editaddress_dialog||force_focus) )
3281                                   != NULL ) { 
3282 #ifdef USE_LDAP
3283                                 if (abf && abf->type == ADDR_IF_LDAP) {
3284                                         ldapsvr_set_modified( (LdapServer *) abf, TRUE );
3285                                         person->status = UPDATE_ENTRY;
3286                                 }
3287 #endif
3288                                 if (prefs_common.addressbook_use_editaddress_dialog)
3289                                         addressbook_edit_address_post_cb( person );
3290                         }
3291                         return;
3292                 }
3293         }
3294         else if( obj->type == ADDR_ITEM_PERSON ) {
3295                 /* Edit person - basic page */
3296                 ItemPerson *person = ( ItemPerson * ) obj;
3297                 if( addressbook_edit_person( abf, NULL, person, FALSE, addrbook.editaddress_vbox,
3298                                                                           addressbook_edit_address_post_cb,
3299                                                                           (prefs_common.addressbook_use_editaddress_dialog||force_focus) )
3300                         != NULL ) {
3301 #ifdef USE_LDAP
3302                                 if (abf && abf->type == ADDR_IF_LDAP) {
3303                                         ldapsvr_set_modified( (LdapServer *) abf, TRUE );
3304                                         person->status = UPDATE_ENTRY;
3305                                 }
3306 #endif
3307                                 if (prefs_common.addressbook_use_editaddress_dialog)
3308                                         addressbook_edit_address_post_cb( person );
3309                 }
3310                 return;
3311         }
3312         else if( obj->type == ADDR_ITEM_GROUP ) {
3313                 ItemGroup *itemGrp = ( ItemGroup * ) obj;
3314                 if( addressbook_edit_group( abf, NULL, itemGrp ) == NULL ) return;
3315                 parentNode = addrbook.treeSelected;
3316                 node = addressbook_find_group_node( parentNode, itemGrp );
3317                 name = ADDRITEM_NAME(itemGrp);
3318                 invalidate_address_completion();
3319         }
3320         else {
3321                 return;
3322         }
3323
3324         /* Update tree node with node name */
3325         if( node == NULL ) return;
3326         addressbook_change_node_name( node, name );
3327         gtk_sctree_sort_node( ctree, parentNode );
3328         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened ); 
3329         addressbook_set_clist(
3330                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
3331                         addrbook.opened),
3332                 TRUE);
3333 }
3334
3335 static void addressbook_delete_address_cb(gpointer data, guint action,
3336                                           GtkWidget *widget)
3337 {
3338         gtk_clist_freeze(GTK_CLIST(addrbook.clist));
3339         addressbook_del_clicked(NULL, NULL);
3340         gtk_clist_thaw(GTK_CLIST(addrbook.clist));
3341 }
3342
3343 static void close_cb(gpointer data, guint action, GtkWidget *widget)
3344 {
3345         addressbook_close();
3346 }
3347
3348 static void addressbook_file_save_cb( gpointer data, guint action, GtkWidget *widget ) {
3349         addressbook_export_to_file();
3350 }
3351
3352 static void addressbook_person_expand_node( GtkCTree *ctree, GList *node, gpointer *data ) {
3353         if( node ) {
3354                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
3355                 if( person ) addritem_person_set_opened( person, TRUE );
3356         }
3357 }
3358
3359 static void addressbook_person_collapse_node( GtkCTree *ctree, GList *node, gpointer *data ) {
3360         if( node ) {
3361                 ItemPerson *person = gtk_ctree_node_get_row_data( ctree, GTK_CTREE_NODE(node) );
3362                 if( person ) addritem_person_set_opened( person, FALSE );
3363         }
3364 }
3365
3366 static gchar *addressbook_format_item_clist( ItemPerson *person, ItemEMail *email ) {
3367         gchar *str = NULL;
3368         gchar *eMailAlias = ADDRITEM_NAME(email);
3369         if( eMailAlias && *eMailAlias != '\0' ) {
3370                 if( person ) {
3371                         str = g_strdup_printf( "%s - %s", ADDRITEM_NAME(person), eMailAlias );
3372                 }
3373                 else {
3374                         str = g_strdup( eMailAlias );
3375                 }
3376         }
3377         return str;
3378 }
3379
3380 static void addressbook_load_group( GtkCTree *clist, ItemGroup *itemGroup ) {
3381         GList *items = itemGroup->listEMail;
3382         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_EMAIL );
3383         for( ; items != NULL; items = g_list_next( items ) ) {
3384                 GtkCTreeNode *nodeEMail = NULL;
3385                 gchar *text[N_LIST_COLS];
3386                 ItemEMail *email = items->data;
3387                 ItemPerson *person;
3388                 gchar *str = NULL;
3389
3390                 if( ! email ) continue;
3391
3392                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
3393                 str = addressbook_format_item_clist( person, email );
3394                 if( str ) {
3395                         text[COL_NAME] = addressbook_set_col_name_guard(str);
3396                 }
3397                 else {
3398                         text[COL_NAME] = addressbook_set_col_name_guard(ADDRITEM_NAME(person));
3399                 }
3400                 text[COL_ADDRESS] = email->address;
3401                 text[COL_REMARKS] = email->remarks;
3402                 nodeEMail = gtk_sctree_insert_node(
3403                                 clist, NULL, NULL,
3404                                 text, FOLDER_SPACING,
3405                                 atci->iconXpm, atci->maskXpm,
3406                                 atci->iconXpmOpen, atci->maskXpmOpen,
3407                                 FALSE, FALSE );
3408                 gtk_ctree_node_set_row_data( clist, nodeEMail, email );
3409                 g_free( str );
3410                 str = NULL;
3411         }
3412 }
3413
3414 gchar *addressbook_set_col_name_guard(gchar *value)
3415 {
3416         gchar *ret = "<not set>";
3417         gchar *tmp = g_strdup(value);
3418         g_strstrip(tmp);
3419         if (tmp !=NULL && *tmp != '\0')
3420                 ret = value;
3421         g_free(tmp);
3422         return ret;
3423 }
3424
3425 static void addressbook_folder_load_one_person(
3426                 GtkCTree *clist, ItemPerson *person,
3427                 AddressTypeControlItem *atci,
3428                 AddressTypeControlItem *atciMail )
3429 {
3430         GtkCTreeNode *nodePerson = NULL;
3431         GtkCTreeNode *nodeEMail = NULL;
3432         gchar *text[N_LIST_COLS];
3433         gboolean flgFirst = TRUE, haveAddr = FALSE;
3434         GList *node;
3435 #ifdef USE_LDAP
3436         AddressBookFile *abf = addressbook_get_book_file();
3437 #endif
3438
3439         if( person == NULL ) return;
3440
3441         text[COL_NAME] = "";
3442         node = person->listEMail;
3443         while( node ) {
3444                 ItemEMail *email = node->data;
3445                 gchar *eMailAddr = NULL;
3446                 node = g_list_next( node );
3447
3448                 text[COL_ADDRESS] = email->address;
3449                 text[COL_REMARKS] = email->remarks;
3450                 eMailAddr = ADDRITEM_NAME(email);
3451                 if( eMailAddr && *eMailAddr == '\0' ) eMailAddr = NULL;
3452                 if( flgFirst ) {
3453                         /* First email belongs with person */
3454                         gchar *str = addressbook_format_item_clist( person, email );
3455                         if( str ) {
3456                                 text[COL_NAME] = addressbook_set_col_name_guard(str);
3457                         }
3458 #ifdef USE_LDAP
3459                         else if( abf && abf->type == ADDR_IF_LDAP && 
3460                                  person && person->nickName ) {
3461                                 if (person->nickName) {
3462                                         if (strcmp(person->nickName, "") != 0) {
3463                                                 text[COL_NAME] = addressbook_set_col_name_guard(person->nickName);
3464                                         }
3465                                         else {
3466                                                 text[COL_NAME] = addressbook_set_col_name_guard(ADDRITEM_NAME(person));
3467                                         }
3468                                 }
3469                         }
3470 #endif
3471                         else {
3472                                 text[COL_NAME] = addressbook_set_col_name_guard(ADDRITEM_NAME(person));
3473                         }
3474                         nodePerson = gtk_sctree_insert_node(
3475                                         clist, NULL, NULL,
3476                                         text, FOLDER_SPACING,
3477                                         atci->iconXpm, atci->maskXpm,
3478                                         atci->iconXpmOpen, atci->maskXpmOpen,
3479                                         FALSE, person->isOpened );
3480                         g_free( str );
3481                         str = NULL;
3482                         gtk_ctree_node_set_row_data(clist, nodePerson, person );
3483                 }
3484                 else {
3485                         /* Subsequent email is a child node of person */
3486                         text[COL_NAME] = ADDRITEM_NAME(email);
3487                         nodeEMail = gtk_sctree_insert_node(
3488                                         clist, nodePerson, NULL,
3489                                         text, FOLDER_SPACING,
3490                                         atciMail->iconXpm, atciMail->maskXpm,
3491                                         atciMail->iconXpmOpen, atciMail->maskXpmOpen,
3492                                         FALSE, TRUE );
3493                         gtk_ctree_node_set_row_data(clist, nodeEMail, email );
3494                 }
3495                 flgFirst = FALSE;
3496                 haveAddr = TRUE;
3497         }
3498         if( ! haveAddr ) {
3499                 /* Have name without EMail */
3500                 text[COL_NAME] = addressbook_set_col_name_guard(ADDRITEM_NAME(person));
3501                 text[COL_ADDRESS] = "";
3502                 text[COL_REMARKS] = "";
3503                 nodePerson = gtk_sctree_insert_node(
3504                                 clist, NULL, NULL,
3505                                 text, FOLDER_SPACING,
3506                                 atci->iconXpm, atci->maskXpm,
3507                                 atci->iconXpmOpen, atci->maskXpmOpen,
3508                                 FALSE, person->isOpened );
3509                 gtk_ctree_node_set_row_data(clist, nodePerson, person );
3510         }
3511         return;
3512 }
3513
3514 static void addressbook_folder_load_person( GtkCTree *clist, ItemFolder *itemFolder ) {
3515         GList *items;
3516         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
3517         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
3518
3519         if( atci == NULL ) return;
3520         if( atciMail == NULL ) return;
3521
3522         /* Load email addresses */
3523         items = addritem_folder_get_person_list( itemFolder );
3524         for( ; items != NULL; items = g_list_next( items ) ) {
3525                 addressbook_folder_load_one_person( clist, items->data, atci, atciMail );
3526         }
3527         /* Free up the list */
3528         mgu_clear_list( items );
3529         g_list_free( items );
3530 }
3531
3532 static void addressbook_folder_remove_node( GtkCTree *clist, GtkCTreeNode *node ) { 
3533         addrbook.listSelected = NULL;
3534         gtk_ctree_remove_node( clist, node );
3535         addressbook_menubar_set_sensitive( FALSE );
3536         addressbook_menuitem_set_sensitive(
3537                 gtk_ctree_node_get_row_data(
3538                         GTK_CTREE(clist), addrbook.treeSelected ),
3539                 addrbook.treeSelected );
3540 }
3541
3542 static void addressbook_folder_refresh_one_person( GtkCTree *clist, ItemPerson *person ) {
3543         AddressTypeControlItem *atci = addrbookctl_lookup( ADDR_ITEM_PERSON );
3544         AddressTypeControlItem *atciMail = addrbookctl_lookup( ADDR_ITEM_EMAIL );
3545         GtkCTreeNode *node;
3546         if( atci == NULL ) return;
3547         if( atciMail == NULL ) return;
3548         if( person == NULL ) return;
3549         /* unload the person */
3550         
3551         node = gtk_ctree_find_by_row_data( clist, NULL, person );
3552         if( node )
3553                 addressbook_folder_remove_node( clist, node );
3554         addressbook_folder_load_one_person( clist, person, atci, atciMail );
3555         gtk_sctree_sort_node( clist, NULL );
3556         node = gtk_ctree_find_by_row_data( clist, NULL, person );
3557         if( node ) {
3558                 gtk_sctree_select( GTK_SCTREE(clist), node );
3559                 if (!gtk_ctree_node_is_visible( clist, node ) ) 
3560                         gtk_ctree_node_moveto( clist, node, 0, 0, 0 );
3561         }
3562 }
3563
3564 static void addressbook_folder_remove_one_person( GtkCTree *clist, ItemPerson *person ) {
3565         GtkCTreeNode *node;
3566         gint row;
3567         
3568         if( person == NULL ) return;
3569         node = gtk_ctree_find_by_row_data( clist, NULL, person );
3570         row  = gtk_clist_find_row_from_data( GTK_CLIST(clist), person );
3571         if( node ) {
3572                 addressbook_folder_remove_node( clist, node );
3573         }
3574 }
3575
3576 static void addressbook_folder_load_group( GtkCTree *clist, ItemFolder *itemFolder ) {
3577         GList *items;
3578         AddressTypeControlItem *atci =  addrbookctl_lookup( ADDR_ITEM_GROUP );
3579
3580         /* Load any groups */
3581         if( ! atci ) return;
3582         items = addritem_folder_get_group_list( itemFolder );
3583         for( ; items != NULL; items = g_list_next( items ) ) {
3584                 GtkCTreeNode *nodeGroup = NULL;
3585                 gchar *text[N_LIST_COLS];
3586                 ItemGroup *group = items->data;
3587                 if( group == NULL ) continue;
3588                 text[COL_NAME] = ADDRITEM_NAME(group);
3589                 text[COL_ADDRESS] = "";
3590                 text[COL_REMARKS] = "";
3591                 nodeGroup = gtk_sctree_insert_node(clist, NULL, NULL,
3592                                       text, FOLDER_SPACING,
3593                                       atci->iconXpm, atci->maskXpm,
3594                                       atci->iconXpmOpen, atci->maskXpmOpen,
3595                                       FALSE, FALSE);
3596                 gtk_ctree_node_set_row_data(clist, nodeGroup, group );
3597                 gtk_sctree_sort_node(clist, NULL);
3598         }
3599         /* Free up the list */
3600         mgu_clear_list( items );
3601         g_list_free( items );
3602 }
3603
3604 /**
3605  * Search ctree widget callback function.
3606  * \param  pA Pointer to node.
3607  * \param  pB Pointer to data item being sought.
3608  * \return Zero (0) if group found.
3609  */
3610 static int addressbook_treenode_find_group_cb( gconstpointer pA, gconstpointer pB ) {
3611         AddressObject *aoA;
3612
3613         aoA = ( AddressObject * ) pA;
3614         if( aoA->type == ADDR_ITEM_GROUP ) {
3615                 ItemGroup *group, *grp;
3616
3617                 grp = ADAPTER_GROUP(aoA)->itemGroup;
3618                 group = ( ItemGroup * ) pB;
3619                 if( grp == group ) return 0;    /* Found group */
3620         }
3621         return 1;
3622 }
3623
3624 /*
3625 * Remove folder and group nodes from tree widget for items contained ("cut")
3626 * in clipboard.
3627 */
3628 static void addressbook_treenode_remove_item( void ) {
3629         GList *node;
3630         AddrSelectItem *cutItem;
3631         AddressCache *cache;
3632         AddrItemObject *aio;
3633         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
3634         GtkCTreeNode *tn;
3635
3636         node = _clipBoard_->objectList;
3637         while( node ) {
3638                 cutItem = node->data;
3639                 node = g_list_next( node );
3640                 cache = addrindex_get_cache(
3641                         _clipBoard_->addressIndex, cutItem->cacheID );
3642                 if( cache == NULL ) continue;
3643                 aio = addrcache_get_object( cache, cutItem->uid );
3644                 if( aio ) {
3645                         tn = NULL;
3646                         if( ADDRITEM_TYPE(aio) == ITEMTYPE_FOLDER ) {
3647                                 ItemFolder *folder;
3648
3649                                 folder = ( ItemFolder * ) aio;
3650                                 tn = gtk_ctree_find_by_row_data_custom(
3651                                         ctree, NULL, folder,
3652                                         addressbook_treenode_find_folder_cb );
3653                         }
3654                         else if( ADDRITEM_TYPE(aio) == ITEMTYPE_GROUP ) {
3655                                 ItemGroup *group;
3656
3657                                 group = ( ItemGroup * ) aio;
3658                                 tn = gtk_ctree_find_by_row_data_custom(
3659                                         ctree, NULL, group,
3660                                         addressbook_treenode_find_group_cb );
3661                         }
3662
3663                         if( tn ) {
3664                                 /* Free up adapter and remove node. */
3665                                 gtk_ctree_remove_node( ctree, tn );
3666                         }
3667                 }
3668         }
3669 }
3670
3671 /**
3672  * Find parent datasource for specified tree node.
3673  * \param  node Node to test.
3674  * \return Data source, or NULL if not found.
3675  */
3676 static AddressDataSource *addressbook_find_datasource( GtkCTreeNode *node ) {
3677         AddressDataSource *ds = NULL;
3678         AddressObject *ao;
3679
3680         g_return_val_if_fail(addrbook.ctree != NULL, NULL);
3681
3682         while( node ) {
3683                 if( GTK_CTREE_ROW(node)->level < 2 ) return NULL;
3684                 ao = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
3685                 if( ao ) {
3686                         /* g_print( "ao->type = %d\n", ao->type ); */
3687                         if( ao->type == ADDR_DATASOURCE ) {
3688                                 AdapterDSource *ads = ADAPTER_DSOURCE(ao);
3689                                 /* g_print( "found it\n" ); */
3690                                 ds = ads->dataSource;
3691                                 break;
3692                         }
3693                 }
3694                 node = GTK_CTREE_ROW(node)->parent;
3695         }
3696         return ds;
3697 }
3698
3699 /**
3700  * Load address list widget with children of specified object.
3701  * \param obj Parent object to be loaded.
3702  */
3703 static void addressbook_set_clist( AddressObject *obj, gboolean refresh ) {
3704         GtkCTree *ctreelist = GTK_CTREE(addrbook.clist);
3705         GtkCList *clist = GTK_CLIST(addrbook.clist);
3706         AddressDataSource *ds = NULL;
3707         AdapterDSource *ads = NULL;
3708         static AddressObject *last_obj = NULL;
3709
3710         if (addrbook.clist == NULL) {
3711                 return;
3712         }
3713         if (obj == last_obj && !refresh)
3714                 return;
3715
3716         last_obj = obj;
3717         if( obj == NULL ) {
3718                 gtk_clist_clear(clist);
3719                 return;
3720         }
3721
3722         if( obj->type == ADDR_INTERFACE ) {
3723                 /* g_print( "set_clist: loading datasource...\n" ); */
3724                 /* addressbook_node_load_datasource( GTK_CTREE(clist), obj ); */
3725                 return;
3726         }
3727
3728         gtk_clist_freeze(clist);
3729         gtk_clist_clear(clist);
3730
3731         if( obj->type == ADDR_DATASOURCE ) {
3732                 ads = ADAPTER_DSOURCE(obj);
3733                 ds = ADAPTER_DSOURCE(obj)->dataSource;
3734                 if( ds ) {
3735                         /* Load root folder */
3736                         ItemFolder *rootFolder = NULL;
3737                         rootFolder = addrindex_ds_get_root_folder( ds );
3738                         addressbook_folder_load_person(
3739                                 ctreelist, addrindex_ds_get_root_folder( ds ) );
3740                         addressbook_folder_load_group(
3741                                 ctreelist, addrindex_ds_get_root_folder( ds ) );
3742                 }
3743         }
3744         else {
3745                 if( obj->type == ADDR_ITEM_GROUP ) {
3746                         /* Load groups */
3747                         ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
3748                         addressbook_load_group( ctreelist, itemGroup );
3749                 }
3750                 else if( obj->type == ADDR_ITEM_FOLDER ) {
3751                         /* Load folders */
3752                         ItemFolder *itemFolder = ADAPTER_FOLDER(obj)->itemFolder;
3753                         addressbook_folder_load_person( ctreelist, itemFolder );
3754                         addressbook_folder_load_group( ctreelist, itemFolder );
3755                 }
3756         }
3757         gtk_sctree_sort_recursive(GTK_CTREE(clist), NULL);
3758         clist->focus_row = -1;