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