0470b16e4a48a651179e7b06abed23ce4e23cf32
[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 static gboolean find_person(AddrSelectItem *item_a, ItemPerson *person)
1352 {
1353         return ((ItemPerson *)item_a->addressItem == person)?0:-1;
1354 }
1355
1356 /*
1357 * Delete one or more objects from address list.
1358 */
1359 static void addressbook_del_clicked(GtkButton *button, gpointer data)
1360 {
1361         GtkCTree *clist = GTK_CTREE(addrbook.clist);
1362         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
1363         AddressObject *pobj;
1364         AdapterDSource *ads = NULL;
1365         GtkCTreeNode *nodeList;
1366         gboolean procFlag;
1367         AlertValue aval;
1368         AddressBookFile *abf = NULL;
1369         AddressDataSource *ds = NULL;
1370         AddressInterface *iface;
1371         AddrItemObject *aio;
1372         AddrSelectItem *item;
1373         GList *list, *node;
1374         gboolean refreshList = FALSE;
1375         
1376         pobj = gtk_ctree_node_get_row_data(ctree, addrbook.opened );
1377         g_return_if_fail(pobj != NULL);
1378
1379         /* Test whether anything selected for deletion */
1380         nodeList = addrbook.listSelected;
1381
1382         aio = gtk_ctree_node_get_row_data( clist, nodeList );
1383         if( aio == NULL) return;
1384         ds = addressbook_find_datasource( addrbook.treeSelected );
1385         if( ds == NULL ) return;
1386
1387         /* Test for read only */
1388         iface = ds->interface;
1389         if( iface->readOnly ) {
1390                 alertpanel( _("Delete address(es)"),
1391                         _("This address data is readonly and cannot be deleted."),
1392                         GTK_STOCK_CLOSE, NULL, NULL );
1393                 return;
1394         }
1395
1396         /* Test whether Ok to proceed */
1397         procFlag = FALSE;
1398         if( pobj->type == ADDR_DATASOURCE ) {
1399                 ads = ADAPTER_DSOURCE(pobj);
1400                 if( ads->subType == ADDR_BOOK ) procFlag = TRUE;
1401         }
1402         else if( pobj->type == ADDR_ITEM_FOLDER ) {
1403                 procFlag = TRUE;
1404         }
1405         else if( pobj->type == ADDR_ITEM_GROUP ) {
1406                 procFlag = TRUE;
1407         }
1408         if( ! procFlag ) return;
1409         abf = ds->rawDataSource;
1410         if( abf == NULL ) return;
1411
1412         gtk_clist_freeze(GTK_CLIST(addrbook.clist));
1413         g_signal_handlers_block_by_func
1414                 (G_OBJECT(addrbook.clist),
1415                  G_CALLBACK(addressbook_list_row_unselected), NULL);
1416
1417         /* Process deletions */
1418         if( pobj->type == ADDR_DATASOURCE || pobj->type == ADDR_ITEM_FOLDER ) {
1419                 GList *groups = NULL, *persons = NULL, *emails = NULL;
1420                 gboolean group_delete = TRUE;
1421                 /* Items inside folders */
1422                 list = addrselect_get_list( _addressSelect_ );
1423                 /* Confirm deletion */
1424                 node = list;
1425                 while( node ) {
1426                         item = node->data;
1427                         node = g_list_next( node );
1428                         aio = ( AddrItemObject * ) item->addressItem;
1429                         if( aio->type == ADDR_ITEM_PERSON || aio->type == ADDR_ITEM_EMAIL ) {
1430                                 group_delete = FALSE;
1431                                 break;
1432                         }
1433                 }
1434                 if (group_delete) {
1435                         aval = alertpanel( _("Delete group"),
1436                                         _("Really delete the group(s)?\n"
1437                                           "The addresses it contains will not be lost."),
1438                                         GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
1439                         if( aval != G_ALERTALTERNATE ) {
1440                                 goto thaw_ret;
1441                         }
1442                 } else {
1443                         aval = alertpanel( _("Delete address(es)"),
1444                                         _("Really delete the address(es)?"),
1445                                         GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
1446                         if( aval != G_ALERTALTERNATE ) {
1447                                 goto thaw_ret;
1448                         }
1449                 }
1450         
1451         /* first, set lists of groups and persons to remove */
1452                 node = list;
1453                 while( node ) {
1454                         item = node->data;
1455                         node = g_list_next( node );
1456                         aio = ( AddrItemObject * ) item->addressItem;
1457                         if (!aio)
1458                                 continue;
1459                         if( aio->type == ADDR_ITEM_GROUP ) {
1460                                 groups = g_list_prepend(groups, item);
1461                         }
1462                         else if( aio->type == ADDR_ITEM_PERSON ) {
1463                                 persons = g_list_prepend(persons, item);
1464                         }
1465                 }
1466         /* then set list of emails to remove *if* they're not children of
1467          * persons to remove */
1468                 node = list;
1469                 while( node ) {
1470                         item = node->data;
1471                         node = g_list_next( node );
1472                         aio = ( AddrItemObject * ) item->addressItem;
1473                         if (!aio)
1474                                 continue;
1475                         if( aio->type == ADDR_ITEM_EMAIL ) {
1476                                 ItemEMail *sitem = ( ItemEMail * ) aio;
1477                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(sitem);
1478                                 if (!g_list_find_custom(persons, person, (GCompareFunc)(find_person))) {
1479                                         emails = g_list_prepend(emails, item);
1480                                 }
1481                                 /* else, the email will be removed via the parent person */
1482                         }
1483                 }
1484         /* then delete groups */
1485                 node = groups;
1486                 while( node ) {
1487                         item = node->data;
1488                         node = g_list_next( node );
1489                         aio = ( AddrItemObject * ) item->addressItem;
1490                         if (!aio)
1491                                 continue;
1492                         if( aio->type == ADDR_ITEM_GROUP ) {
1493                                 ItemGroup *item = ( ItemGroup * ) aio;
1494                                 GtkCTreeNode *nd = NULL;
1495                                 nd = addressbook_find_group_node( addrbook.opened, item );
1496                                 item = addrbook_remove_group( abf, item );
1497                                 if( item ) {
1498                                         addritem_free_item_group( item );
1499                                 }
1500                                 /* Remove group from parent node */
1501                                 gtk_ctree_remove_node( ctree, nd );
1502                                 refreshList = TRUE;
1503                         }
1504                 }
1505         /* then delete persons */
1506                 node = persons;
1507                 while( node ) {
1508                         item = node->data;
1509                         node = g_list_next( node );
1510                         aio = ( AddrItemObject * ) item->addressItem;
1511                         if (!aio)
1512                                 continue;
1513                         if( aio->type == ADDR_ITEM_PERSON ) {
1514                                 ItemPerson *item = ( ItemPerson * ) aio;
1515                                 item->status = DELETE_ENTRY; 
1516                                 addressbook_folder_remove_one_person( clist, item );
1517                                 if (pobj->type == ADDR_ITEM_FOLDER)
1518                                         addritem_folder_remove_person(ADAPTER_FOLDER(pobj)->itemFolder, item);
1519                                 item = addrbook_remove_person( abf, item );
1520 #ifdef USE_LDAP
1521                                 if (ds && ds->type == ADDR_IF_LDAP) {
1522                                         LdapServer *server = ds->rawDataSource;
1523                                         ldapsvr_set_modified(server, TRUE);
1524                                         ldapsvr_update_book(server, item);
1525                                 }
1526 #endif
1527                                 if( item ) {
1528                                         gchar *filename = addritem_person_get_picture(item);
1529                                         if (filename && is_file_exist(filename))
1530                                                 claws_unlink(filename);
1531                                         g_free(filename);
1532                                         addritem_free_item_person( item );
1533                                 }
1534                         }
1535                 }
1536         /* then delete emails */
1537                 node = emails;
1538                 while( node ) {
1539                         item = node->data;
1540                         node = g_list_next( node );
1541                         aio = ( AddrItemObject * ) item->addressItem;
1542                         if (!aio)
1543                                 continue;
1544
1545                         if( aio->type == ADDR_ITEM_EMAIL ) {
1546                                 ItemEMail *sitem = ( ItemEMail * ) aio;
1547                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(sitem);
1548                                 sitem = addrbook_person_remove_email( abf, person, sitem );
1549                                 if( sitem ) {
1550                                         addrcache_remove_email(abf->addressCache, sitem);
1551                                         addritem_free_item_email( sitem );
1552                                 }
1553                                 addressbook_folder_refresh_one_person( clist, person );
1554                         }
1555                 }
1556                 g_list_free( groups );
1557                 g_list_free( persons );
1558                 g_list_free( emails );
1559                 g_list_free( list );
1560                 addressbook_list_select_clear();
1561                 if( refreshList ) {
1562                         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened);
1563                         addressbook_set_clist(
1564                                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
1565                                         addrbook.opened),
1566                                 TRUE);
1567                 }
1568                 addrbook_set_dirty(abf, TRUE);
1569                 addressbook_export_to_file();
1570                 addressbook_list_menu_setup();
1571                 goto thaw_ret;
1572         }
1573         else if( pobj->type == ADDR_ITEM_GROUP ) {
1574                 /* Items inside groups */
1575                 list = addrselect_get_list( _addressSelect_ );
1576                 node = list;
1577                 while( node ) {
1578                         item = node->data;
1579                         node = g_list_next( node );
1580                         aio = ( AddrItemObject * ) item->addressItem;
1581                         if( aio->type == ADDR_ITEM_EMAIL ) {
1582                                 ItemEMail *item = ( ItemEMail * ) aio;
1583                                 ItemPerson *person = ( ItemPerson * ) ADDRITEM_PARENT(item);
1584                                 item = addrbook_person_remove_email( abf, person, item );
1585                                 if( item ) {
1586                                         addritem_free_item_email( item );
1587                                 }
1588                         }
1589                 }
1590                 g_list_free( list );
1591                 addressbook_list_select_clear();
1592                 gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened);
1593                 addressbook_set_clist(
1594                         gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
1595                                 addrbook.opened),
1596                         TRUE);
1597                 
1598                 addrbook_set_dirty(abf, TRUE);
1599                 addressbook_export_to_file();
1600                 addressbook_list_menu_setup();
1601                 goto thaw_ret;
1602         }
1603
1604         gtk_ctree_node_set_row_data( clist, nodeList, NULL );
1605         gtk_ctree_remove_node( clist, nodeList );
1606 thaw_ret:
1607         gtk_clist_thaw(GTK_CLIST(addrbook.clist));
1608         g_signal_handlers_unblock_by_func
1609                 (G_OBJECT(addrbook.clist),
1610                  G_CALLBACK(addressbook_list_row_unselected), NULL);
1611 }
1612
1613 static void addressbook_reg_clicked(GtkButton *button, gpointer data)
1614 {
1615         addressbook_new_address_cb( NULL, 0, NULL );
1616 }
1617
1618 static gchar *addressbook_format_address( AddrItemObject * aio ) {
1619         gchar *buf = NULL;
1620         gchar *name = NULL;
1621         gchar *address = NULL;
1622
1623         if( aio->type == ADDR_ITEM_EMAIL ) {
1624                 ItemPerson *person = NULL;
1625                 ItemEMail *email = ( ItemEMail * ) aio;
1626
1627                 person = ( ItemPerson * ) ADDRITEM_PARENT(email);
1628                 if( email->address ) {
1629                         if( ADDRITEM_NAME(email) ) {
1630                                 name = ADDRITEM_NAME(email);
1631                                 if( *name == '\0' ) {
1632                                         name = ADDRITEM_NAME(person);
1633                                 }
1634                         }
1635                         else if( ADDRITEM_NAME(person) ) {
1636                                 name = ADDRITEM_NAME(person);
1637                         }
1638                         else {
1639                                 buf = g_strdup( email->address );
1640                         }
1641                         address = email->address;
1642                 }
1643         }
1644         else if( aio->type == ADDR_ITEM_PERSON ) {
1645                 ItemPerson *person = ( ItemPerson * ) aio;
1646                 GList *node = person->listEMail;
1647
1648                 name = ADDRITEM_NAME(person);
1649                 if( node ) {
1650                         ItemEMail *email = ( ItemEMail * ) node->data;
1651                         address = email->address;
1652                 }
1653         }
1654         if( address ) {
1655                 if( name && name[0] != '\0' ) {
1656                         if( strchr_with_skip_quote( name, '"', ',' ) )
1657                                 buf = g_strdup_printf( "\"%s\" <%s>", name, address );
1658                         else
1659                                 buf = g_strdup_printf( "%s <%s>", name, address );
1660                 }
1661                 else {
1662                         buf = g_strdup( address );
1663                 }
1664         }
1665
1666         return buf;
1667 }
1668
1669 static void addressbook_to_clicked(GtkButton *button, gpointer data)
1670 {
1671         GList *list, *node;
1672         Compose *compose;
1673         AddrSelectItem *item;
1674         AddrItemObject *aio;
1675         gchar *addr;
1676
1677         compose = addrbook.target_compose;
1678         if( ! compose ) return;
1679
1680         /* Nothing selected, but maybe there is something in text entry */
1681         addr = (char *)gtk_entry_get_text( GTK_ENTRY( addrbook.entry) );
1682         if ( addr ) {
1683                 compose_entry_append(
1684                         compose, addr, (ComposeEntryType)data );
1685         }
1686
1687         /* Select from address list */
1688         list = addrselect_get_list( _addressSelect_ );
1689         node = list;
1690         if (node) {
1691                 while( node ) {
1692                         item = node->data;
1693                         node = g_list_next( node );
1694                         aio = item->addressItem;
1695                         if( aio->type == ADDR_ITEM_PERSON ||
1696                             aio->type == ADDR_ITEM_EMAIL ) {
1697                                 addr = addressbook_format_address( aio );
1698                                 compose_entry_append(
1699                                         compose, addr, (ComposeEntryType) data );
1700                                 g_free( addr );
1701                         }
1702                         else if( aio->type == ADDR_ITEM_GROUP ) {
1703                                 ItemGroup *group = ( ItemGroup * ) aio;
1704                                 GList *nodeMail = group->listEMail;
1705                                 while( nodeMail ) {
1706                                         ItemEMail *email = nodeMail->data;
1707
1708                                         addr = addressbook_format_address(
1709                                                         ( AddrItemObject * ) email );
1710                                         compose_entry_append(
1711                                                 compose, addr, (ComposeEntryType) data );
1712                                         g_free( addr );
1713                                         nodeMail = g_list_next( nodeMail );
1714                                 }
1715                         }
1716                 }
1717         } else {
1718                 AddressObject *obj = NULL;
1719
1720                 obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1721         
1722                 if( obj && obj->type == ADDR_ITEM_GROUP ) {
1723                         ItemGroup *itemGroup = ADAPTER_GROUP(obj)->itemGroup;
1724                         GList *nodeMail = itemGroup->listEMail;
1725                         while( nodeMail ) {
1726                                 ItemEMail *email = nodeMail->data;
1727
1728                                 addr = addressbook_format_address(
1729                                                 ( AddrItemObject * ) email );
1730                                 compose_entry_append(
1731                                         compose, addr, (ComposeEntryType) data );
1732                                 g_free( addr );
1733                                 nodeMail = g_list_next( nodeMail );
1734                         }
1735                 }
1736         }
1737         g_list_free( list );
1738 }
1739
1740 static void addressbook_menubar_set_sensitive( gboolean sensitive ) {
1741         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",   sensitive );
1742         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book", sensitive );
1743         menu_set_sensitive( addrbook.menu_factory, "/Book/New Folder",  sensitive );
1744
1745         menu_set_sensitive( addrbook.menu_factory, "/Address/Select all",    TRUE );
1746         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",    sensitive );
1747         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",   sensitive );
1748         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",  sensitive );
1749
1750         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", sensitive );
1751         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   sensitive );
1752         menu_set_sensitive( addrbook.menu_factory, "/Address/Mail To",     sensitive );
1753         gtk_widget_set_sensitive( addrbook.reg_btn, sensitive );
1754         gtk_widget_set_sensitive( addrbook.edit_btn, sensitive );
1755         gtk_widget_set_sensitive( addrbook.del_btn, sensitive );
1756 }
1757
1758 static void addressbook_menuitem_set_sensitive( AddressObject *obj, GtkCTreeNode *node ) {
1759         gboolean canEdit = FALSE;
1760         gboolean canDelete = TRUE;
1761         gboolean canAdd = FALSE;
1762         gboolean canEditTr = TRUE;
1763         gboolean editAddress = FALSE;
1764         gboolean canExport = TRUE;
1765         AddressTypeControlItem *atci = NULL;
1766         AddressDataSource *ds = NULL;
1767         AddressInterface *iface = NULL;
1768
1769         if( obj == NULL ) return;
1770         if( obj->type == ADDR_INTERFACE ) {
1771                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
1772                 iface = adapter->interface;
1773                 if( iface ) {
1774                         if( iface->haveLibrary ) {
1775                                 /* Enable appropriate File / New command */
1776                                 atci = adapter->atci;
1777                                 menu_set_sensitive( addrbook.menu_factory, atci->menuCommand, TRUE );
1778                         }
1779                 }
1780                 canEditTr = canExport = FALSE;
1781         }
1782         else if( obj->type == ADDR_DATASOURCE ) {
1783                 AdapterDSource *ads = ADAPTER_DSOURCE(obj);
1784                 ds = ads->dataSource;
1785                 iface = ds->interface;
1786                 if( ! iface->readOnly ) {
1787                         canAdd = canEdit = editAddress = canDelete = TRUE;
1788                 }
1789                 if( ! iface->haveLibrary ) {
1790                         canAdd = canEdit = editAddress = canExport = canDelete = FALSE;
1791                 }
1792         }
1793         else if( obj->type == ADDR_ITEM_FOLDER ) {
1794                 ds = addressbook_find_datasource( addrbook.treeSelected );
1795                 if( ds ) {
1796                         iface = ds->interface;
1797                         if( iface->readOnly ) {
1798                                 canEditTr = FALSE;
1799                                 canDelete = FALSE;
1800                         }
1801                         else {
1802                                 canAdd = editAddress = TRUE;
1803                         }
1804                 }
1805         }
1806         else if( obj->type == ADDR_ITEM_GROUP ) {
1807                 ds = addressbook_find_datasource( addrbook.treeSelected );
1808                 if( ds ) {
1809                         iface = ds->interface;
1810                         if( ! iface->readOnly ) {
1811                                 editAddress = TRUE;
1812                         }
1813                 }
1814         }
1815
1816         if( addrbook.listSelected == NULL )
1817                 canEdit = FALSE;
1818
1819         /* Enable add */
1820         menu_set_sensitive( addrbook.menu_factory, "/Address/New Address", editAddress );
1821         menu_set_sensitive( addrbook.menu_factory, "/Address/New Group",   canAdd );
1822         menu_set_sensitive( addrbook.menu_factory, "/Book/New Folder",  canAdd );
1823         gtk_widget_set_sensitive( addrbook.reg_btn, editAddress );
1824
1825         /* Enable edit */
1826         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",   canEdit );
1827         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete", canDelete );
1828         gtk_widget_set_sensitive( addrbook.edit_btn, canEdit );
1829         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
1830
1831         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",      canEditTr );
1832         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book",    canEditTr );
1833
1834         /* Export data */
1835         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export HTML...", canExport );
1836         menu_set_sensitive( addrbook.menu_factory, "/Tools/Export LDIF...", canExport );
1837 }
1838
1839 /**
1840  * Address book tree callback function that responds to selection of tree
1841  * items.
1842  *
1843  * \param ctree  Tree widget.
1844  * \param node   Node that was selected.
1845  * \param column Column number where selected occurred.
1846  * \param data   Pointer to user data.
1847  */
1848 static void addressbook_tree_selected(GtkCTree *ctree, GtkCTreeNode *node,
1849                                       gint column, gpointer data)
1850 {
1851         AddressObject *obj = NULL;
1852         AdapterDSource *ads = NULL;
1853         AddressDataSource *ds = NULL;
1854         ItemFolder *rootFolder = NULL;
1855         AddressObjectType aot;
1856
1857         addrbook.treeSelected = node;
1858         addrbook.listSelected = NULL;
1859         addressbook_status_show( "" );
1860         if( addrbook.entry != NULL ) gtk_entry_set_text(GTK_ENTRY(addrbook.entry), "");
1861
1862         if( node ) obj = gtk_ctree_node_get_row_data( ctree, node );
1863         if( obj == NULL ) {
1864                 addressbook_set_clist(NULL, TRUE);
1865                 return;
1866         }
1867         addrbook.opened = node;
1868
1869         if( obj->type == ADDR_DATASOURCE ) {
1870                 /* Read from file */
1871                 static gboolean tVal = TRUE;
1872
1873                 ads = ADAPTER_DSOURCE(obj);
1874                 if( ads == NULL ) return;
1875                 ds = ads->dataSource;
1876                 if( ds == NULL ) return;                
1877
1878                 if( addrindex_ds_get_modify_flag( ds ) ) {
1879                         addrindex_ds_read_data( ds );
1880                 }
1881
1882                 if( ! addrindex_ds_get_read_flag( ds ) ) {
1883                         addrindex_ds_read_data( ds );
1884                 }
1885                 addressbook_ds_show_message( ds );
1886
1887                 if( ! addrindex_ds_get_access_flag( ds ) ) {
1888                         /* Remove existing folders and groups */
1889                         gtk_clist_freeze( GTK_CLIST(ctree) );
1890                         addressbook_tree_remove_children( ctree, node );
1891                         gtk_clist_thaw( GTK_CLIST(ctree) );
1892
1893                         /* Load folders into the tree */
1894                         rootFolder = addrindex_ds_get_root_folder( ds );
1895                         if( ds && ds->type == ADDR_IF_JPILOT ) {
1896                                 aot = ADDR_CATEGORY;
1897                         }
1898                         else if( ds && ds->type == ADDR_IF_LDAP ) {
1899                                 aot = ADDR_LDAP_QUERY;
1900                         }
1901                         else {
1902                                 aot = ADDR_ITEM_FOLDER;
1903                         }
1904                         addressbook_node_add_folder( node, ds, rootFolder, aot );
1905                         addrindex_ds_set_access_flag( ds, &tVal );
1906                         gtk_ctree_expand( ctree, node );
1907                 }
1908         } else {
1909                 addressbook_set_clist(NULL, TRUE);
1910         }
1911
1912         /* Update address list */
1913         g_signal_handlers_block_by_func
1914                 (G_OBJECT(ctree),
1915                  G_CALLBACK(addressbook_tree_selected), NULL);
1916         addressbook_set_clist( obj, FALSE );
1917         g_signal_handlers_unblock_by_func
1918                 (G_OBJECT(ctree),
1919                  G_CALLBACK(addressbook_tree_selected), NULL);
1920         if (!prefs_common.addressbook_use_editaddress_dialog)
1921                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
1922
1923         /* Setup main menu selections */
1924         addressbook_menubar_set_sensitive( FALSE );
1925         addressbook_menuitem_set_sensitive( obj, node );
1926         addressbook_list_select_clear();
1927         addressbook_list_menu_setup();
1928         return;
1929 }
1930
1931 /**
1932  * Setup address list popup menu items. Items are enabled or disabled as
1933  * required.
1934  */
1935 static void addressbook_list_menu_setup( void ) {
1936         GtkCTree *clist = NULL;
1937         AddressObject *pobj = NULL;
1938         AddressObject *obj = NULL;
1939         AdapterDSource *ads = NULL;
1940         AddressInterface *iface = NULL;
1941         AddressDataSource *ds = NULL;
1942         gboolean canEdit = FALSE;
1943         gboolean canDelete = FALSE;
1944         gboolean canCut = FALSE;
1945         gboolean canCopy = FALSE;
1946         gboolean canPaste = FALSE;
1947         gboolean canBrowse = FALSE;
1948
1949         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
1950         if( pobj == NULL ) return;
1951
1952         clist = GTK_CTREE(addrbook.clist);
1953         obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
1954         if( obj == NULL ) canEdit = FALSE;
1955
1956         menu_set_insensitive_all( GTK_MENU_SHELL(addrbook.list_popup) );
1957         menu_set_sensitive( addrbook.list_factory, "/Select all", TRUE );
1958
1959         if( pobj->type == ADDR_DATASOURCE ) {
1960                 /* Parent object is a data source */
1961                 ads = ADAPTER_DSOURCE(pobj);
1962                 ds = ads->dataSource;
1963                 if (!ds)
1964                         return;
1965                 iface = ds->interface;
1966                 if (!iface)
1967                         return;
1968                 if( ! iface->readOnly ) {
1969                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1970                         if (iface->type != ADDR_IF_LDAP)
1971                                 menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1972                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1973                         if( obj )
1974                                 canEdit = TRUE;
1975                         canDelete = canEdit;
1976                 }
1977         }
1978         else if( pobj->type != ADDR_INTERFACE ) {
1979                 /* Parent object is not an interface */
1980                 ds = addressbook_find_datasource( addrbook.treeSelected );
1981                 if (!ds)
1982                         return;
1983                 iface = ds->interface;
1984                 if (!iface)
1985                         return;
1986                 if( ! iface->readOnly ) {
1987                         /* Folder or group */
1988                         if( pobj->type == ADDR_ITEM_FOLDER || pobj->type == ADDR_ITEM_GROUP ) {
1989                                 menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
1990                                 gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
1991                                 if( obj ) canEdit = TRUE;
1992                         }
1993                         /* Folder */
1994                         if( pobj->type == ADDR_ITEM_FOLDER ) {
1995                                 if (iface->type != ADDR_IF_LDAP)
1996                                         menu_set_sensitive( addrbook.list_factory, "/New Group", TRUE );
1997                                 if( obj ) canEdit = TRUE;
1998                         }
1999                         canDelete = canEdit;
2000                 }
2001                 if( iface->type == ADDR_IF_LDAP ) {
2002                         if( obj ) canBrowse = TRUE;
2003                         canEdit = TRUE;
2004                         canDelete = TRUE;
2005                 }
2006         }
2007
2008         if( iface ) {
2009                 /* Enable cut and paste */
2010                 if( ! addrclip_is_empty( _clipBoard_ ) )
2011                         canPaste = TRUE;
2012                 if( ! addrselect_test_empty( _addressSelect_ ) )
2013                         canCut = TRUE;
2014                 /* Enable copy if something is selected */
2015                 if( ! addrselect_test_empty( _addressSelect_ ) )
2016                         canCopy = TRUE;
2017         }
2018
2019         /* Disable edit or browse if more than one row selected */
2020         if( GTK_CLIST(clist)->selection && GTK_CLIST(clist)->selection->next ) {
2021                 canEdit = FALSE;
2022                 canBrowse = FALSE;
2023         }
2024
2025         /* Forbid write changes when read-only */
2026         if( iface && iface->readOnly ) {
2027                 canCut = FALSE;
2028                 canDelete = FALSE;
2029                 canPaste = FALSE;
2030         }
2031
2032         /* Now go finalize menu items */
2033         menu_set_sensitive( addrbook.list_factory, "/Edit",   canEdit );
2034         menu_set_sensitive( addrbook.list_factory, "/Delete", canDelete );
2035
2036         menu_set_sensitive( addrbook.list_factory, "/Cut",           canCut );
2037         menu_set_sensitive( addrbook.list_factory, "/Copy",          canCopy );
2038         menu_set_sensitive( addrbook.list_factory, "/Paste",         canPaste );
2039
2040         menu_set_sensitive( addrbook.list_factory, "/Mail To",       canCopy );
2041
2042         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",           canCut );
2043         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",          canCopy );
2044         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",         canPaste );
2045
2046         menu_set_sensitive( addrbook.menu_factory, "/Address/Edit",    canEdit );
2047         menu_set_sensitive( addrbook.menu_factory, "/Address/Delete",  canDelete );
2048         menu_set_sensitive( addrbook.menu_factory, "/Address/Mail To", canCopy );
2049
2050         gtk_widget_set_sensitive( addrbook.edit_btn, canEdit );
2051         gtk_widget_set_sensitive( addrbook.del_btn, canDelete );
2052
2053 #ifdef USE_LDAP
2054         menu_set_sensitive( addrbook.list_factory, "/Browse Entry",    canBrowse );
2055 #endif
2056 }
2057
2058 static void addressbook_select_row_tree (GtkCTree       *ctree,
2059                                          GtkCTreeNode   *node,
2060                                          gint            column,
2061                                          gpointer        data)
2062 {
2063 }
2064
2065 /**
2066  * Add list of items into tree node below specified tree node.
2067  * \param treeNode  Tree node.
2068  * \param ds        Data source.
2069  * \param listItems List of items.
2070  */
2071 static void addressbook_treenode_add_list(
2072         GtkCTreeNode *treeNode, AddressDataSource *ds, GList *listItems )
2073 {
2074         GList *node;
2075
2076         node = listItems;
2077         while( node ) {
2078                 AddrItemObject *aio;
2079                 GtkCTreeNode *nn;
2080
2081                 aio = node->data;
2082                 if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_GROUP ) {
2083                         ItemGroup *group;
2084
2085                         group = ( ItemGroup * ) aio;
2086                         nn = addressbook_node_add_group( treeNode, ds, group );
2087                 }
2088                 else if( ADDRESS_OBJECT_TYPE(aio) == ITEMTYPE_FOLDER ) {
2089                         ItemFolder *folder;
2090
2091                         folder = ( ItemFolder * ) aio;
2092                         nn = addressbook_node_add_folder(
2093                                 treeNode, ds, folder, ADDR_ITEM_FOLDER );
2094                 }
2095                 node = g_list_next( node );
2096         }
2097 }
2098
2099 static void addressbook_select_all_cb( void ) {
2100         gtk_clist_select_all(GTK_CLIST(addrbook.clist));
2101 }
2102
2103 /**
2104  * Cut from address list widget.
2105  */
2106 static void addressbook_clip_cut_cb( void ) {
2107         _clipBoard_->cutFlag = TRUE;
2108         addrclip_clear( _clipBoard_ );
2109         addrclip_add( _clipBoard_, _addressSelect_ );
2110         /* addrclip_list_show( _clipBoard_, stdout ); */
2111 }
2112
2113 /**
2114  * Copy from address list widget.
2115  */
2116 static void addressbook_clip_copy_cb( void ) {
2117         _clipBoard_->cutFlag = FALSE;
2118         addrclip_clear( _clipBoard_ );
2119         addrclip_add( _clipBoard_, _addressSelect_ );
2120         /* addrclip_list_show( _clipBoard_, stdout ); */
2121 }
2122
2123 /**
2124  * Paste clipboard into address list widget.
2125  */
2126 static void addressbook_clip_paste_cb( void ) {
2127         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2128         AddressObject *pobj = NULL;
2129         AddressDataSource *ds = NULL;
2130         AddressBookFile *abf = NULL;
2131         ItemFolder *folder = NULL;
2132         GList *folderGroup = NULL;
2133
2134         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
2135         if( ds == NULL ) return;
2136         if( addrindex_ds_get_readonly( ds ) ) {
2137                 alertpanel_error( _("Cannot paste. Target address book is readonly.") );
2138                 return;
2139         }
2140
2141         pobj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2142         if( pobj ) {
2143                 if( pobj->type == ADDR_ITEM_FOLDER ) {
2144                         folder = ADAPTER_FOLDER(pobj)->itemFolder;
2145                 }
2146                 else if( pobj->type == ADDR_ITEM_GROUP ) {
2147                         alertpanel_error( _("Cannot paste into an address group.") );
2148                         return;
2149                 }
2150         }
2151
2152         /* Get an address book */
2153         abf = addressbook_get_book_file();
2154         if( abf == NULL ) return;
2155
2156         if( _clipBoard_->cutFlag ) {
2157                 /* Paste/Cut */
2158                 folderGroup = addrclip_paste_cut( _clipBoard_, abf, folder );
2159
2160                 /* Remove all groups and folders in clipboard from tree node */
2161                 addressbook_treenode_remove_item();
2162
2163                 /* Remove all "cut" items */
2164                 addrclip_delete_item( _clipBoard_ );
2165
2166                 /* Clear clipboard - cut items??? */
2167                 addrclip_clear( _clipBoard_ );
2168         }
2169         else {
2170                 /* Paste/Copy */
2171                 folderGroup = addrclip_paste_copy( _clipBoard_, abf, folder );
2172         }
2173
2174         /* addrclip_list_show( _clipBoard_, stdout ); */
2175         if( folderGroup ) {
2176                 /* Update tree by inserting node for each folder or group */
2177                 addressbook_treenode_add_list(
2178                         addrbook.treeSelected, ds, folderGroup );
2179                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2180                 g_list_free( folderGroup );
2181                 folderGroup = NULL;
2182         }
2183
2184         /* Display items pasted */
2185         gtk_sctree_select( GTK_SCTREE(ctree), addrbook.opened );
2186         addressbook_set_clist(
2187                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
2188                         addrbook.opened),
2189                 TRUE);
2190         
2191
2192 }
2193
2194 /**
2195  * Add current treenode object to clipboard. Note that widget only allows
2196  * one entry from the tree list to be selected.
2197  */
2198 static void addressbook_treenode_to_clipboard( void ) {
2199         AddressObject *obj = NULL;
2200         AddressDataSource *ds = NULL;
2201         AddrSelectItem *item;
2202         GtkCTree *ctree = GTK_CTREE( addrbook.ctree );
2203         GtkCTreeNode *node;
2204
2205         node = addrbook.treeSelected;
2206         if( node == NULL ) return;
2207         obj = gtk_ctree_node_get_row_data( ctree, node );
2208         if( obj == NULL ) return;
2209
2210         ds = addressbook_find_datasource( node );
2211         if( ds == NULL ) return;
2212
2213         item = NULL;
2214         if( obj->type == ADDR_ITEM_FOLDER ) {
2215                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2216                 ItemFolder *folder = adapter->itemFolder;
2217
2218                 item = addrselect_create_node( obj );
2219                 item->uid = g_strdup( ADDRITEM_ID(folder) );
2220         }
2221         else if( obj->type == ADDR_ITEM_GROUP ) {
2222                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2223                 ItemGroup *group = adapter->itemGroup;
2224
2225                 item = addrselect_create_node( obj );
2226                 item->uid = g_strdup( ADDRITEM_ID(group) );
2227         }
2228         else if( obj->type == ADDR_DATASOURCE ) {
2229                 /* Data source */
2230                 item = addrselect_create_node( obj );
2231                 item->uid = NULL;
2232         }
2233
2234         if( item ) {
2235                 /* Clear existing list and add item into list */
2236                 gchar *cacheID;
2237
2238                 addressbook_list_select_clear();
2239                 cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2240                 addrselect_list_add( _addressSelect_, item, cacheID );
2241                 g_free( cacheID );
2242         }
2243 }
2244
2245 /**
2246  * Cut from tree widget.
2247  */
2248 static void addressbook_treenode_cut_cb( void ) {
2249         _clipBoard_->cutFlag = TRUE;
2250         addressbook_treenode_to_clipboard();
2251         addrclip_clear( _clipBoard_ );
2252         addrclip_add( _clipBoard_, _addressSelect_ );
2253         /* addrclip_list_show( _clipBoard_, stdout ); */
2254 }
2255
2256 /**
2257  * Copy from tree widget.
2258  */
2259 static void addressbook_treenode_copy_cb( void ) {
2260         _clipBoard_->cutFlag = FALSE;
2261         addressbook_treenode_to_clipboard();
2262         addrclip_clear( _clipBoard_ );
2263         addrclip_add( _clipBoard_, _addressSelect_ );
2264         /* addrclip_list_show( _clipBoard_, stdout ); */
2265 }
2266
2267 /**
2268  * Paste clipboard into address tree widget.
2269  */
2270 static void addressbook_treenode_paste_cb( void ) {
2271         addressbook_clip_paste_cb();
2272 }
2273
2274 /**
2275  * Clear selected entries in clipboard.
2276  */
2277 static void addressbook_list_select_clear( void ) {
2278         addrselect_list_clear( _addressSelect_ );
2279 }
2280
2281 /**
2282  * Add specified address item to selected address list.
2283  * \param aio Address item object.
2284  * \param ds  Datasource.
2285  */
2286 static void addressbook_list_select_add( AddrItemObject *aio, AddressDataSource *ds ) {
2287         gchar *cacheID;
2288
2289         if( ds == NULL ) return;
2290         cacheID = addrindex_get_cache_id( _addressIndex_, ds );
2291         addrselect_list_add_obj( _addressSelect_, aio, cacheID );
2292         g_free( cacheID );
2293 }
2294
2295 /**
2296  * Remove specified address item from selected address list.
2297  * \param aio Address item object.
2298  */
2299 static void addressbook_list_select_remove( AddrItemObject *aio ) {
2300         addrselect_list_remove( _addressSelect_, aio );
2301 }
2302
2303 /**
2304  * Invoke EMail compose window with addresses in selected address list.
2305  */
2306 static void addressbook_mail_to_cb( void ) {
2307         GList *listAddress;
2308
2309         if( ! addrselect_test_empty( _addressSelect_ ) ) {
2310                 listAddress = addrselect_build_list( _addressSelect_ );
2311                 compose_new_with_list( NULL, listAddress );
2312                 mgu_free_dlist( listAddress );
2313                 listAddress = NULL;
2314         }
2315 }
2316
2317 static void addressbook_list_row_selected( GtkCTree *clist,
2318                                            GtkCTreeNode *node,
2319                                            gint column,
2320                                            gpointer data )
2321 {
2322         GtkEntry *entry = GTK_ENTRY(addrbook.entry);
2323         AddrItemObject *aio = NULL;
2324         AddressObject *pobj = NULL;
2325         AdapterDSource *ads = NULL;
2326         AddressDataSource *ds = NULL;
2327
2328         gtk_entry_set_text( entry, "" );
2329         addrbook.listSelected = node;
2330
2331         pobj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), addrbook.treeSelected );
2332         if( pobj == NULL ) return;
2333
2334         if( pobj->type == ADDR_DATASOURCE ) {
2335                 ads = ADAPTER_DSOURCE(pobj);
2336                 ds = ads->dataSource;
2337         }
2338         else if( pobj->type != ADDR_INTERFACE ) {
2339                 ds = addressbook_find_datasource( addrbook.treeSelected );
2340         }
2341
2342         aio = gtk_ctree_node_get_row_data( clist, node );
2343         if( aio ) {
2344                 /* g_print( "list select: %d : '%s'\n", aio->type, aio->name ); */
2345                 addressbook_list_select_add( aio, ds );
2346         }
2347
2348         addressbook_list_menu_setup();
2349
2350         if (!addrbook.target_compose && !prefs_common.addressbook_use_editaddress_dialog) {
2351                 AddressObject *obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
2352
2353                 if (obj && obj->type != ADDR_ITEM_GROUP)
2354                         addressbook_edit_address(NULL, 0, NULL, FALSE);
2355         }
2356 }
2357
2358 static void addressbook_list_row_unselected( GtkCTree *ctree,
2359                                              GtkCTreeNode *node,
2360                                              gint column,
2361                                              gpointer data )
2362 {
2363         AddrItemObject *aio;
2364
2365         aio = gtk_ctree_node_get_row_data( ctree, node );
2366         if( aio != NULL ) {
2367                 /* g_print( "list unselect: %d : '%s'\n", aio->type, aio->name ); */
2368                 addressbook_list_select_remove( aio );
2369         }
2370
2371         if (!prefs_common.addressbook_use_editaddress_dialog)
2372                 addressbook_edit_person_invalidate(NULL, NULL, NULL);
2373 }
2374
2375 /* from gdkevents.c */
2376 #define DOUBLE_CLICK_TIME 250
2377
2378 static gboolean addressbook_list_button_pressed(GtkWidget *widget,
2379                                                 GdkEventButton *event,
2380                                                 gpointer data)
2381 {
2382         static guint32 lasttime = 0;
2383         if( ! event ) return FALSE;
2384
2385         addressbook_list_menu_setup();
2386
2387         if( event->button == 3 ) {
2388                 gtk_menu_popup( GTK_MENU(addrbook.list_popup), NULL, NULL, NULL, NULL,
2389                        event->button, event->time );
2390         } else if (event->button == 1) {
2391                 if (event->time - lasttime < DOUBLE_CLICK_TIME) {
2392                         if (prefs_common.add_address_by_click &&
2393                             addrbook.target_compose)
2394                                 addressbook_to_clicked(NULL, GINT_TO_POINTER(COMPOSE_TO));
2395                         else
2396                                 if (prefs_common.addressbook_use_editaddress_dialog)
2397                                         addressbook_edit_address_cb(NULL, 0, NULL);
2398                                 else {
2399                                         GtkCTree *clist = GTK_CTREE(addrbook.clist);
2400                                         AddressObject *obj = gtk_ctree_node_get_row_data( clist, addrbook.listSelected );
2401                                         if( obj && obj->type == ADDR_ITEM_GROUP )
2402                                                 addressbook_edit_address_cb(NULL, 0, NULL);
2403                                 }
2404
2405                         lasttime = 0;
2406                 } else
2407                         lasttime = event->time;
2408         }
2409
2410         return FALSE;
2411 }
2412
2413 static gboolean addressbook_list_button_released(GtkWidget *widget,
2414                                                  GdkEventButton *event,
2415                                                  gpointer data)
2416 {
2417         return FALSE;
2418 }
2419
2420 static gboolean addressbook_tree_button_pressed(GtkWidget *ctree,
2421                                                 GdkEventButton *event,
2422                                                 gpointer data)
2423 {
2424         GtkCList *clist = GTK_CLIST(ctree);
2425         gint row, column;
2426         AddressObject *obj = NULL;
2427         AdapterDSource *ads = NULL;
2428         AddressInterface *iface = NULL;
2429         AddressDataSource *ds = NULL;
2430         gboolean canEdit = FALSE;
2431         gboolean canDelete = FALSE;
2432         gboolean canCut = FALSE;
2433         gboolean canCopy = FALSE;
2434         gboolean canPaste = FALSE;
2435         gboolean canTreeCut = FALSE;
2436         gboolean canTreeCopy = FALSE;
2437         gboolean canTreePaste = FALSE;
2438         gboolean canLookup = FALSE;
2439         GtkCTreeNode *node = NULL;
2440
2441         if( ! event ) return FALSE;
2442         addressbook_menubar_set_sensitive( FALSE );
2443
2444         if( gtk_clist_get_selection_info( clist, event->x, event->y, &row, &column ) ) {
2445                 gtkut_clist_set_focus_row(clist, row);
2446                 obj = gtk_clist_get_row_data( clist, row );
2447         }
2448
2449         menu_set_insensitive_all(GTK_MENU_SHELL(addrbook.tree_popup));
2450
2451         if( obj == NULL ) return FALSE;
2452         node = gtk_ctree_node_nth(GTK_CTREE(clist), row);
2453
2454         if( ! addrclip_is_empty( _clipBoard_ ) ) {
2455                 canTreePaste = TRUE;
2456         }
2457         if (obj->type == ADDR_INTERFACE) {
2458                 AdapterInterface *adapter = ADAPTER_INTERFACE(obj);
2459                 if( !adapter )
2460                         goto just_set_sens;
2461                 iface = adapter->interface;
2462                 if( !iface )
2463                         goto just_set_sens;
2464                 if( !iface->readOnly ) {
2465                         menu_set_sensitive( addrbook.tree_factory, "/New Book", TRUE );
2466                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2467                 }
2468                 if( iface->externalQuery ) canLookup = TRUE;
2469         }
2470         if (obj->type == ADDR_DATASOURCE) {
2471                 ads = ADAPTER_DSOURCE(obj);
2472                 ds = ads->dataSource;
2473                 if( !ds )
2474                         goto just_set_sens;
2475                 iface = ds->interface;
2476                 if( !iface )
2477                         goto just_set_sens;
2478                 if( !iface->readOnly ) {
2479                         canDelete = TRUE;
2480                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2481                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2482                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2483                 }
2484                 canEdit = TRUE;
2485                 canTreeCopy = TRUE;
2486                 if( iface->externalQuery ) canLookup = TRUE;
2487         }
2488         else if (obj->type == ADDR_ITEM_FOLDER) {
2489                 ds = addressbook_find_datasource( node );
2490                 if( !ds )
2491                         goto just_set_sens;
2492                 iface = ds->interface;
2493                 if( !iface )
2494                         goto just_set_sens;
2495                 if( !iface->readOnly ) {
2496                         canEdit = TRUE;
2497                         canDelete = TRUE;
2498                         canTreeCut = TRUE;
2499                         menu_set_sensitive( addrbook.tree_factory, "/New Folder", TRUE );
2500                         menu_set_sensitive( addrbook.tree_factory, "/New Group", TRUE );
2501                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2502                 }
2503                 canTreeCopy = TRUE;
2504
2505                 if( iface->externalQuery ) {
2506                         /* Enable deletion of LDAP folder */
2507                         canLookup = TRUE;
2508                         canDelete = TRUE;
2509                 }
2510         }
2511         else if (obj->type == ADDR_ITEM_GROUP) {
2512                 ds = addressbook_find_datasource( node );
2513                 if( !ds )
2514                         goto just_set_sens;
2515                 iface = ds->interface;
2516                 if( !iface )
2517                         goto just_set_sens;
2518                 if( ! iface->readOnly ) {
2519                         canEdit = TRUE;
2520                         canDelete = TRUE;
2521                         menu_set_sensitive( addrbook.list_factory, "/New Address", TRUE );
2522                         gtk_widget_set_sensitive( addrbook.reg_btn, TRUE );
2523                 }
2524         }
2525
2526         if( canEdit ) {
2527                 if( ! addrselect_test_empty( _addressSelect_ ) )
2528                         canCut = TRUE;
2529         }
2530         if( ! addrselect_test_empty( _addressSelect_ ) )
2531                 canCopy = TRUE;
2532         if( ! addrclip_is_empty( _clipBoard_ ) )
2533                 canPaste = TRUE;
2534
2535         /* Forbid write changes when read-only */
2536         if( iface && iface->readOnly ) {
2537                 canTreeCut = FALSE;
2538                 canTreePaste = FALSE;
2539                 canCut = FALSE;
2540                 canDelete = FALSE;
2541                 canPaste = FALSE;
2542         }
2543
2544 just_set_sens:
2545         /* Enable edit */
2546         menu_set_sensitive( addrbook.tree_factory, "/Edit",   canEdit );
2547         menu_set_sensitive( addrbook.tree_factory, "/Delete", canDelete );
2548         menu_set_sensitive( addrbook.tree_factory, "/Cut",    canTreeCut );
2549         menu_set_sensitive( addrbook.tree_factory, "/Copy",   canTreeCopy );
2550         menu_set_sensitive( addrbook.tree_factory, "/Paste",  canTreePaste );
2551
2552         menu_set_sensitive( addrbook.menu_factory, "/Book/Edit book",          canEdit );
2553         menu_set_sensitive( addrbook.menu_factory, "/Book/Delete book",        canEdit );
2554         menu_set_sensitive( addrbook.menu_factory, "/Address/Cut",           canCut );
2555         menu_set_sensitive( addrbook.menu_factory, "/Address/Copy",          canCopy );
2556         menu_set_sensitive( addrbook.menu_factory, "/Address/Paste",         canPaste );
2557
2558         addressbook_show_buttons(addrbook.target_compose == NULL, canLookup,
2559                         addrbook.target_compose != NULL);
2560
2561         if( event->button == 3 ) {
2562                 gtk_menu_popup(GTK_MENU(addrbook.tree_popup), NULL, NULL, NULL, NULL,
2563                                event->button, event->time);
2564         }
2565
2566         return FALSE;
2567 }
2568
2569 static gboolean addressbook_tree_button_released(GtkWidget *ctree,
2570                                                  GdkEventButton *event,
2571                                                  gpointer data)
2572 {
2573         gtkut_ctree_set_focus_row(GTK_CTREE(addrbook.ctree), addrbook.opened);
2574         return FALSE;
2575 }
2576
2577 static void addressbook_new_folder_cb(gpointer data, guint action,
2578                                       GtkWidget *widget)
2579 {
2580         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2581         AddressObject *obj = NULL;
2582         AddressDataSource *ds = NULL;
2583         AddressBookFile *abf = NULL;
2584         ItemFolder *parentFolder = NULL;
2585         ItemFolder *folder = NULL;
2586
2587         if( ! addrbook.treeSelected ) return;
2588         obj = gtk_ctree_node_get_row_data( ctree, addrbook.treeSelected );
2589         if( obj == NULL ) return;
2590         ds = addressbook_find_datasource( addrbook.treeSelected );
2591         if( ds == NULL ) return;
2592
2593         if( obj->type == ADDR_DATASOURCE ) {
2594                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2595         }
2596         else if( obj->type == ADDR_ITEM_FOLDER ) {
2597                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2598         }
2599         else {
2600                 return;
2601         }
2602
2603         abf = ds->rawDataSource;
2604         if( abf == NULL ) return;
2605         folder = addressbook_edit_folder( abf, parentFolder, NULL );
2606         if( folder ) {
2607                 GtkCTreeNode *nn;
2608                 nn = addressbook_node_add_folder(
2609                         addrbook.treeSelected, ds, folder, ADDR_ITEM_FOLDER );
2610                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2611                 if( addrbook.treeSelected == addrbook.opened )
2612                         addressbook_set_clist(obj, TRUE);
2613         }
2614
2615 }
2616
2617 static void addressbook_new_group_cb(gpointer data, guint action,
2618                                      GtkWidget *widget)
2619 {
2620         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2621         AddressObject *obj = NULL;
2622         AddressDataSource *ds = NULL;
2623         AddressBookFile *abf = NULL;
2624         ItemFolder *parentFolder = NULL;
2625         ItemGroup *group = NULL;
2626
2627         if( ! addrbook.treeSelected ) return;
2628         obj = gtk_ctree_node_get_row_data(ctree, addrbook.treeSelected);
2629         if( obj == NULL ) return;
2630         ds = addressbook_find_datasource( addrbook.treeSelected );
2631         if( ds == NULL ) return;
2632
2633         if( obj->type == ADDR_DATASOURCE ) {
2634                 if( ADAPTER_DSOURCE(obj)->subType != ADDR_BOOK ) return;
2635         }
2636         else if( obj->type == ADDR_ITEM_FOLDER ) {
2637                 parentFolder = ADAPTER_FOLDER(obj)->itemFolder;
2638         }
2639         else {
2640                 return;
2641         }
2642
2643         abf = ds->rawDataSource;
2644         if( abf == NULL ) return;
2645         group = addressbook_edit_group( abf, parentFolder, NULL );
2646         if( group ) {
2647                 GtkCTreeNode *nn;
2648                 nn = addressbook_node_add_group( addrbook.treeSelected, ds, group );
2649                 gtk_ctree_expand( ctree, addrbook.treeSelected );
2650                 if( addrbook.treeSelected == addrbook.opened )
2651                         addressbook_set_clist(obj, TRUE);
2652         }
2653
2654 }
2655
2656 static void addressbook_change_node_name(GtkCTreeNode *node, const gchar *name)
2657 {
2658         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2659         gchar *text[1];
2660         guint8 spacing;
2661         GdkPixmap *pix_cl, *pix_op;
2662         GdkBitmap *mask_cl, *mask_op;
2663         gboolean is_leaf, expanded;
2664
2665         gtk_ctree_get_node_info(ctree, node, text, &spacing,
2666                                 &pix_cl, &mask_cl, &pix_op, &mask_op,
2667                                 &is_leaf, &expanded);
2668         gtk_sctree_set_node_info(ctree, node, name, spacing,
2669                                 pix_cl, mask_cl, pix_op, mask_op,
2670                                 is_leaf, expanded);
2671 }
2672
2673 /**
2674  * Edit data source.
2675  * \param obj  Address object to edit.
2676  * \param node Node in tree.
2677  * \return New name of data source.
2678  */
2679 static gchar *addressbook_edit_datasource( AddressObject *obj, GtkCTreeNode *node ) {
2680         gchar *newName = NULL;
2681         AddressDataSource *ds = NULL;
2682         AddressInterface *iface = NULL;
2683         AdapterDSource *ads = NULL;
2684
2685         ds = addressbook_find_datasource( node );
2686         if( ds == NULL ) return NULL;
2687         iface = ds->interface;
2688         if( ! iface->haveLibrary ) return NULL;
2689
2690         /* Read data from data source */
2691         if( addrindex_ds_get_modify_flag( ds ) ) {
2692                 addrindex_ds_read_data( ds );
2693         }
2694
2695         if( ! addrindex_ds_get_read_flag( ds ) ) {
2696                 addrindex_ds_read_data( ds );
2697         }
2698
2699         /* Handle edit */
2700         ads = ADAPTER_DSOURCE(obj);
2701         if( ads->subType == ADDR_BOOK ) {
2702                 if( addressbook_edit_book( _addressIndex_, ads ) == NULL ) return NULL;
2703         }
2704         else if( ads->subType == ADDR_VCARD ) {
2705                 if( addressbook_edit_vcard( _addressIndex_, ads ) == NULL ) return NULL;
2706         }
2707 #ifdef USE_JPILOT
2708         else if( ads->subType == ADDR_JPILOT ) {
2709                 if( addressbook_edit_jpilot( _addressIndex_, ads ) == NULL ) return NULL;
2710         }
2711 #endif
2712 #ifdef USE_LDAP
2713         else if( ads->subType == ADDR_LDAP ) {
2714                 if( addressbook_edit_ldap( _addressIndex_, ads ) == NULL ) return NULL;
2715         }
2716 #endif
2717         else {
2718                 return NULL;
2719         }
2720         newName = obj->name;
2721         return newName;
2722 }
2723
2724 /*
2725 * Edit an object that is in the address tree area.
2726 */
2727 static void addressbook_treenode_edit_cb(gpointer data, guint action,
2728                                        GtkWidget *widget)
2729 {
2730         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2731         AddressObject *obj;
2732         AddressDataSource *ds = NULL;
2733         AddressBookFile *abf = NULL;
2734         GtkCTreeNode *node = NULL, *parentNode = NULL;
2735         gchar *name = NULL;
2736
2737         if( ! addrbook.treeSelected ) return;
2738         node = addrbook.treeSelected;
2739         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2740         obj = gtk_ctree_node_get_row_data( ctree, node );
2741         if( obj == NULL ) return;
2742         parentNode = GTK_CTREE_ROW(node)->parent;
2743
2744         ds = addressbook_find_datasource( node );
2745         if( ds == NULL ) return;
2746
2747         if( obj->type == ADDR_DATASOURCE ) {
2748                 name = addressbook_edit_datasource( obj, node );
2749                 if( name == NULL ) return;
2750         }
2751         else {
2752                 abf = ds->rawDataSource;
2753                 if( abf == NULL ) return;
2754                 if( obj->type == ADDR_ITEM_FOLDER ) {
2755                         AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2756                         ItemFolder *item = adapter->itemFolder;
2757                         ItemFolder *parentFolder = NULL;
2758                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2759                         if( addressbook_edit_folder( abf, parentFolder, item ) == NULL ) return;
2760                         name = ADDRITEM_NAME(item);
2761                 }
2762                 else if( obj->type == ADDR_ITEM_GROUP ) {
2763                         AdapterGroup *adapter = ADAPTER_GROUP(obj);
2764                         ItemGroup *item = adapter->itemGroup;
2765                         ItemFolder *parentFolder = NULL;
2766                         parentFolder = ( ItemFolder * ) ADDRITEM_PARENT(item);
2767                         if( addressbook_edit_group( abf, parentFolder, item ) == NULL ) return;
2768                         name = ADDRITEM_NAME(item);
2769                 }
2770         }
2771         if( name && parentNode ) {
2772                 /* Update node in tree view */
2773                 addressbook_change_node_name( node, name );
2774                 gtk_sctree_sort_node(ctree, parentNode);
2775                 gtk_ctree_expand( ctree, node );
2776                 gtk_sctree_select( GTK_SCTREE( ctree), node );
2777         }
2778 }
2779
2780 typedef enum {
2781         ADDRTREE_DEL_NONE,
2782         ADDRTREE_DEL_DATA,
2783         ADDRTREE_DEL_FOLDER_ONLY,
2784         ADDRTREE_DEL_FOLDER_ADDR
2785 } TreeItemDelType ;
2786
2787 /**
2788  * Delete an item from the tree widget.
2789  * \param data   Data passed in.
2790  * \param action Action.
2791  * \param widget Widget issuing callback.
2792  */
2793 static void addressbook_treenode_delete_cb(
2794                 gpointer data, guint action, GtkWidget *widget )
2795 {
2796         GtkCTree *ctree = GTK_CTREE(addrbook.ctree);
2797         GtkCTreeNode *node = NULL;
2798         AddressObject *obj;
2799         gchar *message;
2800         AlertValue aval;
2801         AddrBookBase *adbase;
2802         AddressCache *cache;
2803         AdapterDSource *ads = NULL;
2804         AddressInterface *iface = NULL;
2805         AddressDataSource *ds = NULL;
2806         gboolean remFlag = FALSE;
2807         TreeItemDelType delType;
2808
2809         if( ! addrbook.treeSelected ) return;
2810         node = addrbook.treeSelected;
2811         if( GTK_CTREE_ROW(node)->level == 1 ) return;
2812
2813         obj = gtk_ctree_node_get_row_data( ctree, node );
2814         g_return_if_fail(obj != NULL);
2815
2816         if( obj->type == ADDR_DATASOURCE ) {
2817                 ads = ADAPTER_DSOURCE(obj);
2818                 if( ads == NULL ) return;
2819                 ds = ads->dataSource;
2820                 if( ds == NULL ) return;
2821         }
2822         else {
2823                 /* Must be folder or something else */
2824                 ds = addressbook_find_datasource( node );
2825                 if( ds == NULL ) return;
2826
2827                 /* Only allow deletion from non-readOnly */
2828                 iface = ds->interface;
2829                 if( iface->readOnly ) {
2830                         /* Allow deletion of query results */
2831                         if( ! iface->externalQuery ) return;
2832                 }
2833         }
2834
2835         /* Confirm deletion */
2836         delType = ADDRTREE_DEL_NONE;
2837         if( obj->type == ADDR_ITEM_FOLDER ) {
2838                 if( iface->externalQuery ) {
2839                         message = g_strdup_printf( _(
2840                                 "Do you want to delete the query " \
2841                                 "results and addresses in '%s' ?" ),
2842                                 obj->name );
2843                         aval = alertpanel( _("Delete"), message,
2844                                 GTK_STOCK_CANCEL, "+"GTK_STOCK_DELETE, NULL );
2845                         g_free(message);
2846                         if( aval == G_ALERTALTERNATE ) {
2847                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2848                         }
2849                 }
2850                 else {
2851                         message = g_strdup_printf
2852                                 ( _( "Do you want to delete '%s' ?"
2853                                      "If you delete the folder only, the addresses it contains will be moved into the parent folder." ),
2854                                  obj->name );
2855                         aval = alertpanel( _("Delete folder"), message,
2856                                 GTK_STOCK_CANCEL, _("+Delete _folder only"), _("Delete folder and _addresses"));
2857                         g_free(message);
2858                         if( aval == G_ALERTALTERNATE ) {
2859                                 delType = ADDRTREE_DEL_FOLDER_ONLY;
2860                         }
2861                         else if( aval == G_ALERTOTHER ) {
2862                                 delType = ADDRTREE_DEL_FOLDER_ADDR;
2863                         }
2864                 }
2865         }
2866         else if( obj->type == ADDR_ITEM_GROUP ) {
2867                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2868                                             "The addresses it contains will not be lost."), obj->name);
2869                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
2870                                 "+" GTK_STOCK_DELETE, NULL);
2871                 g_free(message);
2872                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_FOLDER_ONLY;
2873         } else {
2874                 message = g_strdup_printf(_("Do you want to delete '%s'?\n"
2875                                             "The addresses it contains will be lost."), obj->name);
2876                 aval = alertpanel(_("Delete"), message, GTK_STOCK_CANCEL, 
2877                                 "+" GTK_STOCK_DELETE, NULL);
2878                 g_free(message);
2879                 if( aval == G_ALERTALTERNATE ) delType = ADDRTREE_DEL_DATA;
2880         }
2881         if( delType == ADDRTREE_DEL_NONE ) return;
2882
2883         /* Proceed with deletion */
2884         if( obj->type == ADDR_DATASOURCE ) {
2885                 /* Remove node from tree */
2886                 gtk_ctree_remove_node( ctree, node );
2887         
2888                 /* Remove data source. */
2889                 if( addrindex_index_remove_datasource( _addressIndex_, ds ) ) {
2890                         addrindex_free_datasource( ds );
2891                 }
2892                 return;
2893         }
2894
2895         /* Get reference to cache */
2896         adbase = ( AddrBookBase * ) ds->rawDataSource;
2897         if( adbase == NULL ) return;
2898         cache = adbase->addressCache;
2899
2900         /* Remove query results folder */
2901         if( iface->externalQuery ) {
2902                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2903                 ItemFolder *folder = adapter->itemFolder;
2904
2905                 adapter->itemFolder = NULL;
2906                 /*
2907                 g_print( "remove folder for ::%s::\n", obj->name );
2908                 g_print( "      folder name ::%s::\n", ADDRITEM_NAME(folder) );
2909                 g_print( "-------------- remove results\n" );
2910                 */
2911                 addrindex_remove_results( ds, folder );
2912                 /* g_print( "-------------- remove node\n" ); */
2913                 gtk_ctree_remove_node( ctree, node );
2914                 return;
2915         }
2916
2917         /* Code below is valid for regular address book deletion */
2918         if( obj->type == ADDR_ITEM_FOLDER ) {
2919                 AdapterFolder *adapter = ADAPTER_FOLDER(obj);
2920                 ItemFolder *item = adapter->itemFolder;
2921
2922                 if( delType == ADDRTREE_DEL_FOLDER_ONLY ) {
2923                         /* Remove folder only */
2924                         item = addrcache_remove_folder( cache, item );
2925                         if( item ) {
2926                                 addritem_free_item_folder( item );
2927                                 addressbook_move_nodes_up( ctree, node );
2928                                 remFlag = TRUE;
2929                         }
2930                 }
2931                 else if( delType == ADDRTREE_DEL_FOLDER_ADDR ) {
2932                         /* Remove folder and addresses */
2933                         item = addrcache_remove_folder_delete( cache, item );
2934                         if( item ) {
2935                                 addritem_free_item_folder( item );
2936                                 remFlag = TRUE;
2937                         }
2938                 }
2939         }
2940         else if( obj->type == ADDR_ITEM_GROUP ) {
2941                 AdapterGroup *adapter = ADAPTER_GROUP(obj);
2942                 ItemGroup *item = adapter->itemGroup;
2943
2944                 item = addrcache_remove_group( cache, item );
2945                 if( item ) {
2946                         addritem_free_item_group( item );
2947                         remFlag = TRUE;
2948                 }
2949         }
2950
2951         if( remFlag ) {
2952                 /* Remove node. */
2953                 gtk_ctree_remove_node(ctree, node );
2954         }
2955 }
2956
2957 static void addressbook_new_address_from_book_post_cb( ItemPerson *person )
2958 {
2959         if( person && addrbook.treeSelected == addrbook.opened ) {
2960                 person->status = ADD_ENTRY;
2961                 gtk_clist_unselect_all( GTK_CLIST(addrbook.clist) );
2962                 addressbook_folder_refresh_one_person(
2963                         GTK_CTREE(addrbook.clist), person );
2964         }
2965         addressbook_address_list_set_focus();
2966 }
2967
2968 static void addressbook_new_address_from_folder_post_cb( ItemPerson *person )
2969 {
2970         if( person && addrbook.treeSelected == addrbook.opened) {
2971                 person->status = ADD_ENTRY;
2972                 gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
2973                 addressbook_set_clist(
2974                         gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
2975                                 addrbook.opened),
2976                         TRUE);
2977         }
2978         addressbook_address_list_set_focus();
2979 }
2980
2981 /**
2982  * Label (a format string) that is used to name each folder.
2983  */
2984 static gchar *_queryFolderLabel_ = N_( "Search '%s'" );
2985
2986 /**
2987  * Search ctree widget callback function.
2988  * \param  pA Pointer to node.
2989  * \param  pB Pointer to data item being sought.
2990  * \return Zero (0) if folder found.
2991  */
2992 static int addressbook_treenode_find_folder_cb( gconstpointer pA, gconstpointer pB ) {
2993         AddressObject *aoA;
2994
2995         aoA = ( AddressObject * ) pA;
2996         if( aoA->type == ADDR_ITEM_FOLDER ) {
2997                 ItemFolder *folder, *fld;
2998
2999                 fld = ADAPTER_FOLDER(aoA)->itemFolder;
3000                 folder = ( ItemFolder * ) pB;
3001                 if( fld == folder ) return 0;   /* Found folder */
3002         }
3003         return 1;
3004 }
3005
3006 static ItemFolder * addressbook_setup_subf(
3007                 AddressDataSource *ds, gchar *title,
3008                 GtkCTreeNode *pNode )
3009 {
3010         AddrBookBase *adbase;
3011         AddressCache *cache;
3012         ItemFolder *folder;
3013         GtkCTree *ctree;
3014         GtkCTreeNode *nNode;
3015         gchar *name;
3016         AddressObjectType aoType = ADDR_NONE;
3017         GList *children;
3018         /* Setup a query */
3019         if( *title == '\0' || strlen( title ) < 1 ) return NULL;
3020
3021         if( ds && ds->type == ADDR_IF_LDAP ) {
3022 #if USE_LDAP
3023                 aoType = ADDR_LDAP_QUERY;
3024 #endif
3025         }
3026         else {
3027                 return NULL;
3028         }
3029
3030         ctree = GTK_CTREE(addrbook.ctree);
3031         /* Get reference to address cache */    
3032         adbase = ( AddrBookBase * ) ds->rawDataSource;
3033         cache = adbase->addressCache;
3034         
3035         if ((children = addrcache_get_list_folder(cache)) != NULL) {
3036                 GList *cur = children;
3037                 for (; cur; cur = cur->next) {
3038                         ItemFolder *child = (ItemFolder *) cur->data;
3039                         if (!strcmp2(ADDRITEM_NAME(child), title)) {
3040                                 nNode = gtk_ctree_find_by_row_data_custom(
3041                                         ctree, NULL, child,
3042                                         addressbook_treenode_find_folder_cb );
3043                                 if( nNode ) {
3044                                         addrindex_remove_results( ds, child );
3045                                         while( child->listPerson ) {
3046                                                 ItemPerson *item = ( ItemPerson * ) child->listPerson->data;
3047                                                 item = addrcache_remove_person( cache, item );
3048                                                 if( item ) {
3049                                                         addritem_free_item_person( item );
3050                                                         item = NULL;
3051                                                 }
3052                                         }
3053                                         gtk_sctree_select( GTK_SCTREE(ctree), nNode );
3054                                         addrbook.treeSelected = nNode;
3055                                 }       
3056                                 return child;
3057                         }
3058                 }
3059         }
3060         
3061         /* Create a folder */
3062         folder = addrcache_add_new_folder( cache, NULL );
3063         name = g_strdup_printf( "%s", title );
3064         addritem_folder_set_name( folder, name );
3065         addritem_folder_set_remarks( folder, "" );
3066         g_free( name );
3067
3068         /* Now let's see the folder */
3069         nNode = addressbook_node_add_folder( pNode, ds, folder, aoType );
3070         gtk_ctree_expand( ctree, pNode );
3071         if( nNode ) {
3072                 gtk_sctree_select( GTK_SCTREE(ctree), nNode );
3073                 addrbook.treeSelected = nNode;
3074                 return folder;
3075         }
3076         return NULL;
3077 }
3078
3079 static void addressbook_new_address_cb( gpointer data, guint action, GtkWidget *widget ) {
3080         AddressObject *pobj = NULL;
3081         AddressDataSource *ds = NULL;
3082         AddressBookFile *abf = NULL;
3083         debug_print("adding address\n");
3084         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
3085         if( pobj == NULL ) {
3086                 debug_print("no row data\n");
3087                 return;
3088         }
3089         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3090         if( ds == NULL ) {
3091                 debug_print("no datasource\n");
3092                 return;
3093         }
3094
3095         abf = ds->rawDataSource;
3096         if( abf == NULL ) {
3097                 g_print("no addressbook file\n");
3098                 return;
3099         }
3100
3101         if( pobj->type == ADDR_DATASOURCE ) {
3102                 if (ADAPTER_DSOURCE(pobj)->subType == ADDR_BOOK ||
3103                     ADAPTER_DSOURCE(pobj)->subType == ADDR_LDAP) {
3104                         ItemPerson *person;
3105                         ItemFolder *folder = NULL;
3106 #ifdef USE_LDAP
3107                         if (abf && abf->type == ADDR_IF_LDAP) {
3108                                 GtkCTreeNode *parentNode;
3109                                 ds = addressbook_find_datasource( GTK_CTREE_NODE( addrbook.treeSelected ) );
3110                                 if( ds == NULL ) return;
3111
3112                                 /* We must have a datasource that is an external interface */
3113                                 if( ! ds->interface->haveLibrary ) return;
3114                                 if( ! ds->interface->externalQuery ) return;
3115
3116                                 if( pobj->type == ADDR_ITEM_FOLDER ) {
3117                                         parentNode = GTK_CTREE_ROW(GTK_CTREE_NODE( addrbook.treeSelected ) )->parent;
3118                                 }
3119                                 else {
3120                                         parentNode = GTK_CTREE_NODE( addrbook.treeSelected );
3121                                 }
3122                                 folder = addressbook_setup_subf( ds, _("New Contacts"), parentNode );
3123
3124                                 pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
3125                                 ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3126                                 abf = ds->rawDataSource;
3127                         }
3128 #endif
3129                         person = addressbook_edit_person( abf, folder, NULL, FALSE,
3130                                                                   addrbook.editaddress_vbox,
3131                                                                   addressbook_new_address_from_book_post_cb,
3132                                                                   TRUE );
3133 #ifdef USE_LDAP
3134                         if (abf && abf->type == ADDR_IF_LDAP) {
3135                                 LdapServer *server = ds->rawDataSource;
3136                                 ldapsvr_set_modified(server, TRUE);
3137                                 ldapsvr_update_book(server, NULL);
3138                                 if (server->retVal != LDAPRC_SUCCESS) {
3139                                         alertpanel( _("Add address(es)"),
3140                                                 addressbook_err2string(_lutErrorsLDAP_, server->retVal),
3141                                                 GTK_STOCK_CLOSE, NULL, NULL );
3142                                         server->retVal = LDAPRC_SUCCESS;
3143                                         return;
3144                                 }
3145                         }
3146 #endif
3147                         if (prefs_common.addressbook_use_editaddress_dialog)
3148                                 addressbook_new_address_from_book_post_cb( person );
3149                 }
3150         }
3151         else if( pobj->type == ADDR_ITEM_FOLDER ) {
3152                 /* New address */
3153                 ItemFolder *folder = ADAPTER_FOLDER(pobj)->itemFolder;
3154                 ItemPerson *person;
3155 #ifdef USE_LDAP
3156                 if (abf && abf->type == ADDR_IF_LDAP) {
3157                         GtkCTreeNode *parentNode;
3158                         ds = addressbook_find_datasource( GTK_CTREE_NODE( addrbook.treeSelected ) );
3159                         if( ds == NULL ) return;
3160
3161                         /* We must have a datasource that is an external interface */
3162                         if( ! ds->interface->haveLibrary ) return;
3163                         if( ! ds->interface->externalQuery ) return;
3164
3165                         if( pobj->type == ADDR_ITEM_FOLDER ) {
3166                                 parentNode = GTK_CTREE_ROW(GTK_CTREE_NODE( addrbook.treeSelected ) )->parent;
3167                         }
3168                         else {
3169                                 parentNode = GTK_CTREE_NODE( addrbook.treeSelected );
3170                         }
3171                         folder = addressbook_setup_subf( ds, _("New Contacts"), parentNode );
3172                         if (!folder)
3173                                 return;
3174                         pobj = gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree), addrbook.treeSelected);
3175                         ds = addressbook_find_datasource( GTK_CTREE_NODE(addrbook.treeSelected) );
3176                         abf = ds->rawDataSource;
3177                 }
3178 #endif
3179                 person = addressbook_edit_person( abf, folder, NULL, FALSE,
3180                                                           addrbook.editaddress_vbox,
3181                                                           addressbook_new_address_from_folder_post_cb,
3182                                                           TRUE );
3183 #ifdef USE_LDAP
3184                 if (abf && abf->type == ADDR_IF_LDAP) {
3185                         LdapServer *server = ds->rawDataSource;
3186                         ldapsvr_set_modified(server, TRUE);
3187                         ldapsvr_update_book(server, NULL);
3188                         if (server->retVal != LDAPRC_SUCCESS) {
3189                                 alertpanel( _("Add address(es)"),
3190                                                 addressbook_err2string(_lutErrorsLDAP_, server->retVal),
3191                                         GTK_STOCK_CLOSE, NULL, NULL );
3192                                 return;
3193                         }
3194                 }
3195 #endif
3196                 if (prefs_common.addressbook_use_editaddress_dialog)
3197                         addressbook_new_address_from_folder_post_cb( person );
3198         }
3199         else if( pobj->type == ADDR_ITEM_GROUP ) {
3200                 /* New address in group */
3201                 ItemGroup *group = ADAPTER_GROUP(pobj)->itemGroup;
3202                 if( addressbook_edit_group( abf, NULL, group ) == NULL ) return;
3203                 if (addrbook.treeSelected == addrbook.opened) {
3204                         /* Change node name in tree. */
3205                         addressbook_change_node_name( addrbook.treeSelected, ADDRITEM_NAME(group) );
3206                         gtk_sctree_select( GTK_SCTREE(addrbook.ctree), addrbook.opened );
3207                         addressbook_set_clist(
3208                                 gtk_ctree_node_get_row_data(GTK_CTREE(addrbook.ctree),
3209                                         addrbook.opened),
3210                                 TRUE);
3211                 }
3212         }
3213 }
3214
3215 /**
3216  * Search for specified child group node in address index tree.
3217  * \param parent Parent node.
3218  * \param group  Group to find.
3219  */
3220 static GtkCTreeNode *addressbook_find_group_node( GtkCTreeNode *parent, ItemGroup *group ) {
3221         GtkCTreeNode *node = NULL;
3222         GtkCTreeRow *currRow;
3223
3224         currRow = GTK_CTREE_ROW( parent );
3225         if( currRow ) {
3226                 node = currRow->children;
3227                 while( node ) {
3228                         AddressObject *obj;
3229
3230                         obj = gtk_ctree_node_get_row_data( GTK_CTREE(addrbook.ctree), node );
3231                         if( obj->type == ADDR_ITEM_GROUP ) {
3232                                 ItemGroup *g = ADAPTER_GROUP(obj)->itemGroup;
3233                                 if( g == group ) return node;
3234                         }
3235                         currRow = GTK_CTREE_ROW(node);
3236                         node = currRow->sibling;
3237                 }