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